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