From 32d88206f156208af97b69b7393113a243415413 Mon Sep 17 00:00:00 2001
From: Adam Welc <adam.welc@oracle.com>
Date: Fri, 20 Mar 2015 20:59:28 -0700
Subject: [PATCH] Consolidate attribute-related functionality in
 RAbstractContainer interface.

---
 .../truffle/r/runtime/data/RDataFrame.java    | 20 +++++++++++++
 .../truffle/r/runtime/data/RExpression.java   | 22 ++++++++++++++
 .../truffle/r/runtime/data/RFactor.java       | 20 +++++++++++++
 .../truffle/r/runtime/data/RLanguage.java     | 29 +++++++++++++++----
 .../truffle/r/runtime/data/RPairList.java     | 27 +++++++++++++++--
 .../truffle/r/runtime/data/RSequence.java     | 28 ++++++++++++++++--
 .../data/closures/RToVectorClosure.java       | 22 ++++++++++++++
 .../data/model/RAbstractContainer.java        |  8 +++++
 .../r/runtime/data/model/RAbstractVector.java |  6 ----
 9 files changed, 165 insertions(+), 17 deletions(-)

diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RDataFrame.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RDataFrame.java
index 5b83186b05..bc87049e1b 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RDataFrame.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RDataFrame.java
@@ -97,6 +97,11 @@ public final class RDataFrame implements RShareable, RAbstractContainer {
         return null;
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        Utils.nyi("data frame's dimensions need to be set using builtins");
+    }
+
     @Override
     public Class<?> getElementClass() {
         return RDataFrame.class;
@@ -121,17 +126,32 @@ public final class RDataFrame implements RShareable, RAbstractContainer {
         return vector.getNames(attrProfiles);
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        vector.setNames(newNames);
+    }
+
     @Override
     public RList getDimNames() {
         Utils.nyi("data frame's dimnames needs to be obtained using builtins");
         return null;
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        vector.setDimNames(newDimNames);
+    }
+
     @Override
     public Object getRowNames(RAttributeProfiles attrProfiles) {
         return vector.getRowNames(attrProfiles);
     }
 
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        vector.setRowNames(rowNames);
+    }
+
     @Override
     public RStringVector getClassHierarchy() {
         return vector.getClassHierarchy();
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RExpression.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RExpression.java
index b886be702f..d6e0c61099 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RExpression.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RExpression.java
@@ -69,6 +69,11 @@ public class RExpression implements RShareable, RAbstractContainer {
         return data.getDimensions();
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        data.setDimensions(newDimensions);
+    }
+
     public Class<?> getElementClass() {
         return RExpression.class;
     }
@@ -81,19 +86,36 @@ public class RExpression implements RShareable, RAbstractContainer {
         return data.getDataAtAsObject(index);
     }
 
+    @Override
     public RStringVector getNames(RAttributeProfiles attrProfiles) {
         return data.getNames(attrProfiles);
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        data.setNames(newNames);
+    }
+
+    @Override
     public RList getDimNames() {
         return data.getDimNames();
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        data.setDimNames(newDimNames);
+    }
+
     @Override
     public Object getRowNames(RAttributeProfiles attrProfiles) {
         return data.getRowNames(attrProfiles);
     }
 
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        data.setRowNames(rowNames);
+    }
+
     public RStringVector getClassHierarchy() {
         return data.getClassHierarchy();
     }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RFactor.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RFactor.java
index 89dda737a0..243cc4483c 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RFactor.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RFactor.java
@@ -98,6 +98,11 @@ public final class RFactor implements RShareable, RAbstractContainer {
         return vector.getDimensions();
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        vector.setDimensions(newDimensions);
+    }
+
     @Override
     public Class<?> getElementClass() {
         return RFactor.class;
@@ -122,16 +127,31 @@ public final class RFactor implements RShareable, RAbstractContainer {
         return vector.getNames(attrProfiles);
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        vector.setNames(newNames);
+    }
+
     @Override
     public RList getDimNames() {
         return vector.getDimNames();
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        vector.setDimNames(newDimNames);
+    }
+
     @Override
     public Object getRowNames(RAttributeProfiles attrProfiles) {
         return vector.getRowNames(attrProfiles);
     }
 
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        vector.setRowNames(rowNames);
+    }
+
     @Override
     public RStringVector getClassHierarchy() {
         return vector.getClassHierarchy();
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RLanguage.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RLanguage.java
index c7f1926a1b..cb3933d8e7 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RLanguage.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RLanguage.java
@@ -72,8 +72,7 @@ public class RLanguage extends RLanguageRep implements RAbstractContainer, RAttr
     }
 
     public boolean isComplete() {
-        assert false;
-        return false;
+        throw RInternalError.shouldNotReachHere();
     }
 
     public int getLength() {
@@ -93,18 +92,21 @@ public class RLanguage extends RLanguageRep implements RAbstractContainer, RAttr
         return null;
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        throw RInternalError.unimplemented();
+    }
+
     public Class<?> getElementClass() {
         return RLanguage.class;
     }
 
     public RVector materializeNonSharedVector() {
-        assert false;
-        return null;
+        throw RInternalError.shouldNotReachHere();
     }
 
     public RShareable materializeToShareable() {
-        assert false;
-        return null;
+        throw RInternalError.shouldNotReachHere();
     }
 
     public Object getDataAtAsObject(int index) {
@@ -116,16 +118,31 @@ public class RLanguage extends RLanguageRep implements RAbstractContainer, RAttr
         return (RStringVector) getAttr(attrProfiles, RRuntime.NAMES_ATTR_KEY);
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        setAttr(RRuntime.NAMES_ATTR_KEY, newNames);
+    }
+
     @Override
     public RList getDimNames() {
         return (RList) getAttr(localAttrProfiles, RRuntime.DIMNAMES_ATTR_KEY);
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        setAttr(RRuntime.DIMNAMES_ATTR_KEY, newDimNames);
+    }
+
     @Override
     public Object getRowNames(RAttributeProfiles attrProfiles) {
         return getAttr(attrProfiles, RRuntime.ROWNAMES_ATTR_KEY);
     }
 
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        setAttr(RRuntime.ROWNAMES_ATTR_KEY, rowNames);
+    }
+
     public RStringVector getClassHierarchy() {
         return RDataFactory.createStringVector(RRuntime.CLASS_LANGUAGE);
     }
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java
index acd47e767a..13f963239b 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RPairList.java
@@ -129,6 +129,11 @@ public class RPairList extends RAttributeStorage implements RAttributable, RAbst
         return new int[]{1};
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        throw RInternalError.shouldNotReachHere();
+    }
+
     public Class<?> getElementClass() {
         return null;
     }
@@ -153,6 +158,7 @@ public class RPairList extends RAttributeStorage implements RAttributable, RAbst
         return pl.car;
     }
 
+    @Override
     public RStringVector getNames(RAttributeProfiles attrProfiles) {
         int l = getLength();
         String[] data = new String[l];
@@ -173,12 +179,29 @@ public class RPairList extends RAttributeStorage implements RAttributable, RAbst
         return RDataFactory.createStringVector(data, complete);
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        throw RInternalError.shouldNotReachHere();
+    }
+
+    @Override
     public RList getDimNames() {
-        return null;
+        throw RInternalError.shouldNotReachHere();
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        throw RInternalError.shouldNotReachHere();
+    }
+
+    @Override
     public Object getRowNames(RAttributeProfiles attrProfiles) {
-        return null;
+        throw RInternalError.shouldNotReachHere();
+    }
+
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        throw RInternalError.shouldNotReachHere();
     }
 
     public RStringVector getClassHierarchy() {
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RSequence.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RSequence.java
index 70573b296e..a8eb66ce8b 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RSequence.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RSequence.java
@@ -50,6 +50,12 @@ public abstract class RSequence extends RBounded implements RAbstractVector {
         return null;
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        // should only be used on materialized sequence
+        throw RInternalError.shouldNotReachHere();
+    }
+
     public final RVector createVector() {
         return internalCreateVector();
     }
@@ -76,21 +82,37 @@ public abstract class RSequence extends RBounded implements RAbstractVector {
         return null;
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        // should only be used on materialized sequence
+        throw RInternalError.shouldNotReachHere();
+    }
+
     @Override
     public final RList getDimNames() {
         return null;
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        // should only be used on materialized sequence
+        throw RInternalError.shouldNotReachHere();
+    }
+
     @Override
     public final Object getRowNames(RAttributeProfiles attrProfiles) {
         return RNull.instance;
     }
 
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        // should only be used on materialized sequence
+        throw RInternalError.shouldNotReachHere();
+    }
+
     @Override
     public final RAttributes initAttributes() {
-        // TODO implement
-        assert false;
-        return null;
+        throw RInternalError.shouldNotReachHere();
     }
 
     @Override
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToVectorClosure.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToVectorClosure.java
index e977dd5da3..c632afb30b 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToVectorClosure.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToVectorClosure.java
@@ -43,14 +43,21 @@ public abstract class RToVectorClosure implements RAbstractVector {
         return vector.isComplete();
     }
 
+    @Override
     public boolean hasDimensions() {
         return vector.hasDimensions();
     }
 
+    @Override
     public int[] getDimensions() {
         return vector.getDimensions();
     }
 
+    @Override
+    public void setDimensions(int[] newDimensions) {
+        vector.setDimensions(newDimensions);
+    }
+
     @Override
     public final void verifyDimensions(int[] newDimensions, SourceSection sourceSection) {
         vector.verifyDimensions(newDimensions, sourceSection);
@@ -61,16 +68,31 @@ public abstract class RToVectorClosure implements RAbstractVector {
         return vector.getNames(attrProfiles);
     }
 
+    @Override
+    public void setNames(RStringVector newNames) {
+        vector.setNames(newNames);
+    }
+
     @Override
     public RList getDimNames() {
         return vector.getDimNames();
     }
 
+    @Override
+    public void setDimNames(RList newDimNames) {
+        vector.setDimNames(newDimNames);
+    }
+
     @Override
     public Object getRowNames(RAttributeProfiles attrProfiles) {
         return vector.getRowNames(attrProfiles);
     }
 
+    @Override
+    public void setRowNames(RAbstractVector rowNames) {
+        vector.setRowNames(rowNames);
+    }
+
     @Override
     public RAttributes initAttributes() {
         return vector.initAttributes();
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractContainer.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractContainer.java
index 0e0a46a421..2c48797863 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractContainer.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractContainer.java
@@ -34,6 +34,8 @@ public interface RAbstractContainer extends RAttributable, RClassHierarchy, RTyp
 
     int[] getDimensions();
 
+    void setDimensions(int[] newDimensions);
+
     Class<?> getElementClass();
 
     RVector materializeNonSharedVector();
@@ -44,10 +46,16 @@ public interface RAbstractContainer extends RAttributable, RClassHierarchy, RTyp
 
     RStringVector getNames(RAttributeProfiles attrProfiles);
 
+    void setNames(RStringVector newNames);
+
     RList getDimNames();
 
+    void setDimNames(RList newDimNames);
+
     Object getRowNames(RAttributeProfiles attrProfiles);
 
+    void setRowNames(RAbstractVector rowNames);
+
     /**
      * Returns {@code true} if and only if the value has a {@code class} attribute added explicitly.
      * When {@code true}, it is possible to call {@link RClassHierarchy#getClassHierarchy()}.
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractVector.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractVector.java
index d118ff88be..6455555482 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractVector.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractVector.java
@@ -28,10 +28,6 @@ import com.oracle.truffle.r.runtime.data.*;
 
 public interface RAbstractVector extends RAbstractContainer {
 
-    boolean hasDimensions();
-
-    int[] getDimensions();
-
     /**
      * Creates a copy of the vector. This copies all of the contained data as well. If the data in
      * the vector is to be updated upon copying, the corresponding {@code copyResetData()} method
@@ -58,8 +54,6 @@ public interface RAbstractVector extends RAbstractContainer {
 
     RVector materialize();
 
-    RList getDimNames();
-
     boolean isMatrix();
 
     boolean isArray();
-- 
GitLab