]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/elaboration/modules/SCLValue.java
Minor refactorings related to SCL constructors
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / elaboration / modules / SCLValue.java
1 package org.simantics.scl.compiler.elaboration.modules;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.scl.compiler.common.names.Name;
7 import org.simantics.scl.compiler.common.precedence.Precedence;
8 import org.simantics.scl.compiler.constants.Constant;
9 import org.simantics.scl.compiler.elaboration.contexts.SimplificationContext;
10 import org.simantics.scl.compiler.elaboration.expressions.Expression;
11 import org.simantics.scl.compiler.elaboration.macros.MacroRule;
12 import org.simantics.scl.compiler.errors.Locations;
13 import org.simantics.scl.compiler.internal.codegen.references.IVal;
14 import org.simantics.scl.compiler.internal.codegen.utils.TransientClassBuilder;
15 import org.simantics.scl.compiler.types.Type;
16 import org.simantics.scl.compiler.types.util.Typed;
17
18 /**
19  * Information about SCL values that is needed in parsing 
20  * and elaboration phases.
21  * @author Hannu Niemistö
22  */
23 public final class SCLValue implements Typed {
24     private static final int SIMPLIFIED = 1;
25     private static final int INLINE_IN_SIMPLIFICATION = 2;
26     private static final int FLAG_MASK = 0xffff;
27     
28     private Name name;
29     private Type type;
30     private Precedence precedence = Precedence.DEFAULT;
31     private IVal value;
32     private Expression expression;
33     private MacroRule macroRule;
34     private int flags = 0;
35     private ArrayList<SCLValueProperty> properties = new ArrayList<SCLValueProperty>(2);
36     public String documentation;
37     public long definitionLocation = Locations.NO_LOCATION;
38     public String[] parameterNames;
39     
40     public SCLValue(Name name) {
41         this.name = name;
42     }
43     
44     public SCLValue(Name name, Constant value) {
45         this(name);
46         setValue(value);
47     }
48
49     public Name getName() {
50         return name;
51     }
52     
53     public Type getType() {
54         return type;
55     }
56     
57     public void setType(Type type) {
58         this.type = type;
59     }
60     
61     public void addProperty(SCLValueProperty property) {
62         properties.add(property);
63     }
64     
65     public List<SCLValueProperty> getProperties() {
66         return properties;
67     }
68     
69     public Precedence getPrecedence() {
70         return precedence;
71     }
72     
73     public void setPrecedence(Precedence precedence) {
74         this.precedence = precedence;
75     }
76     
77     public IVal getValue() {
78         return value;
79     }
80     
81     public void setValue(IVal value) {
82         this.value = value;
83         if(type == null)
84             type = value.getType();
85     }
86     
87     public Expression getExpression() {
88         return expression;
89     }
90     
91     public Expression getSimplifiedExpression(SimplificationContext context) {
92         if(expression != null && (flags & SIMPLIFIED) == 0) {
93             //System.out.println("Simplify: " + name);
94             //System.out.println("BEFORE: " + expression);
95             expression = expression.simplify(context);
96             //System.out.println("AFTER: " + expression);
97             flags |= SIMPLIFIED;
98         }
99         return expression;
100     }
101     
102     public void setExpression(Expression expression) {
103         this.expression = expression;
104     }
105     
106     public MacroRule getMacroRule() {
107         return macroRule;
108     }
109     
110     public void setMacroRule(MacroRule macroRule) {
111         this.macroRule = macroRule;
112     }
113     
114     @Override
115     public String toString() {
116         return name.toString();
117     }
118     
119     private void setFlag(int flagMask, boolean value) {
120         if(value)
121             flags |= flagMask;
122         else
123             flags &= FLAG_MASK - flagMask; 
124     }
125     
126     public void setSimplified(boolean value) {
127         setFlag(SIMPLIFIED, value);
128     }
129     
130     public void setInlineInSimplification(boolean value) {
131         setFlag(INLINE_IN_SIMPLIFICATION, value);
132     }
133     
134     public boolean getInlineInSimplification() {
135         return (flags & INLINE_IN_SIMPLIFICATION) != 0; 
136     }
137     
138     public Object realizeValue(TransientClassBuilder classLoader) {
139         return getValue().realizeValue(classLoader);
140     }
141
142         public boolean isPrivate() {
143                 for(SCLValueProperty property : properties)
144                         if(property == PrivateProperty.INSTANCE)
145                                 return true;
146                 return false;
147         }
148         
149         public String isDeprecated() {
150             for(SCLValueProperty property : properties)
151             if(property instanceof DeprecatedProperty)
152                 return ((DeprecatedProperty)property).description;
153         return null;
154         }
155
156         public void setDocumentation(String documentation) {
157                 this.documentation = documentation;
158         }
159         
160         public String getDocumentation() {
161                 return documentation;
162         }
163
164     public boolean isPrivateOrDerived() {
165         for(SCLValueProperty property : properties)
166             if(property == PrivateProperty.INSTANCE || property == DerivedProperty.INSTANCE)
167                 return true;
168         return false;
169     }
170 }