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