annotate dmd/interpret/Util.d @ 79:43073c7c7769

updated to 2.035 also implemented a few missing functions still crashes in Import.importAll though
author Trass3r
date Mon, 30 Aug 2010 03:57:51 +0200
parents 7e0d548de9e6
children be2ab491772e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
63
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
1 module dmd.interpret.Util;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
2
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
3 import dmd.StructDeclaration;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
4 import dmd.Expression;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
5 import dmd.InterState;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
6 import dmd.ArrayTypes;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
7 import dmd.GlobalExpressions;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
8 import dmd.TOK;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
9 import dmd.AssocArrayLiteralExp;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
10 import dmd.IntegerExp;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
11 import dmd.Type;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
12 import dmd.Declaration;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
13 import dmd.Loc;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
14 import dmd.ArrayLiteralExp;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
15 import dmd.TypeAArray;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
16 import dmd.TypeSArray;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
17 import dmd.STC;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
18 import dmd.SymbolDeclaration;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
19 import dmd.StructLiteralExp;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
20 import dmd.VarDeclaration;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
21 import dmd.Util;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
22
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
23 Expression interpret_aaLen(InterState istate, Expressions arguments)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
24 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
25 if (!arguments || arguments.dim != 1)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
26 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
27 Expression earg = cast(Expression)arguments.data[0];
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
28 earg = earg.interpret(istate);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
29 if (earg is EXP_CANT_INTERPRET)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
30 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
31 if (earg.op != TOKassocarrayliteral)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
32 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
33 AssocArrayLiteralExp aae = cast(AssocArrayLiteralExp)earg;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
34 Expression e = new IntegerExp(aae.loc, aae.keys.dim, Type.tsize_t);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
35 return e;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
36 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
37
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
38 Expression interpret_aaKeys(InterState istate, Expressions arguments)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
39 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
40 version (LOG) {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
41 printf("interpret_aaKeys()\n");
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
42 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
43 if (!arguments || arguments.dim != 2)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
44 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
45 Expression earg = cast(Expression)arguments.data[0];
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
46 earg = earg.interpret(istate);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
47 if (earg is EXP_CANT_INTERPRET)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
48 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
49 if (earg.op != TOKassocarrayliteral)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
50 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
51 AssocArrayLiteralExp aae = cast(AssocArrayLiteralExp)earg;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
52 Expression e = new ArrayLiteralExp(aae.loc, aae.keys);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
53 Type elemType = (cast(TypeAArray)aae.type).index;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
54 e.type = new TypeSArray(elemType, new IntegerExp(arguments ? arguments.dim : 0));
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
55 return e;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
56 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
57
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
58 Expression interpret_aaValues(InterState istate, Expressions arguments)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
59 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
60 //printf("interpret_aaValues()\n");
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
61 if (!arguments || arguments.dim != 3)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
62 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
63 Expression earg = cast(Expression)arguments.data[0];
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
64 earg = earg.interpret(istate);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
65 if (earg is EXP_CANT_INTERPRET)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
66 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
67 if (earg.op != TOKassocarrayliteral)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
68 return null;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
69 AssocArrayLiteralExp aae = cast(AssocArrayLiteralExp)earg;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
70 Expression e = new ArrayLiteralExp(aae.loc, aae.values);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
71 Type elemType = (cast(TypeAArray)aae.type).next;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
72 e.type = new TypeSArray(elemType, new IntegerExp(arguments ? arguments.dim : 0));
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
73 //printf("result is %s\n", e.toChars());
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
74 return e;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
75 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
76
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
77 Expression getVarExp(Loc loc, InterState istate, Declaration d)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
78 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
79 Expression e = EXP_CANT_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
80 VarDeclaration v = d.isVarDeclaration();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
81 SymbolDeclaration s = d.isSymbolDeclaration();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
82 if (v)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
83 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
84 ///version (DMDV2) {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
85 if ((v.isConst() || v.isInvariant() || v.storage_class & STCmanifest) && v.init && !v.value)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
86 ///} else {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
87 /// if (v.isConst() && v.init)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
88 ///}
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
89 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
90 e = v.init.toExpression();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
91 if (e && !e.type)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
92 e.type = v.type;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
93 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
94 else
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
95 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
96 e = v.value;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
97 if (v.isDataseg())
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
98 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
99 error(loc, "static variable %s cannot be read at compile time", v.toChars());
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
100 e = EXP_CANT_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
101 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
102 else if (!e)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
103 error(loc, "variable %s is used before initialization", v.toChars());
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
104 else if (e !is EXP_CANT_INTERPRET)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
105 e = e.interpret(istate);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
106 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
107 if (!e)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
108 e = EXP_CANT_INTERPRET;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
109 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
110 else if (s)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
111 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
112 if (s.dsym.toInitializer() == s.sym)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
113 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
114 Expressions exps = new Expressions();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
115 e = new StructLiteralExp(Loc(0), s.dsym, exps);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
116 e = e.semantic(null);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
117 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
118 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
119 return e;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
120 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
121
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
122 /* Helper functions for BinExp.interpretAssignCommon
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
123 */
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
124
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
125 /***************************************
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
126 * Duplicate the elements array, then set field 'indexToChange' = newelem.
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
127 */
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
128 Expressions changeOneElement(Expressions oldelems, size_t indexToChange, void* newelem)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
129 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
130 Expressions expsx = new Expressions();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
131 expsx.setDim(oldelems.dim);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
132 for (size_t j = 0; j < expsx.dim; j++)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
133 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
134 if (j == indexToChange)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
135 expsx.data[j] = newelem;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
136 else
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
137 expsx.data[j] = oldelems.data[j];
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
138 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
139 return expsx;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
140 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
141
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
142 /***************************************
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
143 * Returns oldelems[0..insertpoint] ~ newelems ~ oldelems[insertpoint..$]
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
144 */
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
145 Expressions spliceElements(Expressions oldelems, Expressions newelems, size_t insertpoint)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
146 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
147 Expressions expsx = new Expressions();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
148 expsx.setDim(oldelems.dim);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
149 for (size_t j = 0; j < expsx.dim; j++)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
150 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
151 if (j >= insertpoint && j < insertpoint + newelems.dim)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
152 expsx.data[j] = newelems.data[j - insertpoint];
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
153 else
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
154 expsx.data[j] = oldelems.data[j];
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
155 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
156 return expsx;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
157 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
158
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
159 /******************************
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
160 * Create an array literal consisting of 'elem' duplicated 'dim' times.
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
161 */
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
162 ArrayLiteralExp createBlockDuplicatedArrayLiteral(Type type, Expression elem, size_t dim)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
163 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
164 Expressions elements = new Expressions();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
165 elements.setDim(dim);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
166 for (size_t i = 0; i < dim; i++) {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
167 elements.data[i] = cast(void*)elem;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
168 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
169
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
170 ArrayLiteralExp ae = new ArrayLiteralExp(Loc(0), elements);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
171 ae.type = type;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
172 return ae;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
173 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
174
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
175
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
176 /********************************
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
177 * Necessary because defaultInit() for a struct is a VarExp, not a StructLiteralExp.
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
178 */
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
179 StructLiteralExp createDefaultInitStructLiteral(Loc loc, StructDeclaration sym)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
180 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
181 Expressions structelems = new Expressions();
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
182 structelems.setDim(sym.fields.dim);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
183 for (size_t j = 0; j < structelems.dim; j++)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
184 {
79
43073c7c7769 updated to 2.035
Trass3r
parents: 74
diff changeset
185 structelems.data[j] = cast(void*)(cast(VarDeclaration)(sym.fields[j])).type.defaultInit(Loc(0));
63
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
186 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
187 StructLiteralExp structinit = new StructLiteralExp(loc, sym, structelems);
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
188 // Why doesn't the StructLiteralExp constructor do this, when
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
189 // sym.type != null ?
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
190 structinit.type = sym.type;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
191 return structinit;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
192 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
193
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
194 /********************************
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
195 * Add v to the istate list, unless it already exists there.
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
196 */
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
197 void addVarToInterstate(InterState istate, VarDeclaration v)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
198 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
199 if (!v.isParameter())
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
200 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
201 for (size_t i = 0; 1; i++)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
202 {
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
203 if (i == istate.vars.dim)
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
204 {
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 63
diff changeset
205 istate.vars.push(v);
63
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
206 //printf("\tadding %s to istate\n", v.toChars());
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
207 break;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
208 }
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 63
diff changeset
209 if (v == cast(VarDeclaration)istate.vars[i])
63
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
210 break;
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
211 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
212 }
cab4c37afb89 A bunch of implementations
korDen
parents:
diff changeset
213 }