annotate dmd/ArrayInitializer.d @ 84:be2ab491772e

Expressions -> Vector!Expression
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Mon, 30 Aug 2010 16:12:19 +0100
parents 43073c7c7769
children 39648eb578f6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.ArrayInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
79
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
5 import dmd.TypeAArray;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.TypeNext;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Initializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.ArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.ErrorExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.backend.dt_t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.codegen.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 class ArrayInitializer : Initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 Expressions index; // indices
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Initializers value; // of Initializer *'s
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 uint dim = 0; // length of array being initialized
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 Type type = null; // type that array will be used to initialize
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 int sem = 0; // !=0 if semantic() is run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 this(Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 index = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 value = new Initializers();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
43 override Initializer syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 //printf("ArrayInitializer.syntaxCopy()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 ArrayInitializer ai = new ArrayInitializer(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 assert(index.dim == value.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 ai.index.setDim(index.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 ai.value.setDim(value.dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 for (int i = 0; i < ai.value.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
54 Expression e = index[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 e = e.syntaxCopy();
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
57 ai.index[i] = e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 Initializer init = cast(Initializer)value.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 init = init.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 ai.value.data[i] = cast(void*)init;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 return ai;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 void addInit(Expression index, Initializer value)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
68 this.index.push(index);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 this.value.push(cast(void*)value);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 dim = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 type = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
74 override Initializer semantic(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 uint length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 //printf("ArrayInitializer.semantic(%s)\n", t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 if (sem) // if semantic() already run
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 sem = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 t = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 switch (t.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 case Tpointer:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 case Tsarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 case Tarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 error(loc, "cannot use array to initialize %s", type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 length = 0;
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
98 foreach (size_t i, Expression idx; index)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 Initializer val;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 if (idx)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 idx = idx.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 idx = idx.optimize(WANTvalue | WANTinterpret);
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
106 index[i] = idx;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 length = cast(uint)idx.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 val = cast(Initializer)value.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 val = val.semantic(sc, t.nextOf());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 value.data[i] = cast(void*)val;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 length++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 if (length == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 error(loc, "array dimension overflow");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (length > dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 dim = length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 uint amax = 0x80000000;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 if (cast(uint) (dim * t.nextOf().size()) >= amax)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 error(loc, "array dimension %u exceeds max of %ju", dim, amax / t.nextOf().size());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
126 override Type inferType(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 //printf("ArrayInitializer.inferType() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 for (size_t i = 0; i < value.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 if (index.data[i])
79
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
133 goto Laa;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 for (size_t i = 0; i < value.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 Initializer iz = cast(Initializer)value.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 if (iz)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 Type t = iz.inferType(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 if (i == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 t = new TypeSArray(t, new IntegerExp(value.dim));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 t = t.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 type = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 return type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151
79
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
152 Laa:
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
153 /* It's possibly an associative array initializer
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
154 */
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
155 Initializer iz = cast(Initializer)value.data[0];
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
156 Expression indexinit = cast(Expression)index.data[0];
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
157 if (iz && indexinit)
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
158 {
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
159 Type t = iz.inferType(sc);
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
160 indexinit = indexinit.semantic(sc);
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
161 Type indext = indexinit.type;
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
162 t = new TypeAArray(t, indext);
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
163 type = t.semantic(loc, sc);
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
164 }
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
165 else
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
166 error(loc, "cannot infer type from this array initializer");
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
167 return type;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 /********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 * If possible, convert array initializer to array literal.
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
172 * Otherwise return null.
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 */
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
174 override Expression toExpression()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 Expression e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 //printf("ArrayInitializer.toExpression(), dim = %d\n", dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 //static int i; if (++i == 2) halt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 size_t edim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 Type t = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 {
79
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
185 if (type == Type.terror)
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
186 return new ErrorExp();
43073c7c7769 updated to 2.035
Trass3r
parents: 73
diff changeset
187
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 t = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 switch (t.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 case Tsarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 edim = cast(uint)(cast(TypeSArray)t).dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 case Tpointer:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 case Tarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 edim = dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 edim = value.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 for (size_t i = 0, j = 0; i < value.dim; i++, j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 if (index.data[i])
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 j = cast(uint)(cast(Expression)index.data[i]).toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 if (j >= edim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 edim = j + 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 Expressions elements = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 elements.setDim(edim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 for (size_t i = 0, j = 0; i < value.dim; i++, j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 if (index.data[i])
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 j = cast(uint)(cast(Expression)index.data[i]).toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 assert(j < edim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 Initializer iz = cast(Initializer)value.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 if (!iz)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 goto Lno;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 Expression ex = iz.toExpression();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 if (!ex)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 goto Lno;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 }
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
231 elements[j] = ex;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 /* Fill in any missing elements with the default initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 Expression init = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 for (size_t i = 0; i < edim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 if (!elements.data[i])
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 if (!type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 goto Lno;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 if (!init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 init = (cast(TypeNext)t).next.defaultInit(Loc(0));
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
246 elements[i] = init;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 Expression e2 = new ArrayLiteralExp(loc, elements);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 e2.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 return e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 Lno:
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
256 return null;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 Initializer toAssocArrayInitializer()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
264 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
269 override dt_t* toDt()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 //printf("ArrayInitializer.toDt('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 Type tb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 Type tn = tb.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 scope Array dts = new Array();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 uint size;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 uint length;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 uint i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 dt_t* dt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 dt_t* d;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 dt_t** pdtend;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 //printf("\tdim = %d\n", dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 dts.setDim(dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 dts.zero();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 size = cast(uint)tn.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 length = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 for (i = 0; i < index.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 Expression idx;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 Initializer val;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 idx = cast(Expression)index.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 if (idx)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 length = cast(uint)idx.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 //printf("\tindex[%d] = %p, length = %u, dim = %u\n", i, idx, length, dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300 assert(length < dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 val = cast(Initializer)value.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 dt = val.toDt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 if (dts.data[length])
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 error(loc, "duplicate initializations for index %d", length);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 dts.data[length] = cast(void*)dt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 length++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 Expression edefault = tb.nextOf().defaultInit(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 uint n = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 for (Type tbn = tn; tbn.ty == Tsarray; tbn = tbn.nextOf().toBasetype())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 TypeSArray tsa = cast(TypeSArray)tbn;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 n *= tsa.dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 d = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 pdtend = &d;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 for (i = 0; i < dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 dt = cast(dt_t*)dts.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 if (dt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 pdtend = dtcat(pdtend, dt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 for (int j = 0; j < n; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 pdtend = edefault.toDt(pdtend);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 switch (tb.ty)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 case Tsarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 uint tadim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 TypeSArray ta = cast(TypeSArray)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 tadim = cast(uint)ta.dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 if (dim < tadim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 if (edefault.isBool(false))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 // pad out end of array
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 pdtend = dtnzeros(pdtend, size * (tadim - dim));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 for (i = dim; i < tadim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 for (int j = 0; j < n; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 pdtend = edefault.toDt(pdtend);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 else if (dim > tadim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 debug writef("1: ");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 error(loc, "too many initializers, %d, for array[%d]", dim, tadim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 case Tpointer:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 case Tarray:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 // Create symbol, and then refer to it
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 Symbol* s = static_sym();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 s.Sdt = d;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 outdata(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 d = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 if (tb.ty == Tarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 dtdword(&d, dim);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 dtxoff(&d, s, 0, TYnptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 return d;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 dt_t* toDtBit() // for bit arrays
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
385 override ArrayInitializer isArrayInitializer() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 59
diff changeset
386 }