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