Mercurial > projects > ddmd
annotate dmd/TypeAArray.d @ 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.
author | Robert Clipsham <robert@octarineparrot.com> |
---|---|
date | Sun, 04 Apr 2010 02:06:32 +0100 |
parents | 10317f0c89a5 |
children | b7d29f613539 |
rev | line source |
---|---|
0 | 1 module dmd.TypeAArray; |
2 | |
3 import dmd.TypeArray; | |
4 import dmd.MOD; | |
5 import dmd.ArrayTypes; | |
6 import dmd.TypeInfoDeclaration; | |
7 import dmd.Expression; | |
8 import dmd.Scope; | |
9 import dmd.Loc; | |
10 import dmd.Dsymbol; | |
11 import dmd.Type; | |
12 import dmd.TypeSArray; | |
13 import dmd.OutBuffer; | |
14 import dmd.HdrGenState; | |
15 import dmd.Identifier; | |
16 import dmd.MATCH; | |
17 import dmd.TY; | |
18 import dmd.Id; | |
19 import dmd.CallExp; | |
20 import dmd.IntegerExp; | |
21 import dmd.FuncDeclaration; | |
22 import dmd.VarExp; | |
23 import dmd.TypeFunction; | |
24 import dmd.NullExp; | |
25 import dmd.Array; | |
26 | |
27 import dmd.backend.Symbol; | |
28 import dmd.backend.TYPE; | |
29 import dmd.backend.Util; | |
30 import dmd.backend.SC; | |
31 import dmd.backend.LIST; | |
32 import dmd.backend.TYM; | |
33 import dmd.backend.TF; | |
34 import dmd.backend.mTYman; | |
35 | |
36 import core.stdc.stdio; | |
37 import core.stdc.stdlib; | |
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) |
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 import core.memory; |
0 | 40 class TypeAArray : TypeArray |
41 { | |
42 Type index; // key type | |
43 | |
44 this(Type t, Type index) | |
45 { | |
46 super(Taarray, t); | |
47 this.index = index; | |
48 } | |
49 | |
50 Type syntaxCopy() | |
51 { | |
52 assert(false); | |
53 } | |
54 | |
55 version (DumbClone) { | |
56 } else { | |
57 Type clone() | |
58 { | |
59 assert(false); | |
60 } | |
61 } | |
62 ulong size(Loc loc) | |
63 { | |
64 return PTRSIZE /* * 2*/; | |
65 } | |
66 | |
67 Type semantic(Loc loc, Scope sc) | |
68 { | |
69 //printf("TypeAArray::semantic() %s index.ty = %d\n", toChars(), index.ty); | |
70 | |
71 // Deal with the case where we thought the index was a type, but | |
72 // in reality it was an expression. | |
73 if (index.ty == Tident || index.ty == Tinstance || index.ty == Tsarray) | |
74 { | |
75 Expression e; | |
76 Type t; | |
77 Dsymbol s; | |
78 | |
79 index.resolve(loc, sc, &e, &t, &s); | |
80 if (e) | |
81 { // It was an expression - | |
82 // Rewrite as a static array | |
83 TypeSArray tsa = new TypeSArray(next, e); | |
84 return tsa.semantic(loc,sc); | |
85 } | |
86 else if (t) | |
87 index = t; | |
88 else | |
89 index.error(loc, "index is not a type or an expression"); | |
90 } | |
91 else | |
92 index = index.semantic(loc,sc); | |
93 | |
94 if (index.nextOf() && !index.nextOf().isInvariant()) | |
95 { | |
96 index = index.constOf().mutableOf(); | |
97 static if (false) { | |
98 printf("index is %p %s\n", index, index.toChars()); | |
99 index.check(); | |
100 printf("index.mod = x%x\n", index.mod); | |
101 printf("index.ito = x%x\n", index.ito); | |
102 if (index.ito) { | |
103 printf("index.ito.mod = x%x\n", index.ito.mod); | |
104 printf("index.ito.ito = x%x\n", index.ito.ito); | |
105 } | |
106 } | |
107 } | |
108 | |
109 switch (index.toBasetype().ty) | |
110 { | |
111 case Tbool: | |
112 case Tfunction: | |
113 case Tvoid: | |
114 case Tnone: | |
115 error(loc, "can't have associative array key of %s", index.toBasetype().toChars()); | |
116 break; | |
117 default: | |
118 break; /// | |
119 } | |
120 next = next.semantic(loc,sc); | |
121 transitive(); | |
122 | |
123 switch (next.toBasetype().ty) | |
124 { | |
125 case Tfunction: | |
126 case Tnone: | |
127 error(loc, "can't have associative array of %s", next.toChars()); | |
128 break; | |
129 default: | |
130 break; /// | |
131 } | |
132 if (next.isauto()) | |
133 error(loc, "cannot have array of auto %s", next.toChars()); | |
134 | |
135 return merge(); | |
136 } | |
137 | |
138 void resolve(Loc loc, Scope sc, Expression* pe, Type* pt, Dsymbol* ps) | |
139 { | |
140 assert(false); | |
141 } | |
142 | |
143 void toDecoBuffer(OutBuffer buf, int flag) | |
144 { | |
145 Type.toDecoBuffer(buf, flag); | |
146 index.toDecoBuffer(buf); | |
147 next.toDecoBuffer(buf, (flag & 0x100) ? MOD.MODundefined : mod); | |
148 } | |
149 | |
150 void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) | |
151 { | |
152 assert(false); | |
153 } | |
154 | |
155 Expression dotExp(Scope sc, Expression e, Identifier ident) | |
156 { | |
157 version (LOGDOTEXP) { | |
158 printf("TypeAArray.dotExp(e = '%s', ident = '%s')\n", e.toChars(), ident.toChars()); | |
159 } | |
160 if (ident == Id.length) | |
161 { | |
162 Expression ec; | |
163 FuncDeclaration fd; | |
164 Expressions arguments; | |
165 | |
166 fd = FuncDeclaration.genCfunc(Type.tsize_t, Id.aaLen); | |
167 ec = new VarExp(Loc(0), fd); | |
168 arguments = new Expressions(); | |
169 arguments.push(cast(void*)e); | |
170 e = new CallExp(e.loc, ec, arguments); | |
171 e.type = (cast(TypeFunction)fd.type).next; | |
172 } | |
173 else if (ident == Id.keys) | |
174 { | |
175 Expression ec; | |
176 FuncDeclaration fd; | |
177 Expressions arguments; | |
178 int size = cast(int)index.size(e.loc); | |
179 | |
180 assert(size); | |
181 fd = FuncDeclaration.genCfunc(Type.tindex, Id.aaKeys); | |
182 ec = new VarExp(Loc(0), fd); | |
183 arguments = new Expressions(); | |
184 arguments.push(cast(void*)e); | |
185 arguments.push(cast(void*)new IntegerExp(Loc(0), size, Type.tsize_t)); | |
186 e = new CallExp(e.loc, ec, arguments); | |
187 e.type = index.arrayOf(); | |
188 } | |
189 else if (ident == Id.values) | |
190 { | |
191 Expression ec; | |
192 FuncDeclaration fd; | |
193 Expressions arguments; | |
194 | |
195 fd = FuncDeclaration.genCfunc(Type.tindex, Id.aaValues); | |
196 ec = new VarExp(Loc(0), fd); | |
197 arguments = new Expressions(); | |
198 arguments.push(cast(void*)e); | |
199 size_t keysize = cast(size_t)index.size(e.loc); | |
200 keysize = (keysize + PTRSIZE - 1) & ~(PTRSIZE - 1); | |
201 arguments.push(cast(void*)new IntegerExp(Loc(0), keysize, Type.tsize_t)); | |
202 arguments.push(cast(void*)new IntegerExp(Loc(0), next.size(e.loc), Type.tsize_t)); | |
203 e = new CallExp(e.loc, ec, arguments); | |
204 e.type = next.arrayOf(); | |
205 } | |
206 else if (ident == Id.rehash) | |
207 { | |
208 Expression ec; | |
209 FuncDeclaration fd; | |
210 Expressions arguments; | |
211 | |
212 fd = FuncDeclaration.genCfunc(Type.tint64, Id.aaRehash); | |
213 ec = new VarExp(Loc(0), fd); | |
214 arguments = new Expressions(); | |
215 arguments.push(cast(void*)e.addressOf(sc)); | |
216 arguments.push(cast(void*)index.getInternalTypeInfo(sc)); | |
217 e = new CallExp(e.loc, ec, arguments); | |
218 e.type = this; | |
219 } | |
220 else | |
221 { | |
222 e = Type.dotExp(sc, e, ident); | |
223 } | |
224 return e; | |
225 } | |
226 | |
227 Expression defaultInit(Loc loc) | |
228 { | |
229 version (LOGDEFAULTINIT) { | |
230 printf("TypeAArray.defaultInit() '%s'\n", toChars()); | |
231 } | |
232 Expression e = new NullExp(loc); | |
233 e.type = this; | |
234 return e; | |
235 } | |
236 | |
237 MATCH deduceType(Scope sc, Type tparam, TemplateParameters parameters, Objects dedtypes) | |
238 { | |
239 assert(false); | |
240 } | |
241 | |
242 bool isZeroInit(Loc loc) | |
243 { | |
244 assert(false); | |
245 } | |
246 | |
247 bool checkBoolean() | |
248 { | |
249 assert(false); | |
250 } | |
251 | |
252 TypeInfoDeclaration getTypeInfoDeclaration() | |
253 { | |
254 assert(false); | |
255 } | |
256 | |
257 bool hasPointers() | |
258 { | |
259 return true; | |
260 } | |
261 | |
262 MATCH implicitConvTo(Type to) | |
263 { | |
264 assert(false); | |
265 } | |
266 | |
267 MATCH constConv(Type to) | |
268 { | |
269 assert(false); | |
270 } | |
271 | |
272 version (CPP_MANGLE) { | |
273 void toCppMangle(OutBuffer buf, CppMangleState* cms) | |
274 { | |
275 assert(false); | |
276 } | |
277 } | |
278 | |
279 // Back end | |
280 /******************************************** | |
281 * Determine the right symbol to look up | |
282 * an associative array element. | |
283 * Input: | |
284 * flags 0 don't add value signature | |
285 * 1 add value signature | |
286 */ | |
287 Symbol* aaGetSymbol(const(char)* func, int flags) | |
288 in | |
289 { | |
290 assert(func); | |
291 assert((flags & ~1) == 0); | |
292 } | |
293 out (result) | |
294 { | |
295 assert(result); | |
296 } | |
297 body | |
298 { | |
299 int sz; | |
300 char* id; | |
301 type* t; | |
302 Symbol* s; | |
303 int i; | |
304 | |
305 // Dumb linear symbol table - should use associative array! | |
306 static Array sarray = null; | |
307 | |
308 //printf("aaGetSymbol(func = '%s', flags = %d, key = %p)\n", func, flags, key); | |
309 static if (false) { | |
310 scope OutBuffer buf = new OutBuffer(); | |
311 key.toKeyBuffer(buf); | |
312 | |
313 sz = next.size(); // it's just data, so we only care about the size | |
314 sz = (sz + 3) & ~3; // reduce proliferation of library routines | |
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
|
315 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
|
316 id = cast(char*)GC.malloc(3 + strlen(func) + buf.offset + sizeof(sz) * 3 + 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
|
317 else |
0 | 318 id = cast(char*)alloca(3 + strlen(func) + buf.offset + sizeof(sz) * 3 + 1); |
319 buf.writeByte(0); | |
320 if (flags & 1) | |
321 sprintf(id, "_aa%s%s%d", func, buf.data, sz); | |
322 else | |
323 sprintf(id, "_aa%s%s", func, buf.data); | |
324 } else { | |
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
|
325 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
|
326 id = cast(char*)GC.malloc(3 + strlen(func) + 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
|
327 else |
0 | 328 id = cast(char*)alloca(3 + strlen(func) + 1); |
329 sprintf(id, "_aa%s", func); | |
330 } | |
331 if (!sarray) | |
332 sarray = new Array(); | |
333 | |
334 // See if symbol is already in sarray | |
335 for (i = 0; i < sarray.dim; i++) | |
336 { | |
337 s = cast(Symbol*)sarray.data[i]; | |
338 if (strcmp(id, s.Sident.ptr) == 0) | |
339 return s; // use existing Symbol | |
340 } | |
341 | |
342 // Create new Symbol | |
343 | |
344 s = symbol_calloc(id); | |
345 slist_add(s); | |
346 s.Sclass = SCextern; | |
347 s.Ssymnum = -1; | |
348 symbol_func(s); | |
349 | |
350 t = type_alloc(TYnfunc); | |
351 t.Tflags = TFprototype | TFfixed; | |
352 t.Tmangle = mTYman_c; | |
353 t.Tparamtypes = null; | |
354 t.Tnext = next.toCtype(); | |
355 t.Tnext.Tcount++; | |
356 t.Tcount++; | |
357 s.Stype = t; | |
358 | |
359 sarray.push(s); // remember it | |
360 return s; | |
361 } | |
362 | |
363 type* toCtype() | |
364 { | |
365 assert(false); | |
366 } | |
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
|
367 } |