Mercurial > projects > ddmd
annotate dmd/TypeDArray.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 | 190ba98276b3 |
children |
rev | line source |
---|---|
0 | 1 module dmd.TypeDArray; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.TypeArray; |
5 import dmd.MOD; | |
6 import dmd.Id; | |
7 import dmd.TOK; | |
8 import dmd.StringExp; | |
9 import dmd.IntegerExp; | |
10 import dmd.ArrayLengthExp; | |
11 import dmd.Type; | |
12 import dmd.Loc; | |
13 import dmd.Scope; | |
14 import dmd.OutBuffer; | |
15 import dmd.HdrGenState; | |
16 import dmd.Expression; | |
17 import dmd.Identifier; | |
18 import dmd.MATCH; | |
19 import dmd.ArrayTypes; | |
20 import dmd.TypeInfoDeclaration; | |
21 import dmd.TypeInfoArrayDeclaration; | |
22 import dmd.NullExp; | |
23 import dmd.TY; | |
24 import dmd.TypeStruct; | |
25 import dmd.Util; | |
26 import dmd.TypePointer; | |
27 import dmd.Global; | |
28 | |
29 import dmd.backend.TYPE; | |
30 import dmd.backend.Symbol; | |
31 import dmd.backend.Classsym; | |
32 import dmd.backend.Util; | |
33 import dmd.backend.SC; | |
34 import dmd.backend.TYM; | |
35 import dmd.backend.LIST; | |
36 | |
37 import core.stdc.stdlib; | |
38 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
|
39 version (Bug4054) import core.memory; |
0 | 40 |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
183
diff
changeset
|
41 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
183
diff
changeset
|
42 |
0 | 43 // Dynamic array, no dimension |
44 class TypeDArray : TypeArray | |
45 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
183
diff
changeset
|
46 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
183
diff
changeset
|
47 |
0 | 48 this(Type t) |
49 { | |
178 | 50 register(); |
0 | 51 super(TY.Tarray, t); |
52 //printf("TypeDArray(t = %p)\n", t); | |
53 } | |
54 | |
72 | 55 override Type syntaxCopy() |
0 | 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 | |
72 | 68 override ulong size(Loc loc) |
0 | 69 { |
70 //printf("TypeDArray.size()\n"); | |
71 return PTRSIZE * 2; | |
72 } | |
73 | |
72 | 74 override uint alignsize() |
0 | 75 { |
76 // A DArray consists of two ptr-sized values, so align it on pointer size | |
77 // boundary | |
78 return PTRSIZE; | |
79 } | |
80 | |
72 | 81 override Type semantic(Loc loc, Scope sc) |
0 | 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 | |
72 | 114 override void toDecoBuffer(OutBuffer buf, int flag) |
0 | 115 { |
116 Type.toDecoBuffer(buf, flag); | |
117 if (next) | |
118 next.toDecoBuffer(buf, (flag & 0x100) ? 0 : mod); | |
119 } | |
120 | |
96 | 121 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 122 { |
123 if (mod != this.mod) | |
124 { | |
125 toCBuffer3(buf, hgs, mod); | |
126 return; | |
127 } | |
176 | 128 if (equals(global.tstring)) |
96 | 129 buf.writestring("string"); |
130 else | |
131 { | |
132 next.toCBuffer2(buf, hgs, this.mod); | |
133 buf.writestring("[]"); | |
134 } | |
0 | 135 } |
136 | |
72 | 137 override Expression dotExp(Scope sc, Expression e, Identifier ident) |
0 | 138 { |
139 version (LOGDOTEXP) { | |
140 printf("TypeDArray.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars()); | |
141 } | |
142 if (ident is Id.length) | |
143 { | |
144 if (e.op == TOK.TOKstring) | |
145 { | |
146 StringExp se = cast(StringExp)e; | |
147 | |
148 return new IntegerExp(se.loc, se.len, Type.tindex); | |
149 } | |
150 e = new ArrayLengthExp(e.loc, e); | |
151 e.type = Type.tsize_t; | |
152 return e; | |
153 } | |
154 else if (ident is Id.ptr) | |
155 { | |
156 e = e.castTo(sc, next.pointerTo()); | |
157 return e; | |
158 } | |
159 else | |
160 { | |
161 e = TypeArray.dotExp(sc, e, ident); | |
162 } | |
163 return e; | |
164 } | |
165 | |
72 | 166 override bool isString() |
0 | 167 { |
12
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
168 TY nty = next.toBasetype().ty; |
832f71e6f96c
*Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents:
0
diff
changeset
|
169 return nty == Tchar || nty == Twchar || nty == Tdchar; |
0 | 170 } |
171 | |
72 | 172 override bool isZeroInit(Loc loc) |
0 | 173 { |
174 return true; | |
175 } | |
176 | |
72 | 177 override bool checkBoolean() |
0 | 178 { |
179 return true; | |
180 } | |
181 | |
72 | 182 override MATCH implicitConvTo(Type to) |
0 | 183 { |
184 //printf("TypeDArray.implicitConvTo(to = %s) this = %s\n", to.toChars(), toChars()); | |
185 if (equals(to)) | |
186 return MATCHexact; | |
187 | |
188 // Allow implicit conversion of array to pointer | |
189 if (IMPLICIT_ARRAY_TO_PTR && to.ty == Tpointer) | |
190 { | |
191 TypePointer tp = cast(TypePointer)to; | |
192 | |
193 /* Allow conversion to void* | |
194 */ | |
195 if (tp.next.ty == Tvoid && | |
135 | 196 MODimplicitConv(next.mod, tp.next.mod)) |
0 | 197 { |
198 return MATCHconvert; | |
199 } | |
200 | |
201 return next.constConv(to); | |
202 } | |
203 | |
204 if (to.ty == Tarray) | |
205 { | |
206 int offset = 0; | |
207 TypeDArray ta = cast(TypeDArray)to; | |
208 | |
135 | 209 if (!MODimplicitConv(next.mod, ta.next.mod)) |
0 | 210 return MATCHnomatch; // not const-compatible |
211 | |
212 /* Allow conversion to void[] | |
213 */ | |
214 if (next.ty != Tvoid && ta.next.ty == Tvoid) | |
215 { | |
216 return MATCHconvert; | |
217 } | |
218 | |
219 MATCH m = next.constConv(ta.next); | |
220 if (m != MATCHnomatch) | |
221 { | |
222 if (m == MATCHexact && mod != to.mod) | |
223 m = MATCHconst; | |
224 return m; | |
225 } | |
226 | |
135 | 227 static if(false) { |
0 | 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 } | |
135 | 236 } |
0 | 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 } | |
135 | 250 return new NullExp(loc, this); |
0 | 251 } |
252 | |
72 | 253 override bool builtinTypeInfo() |
0 | 254 { |
255 version (DMDV2) { | |
256 return !mod && (next.isTypeBasic() !is null && !next.mod || | |
257 // strings are so common, make them builtin | |
135 | 258 next.ty == Tchar && next.mod == MODimmutable); |
0 | 259 } else { |
260 return next.isTypeBasic() !is null; | |
261 } | |
262 } | |
263 version (DMDV2) { | |
72 | 264 override MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) |
0 | 265 { |
266 static if (false) { | |
267 printf("TypeDArray.deduceType()\n"); | |
268 printf("\tthis = %d, ", ty); print(); | |
269 printf("\ttparam = %d, ", tparam.ty); tparam.print(); | |
270 } | |
271 return Type.deduceType(sc, tparam, parameters, dedtypes); | |
272 | |
273 Lnomatch: | |
274 return MATCHnomatch; | |
275 } | |
276 } | |
72 | 277 override TypeInfoDeclaration getTypeInfoDeclaration() |
0 | 278 { |
279 return new TypeInfoArrayDeclaration(this); | |
280 } | |
281 | |
72 | 282 override bool hasPointers() |
0 | 283 { |
284 return true; | |
285 } | |
286 | |
287 version (CPP_MANGLE) { | |
183
190ba98276b3
Several changes to make it build on posix systems.
Jacob Carlborg <doob@me.com>
parents:
178
diff
changeset
|
288 void toCppMangle(OutBuffer buf, CppMangleState* cms) |
190ba98276b3
Several changes to make it build on posix systems.
Jacob Carlborg <doob@me.com>
parents:
178
diff
changeset
|
289 { |
190ba98276b3
Several changes to make it build on posix systems.
Jacob Carlborg <doob@me.com>
parents:
178
diff
changeset
|
290 assert(false); |
190ba98276b3
Several changes to make it build on posix systems.
Jacob Carlborg <doob@me.com>
parents:
178
diff
changeset
|
291 } |
0 | 292 } |
293 | |
72 | 294 override type* toCtype() |
0 | 295 { |
296 type *t; | |
297 | |
298 if (ctype) | |
299 return ctype; | |
300 | |
301 if (0 && global.params.symdebug) | |
302 { | |
303 /* Create a C type out of: | |
304 * struct _Array_T { size_t length; T* data; } | |
305 */ | |
306 Symbol* s; | |
307 char *id; | |
308 | |
309 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
|
310 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
|
311 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
|
312 else |
0 | 313 id = cast(char*) alloca(7 + next.deco.length + 1); |
314 sprintf(id, "_Array_%.*s", next.deco); | |
315 s = symbol_calloc(id); | |
316 s.Sclass = SC.SCstruct; | |
317 s.Sstruct = struct_calloc(); | |
318 s.Sstruct.Sflags |= 0; | |
319 s.Sstruct.Salignsize = alignsize(); | |
320 s.Sstruct.Sstructalign = cast(ubyte)global.structalign; | |
321 s.Sstruct.Sstructsize = cast(uint)size(Loc(0)); | |
322 slist_add(s); | |
323 | |
324 Symbol* s1 = symbol_name("length", SC.SCmember, Type.tsize_t.toCtype()); | |
325 list_append(&s.Sstruct.Sfldlst, s1); | |
326 | |
327 Symbol* s2 = symbol_name("data", SC.SCmember, next.pointerTo().toCtype()); | |
328 s2.Smemoff = cast(uint)Type.tsize_t.size(); | |
329 list_append(&s.Sstruct.Sfldlst, s2); | |
330 | |
331 t = type_alloc(TYM.TYstruct); | |
332 t.Ttag = cast(Classsym*)s; // structure tag name | |
333 t.Tcount++; | |
334 s.Stype = t; | |
335 } | |
336 else | |
337 { | |
338 if (global.params.symdebug == 1) | |
339 { | |
340 // Generate D symbolic debug info, rather than C | |
341 t = type_allocn(TYM.TYdarray, next.toCtype()); | |
342 } | |
343 else | |
344 t = type_fake(TYM.TYdarray); | |
345 } | |
346 t.Tcount++; | |
347 ctype = t; | |
348 return t; | |
349 } | |
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
|
350 } |