annotate dmd/ArrayLiteralExp.d @ 114:e28b18c23469

added a module dmd.common for commonly used stuff it currently holds code for consistency checking of predefined versions also added a VisualD project file
author Trass3r
date Wed, 01 Sep 2010 18:21:58 +0200
parents 39648eb578f6
children 9e39c7de8438
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1 module dmd.ArrayLiteralExp;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 90
diff changeset
3 import dmd.common;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
4 import dmd.Expression;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
5 import dmd.backend.elem;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
6 import dmd.InterState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
7 import dmd.MATCH;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
8 import dmd.Type;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
9 import dmd.OutBuffer;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
10 import dmd.Loc;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.WANT;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
12 import dmd.Scope;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
13 import dmd.InlineCostState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
14 import dmd.IRState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
15 import dmd.InlineDoState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
16 import dmd.HdrGenState;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
17 import dmd.backend.dt_t;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
18 import dmd.InlineScanState;
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
19 import dmd.Array;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
20 import dmd.ArrayTypes;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.backend.RTLSYM;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
30 import dmd.backend.OPER;
68
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
31 import dmd.backend.Symbol;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.backend.TYM;
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
33 import dmd.backend.mTY;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
34 import dmd.codegen.Util;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
35
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 class ArrayLiteralExp : Expression
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 Expressions elements;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 this(Loc loc, Expressions elements)
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
41 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 super(loc, TOK.TOKarrayliteral, ArrayLiteralExp.sizeof);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 this.elements = elements;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 this(Loc loc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 super(loc, TOK.TOKarrayliteral, ArrayLiteralExp.sizeof);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 elements = new Expressions();
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
50 elements.push(e);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
53 override Expression syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
58 override Expression semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 Type t0 = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 printf("ArrayLiteralExp.semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 // Run semantic() on each element
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
70 foreach (ref Expression e; elements)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 assert(e.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 expandTuples(elements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
78 foreach (size_t i, Expression e; elements)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 if (!e.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 error("%s has no value", e.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 e = resolveProperties(sc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 ubyte committed = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 if (e.op == TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 committed = (cast(StringExp)e).committed;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 if (!t0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 t0 = e.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 // Convert any static arrays to dynamic arrays
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 if (t0.ty == Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 t0 = (cast(TypeSArray)t0).next.arrayOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 e = e.implicitCastTo(sc, t0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 e = e.implicitCastTo(sc, t0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 if (!committed && e.op == TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
104 auto se = cast(StringExp)e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 se.committed = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
107 elements[i] = e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 if (!t0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 t0 = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 type = new TypeSArray(t0, new IntegerExp(elements.dim));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
117 override bool isBool(bool result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 size_t dim = elements ? elements.dim : 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 return result ? (dim != 0) : (dim == 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
123 override elem* toElem(IRState* irs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 elem* e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 size_t dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 //printf("ArrayLiteralExp.toElem() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 if (elements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
131 scope args = new Array(); // ddmd was Expressions
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 dim = elements.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 args.setDim(dim + 1); // +1 for number of args parameter
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 e = el_long(TYint, dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 args.data[dim] = cast(void*)e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 for (size_t i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
138 auto el = elements[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 elem* ep = el.toElem(irs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 if (tybasic(ep.Ety) == TYstruct || tybasic(ep.Ety) == TYarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 ep = el_una(OPstrpar, TYstruct, ep);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 ep.Enumbytes = cast(uint)el.type.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 args.data[dim - (i + 1)] = cast(void *)ep;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 /* Because the number of parameters can get very large, produce
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 * a balanced binary tree so we don't blow up the stack in
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 * the subsequent tree walking code.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 e = el_params(args.data, dim + 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 dim = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 e = el_long(TYint, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 Type tb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 static if (true) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 e = el_param(e, type.getTypeInfo(null).toElem(irs));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 // call _d_arrayliteralT(ti, dim, ...)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 e = el_bin(OPcall,TYnptr,el_var(rtlsym[RTLSYM_ARRAYLITERALT]),e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 e = el_param(e, el_long(TYint, tb.next.size()));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 // call _d_arrayliteral(size, dim, ...)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 e = el_bin(OPcall,TYnptr,el_var(rtlsym[RTLSYM_ARRAYLITERAL]),e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 if (tb.ty == Tarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 e = el_pair(TYullong, el_long(TYint, dim), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 else if (tb.ty == Tpointer)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 e = el_una(OPind,TYstruct,e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 e.Enumbytes = cast(uint)type.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 el_setLoc(e,loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
189 override bool checkSideEffect(int flag)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 bool f = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
193 foreach (e; elements)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 f |= e.checkSideEffect(2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 if (flag == 0 && f == false)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 Expression.checkSideEffect(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 return f;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
203 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 buf.writeByte('[');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 argsToCBuffer(buf, elements, hgs);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 buf.writeByte(']');
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
210 override void toMangleBuffer(OutBuffer buf)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 size_t dim = elements ? elements.dim : 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 buf.printf("A%u", dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 for (size_t i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
216 auto e = elements[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 e.toMangleBuffer(buf);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
221 override void scanForNestedRef(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
226 override Expression optimize(int result)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 if (elements)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
230 foreach (ref Expression e; elements)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 e = e.optimize(WANTvalue | (result & WANTinterpret));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
239 override Expression interpret(InterState istate)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
244 override MATCH implicitConvTo(Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 MATCH result = MATCHexact;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 printf("ArrayLiteralExp.implicitConvTo(this=%s, type=%s, t=%s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 Type typeb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 if ((tb.ty == Tarray || tb.ty == Tsarray) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 (typeb.ty == Tarray || typeb.ty == Tsarray))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 if (tb.ty == Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 TypeSArray tsa = cast(TypeSArray)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 if (elements.dim != tsa.dim.toInteger())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 result = MATCHnomatch;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
264 foreach (e; elements)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
266 auto m = cast(MATCH)e.implicitConvTo(tb.nextOf());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 if (m < result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 result = m; // remember worst match
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 if (result == MATCHnomatch)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 break; // no need to check for worse
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 return result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 return Expression.implicitConvTo(t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
278 override Expression castTo(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 printf("ArrayLiteralExp.castTo(this=%s, type=%s, => %s)\n",
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 toChars(), type.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 if (type == t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 ArrayLiteralExp e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 Type typeb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 if ((tb.ty == Tarray || tb.ty == Tsarray) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 (typeb.ty == Tarray || typeb.ty == Tsarray) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 // Not trying to convert non-void[] to void[]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 !(tb.nextOf().toBasetype().ty == Tvoid && typeb.nextOf().toBasetype().ty != Tvoid))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 if (tb.ty == Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 TypeSArray tsa = cast(TypeSArray)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 if (elements.dim != tsa.dim.toInteger())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 e = cast(ArrayLiteralExp)copy();
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
302 e.elements = elements.copy();
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
303 foreach (size_t i, Expression ex; elements)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
305 e.elements[i] = ex.castTo(sc, tb.nextOf());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 e.type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 if (tb.ty == Tpointer && typeb.ty == Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 Type tp = typeb.nextOf().pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 if (!tp.equals(e.type))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 e = cast(ArrayLiteralExp)copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 e.type = tp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 return e.Expression.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
323 override dt_t** toDt(dt_t** pdt)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 {
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
325 //printf("ArrayLiteralExp.toDt() '%s', type = %s\n", toChars(), type.toChars());
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
326
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
327 dt_t *d;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
328 dt_t **pdtend;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
329
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
330 d = null;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
331 pdtend = &d;
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
332 foreach (e; elements)
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
333 {
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
334 pdtend = e.toDt(pdtend);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
335 }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
336 Type t = type.toBasetype();
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
337
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
338 switch (t.ty)
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
339 {
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
340 case Tsarray:
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
341 pdt = dtcat(pdt, d);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
342 break;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
343
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
344 case Tpointer:
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
345 case Tarray:
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
346 if (t.ty == Tarray)
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
347 dtdword(pdt, elements.dim);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
348 if (d)
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
349 {
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
350 // Create symbol, and then refer to it
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
351 Symbol* s;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
352 s = static_sym();
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
353 s.Sdt = d;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
354 outdata(s);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
355
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
356 dtxoff(pdt, s, 0, TYnptr);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
357 }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
358 else
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
359 dtdword(pdt, 0);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
360
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
361 break;
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
362
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
363 default:
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
364 assert(0);
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
365 }
68
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 63
diff changeset
366 return pdt;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 version (DMDV2) {
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
370 override bool canThrow()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 return 1; // because it can fail allocating memory
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
375 override int inlineCost(InlineCostState* ics)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 return 1 + arrayInlineCost(ics, elements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
380 override Expression doInline(InlineDoState ids)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 ArrayLiteralExp ce = cast(ArrayLiteralExp)copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 ce.elements = arrayExpressiondoInline(elements, ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 return ce;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
387 override Expression inlineScan(InlineScanState* iss)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 Expression e = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 //printf("ArrayLiteralExp.inlineScan()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 arrayInlineScan(iss, elements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397