From b5e6f765a2adf831b2caac2c4ee3250ed13d82a0 Mon Sep 17 00:00:00 2001
From: Mick Jordan <mick.jordan@oracle.com>
Date: Fri, 30 Jan 2015 09:51:26 -0800
Subject: [PATCH] fix names attribute printing for language objects

---
 .../r/nodes/builtin/base/Attributes.java      | 29 +++++++++++++++----
 .../nodes/builtin/base/PrettyPrinterNode.java | 17 +++++++----
 2 files changed, 34 insertions(+), 12 deletions(-)

diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Attributes.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Attributes.java
index 439e49de27..2ee249b9f7 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Attributes.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/Attributes.java
@@ -24,6 +24,8 @@ package com.oracle.truffle.r.nodes.builtin.base;
 
 import static com.oracle.truffle.r.runtime.RBuiltinKind.*;
 
+import java.util.*;
+
 import com.oracle.truffle.api.dsl.*;
 import com.oracle.truffle.api.utilities.*;
 import com.oracle.truffle.r.nodes.builtin.*;
@@ -45,9 +47,9 @@ public abstract class Attributes extends RBuiltinNode {
     }
 
     @Specialization(guards = "hasAttributes")
-    protected RList attributes(RAbstractContainer container) {
+    protected Object attributes(RAbstractContainer container) {
         controlVisibility();
-        return createResult(container);
+        return createResult(container, container instanceof RLanguage);
     }
 
     /**
@@ -62,22 +64,29 @@ public abstract class Attributes extends RBuiltinNode {
             if (!hasAttributesRA((RAttributable) object)) {
                 return RNull.instance;
             } else {
-                return createResult((RAttributable) object);
+                return createResult((RAttributable) object, false);
             }
         } else {
             throw RError.nyi(getEncapsulatingSourceSection(), ": object cannot be attributed");
         }
     }
 
-    private RList createResult(RAttributable attributable) {
+    /**
+     * {@code language} objects behave differently regarding "names"; they don't get included.
+     */
+    private Object createResult(RAttributable attributable, boolean ignoreNames) {
         RAttributes attributes = attributable.getAttributes();
         int size = attributes.size();
         String[] names = new String[size];
         Object[] values = new Object[size];
         int z = 0;
         for (RAttribute attr : attributes) {
-            names[z] = attr.getName();
-            if (names[z].equals(RRuntime.ROWNAMES_ATTR_KEY)) {
+            String name = attr.getName();
+            if (ignoreNames && name.equals(RRuntime.NAMES_ATTR_KEY)) {
+                continue;
+            }
+            names[z] = name;
+            if (name.equals(RRuntime.ROWNAMES_ATTR_KEY)) {
                 rownamesBranch.enter();
                 values[z] = Attr.getFullRowNames(attr.getValue());
             } else {
@@ -85,6 +94,14 @@ public abstract class Attributes extends RBuiltinNode {
             }
             ++z;
         }
+        if (ignoreNames && z != names.length) {
+            if (z == 0) {
+                return RNull.instance;
+            } else {
+                names = Arrays.copyOfRange(names, 0, names.length - 1);
+                values = Arrays.copyOfRange(values, 0, values.length - 1);
+            }
+        }
         RList result = RDataFactory.createList(values);
         result.setNames(RDataFactory.createStringVector(names, true));
         return result;
diff --git a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/PrettyPrinterNode.java b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/PrettyPrinterNode.java
index 6e37717aaf..61419431eb 100644
--- a/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/PrettyPrinterNode.java
+++ b/com.oracle.truffle.r.nodes.builtin/src/com/oracle/truffle/r/nodes/builtin/base/PrettyPrinterNode.java
@@ -248,13 +248,13 @@ public abstract class PrettyPrinterNode extends RNode {
             }
             string = source;
         }
-        return printValueAndAttributes(string, operand);
+        return printValueAndAttributes(string, operand, false);
     }
 
     @TruffleBoundary
     @Specialization
     protected String prettyPrint(REnvironment operand, Object listElementName, byte quote, byte right) {
-        return printValueAndAttributes(operand.toString(), operand);
+        return printValueAndAttributes(operand.toString(), operand, false);
     }
 
     @TruffleBoundary
@@ -281,13 +281,13 @@ public abstract class PrettyPrinterNode extends RNode {
     @TruffleBoundary
     @Specialization
     protected String prettyPrintSymbol(RSymbol operand, Object listElementName, byte quote, byte right) {
-        return printValueAndAttributes(operand.getName(), operand);
+        return printValueAndAttributes(operand.getName(), operand, false);
     }
 
     @TruffleBoundary
     @Specialization
     protected String prettyPrintExternalPtr(RExternalPtr operand, Object listElementName, byte quote, byte right) {
-        return printValueAndAttributes(String.format("<pointer: %#x>", operand.value), operand);
+        return printValueAndAttributes(String.format("<pointer: %#x>", operand.value), operand, false);
     }
 
     @TruffleBoundary
@@ -303,7 +303,7 @@ public abstract class PrettyPrinterNode extends RNode {
     @TruffleBoundary
     @Specialization
     protected String prettyPrintLanguage(RLanguage language, Object listElementName, byte quote, byte right) {
-        return printValueAndAttributes(prettyPrintLanguageInternal(language), language);
+        return printValueAndAttributes(prettyPrintLanguageInternal(language), language, true);
     }
 
     private static String prettyPrintLanguageInternal(RLanguage language) {
@@ -393,14 +393,19 @@ public abstract class PrettyPrinterNode extends RNode {
 
     /**
      * Encapsulates the printing of the value and attributes for {@link RAttributable} types.
+     * 
+     * @param ignoreNames TODO
      */
-    private String printValueAndAttributes(String value, RAttributable object) {
+    private String printValueAndAttributes(String value, RAttributable object, boolean ignoreNames) {
         RAttributes attributes = object.getAttributes();
         if (attributes == null) {
             return value;
         } else {
             StringBuilder builder = new StringBuilder(value);
             for (RAttribute attr : attributes) {
+                if (ignoreNames && attr.getName().equals(RRuntime.NAMES_ATTR_KEY)) {
+                    continue;
+                }
                 printAttribute(builder, attr);
             }
             return builderToString(builder);
-- 
GitLab