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