annotate dmd/ArrayInitializer.d @ 127:9ee9b55452cb

Identifiers, Initializers -> Vector
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Fri, 03 Sep 2010 23:55:51 +0100
parents e28b18c23469
children e6e542f37b94
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
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
60 auto init = value[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 init = init.syntaxCopy();
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
62 ai.value[i] = init;
0
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);
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
70 this.value.push(value);
0
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
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
111 val = value[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 val = val.semantic(sc, t.nextOf());
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
113 value[i] = val;
0
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
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
137 foreach (size_t i, Initializer iz; value)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 {
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 */
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
155 Initializer iz = value[0];
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
156 Expression indexinit = index[0];
79
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 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
209 if (index[i])
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
210 j = cast(uint)(index[i].toInteger());
0
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
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
216 auto elements = new Expressions();
0
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 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
220 if (index[i])
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
221 j = cast(uint)(index[i].toInteger());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 assert(j < edim);
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
223 Initializer iz = value[i];
0
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 {
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
240 if (!elements[i])
0
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
90
39648eb578f6 more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 84
diff changeset
295 idx = index[i];
0
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);
127
9ee9b55452cb Identifiers, Initializers -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
301 val = value[i];
0
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 }