Mercurial > projects > ddmd
annotate dmd/TypeDArray.d @ 12:832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
Some unittest-specific functions implemented
author | korDen |
---|---|
date | Mon, 12 Apr 2010 15:13:00 +0400 |
parents | 10317f0c89a5 |
children | 460959608115 |
rev | line source |
---|---|
0 | 1 module dmd.TypeDArray; |
2 | |
3 import dmd.TypeArray; | |
4 import dmd.MOD; | |
5 import dmd.Id; | |
6 import dmd.TOK; | |
7 import dmd.StringExp; | |
8 import dmd.IntegerExp; | |
9 import dmd.ArrayLengthExp; | |
10 import dmd.Type; | |
11 import dmd.Loc; | |
12 import dmd.Scope; | |
13 import dmd.OutBuffer; | |
14 import dmd.HdrGenState; | |
15 import dmd.Expression; | |
16 import dmd.Identifier; | |
17 import dmd.MATCH; | |
18 import dmd.ArrayTypes; | |
19 import dmd.TypeInfoDeclaration; | |
20 import dmd.TypeInfoArrayDeclaration; | |
21 import dmd.NullExp; | |
22 import dmd.TY; | |
23 import dmd.TypeStruct; | |
24 import dmd.Util; | |
25 import dmd.TypePointer; | |
26 import dmd.Global; | |
27 | |
28 import dmd.backend.TYPE; | |
29 import dmd.backend.Symbol; | |
30 import dmd.backend.Classsym; | |
31 import dmd.backend.Util; | |
32 import dmd.backend.SC; | |
33 import dmd.backend.TYM; | |
34 import dmd.backend.LIST; | |
35 | |
36 import core.stdc.stdlib; | |
37 import core.stdc.stdio; | |
38 | |
39 // Dynamic array, no dimension | |
40 class TypeDArray : TypeArray | |
41 { | |
42 this(Type t) | |
43 { | |
44 super(TY.Tarray, t); | |
45 //printf("TypeDArray(t = %p)\n", t); | |
46 } | |
47 | |
48 version (DumbClone) { | |
49 } else { | |
50 Type clone() | |
51 { | |
52 assert(false); | |
53 } | |
54 } | |
55 Type syntaxCopy() | |
56 { | |
57 Type t = next.syntaxCopy(); | |
58 if (t == next) | |
59 t = this; | |
60 else | |
61 { | |
62 t = new TypeDArray(t); | |
63 t.mod = mod; | |
64 } | |
65 return t; | |
66 } | |
67 | |
68 ulong size(Loc loc) | |
69 { | |
70 //printf("TypeDArray.size()\n"); | |
71 return PTRSIZE * 2; | |
72 } | |
73 | |
74 uint alignsize() | |
75 { | |
76 // A DArray consists of two ptr-sized values, so align it on pointer size | |
77 // boundary | |
78 return PTRSIZE; | |
79 } | |
80 | |
81 Type semantic(Loc loc, Scope sc) | |
82 { | |
83 Type tn = next; | |
84 | |
85 tn = next.semantic(loc,sc); | |
86 Type tbn = tn.toBasetype(); | |
87 switch (tbn.ty) | |
88 { | |
89 case TY.Tfunction: | |
90 case TY.Tnone: | |
91 case TY.Ttuple: | |
92 error(loc, "can't have array of %s", tbn.toChars()); | |
93 tn = next = tint32; | |
94 break; | |
95 case TY.Tstruct: | |
96 { | |
97 TypeStruct ts = cast(TypeStruct)tbn; | |
98 if (ts.sym.isnested) | |
99 error(loc, "cannot have array of inner structs %s", ts.toChars()); | |
100 break; | |
101 } | |
102 | |
103 default: | |
104 break; /// | |
105 } | |
106 if (tn.isauto()) | |
107 error(loc, "cannot have array of auto %s", tn.toChars()); | |
108 | |
109 next = tn; | |
110 transitive(); | |
111 return merge(); | |
112 } | |
113 | |
114 void toDecoBuffer(OutBuffer buf, int flag) | |
115 { | |
116 Type.toDecoBuffer(buf, flag); | |
117 if (next) | |
118 next.toDecoBuffer(buf, (flag & 0x100) ? 0 : mod); | |
119 } | |
120 | |
121 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) | |
122 { | |
123 if (mod != this.mod) | |
124 { | |
125 toCBuffer3(buf, hgs, mod); | |
126 return; | |
127 } | |
128 next.toCBuffer2(buf, hgs, this.mod); | |
129 buf.writestring("[]"); | |
130 } | |
131 | |
132 Expression dotExp(Scope sc, Expression e, Identifier ident) | |
133 { | |
134 version (LOGDOTEXP) { | |
135 printf("TypeDArray.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars()); | |
136 } | |
137 if (ident is Id.length) | |
138 { | |
139 if (e.op == TOK.TOKstring) | |
140 { | |
141 StringExp se = cast(StringExp)e; | |
142 | |
143 return new IntegerExp(se.loc, se.len, Type.tindex); | |
144 } | |
145 e = new ArrayLengthExp(e.loc, e); | |
146 e.type = Type.tsize_t; | |
147 return e; | |
148 } | |
149 else if (ident is Id.ptr) | |
150 { | |
151 e = e.castTo(sc, next.pointerTo()); | |
152 return e; | |
153 } | |
154 else | |
155 { | |
156 e = TypeArray.dotExp(sc, e, ident); | |
157 } | |
158 return e; | |
159 } | |
160 | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
161 bool isString() |
0 | 162 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
163 TY nty = next.toBasetype().ty; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
164 return nty == Tchar || nty == Twchar || nty == Tdchar; |
0 | 165 } |
166 | |
167 bool isZeroInit(Loc loc) | |
168 { | |
169 return true; | |
170 } | |
171 | |
172 bool checkBoolean() | |
173 { | |
174 return true; | |
175 } | |
176 | |
177 MATCH implicitConvTo(Type to) | |
178 { | |
179 //printf("TypeDArray.implicitConvTo(to = %s) this = %s\n", to.toChars(), toChars()); | |
180 if (equals(to)) | |
181 return MATCHexact; | |
182 | |
183 // Allow implicit conversion of array to pointer | |
184 if (IMPLICIT_ARRAY_TO_PTR && to.ty == Tpointer) | |
185 { | |
186 TypePointer tp = cast(TypePointer)to; | |
187 | |
188 /* Allow conversion to void* | |
189 */ | |
190 if (tp.next.ty == Tvoid && | |
191 (next.mod == tp.next.mod || tp.next.mod == MODconst)) | |
192 { | |
193 return MATCHconvert; | |
194 } | |
195 | |
196 return next.constConv(to); | |
197 } | |
198 | |
199 if (to.ty == Tarray) | |
200 { | |
201 int offset = 0; | |
202 TypeDArray ta = cast(TypeDArray)to; | |
203 | |
204 if (!(next.mod == ta.next.mod || ta.next.mod == MODconst)) | |
205 return MATCHnomatch; // not const-compatible | |
206 | |
207 /* Allow conversion to void[] | |
208 */ | |
209 if (next.ty != Tvoid && ta.next.ty == Tvoid) | |
210 { | |
211 return MATCHconvert; | |
212 } | |
213 | |
214 MATCH m = next.constConv(ta.next); | |
215 if (m != MATCHnomatch) | |
216 { | |
217 if (m == MATCHexact && mod != to.mod) | |
218 m = MATCHconst; | |
219 return m; | |
220 } | |
221 | |
222 /* Allow conversions of T[][] to const(T)[][] | |
223 */ | |
224 if (mod == ta.mod && next.ty == Tarray && ta.next.ty == Tarray) | |
225 { | |
226 m = next.implicitConvTo(ta.next); | |
227 if (m == MATCHconst) | |
228 return m; | |
229 } | |
230 | |
231 /* Conversion of array of derived to array of base | |
232 */ | |
233 if (ta.next.isBaseOf(next, &offset) && offset == 0) | |
234 return MATCHconvert; | |
235 } | |
236 return Type.implicitConvTo(to); | |
237 } | |
238 | |
239 Expression defaultInit(Loc loc) | |
240 { | |
241 version (LOGDEFAULTINIT) { | |
242 printf("TypeDArray.defaultInit() '%s'\n", toChars()); | |
243 } | |
244 Expression e = new NullExp(loc); | |
245 e.type = this; | |
246 return e; | |
247 } | |
248 | |
249 bool builtinTypeInfo() | |
250 { | |
251 version (DMDV2) { | |
252 return !mod && (next.isTypeBasic() !is null && !next.mod || | |
253 // strings are so common, make them builtin | |
254 next.ty == Tchar && next.mod == MODinvariant); | |
255 } else { | |
256 return next.isTypeBasic() !is null; | |
257 } | |
258 } | |
259 version (DMDV2) { | |
260 MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) | |
261 { | |
262 static if (false) { | |
263 printf("TypeDArray.deduceType()\n"); | |
264 printf("\tthis = %d, ", ty); print(); | |
265 printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
266 } | |
267 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
268 | |
269 Lnomatch: | |
270 return MATCHnomatch; | |
271 } | |
272 } | |
273 TypeInfoDeclaration getTypeInfoDeclaration() | |
274 { | |
275 return new TypeInfoArrayDeclaration(this); | |
276 } | |
277 | |
278 bool hasPointers() | |
279 { | |
280 return true; | |
281 } | |
282 | |
283 version (CPP_MANGLE) { | |
284 void toCppMangle(OutBuffer buf, CppMangleState* cms); | |
285 } | |
286 | |
287 type* toCtype() | |
288 { | |
289 type *t; | |
290 | |
291 if (ctype) | |
292 return ctype; | |
293 | |
294 if (0 && global.params.symdebug) | |
295 { | |
296 /* Create a C type out of: | |
297 * struct _Array_T { size_t length; T* data; } | |
298 */ | |
299 Symbol* s; | |
300 char *id; | |
301 | |
302 assert(next.deco); | |
303 id = cast(char*) alloca(7 + next.deco.length + 1); | |
304 sprintf(id, "_Array_%.*s", next.deco); | |
305 s = symbol_calloc(id); | |
306 s.Sclass = SC.SCstruct; | |
307 s.Sstruct = struct_calloc(); | |
308 s.Sstruct.Sflags |= 0; | |
309 s.Sstruct.Salignsize = alignsize(); | |
310 s.Sstruct.Sstructalign = cast(ubyte)global.structalign; | |
311 s.Sstruct.Sstructsize = cast(uint)size(Loc(0)); | |
312 slist_add(s); | |
313 | |
314 Symbol* s1 = symbol_name("length", SC.SCmember, Type.tsize_t.toCtype()); | |
315 list_append(&s.Sstruct.Sfldlst, s1); | |
316 | |
317 Symbol* s2 = symbol_name("data", SC.SCmember, next.pointerTo().toCtype()); | |
318 s2.Smemoff = cast(uint)Type.tsize_t.size(); | |
319 list_append(&s.Sstruct.Sfldlst, s2); | |
320 | |
321 t = type_alloc(TYM.TYstruct); | |
322 t.Ttag = cast(Classsym*)s; // structure tag name | |
323 t.Tcount++; | |
324 s.Stype = t; | |
325 } | |
326 else | |
327 { | |
328 if (global.params.symdebug == 1) | |
329 { | |
330 // Generate D symbolic debug info, rather than C | |
331 t = type_allocn(TYM.TYdarray, next.toCtype()); | |
332 } | |
333 else | |
334 t = type_fake(TYM.TYdarray); | |
335 } | |
336 t.Tcount++; | |
337 ctype = t; | |
338 return t; | |
339 } | |
340 } |