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