Mercurial > projects > ddmd
annotate dmd/TypeSArray.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 | 3482c73a991b |
children | 347de076ad34 |
rev | line source |
---|---|
0 | 1 module dmd.TypeSArray; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.TypeArray; |
64 | 5 import dmd.TypeInfoStaticArrayDeclaration; |
51 | 6 import dmd.TypeAArray; |
0 | 7 import dmd.MOD; |
8 import dmd.Argument; | |
51 | 9 import dmd.TypeIdentifier; |
10 import dmd.TemplateParameter; | |
11 import dmd.TemplateValueParameter; | |
0 | 12 import dmd.TypeStruct; |
13 import dmd.TypeTuple; | |
14 import dmd.VarExp; | |
15 import dmd.IntegerExp; | |
16 import dmd.Expression; | |
17 import dmd.Type; | |
18 import dmd.TupleDeclaration; | |
19 import dmd.TOK; | |
20 import dmd.Loc; | |
21 import dmd.STC; | |
22 import dmd.Scope; | |
23 import dmd.Dsymbol; | |
24 import dmd.OutBuffer; | |
25 import dmd.HdrGenState; | |
26 import dmd.Identifier; | |
27 import dmd.MATCH; | |
28 import dmd.TypeDArray; | |
29 import dmd.TypePointer; | |
30 import dmd.ArrayTypes; | |
31 import dmd.WANT; | |
32 import dmd.TypeInfoDeclaration; | |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
33 import dmd.ScopeDsymbol; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
34 import dmd.ArrayScopeSymbol; |
0 | 35 import dmd.TY; |
36 import dmd.Util; | |
37 import dmd.Id; | |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
38 import dmd.IndexExp; |
0 | 39 |
40 import dmd.type.Util; | |
41 | |
42 import dmd.backend.dt_t; | |
43 import dmd.backend.TYPE; | |
44 import dmd.backend.Util; | |
45 import dmd.backend.TYM; | |
46 import dmd.backend.DT; | |
47 | |
48 // Static array, one with a fixed dimension | |
49 class TypeSArray : TypeArray | |
50 { | |
51 Expression dim; | |
52 | |
53 this(Type t, Expression dim) | |
54 { | |
55 super(TY.Tsarray, t); | |
56 //printf("TypeSArray(%s)\n", dim.toChars()); | |
57 this.dim = dim; | |
58 } | |
59 | |
60 version (DumbClone) { | |
61 } else { | |
62 Type clone() | |
63 { | |
64 assert(false); | |
65 } | |
66 } | |
67 | |
72 | 68 override Type syntaxCopy() |
0 | 69 { |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
70 Type t = next.syntaxCopy(); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
71 Expression e = dim.syntaxCopy(); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
72 t = new TypeSArray(t, e); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
73 t.mod = mod; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
74 return t; |
0 | 75 } |
76 | |
72 | 77 override ulong size(Loc loc) |
0 | 78 { |
79 if (!dim) | |
80 return Type.size(loc); | |
81 | |
82 long sz = dim.toInteger(); | |
83 | |
84 { | |
85 long n, n2; | |
86 n = next.size(); | |
87 n2 = n * sz; | |
88 if (n && (n2 / n) != sz) | |
89 goto Loverflow; | |
90 | |
91 sz = n2; | |
92 } | |
93 return sz; | |
94 | |
95 Loverflow: | |
96 error(loc, "index %jd overflow for static array", sz); | |
97 return 1; | |
98 } | |
99 | |
72 | 100 override uint alignsize() |
0 | 101 { |
102 return next.alignsize(); | |
103 } | |
104 | |
72 | 105 override Type semantic(Loc loc, Scope sc) |
0 | 106 { |
107 //printf("TypeSArray.semantic() %s\n", toChars()); | |
108 | |
109 Type t; | |
110 Expression e; | |
111 Dsymbol s; | |
112 next.resolve(loc, sc, &e, &t, &s); | |
113 if (dim && s && s.isTupleDeclaration()) | |
114 { | |
115 TupleDeclaration sd = s.isTupleDeclaration(); | |
116 | |
117 dim = semanticLength(sc, sd, dim); | |
118 dim = dim.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
119 ulong d = dim.toUInteger(); | |
120 | |
121 if (d >= sd.objects.dim) | |
122 { error(loc, "tuple index %ju exceeds %u", d, sd.objects.dim); | |
123 return Type.terror; | |
124 } | |
125 ///Object o = cast(Object)sd.objects.data[(size_t)d]; | |
126 ///if (o.dyncast() != DYNCAST_TYPE) | |
127 ///{ | |
128 /// error(loc, "%s is not a type", toChars()); | |
129 /// return Type.terror; | |
130 ///} | |
131 ///t = cast(Type)o; | |
132 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
133 t = cast(Type)sd.objects[cast(size_t)d]; |
0 | 134 if (t is null) { |
135 error(loc, "%s is not a type", toChars()); | |
136 return Type.terror; | |
137 } | |
138 return t; | |
139 } | |
140 | |
141 next = next.semantic(loc,sc); | |
142 transitive(); | |
143 | |
144 Type tbn = next.toBasetype(); | |
145 | |
146 if (dim) | |
147 { | |
148 long n, n2; | |
149 | |
150 dim = semanticLength(sc, tbn, dim); | |
151 | |
152 dim = dim.optimize(WANT.WANTvalue | WANT.WANTinterpret); | |
153 if (sc && sc.parameterSpecialization && dim.op == TOK.TOKvar && | |
154 (cast(VarExp)dim).var.storage_class & STC.STCtemplateparameter) | |
155 { | |
156 /* It could be a template parameter N which has no value yet: | |
157 * template Foo(T : T[N], size_t N); | |
158 */ | |
159 return this; | |
160 } | |
161 long d1 = dim.toInteger(); | |
162 dim = dim.castTo(sc, tsize_t); | |
163 dim = dim.optimize(WANT.WANTvalue); | |
164 long d2 = dim.toInteger(); | |
165 | |
166 if (d1 != d2) | |
167 goto Loverflow; | |
168 | |
169 if (tbn.isintegral() || | |
170 tbn.isfloating() || | |
171 tbn.ty == TY.Tpointer || | |
172 tbn.ty == TY.Tarray || | |
173 tbn.ty == TY.Tsarray || | |
174 tbn.ty == TY.Taarray || | |
175 tbn.ty == TY.Tclass) | |
176 { | |
177 /* Only do this for types that don't need to have semantic() | |
178 * run on them for the size, since they may be forward referenced. | |
179 */ | |
180 n = tbn.size(loc); | |
181 n2 = n * d2; | |
182 if (cast(int)n2 < 0) | |
183 goto Loverflow; | |
184 if (n2 >= 0x1000000) // put a 'reasonable' limit on it | |
185 goto Loverflow; | |
186 if (n && n2 / n != d2) | |
187 { | |
188 Loverflow: | |
189 error(loc, "index %jd overflow for static array", d1); | |
190 dim = new IntegerExp(Loc(0), 1, tsize_t); | |
191 } | |
192 } | |
193 } | |
194 switch (tbn.ty) | |
195 { | |
196 case TY.Ttuple: | |
197 { // Index the tuple to get the type | |
198 assert(dim); | |
199 TypeTuple tt = cast(TypeTuple)tbn; | |
200 ulong d = dim.toUInteger(); | |
201 | |
202 if (d >= tt.arguments.dim) | |
203 { | |
204 error(loc, "tuple index %ju exceeds %u", d, tt.arguments.dim); | |
205 return Type.terror; | |
206 } | |
207 Argument arg = cast(Argument)tt.arguments.data[cast(size_t)d]; | |
208 return arg.type; | |
209 } | |
210 case TY.Tstruct: | |
211 { TypeStruct ts = cast(TypeStruct)tbn; | |
212 if (ts.sym.isnested) | |
213 error(loc, "cannot have array of inner structs %s", ts.toChars()); | |
214 break; | |
215 } | |
216 case TY.Tfunction: | |
217 case TY.Tnone: | |
218 error(loc, "can't have array of %s", tbn.toChars()); | |
219 tbn = next = tint32; | |
220 break; | |
221 default: /// | |
222 break; | |
223 } | |
224 if (tbn.isauto()) | |
225 error(loc, "cannot have array of auto %s", tbn.toChars()); | |
226 return merge(); | |
227 } | |
228 | |
72 | 229 override void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps) |
0 | 230 { |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
231 //printf("TypeSArray.resolve() %s\n", toChars()); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
232 next.resolve(loc, sc, pe, pt, ps); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
233 //printf("s = %p, e = %p, t = %p\n", *ps, *pe, *pt); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
234 if (*pe) |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
235 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
236 // It's really an index expression |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
237 Expression e = new IndexExp(loc, *pe, dim); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
238 *pe = e; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
239 } |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
240 else if (*ps) |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
241 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
242 Dsymbol s = *ps; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
243 TupleDeclaration td = s.isTupleDeclaration(); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
244 if (td) |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
245 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
246 ScopeDsymbol sym = new ArrayScopeSymbol(sc, td); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
247 sym.parent = sc.scopesym; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
248 sc = sc.push(sym); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
249 |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
250 dim = dim.semantic(sc); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
251 dim = dim.optimize(WANTvalue | WANTinterpret); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
252 ulong d = dim.toUInteger(); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
253 |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
254 sc = sc.pop(); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
255 |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
256 if (d >= td.objects.dim) |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
257 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
258 error(loc, "tuple index %ju exceeds %u", d, td.objects.dim); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
259 goto Ldefault; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
260 } |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
261 Object o = td.objects[cast(size_t)d]; |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
262 if ((*ps = isDsymbol(o)) !is null) /// ! |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
263 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
264 return; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
265 } |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
266 if ((*pe = isExpression(o)) !is null) /// ! |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
267 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
268 return; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
269 } |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
270 |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
271 /* Create a new TupleDeclaration which |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
272 * is a slice [d..d+1] out of the old one. |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
273 * Do it this way because TemplateInstance.semanticTiargs() |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
274 * can handle unresolved Objects this way. |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
275 */ |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
276 auto objects = new Objects; |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
277 objects.setDim(1); |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
278 objects[0] = o; |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
279 |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
280 auto tds = new TupleDeclaration(loc, td.ident, objects); |
41
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
281 *ps = tds; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
282 } |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
283 else |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
284 goto Ldefault; |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
285 } |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
286 else |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
287 { |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
288 Ldefault: |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
289 Type.resolve(loc, sc, pe, pt, ps); |
f23312cb6f2e
TypeSArray.syntaxCopy & TypeSArray.resolve implemented
korDen
parents:
12
diff
changeset
|
290 } |
0 | 291 } |
292 | |
72 | 293 override void toDecoBuffer(OutBuffer buf, int flag) |
0 | 294 { |
295 Type.toDecoBuffer(buf, flag); | |
296 if (dim) | |
297 //buf.printf("%ju", dim.toInteger()); /// | |
298 buf.printf("%s", dim.toInteger()); | |
299 if (next) | |
300 /* Note that static arrays are value types, so | |
301 * for a parameter, propagate the 0x100 to the next | |
302 * level, since for T[4][3], any const should apply to the T, | |
303 * not the [4]. | |
304 */ | |
305 next.toDecoBuffer(buf, (flag & 0x100) ? flag : mod); | |
306 } | |
307 | |
72 | 308 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 309 { |
67 | 310 if (mod != this.mod) |
311 { | |
312 toCBuffer3(buf, hgs, mod); | |
313 return; | |
314 } | |
315 next.toCBuffer2(buf, hgs, this.mod); | |
316 buf.printf("[%s]", dim.toChars()); | |
0 | 317 } |
318 | |
72 | 319 override Expression dotExp(Scope sc, Expression e, Identifier ident) |
0 | 320 { |
321 version (LOGDOTEXP) { | |
322 printf("TypeSArray.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars()); | |
323 } | |
324 if (ident == Id.length) | |
325 { | |
326 e = dim; | |
327 } | |
328 else if (ident == Id.ptr) | |
329 { | |
330 e = e.castTo(sc, next.pointerTo()); | |
331 } | |
332 else | |
333 { | |
334 e = TypeArray.dotExp(sc, e, ident); | |
335 } | |
336 return e; | |
337 } | |
338 | |
72 | 339 override bool isString() |
0 | 340 { |
64 | 341 TY nty = next.toBasetype().ty; |
342 return nty == Tchar || nty == Twchar || nty == Tdchar; | |
0 | 343 } |
344 | |
72 | 345 override bool isZeroInit(Loc loc) |
0 | 346 { |
347 return next.isZeroInit(loc); | |
348 } | |
349 | |
72 | 350 override uint memalign(uint salign) |
0 | 351 { |
352 return next.memalign(salign); | |
353 } | |
354 | |
72 | 355 override MATCH constConv(Type to) |
0 | 356 { |
357 assert(false); | |
358 } | |
359 | |
72 | 360 override MATCH implicitConvTo(Type to) |
0 | 361 { |
362 //printf("TypeSArray.implicitConvTo(to = %s) this = %s\n", to.toChars(), toChars()); | |
363 | |
364 // Allow implicit conversion of static array to pointer or dynamic array | |
365 if (IMPLICIT_ARRAY_TO_PTR && to.ty == Tpointer) | |
366 { | |
367 TypePointer tp = cast(TypePointer)to; | |
368 | |
369 if (next.mod != tp.next.mod && tp.next.mod != MODconst) | |
370 return MATCHnomatch; | |
371 | |
372 if (tp.next.ty == Tvoid || next.constConv(tp.next) != MATCHnomatch) | |
373 { | |
374 return MATCHconvert; | |
375 } | |
376 return MATCHnomatch; | |
377 } | |
378 if (to.ty == Tarray) | |
379 { | |
380 int offset = 0; | |
381 TypeDArray ta = cast(TypeDArray)to; | |
382 | |
383 if (next.mod != ta.next.mod && ta.next.mod != MODconst) | |
384 return MATCHnomatch; | |
385 | |
386 if (next.equals(ta.next) || | |
387 next.implicitConvTo(ta.next) >= MATCHconst || | |
388 (ta.next.isBaseOf(next, &offset) && offset == 0) || | |
389 ta.next.ty == Tvoid | |
390 ) | |
391 return MATCHconvert; | |
392 | |
393 return MATCHnomatch; | |
394 } | |
395 if (to.ty == Tsarray) | |
396 { | |
397 if (this == to) | |
398 return MATCHexact; | |
399 | |
400 TypeSArray tsa = cast(TypeSArray)to; | |
401 | |
402 if (dim.equals(tsa.dim)) | |
403 { | |
404 /* Since static arrays are value types, allow | |
405 * conversions from const elements to non-const | |
406 * ones, just like we allow conversion from const int | |
407 * to int. | |
408 */ | |
409 MATCH m = next.implicitConvTo(tsa.next); | |
410 if (m >= MATCHconst) | |
411 { | |
412 if (mod != to.mod) | |
413 m = MATCHconst; | |
414 return m; | |
415 } | |
416 } | |
417 } | |
418 return MATCHnomatch; | |
419 } | |
420 | |
72 | 421 override Expression defaultInit(Loc loc) |
0 | 422 { |
423 version (LOGDEFAULTINIT) { | |
424 printf("TypeSArray.defaultInit() '%s'\n", toChars()); | |
425 } | |
426 return next.defaultInit(loc); | |
427 } | |
428 | |
72 | 429 override dt_t** toDt(dt_t** pdt) |
0 | 430 { |
431 return toDtElem(pdt, null); | |
432 } | |
433 | |
434 dt_t** toDtElem(dt_t** pdt, Expression e) | |
435 { | |
436 int i; | |
437 | |
438 //printf("TypeSArray::toDtElem()\n"); | |
439 uint len = cast(uint)dim.toInteger(); | |
440 if (len) | |
441 { | |
442 while (*pdt) | |
443 pdt = &((*pdt).DTnext); | |
444 Type tnext = next; | |
445 Type tbn = tnext.toBasetype(); | |
446 while (tbn.ty == Tsarray) | |
447 { | |
448 TypeSArray tsa = cast(TypeSArray)tbn; | |
449 | |
450 len *= tsa.dim.toInteger(); | |
451 tnext = tbn.nextOf(); | |
452 tbn = tnext.toBasetype(); | |
453 } | |
454 if (!e) // if not already supplied | |
455 e = tnext.defaultInit(Loc(0)); // use default initializer | |
456 if (tbn.ty == Tstruct) | |
457 tnext.toDt(pdt); | |
458 else | |
459 e.toDt(pdt); | |
460 dt_optimize(*pdt); | |
461 if ((*pdt).dt == DT_azeros && !(*pdt).DTnext) | |
462 { | |
463 (*pdt).DTazeros *= len; | |
464 pdt = &((*pdt).DTnext); | |
465 } | |
466 else if ((*pdt).dt == DT_1byte && (*pdt).DTonebyte == 0 && !(*pdt).DTnext) | |
467 { | |
468 (*pdt).dt = DT_azeros; | |
469 (*pdt).DTazeros = len; | |
470 pdt = &((*pdt).DTnext); | |
471 } | |
472 else if (e.op != TOKstring) | |
473 { | |
474 for (i = 1; i < len; i++) | |
475 { | |
476 if (tbn.ty == Tstruct) | |
477 { | |
478 pdt = tnext.toDt(pdt); | |
479 while (*pdt) | |
480 pdt = &((*pdt).DTnext); | |
481 } | |
482 else | |
483 pdt = e.toDt(pdt); | |
484 } | |
485 } | |
486 } | |
487 return pdt; | |
488 } | |
489 | |
72 | 490 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 491 { |
51 | 492 static if (false) { |
493 printf("TypeSArray.deduceType()\n"); | |
494 printf("\tthis = %d, ", ty); print(); | |
495 printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
496 } | |
497 | |
498 // Extra check that array dimensions must match | |
499 if (tparam) | |
500 { | |
501 if (tparam.ty == Tsarray) | |
502 { | |
503 TypeSArray tp = cast(TypeSArray)tparam; | |
504 | |
505 if (tp.dim.op == TOKvar && (cast(VarExp)tp.dim).var.storage_class & STCtemplateparameter) | |
506 { | |
507 int i = templateIdentifierLookup((cast(VarExp)tp.dim).var.ident, parameters); | |
508 // This code matches code in TypeInstance.deduceType() | |
509 if (i == -1) | |
510 goto Lnomatch; | |
511 TemplateParameter tp2 = cast(TemplateParameter)parameters.data[i]; | |
512 TemplateValueParameter tvp = tp2.isTemplateValueParameter(); | |
513 if (!tvp) | |
514 goto Lnomatch; | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
515 Expression e = cast(Expression)dedtypes[i]; |
51 | 516 if (e) |
517 { | |
518 if (!dim.equals(e)) | |
519 goto Lnomatch; | |
520 } | |
521 else | |
522 { | |
523 Type vt = tvp.valType.semantic(Loc(0), sc); | |
524 MATCH m = cast(MATCH)dim.implicitConvTo(vt); | |
525 if (!m) | |
526 goto Lnomatch; | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
527 dedtypes[i] = dim; |
51 | 528 } |
529 } | |
530 else if (dim.toInteger() != tp.dim.toInteger()) | |
531 return MATCHnomatch; | |
532 } | |
533 else if (tparam.ty == Taarray) | |
534 { | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
535 auto tp = cast(TypeAArray)tparam; |
51 | 536 if (tp.index.ty == Tident) |
537 { | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
538 auto tident = cast(TypeIdentifier)tp.index; |
51 | 539 |
540 if (tident.idents.dim == 0) | |
541 { | |
542 Identifier id = tident.ident; | |
543 | |
544 for (size_t i = 0; i < parameters.dim; i++) | |
545 { | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
546 auto tp2 = cast(TemplateParameter)parameters.data[i]; |
51 | 547 |
548 if (tp2.ident.equals(id)) | |
549 { | |
550 // Found the corresponding template parameter | |
551 TemplateValueParameter tvp = tp2.isTemplateValueParameter(); | |
552 if (!tvp || !tvp.valType.isintegral()) | |
553 goto Lnomatch; | |
554 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
555 if (dedtypes[i]) |
51 | 556 { |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
557 if (!dim.equals(dedtypes[i])) |
51 | 558 goto Lnomatch; |
559 } | |
560 else | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
561 { dedtypes[i] = dim; |
51 | 562 } |
563 return next.deduceType(sc, tparam.nextOf(), parameters, dedtypes); | |
564 } | |
565 } | |
566 } | |
567 } | |
568 } | |
569 else if (tparam.ty == Tarray) | |
570 { | |
571 MATCH m; | |
572 | |
573 m = next.deduceType(sc, tparam.nextOf(), parameters, dedtypes); | |
574 if (m == MATCHexact) | |
575 m = MATCHconvert; | |
576 return m; | |
577 } | |
578 } | |
579 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
580 | |
581 Lnomatch: | |
582 return MATCHnomatch; | |
0 | 583 } |
584 | |
72 | 585 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 586 { |
64 | 587 return new TypeInfoStaticArrayDeclaration(this); |
0 | 588 } |
589 | |
72 | 590 override Expression toExpression() |
0 | 591 { |
592 assert(false); | |
593 } | |
594 | |
72 | 595 override bool hasPointers() |
0 | 596 { |
597 return next.hasPointers(); | |
598 } | |
599 | |
600 version (CPP_MANGLE) { | |
601 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
602 { | |
603 assert(false); | |
604 } | |
605 } | |
606 | |
72 | 607 override type* toCtype() |
0 | 608 { |
609 if (!ctype) | |
610 { | |
611 type* tn = next.toCtype(); | |
612 ctype = type_allocn(TYarray, tn); | |
613 ctype.Tdim = cast(uint)dim.toInteger(); | |
614 } | |
615 | |
616 return ctype; | |
617 } | |
618 | |
72 | 619 override type* toCParamtype() |
0 | 620 { |
96 | 621 version(SARRAYVALUE) |
622 { | |
623 return toCtype(); | |
624 } | |
625 else | |
626 { | |
0 | 627 // arrays are passed as pointers |
628 return next.pointerTo().toCtype(); | |
96 | 629 } |
0 | 630 } |
72 | 631 } |