]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/expressions/Variable.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / expressions / Variable.java
1 package org.simantics.scl.compiler.elaboration.expressions;\r
2 \r
3 import org.simantics.scl.compiler.common.exceptions.InternalCompilerError;\r
4 import org.simantics.scl.compiler.internal.codegen.references.IVal;\r
5 import org.simantics.scl.compiler.types.Type;\r
6 import org.simantics.scl.compiler.types.Types;\r
7 import org.simantics.scl.compiler.types.exceptions.UnificationException;\r
8 import org.simantics.scl.compiler.types.util.Typed;\r
9 \r
10 public class Variable implements Typed {\r
11     public static final Variable[] EMPTY_ARRAY = new Variable[0];\r
12     \r
13     String name;\r
14     Type type;\r
15     transient private IVal val;\r
16 \r
17     public Variable(String name) {\r
18         this.name = name;\r
19     }\r
20 \r
21     public Variable(String name, Type type) {\r
22         this.name = name;\r
23         this.type = type;\r
24     }\r
25 \r
26     @Override\r
27     public Type getType() {\r
28         return type;\r
29     }\r
30 \r
31     public String getName() {\r
32         return name;\r
33     }\r
34     \r
35     public void setType(Type type) {\r
36         if(type == null)\r
37             throw new NullPointerException();\r
38         if(this.type != null)\r
39             try {\r
40                 Types.unify(type, this.type);\r
41             } catch(UnificationException e) {\r
42                 throw new InternalCompilerError(e.getMessage());\r
43             }\r
44         else\r
45             this.type = type;\r
46     }\r
47 \r
48     @Override\r
49     public String toString() {\r
50         return name; // + "$" + hashCode();\r
51     }\r
52     \r
53     public void setVal(IVal val) {\r
54         this.val = val;\r
55         val.setLabel(name);\r
56     }\r
57     \r
58     public IVal getVal() {\r
59         if(val == null)\r
60             throw new InternalCompilerError("Variable " + name + " (with type " + type + ") is not given value anywhere.");\r
61         return val;\r
62     }\r
63 \r
64     public Variable copy() {\r
65         return new Variable(name, type);\r
66     }\r
67 \r
68     public void setName(String name) {\r
69         this.name = name;\r
70     }\r
71 \r
72     public static Variable[] concat(Variable[] a,\r
73             Variable[] b) {\r
74         Variable[] result = new Variable[a.length + b.length];\r
75         System.arraycopy(a, 0, result, 0, a.length);\r
76         System.arraycopy(b, 0, result, a.length, b.length);\r
77         return result;\r
78     }\r
79 }\r