Mercurial > projects > ddmd
annotate dmd/TypeSArray.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | 1765f3ef917d |
children | af1bebfd96a4 |
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; |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
8 import dmd.Parameter; |
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); | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
199 auto tt = cast(TypeTuple)tbn; |
0 | 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 } | |
126
1765f3ef917d
ClassDeclarations, Arguments -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
121
diff
changeset
|
207 auto arg = tt.arguments[cast(size_t)d]; |
0 | 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 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
126
diff
changeset
|
336 e = e.semantic(sc); |
0 | 337 return e; |
338 } | |
339 | |
72 | 340 override bool isString() |
0 | 341 { |
64 | 342 TY nty = next.toBasetype().ty; |
343 return nty == Tchar || nty == Twchar || nty == Tdchar; | |
0 | 344 } |
345 | |
72 | 346 override bool isZeroInit(Loc loc) |
0 | 347 { |
348 return next.isZeroInit(loc); | |
349 } | |
350 | |
72 | 351 override uint memalign(uint salign) |
0 | 352 { |
353 return next.memalign(salign); | |
354 } | |
355 | |
72 | 356 override MATCH constConv(Type to) |
0 | 357 { |
358 assert(false); | |
359 } | |
360 | |
72 | 361 override MATCH implicitConvTo(Type to) |
0 | 362 { |
363 //printf("TypeSArray.implicitConvTo(to = %s) this = %s\n", to.toChars(), toChars()); | |
364 | |
365 // Allow implicit conversion of static array to pointer or dynamic array | |
366 if (IMPLICIT_ARRAY_TO_PTR && to.ty == Tpointer) | |
367 { | |
368 TypePointer tp = cast(TypePointer)to; | |
369 | |
370 if (next.mod != tp.next.mod && tp.next.mod != MODconst) | |
371 return MATCHnomatch; | |
372 | |
373 if (tp.next.ty == Tvoid || next.constConv(tp.next) != MATCHnomatch) | |
374 { | |
375 return MATCHconvert; | |
376 } | |
377 return MATCHnomatch; | |
378 } | |
379 if (to.ty == Tarray) | |
380 { | |
381 int offset = 0; | |
382 TypeDArray ta = cast(TypeDArray)to; | |
383 | |
384 if (next.mod != ta.next.mod && ta.next.mod != MODconst) | |
385 return MATCHnomatch; | |
386 | |
387 if (next.equals(ta.next) || | |
388 next.implicitConvTo(ta.next) >= MATCHconst || | |
389 (ta.next.isBaseOf(next, &offset) && offset == 0) || | |
390 ta.next.ty == Tvoid | |
391 ) | |
392 return MATCHconvert; | |
393 | |
394 return MATCHnomatch; | |
395 } | |
396 if (to.ty == Tsarray) | |
397 { | |
398 if (this == to) | |
399 return MATCHexact; | |
400 | |
401 TypeSArray tsa = cast(TypeSArray)to; | |
402 | |
403 if (dim.equals(tsa.dim)) | |
404 { | |
405 /* Since static arrays are value types, allow | |
406 * conversions from const elements to non-const | |
407 * ones, just like we allow conversion from const int | |
408 * to int. | |
409 */ | |
410 MATCH m = next.implicitConvTo(tsa.next); | |
411 if (m >= MATCHconst) | |
412 { | |
413 if (mod != to.mod) | |
414 m = MATCHconst; | |
415 return m; | |
416 } | |
417 } | |
418 } | |
419 return MATCHnomatch; | |
420 } | |
421 | |
72 | 422 override Expression defaultInit(Loc loc) |
0 | 423 { |
424 version (LOGDEFAULTINIT) { | |
425 printf("TypeSArray.defaultInit() '%s'\n", toChars()); | |
426 } | |
427 return next.defaultInit(loc); | |
428 } | |
429 | |
72 | 430 override dt_t** toDt(dt_t** pdt) |
0 | 431 { |
432 return toDtElem(pdt, null); | |
433 } | |
434 | |
435 dt_t** toDtElem(dt_t** pdt, Expression e) | |
436 { | |
437 int i; | |
438 | |
439 //printf("TypeSArray::toDtElem()\n"); | |
440 uint len = cast(uint)dim.toInteger(); | |
441 if (len) | |
442 { | |
443 while (*pdt) | |
444 pdt = &((*pdt).DTnext); | |
445 Type tnext = next; | |
446 Type tbn = tnext.toBasetype(); | |
447 while (tbn.ty == Tsarray) | |
448 { | |
449 TypeSArray tsa = cast(TypeSArray)tbn; | |
450 | |
451 len *= tsa.dim.toInteger(); | |
452 tnext = tbn.nextOf(); | |
453 tbn = tnext.toBasetype(); | |
454 } | |
455 if (!e) // if not already supplied | |
456 e = tnext.defaultInit(Loc(0)); // use default initializer | |
457 if (tbn.ty == Tstruct) | |
458 tnext.toDt(pdt); | |
459 else | |
460 e.toDt(pdt); | |
461 dt_optimize(*pdt); | |
462 if ((*pdt).dt == DT_azeros && !(*pdt).DTnext) | |
463 { | |
464 (*pdt).DTazeros *= len; | |
465 pdt = &((*pdt).DTnext); | |
466 } | |
467 else if ((*pdt).dt == DT_1byte && (*pdt).DTonebyte == 0 && !(*pdt).DTnext) | |
468 { | |
469 (*pdt).dt = DT_azeros; | |
470 (*pdt).DTazeros = len; | |
471 pdt = &((*pdt).DTnext); | |
472 } | |
473 else if (e.op != TOKstring) | |
474 { | |
475 for (i = 1; i < len; i++) | |
476 { | |
477 if (tbn.ty == Tstruct) | |
478 { | |
479 pdt = tnext.toDt(pdt); | |
480 while (*pdt) | |
481 pdt = &((*pdt).DTnext); | |
482 } | |
483 else | |
484 pdt = e.toDt(pdt); | |
485 } | |
486 } | |
487 } | |
488 return pdt; | |
489 } | |
490 | |
72 | 491 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 492 { |
51 | 493 static if (false) { |
494 printf("TypeSArray.deduceType()\n"); | |
495 printf("\tthis = %d, ", ty); print(); | |
496 printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
497 } | |
498 | |
499 // Extra check that array dimensions must match | |
500 if (tparam) | |
501 { | |
502 if (tparam.ty == Tsarray) | |
503 { | |
504 TypeSArray tp = cast(TypeSArray)tparam; | |
505 | |
506 if (tp.dim.op == TOKvar && (cast(VarExp)tp.dim).var.storage_class & STCtemplateparameter) | |
507 { | |
508 int i = templateIdentifierLookup((cast(VarExp)tp.dim).var.ident, parameters); | |
509 // This code matches code in TypeInstance.deduceType() | |
510 if (i == -1) | |
511 goto Lnomatch; | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
512 auto tp2 = parameters[i]; |
51 | 513 TemplateValueParameter tvp = tp2.isTemplateValueParameter(); |
514 if (!tvp) | |
515 goto Lnomatch; | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
516 Expression e = cast(Expression)dedtypes[i]; |
51 | 517 if (e) |
518 { | |
519 if (!dim.equals(e)) | |
520 goto Lnomatch; | |
521 } | |
522 else | |
523 { | |
524 Type vt = tvp.valType.semantic(Loc(0), sc); | |
525 MATCH m = cast(MATCH)dim.implicitConvTo(vt); | |
526 if (!m) | |
527 goto Lnomatch; | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
528 dedtypes[i] = dim; |
51 | 529 } |
530 } | |
531 else if (dim.toInteger() != tp.dim.toInteger()) | |
532 return MATCHnomatch; | |
533 } | |
534 else if (tparam.ty == Taarray) | |
535 { | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
536 auto tp = cast(TypeAArray)tparam; |
51 | 537 if (tp.index.ty == Tident) |
538 { | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
539 auto tident = cast(TypeIdentifier)tp.index; |
51 | 540 |
541 if (tident.idents.dim == 0) | |
542 { | |
543 Identifier id = tident.ident; | |
544 | |
545 for (size_t i = 0; i < parameters.dim; i++) | |
546 { | |
121
347de076ad34
TemplateParameters -> Vector
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
547 auto tp2 = parameters[i]; |
51 | 548 |
549 if (tp2.ident.equals(id)) | |
550 { | |
551 // Found the corresponding template parameter | |
552 TemplateValueParameter tvp = tp2.isTemplateValueParameter(); | |
553 if (!tvp || !tvp.valType.isintegral()) | |
554 goto Lnomatch; | |
555 | |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
556 if (dedtypes[i]) |
51 | 557 { |
113
3482c73a991b
More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
110
diff
changeset
|
558 if (!dim.equals(dedtypes[i])) |
51 | 559 goto Lnomatch; |
560 } | |
561 else | |
94
3a0b150c9841
Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
562 { dedtypes[i] = dim; |
51 | 563 } |
564 return next.deduceType(sc, tparam.nextOf(), parameters, dedtypes); | |
565 } | |
566 } | |
567 } | |
568 } | |
569 } | |
570 else if (tparam.ty == Tarray) | |
571 { | |
572 MATCH m; | |
573 | |
574 m = next.deduceType(sc, tparam.nextOf(), parameters, dedtypes); | |
575 if (m == MATCHexact) | |
576 m = MATCHconvert; | |
577 return m; | |
578 } | |
579 } | |
580 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
581 | |
582 Lnomatch: | |
583 return MATCHnomatch; | |
0 | 584 } |
585 | |
72 | 586 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 587 { |
64 | 588 return new TypeInfoStaticArrayDeclaration(this); |
0 | 589 } |
590 | |
72 | 591 override Expression toExpression() |
0 | 592 { |
593 assert(false); | |
594 } | |
595 | |
72 | 596 override bool hasPointers() |
0 | 597 { |
598 return next.hasPointers(); | |
599 } | |
600 | |
601 version (CPP_MANGLE) { | |
602 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
603 { | |
604 assert(false); | |
605 } | |
606 } | |
607 | |
72 | 608 override type* toCtype() |
0 | 609 { |
610 if (!ctype) | |
611 { | |
612 type* tn = next.toCtype(); | |
613 ctype = type_allocn(TYarray, tn); | |
614 ctype.Tdim = cast(uint)dim.toInteger(); | |
615 } | |
616 | |
617 return ctype; | |
618 } | |
619 | |
72 | 620 override type* toCParamtype() |
0 | 621 { |
96 | 622 version(SARRAYVALUE) |
623 { | |
624 return toCtype(); | |
625 } | |
626 else | |
627 { | |
0 | 628 // arrays are passed as pointers |
629 return next.pointerTo().toCtype(); | |
96 | 630 } |
0 | 631 } |
72 | 632 } |