]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / Variable.java
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java
new file mode 100755 (executable)
index 0000000..c48232c
--- /dev/null
@@ -0,0 +1,79 @@
+package org.simantics.scl.compiler.elaboration.expressions;\r
+\r
+import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
+import org.simantics.scl.compiler.internal.codegen.references.IVal;\r
+import org.simantics.scl.compiler.types.Type;\r
+import org.simantics.scl.compiler.types.Types;\r
+import org.simantics.scl.compiler.types.exceptions.UnificationException;\r
+import org.simantics.scl.compiler.types.util.Typed;\r
+\r
+public class Variable implements Typed {\r
+    public static final Variable[] EMPTY_ARRAY = new Variable[0];\r
+    \r
+    String name;\r
+    Type type;\r
+    transient private IVal val;\r
+\r
+    public Variable(String name) {\r
+        this.name = name;\r
+    }\r
+\r
+    public Variable(String name, Type type) {\r
+        this.name = name;\r
+        this.type = type;\r
+    }\r
+\r
+    @Override\r
+    public Type getType() {\r
+        return type;\r
+    }\r
+\r
+    public String getName() {\r
+        return name;\r
+    }\r
+    \r
+    public void setType(Type type) {\r
+        if(type == null)\r
+            throw new NullPointerException();\r
+        if(this.type != null)\r
+            try {\r
+                Types.unify(type, this.type);\r
+            } catch(UnificationException e) {\r
+                throw new InternalCompilerError(e.getMessage());\r
+            }\r
+        else\r
+            this.type = type;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return name; // + "$" + hashCode();\r
+    }\r
+    \r
+    public void setVal(IVal val) {\r
+        this.val = val;\r
+        val.setLabel(name);\r
+    }\r
+    \r
+    public IVal getVal() {\r
+        if(val == null)\r
+            throw new InternalCompilerError("Variable " + name + " (with type " + type + ") is not given value anywhere.");\r
+        return val;\r
+    }\r
+\r
+    public Variable copy() {\r
+        return new Variable(name, type);\r
+    }\r
+\r
+    public void setName(String name) {\r
+        this.name = name;\r
+    }\r
+\r
+    public static Variable[] concat(Variable[] a,\r
+            Variable[] b) {\r
+        Variable[] result = new Variable[a.length + b.length];\r
+        System.arraycopy(a, 0, result, 0, a.length);\r
+        System.arraycopy(b, 0, result, a.length, b.length);\r
+        return result;\r
+    }\r
+}\r