Mercurial > projects > ldc
annotate dmd/expression.c @ 584:c7d7e2282ba3
Make sure functions containing inline asm are never inlined to avoid
duplicated labels.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Mon, 08 Sep 2008 20:38:55 +0200 |
parents | cbd6c8073a32 |
children | fbb1a366cfbc |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2008 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <ctype.h> | |
14 #include <assert.h> | |
15 #include <complex> | |
16 #include <math.h> | |
17 | |
18 #if _WIN32 && __DMC__ | |
19 extern "C" char * __cdecl __locale_decpoint; | |
20 #endif | |
21 | |
285
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
22 #if __MINGW32__ |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
23 #ifndef isnan |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
24 #define isnan _isnan |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
25 #endif |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
26 #endif |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
27 |
536
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
28 #ifdef __APPLE__ |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
29 #ifndef isnan |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
30 int isnan(double); |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
31 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
32 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
33 |
159 | 34 #if IN_GCC |
35 // Issues with using -include total.h (defines integer_t) and then complex.h fails... | |
36 #undef integer_t | |
37 #endif | |
38 | |
39 #ifdef __APPLE__ | |
40 #define integer_t dmd_integer_t | |
41 #endif | |
42 | |
43 #if IN_GCC || IN_LLVM | |
44 #include "mem.h" | |
45 #elif _WIN32 | |
46 #include "..\root\mem.h" | |
571
cbd6c8073a32
Changed all '#if linux || __APPLE__' to '#if POSIX' so we can support other platforms too, thanx for the suggestion anders.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
536
diff
changeset
|
47 #elif POSIX |
159 | 48 #include "../root/mem.h" |
49 #endif | |
50 | |
51 //#include "port.h" | |
52 #include "mtype.h" | |
53 #include "init.h" | |
54 #include "expression.h" | |
55 #include "template.h" | |
56 #include "utf.h" | |
57 #include "enum.h" | |
58 #include "scope.h" | |
59 #include "statement.h" | |
60 #include "declaration.h" | |
61 #include "aggregate.h" | |
62 #include "import.h" | |
63 #include "id.h" | |
64 #include "dsymbol.h" | |
65 #include "module.h" | |
66 #include "attrib.h" | |
67 #include "hdrgen.h" | |
68 #include "parse.h" | |
69 | |
70 Expression *createTypeInfoArray(Scope *sc, Expression *args[], int dim); | |
71 | |
72 #define LOGSEMANTIC 0 | |
73 | |
74 /********************************** | |
75 * Set operator precedence for each operator. | |
76 */ | |
77 | |
78 // Operator precedence - greater values are higher precedence | |
79 | |
80 enum PREC | |
81 { | |
82 PREC_zero, | |
83 PREC_expr, | |
84 PREC_assign, | |
85 PREC_cond, | |
86 PREC_oror, | |
87 PREC_andand, | |
88 PREC_or, | |
89 PREC_xor, | |
90 PREC_and, | |
91 PREC_equal, | |
92 PREC_rel, | |
93 PREC_shift, | |
94 PREC_add, | |
95 PREC_mul, | |
96 PREC_unary, | |
97 PREC_primary, | |
98 }; | |
99 | |
100 enum PREC precedence[TOKMAX]; | |
101 | |
102 void initPrecedence() | |
103 { | |
104 precedence[TOKdotvar] = PREC_primary; | |
105 precedence[TOKimport] = PREC_primary; | |
106 precedence[TOKidentifier] = PREC_primary; | |
107 precedence[TOKthis] = PREC_primary; | |
108 precedence[TOKsuper] = PREC_primary; | |
109 precedence[TOKint64] = PREC_primary; | |
110 precedence[TOKfloat64] = PREC_primary; | |
111 precedence[TOKnull] = PREC_primary; | |
112 precedence[TOKstring] = PREC_primary; | |
113 precedence[TOKarrayliteral] = PREC_primary; | |
114 precedence[TOKtypedot] = PREC_primary; | |
115 precedence[TOKtypeid] = PREC_primary; | |
116 precedence[TOKis] = PREC_primary; | |
117 precedence[TOKassert] = PREC_primary; | |
118 precedence[TOKfunction] = PREC_primary; | |
119 precedence[TOKvar] = PREC_primary; | |
336 | 120 #if DMDV2 |
121 precedence[TOKdefault] = PREC_primary; | |
122 #endif | |
159 | 123 |
124 // post | |
125 precedence[TOKdotti] = PREC_primary; | |
126 precedence[TOKdot] = PREC_primary; | |
127 // precedence[TOKarrow] = PREC_primary; | |
128 precedence[TOKplusplus] = PREC_primary; | |
129 precedence[TOKminusminus] = PREC_primary; | |
130 precedence[TOKcall] = PREC_primary; | |
131 precedence[TOKslice] = PREC_primary; | |
132 precedence[TOKarray] = PREC_primary; | |
133 | |
134 precedence[TOKaddress] = PREC_unary; | |
135 precedence[TOKstar] = PREC_unary; | |
136 precedence[TOKneg] = PREC_unary; | |
137 precedence[TOKuadd] = PREC_unary; | |
138 precedence[TOKnot] = PREC_unary; | |
139 precedence[TOKtobool] = PREC_add; | |
140 precedence[TOKtilde] = PREC_unary; | |
141 precedence[TOKdelete] = PREC_unary; | |
142 precedence[TOKnew] = PREC_unary; | |
143 precedence[TOKcast] = PREC_unary; | |
144 | |
145 precedence[TOKmul] = PREC_mul; | |
146 precedence[TOKdiv] = PREC_mul; | |
147 precedence[TOKmod] = PREC_mul; | |
148 | |
149 precedence[TOKadd] = PREC_add; | |
150 precedence[TOKmin] = PREC_add; | |
151 precedence[TOKcat] = PREC_add; | |
152 | |
153 precedence[TOKshl] = PREC_shift; | |
154 precedence[TOKshr] = PREC_shift; | |
155 precedence[TOKushr] = PREC_shift; | |
156 | |
157 precedence[TOKlt] = PREC_rel; | |
158 precedence[TOKle] = PREC_rel; | |
159 precedence[TOKgt] = PREC_rel; | |
160 precedence[TOKge] = PREC_rel; | |
161 precedence[TOKunord] = PREC_rel; | |
162 precedence[TOKlg] = PREC_rel; | |
163 precedence[TOKleg] = PREC_rel; | |
164 precedence[TOKule] = PREC_rel; | |
165 precedence[TOKul] = PREC_rel; | |
166 precedence[TOKuge] = PREC_rel; | |
167 precedence[TOKug] = PREC_rel; | |
168 precedence[TOKue] = PREC_rel; | |
169 precedence[TOKin] = PREC_rel; | |
170 | |
171 precedence[TOKequal] = PREC_equal; | |
172 precedence[TOKnotequal] = PREC_equal; | |
173 precedence[TOKidentity] = PREC_equal; | |
174 precedence[TOKnotidentity] = PREC_equal; | |
175 | |
176 precedence[TOKand] = PREC_and; | |
177 | |
178 precedence[TOKxor] = PREC_xor; | |
179 | |
180 precedence[TOKor] = PREC_or; | |
181 | |
182 precedence[TOKandand] = PREC_andand; | |
183 | |
184 precedence[TOKoror] = PREC_oror; | |
185 | |
186 precedence[TOKquestion] = PREC_cond; | |
187 | |
188 precedence[TOKassign] = PREC_assign; | |
189 precedence[TOKconstruct] = PREC_assign; | |
190 precedence[TOKblit] = PREC_assign; | |
191 precedence[TOKaddass] = PREC_assign; | |
192 precedence[TOKminass] = PREC_assign; | |
193 precedence[TOKcatass] = PREC_assign; | |
194 precedence[TOKmulass] = PREC_assign; | |
195 precedence[TOKdivass] = PREC_assign; | |
196 precedence[TOKmodass] = PREC_assign; | |
197 precedence[TOKshlass] = PREC_assign; | |
198 precedence[TOKshrass] = PREC_assign; | |
199 precedence[TOKushrass] = PREC_assign; | |
200 precedence[TOKandass] = PREC_assign; | |
201 precedence[TOKorass] = PREC_assign; | |
202 precedence[TOKxorass] = PREC_assign; | |
203 | |
204 precedence[TOKcomma] = PREC_expr; | |
205 } | |
206 | |
336 | 207 /************************************************************* |
208 * Given var, we need to get the | |
209 * right 'this' pointer if var is in an outer class, but our | |
210 * existing 'this' pointer is in an inner class. | |
211 * Input: | |
212 * e1 existing 'this' | |
213 * ad struct or class we need the correct 'this' for | |
214 * var the specific member of ad we're accessing | |
215 */ | |
216 | |
217 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, | |
218 Expression *e1, Declaration *var) | |
219 { | |
220 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars()); | |
221 L1: | |
222 Type *t = e1->type->toBasetype(); | |
223 //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars()); | |
224 | |
225 /* If e1 is not the 'this' pointer for ad | |
226 */ | |
227 if (ad && | |
228 !(t->ty == Tpointer && t->nextOf()->ty == Tstruct && | |
229 ((TypeStruct *)t->nextOf())->sym == ad) | |
230 && | |
231 !(t->ty == Tstruct && | |
232 ((TypeStruct *)t)->sym == ad) | |
233 ) | |
234 { | |
235 ClassDeclaration *cd = ad->isClassDeclaration(); | |
236 ClassDeclaration *tcd = t->isClassHandle(); | |
237 | |
238 /* e1 is the right this if ad is a base class of e1 | |
239 */ | |
240 if (!cd || !tcd || | |
241 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
242 ) | |
243 { | |
244 /* Only classes can be inner classes with an 'outer' | |
245 * member pointing to the enclosing class instance | |
246 */ | |
247 if (tcd && tcd->isNested()) | |
248 { /* e1 is the 'this' pointer for an inner class: tcd. | |
249 * Rewrite it as the 'this' pointer for the outer class. | |
250 */ | |
251 | |
252 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
253 e1->type = tcd->vthis->type; | |
254 // Do not call checkNestedRef() | |
255 //e1 = e1->semantic(sc); | |
256 | |
257 // Skip up over nested functions, and get the enclosing | |
258 // class type. | |
259 int n = 0; | |
260 Dsymbol *s; | |
261 for (s = tcd->toParent(); | |
262 s && s->isFuncDeclaration(); | |
263 s = s->toParent()) | |
264 { FuncDeclaration *f = s->isFuncDeclaration(); | |
265 if (f->vthis) | |
266 { | |
267 //printf("rewriting e1 to %s's this\n", f->toChars()); | |
268 n++; | |
486
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
269 |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
270 // LLVMDC seems dmd misses it sometimes here :/ |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
271 f->vthis->nestedref = 1; |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
272 |
336 | 273 e1 = new VarExp(loc, f->vthis); |
274 } | |
275 } | |
276 if (s && s->isClassDeclaration()) | |
277 { e1->type = s->isClassDeclaration()->type; | |
278 if (n > 1) | |
279 e1 = e1->semantic(sc); | |
280 } | |
281 else | |
282 e1 = e1->semantic(sc); | |
283 goto L1; | |
284 } | |
285 /* Can't find a path from e1 to ad | |
286 */ | |
287 e1->error("this for %s needs to be type %s not type %s", | |
288 var->toChars(), ad->toChars(), t->toChars()); | |
289 } | |
290 } | |
291 return e1; | |
292 } | |
293 | |
159 | 294 /***************************************** |
295 * Determine if 'this' is available. | |
296 * If it is, return the FuncDeclaration that has it. | |
297 */ | |
298 | |
299 FuncDeclaration *hasThis(Scope *sc) | |
300 { FuncDeclaration *fd; | |
301 FuncDeclaration *fdthis; | |
302 | |
303 //printf("hasThis()\n"); | |
304 fdthis = sc->parent->isFuncDeclaration(); | |
305 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
306 | |
307 // Go upwards until we find the enclosing member function | |
308 fd = fdthis; | |
309 while (1) | |
310 { | |
311 if (!fd) | |
312 { | |
313 goto Lno; | |
314 } | |
315 if (!fd->isNested()) | |
316 break; | |
317 | |
318 Dsymbol *parent = fd->parent; | |
319 while (parent) | |
320 { | |
321 TemplateInstance *ti = parent->isTemplateInstance(); | |
322 if (ti) | |
323 parent = ti->parent; | |
324 else | |
325 break; | |
326 } | |
327 | |
328 fd = fd->parent->isFuncDeclaration(); | |
329 } | |
330 | |
331 if (!fd->isThis()) | |
332 { //printf("test '%s'\n", fd->toChars()); | |
333 goto Lno; | |
334 } | |
335 | |
336 assert(fd->vthis); | |
337 return fd; | |
338 | |
339 Lno: | |
340 return NULL; // don't have 'this' available | |
341 } | |
342 | |
343 | |
344 /*************************************** | |
345 * Pull out any properties. | |
346 */ | |
347 | |
348 Expression *resolveProperties(Scope *sc, Expression *e) | |
349 { | |
350 //printf("resolveProperties(%s)\n", e->toChars()); | |
351 if (e->type) | |
352 { | |
353 Type *t = e->type->toBasetype(); | |
354 | |
355 if (t->ty == Tfunction) | |
356 { | |
357 e = new CallExp(e->loc, e); | |
358 e = e->semantic(sc); | |
359 } | |
360 | |
361 /* Look for e being a lazy parameter; rewrite as delegate call | |
362 */ | |
363 else if (e->op == TOKvar) | |
364 { VarExp *ve = (VarExp *)e; | |
365 | |
366 if (ve->var->storage_class & STClazy) | |
367 { | |
368 e = new CallExp(e->loc, e); | |
369 e = e->semantic(sc); | |
370 } | |
371 } | |
372 | |
373 else if (e->op == TOKdotexp) | |
374 { | |
375 e->error("expression has no value"); | |
376 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
377 |
159 | 378 } |
379 return e; | |
380 } | |
381 | |
382 /****************************** | |
383 * Perform semantic() on an array of Expressions. | |
384 */ | |
385 | |
386 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
387 { | |
388 if (exps) | |
389 { | |
390 for (size_t i = 0; i < exps->dim; i++) | |
391 { Expression *e = (Expression *)exps->data[i]; | |
392 | |
393 e = e->semantic(sc); | |
394 exps->data[i] = (void *)e; | |
395 } | |
396 } | |
397 } | |
398 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
399 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
400 /****************************** |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
401 * Perform canThrow() on an array of Expressions. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
402 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
403 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
404 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
405 int arrayExpressionCanThrow(Expressions *exps) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
406 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
407 if (exps) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
408 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
409 for (size_t i = 0; i < exps->dim; i++) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
410 { Expression *e = (Expression *)exps->data[i]; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
411 if (e && e->canThrow()) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
412 return 1; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
413 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
414 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
415 return 0; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
416 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
417 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
418 |
159 | 419 /**************************************** |
420 * Expand tuples. | |
421 */ | |
422 | |
423 void expandTuples(Expressions *exps) | |
424 { | |
425 //printf("expandTuples()\n"); | |
426 if (exps) | |
427 { | |
428 for (size_t i = 0; i < exps->dim; i++) | |
429 { Expression *arg = (Expression *)exps->data[i]; | |
430 if (!arg) | |
431 continue; | |
432 | |
433 // Look for tuple with 0 members | |
434 if (arg->op == TOKtype) | |
435 { TypeExp *e = (TypeExp *)arg; | |
436 if (e->type->toBasetype()->ty == Ttuple) | |
437 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
438 | |
439 if (!tt->arguments || tt->arguments->dim == 0) | |
440 { | |
441 exps->remove(i); | |
442 if (i == exps->dim) | |
443 return; | |
444 i--; | |
445 continue; | |
446 } | |
447 } | |
448 } | |
449 | |
450 // Inline expand all the tuples | |
451 while (arg->op == TOKtuple) | |
452 { TupleExp *te = (TupleExp *)arg; | |
453 | |
454 exps->remove(i); // remove arg | |
455 exps->insert(i, te->exps); // replace with tuple contents | |
456 if (i == exps->dim) | |
457 return; // empty tuple, no more arguments | |
458 arg = (Expression *)exps->data[i]; | |
459 } | |
460 } | |
461 } | |
462 } | |
463 | |
464 /**************************************** | |
465 * Preprocess arguments to function. | |
466 */ | |
467 | |
468 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps) | |
469 { | |
470 if (exps) | |
471 { | |
472 expandTuples(exps); | |
473 | |
474 for (size_t i = 0; i < exps->dim; i++) | |
475 { Expression *arg = (Expression *)exps->data[i]; | |
476 | |
477 if (!arg->type) | |
478 { | |
479 #ifdef DEBUG | |
480 if (!global.gag) | |
481 printf("1: \n"); | |
482 #endif | |
483 arg->error("%s is not an expression", arg->toChars()); | |
484 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
485 } | |
486 | |
487 arg = resolveProperties(sc, arg); | |
488 exps->data[i] = (void *) arg; | |
489 | |
490 //arg->rvalue(); | |
491 #if 0 | |
492 if (arg->type->ty == Tfunction) | |
493 { | |
494 arg = new AddrExp(arg->loc, arg); | |
495 arg = arg->semantic(sc); | |
496 exps->data[i] = (void *) arg; | |
497 } | |
498 #endif | |
499 } | |
500 } | |
501 } | |
502 | |
336 | 503 /********************************************* |
504 * Call copy constructor for struct value argument. | |
505 */ | |
506 #if DMDV2 | |
507 Expression *callCpCtor(Loc loc, Scope *sc, Expression *e) | |
508 { | |
509 Type *tb = e->type->toBasetype(); | |
510 assert(tb->ty == Tstruct); | |
511 StructDeclaration *sd = ((TypeStruct *)tb)->sym; | |
512 if (sd->cpctor) | |
513 { | |
514 /* Create a variable tmp, and replace the argument e with: | |
515 * (tmp = e),tmp | |
516 * and let AssignExp() handle the construction. | |
517 * This is not the most efficent, ideally tmp would be constructed | |
518 * directly onto the stack. | |
519 */ | |
520 Identifier *idtmp = Lexer::uniqueId("__tmp"); | |
521 VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e)); | |
522 Expression *ae = new DeclarationExp(loc, tmp); | |
523 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
524 e = e->semantic(sc); | |
525 } | |
526 return e; | |
527 } | |
528 #endif | |
159 | 529 |
530 /**************************************** | |
531 * Now that we know the exact type of the function we're calling, | |
532 * the arguments[] need to be adjusted: | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
533 * 1. implicitly convert argument to the corresponding parameter type |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
534 * 2. add default arguments for any missing arguments |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
535 * 3. do default promotions on arguments corresponding to ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
536 * 4. add hidden _arguments[] argument |
159 | 537 */ |
538 | |
539 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) | |
540 { | |
541 unsigned n; | |
542 int done; | |
543 Type *tb; | |
544 | |
545 //printf("functionArguments()\n"); | |
546 assert(arguments); | |
547 size_t nargs = arguments ? arguments->dim : 0; | |
548 size_t nparams = Argument::dim(tf->parameters); | |
549 | |
550 if (nargs > nparams && tf->varargs == 0) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
551 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs); |
159 | 552 |
553 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
554 | |
555 done = 0; | |
556 for (size_t i = 0; i < n; i++) | |
557 { | |
558 Expression *arg; | |
559 | |
560 if (i < nargs) | |
561 arg = (Expression *)arguments->data[i]; | |
562 else | |
563 arg = NULL; | |
564 | |
565 if (i < nparams) | |
566 { | |
567 Argument *p = Argument::getNth(tf->parameters, i); | |
568 | |
569 if (!arg) | |
570 { | |
571 if (!p->defaultArg) | |
572 { | |
573 if (tf->varargs == 2 && i + 1 == nparams) | |
574 goto L2; | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
575 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs); |
159 | 576 break; |
577 } | |
336 | 578 arg = p->defaultArg; |
579 #if DMDV2 | |
580 if (arg->op == TOKdefault) | |
581 { DefaultInitExp *de = (DefaultInitExp *)arg; | |
582 arg = de->resolve(loc, sc); | |
583 } | |
584 else | |
585 #endif | |
586 arg = arg->copy(); | |
159 | 587 arguments->push(arg); |
588 nargs++; | |
589 } | |
590 | |
591 if (tf->varargs == 2 && i + 1 == nparams) | |
592 { | |
593 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
594 if (arg->implicitConvTo(p->type)) | |
595 { | |
596 if (nargs != nparams) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
597 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs); |
159 | 598 goto L1; |
599 } | |
600 L2: | |
601 Type *tb = p->type->toBasetype(); | |
602 Type *tret = p->isLazyArray(); | |
603 switch (tb->ty) | |
604 { | |
605 case Tsarray: | |
606 case Tarray: | |
607 { // Create a static array variable v of type arg->type | |
608 #ifdef IN_GCC | |
609 /* GCC 4.0 does not like zero length arrays used like | |
610 this; pass a null array value instead. Could also | |
611 just make a one-element array. */ | |
612 if (nargs - i == 0) | |
613 { | |
614 arg = new NullExp(loc); | |
615 break; | |
616 } | |
617 #endif | |
618 static int idn; | |
619 char name[10 + sizeof(idn)*3 + 1]; | |
620 sprintf(name, "__arrayArg%d", ++idn); | |
621 Identifier *id = Lexer::idPool(name); | |
622 Type *t = new TypeSArray(tb->next, new IntegerExp(nargs - i)); | |
623 t = t->semantic(loc, sc); | |
624 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
625 v->semantic(sc); | |
626 v->parent = sc->parent; | |
627 //sc->insert(v); | |
628 | |
629 Expression *c = new DeclarationExp(0, v); | |
630 c->type = v->type; | |
631 | |
632 for (size_t u = i; u < nargs; u++) | |
633 { Expression *a = (Expression *)arguments->data[u]; | |
634 if (tret && !tb->next->equals(a->type)) | |
635 a = a->toDelegate(sc, tret); | |
636 | |
637 Expression *e = new VarExp(loc, v); | |
638 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
639 e = new AssignExp(loc, e, a); | |
640 if (c) | |
641 c = new CommaExp(loc, c, e); | |
642 else | |
643 c = e; | |
644 } | |
645 arg = new VarExp(loc, v); | |
646 if (c) | |
647 arg = new CommaExp(loc, c, arg); | |
648 break; | |
649 } | |
650 case Tclass: | |
651 { /* Set arg to be: | |
652 * new Tclass(arg0, arg1, ..., argn) | |
653 */ | |
654 Expressions *args = new Expressions(); | |
655 args->setDim(nargs - i); | |
656 for (size_t u = i; u < nargs; u++) | |
657 args->data[u - i] = arguments->data[u]; | |
658 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
659 break; | |
660 } | |
661 default: | |
662 if (!arg) | |
663 { error(loc, "not enough arguments"); | |
664 return; | |
665 } | |
666 break; | |
667 } | |
668 arg = arg->semantic(sc); | |
669 //printf("\targ = '%s'\n", arg->toChars()); | |
670 arguments->setDim(i + 1); | |
671 done = 1; | |
672 } | |
673 | |
674 L1: | |
675 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
676 arg = arg->implicitCastTo(sc, p->type); | |
677 if (p->storageClass & (STCout | STCref)) | |
678 { | |
679 // BUG: should check that argument to ref is type 'invariant' | |
680 // BUG: assignments to ref should also be type 'invariant' | |
681 arg = arg->modifiableLvalue(sc, arg); | |
682 | |
683 //if (arg->op == TOKslice) | |
684 //arg->error("cannot modify slice %s", arg->toChars()); | |
685 } | |
686 | |
687 // Convert static arrays to pointers | |
688 tb = arg->type->toBasetype(); | |
689 if (tb->ty == Tsarray) | |
690 { | |
691 arg = arg->checkToPointer(); | |
692 } | |
693 | |
694 // Convert lazy argument to a delegate | |
695 if (p->storageClass & STClazy) | |
696 { | |
697 arg = arg->toDelegate(sc, p->type); | |
698 } | |
699 } | |
700 else | |
701 { | |
702 | |
703 // If not D linkage, do promotions | |
704 if (tf->linkage != LINKd) | |
705 { | |
706 // Promote bytes, words, etc., to ints | |
707 arg = arg->integralPromotions(sc); | |
708 | |
709 // Promote floats to doubles | |
710 switch (arg->type->ty) | |
711 { | |
712 case Tfloat32: | |
713 arg = arg->castTo(sc, Type::tfloat64); | |
714 break; | |
715 | |
716 case Timaginary32: | |
717 arg = arg->castTo(sc, Type::timaginary64); | |
718 break; | |
719 } | |
720 } | |
721 | |
722 // Convert static arrays to dynamic arrays | |
723 tb = arg->type->toBasetype(); | |
724 if (tb->ty == Tsarray) | |
725 { TypeSArray *ts = (TypeSArray *)tb; | |
336 | 726 Type *ta = ts->next->arrayOf(); |
159 | 727 if (ts->size(arg->loc) == 0) |
728 { arg = new NullExp(arg->loc); | |
729 arg->type = ta; | |
730 } | |
731 else | |
732 arg = arg->castTo(sc, ta); | |
733 } | |
336 | 734 #if DMDV2 |
735 if (tb->ty == Tstruct) | |
736 { | |
737 arg = callCpCtor(loc, sc, arg); | |
738 } | |
739 | |
740 // Give error for overloaded function addresses | |
741 if (arg->op == TOKsymoff) | |
742 { SymOffExp *se = (SymOffExp *)arg; | |
743 if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique()) | |
744 arg->error("function %s is overloaded", arg->toChars()); | |
745 } | |
746 #endif | |
159 | 747 arg->rvalue(); |
748 } | |
749 arg = arg->optimize(WANTvalue); | |
750 arguments->data[i] = (void *) arg; | |
751 if (done) | |
752 break; | |
753 } | |
754 | |
755 #if !IN_LLVM | |
756 // If D linkage and variadic, add _arguments[] as first argument | |
757 if (tf->linkage == LINKd && tf->varargs == 1) | |
758 { | |
759 Expression *e; | |
760 | |
761 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], | |
762 arguments->dim - nparams); | |
763 arguments->insert(0, e); | |
764 } | |
765 #endif | |
766 } | |
767 | |
768 /************************************************** | |
769 * Write expression out to buf, but wrap it | |
770 * in ( ) if its precedence is less than pr. | |
771 */ | |
772 | |
773 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
774 { | |
336 | 775 //if (precedence[e->op] == 0) e->dump(0); |
159 | 776 if (precedence[e->op] < pr) |
777 { | |
778 buf->writeByte('('); | |
779 e->toCBuffer(buf, hgs); | |
780 buf->writeByte(')'); | |
781 } | |
782 else | |
783 e->toCBuffer(buf, hgs); | |
784 } | |
785 | |
786 /************************************************** | |
787 * Write out argument list to buf. | |
788 */ | |
789 | |
790 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
791 { | |
792 if (arguments) | |
793 { | |
794 for (size_t i = 0; i < arguments->dim; i++) | |
795 { Expression *arg = (Expression *)arguments->data[i]; | |
796 | |
797 if (arg) | |
798 { if (i) | |
799 buf->writeByte(','); | |
800 expToCBuffer(buf, hgs, arg, PREC_assign); | |
801 } | |
802 } | |
803 } | |
804 } | |
805 | |
806 /************************************************** | |
807 * Write out argument types to buf. | |
808 */ | |
809 | |
810 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
811 { | |
812 if (arguments) | |
813 { OutBuffer argbuf; | |
814 | |
815 for (size_t i = 0; i < arguments->dim; i++) | |
816 { Expression *arg = (Expression *)arguments->data[i]; | |
817 | |
818 if (i) | |
819 buf->writeByte(','); | |
820 argbuf.reset(); | |
821 arg->type->toCBuffer2(&argbuf, hgs, 0); | |
822 buf->write(&argbuf); | |
823 } | |
824 } | |
825 } | |
826 | |
827 /******************************** Expression **************************/ | |
828 | |
829 Expression::Expression(Loc loc, enum TOK op, int size) | |
830 : loc(loc) | |
831 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
832 //printf("Expression::Expression(op = %d) this = %p\n", op, this); |
159 | 833 this->loc = loc; |
834 this->op = op; | |
835 this->size = size; | |
836 type = NULL; | |
837 } | |
838 | |
839 Expression *Expression::syntaxCopy() | |
840 { | |
841 //printf("Expression::syntaxCopy()\n"); | |
842 //dump(0); | |
843 return copy(); | |
844 } | |
845 | |
846 /********************************* | |
847 * Does *not* do a deep copy. | |
848 */ | |
849 | |
850 Expression *Expression::copy() | |
851 { | |
852 Expression *e; | |
853 if (!size) | |
854 { | |
855 #ifdef DEBUG | |
856 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
857 printf("op = %d\n", op); | |
858 dump(0); | |
859 #endif | |
860 assert(0); | |
861 } | |
862 e = (Expression *)mem.malloc(size); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
863 //printf("Expression::copy(op = %d) e = %p\n", op, e); |
159 | 864 return (Expression *)memcpy(e, this, size); |
865 } | |
866 | |
867 /************************** | |
868 * Semantically analyze Expression. | |
869 * Determine types, fold constants, etc. | |
870 */ | |
871 | |
872 Expression *Expression::semantic(Scope *sc) | |
873 { | |
874 #if LOGSEMANTIC | |
336 | 875 printf("Expression::semantic() %s\n", toChars()); |
159 | 876 #endif |
877 if (type) | |
878 type = type->semantic(loc, sc); | |
879 else | |
880 type = Type::tvoid; | |
881 return this; | |
882 } | |
883 | |
884 void Expression::print() | |
885 { | |
886 fprintf(stdmsg, "%s\n", toChars()); | |
887 fflush(stdmsg); | |
888 } | |
889 | |
890 char *Expression::toChars() | |
891 { OutBuffer *buf; | |
892 HdrGenState hgs; | |
893 | |
894 memset(&hgs, 0, sizeof(hgs)); | |
895 buf = new OutBuffer(); | |
896 toCBuffer(buf, &hgs); | |
897 return buf->toChars(); | |
898 } | |
899 | |
900 void Expression::error(const char *format, ...) | |
901 { | |
902 va_list ap; | |
903 va_start(ap, format); | |
904 ::verror(loc, format, ap); | |
905 va_end( ap ); | |
906 } | |
907 | |
908 void Expression::rvalue() | |
909 { | |
910 if (type && type->toBasetype()->ty == Tvoid) | |
911 { error("expression %s is void and has no value", toChars()); | |
912 #if 0 | |
913 dump(0); | |
914 halt(); | |
915 #endif | |
336 | 916 type = Type::tint32; |
159 | 917 } |
918 } | |
919 | |
920 Expression *Expression::combine(Expression *e1, Expression *e2) | |
921 { | |
922 if (e1) | |
923 { | |
924 if (e2) | |
925 { | |
926 e1 = new CommaExp(e1->loc, e1, e2); | |
927 e1->type = e2->type; | |
928 } | |
929 } | |
930 else | |
931 e1 = e2; | |
932 return e1; | |
933 } | |
934 | |
935 integer_t Expression::toInteger() | |
936 { | |
937 //printf("Expression %s\n", Token::toChars(op)); | |
938 error("Integer constant expression expected instead of %s", toChars()); | |
939 return 0; | |
940 } | |
941 | |
942 uinteger_t Expression::toUInteger() | |
943 { | |
944 //printf("Expression %s\n", Token::toChars(op)); | |
945 return (uinteger_t)toInteger(); | |
946 } | |
947 | |
948 real_t Expression::toReal() | |
949 { | |
950 error("Floating point constant expression expected instead of %s", toChars()); | |
951 return 0; | |
952 } | |
953 | |
954 real_t Expression::toImaginary() | |
955 { | |
956 error("Floating point constant expression expected instead of %s", toChars()); | |
957 return 0; | |
958 } | |
959 | |
960 complex_t Expression::toComplex() | |
961 { | |
962 error("Floating point constant expression expected instead of %s", toChars()); | |
963 #ifdef IN_GCC | |
964 return complex_t(real_t(0)); // %% nicer | |
965 #else | |
966 return 0; | |
967 #endif | |
968 } | |
969 | |
970 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
971 { | |
972 buf->writestring(Token::toChars(op)); | |
973 } | |
974 | |
975 void Expression::toMangleBuffer(OutBuffer *buf) | |
976 { | |
977 error("expression %s is not a valid template value argument", toChars()); | |
978 } | |
979 | |
980 /******************************* | |
981 * Give error if we're not an lvalue. | |
982 * If we can, convert expression to be an lvalue. | |
983 */ | |
984 | |
985 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
986 { | |
987 if (!e) | |
988 e = this; | |
989 else if (!loc.filename) | |
990 loc = e->loc; | |
991 error("%s is not an lvalue", e->toChars()); | |
992 return this; | |
993 } | |
994 | |
995 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
996 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
997 //printf("Expression::modifiableLvalue() %s, type = %s\n", toChars(), type->toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
998 |
159 | 999 // See if this expression is a modifiable lvalue (i.e. not const) |
1000 return toLvalue(sc, e); | |
1001 } | |
1002 | |
1003 /************************************ | |
1004 * Detect cases where pointers to the stack can 'escape' the | |
1005 * lifetime of the stack frame. | |
1006 */ | |
1007 | |
1008 void Expression::checkEscape() | |
1009 { | |
1010 } | |
1011 | |
1012 void Expression::checkScalar() | |
1013 { | |
1014 if (!type->isscalar()) | |
1015 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
1016 } | |
1017 | |
1018 void Expression::checkNoBool() | |
1019 { | |
1020 if (type->toBasetype()->ty == Tbool) | |
1021 error("operation not allowed on bool '%s'", toChars()); | |
1022 } | |
1023 | |
1024 Expression *Expression::checkIntegral() | |
1025 { | |
1026 if (!type->isintegral()) | |
1027 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
1028 return new IntegerExp(0); | |
1029 } | |
1030 return this; | |
1031 } | |
1032 | |
1033 Expression *Expression::checkArithmetic() | |
1034 { | |
1035 if (!type->isintegral() && !type->isfloating()) | |
1036 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); | |
1037 return new IntegerExp(0); | |
1038 } | |
1039 return this; | |
1040 } | |
1041 | |
1042 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
1043 { | |
1044 s->checkDeprecated(loc, sc); | |
1045 } | |
1046 | |
1047 /******************************** | |
1048 * Check for expressions that have no use. | |
1049 * Input: | |
1050 * flag 0 not going to use the result, so issue error message if no | |
1051 * side effects | |
1052 * 1 the result of the expression is used, but still check | |
1053 * for useless subexpressions | |
1054 * 2 do not issue error messages, just return !=0 if expression | |
1055 * has side effects | |
1056 */ | |
1057 | |
1058 int Expression::checkSideEffect(int flag) | |
1059 { | |
1060 if (flag == 0) | |
1061 { if (op == TOKimport) | |
1062 { | |
1063 error("%s has no effect", toChars()); | |
1064 } | |
1065 else | |
1066 error("%s has no effect in expression (%s)", | |
1067 Token::toChars(op), toChars()); | |
1068 } | |
1069 return 0; | |
1070 } | |
1071 | |
1072 /***************************** | |
1073 * Check that expression can be tested for true or false. | |
1074 */ | |
1075 | |
1076 Expression *Expression::checkToBoolean() | |
1077 { | |
1078 // Default is 'yes' - do nothing | |
1079 | |
1080 #ifdef DEBUG | |
1081 if (!type) | |
1082 dump(0); | |
1083 #endif | |
1084 | |
1085 if (!type->checkBoolean()) | |
1086 { | |
1087 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
1088 } | |
1089 return this; | |
1090 } | |
1091 | |
1092 /**************************** | |
1093 */ | |
1094 | |
1095 Expression *Expression::checkToPointer() | |
1096 { | |
1097 Expression *e; | |
1098 Type *tb; | |
1099 | |
1100 //printf("Expression::checkToPointer()\n"); | |
1101 e = this; | |
1102 | |
1103 // If C static array, convert to pointer | |
1104 tb = type->toBasetype(); | |
1105 if (tb->ty == Tsarray) | |
1106 { TypeSArray *ts = (TypeSArray *)tb; | |
1107 if (ts->size(loc) == 0) | |
1108 e = new NullExp(loc); | |
1109 else | |
1110 e = new AddrExp(loc, this); | |
336 | 1111 e->type = ts->next->pointerTo(); |
159 | 1112 } |
1113 return e; | |
1114 } | |
1115 | |
1116 /****************************** | |
1117 * Take address of expression. | |
1118 */ | |
1119 | |
1120 Expression *Expression::addressOf(Scope *sc) | |
1121 { | |
1122 Expression *e; | |
1123 | |
1124 //printf("Expression::addressOf()\n"); | |
1125 e = toLvalue(sc, NULL); | |
1126 e = new AddrExp(loc, e); | |
1127 e->type = type->pointerTo(); | |
1128 return e; | |
1129 } | |
1130 | |
1131 /****************************** | |
1132 * If this is a reference, dereference it. | |
1133 */ | |
1134 | |
1135 Expression *Expression::deref() | |
1136 { | |
1137 //printf("Expression::deref()\n"); | |
1138 if (type->ty == Treference) | |
1139 { Expression *e; | |
1140 | |
1141 e = new PtrExp(loc, this); | |
336 | 1142 e->type = ((TypeReference *)type)->next; |
159 | 1143 return e; |
1144 } | |
1145 return this; | |
1146 } | |
1147 | |
1148 /******************************** | |
1149 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
1150 */ | |
1151 | |
1152 int Expression::isBool(int result) | |
1153 { | |
1154 return FALSE; | |
1155 } | |
1156 | |
1157 /******************************** | |
1158 * Does this expression result in either a 1 or a 0? | |
1159 */ | |
1160 | |
1161 int Expression::isBit() | |
1162 { | |
1163 return FALSE; | |
1164 } | |
1165 | |
336 | 1166 /******************************** |
1167 * Can this expression throw an exception? | |
1168 * Valid only after semantic() pass. | |
1169 */ | |
1170 | |
1171 int Expression::canThrow() | |
1172 { | |
1173 return TRUE; | |
1174 } | |
1175 | |
1176 | |
1177 | |
159 | 1178 Expressions *Expression::arraySyntaxCopy(Expressions *exps) |
1179 { Expressions *a = NULL; | |
1180 | |
1181 if (exps) | |
1182 { | |
1183 a = new Expressions(); | |
1184 a->setDim(exps->dim); | |
1185 for (int i = 0; i < a->dim; i++) | |
1186 { Expression *e = (Expression *)exps->data[i]; | |
1187 | |
1188 e = e->syntaxCopy(); | |
1189 a->data[i] = e; | |
1190 } | |
1191 } | |
1192 return a; | |
1193 } | |
1194 | |
1195 /******************************** IntegerExp **************************/ | |
1196 | |
1197 IntegerExp::IntegerExp(Loc loc, integer_t value, Type *type) | |
1198 : Expression(loc, TOKint64, sizeof(IntegerExp)) | |
1199 { | |
1200 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1201 if (type && !type->isscalar()) | |
1202 { | |
336 | 1203 //printf("%s, loc = %d\n", toChars(), loc.linnum); |
159 | 1204 error("integral constant must be scalar type, not %s", type->toChars()); |
1205 type = Type::terror; | |
1206 } | |
1207 this->type = type; | |
1208 this->value = value; | |
1209 } | |
1210 | |
1211 IntegerExp::IntegerExp(integer_t value) | |
1212 : Expression(0, TOKint64, sizeof(IntegerExp)) | |
1213 { | |
1214 this->type = Type::tint32; | |
1215 this->value = value; | |
1216 } | |
1217 | |
1218 int IntegerExp::equals(Object *o) | |
1219 { IntegerExp *ne; | |
1220 | |
1221 if (this == o || | |
1222 (((Expression *)o)->op == TOKint64 && | |
1223 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1224 value == ne->value)) | |
1225 return 1; | |
1226 return 0; | |
1227 } | |
1228 | |
1229 char *IntegerExp::toChars() | |
1230 { | |
1231 #if 1 | |
1232 return Expression::toChars(); | |
1233 #else | |
1234 static char buffer[sizeof(value) * 3 + 1]; | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1235 sprintf(buffer, "%lld", value); |
159 | 1236 return buffer; |
1237 #endif | |
1238 } | |
1239 | |
1240 integer_t IntegerExp::toInteger() | |
1241 { Type *t; | |
1242 | |
1243 t = type; | |
1244 while (t) | |
1245 { | |
1246 switch (t->ty) | |
1247 { | |
1248 case Tbit: | |
1249 case Tbool: value = (value != 0); break; | |
1250 case Tint8: value = (d_int8) value; break; | |
1251 case Tchar: | |
1252 case Tuns8: value = (d_uns8) value; break; | |
1253 case Tint16: value = (d_int16) value; break; | |
1254 case Twchar: | |
1255 case Tuns16: value = (d_uns16) value; break; | |
1256 case Tint32: value = (d_int32) value; break; | |
1257 case Tpointer: | |
1258 case Tdchar: | |
1259 case Tuns32: value = (d_uns32) value; break; | |
1260 case Tint64: value = (d_int64) value; break; | |
1261 case Tuns64: value = (d_uns64) value; break; | |
1262 | |
1263 case Tenum: | |
1264 { | |
1265 TypeEnum *te = (TypeEnum *)t; | |
1266 t = te->sym->memtype; | |
1267 continue; | |
1268 } | |
1269 | |
1270 case Ttypedef: | |
1271 { | |
1272 TypeTypedef *tt = (TypeTypedef *)t; | |
1273 t = tt->sym->basetype; | |
1274 continue; | |
1275 } | |
1276 | |
1277 default: | |
336 | 1278 /* This can happen if errors, such as |
1279 * the type is painted on like in fromConstInitializer(). | |
1280 */ | |
1281 if (!global.errors) | |
1282 { type->print(); | |
1283 assert(0); | |
1284 } | |
159 | 1285 break; |
1286 } | |
1287 break; | |
1288 } | |
1289 return value; | |
1290 } | |
1291 | |
1292 real_t IntegerExp::toReal() | |
1293 { | |
1294 Type *t; | |
1295 | |
1296 toInteger(); | |
1297 t = type->toBasetype(); | |
1298 if (t->ty == Tuns64) | |
1299 return (real_t)(d_uns64)value; | |
1300 else | |
1301 return (real_t)(d_int64)value; | |
1302 } | |
1303 | |
1304 real_t IntegerExp::toImaginary() | |
1305 { | |
1306 return (real_t) 0; | |
1307 } | |
1308 | |
1309 complex_t IntegerExp::toComplex() | |
1310 { | |
1311 return toReal(); | |
1312 } | |
1313 | |
1314 int IntegerExp::isBool(int result) | |
1315 { | |
1316 return result ? value != 0 : value == 0; | |
1317 } | |
1318 | |
1319 Expression *IntegerExp::semantic(Scope *sc) | |
1320 { | |
1321 if (!type) | |
1322 { | |
1323 // Determine what the type of this number is | |
1324 integer_t number = value; | |
1325 | |
1326 if (number & 0x8000000000000000LL) | |
1327 type = Type::tuns64; | |
1328 else if (number & 0xFFFFFFFF80000000LL) | |
1329 type = Type::tint64; | |
1330 else | |
1331 type = Type::tint32; | |
1332 } | |
1333 else | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1334 { if (!type->deco) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1335 type = type->semantic(loc, sc); |
159 | 1336 } |
1337 return this; | |
1338 } | |
1339 | |
1340 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1341 { | |
1342 if (!e) | |
1343 e = this; | |
1344 else if (!loc.filename) | |
1345 loc = e->loc; | |
1346 e->error("constant %s is not an lvalue", e->toChars()); | |
1347 return this; | |
1348 } | |
1349 | |
1350 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1351 { | |
1352 integer_t v = toInteger(); | |
1353 | |
1354 if (type) | |
1355 { Type *t = type; | |
1356 | |
1357 L1: | |
1358 switch (t->ty) | |
1359 { | |
1360 case Tenum: | |
1361 { TypeEnum *te = (TypeEnum *)t; | |
1362 buf->printf("cast(%s)", te->sym->toChars()); | |
1363 t = te->sym->memtype; | |
1364 goto L1; | |
1365 } | |
1366 | |
1367 case Ttypedef: | |
1368 { TypeTypedef *tt = (TypeTypedef *)t; | |
1369 buf->printf("cast(%s)", tt->sym->toChars()); | |
1370 t = tt->sym->basetype; | |
1371 goto L1; | |
1372 } | |
1373 | |
1374 case Twchar: // BUG: need to cast(wchar) | |
1375 case Tdchar: // BUG: need to cast(dchar) | |
1376 if ((uinteger_t)v > 0xFF) | |
1377 { | |
1378 buf->printf("'\\U%08x'", v); | |
1379 break; | |
1380 } | |
1381 case Tchar: | |
1382 if (v == '\'') | |
1383 buf->writestring("'\\''"); | |
1384 else if (isprint(v) && v != '\\') | |
1385 buf->printf("'%c'", (int)v); | |
1386 else | |
1387 buf->printf("'\\x%02x'", (int)v); | |
1388 break; | |
1389 | |
1390 case Tint8: | |
1391 buf->writestring("cast(byte)"); | |
1392 goto L2; | |
1393 | |
1394 case Tint16: | |
1395 buf->writestring("cast(short)"); | |
1396 goto L2; | |
1397 | |
1398 case Tint32: | |
1399 L2: | |
1400 buf->printf("%d", (int)v); | |
1401 break; | |
1402 | |
1403 case Tuns8: | |
1404 buf->writestring("cast(ubyte)"); | |
1405 goto L3; | |
1406 | |
1407 case Tuns16: | |
1408 buf->writestring("cast(ushort)"); | |
1409 goto L3; | |
1410 | |
1411 case Tuns32: | |
1412 L3: | |
1413 buf->printf("%du", (unsigned)v); | |
1414 break; | |
1415 | |
1416 case Tint64: | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1417 buf->printf("%lldL", v); |
159 | 1418 break; |
1419 | |
1420 case Tuns64: | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1421 buf->printf("%lluLU", v); |
159 | 1422 break; |
1423 | |
1424 case Tbit: | |
1425 case Tbool: | |
1426 buf->writestring((char *)(v ? "true" : "false")); | |
1427 break; | |
1428 | |
1429 case Tpointer: | |
1430 buf->writestring("cast("); | |
1431 buf->writestring(t->toChars()); | |
1432 buf->writeByte(')'); | |
1433 goto L3; | |
1434 | |
1435 default: | |
336 | 1436 /* This can happen if errors, such as |
1437 * the type is painted on like in fromConstInitializer(). | |
1438 */ | |
1439 if (!global.errors) | |
1440 { | |
159 | 1441 #ifdef DEBUG |
336 | 1442 t->print(); |
159 | 1443 #endif |
336 | 1444 assert(0); |
1445 } | |
1446 break; | |
159 | 1447 } |
1448 } | |
1449 else if (v & 0x8000000000000000LL) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1450 buf->printf("0x%llx", v); |
159 | 1451 else |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1452 buf->printf("%lld", v); |
159 | 1453 } |
1454 | |
1455 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1456 { | |
1457 if ((sinteger_t)value < 0) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1458 buf->printf("N%lld", -value); |
159 | 1459 else |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1460 buf->printf("%lld", value); |
159 | 1461 } |
1462 | |
1463 /******************************** RealExp **************************/ | |
1464 | |
1465 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1466 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1467 { | |
1468 //printf("RealExp::RealExp(%Lg)\n", value); | |
1469 this->value = value; | |
1470 this->type = type; | |
1471 } | |
1472 | |
1473 char *RealExp::toChars() | |
1474 { | |
1475 char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1476 | |
1477 #ifdef IN_GCC | |
1478 value.format(buffer, sizeof(buffer)); | |
1479 if (type->isimaginary()) | |
1480 strcat(buffer, "i"); | |
1481 #else | |
1482 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1483 #endif | |
1484 assert(strlen(buffer) < sizeof(buffer)); | |
1485 return mem.strdup(buffer); | |
1486 } | |
1487 | |
1488 integer_t RealExp::toInteger() | |
1489 { | |
1490 #ifdef IN_GCC | |
1491 return toReal().toInt(); | |
1492 #else | |
1493 return (sinteger_t) toReal(); | |
1494 #endif | |
1495 } | |
1496 | |
1497 uinteger_t RealExp::toUInteger() | |
1498 { | |
1499 #ifdef IN_GCC | |
1500 return (uinteger_t) toReal().toInt(); | |
1501 #else | |
1502 return (uinteger_t) toReal(); | |
1503 #endif | |
1504 } | |
1505 | |
1506 real_t RealExp::toReal() | |
1507 { | |
1508 return type->isreal() ? value : 0; | |
1509 } | |
1510 | |
1511 real_t RealExp::toImaginary() | |
1512 { | |
1513 return type->isreal() ? 0 : value; | |
1514 } | |
1515 | |
1516 complex_t RealExp::toComplex() | |
1517 { | |
1518 #ifdef __DMC__ | |
1519 return toReal() + toImaginary() * I; | |
1520 #else | |
1521 return complex_t(toReal(), toImaginary()); | |
1522 #endif | |
1523 } | |
1524 | |
1525 /******************************** | |
1526 * Test to see if two reals are the same. | |
1527 * Regard NaN's as equivalent. | |
1528 * Regard +0 and -0 as different. | |
1529 */ | |
1530 | |
1531 int RealEquals(real_t x1, real_t x2) | |
1532 { | |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1533 return // special case nans |
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1534 (isnan(x1) && isnan(x2)) || |
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1535 // and zero, in order to distinguish +0 from -0 |
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1536 (x1 == 0 && x2 == 0 && 1./x1 == 1./x2) || |
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1537 // otherwise just compare |
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1538 (x1 != 0. && x1 == x2); |
159 | 1539 } |
1540 | |
1541 int RealExp::equals(Object *o) | |
1542 { RealExp *ne; | |
1543 | |
1544 if (this == o || | |
1545 (((Expression *)o)->op == TOKfloat64 && | |
1546 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1547 RealEquals(value, ne->value) | |
1548 ) | |
1549 ) | |
1550 return 1; | |
1551 return 0; | |
1552 } | |
1553 | |
1554 Expression *RealExp::semantic(Scope *sc) | |
1555 { | |
1556 if (!type) | |
1557 type = Type::tfloat64; | |
1558 else | |
1559 type = type->semantic(loc, sc); | |
1560 return this; | |
1561 } | |
1562 | |
1563 int RealExp::isBool(int result) | |
1564 { | |
1565 #ifdef IN_GCC | |
1566 return result ? (! value.isZero()) : (value.isZero()); | |
1567 #else | |
1568 return result ? (value != 0) | |
1569 : (value == 0); | |
1570 #endif | |
1571 } | |
1572 | |
1573 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1574 { | |
1575 /* In order to get an exact representation, try converting it | |
1576 * to decimal then back again. If it matches, use it. | |
1577 * If it doesn't, fall back to hex, which is | |
1578 * always exact. | |
1579 */ | |
1580 char buffer[25]; | |
1581 sprintf(buffer, "%Lg", value); | |
1582 assert(strlen(buffer) < sizeof(buffer)); | |
1583 #if _WIN32 && __DMC__ | |
1584 char *save = __locale_decpoint; | |
1585 __locale_decpoint = "."; | |
1586 real_t r = strtold(buffer, NULL); | |
1587 __locale_decpoint = save; | |
1588 #else | |
1589 real_t r = strtold(buffer, NULL); | |
1590 #endif | |
1591 if (r == value) // if exact duplication | |
1592 buf->writestring(buffer); | |
1593 else | |
1594 buf->printf("%La", value); // ensure exact duplication | |
1595 | |
1596 if (type) | |
1597 { | |
1598 Type *t = type->toBasetype(); | |
1599 switch (t->ty) | |
1600 { | |
1601 case Tfloat32: | |
1602 case Timaginary32: | |
1603 case Tcomplex32: | |
1604 buf->writeByte('F'); | |
1605 break; | |
1606 | |
1607 case Tfloat80: | |
1608 case Timaginary80: | |
1609 case Tcomplex80: | |
1610 buf->writeByte('L'); | |
1611 break; | |
1612 | |
1613 default: | |
1614 break; | |
1615 } | |
1616 if (t->isimaginary()) | |
1617 buf->writeByte('i'); | |
1618 } | |
1619 } | |
1620 | |
1621 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1622 { | |
1623 floatToBuffer(buf, type, value); | |
1624 } | |
1625 | |
1626 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1627 { | |
1628 /* Rely on %A to get portable mangling. | |
1629 * Must munge result to get only identifier characters. | |
1630 * | |
1631 * Possible values from %A => mangled result | |
1632 * NAN => NAN | |
1633 * -INF => NINF | |
1634 * INF => INF | |
1635 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1636 * 0X1.9P+2 => 19P2 | |
1637 */ | |
1638 | |
1639 if (isnan(value)) | |
1640 buf->writestring("NAN"); // no -NAN bugs | |
1641 else | |
1642 { | |
1643 char buffer[32]; | |
1644 int n = sprintf(buffer, "%LA", value); | |
1645 assert(n > 0 && n < sizeof(buffer)); | |
1646 for (int i = 0; i < n; i++) | |
1647 { char c = buffer[i]; | |
1648 | |
1649 switch (c) | |
1650 { | |
1651 case '-': | |
1652 buf->writeByte('N'); | |
1653 break; | |
1654 | |
1655 case '+': | |
1656 case 'X': | |
1657 case '.': | |
1658 break; | |
1659 | |
1660 case '0': | |
1661 if (i < 2) | |
1662 break; // skip leading 0X | |
1663 default: | |
1664 buf->writeByte(c); | |
1665 break; | |
1666 } | |
1667 } | |
1668 } | |
1669 } | |
1670 | |
1671 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1672 { | |
1673 buf->writeByte('e'); | |
1674 realToMangleBuffer(buf, value); | |
1675 } | |
1676 | |
1677 | |
1678 /******************************** ComplexExp **************************/ | |
1679 | |
1680 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1681 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1682 { | |
1683 this->value = value; | |
1684 this->type = type; | |
1685 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1686 } | |
1687 | |
1688 char *ComplexExp::toChars() | |
1689 { | |
1690 char buffer[sizeof(value) * 3 + 8 + 1]; | |
1691 | |
1692 #ifdef IN_GCC | |
1693 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1694 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1695 creall(value).format(buf1, sizeof(buf1)); | |
1696 cimagl(value).format(buf2, sizeof(buf2)); | |
1697 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1698 #else | |
1699 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1700 assert(strlen(buffer) < sizeof(buffer)); | |
1701 #endif | |
1702 return mem.strdup(buffer); | |
1703 } | |
1704 | |
1705 integer_t ComplexExp::toInteger() | |
1706 { | |
1707 #ifdef IN_GCC | |
1708 return (sinteger_t) toReal().toInt(); | |
1709 #else | |
1710 return (sinteger_t) toReal(); | |
1711 #endif | |
1712 } | |
1713 | |
1714 uinteger_t ComplexExp::toUInteger() | |
1715 { | |
1716 #ifdef IN_GCC | |
1717 return (uinteger_t) toReal().toInt(); | |
1718 #else | |
1719 return (uinteger_t) toReal(); | |
1720 #endif | |
1721 } | |
1722 | |
1723 real_t ComplexExp::toReal() | |
1724 { | |
1725 return creall(value); | |
1726 } | |
1727 | |
1728 real_t ComplexExp::toImaginary() | |
1729 { | |
1730 return cimagl(value); | |
1731 } | |
1732 | |
1733 complex_t ComplexExp::toComplex() | |
1734 { | |
1735 return value; | |
1736 } | |
1737 | |
1738 int ComplexExp::equals(Object *o) | |
1739 { ComplexExp *ne; | |
1740 | |
1741 if (this == o || | |
1742 (((Expression *)o)->op == TOKcomplex80 && | |
1743 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1744 RealEquals(creall(value), creall(ne->value)) && | |
1745 RealEquals(cimagl(value), cimagl(ne->value)) | |
1746 ) | |
1747 ) | |
1748 return 1; | |
1749 return 0; | |
1750 } | |
1751 | |
1752 Expression *ComplexExp::semantic(Scope *sc) | |
1753 { | |
1754 if (!type) | |
1755 type = Type::tcomplex80; | |
1756 else | |
1757 type = type->semantic(loc, sc); | |
1758 return this; | |
1759 } | |
1760 | |
1761 int ComplexExp::isBool(int result) | |
1762 { | |
1763 if (result) | |
1764 return (bool)(value); | |
1765 else | |
1766 return !value; | |
1767 } | |
1768 | |
1769 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1770 { | |
1771 /* Print as: | |
1772 * (re+imi) | |
1773 */ | |
1774 #ifdef IN_GCC | |
1775 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1776 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1777 creall(value).format(buf1, sizeof(buf1)); | |
1778 cimagl(value).format(buf2, sizeof(buf2)); | |
1779 buf->printf("(%s+%si)", buf1, buf2); | |
1780 #else | |
1781 buf->writeByte('('); | |
1782 floatToBuffer(buf, type, creall(value)); | |
1783 buf->writeByte('+'); | |
1784 floatToBuffer(buf, type, cimagl(value)); | |
1785 buf->writestring("i)"); | |
1786 #endif | |
1787 } | |
1788 | |
1789 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
1790 { | |
1791 buf->writeByte('c'); | |
1792 real_t r = toReal(); | |
1793 realToMangleBuffer(buf, r); | |
1794 buf->writeByte('c'); // separate the two | |
1795 r = toImaginary(); | |
1796 realToMangleBuffer(buf, r); | |
1797 } | |
1798 | |
1799 /******************************** IdentifierExp **************************/ | |
1800 | |
1801 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
1802 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
1803 { | |
1804 this->ident = ident; | |
1805 } | |
1806 | |
1807 Expression *IdentifierExp::semantic(Scope *sc) | |
1808 { | |
1809 Dsymbol *s; | |
1810 Dsymbol *scopesym; | |
1811 | |
1812 #if LOGSEMANTIC | |
1813 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
1814 #endif | |
1815 s = sc->search(loc, ident, &scopesym); | |
1816 if (s) | |
1817 { Expression *e; | |
1818 WithScopeSymbol *withsym; | |
1819 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1820 /* See if the symbol was a member of an enclosing 'with' |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1821 */ |
159 | 1822 withsym = scopesym->isWithScopeSymbol(); |
1823 if (withsym) | |
1824 { | |
1825 s = s->toAlias(); | |
1826 | |
1827 // Same as wthis.ident | |
1828 if (s->needThis() || s->isTemplateDeclaration()) | |
1829 { | |
1830 e = new VarExp(loc, withsym->withstate->wthis); | |
1831 e = new DotIdExp(loc, e, ident); | |
1832 } | |
1833 else | |
1834 { Type *t = withsym->withstate->wthis->type; | |
1835 if (t->ty == Tpointer) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1836 t = ((TypePointer *)t)->next; |
159 | 1837 e = new TypeDotIdExp(loc, t, ident); |
1838 } | |
1839 } | |
1840 else | |
1841 { | |
1842 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
1843 { // Kludge to run semantic() here because | |
1844 // ArrayScopeSymbol::search() doesn't have access to sc. | |
1845 s->semantic(sc); | |
1846 } | |
1847 // Look to see if f is really a function template | |
1848 FuncDeclaration *f = s->isFuncDeclaration(); | |
1849 if (f && f->parent) | |
1850 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
1851 | |
1852 if (ti && | |
1853 !ti->isTemplateMixin() && | |
1854 (ti->name == f->ident || | |
1855 ti->toAlias()->ident == f->ident) | |
1856 && | |
1857 ti->tempdecl && ti->tempdecl->onemember) | |
1858 { | |
1859 TemplateDeclaration *tempdecl = ti->tempdecl; | |
1860 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
1861 tempdecl = tempdecl->overroot; // then get the start | |
1862 e = new TemplateExp(loc, tempdecl); | |
1863 e = e->semantic(sc); | |
1864 return e; | |
1865 } | |
1866 } | |
1867 e = new DsymbolExp(loc, s); | |
1868 } | |
1869 return e->semantic(sc); | |
1870 } | |
1871 error("undefined identifier %s", ident->toChars()); | |
1872 type = Type::terror; | |
1873 return this; | |
1874 } | |
1875 | |
1876 char *IdentifierExp::toChars() | |
1877 { | |
1878 return ident->toChars(); | |
1879 } | |
1880 | |
1881 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1882 { | |
1883 if (hgs->hdrgen) | |
1884 buf->writestring(ident->toHChars2()); | |
1885 else | |
1886 buf->writestring(ident->toChars()); | |
1887 } | |
1888 | |
1889 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) | |
1890 { | |
1891 #if 0 | |
1892 tym = tybasic(e1->ET->Tty); | |
1893 if (!(tyscalar(tym) || | |
1894 tym == TYstruct || | |
1895 tym == TYarray && e->Eoper == TOKaddr)) | |
1896 synerr(EM_lvalue); // lvalue expected | |
1897 #endif | |
1898 return this; | |
1899 } | |
1900 | |
1901 /******************************** DollarExp **************************/ | |
1902 | |
1903 DollarExp::DollarExp(Loc loc) | |
1904 : IdentifierExp(loc, Id::dollar) | |
1905 { | |
1906 } | |
1907 | |
1908 /******************************** DsymbolExp **************************/ | |
1909 | |
1910 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
1911 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
1912 { | |
1913 this->s = s; | |
1914 } | |
1915 | |
1916 Expression *DsymbolExp::semantic(Scope *sc) | |
1917 { | |
1918 #if LOGSEMANTIC | |
1919 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
1920 #endif | |
1921 | |
1922 Lagain: | |
1923 EnumMember *em; | |
1924 Expression *e; | |
1925 VarDeclaration *v; | |
1926 FuncDeclaration *f; | |
1927 FuncLiteralDeclaration *fld; | |
1928 Declaration *d; | |
1929 ClassDeclaration *cd; | |
1930 ClassDeclaration *thiscd = NULL; | |
1931 Import *imp; | |
1932 Package *pkg; | |
1933 Type *t; | |
1934 | |
1935 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
1936 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
1937 if (type) | |
1938 return this; | |
1939 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
1940 checkDeprecated(sc, s); | |
1941 s = s->toAlias(); | |
1942 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
1943 if (!s->isFuncDeclaration()) | |
1944 checkDeprecated(sc, s); | |
1945 | |
1946 if (sc->func) | |
1947 thiscd = sc->func->parent->isClassDeclaration(); | |
1948 | |
1949 // BUG: This should happen after overload resolution for functions, not before | |
1950 if (s->needThis()) | |
1951 { | |
1952 if (hasThis(sc) /*&& !s->isFuncDeclaration()*/) | |
1953 { | |
1954 // Supply an implicit 'this', as in | |
1955 // this.ident | |
1956 | |
1957 DotVarExp *de; | |
1958 | |
1959 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
1960 return de->semantic(sc); | |
1961 } | |
1962 } | |
1963 | |
1964 em = s->isEnumMember(); | |
1965 if (em) | |
1966 { | |
1967 e = em->value->copy(); | |
1968 e = e->semantic(sc); | |
1969 return e; | |
1970 } | |
1971 v = s->isVarDeclaration(); | |
1972 if (v) | |
1973 { | |
1974 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
1975 if (!type) | |
1976 { type = v->type; | |
1977 if (!v->type) | |
1978 { error("forward reference of %s", v->toChars()); | |
1979 type = Type::terror; | |
1980 } | |
1981 } | |
1982 if (v->isConst() && type->toBasetype()->ty != Tsarray) | |
1983 { | |
1984 if (v->init) | |
1985 { | |
1986 if (v->inuse) | |
1987 { | |
1988 error("circular reference to '%s'", v->toChars()); | |
1989 type = Type::tint32; | |
1990 return this; | |
1991 } | |
1992 ExpInitializer *ei = v->init->isExpInitializer(); | |
1993 if (ei) | |
1994 { | |
1995 e = ei->exp->copy(); // make copy so we can change loc | |
1996 if (e->op == TOKstring || !e->type) | |
1997 e = e->semantic(sc); | |
1998 e = e->implicitCastTo(sc, type); | |
1999 e->loc = loc; | |
2000 return e; | |
2001 } | |
2002 } | |
2003 else | |
2004 { | |
2005 e = type->defaultInit(); | |
2006 e->loc = loc; | |
2007 return e; | |
2008 } | |
2009 } | |
2010 e = new VarExp(loc, v); | |
2011 e->type = type; | |
2012 e = e->semantic(sc); | |
2013 return e->deref(); | |
2014 } | |
2015 fld = s->isFuncLiteralDeclaration(); | |
2016 if (fld) | |
2017 { //printf("'%s' is a function literal\n", fld->toChars()); | |
2018 e = new FuncExp(loc, fld); | |
2019 return e->semantic(sc); | |
2020 } | |
2021 f = s->isFuncDeclaration(); | |
2022 if (f) | |
2023 { //printf("'%s' is a function\n", f->toChars()); | |
2024 return new VarExp(loc, f); | |
2025 } | |
2026 cd = s->isClassDeclaration(); | |
2027 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
2028 { | |
2029 // We need to add an implicit 'this' if cd is this class or a base class. | |
2030 DotTypeExp *dte; | |
2031 | |
2032 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
2033 return dte->semantic(sc); | |
2034 } | |
2035 imp = s->isImport(); | |
2036 if (imp) | |
2037 { | |
2038 ScopeExp *ie; | |
2039 | |
2040 ie = new ScopeExp(loc, imp->pkg); | |
2041 return ie->semantic(sc); | |
2042 } | |
2043 pkg = s->isPackage(); | |
2044 if (pkg) | |
2045 { | |
2046 ScopeExp *ie; | |
2047 | |
2048 ie = new ScopeExp(loc, pkg); | |
2049 return ie->semantic(sc); | |
2050 } | |
2051 Module *mod = s->isModule(); | |
2052 if (mod) | |
2053 { | |
2054 ScopeExp *ie; | |
2055 | |
2056 ie = new ScopeExp(loc, mod); | |
2057 return ie->semantic(sc); | |
2058 } | |
2059 | |
2060 t = s->getType(); | |
2061 if (t) | |
2062 { | |
2063 return new TypeExp(loc, t); | |
2064 } | |
2065 | |
2066 TupleDeclaration *tup = s->isTupleDeclaration(); | |
2067 if (tup) | |
2068 { | |
2069 e = new TupleExp(loc, tup); | |
2070 e = e->semantic(sc); | |
2071 return e; | |
2072 } | |
2073 | |
2074 TemplateInstance *ti = s->isTemplateInstance(); | |
2075 if (ti && !global.errors) | |
2076 { if (!ti->semanticdone) | |
2077 ti->semantic(sc); | |
2078 s = ti->inst->toAlias(); | |
2079 if (!s->isTemplateInstance()) | |
2080 goto Lagain; | |
2081 e = new ScopeExp(loc, ti); | |
2082 e = e->semantic(sc); | |
2083 return e; | |
2084 } | |
2085 | |
2086 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
2087 if (td) | |
2088 { | |
2089 e = new TemplateExp(loc, td); | |
2090 e = e->semantic(sc); | |
2091 return e; | |
2092 } | |
2093 | |
2094 Lerr: | |
2095 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
2096 type = Type::terror; | |
2097 return this; | |
2098 } | |
2099 | |
2100 char *DsymbolExp::toChars() | |
2101 { | |
2102 return s->toChars(); | |
2103 } | |
2104 | |
2105 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2106 { | |
2107 buf->writestring(s->toChars()); | |
2108 } | |
2109 | |
2110 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) | |
2111 { | |
2112 #if 0 | |
2113 tym = tybasic(e1->ET->Tty); | |
2114 if (!(tyscalar(tym) || | |
2115 tym == TYstruct || | |
2116 tym == TYarray && e->Eoper == TOKaddr)) | |
2117 synerr(EM_lvalue); // lvalue expected | |
2118 #endif | |
2119 return this; | |
2120 } | |
2121 | |
2122 /******************************** ThisExp **************************/ | |
2123 | |
2124 ThisExp::ThisExp(Loc loc) | |
2125 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
2126 { | |
2127 var = NULL; | |
2128 } | |
2129 | |
2130 Expression *ThisExp::semantic(Scope *sc) | |
2131 { FuncDeclaration *fd; | |
2132 FuncDeclaration *fdthis; | |
2133 int nested = 0; | |
2134 | |
2135 #if LOGSEMANTIC | |
2136 printf("ThisExp::semantic()\n"); | |
2137 #endif | |
2138 if (type) | |
2139 { //assert(global.errors || var); | |
2140 return this; | |
2141 } | |
2142 | |
2143 /* Special case for typeof(this) and typeof(super) since both | |
2144 * should work even if they are not inside a non-static member function | |
2145 */ | |
2146 if (sc->intypeof) | |
2147 { | |
2148 // Find enclosing struct or class | |
2149 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2150 { | |
2151 ClassDeclaration *cd; | |
2152 StructDeclaration *sd; | |
2153 | |
2154 if (!s) | |
2155 { | |
2156 error("%s is not in a struct or class scope", toChars()); | |
2157 goto Lerr; | |
2158 } | |
2159 cd = s->isClassDeclaration(); | |
2160 if (cd) | |
2161 { | |
2162 type = cd->type; | |
2163 return this; | |
2164 } | |
2165 sd = s->isStructDeclaration(); | |
2166 if (sd) | |
2167 { | |
2168 type = sd->type->pointerTo(); | |
2169 return this; | |
2170 } | |
2171 } | |
2172 } | |
2173 | |
2174 fdthis = sc->parent->isFuncDeclaration(); | |
2175 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
2176 if (!fd) | |
2177 goto Lerr; | |
2178 | |
2179 assert(fd->vthis); | |
2180 var = fd->vthis; | |
2181 assert(var->parent); | |
2182 type = var->type; | |
2183 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2184 #if 0 | |
2185 if (fd != fdthis) // if nested | |
2186 { | |
2187 fdthis->getLevel(loc, fd); | |
2188 fd->vthis->nestedref = 1; | |
2189 fd->nestedFrameRef = 1; | |
2190 } | |
2191 #endif | |
336 | 2192 if (!sc->intypeof) |
2193 sc->callSuper |= CSXthis; | |
159 | 2194 return this; |
2195 | |
2196 Lerr: | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2197 error("'this' is only defined in non-static member functions, not %s", sc->parent->toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2198 type = Type::terror; |
159 | 2199 return this; |
2200 } | |
2201 | |
2202 int ThisExp::isBool(int result) | |
2203 { | |
2204 return result ? TRUE : FALSE; | |
2205 } | |
2206 | |
2207 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2208 { | |
2209 buf->writestring("this"); | |
2210 } | |
2211 | |
2212 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) | |
2213 { | |
2214 return this; | |
2215 } | |
2216 | |
2217 /******************************** SuperExp **************************/ | |
2218 | |
2219 SuperExp::SuperExp(Loc loc) | |
2220 : ThisExp(loc) | |
2221 { | |
2222 op = TOKsuper; | |
2223 } | |
2224 | |
2225 Expression *SuperExp::semantic(Scope *sc) | |
2226 { FuncDeclaration *fd; | |
2227 FuncDeclaration *fdthis; | |
2228 ClassDeclaration *cd; | |
2229 Dsymbol *s; | |
2230 | |
2231 #if LOGSEMANTIC | |
2232 printf("SuperExp::semantic('%s')\n", toChars()); | |
2233 #endif | |
2234 if (type) | |
2235 return this; | |
2236 | |
2237 /* Special case for typeof(this) and typeof(super) since both | |
2238 * should work even if they are not inside a non-static member function | |
2239 */ | |
2240 if (sc->intypeof) | |
2241 { | |
2242 // Find enclosing class | |
2243 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2244 { | |
2245 ClassDeclaration *cd; | |
2246 | |
2247 if (!s) | |
2248 { | |
2249 error("%s is not in a class scope", toChars()); | |
2250 goto Lerr; | |
2251 } | |
2252 cd = s->isClassDeclaration(); | |
2253 if (cd) | |
2254 { | |
2255 cd = cd->baseClass; | |
2256 if (!cd) | |
2257 { error("class %s has no 'super'", s->toChars()); | |
2258 goto Lerr; | |
2259 } | |
2260 type = cd->type; | |
2261 return this; | |
2262 } | |
2263 } | |
2264 } | |
2265 | |
2266 fdthis = sc->parent->isFuncDeclaration(); | |
2267 fd = hasThis(sc); | |
2268 if (!fd) | |
2269 goto Lerr; | |
2270 assert(fd->vthis); | |
2271 var = fd->vthis; | |
2272 assert(var->parent); | |
2273 | |
2274 s = fd->toParent(); | |
2275 while (s && s->isTemplateInstance()) | |
2276 s = s->toParent(); | |
2277 assert(s); | |
2278 cd = s->isClassDeclaration(); | |
2279 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2280 if (!cd) | |
2281 goto Lerr; | |
2282 if (!cd->baseClass) | |
2283 { | |
2284 error("no base class for %s", cd->toChars()); | |
2285 type = fd->vthis->type; | |
2286 } | |
2287 else | |
2288 { | |
2289 type = cd->baseClass->type; | |
2290 } | |
2291 | |
2292 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2293 #if 0 | |
2294 if (fd != fdthis) | |
2295 { | |
2296 fdthis->getLevel(loc, fd); | |
2297 fd->vthis->nestedref = 1; | |
2298 fd->nestedFrameRef = 1; | |
2299 } | |
2300 #endif | |
2301 | |
336 | 2302 if (!sc->intypeof) |
2303 sc->callSuper |= CSXsuper; | |
159 | 2304 return this; |
2305 | |
2306 | |
2307 Lerr: | |
2308 error("'super' is only allowed in non-static class member functions"); | |
2309 type = Type::tint32; | |
2310 return this; | |
2311 } | |
2312 | |
2313 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2314 { | |
2315 buf->writestring("super"); | |
2316 } | |
2317 | |
2318 | |
2319 /******************************** NullExp **************************/ | |
2320 | |
2321 NullExp::NullExp(Loc loc) | |
2322 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2323 { | |
2324 committed = 0; | |
2325 } | |
2326 | |
2327 Expression *NullExp::semantic(Scope *sc) | |
2328 { | |
2329 #if LOGSEMANTIC | |
2330 printf("NullExp::semantic('%s')\n", toChars()); | |
2331 #endif | |
2332 // NULL is the same as (void *)0 | |
2333 if (!type) | |
2334 type = Type::tvoid->pointerTo(); | |
2335 return this; | |
2336 } | |
2337 | |
2338 int NullExp::isBool(int result) | |
2339 { | |
2340 return result ? FALSE : TRUE; | |
2341 } | |
2342 | |
2343 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2344 { | |
2345 buf->writestring("null"); | |
2346 } | |
2347 | |
2348 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2349 { | |
2350 buf->writeByte('n'); | |
2351 } | |
2352 | |
2353 /******************************** StringExp **************************/ | |
2354 | |
2355 StringExp::StringExp(Loc loc, char *string) | |
2356 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2357 { | |
2358 this->string = string; | |
2359 this->len = strlen(string); | |
2360 this->sz = 1; | |
2361 this->committed = 0; | |
2362 this->postfix = 0; | |
2363 } | |
2364 | |
2365 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2366 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2367 { | |
2368 this->string = string; | |
2369 this->len = len; | |
2370 this->sz = 1; | |
2371 this->committed = 0; | |
2372 this->postfix = 0; | |
2373 } | |
2374 | |
2375 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2376 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2377 { | |
2378 this->string = string; | |
2379 this->len = len; | |
2380 this->sz = 1; | |
2381 this->committed = 0; | |
2382 this->postfix = postfix; | |
2383 } | |
2384 | |
2385 #if 0 | |
2386 Expression *StringExp::syntaxCopy() | |
2387 { | |
2388 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2389 return copy(); | |
2390 } | |
2391 #endif | |
2392 | |
2393 int StringExp::equals(Object *o) | |
2394 { | |
2395 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2396 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2397 { Expression *e = (Expression *)o; | |
2398 | |
2399 if (e->op == TOKstring) | |
2400 { | |
2401 return compare(o) == 0; | |
2402 } | |
2403 } | |
2404 return FALSE; | |
2405 } | |
2406 | |
2407 char *StringExp::toChars() | |
2408 { | |
2409 OutBuffer buf; | |
2410 HdrGenState hgs; | |
2411 char *p; | |
2412 | |
2413 memset(&hgs, 0, sizeof(hgs)); | |
2414 toCBuffer(&buf, &hgs); | |
2415 buf.writeByte(0); | |
2416 p = (char *)buf.data; | |
2417 buf.data = NULL; | |
2418 return p; | |
2419 } | |
2420 | |
2421 Expression *StringExp::semantic(Scope *sc) | |
2422 { | |
2423 #if LOGSEMANTIC | |
2424 printf("StringExp::semantic() %s\n", toChars()); | |
2425 #endif | |
2426 if (!type) | |
2427 { OutBuffer buffer; | |
2428 size_t newlen = 0; | |
2429 char *p; | |
2430 size_t u; | |
2431 unsigned c; | |
2432 | |
2433 switch (postfix) | |
2434 { | |
2435 case 'd': | |
2436 for (u = 0; u < len;) | |
2437 { | |
2438 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2439 if (p) | |
2440 { error("%s", p); | |
2441 break; | |
2442 } | |
2443 else | |
2444 { buffer.write4(c); | |
2445 newlen++; | |
2446 } | |
2447 } | |
2448 buffer.write4(0); | |
2449 string = buffer.extractData(); | |
2450 len = newlen; | |
2451 sz = 4; | |
2452 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2453 committed = 1; | |
2454 break; | |
2455 | |
2456 case 'w': | |
2457 for (u = 0; u < len;) | |
2458 { | |
2459 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2460 if (p) | |
2461 { error("%s", p); | |
2462 break; | |
2463 } | |
2464 else | |
2465 { buffer.writeUTF16(c); | |
2466 newlen++; | |
2467 if (c >= 0x10000) | |
2468 newlen++; | |
2469 } | |
2470 } | |
2471 buffer.writeUTF16(0); | |
2472 string = buffer.extractData(); | |
2473 len = newlen; | |
2474 sz = 2; | |
2475 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2476 committed = 1; | |
2477 break; | |
2478 | |
2479 case 'c': | |
2480 committed = 1; | |
2481 default: | |
2482 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2483 break; | |
2484 } | |
2485 type = type->semantic(loc, sc); | |
2486 } | |
2487 return this; | |
2488 } | |
2489 | |
2490 /**************************************** | |
2491 * Convert string to char[]. | |
2492 */ | |
2493 | |
2494 StringExp *StringExp::toUTF8(Scope *sc) | |
2495 { | |
2496 if (sz != 1) | |
2497 { // Convert to UTF-8 string | |
2498 committed = 0; | |
2499 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2500 e = e->optimize(WANTvalue); | |
2501 assert(e->op == TOKstring); | |
2502 StringExp *se = (StringExp *)e; | |
2503 assert(se->sz == 1); | |
2504 return se; | |
2505 } | |
2506 return this; | |
2507 } | |
2508 | |
2509 int StringExp::compare(Object *obj) | |
2510 { | |
2511 // Used to sort case statement expressions so we can do an efficient lookup | |
2512 StringExp *se2 = (StringExp *)(obj); | |
2513 | |
2514 // This is a kludge so isExpression() in template.c will return 5 | |
2515 // for StringExp's. | |
2516 if (!se2) | |
2517 return 5; | |
2518 | |
2519 assert(se2->op == TOKstring); | |
2520 | |
2521 int len1 = len; | |
2522 int len2 = se2->len; | |
2523 | |
2524 if (len1 == len2) | |
2525 { | |
2526 switch (sz) | |
2527 { | |
2528 case 1: | |
2529 return strcmp((char *)string, (char *)se2->string); | |
2530 | |
2531 case 2: | |
2532 { unsigned u; | |
2533 d_wchar *s1 = (d_wchar *)string; | |
2534 d_wchar *s2 = (d_wchar *)se2->string; | |
2535 | |
2536 for (u = 0; u < len; u++) | |
2537 { | |
2538 if (s1[u] != s2[u]) | |
2539 return s1[u] - s2[u]; | |
2540 } | |
2541 } | |
2542 | |
2543 case 4: | |
2544 { unsigned u; | |
2545 d_dchar *s1 = (d_dchar *)string; | |
2546 d_dchar *s2 = (d_dchar *)se2->string; | |
2547 | |
2548 for (u = 0; u < len; u++) | |
2549 { | |
2550 if (s1[u] != s2[u]) | |
2551 return s1[u] - s2[u]; | |
2552 } | |
2553 } | |
2554 break; | |
2555 | |
2556 default: | |
2557 assert(0); | |
2558 } | |
2559 } | |
2560 return len1 - len2; | |
2561 } | |
2562 | |
2563 int StringExp::isBool(int result) | |
2564 { | |
2565 return result ? TRUE : FALSE; | |
2566 } | |
2567 | |
2568 unsigned StringExp::charAt(size_t i) | |
2569 { unsigned value; | |
2570 | |
2571 switch (sz) | |
2572 { | |
2573 case 1: | |
2574 value = ((unsigned char *)string)[i]; | |
2575 break; | |
2576 | |
2577 case 2: | |
2578 value = ((unsigned short *)string)[i]; | |
2579 break; | |
2580 | |
2581 case 4: | |
2582 value = ((unsigned int *)string)[i]; | |
2583 break; | |
2584 | |
2585 default: | |
2586 assert(0); | |
2587 break; | |
2588 } | |
2589 return value; | |
2590 } | |
2591 | |
2592 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2593 { | |
2594 buf->writeByte('"'); | |
2595 for (size_t i = 0; i < len; i++) | |
2596 { unsigned c = charAt(i); | |
2597 | |
2598 switch (c) | |
2599 { | |
2600 case '"': | |
2601 case '\\': | |
2602 if (!hgs->console) | |
2603 buf->writeByte('\\'); | |
2604 default: | |
2605 if (c <= 0xFF) | |
2606 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2607 buf->writeByte(c); | |
2608 else | |
2609 buf->printf("\\x%02x", c); | |
2610 } | |
2611 else if (c <= 0xFFFF) | |
2612 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2613 else | |
2614 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2615 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2616 break; | |
2617 } | |
2618 } | |
2619 buf->writeByte('"'); | |
2620 if (postfix) | |
2621 buf->writeByte(postfix); | |
2622 } | |
2623 | |
2624 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2625 { char m; | |
2626 OutBuffer tmp; | |
2627 char *p; | |
2628 unsigned c; | |
2629 size_t u; | |
2630 unsigned char *q; | |
2631 unsigned qlen; | |
2632 | |
2633 /* Write string in UTF-8 format | |
2634 */ | |
2635 switch (sz) | |
2636 { case 1: | |
2637 m = 'a'; | |
2638 q = (unsigned char *)string; | |
2639 qlen = len; | |
2640 break; | |
2641 case 2: | |
2642 m = 'w'; | |
2643 for (u = 0; u < len; ) | |
2644 { | |
2645 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2646 if (p) | |
2647 error("%s", p); | |
2648 else | |
2649 tmp.writeUTF8(c); | |
2650 } | |
2651 q = tmp.data; | |
2652 qlen = tmp.offset; | |
2653 break; | |
2654 case 4: | |
2655 m = 'd'; | |
2656 for (u = 0; u < len; u++) | |
2657 { | |
2658 c = ((unsigned *)string)[u]; | |
2659 if (!utf_isValidDchar(c)) | |
2660 error("invalid UCS-32 char \\U%08x", c); | |
2661 else | |
2662 tmp.writeUTF8(c); | |
2663 } | |
2664 q = tmp.data; | |
2665 qlen = tmp.offset; | |
2666 break; | |
2667 default: | |
2668 assert(0); | |
2669 } | |
2670 buf->writeByte(m); | |
2671 buf->printf("%d_", qlen); | |
2672 for (size_t i = 0; i < qlen; i++) | |
2673 buf->printf("%02x", q[i]); | |
2674 } | |
2675 | |
2676 /************************ ArrayLiteralExp ************************************/ | |
2677 | |
2678 // [ e1, e2, e3, ... ] | |
2679 | |
2680 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2681 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2682 { | |
2683 this->elements = elements; | |
2684 } | |
2685 | |
2686 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2687 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2688 { | |
2689 elements = new Expressions; | |
2690 elements->push(e); | |
2691 } | |
2692 | |
2693 Expression *ArrayLiteralExp::syntaxCopy() | |
2694 { | |
2695 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2696 } | |
2697 | |
2698 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2699 { Expression *e; | |
2700 Type *t0 = NULL; | |
2701 | |
2702 #if LOGSEMANTIC | |
2703 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2704 #endif | |
2705 if (type) | |
2706 return this; | |
2707 | |
2708 // Run semantic() on each element | |
2709 for (int i = 0; i < elements->dim; i++) | |
2710 { e = (Expression *)elements->data[i]; | |
2711 e = e->semantic(sc); | |
2712 elements->data[i] = (void *)e; | |
2713 } | |
2714 expandTuples(elements); | |
2715 for (int i = 0; i < elements->dim; i++) | |
2716 { e = (Expression *)elements->data[i]; | |
2717 | |
2718 if (!e->type) | |
2719 error("%s has no value", e->toChars()); | |
2720 e = resolveProperties(sc, e); | |
2721 | |
2722 unsigned char committed = 1; | |
2723 if (e->op == TOKstring) | |
2724 committed = ((StringExp *)e)->committed; | |
2725 | |
2726 if (!t0) | |
2727 { t0 = e->type; | |
2728 // Convert any static arrays to dynamic arrays | |
2729 if (t0->ty == Tsarray) | |
2730 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2731 t0 = ((TypeSArray *)t0)->next->arrayOf(); |
159 | 2732 e = e->implicitCastTo(sc, t0); |
2733 } | |
2734 } | |
2735 else | |
2736 e = e->implicitCastTo(sc, t0); | |
2737 if (!committed && e->op == TOKstring) | |
2738 { StringExp *se = (StringExp *)e; | |
2739 se->committed = 0; | |
2740 } | |
2741 elements->data[i] = (void *)e; | |
2742 } | |
2743 | |
2744 if (!t0) | |
2745 t0 = Type::tvoid; | |
2746 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
2747 type = type->semantic(loc, sc); | |
2748 return this; | |
2749 } | |
2750 | |
2751 int ArrayLiteralExp::checkSideEffect(int flag) | |
2752 { int f = 0; | |
2753 | |
2754 for (size_t i = 0; i < elements->dim; i++) | |
2755 { Expression *e = (Expression *)elements->data[i]; | |
2756 | |
2757 f |= e->checkSideEffect(2); | |
2758 } | |
2759 if (flag == 0 && f == 0) | |
2760 Expression::checkSideEffect(0); | |
2761 return f; | |
2762 } | |
2763 | |
2764 int ArrayLiteralExp::isBool(int result) | |
2765 { | |
2766 size_t dim = elements ? elements->dim : 0; | |
2767 return result ? (dim != 0) : (dim == 0); | |
2768 } | |
2769 | |
2770 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2771 { | |
2772 buf->writeByte('['); | |
2773 argsToCBuffer(buf, elements, hgs); | |
2774 buf->writeByte(']'); | |
2775 } | |
2776 | |
2777 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2778 { | |
2779 size_t dim = elements ? elements->dim : 0; | |
2780 buf->printf("A%u", dim); | |
2781 for (size_t i = 0; i < dim; i++) | |
2782 { Expression *e = (Expression *)elements->data[i]; | |
2783 e->toMangleBuffer(buf); | |
2784 } | |
2785 } | |
2786 | |
2787 /************************ AssocArrayLiteralExp ************************************/ | |
2788 | |
2789 // [ key0 : value0, key1 : value1, ... ] | |
2790 | |
2791 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
2792 Expressions *keys, Expressions *values) | |
2793 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
2794 { | |
2795 assert(keys->dim == values->dim); | |
2796 this->keys = keys; | |
2797 this->values = values; | |
2798 } | |
2799 | |
2800 Expression *AssocArrayLiteralExp::syntaxCopy() | |
2801 { | |
2802 return new AssocArrayLiteralExp(loc, | |
2803 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
2804 } | |
2805 | |
2806 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
2807 { Expression *e; | |
2808 Type *tkey = NULL; | |
2809 Type *tvalue = NULL; | |
2810 | |
2811 #if LOGSEMANTIC | |
2812 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
2813 #endif | |
2814 | |
2815 // Run semantic() on each element | |
2816 for (size_t i = 0; i < keys->dim; i++) | |
2817 { Expression *key = (Expression *)keys->data[i]; | |
2818 Expression *value = (Expression *)values->data[i]; | |
2819 | |
2820 key = key->semantic(sc); | |
2821 value = value->semantic(sc); | |
2822 | |
2823 keys->data[i] = (void *)key; | |
2824 values->data[i] = (void *)value; | |
2825 } | |
2826 expandTuples(keys); | |
2827 expandTuples(values); | |
2828 if (keys->dim != values->dim) | |
2829 { | |
2830 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
2831 keys->setDim(0); | |
2832 values->setDim(0); | |
2833 } | |
2834 for (size_t i = 0; i < keys->dim; i++) | |
2835 { Expression *key = (Expression *)keys->data[i]; | |
2836 Expression *value = (Expression *)values->data[i]; | |
2837 | |
2838 if (!key->type) | |
2839 error("%s has no value", key->toChars()); | |
2840 if (!value->type) | |
2841 error("%s has no value", value->toChars()); | |
2842 key = resolveProperties(sc, key); | |
2843 value = resolveProperties(sc, value); | |
2844 | |
2845 if (!tkey) | |
2846 tkey = key->type; | |
2847 else | |
2848 key = key->implicitCastTo(sc, tkey); | |
2849 keys->data[i] = (void *)key; | |
2850 | |
2851 if (!tvalue) | |
2852 tvalue = value->type; | |
2853 else | |
2854 value = value->implicitCastTo(sc, tvalue); | |
2855 values->data[i] = (void *)value; | |
2856 } | |
2857 | |
2858 if (!tkey) | |
2859 tkey = Type::tvoid; | |
2860 if (!tvalue) | |
2861 tvalue = Type::tvoid; | |
2862 type = new TypeAArray(tvalue, tkey); | |
2863 type = type->semantic(loc, sc); | |
2864 return this; | |
2865 } | |
2866 | |
2867 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
2868 { int f = 0; | |
2869 | |
2870 for (size_t i = 0; i < keys->dim; i++) | |
2871 { Expression *key = (Expression *)keys->data[i]; | |
2872 Expression *value = (Expression *)values->data[i]; | |
2873 | |
2874 f |= key->checkSideEffect(2); | |
2875 f |= value->checkSideEffect(2); | |
2876 } | |
2877 if (flag == 0 && f == 0) | |
2878 Expression::checkSideEffect(0); | |
2879 return f; | |
2880 } | |
2881 | |
2882 int AssocArrayLiteralExp::isBool(int result) | |
2883 { | |
2884 size_t dim = keys->dim; | |
2885 return result ? (dim != 0) : (dim == 0); | |
2886 } | |
2887 | |
2888 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2889 { | |
2890 buf->writeByte('['); | |
2891 for (size_t i = 0; i < keys->dim; i++) | |
2892 { Expression *key = (Expression *)keys->data[i]; | |
2893 Expression *value = (Expression *)values->data[i]; | |
2894 | |
2895 if (i) | |
2896 buf->writeByte(','); | |
2897 expToCBuffer(buf, hgs, key, PREC_assign); | |
2898 buf->writeByte(':'); | |
2899 expToCBuffer(buf, hgs, value, PREC_assign); | |
2900 } | |
2901 buf->writeByte(']'); | |
2902 } | |
2903 | |
2904 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2905 { | |
2906 size_t dim = keys->dim; | |
2907 buf->printf("A%u", dim); | |
2908 for (size_t i = 0; i < dim; i++) | |
2909 { Expression *key = (Expression *)keys->data[i]; | |
2910 Expression *value = (Expression *)values->data[i]; | |
2911 | |
2912 key->toMangleBuffer(buf); | |
2913 value->toMangleBuffer(buf); | |
2914 } | |
2915 } | |
2916 | |
2917 /************************ StructLiteralExp ************************************/ | |
2918 | |
2919 // sd( e1, e2, e3, ... ) | |
2920 | |
2921 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
2922 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
2923 { | |
2924 this->sd = sd; | |
2925 this->elements = elements; | |
2926 this->sym = NULL; | |
2927 this->soffset = 0; | |
2928 this->fillHoles = 1; | |
2929 } | |
2930 | |
2931 Expression *StructLiteralExp::syntaxCopy() | |
2932 { | |
2933 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
2934 } | |
2935 | |
2936 Expression *StructLiteralExp::semantic(Scope *sc) | |
2937 { Expression *e; | |
2938 | |
2939 #if LOGSEMANTIC | |
2940 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
2941 #endif | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2942 if (type) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2943 return this; |
159 | 2944 |
2945 // Run semantic() on each element | |
2946 for (size_t i = 0; i < elements->dim; i++) | |
2947 { e = (Expression *)elements->data[i]; | |
2948 if (!e) | |
2949 continue; | |
2950 e = e->semantic(sc); | |
2951 elements->data[i] = (void *)e; | |
2952 } | |
2953 expandTuples(elements); | |
2954 size_t offset = 0; | |
2955 for (size_t i = 0; i < elements->dim; i++) | |
2956 { e = (Expression *)elements->data[i]; | |
2957 if (!e) | |
2958 continue; | |
2959 | |
2960 if (!e->type) | |
2961 error("%s has no value", e->toChars()); | |
2962 e = resolveProperties(sc, e); | |
2963 if (i >= sd->fields.dim) | |
2964 { error("more initializers than fields of %s", sd->toChars()); | |
2965 break; | |
2966 } | |
2967 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
2968 VarDeclaration *v = s->isVarDeclaration(); | |
2969 assert(v); | |
2970 if (v->offset < offset) | |
2971 error("overlapping initialization for %s", v->toChars()); | |
2972 offset = v->offset + v->type->size(); | |
2973 | |
2974 Type *telem = v->type; | |
2975 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
2976 { /* Static array initialization, as in: | |
2977 * T[3][5] = e; | |
2978 */ | |
2979 telem = telem->toBasetype()->nextOf(); | |
2980 } | |
2981 | |
2982 e = e->implicitCastTo(sc, telem); | |
2983 | |
2984 elements->data[i] = (void *)e; | |
2985 } | |
2986 | |
2987 /* Fill out remainder of elements[] with default initializers for fields[] | |
2988 */ | |
2989 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
2990 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
2991 VarDeclaration *v = s->isVarDeclaration(); | |
2992 assert(v); | |
2993 | |
2994 if (v->offset < offset) | |
2995 { e = NULL; | |
2996 sd->hasUnions = 1; | |
2997 } | |
2998 else | |
2999 { | |
3000 if (v->init) | |
3001 { e = v->init->toExpression(); | |
3002 if (!e) | |
3003 error("cannot make expression out of initializer for %s", v->toChars()); | |
3004 } | |
3005 else | |
3006 { e = v->type->defaultInit(); | |
3007 e->loc = loc; | |
3008 } | |
3009 offset = v->offset + v->type->size(); | |
3010 } | |
3011 elements->push(e); | |
3012 } | |
3013 | |
3014 type = sd->type; | |
3015 return this; | |
3016 } | |
3017 | |
3018 /************************************** | |
3019 * Gets expression at offset of type. | |
3020 * Returns NULL if not found. | |
3021 */ | |
3022 | |
3023 Expression *StructLiteralExp::getField(Type *type, unsigned offset) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3024 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3025 //printf("StructLiteralExp::getField(this = %s, type = %s, offset = %u)\n", |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3026 // /*toChars()*/"", type->toChars(), offset); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3027 Expression *e = NULL; |
159 | 3028 int i = getFieldIndex(type, offset); |
3029 | |
3030 if (i != -1) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3031 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3032 //printf("\ti = %d\n", i); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3033 assert(i < elements->dim); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3034 e = (Expression *)elements->data[i]; |
159 | 3035 if (e) |
3036 { | |
3037 e = e->copy(); | |
3038 e->type = type; | |
3039 } | |
3040 } | |
3041 return e; | |
3042 } | |
3043 | |
3044 /************************************ | |
3045 * Get index of field. | |
3046 * Returns -1 if not found. | |
3047 */ | |
3048 | |
3049 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
3050 { | |
3051 /* Find which field offset is by looking at the field offsets | |
3052 */ | |
3053 for (size_t i = 0; i < sd->fields.dim; i++) | |
3054 { | |
3055 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3056 VarDeclaration *v = s->isVarDeclaration(); | |
3057 assert(v); | |
3058 | |
3059 if (offset == v->offset && | |
3060 type->size() == v->type->size()) | |
3061 { Expression *e = (Expression *)elements->data[i]; | |
3062 if (e) | |
3063 { | |
3064 return i; | |
3065 } | |
3066 break; | |
3067 } | |
3068 } | |
3069 return -1; | |
3070 } | |
3071 | |
3072 | |
3073 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) | |
3074 { | |
3075 return this; | |
3076 } | |
3077 | |
3078 | |
3079 int StructLiteralExp::checkSideEffect(int flag) | |
3080 { int f = 0; | |
3081 | |
3082 for (size_t i = 0; i < elements->dim; i++) | |
3083 { Expression *e = (Expression *)elements->data[i]; | |
3084 if (!e) | |
3085 continue; | |
3086 | |
3087 f |= e->checkSideEffect(2); | |
3088 } | |
3089 if (flag == 0 && f == 0) | |
3090 Expression::checkSideEffect(0); | |
3091 return f; | |
3092 } | |
3093 | |
3094 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3095 { | |
3096 buf->writestring(sd->toChars()); | |
3097 buf->writeByte('('); | |
3098 argsToCBuffer(buf, elements, hgs); | |
3099 buf->writeByte(')'); | |
3100 } | |
3101 | |
3102 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3103 { | |
3104 size_t dim = elements ? elements->dim : 0; | |
3105 buf->printf("S%u", dim); | |
3106 for (size_t i = 0; i < dim; i++) | |
3107 { Expression *e = (Expression *)elements->data[i]; | |
3108 if (e) | |
3109 e->toMangleBuffer(buf); | |
3110 else | |
3111 buf->writeByte('v'); // 'v' for void | |
3112 } | |
3113 } | |
3114 | |
3115 /************************ TypeDotIdExp ************************************/ | |
3116 | |
3117 /* Things like: | |
3118 * int.size | |
3119 * foo.size | |
3120 * (foo).size | |
3121 * cast(foo).size | |
3122 */ | |
3123 | |
3124 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident) | |
3125 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp)) | |
3126 { | |
3127 this->type = type; | |
3128 this->ident = ident; | |
3129 } | |
3130 | |
3131 Expression *TypeDotIdExp::syntaxCopy() | |
3132 { | |
3133 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident); | |
3134 return te; | |
3135 } | |
3136 | |
3137 Expression *TypeDotIdExp::semantic(Scope *sc) | |
3138 { Expression *e; | |
3139 | |
3140 #if LOGSEMANTIC | |
3141 printf("TypeDotIdExp::semantic()\n"); | |
3142 #endif | |
3143 e = new DotIdExp(loc, new TypeExp(loc, type), ident); | |
3144 e = e->semantic(sc); | |
3145 return e; | |
3146 } | |
3147 | |
3148 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3149 { | |
3150 buf->writeByte('('); | |
3151 type->toCBuffer(buf, NULL, hgs); | |
3152 buf->writeByte(')'); | |
3153 buf->writeByte('.'); | |
3154 buf->writestring(ident->toChars()); | |
3155 } | |
3156 | |
3157 /************************************************************/ | |
3158 | |
3159 // Mainly just a placeholder | |
3160 | |
3161 TypeExp::TypeExp(Loc loc, Type *type) | |
3162 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
3163 { | |
3164 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
3165 this->type = type; | |
3166 } | |
3167 | |
3168 Expression *TypeExp::semantic(Scope *sc) | |
3169 { | |
3170 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
3171 type = type->semantic(loc, sc); | |
3172 return this; | |
3173 } | |
3174 | |
3175 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3176 { | |
3177 type->toCBuffer(buf, NULL, hgs); | |
3178 } | |
3179 | |
3180 /************************************************************/ | |
3181 | |
3182 // Mainly just a placeholder | |
3183 | |
3184 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
3185 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
3186 { | |
3187 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
3188 //static int count; if (++count == 38) *(char*)0=0; | |
3189 this->sds = pkg; | |
3190 } | |
3191 | |
3192 Expression *ScopeExp::syntaxCopy() | |
3193 { | |
3194 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
3195 return se; | |
3196 } | |
3197 | |
3198 Expression *ScopeExp::semantic(Scope *sc) | |
3199 { | |
3200 TemplateInstance *ti; | |
3201 ScopeDsymbol *sds2; | |
3202 | |
3203 #if LOGSEMANTIC | |
3204 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
3205 #endif | |
3206 Lagain: | |
3207 ti = sds->isTemplateInstance(); | |
3208 if (ti && !global.errors) | |
3209 { Dsymbol *s; | |
3210 if (!ti->semanticdone) | |
3211 ti->semantic(sc); | |
3212 s = ti->inst->toAlias(); | |
3213 sds2 = s->isScopeDsymbol(); | |
3214 if (!sds2) | |
3215 { Expression *e; | |
3216 | |
3217 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
3218 if (ti->withsym) | |
3219 { | |
3220 // Same as wthis.s | |
3221 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3222 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3223 } | |
3224 else | |
3225 e = new DsymbolExp(loc, s); | |
3226 e = e->semantic(sc); | |
3227 //printf("-1ScopeExp::semantic()\n"); | |
3228 return e; | |
3229 } | |
3230 if (sds2 != sds) | |
3231 { | |
3232 sds = sds2; | |
3233 goto Lagain; | |
3234 } | |
3235 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3236 } | |
3237 else | |
3238 { | |
3239 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3240 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3241 sds->semantic(sc); | |
3242 } | |
3243 type = Type::tvoid; | |
3244 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3245 return this; | |
3246 } | |
3247 | |
3248 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3249 { | |
3250 if (sds->isTemplateInstance()) | |
3251 { | |
3252 sds->toCBuffer(buf, hgs); | |
3253 } | |
3254 else | |
3255 { | |
3256 buf->writestring(sds->kind()); | |
3257 buf->writestring(" "); | |
3258 buf->writestring(sds->toChars()); | |
3259 } | |
3260 } | |
3261 | |
3262 /********************** TemplateExp **************************************/ | |
3263 | |
3264 // Mainly just a placeholder | |
3265 | |
3266 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3267 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3268 { | |
3269 //printf("TemplateExp(): %s\n", td->toChars()); | |
3270 this->td = td; | |
3271 } | |
3272 | |
3273 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3274 { | |
3275 buf->writestring(td->toChars()); | |
3276 } | |
3277 | |
3278 void TemplateExp::rvalue() | |
3279 { | |
3280 error("template %s has no value", toChars()); | |
3281 } | |
3282 | |
3283 /********************** NewExp **************************************/ | |
3284 | |
3285 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, | |
3286 Type *newtype, Expressions *arguments) | |
3287 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3288 { | |
3289 this->thisexp = thisexp; | |
3290 this->newargs = newargs; | |
3291 this->newtype = newtype; | |
3292 this->arguments = arguments; | |
3293 member = NULL; | |
3294 allocator = NULL; | |
3295 onstack = 0; | |
3296 } | |
3297 | |
3298 Expression *NewExp::syntaxCopy() | |
3299 { | |
3300 return new NewExp(loc, | |
3301 thisexp ? thisexp->syntaxCopy() : NULL, | |
3302 arraySyntaxCopy(newargs), | |
3303 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3304 } | |
3305 | |
3306 | |
3307 Expression *NewExp::semantic(Scope *sc) | |
3308 { int i; | |
3309 Type *tb; | |
3310 ClassDeclaration *cdthis = NULL; | |
3311 | |
3312 #if LOGSEMANTIC | |
3313 printf("NewExp::semantic() %s\n", toChars()); | |
3314 if (thisexp) | |
3315 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3316 printf("\tnewtype: %s\n", newtype->toChars()); | |
3317 #endif | |
3318 if (type) // if semantic() already run | |
3319 return this; | |
3320 | |
3321 Lagain: | |
3322 if (thisexp) | |
3323 { thisexp = thisexp->semantic(sc); | |
3324 cdthis = thisexp->type->isClassHandle(); | |
3325 if (cdthis) | |
3326 { | |
3327 sc = sc->push(cdthis); | |
3328 type = newtype->semantic(loc, sc); | |
3329 sc = sc->pop(); | |
3330 } | |
3331 else | |
3332 { | |
3333 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3334 type = newtype->semantic(loc, sc); | |
3335 } | |
3336 } | |
3337 else | |
3338 type = newtype->semantic(loc, sc); | |
3339 newtype = type; // in case type gets cast to something else | |
3340 tb = type->toBasetype(); | |
3341 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3342 | |
3343 arrayExpressionSemantic(newargs, sc); | |
3344 preFunctionArguments(loc, sc, newargs); | |
3345 arrayExpressionSemantic(arguments, sc); | |
3346 preFunctionArguments(loc, sc, arguments); | |
3347 | |
3348 if (thisexp && tb->ty != Tclass) | |
3349 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3350 | |
3351 if (tb->ty == Tclass) | |
3352 { TypeFunction *tf; | |
3353 | |
3354 TypeClass *tc = (TypeClass *)(tb); | |
3355 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3356 if (cd->isInterfaceDeclaration()) | |
3357 error("cannot create instance of interface %s", cd->toChars()); | |
3358 else if (cd->isAbstract()) | |
3359 { error("cannot create instance of abstract class %s", cd->toChars()); | |
3360 for (int i = 0; i < cd->vtbl.dim; i++) | |
3361 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration(); | |
3362 if (fd && fd->isAbstract()) | |
3363 error("function %s is abstract", fd->toChars()); | |
3364 } | |
3365 } | |
3366 checkDeprecated(sc, cd); | |
3367 if (cd->isNested()) | |
3368 { /* We need a 'this' pointer for the nested class. | |
3369 * Ensure we have the right one. | |
3370 */ | |
3371 Dsymbol *s = cd->toParent2(); | |
3372 ClassDeclaration *cdn = s->isClassDeclaration(); | |
3373 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3374 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3375 if (cdn) |
3376 { | |
3377 if (!cdthis) | |
3378 { | |
3379 // Supply an implicit 'this' and try again | |
3380 thisexp = new ThisExp(loc); | |
3381 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3382 { if (!sp) | |
3383 { | |
3384 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3385 break; | |
3386 } | |
3387 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3388 if (!cdp) | |
3389 continue; | |
3390 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3391 break; | |
3392 // Add a '.outer' and try again | |
3393 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3394 } | |
3395 if (!global.errors) | |
3396 goto Lagain; | |
3397 } | |
3398 if (cdthis) | |
3399 { | |
3400 //printf("cdthis = %s\n", cdthis->toChars()); | |
3401 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3402 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3403 } | |
3404 #if 0 | |
3405 else | |
3406 { | |
3407 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3408 { | |
3409 if (!sf) | |
3410 { | |
3411 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3412 break; | |
3413 } | |
3414 printf("sf = %s\n", sf->toChars()); | |
3415 AggregateDeclaration *ad = sf->isThis(); | |
3416 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3417 break; | |
3418 } | |
3419 } | |
3420 #endif | |
3421 } | |
3422 else if (thisexp) | |
3423 error("e.new is only for allocating nested classes"); | |
3424 } | |
3425 else if (thisexp) | |
3426 error("e.new is only for allocating nested classes"); | |
3427 | |
3428 FuncDeclaration *f = cd->ctor; | |
3429 if (f) | |
3430 { | |
3431 assert(f); | |
3432 f = f->overloadResolve(loc, arguments); | |
3433 checkDeprecated(sc, f); | |
3434 member = f->isCtorDeclaration(); | |
3435 assert(member); | |
3436 | |
3437 cd->accessCheck(loc, sc, member); | |
3438 | |
3439 tf = (TypeFunction *)f->type; | |
3440 type = tf->next; | |
3441 | |
3442 if (!arguments) | |
3443 arguments = new Expressions(); | |
3444 functionArguments(loc, sc, tf, arguments); | |
3445 } | |
3446 else | |
3447 { | |
3448 if (arguments && arguments->dim) | |
3449 error("no constructor for %s", cd->toChars()); | |
3450 } | |
3451 | |
3452 if (cd->aggNew) | |
3453 { Expression *e; | |
3454 | |
3455 f = cd->aggNew; | |
3456 | |
3457 // Prepend the uint size argument to newargs[] | |
3458 e = new IntegerExp(loc, cd->size(loc), Type::tuns32); | |
3459 if (!newargs) | |
3460 newargs = new Expressions(); | |
3461 newargs->shift(e); | |
3462 | |
3463 f = f->overloadResolve(loc, newargs); | |
3464 allocator = f->isNewDeclaration(); | |
3465 assert(allocator); | |
3466 | |
3467 tf = (TypeFunction *)f->type; | |
3468 functionArguments(loc, sc, tf, newargs); | |
3469 } | |
3470 else | |
3471 { | |
3472 if (newargs && newargs->dim) | |
3473 error("no allocator for %s", cd->toChars()); | |
3474 } | |
3475 | |
3476 } | |
3477 else if (tb->ty == Tstruct) | |
3478 { | |
3479 TypeStruct *ts = (TypeStruct *)tb; | |
3480 StructDeclaration *sd = ts->sym; | |
3481 FuncDeclaration *f = sd->aggNew; | |
3482 TypeFunction *tf; | |
3483 | |
3484 if (arguments && arguments->dim) | |
3485 error("no constructor for %s", type->toChars()); | |
3486 | |
3487 if (f) | |
3488 { | |
3489 Expression *e; | |
3490 | |
3491 // Prepend the uint size argument to newargs[] | |
3492 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3493 if (!newargs) | |
3494 newargs = new Expressions(); | |
3495 newargs->shift(e); | |
3496 | |
3497 f = f->overloadResolve(loc, newargs); | |
3498 allocator = f->isNewDeclaration(); | |
3499 assert(allocator); | |
3500 | |
3501 tf = (TypeFunction *)f->type; | |
3502 functionArguments(loc, sc, tf, newargs); | |
3503 | |
3504 e = new VarExp(loc, f); | |
3505 e = new CallExp(loc, e, newargs); | |
3506 e = e->semantic(sc); | |
3507 e->type = type->pointerTo(); | |
3508 return e; | |
3509 } | |
3510 | |
3511 type = type->pointerTo(); | |
3512 } | |
3513 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3514 { | |
3515 for (size_t i = 0; i < arguments->dim; i++) | |
3516 { | |
3517 if (tb->ty != Tarray) | |
3518 { error("too many arguments for array"); | |
3519 arguments->dim = i; | |
3520 break; | |
3521 } | |
3522 | |
3523 Expression *arg = (Expression *)arguments->data[i]; | |
3524 arg = resolveProperties(sc, arg); | |
3525 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3526 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3527 error("negative array index %s", arg->toChars()); | |
3528 arguments->data[i] = (void *) arg; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3529 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3530 } |
3531 } | |
3532 else if (tb->isscalar()) | |
3533 { | |
3534 if (arguments && arguments->dim) | |
3535 error("no constructor for %s", type->toChars()); | |
3536 | |
3537 type = type->pointerTo(); | |
3538 } | |
3539 else | |
3540 { | |
3541 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3542 type = type->pointerTo(); | |
3543 } | |
3544 | |
3545 //printf("NewExp: '%s'\n", toChars()); | |
3546 //printf("NewExp:type '%s'\n", type->toChars()); | |
3547 | |
3548 return this; | |
3549 } | |
3550 | |
3551 int NewExp::checkSideEffect(int flag) | |
3552 { | |
3553 return 1; | |
3554 } | |
3555 | |
3556 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3557 { int i; | |
3558 | |
3559 if (thisexp) | |
3560 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3561 buf->writeByte('.'); | |
3562 } | |
3563 buf->writestring("new "); | |
3564 if (newargs && newargs->dim) | |
3565 { | |
3566 buf->writeByte('('); | |
3567 argsToCBuffer(buf, newargs, hgs); | |
3568 buf->writeByte(')'); | |
3569 } | |
3570 newtype->toCBuffer(buf, NULL, hgs); | |
3571 if (arguments && arguments->dim) | |
3572 { | |
3573 buf->writeByte('('); | |
3574 argsToCBuffer(buf, arguments, hgs); | |
3575 buf->writeByte(')'); | |
3576 } | |
3577 } | |
3578 | |
3579 /********************** NewAnonClassExp **************************************/ | |
3580 | |
3581 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3582 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3583 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3584 { | |
3585 this->thisexp = thisexp; | |
3586 this->newargs = newargs; | |
3587 this->cd = cd; | |
3588 this->arguments = arguments; | |
3589 } | |
3590 | |
3591 Expression *NewAnonClassExp::syntaxCopy() | |
3592 { | |
3593 return new NewAnonClassExp(loc, | |
3594 thisexp ? thisexp->syntaxCopy() : NULL, | |
3595 arraySyntaxCopy(newargs), | |
3596 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3597 arraySyntaxCopy(arguments)); | |
3598 } | |
3599 | |
3600 | |
3601 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3602 { | |
3603 #if LOGSEMANTIC | |
3604 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3605 //printf("thisexp = %p\n", thisexp); |
159 | 3606 //printf("type: %s\n", type->toChars()); |
3607 #endif | |
3608 | |
3609 Expression *d = new DeclarationExp(loc, cd); | |
3610 d = d->semantic(sc); | |
3611 | |
3612 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3613 | |
3614 Expression *c = new CommaExp(loc, d, n); | |
3615 return c->semantic(sc); | |
3616 } | |
3617 | |
3618 int NewAnonClassExp::checkSideEffect(int flag) | |
3619 { | |
3620 return 1; | |
3621 } | |
3622 | |
3623 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3624 { int i; | |
3625 | |
3626 if (thisexp) | |
3627 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3628 buf->writeByte('.'); | |
3629 } | |
3630 buf->writestring("new"); | |
3631 if (newargs && newargs->dim) | |
3632 { | |
3633 buf->writeByte('('); | |
3634 argsToCBuffer(buf, newargs, hgs); | |
3635 buf->writeByte(')'); | |
3636 } | |
3637 buf->writestring(" class "); | |
3638 if (arguments && arguments->dim) | |
3639 { | |
3640 buf->writeByte('('); | |
3641 argsToCBuffer(buf, arguments, hgs); | |
3642 buf->writeByte(')'); | |
3643 } | |
3644 //buf->writestring(" { }"); | |
3645 if (cd) | |
3646 { | |
3647 cd->toCBuffer(buf, hgs); | |
3648 } | |
3649 } | |
3650 | |
3651 /********************** SymOffExp **************************************/ | |
3652 | |
3653 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3654 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3655 { | |
3656 assert(var); | |
3657 this->var = var; | |
3658 this->offset = offset; | |
3659 VarDeclaration *v = var->isVarDeclaration(); | |
3660 if (v && v->needThis()) | |
3661 error("need 'this' for address of %s", v->toChars()); | |
3662 } | |
3663 | |
3664 Expression *SymOffExp::semantic(Scope *sc) | |
3665 { | |
3666 #if LOGSEMANTIC | |
3667 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3668 #endif | |
3669 //var->semantic(sc); | |
3670 if (!type) | |
3671 type = var->type->pointerTo(); | |
3672 VarDeclaration *v = var->isVarDeclaration(); | |
3673 if (v) | |
3674 { | |
3675 v->checkNestedReference(sc, loc); | |
3676 } | |
3677 return this; | |
3678 } | |
3679 | |
3680 int SymOffExp::isBool(int result) | |
3681 { | |
3682 return result ? TRUE : FALSE; | |
3683 } | |
3684 | |
3685 void SymOffExp::checkEscape() | |
3686 { | |
3687 VarDeclaration *v = var->isVarDeclaration(); | |
3688 if (v) | |
3689 { | |
3690 if (!v->isDataseg()) | |
3691 error("escaping reference to local variable %s", v->toChars()); | |
3692 } | |
3693 } | |
3694 | |
3695 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3696 { | |
3697 if (offset) | |
3698 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3699 else | |
3700 buf->printf("& %s", var->toChars()); | |
3701 } | |
3702 | |
3703 /******************************** VarExp **************************/ | |
3704 | |
3705 VarExp::VarExp(Loc loc, Declaration *var) | |
3706 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3707 { | |
3708 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3709 this->var = var; | |
3710 this->type = var->type; | |
3711 } | |
3712 | |
3713 int VarExp::equals(Object *o) | |
3714 { VarExp *ne; | |
3715 | |
3716 if (this == o || | |
3717 (((Expression *)o)->op == TOKvar && | |
3718 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3719 var == ne->var)) | |
3720 return 1; | |
3721 return 0; | |
3722 } | |
3723 | |
3724 Expression *VarExp::semantic(Scope *sc) | |
3725 { FuncLiteralDeclaration *fd; | |
3726 | |
3727 #if LOGSEMANTIC | |
3728 printf("VarExp::semantic(%s)\n", toChars()); | |
3729 #endif | |
3730 if (!type) | |
3731 { type = var->type; | |
3732 #if 0 | |
3733 if (var->storage_class & STClazy) | |
3734 { | |
3735 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3736 type = new TypeDelegate(tf); | |
3737 type = type->semantic(loc, sc); | |
3738 } | |
3739 #endif | |
3740 } | |
3741 | |
163
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
159
diff
changeset
|
3742 // LLVMDC: Fixes bug 1161, http://d.puremagic.com/issues/show_bug.cgi?id=1161 |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
159
diff
changeset
|
3743 // check access to VarDeclaration |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
159
diff
changeset
|
3744 accessCheck(loc, sc, NULL, var); |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
159
diff
changeset
|
3745 |
159 | 3746 VarDeclaration *v = var->isVarDeclaration(); |
3747 if (v) | |
3748 { | |
3749 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3750 { | |
3751 ExpInitializer *ei = v->init->isExpInitializer(); | |
3752 if (ei) | |
3753 { | |
3754 //ei->exp->implicitCastTo(sc, type)->print(); | |
3755 return ei->exp->implicitCastTo(sc, type); | |
3756 } | |
3757 } | |
3758 v->checkNestedReference(sc, loc); | |
3759 } | |
3760 #if 0 | |
3761 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3762 { Expression *e; | |
3763 e = new FuncExp(loc, fd); | |
3764 e->type = type; | |
3765 return e; | |
3766 } | |
3767 #endif | |
3768 return this; | |
3769 } | |
3770 | |
3771 char *VarExp::toChars() | |
3772 { | |
3773 return var->toChars(); | |
3774 } | |
3775 | |
3776 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3777 { | |
3778 buf->writestring(var->toChars()); | |
3779 } | |
3780 | |
3781 void VarExp::checkEscape() | |
3782 { | |
3783 VarDeclaration *v = var->isVarDeclaration(); | |
3784 if (v) | |
3785 { Type *tb = v->type->toBasetype(); | |
3786 // if reference type | |
3787 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
3788 { | |
3789 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
3790 error("escaping reference to auto local %s", v->toChars()); | |
3791 else if (v->storage_class & STCvariadic) | |
3792 error("escaping reference to variadic parameter %s", v->toChars()); | |
3793 } | |
3794 } | |
3795 } | |
3796 | |
3797 Expression *VarExp::toLvalue(Scope *sc, Expression *e) | |
3798 { | |
3799 #if 0 | |
3800 tym = tybasic(e1->ET->Tty); | |
3801 if (!(tyscalar(tym) || | |
3802 tym == TYstruct || | |
3803 tym == TYarray && e->Eoper == TOKaddr)) | |
3804 synerr(EM_lvalue); // lvalue expected | |
3805 #endif | |
3806 if (var->storage_class & STClazy) | |
3807 error("lazy variables cannot be lvalues"); | |
3808 return this; | |
3809 } | |
3810 | |
3811 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
3812 { | |
3813 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
3814 if (sc->incontract && var->isParameter()) | |
3815 error("cannot modify parameter '%s' in contract", var->toChars()); | |
3816 | |
3817 if (type && type->toBasetype()->ty == Tsarray) | |
3818 error("cannot change reference to static array '%s'", var->toChars()); | |
3819 | |
3820 VarDeclaration *v = var->isVarDeclaration(); | |
3821 if (v && v->canassign == 0 && | |
3822 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
3823 error("cannot modify final variable '%s'", var->toChars()); | |
3824 | |
3825 if (var->isCtorinit()) | |
3826 { // It's only modifiable if inside the right constructor | |
3827 Dsymbol *s = sc->func; | |
3828 while (1) | |
3829 { | |
3830 FuncDeclaration *fd = NULL; | |
3831 if (s) | |
3832 fd = s->isFuncDeclaration(); | |
3833 if (fd && | |
3834 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
3835 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
3836 fd->toParent() == var->toParent() | |
3837 ) | |
3838 { | |
3839 VarDeclaration *v = var->isVarDeclaration(); | |
3840 assert(v); | |
3841 v->ctorinit = 1; | |
3842 //printf("setting ctorinit\n"); | |
3843 } | |
3844 else | |
3845 { | |
3846 if (s) | |
3847 { s = s->toParent2(); | |
3848 continue; | |
3849 } | |
3850 else | |
3851 { | |
3852 const char *p = var->isStatic() ? "static " : ""; | |
3853 error("can only initialize %sconst %s inside %sconstructor", | |
3854 p, var->toChars(), p); | |
3855 } | |
3856 } | |
3857 break; | |
3858 } | |
3859 } | |
3860 | |
3861 // See if this expression is a modifiable lvalue (i.e. not const) | |
3862 return toLvalue(sc, e); | |
3863 } | |
3864 | |
3865 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3866 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3867 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3868 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3869 OverExp::OverExp(OverloadSet *s) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3870 : Expression(loc, TOKoverloadset, sizeof(OverExp)) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3871 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3872 //printf("OverExp(this = %p, '%s')\n", this, var->toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3873 vars = s; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3874 type = Type::tvoid; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3875 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3876 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3877 int OverExp::isLvalue() |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3878 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3879 return 1; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3880 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3881 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3882 Expression *OverExp::toLvalue(Scope *sc, Expression *e) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3883 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3884 return this; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3885 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3886 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3887 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3888 |
159 | 3889 /******************************** TupleExp **************************/ |
3890 | |
3891 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
3892 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
3893 { | |
3894 //printf("TupleExp(this = %p)\n", this); | |
3895 this->exps = exps; | |
3896 this->type = NULL; | |
3897 } | |
3898 | |
3899 | |
3900 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
3901 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
3902 { | |
3903 exps = new Expressions(); | |
3904 type = NULL; | |
3905 | |
3906 exps->reserve(tup->objects->dim); | |
3907 for (size_t i = 0; i < tup->objects->dim; i++) | |
3908 { Object *o = (Object *)tup->objects->data[i]; | |
3909 if (o->dyncast() == DYNCAST_EXPRESSION) | |
3910 { | |
3911 Expression *e = (Expression *)o; | |
3912 e = e->syntaxCopy(); | |
3913 exps->push(e); | |
3914 } | |
3915 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
3916 { | |
3917 Dsymbol *s = (Dsymbol *)o; | |
3918 Expression *e = new DsymbolExp(loc, s); | |
3919 exps->push(e); | |
3920 } | |
3921 else if (o->dyncast() == DYNCAST_TYPE) | |
3922 { | |
3923 Type *t = (Type *)o; | |
3924 Expression *e = new TypeExp(loc, t); | |
3925 exps->push(e); | |
3926 } | |
3927 else | |
3928 { | |
3929 error("%s is not an expression", o->toChars()); | |
3930 } | |
3931 } | |
3932 } | |
3933 | |
3934 int TupleExp::equals(Object *o) | |
3935 { TupleExp *ne; | |
3936 | |
3937 if (this == o) | |
3938 return 1; | |
3939 if (((Expression *)o)->op == TOKtuple) | |
3940 { | |
3941 TupleExp *te = (TupleExp *)o; | |
3942 if (exps->dim != te->exps->dim) | |
3943 return 0; | |
3944 for (size_t i = 0; i < exps->dim; i++) | |
3945 { Expression *e1 = (Expression *)exps->data[i]; | |
3946 Expression *e2 = (Expression *)te->exps->data[i]; | |
3947 | |
3948 if (!e1->equals(e2)) | |
3949 return 0; | |
3950 } | |
3951 return 1; | |
3952 } | |
3953 return 0; | |
3954 } | |
3955 | |
3956 Expression *TupleExp::syntaxCopy() | |
3957 { | |
3958 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
3959 } | |
3960 | |
3961 Expression *TupleExp::semantic(Scope *sc) | |
3962 { | |
3963 #if LOGSEMANTIC | |
3964 printf("+TupleExp::semantic(%s)\n", toChars()); | |
3965 #endif | |
3966 if (type) | |
3967 return this; | |
3968 | |
3969 // Run semantic() on each argument | |
3970 for (size_t i = 0; i < exps->dim; i++) | |
3971 { Expression *e = (Expression *)exps->data[i]; | |
3972 | |
3973 e = e->semantic(sc); | |
3974 if (!e->type) | |
3975 { error("%s has no value", e->toChars()); | |
3976 e->type = Type::terror; | |
3977 } | |
3978 exps->data[i] = (void *)e; | |
3979 } | |
3980 | |
3981 expandTuples(exps); | |
3982 if (0 && exps->dim == 1) | |
3983 { | |
3984 return (Expression *)exps->data[0]; | |
3985 } | |
3986 type = new TypeTuple(exps); | |
3987 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
3988 return this; | |
3989 } | |
3990 | |
3991 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3992 { | |
3993 buf->writestring("tuple("); | |
3994 argsToCBuffer(buf, exps, hgs); | |
3995 buf->writeByte(')'); | |
3996 } | |
3997 | |
3998 int TupleExp::checkSideEffect(int flag) | |
3999 { int f = 0; | |
4000 | |
4001 for (int i = 0; i < exps->dim; i++) | |
4002 { Expression *e = (Expression *)exps->data[i]; | |
4003 | |
4004 f |= e->checkSideEffect(2); | |
4005 } | |
4006 if (flag == 0 && f == 0) | |
4007 Expression::checkSideEffect(0); | |
4008 return f; | |
4009 } | |
4010 | |
4011 void TupleExp::checkEscape() | |
4012 { | |
4013 for (size_t i = 0; i < exps->dim; i++) | |
4014 { Expression *e = (Expression *)exps->data[i]; | |
4015 e->checkEscape(); | |
4016 } | |
4017 } | |
4018 | |
4019 /******************************** FuncExp *********************************/ | |
4020 | |
4021 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4022 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4023 { | |
4024 this->fd = fd; | |
4025 } | |
4026 | |
4027 Expression *FuncExp::syntaxCopy() | |
4028 { | |
4029 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4030 } | |
4031 | |
4032 Expression *FuncExp::semantic(Scope *sc) | |
4033 { | |
4034 #if LOGSEMANTIC | |
4035 printf("FuncExp::semantic(%s)\n", toChars()); | |
4036 #endif | |
4037 if (!type) | |
4038 { | |
4039 fd->semantic(sc); | |
4040 fd->parent = sc->parent; | |
4041 if (global.errors) | |
4042 { | |
4043 if (!fd->type->next) | |
4044 fd->type->next = Type::terror; | |
4045 } | |
4046 else | |
4047 { | |
4048 fd->semantic2(sc); | |
4049 if (!global.errors) | |
4050 { | |
4051 fd->semantic3(sc); | |
4052 | |
4053 if (!global.errors && global.params.useInline) | |
4054 fd->inlineScan(); | |
4055 } | |
4056 } | |
4057 | |
4058 // Type is a "delegate to" or "pointer to" the function literal | |
4059 if (fd->isNested()) | |
4060 { | |
4061 type = new TypeDelegate(fd->type); | |
4062 type = type->semantic(loc, sc); | |
4063 } | |
4064 else | |
4065 { | |
4066 type = fd->type->pointerTo(); | |
4067 } | |
4068 } | |
4069 return this; | |
4070 } | |
4071 | |
4072 char *FuncExp::toChars() | |
4073 { | |
4074 return fd->toChars(); | |
4075 } | |
4076 | |
4077 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4078 { | |
4079 buf->writestring(fd->toChars()); | |
4080 } | |
4081 | |
4082 | |
4083 /******************************** DeclarationExp **************************/ | |
4084 | |
4085 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4086 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4087 { | |
4088 this->declaration = declaration; | |
4089 } | |
4090 | |
4091 Expression *DeclarationExp::syntaxCopy() | |
4092 { | |
4093 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4094 } | |
4095 | |
4096 Expression *DeclarationExp::semantic(Scope *sc) | |
4097 { | |
4098 if (type) | |
4099 return this; | |
4100 | |
4101 #if LOGSEMANTIC | |
4102 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4103 #endif | |
4104 | |
4105 /* This is here to support extern(linkage) declaration, | |
4106 * where the extern(linkage) winds up being an AttribDeclaration | |
4107 * wrapper. | |
4108 */ | |
4109 Dsymbol *s = declaration; | |
4110 | |
4111 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4112 if (ad) | |
4113 { | |
4114 if (ad->decl && ad->decl->dim == 1) | |
4115 s = (Dsymbol *)ad->decl->data[0]; | |
4116 } | |
4117 | |
4118 if (s->isVarDeclaration()) | |
4119 { // Do semantic() on initializer first, so: | |
4120 // int a = a; | |
4121 // will be illegal. | |
4122 declaration->semantic(sc); | |
4123 s->parent = sc->parent; | |
4124 } | |
4125 | |
4126 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4127 // Insert into both local scope and function scope. | |
4128 // Must be unique in both. | |
4129 if (s->ident) | |
4130 { | |
4131 if (!sc->insert(s)) | |
4132 error("declaration %s is already defined", s->toPrettyChars()); | |
4133 else if (sc->func) | |
4134 { VarDeclaration *v = s->isVarDeclaration(); | |
4135 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4136 !sc->func->localsymtab->insert(s)) | |
4137 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4138 else if (!global.params.useDeprecated) | |
4139 { // Disallow shadowing | |
4140 | |
4141 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4142 { Dsymbol *s2; | |
4143 | |
4144 if (scx->scopesym && scx->scopesym->symtab && | |
4145 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4146 s != s2) | |
4147 { | |
4148 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4149 } | |
4150 } | |
4151 } | |
4152 } | |
4153 } | |
4154 if (!s->isVarDeclaration()) | |
4155 { | |
4156 declaration->semantic(sc); | |
4157 s->parent = sc->parent; | |
4158 } | |
4159 if (!global.errors) | |
4160 { | |
4161 declaration->semantic2(sc); | |
4162 if (!global.errors) | |
4163 { | |
4164 declaration->semantic3(sc); | |
4165 | |
4166 if (!global.errors && global.params.useInline) | |
4167 declaration->inlineScan(); | |
4168 } | |
4169 } | |
4170 | |
4171 type = Type::tvoid; | |
4172 return this; | |
4173 } | |
4174 | |
4175 int DeclarationExp::checkSideEffect(int flag) | |
4176 { | |
4177 return 1; | |
4178 } | |
4179 | |
4180 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4181 { | |
4182 declaration->toCBuffer(buf, hgs); | |
4183 } | |
4184 | |
4185 | |
4186 /************************ TypeidExp ************************************/ | |
4187 | |
4188 /* | |
4189 * typeid(int) | |
4190 */ | |
4191 | |
4192 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4193 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4194 { | |
4195 this->typeidType = typeidType; | |
4196 } | |
4197 | |
4198 | |
4199 Expression *TypeidExp::syntaxCopy() | |
4200 { | |
4201 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4202 } | |
4203 | |
4204 | |
4205 Expression *TypeidExp::semantic(Scope *sc) | |
4206 { Expression *e; | |
4207 | |
4208 #if LOGSEMANTIC | |
4209 printf("TypeidExp::semantic()\n"); | |
4210 #endif | |
4211 typeidType = typeidType->semantic(loc, sc); | |
4212 e = typeidType->getTypeInfo(sc); | |
336 | 4213 if (e->loc.linnum == 0) |
4214 e->loc = loc; // so there's at least some line number info | |
159 | 4215 return e; |
4216 } | |
4217 | |
4218 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4219 { | |
4220 buf->writestring("typeid("); | |
4221 typeidType->toCBuffer(buf, NULL, hgs); | |
4222 buf->writeByte(')'); | |
4223 } | |
4224 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4225 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4226 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4227 /* |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4228 * __traits(identifier, args...) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4229 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4230 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4231 TraitsExp::TraitsExp(Loc loc, Identifier *ident, Objects *args) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4232 : Expression(loc, TOKtraits, sizeof(TraitsExp)) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4233 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4234 this->ident = ident; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4235 this->args = args; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4236 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4237 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4238 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4239 Expression *TraitsExp::syntaxCopy() |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4240 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4241 return new TraitsExp(loc, ident, TemplateInstance::arraySyntaxCopy(args)); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4242 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4243 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4244 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4245 void TraitsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4246 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4247 buf->writestring("__traits("); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4248 buf->writestring(ident->toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4249 if (args) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4250 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4251 for (int i = 0; i < args->dim; i++) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4252 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4253 buf->writeByte(','); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4254 Object *oarg = (Object *)args->data[i]; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4255 ObjectToCBuffer(buf, hgs, oarg); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4256 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4257 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4258 buf->writeByte(')'); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4259 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4260 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4261 |
159 | 4262 /************************************************************/ |
4263 | |
4264 HaltExp::HaltExp(Loc loc) | |
4265 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4266 { | |
4267 } | |
4268 | |
4269 Expression *HaltExp::semantic(Scope *sc) | |
4270 { | |
4271 #if LOGSEMANTIC | |
4272 printf("HaltExp::semantic()\n"); | |
4273 #endif | |
4274 type = Type::tvoid; | |
4275 return this; | |
4276 } | |
4277 | |
4278 int HaltExp::checkSideEffect(int flag) | |
4279 { | |
4280 return 1; | |
4281 } | |
4282 | |
4283 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4284 { | |
4285 buf->writestring("halt"); | |
4286 } | |
4287 | |
4288 /************************************************************/ | |
4289 | |
4290 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4291 Type *tspec, enum TOK tok2) | |
4292 : Expression(loc, TOKis, sizeof(IsExp)) | |
4293 { | |
4294 this->targ = targ; | |
4295 this->id = id; | |
4296 this->tok = tok; | |
4297 this->tspec = tspec; | |
4298 this->tok2 = tok2; | |
4299 } | |
4300 | |
4301 Expression *IsExp::syntaxCopy() | |
4302 { | |
4303 return new IsExp(loc, | |
4304 targ->syntaxCopy(), | |
4305 id, | |
4306 tok, | |
4307 tspec ? tspec->syntaxCopy() : NULL, | |
4308 tok2); | |
4309 } | |
4310 | |
4311 Expression *IsExp::semantic(Scope *sc) | |
4312 { Type *tded; | |
4313 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4314 /* is(targ id tok tspec) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4315 * is(targ id == tok2) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4316 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4317 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4318 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4319 if (id && !(sc->flags & SCOPEstaticif)) |
4320 error("can only declare type aliases within static if conditionals"); | |
4321 | |
4322 unsigned errors_save = global.errors; | |
4323 global.errors = 0; | |
4324 global.gag++; // suppress printing of error messages | |
4325 targ = targ->semantic(loc, sc); | |
4326 global.gag--; | |
4327 unsigned gerrors = global.errors; | |
4328 global.errors = errors_save; | |
4329 | |
4330 if (gerrors) // if any errors happened | |
4331 { // then condition is false | |
4332 goto Lno; | |
4333 } | |
4334 else if (tok2 != TOKreserved) | |
4335 { | |
4336 switch (tok2) | |
4337 { | |
4338 case TOKtypedef: | |
4339 if (targ->ty != Ttypedef) | |
4340 goto Lno; | |
4341 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4342 break; | |
4343 | |
4344 case TOKstruct: | |
4345 if (targ->ty != Tstruct) | |
4346 goto Lno; | |
4347 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4348 goto Lno; | |
4349 tded = targ; | |
4350 break; | |
4351 | |
4352 case TOKunion: | |
4353 if (targ->ty != Tstruct) | |
4354 goto Lno; | |
4355 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4356 goto Lno; | |
4357 tded = targ; | |
4358 break; | |
4359 | |
4360 case TOKclass: | |
4361 if (targ->ty != Tclass) | |
4362 goto Lno; | |
4363 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4364 goto Lno; | |
4365 tded = targ; | |
4366 break; | |
4367 | |
4368 case TOKinterface: | |
4369 if (targ->ty != Tclass) | |
4370 goto Lno; | |
4371 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4372 goto Lno; | |
4373 tded = targ; | |
4374 break; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4375 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4376 case TOKconst: |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4377 if (!targ->isConst()) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4378 goto Lno; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4379 tded = targ; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4380 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4381 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4382 case TOKinvariant: |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4383 if (!targ->isInvariant()) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4384 goto Lno; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4385 tded = targ; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4386 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4387 #endif |
159 | 4388 |
4389 case TOKsuper: | |
4390 // If class or interface, get the base class and interfaces | |
4391 if (targ->ty != Tclass) | |
4392 goto Lno; | |
4393 else | |
4394 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4395 Arguments *args = new Arguments; | |
4396 args->reserve(cd->baseclasses.dim); | |
4397 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4398 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4399 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4400 } | |
4401 tded = new TypeTuple(args); | |
4402 } | |
4403 break; | |
4404 | |
4405 case TOKenum: | |
4406 if (targ->ty != Tenum) | |
4407 goto Lno; | |
4408 tded = ((TypeEnum *)targ)->sym->memtype; | |
4409 break; | |
4410 | |
4411 case TOKdelegate: | |
4412 if (targ->ty != Tdelegate) | |
4413 goto Lno; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4414 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4415 break; |
4416 | |
4417 case TOKfunction: | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4418 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4419 if (targ->ty != Tfunction) |
159 | 4420 goto Lno; |
4421 tded = targ; | |
4422 | |
4423 /* Generate tuple from function parameter types. | |
4424 */ | |
4425 assert(tded->ty == Tfunction); | |
4426 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4427 size_t dim = Argument::dim(params); | |
4428 Arguments *args = new Arguments; | |
4429 args->reserve(dim); | |
4430 for (size_t i = 0; i < dim; i++) | |
4431 { Argument *arg = Argument::getNth(params, i); | |
4432 assert(arg && arg->type); | |
4433 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4434 } | |
4435 tded = new TypeTuple(args); | |
4436 break; | |
4437 } | |
4438 case TOKreturn: | |
4439 /* Get the 'return type' for the function, | |
4440 * delegate, or pointer to function. | |
4441 */ | |
4442 if (targ->ty == Tfunction) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4443 tded = ((TypeFunction *)targ)->next; |
159 | 4444 else if (targ->ty == Tdelegate) |
4445 tded = targ->next->next; | |
4446 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4447 tded = targ->next->next; | |
4448 else | |
4449 goto Lno; | |
4450 break; | |
4451 | |
4452 default: | |
4453 assert(0); | |
4454 } | |
4455 goto Lyes; | |
4456 } | |
4457 else if (id && tspec) | |
4458 { | |
4459 /* Evaluate to TRUE if targ matches tspec. | |
4460 * If TRUE, declare id as an alias for the specialized type. | |
4461 */ | |
4462 | |
4463 MATCH m; | |
4464 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4465 | |
4466 TemplateParameters parameters; | |
4467 parameters.setDim(1); | |
4468 parameters.data[0] = (void *)&tp; | |
4469 | |
4470 Objects dedtypes; | |
4471 dedtypes.setDim(1); | |
4472 dedtypes.data[0] = NULL; | |
4473 | |
4474 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4475 if (m == MATCHnomatch || | |
4476 (m != MATCHexact && tok == TOKequal)) | |
4477 goto Lno; | |
4478 else | |
4479 { | |
4480 assert(dedtypes.dim == 1); | |
4481 tded = (Type *)dedtypes.data[0]; | |
4482 if (!tded) | |
4483 tded = targ; | |
4484 goto Lyes; | |
4485 } | |
4486 } | |
4487 else if (id) | |
4488 { | |
4489 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4490 */ | |
4491 tded = targ; | |
4492 goto Lyes; | |
4493 } | |
4494 else if (tspec) | |
4495 { | |
4496 /* Evaluate to TRUE if targ matches tspec | |
4497 */ | |
4498 tspec = tspec->semantic(loc, sc); | |
4499 //printf("targ = %s\n", targ->toChars()); | |
4500 //printf("tspec = %s\n", tspec->toChars()); | |
4501 if (tok == TOKcolon) | |
4502 { if (targ->implicitConvTo(tspec)) | |
4503 goto Lyes; | |
4504 else | |
4505 goto Lno; | |
4506 } | |
4507 else /* == */ | |
4508 { if (targ->equals(tspec)) | |
4509 goto Lyes; | |
4510 else | |
4511 goto Lno; | |
4512 } | |
4513 } | |
4514 | |
4515 Lyes: | |
4516 if (id) | |
4517 { | |
4518 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4519 s->semantic(sc); | |
4520 sc->insert(s); | |
4521 if (sc->sd) | |
4522 s->addMember(sc, sc->sd, 1); | |
4523 } | |
4524 return new IntegerExp(1); | |
4525 | |
4526 Lno: | |
4527 return new IntegerExp(0); | |
4528 } | |
4529 | |
4530 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4531 { | |
4532 buf->writestring("is("); | |
4533 targ->toCBuffer(buf, id, hgs); | |
4534 if (tok2 != TOKreserved) | |
4535 { | |
4536 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4537 } | |
4538 else if (tspec) | |
4539 { | |
4540 if (tok == TOKcolon) | |
4541 buf->writestring(" : "); | |
4542 else | |
4543 buf->writestring(" == "); | |
4544 tspec->toCBuffer(buf, NULL, hgs); | |
4545 } | |
336 | 4546 #if DMDV2 |
159 | 4547 if (parameters) |
4548 { // First parameter is already output, so start with second | |
4549 for (int i = 1; i < parameters->dim; i++) | |
4550 { | |
4551 buf->writeByte(','); | |
4552 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4553 tp->toCBuffer(buf, hgs); | |
4554 } | |
4555 } | |
4556 #endif | |
4557 buf->writeByte(')'); | |
4558 } | |
4559 | |
4560 | |
4561 /************************************************************/ | |
4562 | |
4563 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4564 : Expression(loc, op, size) | |
4565 { | |
4566 this->e1 = e1; | |
4567 } | |
4568 | |
4569 Expression *UnaExp::syntaxCopy() | |
4570 { UnaExp *e; | |
4571 | |
4572 e = (UnaExp *)copy(); | |
4573 e->type = NULL; | |
4574 e->e1 = e->e1->syntaxCopy(); | |
4575 return e; | |
4576 } | |
4577 | |
4578 Expression *UnaExp::semantic(Scope *sc) | |
4579 { | |
4580 #if LOGSEMANTIC | |
4581 printf("UnaExp::semantic('%s')\n", toChars()); | |
4582 #endif | |
4583 e1 = e1->semantic(sc); | |
4584 // if (!e1->type) | |
4585 // error("%s has no value", e1->toChars()); | |
4586 return this; | |
4587 } | |
4588 | |
4589 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4590 { | |
4591 buf->writestring(Token::toChars(op)); | |
4592 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4593 } | |
4594 | |
4595 /************************************************************/ | |
4596 | |
4597 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4598 : Expression(loc, op, size) | |
4599 { | |
4600 this->e1 = e1; | |
4601 this->e2 = e2; | |
4602 } | |
4603 | |
4604 Expression *BinExp::syntaxCopy() | |
4605 { BinExp *e; | |
4606 | |
4607 e = (BinExp *)copy(); | |
4608 e->type = NULL; | |
4609 e->e1 = e->e1->syntaxCopy(); | |
4610 e->e2 = e->e2->syntaxCopy(); | |
4611 return e; | |
4612 } | |
4613 | |
4614 Expression *BinExp::semantic(Scope *sc) | |
4615 { | |
4616 #if LOGSEMANTIC | |
4617 printf("BinExp::semantic('%s')\n", toChars()); | |
4618 #endif | |
4619 e1 = e1->semantic(sc); | |
4620 if (!e1->type) | |
4621 { | |
4622 error("%s has no value", e1->toChars()); | |
4623 e1->type = Type::terror; | |
4624 } | |
4625 e2 = e2->semantic(sc); | |
4626 if (!e2->type) | |
4627 { | |
4628 error("%s has no value", e2->toChars()); | |
4629 e2->type = Type::terror; | |
4630 } | |
4631 assert(e1->type); | |
4632 return this; | |
4633 } | |
4634 | |
4635 Expression *BinExp::semanticp(Scope *sc) | |
4636 { | |
4637 BinExp::semantic(sc); | |
4638 e1 = resolveProperties(sc, e1); | |
4639 e2 = resolveProperties(sc, e2); | |
4640 return this; | |
4641 } | |
4642 | |
4643 /*************************** | |
4644 * Common semantic routine for some xxxAssignExp's. | |
4645 */ | |
4646 | |
4647 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4648 { Expression *e; | |
4649 | |
4650 if (!type) | |
4651 { | |
4652 BinExp::semantic(sc); | |
4653 e2 = resolveProperties(sc, e2); | |
4654 | |
4655 e = op_overload(sc); | |
4656 if (e) | |
4657 return e; | |
4658 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4659 if (e1->op == TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4660 { // T[] op= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4661 typeCombine(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4662 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4663 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4664 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4665 |
159 | 4666 e1 = e1->modifiableLvalue(sc, e1); |
4667 e1->checkScalar(); | |
4668 type = e1->type; | |
4669 if (type->toBasetype()->ty == Tbool) | |
4670 { | |
4671 error("operator not allowed on bool expression %s", toChars()); | |
4672 } | |
4673 typeCombine(sc); | |
4674 e1->checkArithmetic(); | |
4675 e2->checkArithmetic(); | |
4676 | |
4677 if (op == TOKmodass && e2->type->iscomplex()) | |
4678 { error("cannot perform modulo complex arithmetic"); | |
4679 return new IntegerExp(0); | |
4680 } | |
4681 } | |
4682 return this; | |
4683 } | |
4684 | |
4685 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4686 { Expression *e; | |
4687 | |
4688 if (!type) | |
4689 { | |
4690 BinExp::semantic(sc); | |
4691 e2 = resolveProperties(sc, e2); | |
4692 | |
4693 e = op_overload(sc); | |
4694 if (e) | |
4695 return e; | |
4696 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4697 if (e1->op == TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4698 { // T[] op= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4699 typeCombine(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4700 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4701 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4702 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4703 |
159 | 4704 e1 = e1->modifiableLvalue(sc, e1); |
4705 e1->checkScalar(); | |
4706 type = e1->type; | |
4707 if (type->toBasetype()->ty == Tbool) | |
4708 { | |
4709 e2 = e2->implicitCastTo(sc, type); | |
4710 } | |
4711 | |
4712 typeCombine(sc); | |
4713 e1->checkIntegral(); | |
4714 e2->checkIntegral(); | |
4715 } | |
4716 return this; | |
4717 } | |
4718 | |
4719 int BinExp::checkSideEffect(int flag) | |
4720 { | |
4721 if (op == TOKplusplus || | |
4722 op == TOKminusminus || | |
4723 op == TOKassign || | |
4724 op == TOKconstruct || | |
4725 op == TOKblit || | |
4726 op == TOKaddass || | |
4727 op == TOKminass || | |
4728 op == TOKcatass || | |
4729 op == TOKmulass || | |
4730 op == TOKdivass || | |
4731 op == TOKmodass || | |
4732 op == TOKshlass || | |
4733 op == TOKshrass || | |
4734 op == TOKushrass || | |
4735 op == TOKandass || | |
4736 op == TOKorass || | |
4737 op == TOKxorass || | |
4738 op == TOKin || | |
4739 op == TOKremove) | |
4740 return 1; | |
4741 return Expression::checkSideEffect(flag); | |
4742 } | |
4743 | |
4744 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4745 { | |
4746 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4747 buf->writeByte(' '); | |
4748 buf->writestring(Token::toChars(op)); | |
4749 buf->writeByte(' '); | |
4750 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
4751 } | |
4752 | |
4753 int BinExp::isunsigned() | |
4754 { | |
4755 return e1->type->isunsigned() || e2->type->isunsigned(); | |
4756 } | |
4757 | |
4758 void BinExp::incompatibleTypes() | |
4759 { | |
4760 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
4761 e1->toChars(), Token::toChars(op), e2->toChars(), | |
4762 e1->type->toChars(), e2->type->toChars()); | |
4763 } | |
4764 | |
4765 /************************************************************/ | |
4766 | |
4767 CompileExp::CompileExp(Loc loc, Expression *e) | |
4768 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
4769 { | |
4770 } | |
4771 | |
4772 Expression *CompileExp::semantic(Scope *sc) | |
4773 { | |
4774 #if LOGSEMANTIC | |
4775 printf("CompileExp::semantic('%s')\n", toChars()); | |
4776 #endif | |
4777 UnaExp::semantic(sc); | |
4778 e1 = resolveProperties(sc, e1); | |
4779 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
4780 if (e1->op != TOKstring) | |
4781 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
4782 type = Type::terror; | |
4783 return this; | |
4784 } | |
4785 StringExp *se = (StringExp *)e1; | |
4786 se = se->toUTF8(sc); | |
4787 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
4788 p.loc = loc; | |
4789 p.nextToken(); | |
4790 Expression *e = p.parseExpression(); | |
4791 if (p.token.value != TOKeof) | |
4792 error("incomplete mixin expression (%s)", se->toChars()); | |
4793 return e->semantic(sc); | |
4794 } | |
4795 | |
4796 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4797 { | |
4798 buf->writestring("mixin("); | |
4799 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4800 buf->writeByte(')'); | |
4801 } | |
4802 | |
4803 /************************************************************/ | |
4804 | |
4805 FileExp::FileExp(Loc loc, Expression *e) | |
4806 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
4807 { | |
4808 } | |
4809 | |
4810 Expression *FileExp::semantic(Scope *sc) | |
4811 { char *name; | |
4812 StringExp *se; | |
4813 | |
4814 #if LOGSEMANTIC | |
4815 printf("FileExp::semantic('%s')\n", toChars()); | |
4816 #endif | |
4817 UnaExp::semantic(sc); | |
4818 e1 = resolveProperties(sc, e1); | |
4819 e1 = e1->optimize(WANTvalue); | |
4820 if (e1->op != TOKstring) | |
4821 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
4822 goto Lerror; | |
4823 } | |
4824 se = (StringExp *)e1; | |
4825 se = se->toUTF8(sc); | |
4826 name = (char *)se->string; | |
4827 | |
4828 if (!global.params.fileImppath) | |
4829 { error("need -Jpath switch to import text file %s", name); | |
4830 goto Lerror; | |
4831 } | |
4832 | |
4833 if (name != FileName::name(name)) | |
4834 { error("use -Jpath switch to provide path for filename %s", name); | |
4835 goto Lerror; | |
4836 } | |
4837 | |
4838 name = FileName::searchPath(global.filePath, name, 0); | |
4839 if (!name) | |
4840 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
4841 goto Lerror; | |
4842 } | |
4843 | |
4844 if (global.params.verbose) | |
4845 printf("file %s\t(%s)\n", se->string, name); | |
4846 | |
4847 { File f(name); | |
4848 if (f.read()) | |
4849 { error("cannot read file %s", f.toChars()); | |
4850 goto Lerror; | |
4851 } | |
4852 else | |
4853 { | |
4854 f.ref = 1; | |
4855 se = new StringExp(loc, f.buffer, f.len); | |
4856 } | |
4857 } | |
4858 Lret: | |
4859 return se->semantic(sc); | |
4860 | |
4861 Lerror: | |
4862 se = new StringExp(loc, ""); | |
4863 goto Lret; | |
4864 } | |
4865 | |
4866 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4867 { | |
4868 buf->writestring("import("); | |
4869 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4870 buf->writeByte(')'); | |
4871 } | |
4872 | |
4873 /************************************************************/ | |
4874 | |
4875 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
4876 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
4877 { | |
4878 this->msg = msg; | |
4879 } | |
4880 | |
4881 Expression *AssertExp::syntaxCopy() | |
4882 { | |
4883 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
4884 msg ? msg->syntaxCopy() : NULL); | |
4885 return ae; | |
4886 } | |
4887 | |
4888 Expression *AssertExp::semantic(Scope *sc) | |
4889 { | |
4890 #if LOGSEMANTIC | |
4891 printf("AssertExp::semantic('%s')\n", toChars()); | |
4892 #endif | |
4893 UnaExp::semantic(sc); | |
4894 e1 = resolveProperties(sc, e1); | |
4895 // BUG: see if we can do compile time elimination of the Assert | |
4896 e1 = e1->optimize(WANTvalue); | |
4897 e1 = e1->checkToBoolean(); | |
4898 if (msg) | |
4899 { | |
4900 msg = msg->semantic(sc); | |
4901 msg = resolveProperties(sc, msg); | |
4902 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
4903 msg = msg->optimize(WANTvalue); | |
4904 } | |
4905 if (e1->isBool(FALSE)) | |
4906 { | |
4907 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
4908 fd->hasReturnExp |= 4; | |
4909 | |
4910 if (!global.params.useAssert) | |
4911 { Expression *e = new HaltExp(loc); | |
4912 e = e->semantic(sc); | |
4913 return e; | |
4914 } | |
4915 } | |
4916 type = Type::tvoid; | |
4917 return this; | |
4918 } | |
4919 | |
4920 int AssertExp::checkSideEffect(int flag) | |
4921 { | |
4922 return 1; | |
4923 } | |
4924 | |
4925 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4926 { | |
4927 buf->writestring("assert("); | |
4928 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4929 if (msg) | |
4930 { | |
4931 buf->writeByte(','); | |
4932 expToCBuffer(buf, hgs, msg, PREC_assign); | |
4933 } | |
4934 buf->writeByte(')'); | |
4935 } | |
4936 | |
4937 /************************************************************/ | |
4938 | |
4939 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
4940 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
4941 { | |
4942 this->ident = ident; | |
4943 } | |
4944 | |
4945 Expression *DotIdExp::semantic(Scope *sc) | |
4946 { Expression *e; | |
4947 Expression *eleft; | |
4948 Expression *eright; | |
4949 | |
4950 #if LOGSEMANTIC | |
4951 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
4952 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
4953 #endif | |
4954 | |
4955 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
4956 | |
4957 #if 0 | |
4958 /* Don't do semantic analysis if we'll be converting | |
4959 * it to a string. | |
4960 */ | |
4961 if (ident == Id::stringof) | |
4962 { char *s = e1->toChars(); | |
4963 e = new StringExp(loc, s, strlen(s), 'c'); | |
4964 e = e->semantic(sc); | |
4965 return e; | |
4966 } | |
4967 #endif | |
4968 | |
4969 /* Special case: rewrite this.id and super.id | |
4970 * to be classtype.id and baseclasstype.id | |
4971 * if we have no this pointer. | |
4972 */ | |
4973 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
4974 { ClassDeclaration *cd; | |
4975 StructDeclaration *sd; | |
4976 AggregateDeclaration *ad; | |
4977 | |
4978 ad = sc->getStructClassScope(); | |
4979 if (ad) | |
4980 { | |
4981 cd = ad->isClassDeclaration(); | |
4982 if (cd) | |
4983 { | |
4984 if (e1->op == TOKthis) | |
4985 { | |
4986 e = new TypeDotIdExp(loc, cd->type, ident); | |
4987 return e->semantic(sc); | |
4988 } | |
4989 else if (cd->baseClass && e1->op == TOKsuper) | |
4990 { | |
4991 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
4992 return e->semantic(sc); | |
4993 } | |
4994 } | |
4995 else | |
4996 { | |
4997 sd = ad->isStructDeclaration(); | |
4998 if (sd) | |
4999 { | |
5000 if (e1->op == TOKthis) | |
5001 { | |
5002 e = new TypeDotIdExp(loc, sd->type, ident); | |
5003 return e->semantic(sc); | |
5004 } | |
5005 } | |
5006 } | |
5007 } | |
5008 } | |
5009 | |
5010 UnaExp::semantic(sc); | |
5011 | |
5012 if (e1->op == TOKdotexp) | |
5013 { | |
5014 DotExp *de = (DotExp *)e1; | |
5015 eleft = de->e1; | |
5016 eright = de->e2; | |
5017 } | |
5018 else | |
5019 { | |
5020 e1 = resolveProperties(sc, e1); | |
5021 eleft = NULL; | |
5022 eright = e1; | |
5023 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5024 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5025 if (e1->op == TOKtuple && ident == Id::offsetof) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5026 { /* 'distribute' the .offsetof to each of the tuple elements. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5027 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5028 TupleExp *te = (TupleExp *)e1; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5029 Expressions *exps = new Expressions(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5030 exps->setDim(te->exps->dim); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5031 for (int i = 0; i < exps->dim; i++) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5032 { Expression *e = (Expression *)te->exps->data[i]; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5033 e = e->semantic(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5034 e = new DotIdExp(e->loc, e, Id::offsetof); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5035 exps->data[i] = (void *)e; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5036 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5037 e = new TupleExp(loc, exps); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5038 e = e->semantic(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5039 return e; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5040 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5041 #endif |
159 | 5042 |
5043 if (e1->op == TOKtuple && ident == Id::length) | |
5044 { | |
5045 TupleExp *te = (TupleExp *)e1; | |
5046 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5047 return e; | |
5048 } | |
5049 | |
5050 if (eright->op == TOKimport) // also used for template alias's | |
5051 { | |
5052 Dsymbol *s; | |
5053 ScopeExp *ie = (ScopeExp *)eright; | |
5054 | |
5055 s = ie->sds->search(loc, ident, 0); | |
5056 if (s) | |
5057 { | |
5058 s = s->toAlias(); | |
5059 checkDeprecated(sc, s); | |
5060 | |
5061 EnumMember *em = s->isEnumMember(); | |
5062 if (em) | |
5063 { | |
5064 e = em->value; | |
5065 e = e->semantic(sc); | |
5066 return e; | |
5067 } | |
5068 | |
5069 VarDeclaration *v = s->isVarDeclaration(); | |
5070 if (v) | |
5071 { | |
5072 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5073 if (v->inuse) | |
5074 { | |
5075 error("circular reference to '%s'", v->toChars()); | |
5076 type = Type::tint32; | |
5077 return this; | |
5078 } | |
5079 type = v->type; | |
5080 if (v->isConst()) | |
5081 { | |
5082 if (v->init) | |
5083 { | |
5084 ExpInitializer *ei = v->init->isExpInitializer(); | |
5085 if (ei) | |
5086 { | |
5087 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5088 //ei->exp = ei->exp->semantic(sc); | |
5089 if (ei->exp->type == type) | |
5090 { | |
5091 e = ei->exp->copy(); // make copy so we can change loc | |
5092 e->loc = loc; | |
5093 return e; | |
5094 } | |
5095 } | |
5096 } | |
5097 else if (type->isscalar()) | |
5098 { | |
5099 e = type->defaultInit(); | |
5100 e->loc = loc; | |
5101 return e; | |
5102 } | |
5103 } | |
5104 if (v->needThis()) | |
5105 { | |
5106 if (!eleft) | |
5107 eleft = new ThisExp(loc); | |
5108 e = new DotVarExp(loc, eleft, v); | |
5109 e = e->semantic(sc); | |
5110 } | |
5111 else | |
5112 { | |
5113 e = new VarExp(loc, v); | |
5114 if (eleft) | |
5115 { e = new CommaExp(loc, eleft, e); | |
5116 e->type = v->type; | |
5117 } | |
5118 } | |
5119 return e->deref(); | |
5120 } | |
5121 | |
5122 FuncDeclaration *f = s->isFuncDeclaration(); | |
5123 if (f) | |
5124 { | |
5125 //printf("it's a function\n"); | |
5126 if (f->needThis()) | |
5127 { | |
5128 if (!eleft) | |
5129 eleft = new ThisExp(loc); | |
5130 e = new DotVarExp(loc, eleft, f); | |
5131 e = e->semantic(sc); | |
5132 } | |
5133 else | |
5134 { | |
5135 e = new VarExp(loc, f); | |
5136 if (eleft) | |
5137 { e = new CommaExp(loc, eleft, e); | |
5138 e->type = f->type; | |
5139 } | |
5140 } | |
5141 return e; | |
5142 } | |
5143 | |
5144 Type *t = s->getType(); | |
5145 if (t) | |
5146 { | |
5147 return new TypeExp(loc, t); | |
5148 } | |
5149 | |
336 | 5150 TupleDeclaration *tup = s->isTupleDeclaration(); |
5151 if (tup) | |
5152 { | |
5153 if (eleft) | |
5154 error("cannot have e.tuple"); | |
5155 e = new TupleExp(loc, tup); | |
5156 e = e->semantic(sc); | |
5157 return e; | |
5158 } | |
5159 | |
159 | 5160 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5161 if (sds) | |
5162 { | |
5163 //printf("it's a ScopeDsymbol\n"); | |
5164 e = new ScopeExp(loc, sds); | |
5165 e = e->semantic(sc); | |
5166 if (eleft) | |
5167 e = new DotExp(loc, eleft, e); | |
5168 return e; | |
5169 } | |
5170 | |
5171 Import *imp = s->isImport(); | |
5172 if (imp) | |
5173 { | |
5174 ScopeExp *ie; | |
5175 | |
5176 ie = new ScopeExp(loc, imp->pkg); | |
5177 return ie->semantic(sc); | |
5178 } | |
5179 | |
5180 // BUG: handle other cases like in IdentifierExp::semantic() | |
5181 #ifdef DEBUG | |
5182 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5183 #endif | |
5184 assert(0); | |
5185 } | |
5186 else if (ident == Id::stringof) | |
5187 { char *s = ie->toChars(); | |
5188 e = new StringExp(loc, s, strlen(s), 'c'); | |
5189 e = e->semantic(sc); | |
5190 return e; | |
5191 } | |
5192 error("undefined identifier %s", toChars()); | |
5193 type = Type::tvoid; | |
5194 return this; | |
5195 } | |
5196 else if (e1->type->ty == Tpointer && | |
5197 ident != Id::init && ident != Id::__sizeof && | |
5198 ident != Id::alignof && ident != Id::offsetof && | |
5199 ident != Id::mangleof && ident != Id::stringof) | |
5200 { | |
5201 e = new PtrExp(loc, e1); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5202 e->type = ((TypePointer *)e1->type)->next; |
159 | 5203 return e->type->dotExp(sc, e, ident); |
5204 } | |
5205 else | |
5206 { | |
5207 e = e1->type->dotExp(sc, e1, ident); | |
5208 e = e->semantic(sc); | |
5209 return e; | |
5210 } | |
5211 } | |
5212 | |
5213 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5214 { | |
5215 //printf("DotIdExp::toCBuffer()\n"); | |
5216 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5217 buf->writeByte('.'); | |
5218 buf->writestring(ident->toChars()); | |
5219 } | |
5220 | |
5221 /********************** DotTemplateExp ***********************************/ | |
5222 | |
5223 // Mainly just a placeholder | |
5224 | |
5225 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5226 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5227 | |
5228 { | |
5229 this->td = td; | |
5230 } | |
5231 | |
5232 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5233 { | |
5234 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5235 buf->writeByte('.'); | |
5236 buf->writestring(td->toChars()); | |
5237 } | |
5238 | |
5239 | |
5240 /************************************************************/ | |
5241 | |
5242 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5243 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5244 { | |
5245 //printf("DotVarExp()\n"); | |
5246 this->var = v; | |
5247 } | |
5248 | |
5249 Expression *DotVarExp::semantic(Scope *sc) | |
5250 { | |
5251 #if LOGSEMANTIC | |
5252 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5253 #endif | |
5254 if (!type) | |
5255 { | |
5256 var = var->toAlias()->isDeclaration(); | |
5257 | |
5258 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5259 if (tup) | |
5260 { /* Replace: | |
5261 * e1.tuple(a, b, c) | |
5262 * with: | |
5263 * tuple(e1.a, e1.b, e1.c) | |
5264 */ | |
5265 Expressions *exps = new Expressions; | |
5266 | |
5267 exps->reserve(tup->objects->dim); | |
5268 for (size_t i = 0; i < tup->objects->dim; i++) | |
5269 { Object *o = (Object *)tup->objects->data[i]; | |
5270 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5271 { | |
5272 error("%s is not an expression", o->toChars()); | |
5273 } | |
5274 else | |
5275 { | |
5276 Expression *e = (Expression *)o; | |
5277 if (e->op != TOKdsymbol) | |
5278 error("%s is not a member", e->toChars()); | |
5279 else | |
5280 { DsymbolExp *ve = (DsymbolExp *)e; | |
5281 | |
5282 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5283 exps->push(e); | |
5284 } | |
5285 } | |
5286 } | |
5287 Expression *e = new TupleExp(loc, exps); | |
5288 e = e->semantic(sc); | |
5289 return e; | |
5290 } | |
5291 | |
5292 e1 = e1->semantic(sc); | |
5293 type = var->type; | |
5294 if (!type && global.errors) | |
5295 { // var is goofed up, just return 0 | |
5296 return new IntegerExp(0); | |
5297 } | |
5298 assert(type); | |
5299 | |
5300 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5301 { | |
5302 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
336 | 5303 e1 = getRightThis(loc, sc, ad, e1, var); |
5304 if (!sc->noaccesscheck) | |
5305 accessCheck(loc, sc, e1, var); | |
5306 | |
5307 VarDeclaration *v = var->isVarDeclaration(); | |
5308 if (v && v->isConst()) | |
5309 { ExpInitializer *ei = v->getExpInitializer(); | |
5310 if (ei) | |
5311 { Expression *e = ei->exp->copy(); | |
5312 e = e->semantic(sc); | |
5313 return e; | |
159 | 5314 } |
5315 } | |
5316 } | |
5317 } | |
5318 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5319 return this; | |
5320 } | |
5321 | |
5322 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) | |
5323 { | |
5324 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5325 return this; | |
5326 } | |
5327 | |
5328 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5329 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5330 #if 0 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5331 printf("DotVarExp::modifiableLvalue(%s)\n", toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5332 printf("e1->type = %s\n", e1->type->toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5333 printf("var->type = %s\n", var->type->toChars()); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5334 #endif |
159 | 5335 |
5336 if (var->isCtorinit()) | |
5337 { // It's only modifiable if inside the right constructor | |
5338 Dsymbol *s = sc->func; | |
5339 while (1) | |
5340 { | |
5341 FuncDeclaration *fd = NULL; | |
5342 if (s) | |
5343 fd = s->isFuncDeclaration(); | |
5344 if (fd && | |
5345 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5346 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5347 fd->toParent() == var->toParent() && | |
5348 e1->op == TOKthis | |
5349 ) | |
5350 { | |
5351 VarDeclaration *v = var->isVarDeclaration(); | |
5352 assert(v); | |
5353 v->ctorinit = 1; | |
5354 //printf("setting ctorinit\n"); | |
5355 } | |
5356 else | |
5357 { | |
5358 if (s) | |
5359 { s = s->toParent2(); | |
5360 continue; | |
5361 } | |
5362 else | |
5363 { | |
5364 const char *p = var->isStatic() ? "static " : ""; | |
5365 error("can only initialize %sconst member %s inside %sconstructor", | |
5366 p, var->toChars(), p); | |
5367 } | |
5368 } | |
5369 break; | |
5370 } | |
5371 } | |
5372 return this; | |
5373 } | |
5374 | |
5375 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5376 { | |
5377 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5378 buf->writeByte('.'); | |
5379 buf->writestring(var->toChars()); | |
5380 } | |
5381 | |
5382 /************************************************************/ | |
5383 | |
5384 /* Things like: | |
5385 * foo.bar!(args) | |
5386 */ | |
5387 | |
5388 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5389 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5390 { | |
5391 //printf("DotTemplateInstanceExp()\n"); | |
5392 this->ti = ti; | |
5393 } | |
5394 | |
5395 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5396 { | |
5397 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5398 e1->syntaxCopy(), | |
5399 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5400 return de; | |
5401 } | |
5402 | |
5403 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5404 { Dsymbol *s; | |
5405 Dsymbol *s2; | |
5406 TemplateDeclaration *td; | |
5407 Expression *e; | |
5408 Identifier *id; | |
5409 Type *t1; | |
5410 Expression *eleft = NULL; | |
5411 Expression *eright; | |
5412 | |
5413 #if LOGSEMANTIC | |
5414 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5415 #endif | |
5416 //e1->print(); | |
5417 //print(); | |
5418 e1 = e1->semantic(sc); | |
5419 t1 = e1->type; | |
5420 if (t1) | |
5421 t1 = t1->toBasetype(); | |
5422 //t1->print(); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5423 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5424 /* Extract the following from e1: |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5425 * s: the symbol which ti should be a member of |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5426 * eleft: if not NULL, it is the 'this' pointer for ti |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5427 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5428 |
159 | 5429 if (e1->op == TOKdotexp) |
5430 { DotExp *de = (DotExp *)e1; | |
5431 eleft = de->e1; | |
5432 eright = de->e2; | |
5433 } | |
5434 else | |
5435 { eleft = NULL; | |
5436 eright = e1; | |
5437 } | |
5438 if (eright->op == TOKimport) | |
5439 { | |
5440 s = ((ScopeExp *)eright)->sds; | |
5441 } | |
5442 else if (e1->op == TOKtype) | |
5443 { | |
5444 s = t1->isClassHandle(); | |
5445 if (!s) | |
5446 { if (t1->ty == Tstruct) | |
5447 s = ((TypeStruct *)t1)->sym; | |
5448 else | |
5449 goto L1; | |
5450 } | |
5451 } | |
5452 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5453 { | |
5454 s = t1->toDsymbol(sc); | |
5455 eleft = e1; | |
5456 } | |
5457 else if (t1 && t1->ty == Tpointer) | |
5458 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5459 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 5460 if (t1->ty != Tstruct) |
5461 goto L1; | |
5462 s = t1->toDsymbol(sc); | |
5463 eleft = e1; | |
5464 } | |
5465 else | |
5466 { | |
5467 L1: | |
5468 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5469 goto Lerr; | |
5470 } | |
5471 | |
5472 assert(s); | |
5473 id = ti->name; | |
5474 s2 = s->search(loc, id, 0); | |
5475 if (!s2) | |
5476 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5477 goto Lerr; | |
5478 } | |
5479 s = s2; | |
5480 s->semantic(sc); | |
5481 s = s->toAlias(); | |
5482 td = s->isTemplateDeclaration(); | |
5483 if (!td) | |
5484 { | |
5485 error("%s is not a template", id->toChars()); | |
5486 goto Lerr; | |
5487 } | |
5488 if (global.errors) | |
5489 goto Lerr; | |
5490 | |
5491 ti->tempdecl = td; | |
5492 | |
5493 if (eleft) | |
5494 { Declaration *v; | |
5495 | |
5496 ti->semantic(sc); | |
5497 s = ti->inst->toAlias(); | |
5498 v = s->isDeclaration(); | |
5499 if (v) | |
5500 { e = new DotVarExp(loc, eleft, v); | |
5501 e = e->semantic(sc); | |
5502 return e; | |
5503 } | |
5504 } | |
5505 | |
5506 e = new ScopeExp(loc, ti); | |
5507 if (eleft) | |
5508 { | |
5509 e = new DotExp(loc, eleft, e); | |
5510 } | |
5511 e = e->semantic(sc); | |
5512 return e; | |
5513 | |
5514 Lerr: | |
336 | 5515 return new IntegerExp(loc, 0, Type::tint32); |
159 | 5516 } |
5517 | |
5518 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5519 { | |
5520 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5521 buf->writeByte('.'); | |
5522 ti->toCBuffer(buf, hgs); | |
5523 } | |
5524 | |
5525 /************************************************************/ | |
5526 | |
5527 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5528 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5529 { | |
5530 this->func = f; | |
5531 } | |
5532 | |
5533 Expression *DelegateExp::semantic(Scope *sc) | |
5534 { | |
5535 #if LOGSEMANTIC | |
5536 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5537 #endif | |
5538 if (!type) | |
5539 { | |
5540 e1 = e1->semantic(sc); | |
471
e19e20103119
Changed DMD to make a copy of the TypeFunction in DelegateExp.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
5541 // LLVMDC we need a copy as we store the LLVM tpye in TypeFunction, and delegate/members have different types for 'this' |
e19e20103119
Changed DMD to make a copy of the TypeFunction in DelegateExp.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
468
diff
changeset
|
5542 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 5543 type = type->semantic(loc, sc); |
5544 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 5545 if (func->needThis()) |
5546 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 5547 } |
5548 return this; | |
5549 } | |
5550 | |
5551 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5552 { | |
5553 buf->writeByte('&'); | |
5554 if (!func->isNested()) | |
5555 { | |
5556 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5557 buf->writeByte('.'); | |
5558 } | |
5559 buf->writestring(func->toChars()); | |
5560 } | |
5561 | |
5562 /************************************************************/ | |
5563 | |
5564 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5565 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5566 { | |
5567 this->sym = s; | |
5568 this->type = s->getType(); | |
5569 } | |
5570 | |
5571 Expression *DotTypeExp::semantic(Scope *sc) | |
5572 { | |
5573 #if LOGSEMANTIC | |
5574 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5575 #endif | |
5576 UnaExp::semantic(sc); | |
5577 return this; | |
5578 } | |
5579 | |
5580 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5581 { | |
5582 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5583 buf->writeByte('.'); | |
5584 buf->writestring(sym->toChars()); | |
5585 } | |
5586 | |
5587 /************************************************************/ | |
5588 | |
5589 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5590 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5591 { | |
5592 this->arguments = exps; | |
5593 } | |
5594 | |
5595 CallExp::CallExp(Loc loc, Expression *e) | |
5596 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5597 { | |
5598 this->arguments = NULL; | |
5599 } | |
5600 | |
5601 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5602 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5603 { | |
5604 Expressions *arguments = new Expressions(); | |
5605 arguments->setDim(1); | |
5606 arguments->data[0] = (void *)earg1; | |
5607 | |
5608 this->arguments = arguments; | |
5609 } | |
5610 | |
5611 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5612 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5613 { | |
5614 Expressions *arguments = new Expressions(); | |
5615 arguments->setDim(2); | |
5616 arguments->data[0] = (void *)earg1; | |
5617 arguments->data[1] = (void *)earg2; | |
5618 | |
5619 this->arguments = arguments; | |
5620 } | |
5621 | |
5622 Expression *CallExp::syntaxCopy() | |
5623 { | |
5624 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5625 } | |
5626 | |
5627 | |
5628 Expression *CallExp::semantic(Scope *sc) | |
5629 { | |
5630 TypeFunction *tf; | |
5631 FuncDeclaration *f; | |
5632 int i; | |
5633 Type *t1; | |
5634 int istemp; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5635 Objects *targsi = NULL; // initial list of template arguments |
159 | 5636 |
5637 #if LOGSEMANTIC | |
5638 printf("CallExp::semantic() %s\n", toChars()); | |
5639 #endif | |
5640 if (type) | |
5641 return this; // semantic() already run | |
5642 #if 0 | |
5643 if (arguments && arguments->dim) | |
5644 { | |
5645 Expression *earg = (Expression *)arguments->data[0]; | |
5646 earg->print(); | |
5647 if (earg->type) earg->type->print(); | |
5648 } | |
5649 #endif | |
5650 | |
5651 if (e1->op == TOKdelegate) | |
5652 { DelegateExp *de = (DelegateExp *)e1; | |
5653 | |
5654 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5655 return semantic(sc); | |
5656 } | |
5657 | |
5658 /* Transform: | |
5659 * array.id(args) into id(array,args) | |
5660 * aa.remove(arg) into delete aa[arg] | |
5661 */ | |
5662 if (e1->op == TOKdot) | |
5663 { | |
5664 // BUG: we should handle array.a.b.c.e(args) too | |
5665 | |
5666 DotIdExp *dotid = (DotIdExp *)(e1); | |
5667 dotid->e1 = dotid->e1->semantic(sc); | |
5668 assert(dotid->e1); | |
5669 if (dotid->e1->type) | |
5670 { | |
5671 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
5672 if (e1ty == Taarray && dotid->ident == Id::remove) | |
5673 { | |
5674 if (!arguments || arguments->dim != 1) | |
5675 { error("expected key as argument to aa.remove()"); | |
5676 goto Lagain; | |
5677 } | |
5678 Expression *key = (Expression *)arguments->data[0]; | |
5679 key = key->semantic(sc); | |
5680 key = resolveProperties(sc, key); | |
5681 key->rvalue(); | |
5682 | |
5683 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
5684 key = key->implicitCastTo(sc, taa->index); | |
5685 key = key->implicitCastTo(sc, taa->key); | |
5686 | |
5687 return new RemoveExp(loc, dotid->e1, key); | |
5688 } | |
5689 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
5690 { | |
5691 if (!arguments) | |
5692 arguments = new Expressions(); | |
5693 arguments->shift(dotid->e1); | |
5694 e1 = new IdentifierExp(dotid->loc, dotid->ident); | |
5695 } | |
5696 } | |
5697 } | |
5698 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5699 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5700 /* This recognizes: |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5701 * foo!(tiargs)(funcargs) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5702 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5703 if (e1->op == TOKimport && !e1->type) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5704 { ScopeExp *se = (ScopeExp *)e1; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5705 TemplateInstance *ti = se->sds->isTemplateInstance(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5706 if (ti && !ti->semanticdone) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5707 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5708 /* Attempt to instantiate ti. If that works, go with it. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5709 * If not, go with partial explicit specialization. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5710 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5711 ti->semanticTiargs(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5712 unsigned errors = global.errors; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5713 global.gag++; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5714 ti->semantic(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5715 global.gag--; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5716 if (errors != global.errors) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5717 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5718 /* Didn't work, go with partial explicit specialization |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5719 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5720 global.errors = errors; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5721 targsi = ti->tiargs; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5722 e1 = new IdentifierExp(loc, ti->name); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5723 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5724 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5725 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5726 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5727 /* This recognizes: |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5728 * expr.foo!(tiargs)(funcargs) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5729 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5730 if (e1->op == TOKdotti && !e1->type) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5731 { DotTemplateInstanceExp *se = (DotTemplateInstanceExp *)e1; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5732 TemplateInstance *ti = se->ti; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5733 if (!ti->semanticdone) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5734 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5735 /* Attempt to instantiate ti. If that works, go with it. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5736 * If not, go with partial explicit specialization. |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5737 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5738 ti->semanticTiargs(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5739 Expression *etmp; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5740 unsigned errors = global.errors; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5741 global.gag++; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5742 etmp = e1->semantic(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5743 global.gag--; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5744 if (errors != global.errors) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5745 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5746 global.errors = errors; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5747 targsi = ti->tiargs; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5748 e1 = new DotIdExp(loc, se->e1, ti->name); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5749 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5750 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5751 e1 = etmp; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5752 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5753 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5754 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5755 |
159 | 5756 istemp = 0; |
5757 Lagain: | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5758 //printf("Lagain: %s\n", toChars()); |
159 | 5759 f = NULL; |
5760 if (e1->op == TOKthis || e1->op == TOKsuper) | |
5761 { | |
5762 // semantic() run later for these | |
5763 } | |
5764 else | |
5765 { | |
5766 UnaExp::semantic(sc); | |
5767 | |
5768 /* Look for e1 being a lazy parameter | |
5769 */ | |
5770 if (e1->op == TOKvar) | |
5771 { VarExp *ve = (VarExp *)e1; | |
5772 | |
5773 if (ve->var->storage_class & STClazy) | |
5774 { | |
5775 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
5776 TypeDelegate *t = new TypeDelegate(tf); | |
5777 ve->type = t->semantic(loc, sc); | |
5778 } | |
5779 } | |
5780 | |
5781 if (e1->op == TOKimport) | |
5782 { // Perhaps this should be moved to ScopeExp::semantic() | |
5783 ScopeExp *se = (ScopeExp *)e1; | |
5784 e1 = new DsymbolExp(loc, se->sds); | |
5785 e1 = e1->semantic(sc); | |
5786 } | |
5787 #if 1 // patch for #540 by Oskar Linde | |
5788 else if (e1->op == TOKdotexp) | |
5789 { | |
5790 DotExp *de = (DotExp *) e1; | |
5791 | |
5792 if (de->e2->op == TOKimport) | |
5793 { // This should *really* be moved to ScopeExp::semantic() | |
5794 ScopeExp *se = (ScopeExp *)de->e2; | |
5795 de->e2 = new DsymbolExp(loc, se->sds); | |
5796 de->e2 = de->e2->semantic(sc); | |
5797 } | |
5798 | |
5799 if (de->e2->op == TOKtemplate) | |
5800 { TemplateExp *te = (TemplateExp *) de->e2; | |
5801 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
5802 } | |
5803 } | |
5804 #endif | |
5805 } | |
5806 | |
5807 if (e1->op == TOKcomma) | |
5808 { | |
5809 CommaExp *ce = (CommaExp *)e1; | |
5810 | |
5811 e1 = ce->e2; | |
5812 e1->type = ce->type; | |
5813 ce->e2 = this; | |
5814 ce->type = NULL; | |
5815 return ce->semantic(sc); | |
5816 } | |
5817 | |
5818 t1 = NULL; | |
5819 if (e1->type) | |
5820 t1 = e1->type->toBasetype(); | |
5821 | |
5822 // Check for call operator overload | |
5823 if (t1) | |
5824 { AggregateDeclaration *ad; | |
5825 | |
5826 if (t1->ty == Tstruct) | |
5827 { | |
5828 ad = ((TypeStruct *)t1)->sym; | |
5829 if (search_function(ad, Id::call)) | |
5830 goto L1; // overload of opCall, therefore it's a call | |
5831 | |
5832 if (e1->op != TOKtype) | |
5833 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
5834 /* It's a struct literal | |
5835 */ | |
5836 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
5837 e = e->semantic(sc); | |
5838 e->type = e1->type; // in case e1->type was a typedef | |
5839 return e; | |
5840 } | |
5841 else if (t1->ty == Tclass) | |
5842 { | |
5843 ad = ((TypeClass *)t1)->sym; | |
5844 goto L1; | |
5845 L1: | |
5846 // Rewrite as e1.call(arguments) | |
5847 Expression *e = new DotIdExp(loc, e1, Id::call); | |
5848 e = new CallExp(loc, e, arguments); | |
5849 e = e->semantic(sc); | |
5850 return e; | |
5851 } | |
5852 } | |
5853 | |
5854 arrayExpressionSemantic(arguments, sc); | |
5855 preFunctionArguments(loc, sc, arguments); | |
5856 | |
5857 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
5858 e1->op == TOKdottd) | |
5859 { | |
5860 DotVarExp *dve; | |
5861 DotTemplateExp *dte; | |
5862 AggregateDeclaration *ad; | |
5863 UnaExp *ue = (UnaExp *)(e1); | |
336 | 5864 |
159 | 5865 if (e1->op == TOKdotvar) |
5866 { // Do overload resolution | |
5867 dve = (DotVarExp *)(e1); | |
5868 | |
5869 f = dve->var->isFuncDeclaration(); | |
5870 assert(f); | |
5871 f = f->overloadResolve(loc, arguments); | |
5872 | |
5873 ad = f->toParent()->isAggregateDeclaration(); | |
5874 } | |
5875 else | |
5876 { dte = (DotTemplateExp *)(e1); | |
5877 TemplateDeclaration *td = dte->td; | |
5878 assert(td); | |
5879 if (!arguments) | |
5880 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
5881 arguments = new Expressions(); | |
5882 f = td->deduceFunctionTemplate(sc, loc, NULL, arguments); | |
5883 if (!f) | |
5884 { type = Type::terror; | |
5885 return this; | |
5886 } | |
5887 ad = td->toParent()->isAggregateDeclaration(); | |
5888 } | |
336 | 5889 if (f->needThis()) |
5890 { | |
5891 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
5892 } | |
5893 | |
5894 /* Cannot call public functions from inside invariant | |
5895 * (because then the invariant would have infinite recursion) | |
159 | 5896 */ |
336 | 5897 if (sc->func && sc->func->isInvariantDeclaration() && |
5898 ue->e1->op == TOKthis && | |
5899 f->addPostInvariant() | |
159 | 5900 ) |
5901 { | |
336 | 5902 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 5903 } |
5904 | |
5905 checkDeprecated(sc, f); | |
5906 accessCheck(loc, sc, ue->e1, f); | |
5907 if (!f->needThis()) | |
5908 { | |
5909 VarExp *ve = new VarExp(loc, f); | |
5910 e1 = new CommaExp(loc, ue->e1, ve); | |
5911 e1->type = f->type; | |
5912 } | |
5913 else | |
5914 { | |
5915 if (e1->op == TOKdotvar) | |
5916 dve->var = f; | |
5917 else | |
5918 e1 = new DotVarExp(loc, dte->e1, f); | |
5919 e1->type = f->type; | |
5920 | |
5921 // See if we need to adjust the 'this' pointer | |
5922 AggregateDeclaration *ad = f->isThis(); | |
5923 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
5924 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
5925 ue->e1->op != TOKsuper) | |
5926 { | |
5927 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
5928 ue->e1 = ue->e1->semantic(sc); | |
5929 } | |
5930 } | |
5931 t1 = e1->type; | |
5932 } | |
5933 else if (e1->op == TOKsuper) | |
5934 { | |
5935 // Base class constructor call | |
5936 ClassDeclaration *cd = NULL; | |
5937 | |
5938 if (sc->func) | |
5939 cd = sc->func->toParent()->isClassDeclaration(); | |
5940 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
5941 { | |
5942 error("super class constructor call must be in a constructor"); | |
5943 type = Type::terror; | |
5944 return this; | |
5945 } | |
5946 else | |
5947 { | |
5948 f = cd->baseClass->ctor; | |
5949 if (!f) | |
5950 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
5951 type = Type::terror; | |
5952 return this; | |
5953 } | |
5954 else | |
5955 { | |
336 | 5956 if (!sc->intypeof) |
5957 { | |
159 | 5958 #if 0 |
336 | 5959 if (sc->callSuper & (CSXthis | CSXsuper)) |
5960 error("reference to this before super()"); | |
159 | 5961 #endif |
336 | 5962 if (sc->noctor || sc->callSuper & CSXlabel) |
5963 error("constructor calls not allowed in loops or after labels"); | |
5964 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
5965 error("multiple constructor calls"); | |
5966 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
5967 } | |
159 | 5968 |
5969 f = f->overloadResolve(loc, arguments); | |
5970 checkDeprecated(sc, f); | |
5971 e1 = new DotVarExp(e1->loc, e1, f); | |
5972 e1 = e1->semantic(sc); | |
5973 t1 = e1->type; | |
5974 } | |
5975 } | |
5976 } | |
5977 else if (e1->op == TOKthis) | |
5978 { | |
5979 // same class constructor call | |
5980 ClassDeclaration *cd = NULL; | |
5981 | |
5982 if (sc->func) | |
5983 cd = sc->func->toParent()->isClassDeclaration(); | |
5984 if (!cd || !sc->func->isCtorDeclaration()) | |
5985 { | |
5986 error("class constructor call must be in a constructor"); | |
5987 type = Type::terror; | |
5988 return this; | |
5989 } | |
5990 else | |
5991 { | |
336 | 5992 if (!sc->intypeof) |
5993 { | |
159 | 5994 #if 0 |
336 | 5995 if (sc->callSuper & (CSXthis | CSXsuper)) |
5996 error("reference to this before super()"); | |
159 | 5997 #endif |
336 | 5998 if (sc->noctor || sc->callSuper & CSXlabel) |
5999 error("constructor calls not allowed in loops or after labels"); | |
6000 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6001 error("multiple constructor calls"); | |
6002 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6003 } | |
159 | 6004 |
6005 f = cd->ctor; | |
6006 f = f->overloadResolve(loc, arguments); | |
6007 checkDeprecated(sc, f); | |
6008 e1 = new DotVarExp(e1->loc, e1, f); | |
6009 e1 = e1->semantic(sc); | |
6010 t1 = e1->type; | |
6011 | |
6012 // BUG: this should really be done by checking the static | |
6013 // call graph | |
6014 if (f == sc->func) | |
6015 error("cyclic constructor call"); | |
6016 } | |
6017 } | |
6018 else if (!t1) | |
6019 { | |
6020 error("function expected before (), not '%s'", e1->toChars()); | |
6021 type = Type::terror; | |
6022 return this; | |
6023 } | |
6024 else if (t1->ty != Tfunction) | |
6025 { | |
6026 if (t1->ty == Tdelegate) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6027 { TypeDelegate *td = (TypeDelegate *)t1; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6028 assert(td->next->ty == Tfunction); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6029 tf = (TypeFunction *)(td->next); |
159 | 6030 goto Lcheckargs; |
6031 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6032 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6033 { Expression *e; |
6034 | |
6035 e = new PtrExp(loc, e1); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6036 t1 = ((TypePointer *)t1)->next; |
159 | 6037 e->type = t1; |
6038 e1 = e; | |
6039 } | |
6040 else if (e1->op == TOKtemplate) | |
6041 { | |
6042 TemplateExp *te = (TemplateExp *)e1; | |
6043 f = te->td->deduceFunctionTemplate(sc, loc, NULL, arguments); | |
6044 if (!f) | |
6045 { type = Type::terror; | |
6046 return this; | |
6047 } | |
6048 if (f->needThis() && hasThis(sc)) | |
6049 { | |
6050 // Supply an implicit 'this', as in | |
6051 // this.ident | |
6052 | |
6053 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6054 goto Lagain; | |
6055 } | |
6056 | |
6057 e1 = new VarExp(loc, f); | |
6058 goto Lagain; | |
6059 } | |
6060 else | |
6061 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6062 type = Type::terror; | |
6063 return this; | |
6064 } | |
6065 } | |
6066 else if (e1->op == TOKvar) | |
6067 { | |
6068 // Do overload resolution | |
6069 VarExp *ve = (VarExp *)e1; | |
6070 | |
6071 f = ve->var->isFuncDeclaration(); | |
6072 assert(f); | |
6073 | |
6074 // Look to see if f is really a function template | |
6075 if (0 && !istemp && f->parent) | |
6076 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6077 | |
6078 if (ti && | |
6079 (ti->name == f->ident || | |
6080 ti->toAlias()->ident == f->ident) | |
6081 && | |
6082 ti->tempdecl) | |
6083 { | |
6084 /* This is so that one can refer to the enclosing | |
6085 * template, even if it has the same name as a member | |
6086 * of the template, if it has a !(arguments) | |
6087 */ | |
6088 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6089 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6090 tempdecl = tempdecl->overroot; // then get the start | |
6091 e1 = new TemplateExp(loc, tempdecl); | |
6092 istemp = 1; | |
6093 goto Lagain; | |
6094 } | |
6095 } | |
6096 | |
6097 f = f->overloadResolve(loc, arguments); | |
6098 checkDeprecated(sc, f); | |
6099 | |
6100 if (f->needThis() && hasThis(sc)) | |
6101 { | |
6102 // Supply an implicit 'this', as in | |
6103 // this.ident | |
6104 | |
6105 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6106 goto Lagain; | |
6107 } | |
6108 | |
6109 accessCheck(loc, sc, NULL, f); | |
6110 | |
6111 ve->var = f; | |
6112 ve->type = f->type; | |
6113 t1 = f->type; | |
6114 } | |
6115 assert(t1->ty == Tfunction); | |
6116 tf = (TypeFunction *)(t1); | |
6117 | |
6118 Lcheckargs: | |
6119 assert(tf->ty == Tfunction); | |
6120 type = tf->next; | |
6121 | |
6122 if (!arguments) | |
6123 arguments = new Expressions(); | |
6124 functionArguments(loc, sc, tf, arguments); | |
6125 | |
6126 assert(type); | |
6127 | |
6128 if (f && f->tintro) | |
6129 { | |
6130 Type *t = type; | |
6131 int offset = 0; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6132 TypeFunction *tf = (TypeFunction *)f->tintro; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6133 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6134 if (tf->next->isBaseOf(t, &offset) && offset) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6135 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6136 type = tf->next; |
159 | 6137 return castTo(sc, t); |
6138 } | |
6139 } | |
6140 | |
6141 return this; | |
6142 } | |
6143 | |
6144 int CallExp::checkSideEffect(int flag) | |
6145 { | |
6146 return 1; | |
6147 } | |
6148 | |
6149 Expression *CallExp::toLvalue(Scope *sc, Expression *e) | |
6150 { | |
6151 if (type->toBasetype()->ty == Tstruct) | |
6152 return this; | |
6153 else | |
6154 return Expression::toLvalue(sc, e); | |
6155 } | |
6156 | |
6157 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6158 { int i; | |
6159 | |
6160 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6161 buf->writeByte('('); | |
6162 argsToCBuffer(buf, arguments, hgs); | |
6163 buf->writeByte(')'); | |
6164 } | |
6165 | |
6166 | |
6167 /************************************************************/ | |
6168 | |
6169 AddrExp::AddrExp(Loc loc, Expression *e) | |
6170 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6171 { | |
6172 } | |
6173 | |
6174 Expression *AddrExp::semantic(Scope *sc) | |
6175 { | |
6176 #if LOGSEMANTIC | |
6177 printf("AddrExp::semantic('%s')\n", toChars()); | |
6178 #endif | |
6179 if (!type) | |
6180 { | |
6181 UnaExp::semantic(sc); | |
6182 e1 = e1->toLvalue(sc, NULL); | |
6183 if (!e1->type) | |
6184 { | |
6185 error("cannot take address of %s", e1->toChars()); | |
6186 type = Type::tint32; | |
6187 return this; | |
6188 } | |
6189 type = e1->type->pointerTo(); | |
6190 | |
6191 // See if this should really be a delegate | |
6192 if (e1->op == TOKdotvar) | |
6193 { | |
6194 DotVarExp *dve = (DotVarExp *)e1; | |
6195 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6196 | |
6197 if (f) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6198 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6199 e = e->semantic(sc); |
6200 return e; | |
6201 } | |
6202 } | |
6203 else if (e1->op == TOKvar) | |
6204 { | |
6205 VarExp *dve = (VarExp *)e1; | |
6206 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6207 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6208 | |
486
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6209 // LLVMDC |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6210 if (f && f->isIntrinsic()) |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6211 { |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6212 error("cannot take the address of intrinsic function %s", e1->toChars()); |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6213 return this; |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6214 } |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6215 |
159 | 6216 if (f && f->isNested()) |
6217 { Expression *e; | |
6218 | |
6219 e = new DelegateExp(loc, e1, f); | |
6220 e = e->semantic(sc); | |
6221 return e; | |
6222 } | |
6223 } | |
6224 else if (e1->op == TOKarray) | |
6225 { | |
6226 if (e1->type->toBasetype()->ty == Tbit) | |
6227 error("cannot take address of bit in array"); | |
6228 } | |
6229 return optimize(WANTvalue); | |
6230 } | |
6231 return this; | |
6232 } | |
6233 | |
6234 /************************************************************/ | |
6235 | |
6236 PtrExp::PtrExp(Loc loc, Expression *e) | |
6237 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6238 { | |
6239 if (e->type) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6240 type = ((TypePointer *)e->type)->next; |
159 | 6241 } |
6242 | |
6243 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6244 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6245 { | |
6246 type = t; | |
6247 } | |
6248 | |
6249 Expression *PtrExp::semantic(Scope *sc) | |
6250 { Type *tb; | |
6251 | |
6252 #if LOGSEMANTIC | |
6253 printf("PtrExp::semantic('%s')\n", toChars()); | |
6254 #endif | |
6255 UnaExp::semantic(sc); | |
6256 e1 = resolveProperties(sc, e1); | |
6257 if (type) | |
6258 return this; | |
6259 if (!e1->type) | |
6260 printf("PtrExp::semantic('%s')\n", toChars()); | |
6261 tb = e1->type->toBasetype(); | |
6262 switch (tb->ty) | |
6263 { | |
6264 case Tpointer: | |
6265 type = tb->next; | |
6266 if (type->isbit()) | |
6267 { Expression *e; | |
6268 | |
6269 // Rewrite *p as p[0] | |
6270 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
6271 return e->semantic(sc); | |
6272 } | |
6273 break; | |
6274 | |
6275 case Tsarray: | |
6276 case Tarray: | |
6277 type = tb->next; | |
6278 e1 = e1->castTo(sc, type->pointerTo()); | |
6279 break; | |
6280 | |
6281 default: | |
6282 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
6283 type = Type::tint32; | |
6284 break; | |
6285 } | |
6286 rvalue(); | |
6287 return this; | |
6288 } | |
6289 | |
6290 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) | |
6291 { | |
6292 #if 0 | |
6293 tym = tybasic(e1->ET->Tty); | |
6294 if (!(tyscalar(tym) || | |
6295 tym == TYstruct || | |
6296 tym == TYarray && e->Eoper == TOKaddr)) | |
6297 synerr(EM_lvalue); // lvalue expected | |
6298 #endif | |
6299 return this; | |
6300 } | |
6301 | |
6302 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6303 { | |
6304 buf->writeByte('*'); | |
6305 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6306 } | |
6307 | |
6308 /************************************************************/ | |
6309 | |
6310 NegExp::NegExp(Loc loc, Expression *e) | |
6311 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
6312 { | |
6313 } | |
6314 | |
6315 Expression *NegExp::semantic(Scope *sc) | |
6316 { Expression *e; | |
6317 | |
6318 #if LOGSEMANTIC | |
6319 printf("NegExp::semantic('%s')\n", toChars()); | |
6320 #endif | |
6321 if (!type) | |
6322 { | |
6323 UnaExp::semantic(sc); | |
6324 e1 = resolveProperties(sc, e1); | |
6325 e = op_overload(sc); | |
6326 if (e) | |
6327 return e; | |
6328 | |
6329 e1->checkNoBool(); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6330 if (e1->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6331 e1->checkArithmetic(); |
159 | 6332 type = e1->type; |
6333 } | |
6334 return this; | |
6335 } | |
6336 | |
6337 /************************************************************/ | |
6338 | |
6339 UAddExp::UAddExp(Loc loc, Expression *e) | |
6340 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
6341 { | |
6342 } | |
6343 | |
6344 Expression *UAddExp::semantic(Scope *sc) | |
6345 { Expression *e; | |
6346 | |
6347 #if LOGSEMANTIC | |
6348 printf("UAddExp::semantic('%s')\n", toChars()); | |
6349 #endif | |
6350 assert(!type); | |
6351 UnaExp::semantic(sc); | |
6352 e1 = resolveProperties(sc, e1); | |
6353 e = op_overload(sc); | |
6354 if (e) | |
6355 return e; | |
6356 e1->checkNoBool(); | |
6357 e1->checkArithmetic(); | |
6358 return e1; | |
6359 } | |
6360 | |
6361 /************************************************************/ | |
6362 | |
6363 ComExp::ComExp(Loc loc, Expression *e) | |
6364 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6365 { | |
6366 } | |
6367 | |
6368 Expression *ComExp::semantic(Scope *sc) | |
6369 { Expression *e; | |
6370 | |
6371 if (!type) | |
6372 { | |
6373 UnaExp::semantic(sc); | |
6374 e1 = resolveProperties(sc, e1); | |
6375 e = op_overload(sc); | |
6376 if (e) | |
6377 return e; | |
6378 | |
6379 e1->checkNoBool(); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6380 if (e1->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6381 e1 = e1->checkIntegral(); |
159 | 6382 type = e1->type; |
6383 } | |
6384 return this; | |
6385 } | |
6386 | |
6387 /************************************************************/ | |
6388 | |
6389 NotExp::NotExp(Loc loc, Expression *e) | |
6390 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6391 { | |
6392 } | |
6393 | |
6394 Expression *NotExp::semantic(Scope *sc) | |
6395 { | |
6396 UnaExp::semantic(sc); | |
6397 e1 = resolveProperties(sc, e1); | |
6398 e1 = e1->checkToBoolean(); | |
6399 type = Type::tboolean; | |
6400 return this; | |
6401 } | |
6402 | |
6403 int NotExp::isBit() | |
6404 { | |
6405 return TRUE; | |
6406 } | |
6407 | |
6408 | |
6409 | |
6410 /************************************************************/ | |
6411 | |
6412 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6413 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6414 { | |
6415 type = t; | |
6416 } | |
6417 | |
6418 Expression *BoolExp::semantic(Scope *sc) | |
6419 { | |
6420 UnaExp::semantic(sc); | |
6421 e1 = resolveProperties(sc, e1); | |
6422 e1 = e1->checkToBoolean(); | |
6423 type = Type::tboolean; | |
6424 return this; | |
6425 } | |
6426 | |
6427 int BoolExp::isBit() | |
6428 { | |
6429 return TRUE; | |
6430 } | |
6431 | |
6432 /************************************************************/ | |
6433 | |
6434 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6435 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6436 { | |
6437 } | |
6438 | |
6439 Expression *DeleteExp::semantic(Scope *sc) | |
6440 { | |
6441 Type *tb; | |
6442 | |
6443 UnaExp::semantic(sc); | |
6444 e1 = resolveProperties(sc, e1); | |
6445 e1 = e1->toLvalue(sc, NULL); | |
6446 type = Type::tvoid; | |
6447 | |
6448 tb = e1->type->toBasetype(); | |
6449 switch (tb->ty) | |
6450 { case Tclass: | |
6451 { TypeClass *tc = (TypeClass *)tb; | |
6452 ClassDeclaration *cd = tc->sym; | |
6453 | |
6454 if (cd->isCOMinterface()) | |
6455 { /* Because COM classes are deleted by IUnknown.Release() | |
6456 */ | |
6457 error("cannot delete instance of COM interface %s", cd->toChars()); | |
6458 } | |
6459 break; | |
6460 } | |
6461 case Tpointer: | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6462 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 6463 if (tb->ty == Tstruct) |
6464 { | |
6465 TypeStruct *ts = (TypeStruct *)tb; | |
6466 StructDeclaration *sd = ts->sym; | |
6467 FuncDeclaration *f = sd->aggDelete; | |
6468 | |
6469 if (f) | |
6470 { | |
6471 Type *tpv = Type::tvoid->pointerTo(); | |
6472 | |
6473 Expression *e = e1->castTo(sc, tpv); | |
6474 Expression *ec = new VarExp(loc, f); | |
6475 e = new CallExp(loc, ec, e); | |
6476 return e->semantic(sc); | |
6477 } | |
6478 } | |
6479 break; | |
6480 | |
6481 case Tarray: | |
6482 break; | |
6483 | |
6484 default: | |
6485 if (e1->op == TOKindex) | |
6486 { | |
6487 IndexExp *ae = (IndexExp *)(e1); | |
6488 Type *tb1 = ae->e1->type->toBasetype(); | |
6489 if (tb1->ty == Taarray) | |
6490 break; | |
6491 } | |
6492 error("cannot delete type %s", e1->type->toChars()); | |
6493 break; | |
6494 } | |
6495 | |
6496 if (e1->op == TOKindex) | |
6497 { | |
6498 IndexExp *ae = (IndexExp *)(e1); | |
6499 Type *tb1 = ae->e1->type->toBasetype(); | |
6500 if (tb1->ty == Taarray) | |
6501 { if (!global.params.useDeprecated) | |
6502 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6503 } | |
6504 } | |
6505 | |
6506 return this; | |
6507 } | |
6508 | |
6509 int DeleteExp::checkSideEffect(int flag) | |
6510 { | |
6511 return 1; | |
6512 } | |
6513 | |
6514 Expression *DeleteExp::checkToBoolean() | |
6515 { | |
6516 error("delete does not give a boolean result"); | |
6517 return this; | |
6518 } | |
6519 | |
6520 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6521 { | |
6522 buf->writestring("delete "); | |
6523 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6524 } | |
6525 | |
6526 /************************************************************/ | |
6527 | |
6528 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6529 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6530 { | |
6531 to = t; | |
6532 } | |
6533 | |
6534 Expression *CastExp::syntaxCopy() | |
6535 { | |
6536 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6537 } | |
6538 | |
6539 | |
6540 Expression *CastExp::semantic(Scope *sc) | |
6541 { Expression *e; | |
6542 BinExp *b; | |
6543 UnaExp *u; | |
6544 | |
6545 #if LOGSEMANTIC | |
6546 printf("CastExp::semantic('%s')\n", toChars()); | |
6547 #endif | |
6548 | |
6549 //static int x; assert(++x < 10); | |
6550 | |
6551 if (type) | |
6552 return this; | |
6553 UnaExp::semantic(sc); | |
6554 if (e1->type) // if not a tuple | |
6555 { | |
6556 e1 = resolveProperties(sc, e1); | |
6557 to = to->semantic(loc, sc); | |
6558 | |
6559 e = op_overload(sc); | |
6560 if (e) | |
6561 { | |
6562 return e->implicitCastTo(sc, to); | |
6563 } | |
6564 | |
6565 Type *tob = to->toBasetype(); | |
6566 if (tob->ty == Tstruct && | |
6567 !tob->equals(e1->type->toBasetype()) && | |
6568 ((TypeStruct *)to)->sym->search(0, Id::call, 0) | |
6569 ) | |
6570 { | |
6571 /* Look to replace: | |
6572 * cast(S)t | |
6573 * with: | |
6574 * S(t) | |
6575 */ | |
6576 | |
6577 // Rewrite as to.call(e1) | |
6578 e = new TypeExp(loc, to); | |
6579 e = new DotIdExp(loc, e, Id::call); | |
6580 e = new CallExp(loc, e, e1); | |
6581 e = e->semantic(sc); | |
6582 return e; | |
6583 } | |
6584 } | |
6585 e = e1->castTo(sc, to); | |
6586 return e; | |
6587 } | |
6588 | |
6589 int CastExp::checkSideEffect(int flag) | |
6590 { | |
6591 /* if not: | |
6592 * cast(void) | |
6593 * cast(classtype)func() | |
6594 */ | |
6595 if (!to->equals(Type::tvoid) && | |
6596 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
6597 return Expression::checkSideEffect(flag); | |
6598 return 1; | |
6599 } | |
6600 | |
6601 void CastExp::checkEscape() | |
6602 { Type *tb = type->toBasetype(); | |
6603 if (tb->ty == Tarray && e1->op == TOKvar && | |
6604 e1->type->toBasetype()->ty == Tsarray) | |
6605 { VarExp *ve = (VarExp *)e1; | |
6606 VarDeclaration *v = ve->var->isVarDeclaration(); | |
6607 if (v) | |
6608 { | |
6609 if (!v->isDataseg() && !v->isParameter()) | |
6610 error("escaping reference to local %s", v->toChars()); | |
6611 } | |
6612 } | |
6613 } | |
6614 | |
6615 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6616 { | |
6617 buf->writestring("cast("); | |
6618 to->toCBuffer(buf, NULL, hgs); | |
6619 buf->writeByte(')'); | |
6620 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6621 } | |
6622 | |
6623 | |
6624 /************************************************************/ | |
6625 | |
6626 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
6627 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
6628 { | |
6629 this->upr = upr; | |
6630 this->lwr = lwr; | |
6631 lengthVar = NULL; | |
6632 } | |
6633 | |
6634 Expression *SliceExp::syntaxCopy() | |
6635 { | |
6636 Expression *lwr = NULL; | |
6637 if (this->lwr) | |
6638 lwr = this->lwr->syntaxCopy(); | |
6639 | |
6640 Expression *upr = NULL; | |
6641 if (this->upr) | |
6642 upr = this->upr->syntaxCopy(); | |
6643 | |
6644 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
6645 } | |
6646 | |
6647 Expression *SliceExp::semantic(Scope *sc) | |
6648 { Expression *e; | |
6649 AggregateDeclaration *ad; | |
6650 //FuncDeclaration *fd; | |
6651 ScopeDsymbol *sym; | |
6652 | |
6653 #if LOGSEMANTIC | |
6654 printf("SliceExp::semantic('%s')\n", toChars()); | |
6655 #endif | |
6656 if (type) | |
6657 return this; | |
6658 | |
6659 UnaExp::semantic(sc); | |
6660 e1 = resolveProperties(sc, e1); | |
6661 | |
6662 e = this; | |
6663 | |
6664 Type *t = e1->type->toBasetype(); | |
6665 if (t->ty == Tpointer) | |
6666 { | |
6667 if (!lwr || !upr) | |
6668 error("need upper and lower bound to slice pointer"); | |
6669 } | |
6670 else if (t->ty == Tarray) | |
6671 { | |
6672 } | |
6673 else if (t->ty == Tsarray) | |
6674 { | |
6675 } | |
6676 else if (t->ty == Tclass) | |
6677 { | |
6678 ad = ((TypeClass *)t)->sym; | |
6679 goto L1; | |
6680 } | |
6681 else if (t->ty == Tstruct) | |
6682 { | |
6683 ad = ((TypeStruct *)t)->sym; | |
6684 | |
6685 L1: | |
6686 if (search_function(ad, Id::slice)) | |
6687 { | |
6688 // Rewrite as e1.slice(lwr, upr) | |
6689 e = new DotIdExp(loc, e1, Id::slice); | |
6690 | |
6691 if (lwr) | |
6692 { | |
6693 assert(upr); | |
6694 e = new CallExp(loc, e, lwr, upr); | |
6695 } | |
6696 else | |
6697 { assert(!upr); | |
6698 e = new CallExp(loc, e); | |
6699 } | |
6700 e = e->semantic(sc); | |
6701 return e; | |
6702 } | |
6703 goto Lerror; | |
6704 } | |
6705 else if (t->ty == Ttuple) | |
6706 { | |
6707 if (!lwr && !upr) | |
6708 return e1; | |
6709 if (!lwr || !upr) | |
6710 { error("need upper and lower bound to slice tuple"); | |
6711 goto Lerror; | |
6712 } | |
6713 } | |
6714 else | |
6715 goto Lerror; | |
6716 | |
6717 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
6718 { | |
6719 sym = new ArrayScopeSymbol(this); | |
6720 sym->loc = loc; | |
6721 sym->parent = sc->scopesym; | |
6722 sc = sc->push(sym); | |
6723 } | |
6724 | |
6725 if (lwr) | |
6726 { lwr = lwr->semantic(sc); | |
6727 lwr = resolveProperties(sc, lwr); | |
6728 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
6729 } | |
6730 if (upr) | |
6731 { upr = upr->semantic(sc); | |
6732 upr = resolveProperties(sc, upr); | |
6733 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
6734 } | |
6735 | |
6736 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
6737 sc->pop(); | |
6738 | |
6739 if (t->ty == Ttuple) | |
6740 { | |
6741 lwr = lwr->optimize(WANTvalue); | |
6742 upr = upr->optimize(WANTvalue); | |
6743 uinteger_t i1 = lwr->toUInteger(); | |
6744 uinteger_t i2 = upr->toUInteger(); | |
6745 | |
6746 size_t length; | |
6747 TupleExp *te; | |
6748 TypeTuple *tup; | |
6749 | |
6750 if (e1->op == TOKtuple) // slicing an expression tuple | |
6751 { te = (TupleExp *)e1; | |
6752 length = te->exps->dim; | |
6753 } | |
6754 else if (e1->op == TOKtype) // slicing a type tuple | |
6755 { tup = (TypeTuple *)t; | |
6756 length = Argument::dim(tup->arguments); | |
6757 } | |
6758 else | |
6759 assert(0); | |
6760 | |
6761 if (i1 <= i2 && i2 <= length) | |
6762 { size_t j1 = (size_t) i1; | |
6763 size_t j2 = (size_t) i2; | |
6764 | |
6765 if (e1->op == TOKtuple) | |
6766 { Expressions *exps = new Expressions; | |
6767 exps->setDim(j2 - j1); | |
6768 for (size_t i = 0; i < j2 - j1; i++) | |
6769 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
6770 exps->data[i] = (void *)e; | |
6771 } | |
6772 e = new TupleExp(loc, exps); | |
6773 } | |
6774 else | |
6775 { Arguments *args = new Arguments; | |
6776 args->reserve(j2 - j1); | |
6777 for (size_t i = j1; i < j2; i++) | |
6778 { Argument *arg = Argument::getNth(tup->arguments, i); | |
6779 args->push(arg); | |
6780 } | |
6781 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
6782 } | |
6783 e = e->semantic(sc); | |
6784 } | |
6785 else | |
6786 { | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
6787 error("string slice [%llu .. %llu] is out of bounds", i1, i2); |
159 | 6788 e = e1; |
6789 } | |
6790 return e; | |
6791 } | |
6792 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6793 type = t->nextOf()->arrayOf(); |
159 | 6794 return e; |
6795 | |
6796 Lerror: | |
6797 char *s; | |
6798 if (t->ty == Tvoid) | |
6799 s = e1->toChars(); | |
6800 else | |
6801 s = t->toChars(); | |
6802 error("%s cannot be sliced with []", s); | |
6803 type = Type::terror; | |
6804 return e; | |
6805 } | |
6806 | |
6807 void SliceExp::checkEscape() | |
6808 { | |
6809 e1->checkEscape(); | |
6810 } | |
6811 | |
6812 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) | |
6813 { | |
6814 return this; | |
6815 } | |
6816 | |
6817 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
6818 { | |
6819 error("slice expression %s is not a modifiable lvalue", toChars()); | |
6820 return this; | |
6821 } | |
6822 | |
6823 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6824 { | |
6825 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6826 buf->writeByte('['); | |
6827 if (upr || lwr) | |
6828 { | |
6829 if (lwr) | |
6830 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
6831 else | |
6832 buf->writeByte('0'); | |
6833 buf->writestring(".."); | |
6834 if (upr) | |
6835 expToCBuffer(buf, hgs, upr, PREC_assign); | |
6836 else | |
6837 buf->writestring("length"); // BUG: should be array.length | |
6838 } | |
6839 buf->writeByte(']'); | |
6840 } | |
6841 | |
6842 /********************** ArrayLength **************************************/ | |
6843 | |
6844 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
6845 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
6846 { | |
6847 } | |
6848 | |
6849 Expression *ArrayLengthExp::semantic(Scope *sc) | |
6850 { Expression *e; | |
6851 | |
6852 #if LOGSEMANTIC | |
6853 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
6854 #endif | |
6855 if (!type) | |
6856 { | |
6857 UnaExp::semantic(sc); | |
6858 e1 = resolveProperties(sc, e1); | |
6859 | |
6860 type = Type::tsize_t; | |
6861 } | |
6862 return this; | |
6863 } | |
6864 | |
6865 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6866 { | |
6867 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6868 buf->writestring(".length"); | |
6869 } | |
6870 | |
6871 /*********************** ArrayExp *************************************/ | |
6872 | |
6873 // e1 [ i1, i2, i3, ... ] | |
6874 | |
6875 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
6876 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
6877 { | |
6878 arguments = args; | |
6879 } | |
6880 | |
6881 Expression *ArrayExp::syntaxCopy() | |
6882 { | |
6883 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
6884 } | |
6885 | |
6886 Expression *ArrayExp::semantic(Scope *sc) | |
6887 { Expression *e; | |
6888 Type *t1; | |
6889 | |
6890 #if LOGSEMANTIC | |
6891 printf("ArrayExp::semantic('%s')\n", toChars()); | |
6892 #endif | |
6893 UnaExp::semantic(sc); | |
6894 e1 = resolveProperties(sc, e1); | |
6895 | |
6896 t1 = e1->type->toBasetype(); | |
6897 if (t1->ty != Tclass && t1->ty != Tstruct) | |
6898 { // Convert to IndexExp | |
6899 if (arguments->dim != 1) | |
6900 error("only one index allowed to index %s", t1->toChars()); | |
6901 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
6902 return e->semantic(sc); | |
6903 } | |
6904 | |
6905 // Run semantic() on each argument | |
6906 for (size_t i = 0; i < arguments->dim; i++) | |
6907 { e = (Expression *)arguments->data[i]; | |
6908 | |
6909 e = e->semantic(sc); | |
6910 if (!e->type) | |
6911 error("%s has no value", e->toChars()); | |
6912 arguments->data[i] = (void *)e; | |
6913 } | |
6914 | |
6915 expandTuples(arguments); | |
6916 assert(arguments && arguments->dim); | |
6917 | |
6918 e = op_overload(sc); | |
6919 if (!e) | |
6920 { error("no [] operator overload for type %s", e1->type->toChars()); | |
6921 e = e1; | |
6922 } | |
6923 return e; | |
6924 } | |
6925 | |
6926 | |
6927 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
6928 { | |
6929 if (type && type->toBasetype()->ty == Tvoid) | |
6930 error("voids have no value"); | |
6931 return this; | |
6932 } | |
6933 | |
6934 | |
6935 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6936 { int i; | |
6937 | |
6938 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6939 buf->writeByte('['); | |
6940 argsToCBuffer(buf, arguments, hgs); | |
6941 buf->writeByte(']'); | |
6942 } | |
6943 | |
6944 /************************* DotExp ***********************************/ | |
6945 | |
6946 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
6947 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
6948 { | |
6949 } | |
6950 | |
6951 Expression *DotExp::semantic(Scope *sc) | |
6952 { | |
6953 #if LOGSEMANTIC | |
6954 printf("DotExp::semantic('%s')\n", toChars()); | |
6955 if (type) printf("\ttype = %s\n", type->toChars()); | |
6956 #endif | |
6957 e1 = e1->semantic(sc); | |
6958 e2 = e2->semantic(sc); | |
6959 if (e2->op == TOKimport) | |
6960 { | |
6961 ScopeExp *se = (ScopeExp *)e2; | |
6962 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
6963 if (td) | |
6964 { Expression *e = new DotTemplateExp(loc, e1, td); | |
6965 e = e->semantic(sc); | |
6966 return e; | |
6967 } | |
6968 } | |
6969 if (!type) | |
6970 type = e2->type; | |
6971 return this; | |
6972 } | |
6973 | |
6974 | |
6975 /************************* CommaExp ***********************************/ | |
6976 | |
6977 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
6978 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
6979 { | |
6980 } | |
6981 | |
6982 Expression *CommaExp::semantic(Scope *sc) | |
6983 { | |
6984 if (!type) | |
6985 { BinExp::semanticp(sc); | |
6986 type = e2->type; | |
6987 } | |
6988 return this; | |
6989 } | |
6990 | |
6991 void CommaExp::checkEscape() | |
6992 { | |
6993 e2->checkEscape(); | |
6994 } | |
6995 | |
6996 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) | |
6997 { | |
6998 e2 = e2->toLvalue(sc, NULL); | |
6999 return this; | |
7000 } | |
7001 | |
7002 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7003 { | |
7004 e2 = e2->modifiableLvalue(sc, e); | |
7005 return this; | |
7006 } | |
7007 | |
7008 int CommaExp::isBool(int result) | |
7009 { | |
7010 return e2->isBool(result); | |
7011 } | |
7012 | |
7013 int CommaExp::checkSideEffect(int flag) | |
7014 { | |
7015 if (flag == 2) | |
7016 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7017 else | |
7018 { | |
7019 // Don't check e1 until we cast(void) the a,b code generation | |
7020 return e2->checkSideEffect(flag); | |
7021 } | |
7022 } | |
7023 | |
7024 /************************** IndexExp **********************************/ | |
7025 | |
7026 // e1 [ e2 ] | |
7027 | |
7028 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7029 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7030 { | |
7031 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7032 lengthVar = NULL; | |
7033 modifiable = 0; // assume it is an rvalue | |
7034 } | |
7035 | |
7036 Expression *IndexExp::semantic(Scope *sc) | |
7037 { Expression *e; | |
7038 BinExp *b; | |
7039 UnaExp *u; | |
7040 Type *t1; | |
7041 ScopeDsymbol *sym; | |
7042 | |
7043 #if LOGSEMANTIC | |
7044 printf("IndexExp::semantic('%s')\n", toChars()); | |
7045 #endif | |
7046 if (type) | |
7047 return this; | |
7048 if (!e1->type) | |
7049 e1 = e1->semantic(sc); | |
7050 assert(e1->type); // semantic() should already be run on it | |
7051 e = this; | |
7052 | |
7053 // Note that unlike C we do not implement the int[ptr] | |
7054 | |
7055 t1 = e1->type->toBasetype(); | |
7056 | |
7057 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7058 { // Create scope for 'length' variable | |
7059 sym = new ArrayScopeSymbol(this); | |
7060 sym->loc = loc; | |
7061 sym->parent = sc->scopesym; | |
7062 sc = sc->push(sym); | |
7063 } | |
7064 | |
7065 e2 = e2->semantic(sc); | |
7066 if (!e2->type) | |
7067 { | |
7068 error("%s has no value", e2->toChars()); | |
7069 e2->type = Type::terror; | |
7070 } | |
7071 e2 = resolveProperties(sc, e2); | |
7072 | |
7073 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7074 sc = sc->pop(); | |
7075 | |
7076 switch (t1->ty) | |
7077 { | |
7078 case Tpointer: | |
7079 case Tarray: | |
7080 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7081 e->type = t1->next; | |
7082 break; | |
7083 | |
7084 case Tsarray: | |
7085 { | |
7086 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7087 | |
7088 TypeSArray *tsa = (TypeSArray *)t1; | |
7089 | |
7090 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7091 // Do compile time array bounds checking if possible | |
7092 e2 = e2->optimize(WANTvalue); | |
7093 if (e2->op == TOKint64) | |
7094 { | |
7095 integer_t index = e2->toInteger(); | |
7096 integer_t length = tsa->dim->toInteger(); | |
7097 if (index < 0 || index >= length) | |
7098 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7099 index, length); | |
7100 } | |
7101 #endif | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7102 e->type = t1->nextOf(); |
159 | 7103 break; |
7104 } | |
7105 | |
7106 case Taarray: | |
7107 { TypeAArray *taa = (TypeAArray *)t1; | |
7108 | |
7109 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7110 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7111 type = taa->next; | |
7112 break; | |
7113 } | |
7114 | |
7115 case Ttuple: | |
7116 { | |
7117 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7118 e2 = e2->optimize(WANTvalue); | |
7119 uinteger_t index = e2->toUInteger(); | |
7120 size_t length; | |
7121 TupleExp *te; | |
7122 TypeTuple *tup; | |
7123 | |
7124 if (e1->op == TOKtuple) | |
7125 { te = (TupleExp *)e1; | |
7126 length = te->exps->dim; | |
7127 } | |
7128 else if (e1->op == TOKtype) | |
7129 { | |
7130 tup = (TypeTuple *)t1; | |
7131 length = Argument::dim(tup->arguments); | |
7132 } | |
7133 else | |
7134 assert(0); | |
7135 | |
7136 if (index < length) | |
7137 { | |
7138 | |
7139 if (e1->op == TOKtuple) | |
7140 e = (Expression *)te->exps->data[(size_t)index]; | |
7141 else | |
7142 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
7143 } | |
7144 else | |
7145 { | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7146 error("array index [%llu] is outside array bounds [0 .. %"PRIuSIZE"]", |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7147 index, length); |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7148 e = e1; |
159 | 7149 } |
7150 break; | |
7151 } | |
7152 | |
7153 default: | |
7154 error("%s must be an array or pointer type, not %s", | |
7155 e1->toChars(), e1->type->toChars()); | |
7156 type = Type::tint32; | |
7157 break; | |
7158 } | |
7159 return e; | |
7160 } | |
7161 | |
7162 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) | |
7163 { | |
7164 // if (type && type->toBasetype()->ty == Tvoid) | |
7165 // error("voids have no value"); | |
7166 return this; | |
7167 } | |
7168 | |
7169 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
7170 { | |
7171 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
7172 modifiable = 1; | |
7173 if (e1->op == TOKstring) | |
7174 error("string literals are immutable"); | |
7175 if (e1->type->toBasetype()->ty == Taarray) | |
7176 e1 = e1->modifiableLvalue(sc, e1); | |
7177 return toLvalue(sc, e); | |
7178 } | |
7179 | |
7180 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7181 { | |
7182 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7183 buf->writeByte('['); | |
7184 expToCBuffer(buf, hgs, e2, PREC_assign); | |
7185 buf->writeByte(']'); | |
7186 } | |
7187 | |
7188 | |
7189 /************************* PostExp ***********************************/ | |
7190 | |
7191 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
7192 : BinExp(loc, op, sizeof(PostExp), e, | |
7193 new IntegerExp(loc, 1, Type::tint32)) | |
7194 { | |
7195 } | |
7196 | |
7197 Expression *PostExp::semantic(Scope *sc) | |
7198 { Expression *e = this; | |
7199 | |
7200 if (!type) | |
7201 { | |
7202 BinExp::semantic(sc); | |
7203 e2 = resolveProperties(sc, e2); | |
7204 | |
7205 e = op_overload(sc); | |
7206 if (e) | |
7207 return e; | |
7208 | |
7209 e = this; | |
7210 e1 = e1->modifiableLvalue(sc, e1); | |
7211 e1->checkScalar(); | |
7212 e1->checkNoBool(); | |
7213 if (e1->type->ty == Tpointer) | |
7214 e = scaleFactor(sc); | |
7215 else | |
7216 e2 = e2->castTo(sc, e1->type); | |
7217 e->type = e1->type; | |
7218 } | |
7219 return e; | |
7220 } | |
7221 | |
7222 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7223 { | |
7224 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7225 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
7226 } | |
7227 | |
7228 /************************************************************/ | |
7229 | |
7230 /* Can be TOKconstruct too */ | |
7231 | |
7232 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
7233 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
7234 { | |
7235 ismemset = 0; | |
7236 } | |
7237 | |
7238 Expression *AssignExp::semantic(Scope *sc) | |
7239 { Type *t1; | |
7240 Expression *e1old = e1; | |
7241 | |
7242 #if LOGSEMANTIC | |
7243 printf("AssignExp::semantic('%s')\n", toChars()); | |
7244 #endif | |
7245 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
7246 | |
7247 /* Look for operator overloading of a[i]=value. | |
7248 * Do it before semantic() otherwise the a[i] will have been | |
7249 * converted to a.opIndex() already. | |
7250 */ | |
7251 if (e1->op == TOKarray) | |
7252 { Type *t1; | |
7253 ArrayExp *ae = (ArrayExp *)e1; | |
7254 AggregateDeclaration *ad; | |
7255 Identifier *id = Id::index; | |
7256 | |
7257 ae->e1 = ae->e1->semantic(sc); | |
7258 t1 = ae->e1->type->toBasetype(); | |
7259 if (t1->ty == Tstruct) | |
7260 { | |
7261 ad = ((TypeStruct *)t1)->sym; | |
7262 goto L1; | |
7263 } | |
7264 else if (t1->ty == Tclass) | |
7265 { | |
7266 ad = ((TypeClass *)t1)->sym; | |
7267 L1: | |
7268 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
7269 if (search_function(ad, Id::indexass)) | |
7270 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
7271 Expressions *a = (Expressions *)ae->arguments->copy(); | |
7272 | |
7273 a->insert(0, e2); | |
7274 e = new CallExp(loc, e, a); | |
7275 e = e->semantic(sc); | |
7276 return e; | |
7277 } | |
7278 else | |
7279 { | |
7280 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
7281 if (search_function(ad, id)) | |
7282 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
7283 | |
7284 if (1 || !global.params.useDeprecated) | |
7285 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
7286 | |
7287 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
7288 e = e->semantic(sc); | |
7289 return e; | |
7290 } | |
7291 } | |
7292 } | |
7293 } | |
7294 /* Look for operator overloading of a[i..j]=value. | |
7295 * Do it before semantic() otherwise the a[i..j] will have been | |
7296 * converted to a.opSlice() already. | |
7297 */ | |
7298 if (e1->op == TOKslice) | |
7299 { Type *t1; | |
7300 SliceExp *ae = (SliceExp *)e1; | |
7301 AggregateDeclaration *ad; | |
7302 Identifier *id = Id::index; | |
7303 | |
7304 ae->e1 = ae->e1->semantic(sc); | |
7305 ae->e1 = resolveProperties(sc, ae->e1); | |
7306 t1 = ae->e1->type->toBasetype(); | |
7307 if (t1->ty == Tstruct) | |
7308 { | |
7309 ad = ((TypeStruct *)t1)->sym; | |
7310 goto L2; | |
7311 } | |
7312 else if (t1->ty == Tclass) | |
7313 { | |
7314 ad = ((TypeClass *)t1)->sym; | |
7315 L2: | |
7316 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
7317 if (search_function(ad, Id::sliceass)) | |
7318 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
7319 Expressions *a = new Expressions(); | |
7320 | |
7321 a->push(e2); | |
7322 if (ae->lwr) | |
7323 { a->push(ae->lwr); | |
7324 assert(ae->upr); | |
7325 a->push(ae->upr); | |
7326 } | |
7327 else | |
7328 assert(!ae->upr); | |
7329 e = new CallExp(loc, e, a); | |
7330 e = e->semantic(sc); | |
7331 return e; | |
7332 } | |
7333 } | |
7334 } | |
7335 | |
7336 BinExp::semantic(sc); | |
7337 e2 = resolveProperties(sc, e2); | |
7338 assert(e1->type); | |
7339 | |
7340 /* Rewrite tuple assignment as a tuple of assignments. | |
7341 */ | |
7342 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
7343 { TupleExp *tup1 = (TupleExp *)e1; | |
7344 TupleExp *tup2 = (TupleExp *)e2; | |
7345 size_t dim = tup1->exps->dim; | |
7346 if (dim != tup2->exps->dim) | |
7347 { | |
7348 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
7349 } | |
7350 else | |
7351 { Expressions *exps = new Expressions; | |
7352 exps->setDim(dim); | |
7353 | |
7354 for (int i = 0; i < dim; i++) | |
7355 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
7356 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
7357 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
7358 } | |
7359 Expression *e = new TupleExp(loc, exps); | |
7360 e = e->semantic(sc); | |
7361 return e; | |
7362 } | |
7363 } | |
7364 | |
7365 t1 = e1->type->toBasetype(); | |
7366 | |
7367 if (t1->ty == Tfunction) | |
7368 { // Rewrite f=value to f(value) | |
7369 Expression *e; | |
7370 | |
7371 e = new CallExp(loc, e1, e2); | |
7372 e = e->semantic(sc); | |
7373 return e; | |
7374 } | |
7375 | |
7376 /* If it is an assignment from a 'foreign' type, | |
7377 * check for operator overloading. | |
7378 */ | |
7379 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7380 { | |
7381 if (!e2->type->implicitConvTo(e1->type)) | |
7382 { | |
7383 Expression *e = op_overload(sc); | |
7384 if (e) | |
7385 return e; | |
7386 } | |
7387 } | |
7388 | |
7389 e2->rvalue(); | |
7390 | |
7391 if (e1->op == TOKarraylength) | |
7392 { | |
7393 // e1 is not an lvalue, but we let code generator handle it | |
7394 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7395 | |
7396 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
7397 } | |
7398 else if (e1->op == TOKslice) | |
7399 ; | |
7400 else | |
7401 { // Try to do a decent error message with the expression | |
7402 // before it got constant folded | |
7403 e1 = e1->modifiableLvalue(sc, e1old); | |
7404 } | |
7405 | |
7406 if (e1->op == TOKslice && | |
7407 t1->nextOf() && | |
7408 e2->implicitConvTo(t1->nextOf()) | |
7409 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
7410 ) | |
7411 { // memset | |
7412 ismemset = 1; // make it easy for back end to tell what this is | |
7413 e2 = e2->implicitCastTo(sc, t1->next); | |
7414 } | |
7415 else if (t1->ty == Tsarray) | |
7416 { | |
7417 error("cannot assign to static array %s", e1->toChars()); | |
7418 } | |
7419 else | |
7420 { | |
7421 e2 = e2->implicitCastTo(sc, e1->type); | |
7422 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7423 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7424 /* Look for array operations |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7425 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7426 if (e1->op == TOKslice && !ismemset && |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7427 (e2->op == TOKadd || e2->op == TOKmin || |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7428 e2->op == TOKmul || e2->op == TOKdiv || |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7429 e2->op == TOKmod || e2->op == TOKxor || |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7430 e2->op == TOKand || e2->op == TOKor || |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7431 e2->op == TOKtilde || e2->op == TOKneg)) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7432 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7433 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7434 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7435 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7436 |
159 | 7437 type = e1->type; |
7438 assert(type); | |
7439 return this; | |
7440 } | |
7441 | |
7442 Expression *AssignExp::checkToBoolean() | |
7443 { | |
7444 // Things like: | |
7445 // if (a = b) ... | |
7446 // are usually mistakes. | |
7447 | |
7448 error("'=' does not give a boolean result"); | |
7449 return this; | |
7450 } | |
7451 | |
7452 /************************************************************/ | |
7453 | |
7454 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7455 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7456 { | |
7457 } | |
7458 | |
7459 Expression *AddAssignExp::semantic(Scope *sc) | |
7460 { Expression *e; | |
7461 | |
7462 if (type) | |
7463 return this; | |
7464 | |
7465 BinExp::semantic(sc); | |
7466 e2 = resolveProperties(sc, e2); | |
7467 | |
7468 e = op_overload(sc); | |
7469 if (e) | |
7470 return e; | |
7471 | |
7472 Type *tb1 = e1->type->toBasetype(); | |
7473 Type *tb2 = e2->type->toBasetype(); | |
7474 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7475 if (e1->op == TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7476 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7477 typeCombine(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7478 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7479 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7480 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7481 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7482 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7483 e1 = e1->modifiableLvalue(sc, e1); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7484 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7485 |
159 | 7486 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
7487 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7488 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 7489 ) |
7490 { | |
7491 type = e1->type; | |
7492 e = this; | |
7493 } | |
7494 else | |
7495 { | |
7496 e1->checkScalar(); | |
7497 e1->checkNoBool(); | |
7498 if (tb1->ty == Tpointer && tb2->isintegral()) | |
7499 e = scaleFactor(sc); | |
7500 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
7501 { | |
7502 #if 0 | |
7503 // Need to rethink this | |
7504 if (e1->op != TOKvar) | |
7505 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
7506 VarDeclaration *v; | |
7507 Expression *ea; | |
7508 Expression *ex; | |
7509 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7510 Identifier *id = Lexer::uniqueId("__name"); |
159 | 7511 |
7512 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
7513 v->semantic(sc); | |
7514 if (!sc->insert(v)) | |
7515 assert(0); | |
7516 v->parent = sc->func; | |
7517 | |
7518 ea = new AddrExp(loc, e1); | |
7519 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
7520 | |
7521 ex = new VarExp(loc, v); | |
7522 ex = new PtrExp(loc, ex); | |
7523 e = new AddExp(loc, ex, e2); | |
7524 e = new CastExp(loc, e, e1->type); | |
7525 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
7526 | |
7527 e = new CommaExp(loc, ea, e); | |
7528 } | |
7529 else | |
7530 #endif | |
7531 { // Rewrite e1+=e2 to e1=e1+e2 | |
7532 // BUG: doesn't account for side effects in e1 | |
7533 // BUG: other assignment operators for bits aren't handled at all | |
7534 e = new AddExp(loc, e1, e2); | |
7535 e = new CastExp(loc, e, e1->type); | |
7536 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
7537 } | |
7538 e = e->semantic(sc); | |
7539 } | |
7540 else | |
7541 { | |
7542 type = e1->type; | |
7543 typeCombine(sc); | |
7544 e1->checkArithmetic(); | |
7545 e2->checkArithmetic(); | |
7546 if (type->isreal() || type->isimaginary()) | |
7547 { | |
7548 assert(global.errors || e2->type->isfloating()); | |
7549 e2 = e2->castTo(sc, e1->type); | |
7550 } | |
7551 e = this; | |
7552 } | |
7553 } | |
7554 return e; | |
7555 } | |
7556 | |
7557 /************************************************************/ | |
7558 | |
7559 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7560 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
7561 { | |
7562 } | |
7563 | |
7564 Expression *MinAssignExp::semantic(Scope *sc) | |
7565 { Expression *e; | |
7566 | |
7567 if (type) | |
7568 return this; | |
7569 | |
7570 BinExp::semantic(sc); | |
7571 e2 = resolveProperties(sc, e2); | |
7572 | |
7573 e = op_overload(sc); | |
7574 if (e) | |
7575 return e; | |
7576 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7577 if (e1->op == TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7578 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7579 typeCombine(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7580 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7581 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7582 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7583 |
159 | 7584 e1 = e1->modifiableLvalue(sc, e1); |
7585 e1->checkScalar(); | |
7586 e1->checkNoBool(); | |
7587 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
7588 e = scaleFactor(sc); | |
7589 else | |
7590 { | |
7591 e1 = e1->checkArithmetic(); | |
7592 e2 = e2->checkArithmetic(); | |
7593 type = e1->type; | |
7594 typeCombine(sc); | |
7595 if (type->isreal() || type->isimaginary()) | |
7596 { | |
7597 assert(e2->type->isfloating()); | |
7598 e2 = e2->castTo(sc, e1->type); | |
7599 } | |
7600 e = this; | |
7601 } | |
7602 return e; | |
7603 } | |
7604 | |
7605 /************************************************************/ | |
7606 | |
7607 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7608 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
7609 { | |
7610 } | |
7611 | |
7612 Expression *CatAssignExp::semantic(Scope *sc) | |
7613 { Expression *e; | |
7614 | |
7615 BinExp::semantic(sc); | |
7616 e2 = resolveProperties(sc, e2); | |
7617 | |
7618 e = op_overload(sc); | |
7619 if (e) | |
7620 return e; | |
7621 | |
7622 if (e1->op == TOKslice) | |
7623 { SliceExp *se = (SliceExp *)e1; | |
7624 | |
7625 if (se->e1->type->toBasetype()->ty == Tsarray) | |
7626 error("cannot append to static array %s", se->e1->type->toChars()); | |
7627 } | |
7628 | |
7629 e1 = e1->modifiableLvalue(sc, e1); | |
7630 | |
7631 Type *tb1 = e1->type->toBasetype(); | |
7632 Type *tb2 = e2->type->toBasetype(); | |
7633 | |
7634 e2->rvalue(); | |
7635 | |
7636 if ((tb1->ty == Tarray) && | |
7637 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
7638 e2->implicitConvTo(e1->type) | |
7639 //e1->type->next->equals(e2->type->next) | |
7640 ) | |
7641 { // Append array | |
7642 e2 = e2->castTo(sc, e1->type); | |
7643 type = e1->type; | |
7644 e = this; | |
7645 } | |
7646 else if ((tb1->ty == Tarray) && | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7647 e2->implicitConvTo(tb1->nextOf()) |
159 | 7648 ) |
7649 { // Append element | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7650 e2 = e2->castTo(sc, tb1->nextOf()); |
159 | 7651 type = e1->type; |
7652 e = this; | |
7653 } | |
7654 else | |
7655 { | |
7656 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
7657 type = Type::tint32; | |
7658 e = this; | |
7659 } | |
7660 return e; | |
7661 } | |
7662 | |
7663 /************************************************************/ | |
7664 | |
7665 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7666 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
7667 { | |
7668 } | |
7669 | |
7670 Expression *MulAssignExp::semantic(Scope *sc) | |
7671 { Expression *e; | |
7672 | |
7673 BinExp::semantic(sc); | |
7674 e2 = resolveProperties(sc, e2); | |
7675 | |
7676 e = op_overload(sc); | |
7677 if (e) | |
7678 return e; | |
7679 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7680 if (e1->op == TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7681 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7682 typeCombine(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7683 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7684 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7685 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7686 |
159 | 7687 e1 = e1->modifiableLvalue(sc, e1); |
7688 e1->checkScalar(); | |
7689 e1->checkNoBool(); | |
7690 type = e1->type; | |
7691 typeCombine(sc); | |
7692 e1->checkArithmetic(); | |
7693 e2->checkArithmetic(); | |
7694 if (e2->type->isfloating()) | |
7695 { Type *t1; | |
7696 Type *t2; | |
7697 | |
7698 t1 = e1->type; | |
7699 t2 = e2->type; | |
7700 if (t1->isreal()) | |
7701 { | |
7702 if (t2->isimaginary() || t2->iscomplex()) | |
7703 { | |
7704 e2 = e2->castTo(sc, t1); | |
7705 } | |
7706 } | |
7707 else if (t1->isimaginary()) | |
7708 { | |
7709 if (t2->isimaginary() || t2->iscomplex()) | |
7710 { | |
7711 switch (t1->ty) | |
7712 { | |
7713 case Timaginary32: t2 = Type::tfloat32; break; | |
7714 case Timaginary64: t2 = Type::tfloat64; break; | |
7715 case Timaginary80: t2 = Type::tfloat80; break; | |
7716 default: | |
7717 assert(0); | |
7718 } | |
7719 e2 = e2->castTo(sc, t2); | |
7720 } | |
7721 } | |
7722 } | |
7723 return this; | |
7724 } | |
7725 | |
7726 /************************************************************/ | |
7727 | |
7728 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7729 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
7730 { | |
7731 } | |
7732 | |
7733 Expression *DivAssignExp::semantic(Scope *sc) | |
7734 { Expression *e; | |
7735 | |
7736 BinExp::semantic(sc); | |
7737 e2 = resolveProperties(sc, e2); | |
7738 | |
7739 e = op_overload(sc); | |
7740 if (e) | |
7741 return e; | |
7742 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7743 if (e1->op == TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7744 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7745 typeCombine(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7746 type = e1->type; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7747 return arrayOp(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7748 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7749 |
159 | 7750 e1 = e1->modifiableLvalue(sc, e1); |
7751 e1->checkScalar(); | |
7752 e1->checkNoBool(); | |
7753 type = e1->type; | |
7754 typeCombine(sc); | |
7755 e1->checkArithmetic(); | |
7756 e2->checkArithmetic(); | |
7757 if (e2->type->isimaginary()) | |
7758 { Type *t1; | |
7759 Type *t2; | |
7760 | |
7761 t1 = e1->type; | |
7762 if (t1->isreal()) | |
7763 { // x/iv = i(-x/v) | |
7764 // Therefore, the result is 0 | |
7765 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
7766 e2->type = t1; | |
7767 e = new AssignExp(loc, e1, e2); | |
7768 e->type = t1; | |
7769 return e; | |
7770 } | |
7771 else if (t1->isimaginary()) | |
7772 { Expression *e; | |
7773 | |
7774 switch (t1->ty) | |
7775 { | |
7776 case Timaginary32: t2 = Type::tfloat32; break; | |
7777 case Timaginary64: t2 = Type::tfloat64; break; | |
7778 case Timaginary80: t2 = Type::tfloat80; break; | |
7779 default: | |
7780 assert(0); | |
7781 } | |
7782 e2 = e2->castTo(sc, t2); | |
7783 e = new AssignExp(loc, e1, e2); | |
7784 e->type = t1; | |
7785 return e; | |
7786 } | |
7787 } | |
7788 return this; | |
7789 } | |
7790 | |
7791 /************************************************************/ | |
7792 | |
7793 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7794 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
7795 { | |
7796 } | |
7797 | |
7798 Expression *ModAssignExp::semantic(Scope *sc) | |
7799 { | |
7800 return commonSemanticAssign(sc); | |
7801 } | |
7802 | |
7803 /************************************************************/ | |
7804 | |
7805 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7806 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
7807 { | |
7808 } | |
7809 | |
7810 Expression *ShlAssignExp::semantic(Scope *sc) | |
7811 { Expression *e; | |
7812 | |
7813 //printf("ShlAssignExp::semantic()\n"); | |
7814 BinExp::semantic(sc); | |
7815 e2 = resolveProperties(sc, e2); | |
7816 | |
7817 e = op_overload(sc); | |
7818 if (e) | |
7819 return e; | |
7820 | |
7821 e1 = e1->modifiableLvalue(sc, e1); | |
7822 e1->checkScalar(); | |
7823 e1->checkNoBool(); | |
7824 type = e1->type; | |
7825 typeCombine(sc); | |
7826 e1->checkIntegral(); | |
7827 e2 = e2->checkIntegral(); | |
7828 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
7829 e2 = e2->castTo(sc, e1->type); // LLVMDC | |
7830 return this; | |
7831 } | |
7832 | |
7833 /************************************************************/ | |
7834 | |
7835 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7836 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
7837 { | |
7838 } | |
7839 | |
7840 Expression *ShrAssignExp::semantic(Scope *sc) | |
7841 { Expression *e; | |
7842 | |
7843 BinExp::semantic(sc); | |
7844 e2 = resolveProperties(sc, e2); | |
7845 | |
7846 e = op_overload(sc); | |
7847 if (e) | |
7848 return e; | |
7849 | |
7850 e1 = e1->modifiableLvalue(sc, e1); | |
7851 e1->checkScalar(); | |
7852 e1->checkNoBool(); | |
7853 type = e1->type; | |
7854 typeCombine(sc); | |
7855 e1->checkIntegral(); | |
7856 e2 = e2->checkIntegral(); | |
7857 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
7858 e2 = e2->castTo(sc, e1->type); // LLVMDC | |
7859 return this; | |
7860 } | |
7861 | |
7862 /************************************************************/ | |
7863 | |
7864 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7865 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
7866 { | |
7867 } | |
7868 | |
7869 Expression *UshrAssignExp::semantic(Scope *sc) | |
7870 { Expression *e; | |
7871 | |
7872 BinExp::semantic(sc); | |
7873 e2 = resolveProperties(sc, e2); | |
7874 | |
7875 e = op_overload(sc); | |
7876 if (e) | |
7877 return e; | |
7878 | |
7879 e1 = e1->modifiableLvalue(sc, e1); | |
7880 e1->checkScalar(); | |
7881 e1->checkNoBool(); | |
7882 type = e1->type; | |
7883 typeCombine(sc); | |
7884 e1->checkIntegral(); | |
7885 e2 = e2->checkIntegral(); | |
7886 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
7887 e2 = e2->castTo(sc, e1->type); // LLVMDC | |
7888 return this; | |
7889 } | |
7890 | |
7891 /************************************************************/ | |
7892 | |
7893 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7894 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
7895 { | |
7896 } | |
7897 | |
7898 Expression *AndAssignExp::semantic(Scope *sc) | |
7899 { | |
7900 return commonSemanticAssignIntegral(sc); | |
7901 } | |
7902 | |
7903 /************************************************************/ | |
7904 | |
7905 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7906 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
7907 { | |
7908 } | |
7909 | |
7910 Expression *OrAssignExp::semantic(Scope *sc) | |
7911 { | |
7912 return commonSemanticAssignIntegral(sc); | |
7913 } | |
7914 | |
7915 /************************************************************/ | |
7916 | |
7917 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7918 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
7919 { | |
7920 } | |
7921 | |
7922 Expression *XorAssignExp::semantic(Scope *sc) | |
7923 { | |
7924 return commonSemanticAssignIntegral(sc); | |
7925 } | |
7926 | |
7927 /************************* AddExp *****************************/ | |
7928 | |
7929 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
7930 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
7931 { | |
7932 llvmFieldIndex = false; | |
7933 } | |
7934 | |
7935 Expression *AddExp::semantic(Scope *sc) | |
7936 { Expression *e; | |
7937 | |
7938 #if LOGSEMANTIC | |
7939 printf("AddExp::semantic('%s')\n", toChars()); | |
7940 #endif | |
7941 if (!type) | |
7942 { | |
7943 BinExp::semanticp(sc); | |
7944 | |
7945 e = op_overload(sc); | |
7946 if (e) | |
7947 return e; | |
7948 | |
7949 Type *tb1 = e1->type->toBasetype(); | |
7950 Type *tb2 = e2->type->toBasetype(); | |
7951 | |
7952 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
7953 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7954 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 7955 ) |
7956 { | |
7957 type = e1->type; | |
7958 e = this; | |
7959 } | |
7960 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
7961 tb2->ty == Tpointer && e1->type->isintegral()) | |
7962 e = scaleFactor(sc); | |
7963 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
7964 { | |
7965 incompatibleTypes(); | |
7966 type = e1->type; | |
7967 e = this; | |
7968 } | |
7969 else | |
7970 { | |
7971 typeCombine(sc); | |
7972 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
7973 (e1->type->isimaginary() && e2->type->isreal())) | |
7974 { | |
7975 switch (type->toBasetype()->ty) | |
7976 { | |
7977 case Tfloat32: | |
7978 case Timaginary32: | |
7979 type = Type::tcomplex32; | |
7980 break; | |
7981 | |
7982 case Tfloat64: | |
7983 case Timaginary64: | |
7984 type = Type::tcomplex64; | |
7985 break; | |
7986 | |
7987 case Tfloat80: | |
7988 case Timaginary80: | |
7989 type = Type::tcomplex80; | |
7990 break; | |
7991 | |
7992 default: | |
7993 assert(0); | |
7994 } | |
7995 } | |
7996 e = this; | |
7997 } | |
7998 return e; | |
7999 } | |
8000 return this; | |
8001 } | |
8002 | |
8003 /************************************************************/ | |
8004 | |
8005 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8006 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8007 { | |
8008 } | |
8009 | |
8010 Expression *MinExp::semantic(Scope *sc) | |
8011 { Expression *e; | |
8012 Type *t1; | |
8013 Type *t2; | |
8014 | |
8015 #if LOGSEMANTIC | |
8016 printf("MinExp::semantic('%s')\n", toChars()); | |
8017 #endif | |
8018 if (type) | |
8019 return this; | |
8020 | |
8021 BinExp::semanticp(sc); | |
8022 | |
8023 e = op_overload(sc); | |
8024 if (e) | |
8025 return e; | |
8026 | |
8027 e = this; | |
8028 t1 = e1->type->toBasetype(); | |
8029 t2 = e2->type->toBasetype(); | |
8030 if (t1->ty == Tpointer) | |
8031 { | |
8032 if (t2->ty == Tpointer) | |
8033 { // Need to divide the result by the stride | |
8034 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8035 d_int64 stride; | |
8036 Expression *e; | |
8037 | |
8038 typeCombine(sc); // make sure pointer types are compatible | |
8039 type = Type::tptrdiff_t; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8040 stride = t2->nextOf()->size(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8041 if (stride == 0) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8042 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8043 e = new IntegerExp(loc, 0, Type::tptrdiff_t); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8044 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8045 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8046 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8047 e = new DivExp(loc, this, new IntegerExp(0, stride, Type::tptrdiff_t)); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8048 e->type = Type::tptrdiff_t; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8049 } |
159 | 8050 return e; |
8051 } | |
8052 else if (t2->isintegral()) | |
8053 e = scaleFactor(sc); | |
8054 else | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8055 { error("incompatible types for minus"); |
159 | 8056 return new IntegerExp(0); |
8057 } | |
8058 } | |
8059 else if (t2->ty == Tpointer) | |
8060 { | |
8061 type = e2->type; | |
8062 error("can't subtract pointer from %s", e1->type->toChars()); | |
8063 return new IntegerExp(0); | |
8064 } | |
8065 else | |
8066 { | |
8067 typeCombine(sc); | |
8068 t1 = e1->type->toBasetype(); | |
8069 t2 = e2->type->toBasetype(); | |
8070 if ((t1->isreal() && t2->isimaginary()) || | |
8071 (t1->isimaginary() && t2->isreal())) | |
8072 { | |
8073 switch (type->ty) | |
8074 { | |
8075 case Tfloat32: | |
8076 case Timaginary32: | |
8077 type = Type::tcomplex32; | |
8078 break; | |
8079 | |
8080 case Tfloat64: | |
8081 case Timaginary64: | |
8082 type = Type::tcomplex64; | |
8083 break; | |
8084 | |
8085 case Tfloat80: | |
8086 case Timaginary80: | |
8087 type = Type::tcomplex80; | |
8088 break; | |
8089 | |
8090 default: | |
8091 assert(0); | |
8092 } | |
8093 } | |
8094 } | |
8095 return e; | |
8096 } | |
8097 | |
8098 /************************* CatExp *****************************/ | |
8099 | |
8100 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
8101 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
8102 { | |
8103 } | |
8104 | |
8105 Expression *CatExp::semantic(Scope *sc) | |
8106 { Expression *e; | |
8107 | |
8108 //printf("CatExp::semantic() %s\n", toChars()); | |
8109 if (!type) | |
8110 { | |
8111 BinExp::semanticp(sc); | |
8112 e = op_overload(sc); | |
8113 if (e) | |
8114 return e; | |
8115 | |
8116 Type *tb1 = e1->type->toBasetype(); | |
8117 Type *tb2 = e2->type->toBasetype(); | |
8118 | |
8119 | |
8120 /* BUG: Should handle things like: | |
8121 * char c; | |
8122 * c ~ ' ' | |
8123 * ' ' ~ c; | |
8124 */ | |
8125 | |
8126 #if 0 | |
8127 e1->type->print(); | |
8128 e2->type->print(); | |
8129 #endif | |
8130 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
8131 e2->type->equals(tb1->next)) | |
8132 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8133 type = tb1->nextOf()->arrayOf(); |
159 | 8134 if (tb2->ty == Tarray) |
8135 { // Make e2 into [e2] | |
8136 e2 = new ArrayLiteralExp(e2->loc, e2); | |
8137 e2->type = type; | |
8138 } | |
8139 return this; | |
8140 } | |
8141 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
8142 e1->type->equals(tb2->next)) | |
8143 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8144 type = tb2->nextOf()->arrayOf(); |
159 | 8145 if (tb1->ty == Tarray) |
8146 { // Make e1 into [e1] | |
8147 e1 = new ArrayLiteralExp(e1->loc, e1); | |
8148 e1->type = type; | |
8149 } | |
8150 return this; | |
8151 } | |
8152 | |
8153 typeCombine(sc); | |
8154 | |
8155 if (type->toBasetype()->ty == Tsarray) | |
8156 type = type->toBasetype()->next->arrayOf(); | |
8157 #if 0 | |
8158 e1->type->print(); | |
8159 e2->type->print(); | |
8160 type->print(); | |
8161 print(); | |
8162 #endif | |
8163 if (e1->op == TOKstring && e2->op == TOKstring) | |
8164 e = optimize(WANTvalue); | |
8165 else if (e1->type->equals(e2->type) && | |
8166 (e1->type->toBasetype()->ty == Tarray || | |
8167 e1->type->toBasetype()->ty == Tsarray)) | |
8168 { | |
8169 e = this; | |
8170 } | |
8171 else | |
8172 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8173 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 8174 error("Can only concatenate arrays, not (%s ~ %s)", |
8175 e1->type->toChars(), e2->type->toChars()); | |
8176 type = Type::tint32; | |
8177 e = this; | |
8178 } | |
8179 e->type = e->type->semantic(loc, sc); | |
8180 return e; | |
8181 } | |
8182 return this; | |
8183 } | |
8184 | |
8185 /************************************************************/ | |
8186 | |
8187 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
8188 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
8189 { | |
8190 } | |
8191 | |
8192 Expression *MulExp::semantic(Scope *sc) | |
8193 { Expression *e; | |
8194 | |
8195 #if 0 | |
8196 printf("MulExp::semantic() %s\n", toChars()); | |
8197 #endif | |
8198 if (type) | |
8199 { | |
8200 return this; | |
8201 } | |
8202 | |
8203 BinExp::semanticp(sc); | |
8204 e = op_overload(sc); | |
8205 if (e) | |
8206 return e; | |
8207 | |
8208 typeCombine(sc); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8209 if (e1->op != TOKslice && e2->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8210 { e1->checkArithmetic(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8211 e2->checkArithmetic(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8212 } |
159 | 8213 if (type->isfloating()) |
8214 { Type *t1 = e1->type; | |
8215 Type *t2 = e2->type; | |
8216 | |
8217 if (t1->isreal()) | |
8218 { | |
8219 type = t2; | |
8220 } | |
8221 else if (t2->isreal()) | |
8222 { | |
8223 type = t1; | |
8224 } | |
8225 else if (t1->isimaginary()) | |
8226 { | |
8227 if (t2->isimaginary()) | |
8228 { Expression *e; | |
8229 | |
8230 switch (t1->ty) | |
8231 { | |
8232 case Timaginary32: type = Type::tfloat32; break; | |
8233 case Timaginary64: type = Type::tfloat64; break; | |
8234 case Timaginary80: type = Type::tfloat80; break; | |
8235 default: assert(0); | |
8236 } | |
8237 | |
8238 // iy * iv = -yv | |
8239 e1->type = type; | |
8240 e2->type = type; | |
8241 e = new NegExp(loc, this); | |
8242 e = e->semantic(sc); | |
8243 return e; | |
8244 } | |
8245 else | |
8246 type = t2; // t2 is complex | |
8247 } | |
8248 else if (t2->isimaginary()) | |
8249 { | |
8250 type = t1; // t1 is complex | |
8251 } | |
8252 } | |
8253 return this; | |
8254 } | |
8255 | |
8256 /************************************************************/ | |
8257 | |
8258 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
8259 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
8260 { | |
8261 } | |
8262 | |
8263 Expression *DivExp::semantic(Scope *sc) | |
8264 { Expression *e; | |
8265 | |
8266 if (type) | |
8267 return this; | |
8268 | |
8269 BinExp::semanticp(sc); | |
8270 e = op_overload(sc); | |
8271 if (e) | |
8272 return e; | |
8273 | |
8274 typeCombine(sc); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8275 if (e1->op != TOKslice && e2->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8276 { e1->checkArithmetic(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8277 e2->checkArithmetic(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8278 } |
159 | 8279 if (type->isfloating()) |
8280 { Type *t1 = e1->type; | |
8281 Type *t2 = e2->type; | |
8282 | |
8283 if (t1->isreal()) | |
8284 { | |
8285 type = t2; | |
8286 if (t2->isimaginary()) | |
8287 { Expression *e; | |
8288 | |
8289 // x/iv = i(-x/v) | |
8290 e2->type = t1; | |
8291 e = new NegExp(loc, this); | |
8292 e = e->semantic(sc); | |
8293 return e; | |
8294 } | |
8295 } | |
8296 else if (t2->isreal()) | |
8297 { | |
8298 type = t1; | |
8299 } | |
8300 else if (t1->isimaginary()) | |
8301 { | |
8302 if (t2->isimaginary()) | |
8303 { | |
8304 switch (t1->ty) | |
8305 { | |
8306 case Timaginary32: type = Type::tfloat32; break; | |
8307 case Timaginary64: type = Type::tfloat64; break; | |
8308 case Timaginary80: type = Type::tfloat80; break; | |
8309 default: assert(0); | |
8310 } | |
8311 } | |
8312 else | |
8313 type = t2; // t2 is complex | |
8314 } | |
8315 else if (t2->isimaginary()) | |
8316 { | |
8317 type = t1; // t1 is complex | |
8318 } | |
8319 } | |
8320 return this; | |
8321 } | |
8322 | |
8323 /************************************************************/ | |
8324 | |
8325 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
8326 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
8327 { | |
8328 } | |
8329 | |
8330 Expression *ModExp::semantic(Scope *sc) | |
8331 { Expression *e; | |
8332 | |
8333 if (type) | |
8334 return this; | |
8335 | |
8336 BinExp::semanticp(sc); | |
8337 e = op_overload(sc); | |
8338 if (e) | |
8339 return e; | |
8340 | |
8341 typeCombine(sc); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8342 if (e1->op != TOKslice && e2->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8343 { e1->checkArithmetic(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8344 e2->checkArithmetic(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8345 } |
159 | 8346 if (type->isfloating()) |
8347 { type = e1->type; | |
8348 if (e2->type->iscomplex()) | |
8349 { error("cannot perform modulo complex arithmetic"); | |
8350 return new IntegerExp(0); | |
8351 } | |
8352 } | |
8353 return this; | |
8354 } | |
8355 | |
8356 /************************************************************/ | |
8357 | |
8358 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
8359 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
8360 { | |
8361 } | |
8362 | |
8363 Expression *ShlExp::semantic(Scope *sc) | |
8364 { Expression *e; | |
8365 | |
8366 //printf("ShlExp::semantic(), type = %p\n", type); | |
8367 if (!type) | |
8368 { BinExp::semanticp(sc); | |
8369 e = op_overload(sc); | |
8370 if (e) | |
8371 return e; | |
8372 e1 = e1->checkIntegral(); | |
8373 e2 = e2->checkIntegral(); | |
8374 e1 = e1->integralPromotions(sc); | |
8375 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
8376 e2 = e2->castTo(sc, e1->type); // LLVMDC | |
8377 type = e1->type; | |
8378 } | |
8379 return this; | |
8380 } | |
8381 | |
8382 /************************************************************/ | |
8383 | |
8384 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
8385 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
8386 { | |
8387 } | |
8388 | |
8389 Expression *ShrExp::semantic(Scope *sc) | |
8390 { Expression *e; | |
8391 | |
8392 if (!type) | |
8393 { BinExp::semanticp(sc); | |
8394 e = op_overload(sc); | |
8395 if (e) | |
8396 return e; | |
8397 e1 = e1->checkIntegral(); | |
8398 e2 = e2->checkIntegral(); | |
8399 e1 = e1->integralPromotions(sc); | |
8400 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
8401 e2 = e2->castTo(sc, e1->type); // LLVMDC | |
8402 type = e1->type; | |
8403 } | |
8404 return this; | |
8405 } | |
8406 | |
8407 /************************************************************/ | |
8408 | |
8409 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
8410 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
8411 { | |
8412 } | |
8413 | |
8414 Expression *UshrExp::semantic(Scope *sc) | |
8415 { Expression *e; | |
8416 | |
8417 if (!type) | |
8418 { BinExp::semanticp(sc); | |
8419 e = op_overload(sc); | |
8420 if (e) | |
8421 return e; | |
8422 e1 = e1->checkIntegral(); | |
8423 e2 = e2->checkIntegral(); | |
8424 e1 = e1->integralPromotions(sc); | |
8425 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
8426 e2 = e2->castTo(sc, e1->type); // LLVMDC | |
8427 type = e1->type; | |
8428 } | |
8429 return this; | |
8430 } | |
8431 | |
8432 /************************************************************/ | |
8433 | |
8434 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8435 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8436 { | |
8437 } | |
8438 | |
8439 Expression *AndExp::semantic(Scope *sc) | |
8440 { Expression *e; | |
8441 | |
8442 if (!type) | |
8443 { BinExp::semanticp(sc); | |
8444 e = op_overload(sc); | |
8445 if (e) | |
8446 return e; | |
8447 if (e1->type->toBasetype()->ty == Tbool && | |
8448 e2->type->toBasetype()->ty == Tbool) | |
8449 { | |
8450 type = e1->type; | |
8451 e = this; | |
8452 } | |
8453 else | |
8454 { | |
8455 typeCombine(sc); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8456 if (e1->op != TOKslice && e2->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8457 { e1->checkIntegral(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8458 e2->checkIntegral(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8459 } |
159 | 8460 } |
8461 } | |
8462 return this; | |
8463 } | |
8464 | |
8465 /************************************************************/ | |
8466 | |
8467 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8468 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8469 { | |
8470 } | |
8471 | |
8472 Expression *OrExp::semantic(Scope *sc) | |
8473 { Expression *e; | |
8474 | |
8475 if (!type) | |
8476 { BinExp::semanticp(sc); | |
8477 e = op_overload(sc); | |
8478 if (e) | |
8479 return e; | |
8480 if (e1->type->toBasetype()->ty == Tbool && | |
8481 e2->type->toBasetype()->ty == Tbool) | |
8482 { | |
8483 type = e1->type; | |
8484 e = this; | |
8485 } | |
8486 else | |
8487 { | |
8488 typeCombine(sc); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8489 if (e1->op != TOKslice && e2->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8490 { e1->checkIntegral(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8491 e2->checkIntegral(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8492 } |
159 | 8493 } |
8494 } | |
8495 return this; | |
8496 } | |
8497 | |
8498 /************************************************************/ | |
8499 | |
8500 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
8501 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
8502 { | |
8503 } | |
8504 | |
8505 Expression *XorExp::semantic(Scope *sc) | |
8506 { Expression *e; | |
8507 | |
8508 if (!type) | |
8509 { BinExp::semanticp(sc); | |
8510 e = op_overload(sc); | |
8511 if (e) | |
8512 return e; | |
8513 if (e1->type->toBasetype()->ty == Tbool && | |
8514 e2->type->toBasetype()->ty == Tbool) | |
8515 { | |
8516 type = e1->type; | |
8517 e = this; | |
8518 } | |
8519 else | |
8520 { | |
8521 typeCombine(sc); | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8522 if (e1->op != TOKslice && e2->op != TOKslice) |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8523 { e1->checkIntegral(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8524 e2->checkIntegral(); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8525 } |
159 | 8526 } |
8527 } | |
8528 return this; | |
8529 } | |
8530 | |
8531 | |
8532 /************************************************************/ | |
8533 | |
8534 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
8535 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
8536 { | |
8537 } | |
8538 | |
8539 Expression *OrOrExp::semantic(Scope *sc) | |
8540 { | |
8541 unsigned cs1; | |
8542 | |
8543 // same as for AndAnd | |
8544 e1 = e1->semantic(sc); | |
8545 e1 = resolveProperties(sc, e1); | |
8546 e1 = e1->checkToPointer(); | |
8547 e1 = e1->checkToBoolean(); | |
8548 cs1 = sc->callSuper; | |
8549 | |
8550 if (sc->flags & SCOPEstaticif) | |
8551 { | |
8552 /* If in static if, don't evaluate e2 if we don't have to. | |
8553 */ | |
8554 e1 = e1->optimize(WANTflags); | |
8555 if (e1->isBool(TRUE)) | |
8556 { | |
8557 return new IntegerExp(loc, 1, Type::tboolean); | |
8558 } | |
8559 } | |
8560 | |
8561 e2 = e2->semantic(sc); | |
8562 sc->mergeCallSuper(loc, cs1); | |
8563 e2 = resolveProperties(sc, e2); | |
8564 e2 = e2->checkToPointer(); | |
8565 | |
8566 type = Type::tboolean; | |
8567 if (e1->type->ty == Tvoid) | |
8568 type = Type::tvoid; | |
8569 if (e2->op == TOKtype || e2->op == TOKimport) | |
8570 error("%s is not an expression", e2->toChars()); | |
8571 return this; | |
8572 } | |
8573 | |
8574 Expression *OrOrExp::checkToBoolean() | |
8575 { | |
8576 e2 = e2->checkToBoolean(); | |
8577 return this; | |
8578 } | |
8579 | |
8580 int OrOrExp::isBit() | |
8581 { | |
8582 return TRUE; | |
8583 } | |
8584 | |
8585 int OrOrExp::checkSideEffect(int flag) | |
8586 { | |
8587 if (flag == 2) | |
8588 { | |
8589 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
8590 } | |
8591 else | |
8592 { e1->checkSideEffect(1); | |
8593 return e2->checkSideEffect(flag); | |
8594 } | |
8595 } | |
8596 | |
8597 /************************************************************/ | |
8598 | |
8599 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
8600 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
8601 { | |
8602 } | |
8603 | |
8604 Expression *AndAndExp::semantic(Scope *sc) | |
8605 { | |
8606 unsigned cs1; | |
8607 | |
8608 // same as for OrOr | |
8609 e1 = e1->semantic(sc); | |
8610 e1 = resolveProperties(sc, e1); | |
8611 e1 = e1->checkToPointer(); | |
8612 e1 = e1->checkToBoolean(); | |
8613 cs1 = sc->callSuper; | |
8614 | |
8615 if (sc->flags & SCOPEstaticif) | |
8616 { | |
8617 /* If in static if, don't evaluate e2 if we don't have to. | |
8618 */ | |
8619 e1 = e1->optimize(WANTflags); | |
8620 if (e1->isBool(FALSE)) | |
8621 { | |
8622 return new IntegerExp(loc, 0, Type::tboolean); | |
8623 } | |
8624 } | |
8625 | |
8626 e2 = e2->semantic(sc); | |
8627 sc->mergeCallSuper(loc, cs1); | |
8628 e2 = resolveProperties(sc, e2); | |
8629 e2 = e2->checkToPointer(); | |
8630 | |
8631 type = Type::tboolean; | |
8632 if (e1->type->ty == Tvoid) | |
8633 type = Type::tvoid; | |
8634 if (e2->op == TOKtype || e2->op == TOKimport) | |
8635 error("%s is not an expression", e2->toChars()); | |
8636 return this; | |
8637 } | |
8638 | |
8639 Expression *AndAndExp::checkToBoolean() | |
8640 { | |
8641 e2 = e2->checkToBoolean(); | |
8642 return this; | |
8643 } | |
8644 | |
8645 int AndAndExp::isBit() | |
8646 { | |
8647 return TRUE; | |
8648 } | |
8649 | |
8650 int AndAndExp::checkSideEffect(int flag) | |
8651 { | |
8652 if (flag == 2) | |
8653 { | |
8654 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
8655 } | |
8656 else | |
8657 { | |
8658 e1->checkSideEffect(1); | |
8659 return e2->checkSideEffect(flag); | |
8660 } | |
8661 } | |
8662 | |
8663 /************************************************************/ | |
8664 | |
8665 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
8666 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
8667 { | |
8668 } | |
8669 | |
8670 Expression *InExp::semantic(Scope *sc) | |
8671 { Expression *e; | |
8672 | |
8673 if (type) | |
8674 return this; | |
8675 | |
8676 BinExp::semanticp(sc); | |
8677 e = op_overload(sc); | |
8678 if (e) | |
8679 return e; | |
8680 | |
8681 //type = Type::tboolean; | |
8682 Type *t2b = e2->type->toBasetype(); | |
8683 if (t2b->ty != Taarray) | |
8684 { | |
8685 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
8686 type = Type::terror; | |
8687 } | |
8688 else | |
8689 { | |
8690 TypeAArray *ta = (TypeAArray *)t2b; | |
8691 | |
8692 // Convert key to type of key | |
8693 e1 = e1->implicitCastTo(sc, ta->index); | |
8694 | |
8695 // Return type is pointer to value | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8696 type = ta->nextOf()->pointerTo(); |
159 | 8697 } |
8698 return this; | |
8699 } | |
8700 | |
8701 int InExp::isBit() | |
8702 { | |
8703 return FALSE; | |
8704 } | |
8705 | |
8706 | |
8707 /************************************************************/ | |
8708 | |
8709 /* This deletes the key e1 from the associative array e2 | |
8710 */ | |
8711 | |
8712 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
8713 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
8714 { | |
8715 type = Type::tvoid; | |
8716 } | |
8717 | |
8718 /************************************************************/ | |
8719 | |
8720 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8721 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
8722 { | |
8723 } | |
8724 | |
8725 Expression *CmpExp::semantic(Scope *sc) | |
8726 { Expression *e; | |
8727 Type *t1; | |
8728 Type *t2; | |
8729 | |
8730 #if LOGSEMANTIC | |
8731 printf("CmpExp::semantic('%s')\n", toChars()); | |
8732 #endif | |
8733 if (type) | |
8734 return this; | |
8735 | |
8736 BinExp::semanticp(sc); | |
8737 | |
8738 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
8739 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
8740 { | |
8741 error("do not use null when comparing class types"); | |
8742 } | |
8743 | |
8744 e = op_overload(sc); | |
8745 if (e) | |
8746 { | |
8747 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
8748 e = e->semantic(sc); | |
8749 return e; | |
8750 } | |
8751 | |
8752 typeCombine(sc); | |
8753 type = Type::tboolean; | |
8754 | |
8755 // Special handling for array comparisons | |
8756 t1 = e1->type->toBasetype(); | |
8757 t2 = e2->type->toBasetype(); | |
8758 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
8759 (t2->ty == Tarray || t2->ty == Tsarray)) | |
8760 { | |
8761 if (!t1->next->equals(t2->next)) | |
8762 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
8763 e = this; | |
8764 } | |
8765 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
8766 (t1->ty == Tclass && t2->ty == Tclass)) | |
8767 { | |
8768 if (t2->ty == Tstruct) | |
8769 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
8770 else | |
8771 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
8772 e = this; | |
8773 } | |
8774 #if 1 | |
8775 else if (t1->iscomplex() || t2->iscomplex()) | |
8776 { | |
8777 error("compare not defined for complex operands"); | |
8778 e = new IntegerExp(0); | |
8779 } | |
8780 #endif | |
8781 else | |
8782 e = this; | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8783 //printf("CmpExp: %s\n", e->toChars()); |
159 | 8784 return e; |
8785 } | |
8786 | |
8787 int CmpExp::isBit() | |
8788 { | |
8789 return TRUE; | |
8790 } | |
8791 | |
8792 | |
8793 /************************************************************/ | |
8794 | |
8795 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8796 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
8797 { | |
8798 assert(op == TOKequal || op == TOKnotequal); | |
8799 } | |
8800 | |
8801 Expression *EqualExp::semantic(Scope *sc) | |
8802 { Expression *e; | |
8803 Type *t1; | |
8804 Type *t2; | |
8805 | |
8806 //printf("EqualExp::semantic('%s')\n", toChars()); | |
8807 if (type) | |
8808 return this; | |
8809 | |
8810 BinExp::semanticp(sc); | |
8811 | |
8812 /* Before checking for operator overloading, check to see if we're | |
8813 * comparing the addresses of two statics. If so, we can just see | |
8814 * if they are the same symbol. | |
8815 */ | |
8816 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
8817 { AddrExp *ae1 = (AddrExp *)e1; | |
8818 AddrExp *ae2 = (AddrExp *)e2; | |
8819 | |
8820 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
8821 { VarExp *ve1 = (VarExp *)ae1->e1; | |
8822 VarExp *ve2 = (VarExp *)ae2->e1; | |
8823 | |
8824 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
8825 { | |
8826 // They are the same, result is 'true' for ==, 'false' for != | |
8827 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
8828 return e; | |
8829 } | |
8830 } | |
8831 } | |
8832 | |
8833 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
8834 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
8835 { | |
8836 error("use '%s' instead of '%s' when comparing with null", | |
8837 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
8838 Token::toChars(op)); | |
8839 } | |
8840 | |
8841 //if (e2->op != TOKnull) | |
8842 { | |
8843 e = op_overload(sc); | |
8844 if (e) | |
8845 { | |
8846 if (op == TOKnotequal) | |
8847 { | |
8848 e = new NotExp(e->loc, e); | |
8849 e = e->semantic(sc); | |
8850 } | |
8851 return e; | |
8852 } | |
8853 } | |
8854 | |
8855 e = typeCombine(sc); | |
8856 type = Type::tboolean; | |
8857 | |
8858 // Special handling for array comparisons | |
8859 t1 = e1->type->toBasetype(); | |
8860 t2 = e2->type->toBasetype(); | |
8861 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
8862 (t2->ty == Tarray || t2->ty == Tsarray)) | |
8863 { | |
8864 if (!t1->next->equals(t2->next)) | |
8865 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
8866 } | |
8867 else | |
8868 { | |
8869 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
8870 { | |
8871 // Cast both to complex | |
8872 e1 = e1->castTo(sc, Type::tcomplex80); | |
8873 e2 = e2->castTo(sc, Type::tcomplex80); | |
8874 } | |
8875 } | |
8876 return e; | |
8877 } | |
8878 | |
8879 int EqualExp::isBit() | |
8880 { | |
8881 return TRUE; | |
8882 } | |
8883 | |
8884 | |
8885 | |
8886 /************************************************************/ | |
8887 | |
8888 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8889 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
8890 { | |
8891 } | |
8892 | |
8893 Expression *IdentityExp::semantic(Scope *sc) | |
8894 { | |
8895 if (type) | |
8896 return this; | |
8897 | |
8898 BinExp::semanticp(sc); | |
8899 type = Type::tboolean; | |
8900 typeCombine(sc); | |
8901 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
8902 { | |
8903 // Cast both to complex | |
8904 e1 = e1->castTo(sc, Type::tcomplex80); | |
8905 e2 = e2->castTo(sc, Type::tcomplex80); | |
8906 } | |
8907 return this; | |
8908 } | |
8909 | |
8910 int IdentityExp::isBit() | |
8911 { | |
8912 return TRUE; | |
8913 } | |
8914 | |
8915 | |
8916 /****************************************************************/ | |
8917 | |
8918 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
8919 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
8920 { | |
8921 this->econd = econd; | |
8922 } | |
8923 | |
8924 Expression *CondExp::syntaxCopy() | |
8925 { | |
8926 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
8927 } | |
8928 | |
8929 | |
8930 Expression *CondExp::semantic(Scope *sc) | |
8931 { Type *t1; | |
8932 Type *t2; | |
8933 unsigned cs0; | |
8934 unsigned cs1; | |
8935 | |
8936 #if LOGSEMANTIC | |
8937 printf("CondExp::semantic('%s')\n", toChars()); | |
8938 #endif | |
8939 if (type) | |
8940 return this; | |
8941 | |
8942 econd = econd->semantic(sc); | |
8943 econd = resolveProperties(sc, econd); | |
8944 econd = econd->checkToPointer(); | |
8945 econd = econd->checkToBoolean(); | |
8946 | |
8947 #if 0 /* this cannot work right because the types of e1 and e2 | |
8948 * both contribute to the type of the result. | |
8949 */ | |
8950 if (sc->flags & SCOPEstaticif) | |
8951 { | |
8952 /* If in static if, don't evaluate what we don't have to. | |
8953 */ | |
8954 econd = econd->optimize(WANTflags); | |
8955 if (econd->isBool(TRUE)) | |
8956 { | |
8957 e1 = e1->semantic(sc); | |
8958 e1 = resolveProperties(sc, e1); | |
8959 return e1; | |
8960 } | |
8961 else if (econd->isBool(FALSE)) | |
8962 { | |
8963 e2 = e2->semantic(sc); | |
8964 e2 = resolveProperties(sc, e2); | |
8965 return e2; | |
8966 } | |
8967 } | |
8968 #endif | |
8969 | |
8970 | |
8971 cs0 = sc->callSuper; | |
8972 e1 = e1->semantic(sc); | |
8973 e1 = resolveProperties(sc, e1); | |
8974 cs1 = sc->callSuper; | |
8975 sc->callSuper = cs0; | |
8976 e2 = e2->semantic(sc); | |
8977 e2 = resolveProperties(sc, e2); | |
8978 sc->mergeCallSuper(loc, cs1); | |
8979 | |
8980 | |
8981 // If either operand is void, the result is void | |
8982 t1 = e1->type; | |
8983 t2 = e2->type; | |
8984 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
8985 type = Type::tvoid; | |
8986 else if (t1 == t2) | |
8987 type = t1; | |
8988 else | |
8989 { | |
8990 typeCombine(sc); | |
8991 switch (e1->type->toBasetype()->ty) | |
8992 { | |
8993 case Tcomplex32: | |
8994 case Tcomplex64: | |
8995 case Tcomplex80: | |
8996 e2 = e2->castTo(sc, e1->type); | |
8997 break; | |
8998 } | |
8999 switch (e2->type->toBasetype()->ty) | |
9000 { | |
9001 case Tcomplex32: | |
9002 case Tcomplex64: | |
9003 case Tcomplex80: | |
9004 e1 = e1->castTo(sc, e2->type); | |
9005 break; | |
9006 } | |
9007 } | |
9008 return this; | |
9009 } | |
9010 | |
9011 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) | |
9012 { | |
9013 PtrExp *e; | |
9014 | |
9015 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
9016 e = new PtrExp(loc, this, type); | |
9017 | |
9018 e1 = e1->addressOf(sc); | |
9019 //e1 = e1->toLvalue(sc, NULL); | |
9020 | |
9021 e2 = e2->addressOf(sc); | |
9022 //e2 = e2->toLvalue(sc, NULL); | |
9023 | |
9024 typeCombine(sc); | |
9025 | |
9026 type = e2->type; | |
9027 return e; | |
9028 } | |
9029 | |
9030 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
9031 { | |
9032 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
9033 return this; | |
9034 } | |
9035 | |
9036 void CondExp::checkEscape() | |
9037 { | |
9038 e1->checkEscape(); | |
9039 e2->checkEscape(); | |
9040 } | |
9041 | |
9042 | |
9043 Expression *CondExp::checkToBoolean() | |
9044 { | |
9045 e1 = e1->checkToBoolean(); | |
9046 e2 = e2->checkToBoolean(); | |
9047 return this; | |
9048 } | |
9049 | |
9050 int CondExp::checkSideEffect(int flag) | |
9051 { | |
9052 if (flag == 2) | |
9053 { | |
9054 return econd->checkSideEffect(2) || | |
9055 e1->checkSideEffect(2) || | |
9056 e2->checkSideEffect(2); | |
9057 } | |
9058 else | |
9059 { | |
9060 econd->checkSideEffect(1); | |
9061 e1->checkSideEffect(flag); | |
9062 return e2->checkSideEffect(flag); | |
9063 } | |
9064 } | |
9065 | |
9066 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
9067 { | |
9068 expToCBuffer(buf, hgs, econd, PREC_oror); | |
9069 buf->writestring(" ? "); | |
9070 expToCBuffer(buf, hgs, e1, PREC_expr); | |
9071 buf->writestring(" : "); | |
9072 expToCBuffer(buf, hgs, e2, PREC_cond); | |
9073 } | |
9074 | |
9075 |