Mercurial > projects > ddmd
annotate dmd/TypeDArray.d @ 23:460959608115
Branch merge.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Mon, 12 Apr 2010 17:00:08 +0100 |
parents | 832f71e6f96c 01cadcfa4842 |
children | 2e2a5c3f943a |
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 } | |
56 Type syntaxCopy() | |
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 | |
69 ulong size(Loc loc) | |
70 { | |
71 //printf("TypeDArray.size()\n"); | |
72 return PTRSIZE * 2; | |
73 } | |
74 | |
75 uint alignsize() | |
76 { | |
77 // A DArray consists of two ptr-sized values, so align it on pointer size | |
78 // boundary | |
79 return PTRSIZE; | |
80 } | |
81 | |
82 Type semantic(Loc loc, Scope sc) | |
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 | |
115 void toDecoBuffer(OutBuffer buf, int flag) | |
116 { | |
117 Type.toDecoBuffer(buf, flag); | |
118 if (next) | |
119 next.toDecoBuffer(buf, (flag & 0x100) ? 0 : mod); | |
120 } | |
121 | |
122 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) | |
123 { | |
124 if (mod != this.mod) | |
125 { | |
126 toCBuffer3(buf, hgs, mod); | |
127 return; | |
128 } | |
129 next.toCBuffer2(buf, hgs, this.mod); | |
130 buf.writestring("[]"); | |
131 } | |
132 | |
133 Expression dotExp(Scope sc, Expression e, Identifier ident) | |
134 { | |
135 version (LOGDOTEXP) { | |
136 printf("TypeDArray.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars()); | |
137 } | |
138 if (ident is Id.length) | |
139 { | |
140 if (e.op == TOK.TOKstring) | |
141 { | |
142 StringExp se = cast(StringExp)e; | |
143 | |
144 return new IntegerExp(se.loc, se.len, Type.tindex); | |
145 } | |
146 e = new ArrayLengthExp(e.loc, e); | |
147 e.type = Type.tsize_t; | |
148 return e; | |
149 } | |
150 else if (ident is Id.ptr) | |
151 { | |
152 e = e.castTo(sc, next.pointerTo()); | |
153 return e; | |
154 } | |
155 else | |
156 { | |
157 e = TypeArray.dotExp(sc, e, ident); | |
158 } | |
159 return e; | |
160 } | |
161 | |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
162 bool isString() |
0 | 163 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
164 TY nty = next.toBasetype().ty; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
165 return nty == Tchar || nty == Twchar || nty == Tdchar; |
0 | 166 } |
167 | |
168 bool isZeroInit(Loc loc) | |
169 { | |
170 return true; | |
171 } | |
172 | |
173 bool checkBoolean() | |
174 { | |
175 return true; | |
176 } | |
177 | |
178 MATCH implicitConvTo(Type to) | |
179 { | |
180 //printf("TypeDArray.implicitConvTo(to = %s) this = %s\n", to.toChars(), toChars()); | |
181 if (equals(to)) | |
182 return MATCHexact; | |
183 | |
184 // Allow implicit conversion of array to pointer | |
185 if (IMPLICIT_ARRAY_TO_PTR && to.ty == Tpointer) | |
186 { | |
187 TypePointer tp = cast(TypePointer)to; | |
188 | |
189 /* Allow conversion to void* | |
190 */ | |
191 if (tp.next.ty == Tvoid && | |
192 (next.mod == tp.next.mod || tp.next.mod == MODconst)) | |
193 { | |
194 return MATCHconvert; | |
195 } | |
196 | |
197 return next.constConv(to); | |
198 } | |
199 | |
200 if (to.ty == Tarray) | |
201 { | |
202 int offset = 0; | |
203 TypeDArray ta = cast(TypeDArray)to; | |
204 | |
205 if (!(next.mod == ta.next.mod || ta.next.mod == MODconst)) | |
206 return MATCHnomatch; // not const-compatible | |
207 | |
208 /* Allow conversion to void[] | |
209 */ | |
210 if (next.ty != Tvoid && ta.next.ty == Tvoid) | |
211 { | |
212 return MATCHconvert; | |
213 } | |
214 | |
215 MATCH m = next.constConv(ta.next); | |
216 if (m != MATCHnomatch) | |
217 { | |
218 if (m == MATCHexact && mod != to.mod) | |
219 m = MATCHconst; | |
220 return m; | |
221 } | |
222 | |
223 /* Allow conversions of T[][] to const(T)[][] | |
224 */ | |
225 if (mod == ta.mod && next.ty == Tarray && ta.next.ty == Tarray) | |
226 { | |
227 m = next.implicitConvTo(ta.next); | |
228 if (m == MATCHconst) | |
229 return m; | |
230 } | |
231 | |
232 /* Conversion of array of derived to array of base | |
233 */ | |
234 if (ta.next.isBaseOf(next, &offset) && offset == 0) | |
235 return MATCHconvert; | |
236 } | |
237 return Type.implicitConvTo(to); | |
238 } | |
239 | |
240 Expression defaultInit(Loc loc) | |
241 { | |
242 version (LOGDEFAULTINIT) { | |
243 printf("TypeDArray.defaultInit() '%s'\n", toChars()); | |
244 } | |
245 Expression e = new NullExp(loc); | |
246 e.type = this; | |
247 return e; | |
248 } | |
249 | |
250 bool builtinTypeInfo() | |
251 { | |
252 version (DMDV2) { | |
253 return !mod && (next.isTypeBasic() !is null && !next.mod || | |
254 // strings are so common, make them builtin | |
255 next.ty == Tchar && next.mod == MODinvariant); | |
256 } else { | |
257 return next.isTypeBasic() !is null; | |
258 } | |
259 } | |
260 version (DMDV2) { | |
261 MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) | |
262 { | |
263 static if (false) { | |
264 printf("TypeDArray.deduceType()\n"); | |
265 printf("\tthis = %d, ", ty); print(); | |
266 printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
267 } | |
268 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
269 | |
270 Lnomatch: | |
271 return MATCHnomatch; | |
272 } | |
273 } | |
274 TypeInfoDeclaration getTypeInfoDeclaration() | |
275 { | |
276 return new TypeInfoArrayDeclaration(this); | |
277 } | |
278 | |
279 bool hasPointers() | |
280 { | |
281 return true; | |
282 } | |
283 | |
284 version (CPP_MANGLE) { | |
285 void toCppMangle(OutBuffer buf, CppMangleState* cms); | |
286 } | |
287 | |
288 type* toCtype() | |
289 { | |
290 type *t; | |
291 | |
292 if (ctype) | |
293 return ctype; | |
294 | |
295 if (0 && global.params.symdebug) | |
296 { | |
297 /* Create a C type out of: | |
298 * struct _Array_T { size_t length; T* data; } | |
299 */ | |
300 Symbol* s; | |
301 char *id; | |
302 | |
303 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
|
304 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
|
305 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
|
306 else |
0 | 307 id = cast(char*) alloca(7 + next.deco.length + 1); |
308 sprintf(id, "_Array_%.*s", next.deco); | |
309 s = symbol_calloc(id); | |
310 s.Sclass = SC.SCstruct; | |
311 s.Sstruct = struct_calloc(); | |
312 s.Sstruct.Sflags |= 0; | |
313 s.Sstruct.Salignsize = alignsize(); | |
314 s.Sstruct.Sstructalign = cast(ubyte)global.structalign; | |
315 s.Sstruct.Sstructsize = cast(uint)size(Loc(0)); | |
316 slist_add(s); | |
317 | |
318 Symbol* s1 = symbol_name("length", SC.SCmember, Type.tsize_t.toCtype()); | |
319 list_append(&s.Sstruct.Sfldlst, s1); | |
320 | |
321 Symbol* s2 = symbol_name("data", SC.SCmember, next.pointerTo().toCtype()); | |
322 s2.Smemoff = cast(uint)Type.tsize_t.size(); | |
323 list_append(&s.Sstruct.Sfldlst, s2); | |
324 | |
325 t = type_alloc(TYM.TYstruct); | |
326 t.Ttag = cast(Classsym*)s; // structure tag name | |
327 t.Tcount++; | |
328 s.Stype = t; | |
329 } | |
330 else | |
331 { | |
332 if (global.params.symdebug == 1) | |
333 { | |
334 // Generate D symbolic debug info, rather than C | |
335 t = type_allocn(TYM.TYdarray, next.toCtype()); | |
336 } | |
337 else | |
338 t = type_fake(TYM.TYdarray); | |
339 } | |
340 t.Tcount++; | |
341 ctype = t; | |
342 return t; | |
343 } | |
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
|
344 } |