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