Mercurial > projects > ldc
annotate dmd/expression.c @ 687:0a682c7805d2
Emit region.end debug calls before a throw.
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Sun, 12 Oct 2008 15:30:58 +0200 |
parents | eef8ac26c66c |
children | a26b0c5d5942 |
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 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
270 // LDC seems dmd misses it sometimes here :/ |
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
|
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(); | |
639
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3373 FuncDeclaration *fdn = s->isFuncDeclaration(); |
159 | 3374 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3375 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3376 if (cdn) |
3377 { | |
3378 if (!cdthis) | |
3379 { | |
3380 // Supply an implicit 'this' and try again | |
3381 thisexp = new ThisExp(loc); | |
3382 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3383 { if (!sp) | |
3384 { | |
3385 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3386 break; | |
3387 } | |
3388 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3389 if (!cdp) | |
3390 continue; | |
3391 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3392 break; | |
3393 // Add a '.outer' and try again | |
3394 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3395 } | |
3396 if (!global.errors) | |
3397 goto Lagain; | |
3398 } | |
3399 if (cdthis) | |
3400 { | |
3401 //printf("cdthis = %s\n", cdthis->toChars()); | |
3402 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3403 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3404 } | |
3405 #if 0 | |
3406 else | |
3407 { | |
3408 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3409 { | |
3410 if (!sf) | |
3411 { | |
3412 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3413 break; | |
3414 } | |
3415 printf("sf = %s\n", sf->toChars()); | |
3416 AggregateDeclaration *ad = sf->isThis(); | |
3417 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3418 break; | |
3419 } | |
3420 } | |
3421 #endif | |
3422 } | |
3423 else if (thisexp) | |
3424 error("e.new is only for allocating nested classes"); | |
639
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3425 else if (fdn) |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3426 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3427 // make sure the parent context fdn of cd is reachable from sc |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3428 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3429 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3430 if (fdn == sp) |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3431 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3432 FuncDeclaration *fsp = sp ? sp->isFuncDeclaration() : NULL; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3433 if (!sp || (fsp && fsp->isStatic())) |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3434 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3435 error("outer function context of %s is needed to 'new' nested class %s", fdn->toPrettyChars(), cd->toPrettyChars()); |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3436 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3437 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3438 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3439 |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3440 } |
159 | 3441 } |
3442 else if (thisexp) | |
3443 error("e.new is only for allocating nested classes"); | |
3444 | |
3445 FuncDeclaration *f = cd->ctor; | |
3446 if (f) | |
3447 { | |
3448 assert(f); | |
3449 f = f->overloadResolve(loc, arguments); | |
3450 checkDeprecated(sc, f); | |
3451 member = f->isCtorDeclaration(); | |
3452 assert(member); | |
3453 | |
3454 cd->accessCheck(loc, sc, member); | |
3455 | |
3456 tf = (TypeFunction *)f->type; | |
3457 type = tf->next; | |
3458 | |
3459 if (!arguments) | |
3460 arguments = new Expressions(); | |
3461 functionArguments(loc, sc, tf, arguments); | |
3462 } | |
3463 else | |
3464 { | |
3465 if (arguments && arguments->dim) | |
3466 error("no constructor for %s", cd->toChars()); | |
3467 } | |
3468 | |
3469 if (cd->aggNew) | |
3470 { Expression *e; | |
3471 | |
3472 f = cd->aggNew; | |
3473 | |
3474 // Prepend the uint size argument to newargs[] | |
3475 e = new IntegerExp(loc, cd->size(loc), Type::tuns32); | |
3476 if (!newargs) | |
3477 newargs = new Expressions(); | |
3478 newargs->shift(e); | |
3479 | |
3480 f = f->overloadResolve(loc, newargs); | |
3481 allocator = f->isNewDeclaration(); | |
3482 assert(allocator); | |
3483 | |
3484 tf = (TypeFunction *)f->type; | |
3485 functionArguments(loc, sc, tf, newargs); | |
3486 } | |
3487 else | |
3488 { | |
3489 if (newargs && newargs->dim) | |
3490 error("no allocator for %s", cd->toChars()); | |
3491 } | |
3492 | |
3493 } | |
3494 else if (tb->ty == Tstruct) | |
3495 { | |
3496 TypeStruct *ts = (TypeStruct *)tb; | |
3497 StructDeclaration *sd = ts->sym; | |
3498 FuncDeclaration *f = sd->aggNew; | |
3499 TypeFunction *tf; | |
3500 | |
3501 if (arguments && arguments->dim) | |
3502 error("no constructor for %s", type->toChars()); | |
3503 | |
3504 if (f) | |
3505 { | |
3506 Expression *e; | |
3507 | |
3508 // Prepend the uint size argument to newargs[] | |
3509 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3510 if (!newargs) | |
3511 newargs = new Expressions(); | |
3512 newargs->shift(e); | |
3513 | |
3514 f = f->overloadResolve(loc, newargs); | |
3515 allocator = f->isNewDeclaration(); | |
3516 assert(allocator); | |
3517 | |
3518 tf = (TypeFunction *)f->type; | |
3519 functionArguments(loc, sc, tf, newargs); | |
3520 | |
3521 e = new VarExp(loc, f); | |
3522 e = new CallExp(loc, e, newargs); | |
3523 e = e->semantic(sc); | |
3524 e->type = type->pointerTo(); | |
3525 return e; | |
3526 } | |
3527 | |
3528 type = type->pointerTo(); | |
3529 } | |
3530 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3531 { | |
3532 for (size_t i = 0; i < arguments->dim; i++) | |
3533 { | |
3534 if (tb->ty != Tarray) | |
3535 { error("too many arguments for array"); | |
3536 arguments->dim = i; | |
3537 break; | |
3538 } | |
3539 | |
3540 Expression *arg = (Expression *)arguments->data[i]; | |
3541 arg = resolveProperties(sc, arg); | |
3542 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3543 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3544 error("negative array index %s", arg->toChars()); | |
3545 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
|
3546 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3547 } |
3548 } | |
3549 else if (tb->isscalar()) | |
3550 { | |
3551 if (arguments && arguments->dim) | |
3552 error("no constructor for %s", type->toChars()); | |
3553 | |
3554 type = type->pointerTo(); | |
3555 } | |
3556 else | |
3557 { | |
3558 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3559 type = type->pointerTo(); | |
3560 } | |
3561 | |
3562 //printf("NewExp: '%s'\n", toChars()); | |
3563 //printf("NewExp:type '%s'\n", type->toChars()); | |
3564 | |
3565 return this; | |
3566 } | |
3567 | |
3568 int NewExp::checkSideEffect(int flag) | |
3569 { | |
3570 return 1; | |
3571 } | |
3572 | |
3573 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3574 { int i; | |
3575 | |
3576 if (thisexp) | |
3577 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3578 buf->writeByte('.'); | |
3579 } | |
3580 buf->writestring("new "); | |
3581 if (newargs && newargs->dim) | |
3582 { | |
3583 buf->writeByte('('); | |
3584 argsToCBuffer(buf, newargs, hgs); | |
3585 buf->writeByte(')'); | |
3586 } | |
3587 newtype->toCBuffer(buf, NULL, hgs); | |
3588 if (arguments && arguments->dim) | |
3589 { | |
3590 buf->writeByte('('); | |
3591 argsToCBuffer(buf, arguments, hgs); | |
3592 buf->writeByte(')'); | |
3593 } | |
3594 } | |
3595 | |
3596 /********************** NewAnonClassExp **************************************/ | |
3597 | |
3598 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3599 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3600 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3601 { | |
3602 this->thisexp = thisexp; | |
3603 this->newargs = newargs; | |
3604 this->cd = cd; | |
3605 this->arguments = arguments; | |
3606 } | |
3607 | |
3608 Expression *NewAnonClassExp::syntaxCopy() | |
3609 { | |
3610 return new NewAnonClassExp(loc, | |
3611 thisexp ? thisexp->syntaxCopy() : NULL, | |
3612 arraySyntaxCopy(newargs), | |
3613 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3614 arraySyntaxCopy(arguments)); | |
3615 } | |
3616 | |
3617 | |
3618 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3619 { | |
3620 #if LOGSEMANTIC | |
3621 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3622 //printf("thisexp = %p\n", thisexp); |
159 | 3623 //printf("type: %s\n", type->toChars()); |
3624 #endif | |
3625 | |
3626 Expression *d = new DeclarationExp(loc, cd); | |
3627 d = d->semantic(sc); | |
3628 | |
3629 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3630 | |
3631 Expression *c = new CommaExp(loc, d, n); | |
3632 return c->semantic(sc); | |
3633 } | |
3634 | |
3635 int NewAnonClassExp::checkSideEffect(int flag) | |
3636 { | |
3637 return 1; | |
3638 } | |
3639 | |
3640 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3641 { int i; | |
3642 | |
3643 if (thisexp) | |
3644 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3645 buf->writeByte('.'); | |
3646 } | |
3647 buf->writestring("new"); | |
3648 if (newargs && newargs->dim) | |
3649 { | |
3650 buf->writeByte('('); | |
3651 argsToCBuffer(buf, newargs, hgs); | |
3652 buf->writeByte(')'); | |
3653 } | |
3654 buf->writestring(" class "); | |
3655 if (arguments && arguments->dim) | |
3656 { | |
3657 buf->writeByte('('); | |
3658 argsToCBuffer(buf, arguments, hgs); | |
3659 buf->writeByte(')'); | |
3660 } | |
3661 //buf->writestring(" { }"); | |
3662 if (cd) | |
3663 { | |
3664 cd->toCBuffer(buf, hgs); | |
3665 } | |
3666 } | |
3667 | |
3668 /********************** SymOffExp **************************************/ | |
3669 | |
3670 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3671 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3672 { | |
3673 assert(var); | |
3674 this->var = var; | |
3675 this->offset = offset; | |
3676 VarDeclaration *v = var->isVarDeclaration(); | |
3677 if (v && v->needThis()) | |
3678 error("need 'this' for address of %s", v->toChars()); | |
3679 } | |
3680 | |
3681 Expression *SymOffExp::semantic(Scope *sc) | |
3682 { | |
3683 #if LOGSEMANTIC | |
3684 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3685 #endif | |
3686 //var->semantic(sc); | |
3687 if (!type) | |
3688 type = var->type->pointerTo(); | |
3689 VarDeclaration *v = var->isVarDeclaration(); | |
3690 if (v) | |
3691 { | |
3692 v->checkNestedReference(sc, loc); | |
3693 } | |
3694 return this; | |
3695 } | |
3696 | |
3697 int SymOffExp::isBool(int result) | |
3698 { | |
3699 return result ? TRUE : FALSE; | |
3700 } | |
3701 | |
3702 void SymOffExp::checkEscape() | |
3703 { | |
3704 VarDeclaration *v = var->isVarDeclaration(); | |
3705 if (v) | |
3706 { | |
3707 if (!v->isDataseg()) | |
3708 error("escaping reference to local variable %s", v->toChars()); | |
3709 } | |
3710 } | |
3711 | |
3712 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3713 { | |
3714 if (offset) | |
3715 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3716 else | |
3717 buf->printf("& %s", var->toChars()); | |
3718 } | |
3719 | |
3720 /******************************** VarExp **************************/ | |
3721 | |
3722 VarExp::VarExp(Loc loc, Declaration *var) | |
3723 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3724 { | |
3725 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3726 this->var = var; | |
3727 this->type = var->type; | |
3728 } | |
3729 | |
3730 int VarExp::equals(Object *o) | |
3731 { VarExp *ne; | |
3732 | |
3733 if (this == o || | |
3734 (((Expression *)o)->op == TOKvar && | |
3735 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3736 var == ne->var)) | |
3737 return 1; | |
3738 return 0; | |
3739 } | |
3740 | |
3741 Expression *VarExp::semantic(Scope *sc) | |
3742 { FuncLiteralDeclaration *fd; | |
3743 | |
3744 #if LOGSEMANTIC | |
3745 printf("VarExp::semantic(%s)\n", toChars()); | |
3746 #endif | |
3747 if (!type) | |
3748 { type = var->type; | |
3749 #if 0 | |
3750 if (var->storage_class & STClazy) | |
3751 { | |
3752 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3753 type = new TypeDelegate(tf); | |
3754 type = type->semantic(loc, sc); | |
3755 } | |
3756 #endif | |
3757 } | |
3758 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
3759 // LDC: Fixes bug 1161, http://d.puremagic.com/issues/show_bug.cgi?id=1161 |
163
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
159
diff
changeset
|
3760 // check access to VarDeclaration |
a8cd9bc1021a
[svn r179] lots and lots of fixes, much more of tango now compiles/works.
lindquist
parents:
159
diff
changeset
|
3761 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
|
3762 |
159 | 3763 VarDeclaration *v = var->isVarDeclaration(); |
3764 if (v) | |
3765 { | |
3766 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3767 { | |
3768 ExpInitializer *ei = v->init->isExpInitializer(); | |
3769 if (ei) | |
3770 { | |
3771 //ei->exp->implicitCastTo(sc, type)->print(); | |
3772 return ei->exp->implicitCastTo(sc, type); | |
3773 } | |
3774 } | |
3775 v->checkNestedReference(sc, loc); | |
3776 } | |
3777 #if 0 | |
3778 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3779 { Expression *e; | |
3780 e = new FuncExp(loc, fd); | |
3781 e->type = type; | |
3782 return e; | |
3783 } | |
3784 #endif | |
3785 return this; | |
3786 } | |
3787 | |
3788 char *VarExp::toChars() | |
3789 { | |
3790 return var->toChars(); | |
3791 } | |
3792 | |
3793 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3794 { | |
3795 buf->writestring(var->toChars()); | |
3796 } | |
3797 | |
3798 void VarExp::checkEscape() | |
3799 { | |
3800 VarDeclaration *v = var->isVarDeclaration(); | |
3801 if (v) | |
3802 { Type *tb = v->type->toBasetype(); | |
3803 // if reference type | |
3804 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
3805 { | |
3806 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
3807 error("escaping reference to auto local %s", v->toChars()); | |
3808 else if (v->storage_class & STCvariadic) | |
3809 error("escaping reference to variadic parameter %s", v->toChars()); | |
3810 } | |
3811 } | |
3812 } | |
3813 | |
3814 Expression *VarExp::toLvalue(Scope *sc, Expression *e) | |
3815 { | |
3816 #if 0 | |
3817 tym = tybasic(e1->ET->Tty); | |
3818 if (!(tyscalar(tym) || | |
3819 tym == TYstruct || | |
3820 tym == TYarray && e->Eoper == TOKaddr)) | |
3821 synerr(EM_lvalue); // lvalue expected | |
3822 #endif | |
3823 if (var->storage_class & STClazy) | |
3824 error("lazy variables cannot be lvalues"); | |
3825 return this; | |
3826 } | |
3827 | |
3828 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
3829 { | |
3830 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
3831 if (sc->incontract && var->isParameter()) | |
3832 error("cannot modify parameter '%s' in contract", var->toChars()); | |
3833 | |
3834 if (type && type->toBasetype()->ty == Tsarray) | |
3835 error("cannot change reference to static array '%s'", var->toChars()); | |
3836 | |
3837 VarDeclaration *v = var->isVarDeclaration(); | |
3838 if (v && v->canassign == 0 && | |
3839 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
3840 error("cannot modify final variable '%s'", var->toChars()); | |
3841 | |
3842 if (var->isCtorinit()) | |
3843 { // It's only modifiable if inside the right constructor | |
3844 Dsymbol *s = sc->func; | |
3845 while (1) | |
3846 { | |
3847 FuncDeclaration *fd = NULL; | |
3848 if (s) | |
3849 fd = s->isFuncDeclaration(); | |
3850 if (fd && | |
3851 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
3852 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
3853 fd->toParent() == var->toParent() | |
3854 ) | |
3855 { | |
3856 VarDeclaration *v = var->isVarDeclaration(); | |
3857 assert(v); | |
3858 v->ctorinit = 1; | |
3859 //printf("setting ctorinit\n"); | |
3860 } | |
3861 else | |
3862 { | |
3863 if (s) | |
3864 { s = s->toParent2(); | |
3865 continue; | |
3866 } | |
3867 else | |
3868 { | |
3869 const char *p = var->isStatic() ? "static " : ""; | |
3870 error("can only initialize %sconst %s inside %sconstructor", | |
3871 p, var->toChars(), p); | |
3872 } | |
3873 } | |
3874 break; | |
3875 } | |
3876 } | |
3877 | |
3878 // See if this expression is a modifiable lvalue (i.e. not const) | |
3879 return toLvalue(sc, e); | |
3880 } | |
3881 | |
3882 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3883 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3884 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3885 #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
|
3886 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
|
3887 : 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
|
3888 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3889 //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
|
3890 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
|
3891 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
|
3892 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3893 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3894 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
|
3895 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3896 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
|
3897 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3898 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3899 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
|
3900 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3901 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
|
3902 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3903 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3904 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3905 |
159 | 3906 /******************************** TupleExp **************************/ |
3907 | |
3908 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
3909 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
3910 { | |
3911 //printf("TupleExp(this = %p)\n", this); | |
3912 this->exps = exps; | |
3913 this->type = NULL; | |
3914 } | |
3915 | |
3916 | |
3917 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
3918 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
3919 { | |
3920 exps = new Expressions(); | |
3921 type = NULL; | |
3922 | |
3923 exps->reserve(tup->objects->dim); | |
3924 for (size_t i = 0; i < tup->objects->dim; i++) | |
3925 { Object *o = (Object *)tup->objects->data[i]; | |
3926 if (o->dyncast() == DYNCAST_EXPRESSION) | |
3927 { | |
3928 Expression *e = (Expression *)o; | |
3929 e = e->syntaxCopy(); | |
3930 exps->push(e); | |
3931 } | |
3932 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
3933 { | |
3934 Dsymbol *s = (Dsymbol *)o; | |
3935 Expression *e = new DsymbolExp(loc, s); | |
3936 exps->push(e); | |
3937 } | |
3938 else if (o->dyncast() == DYNCAST_TYPE) | |
3939 { | |
3940 Type *t = (Type *)o; | |
3941 Expression *e = new TypeExp(loc, t); | |
3942 exps->push(e); | |
3943 } | |
3944 else | |
3945 { | |
3946 error("%s is not an expression", o->toChars()); | |
3947 } | |
3948 } | |
3949 } | |
3950 | |
3951 int TupleExp::equals(Object *o) | |
3952 { TupleExp *ne; | |
3953 | |
3954 if (this == o) | |
3955 return 1; | |
3956 if (((Expression *)o)->op == TOKtuple) | |
3957 { | |
3958 TupleExp *te = (TupleExp *)o; | |
3959 if (exps->dim != te->exps->dim) | |
3960 return 0; | |
3961 for (size_t i = 0; i < exps->dim; i++) | |
3962 { Expression *e1 = (Expression *)exps->data[i]; | |
3963 Expression *e2 = (Expression *)te->exps->data[i]; | |
3964 | |
3965 if (!e1->equals(e2)) | |
3966 return 0; | |
3967 } | |
3968 return 1; | |
3969 } | |
3970 return 0; | |
3971 } | |
3972 | |
3973 Expression *TupleExp::syntaxCopy() | |
3974 { | |
3975 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
3976 } | |
3977 | |
3978 Expression *TupleExp::semantic(Scope *sc) | |
3979 { | |
3980 #if LOGSEMANTIC | |
3981 printf("+TupleExp::semantic(%s)\n", toChars()); | |
3982 #endif | |
3983 if (type) | |
3984 return this; | |
3985 | |
3986 // Run semantic() on each argument | |
3987 for (size_t i = 0; i < exps->dim; i++) | |
3988 { Expression *e = (Expression *)exps->data[i]; | |
3989 | |
3990 e = e->semantic(sc); | |
3991 if (!e->type) | |
3992 { error("%s has no value", e->toChars()); | |
3993 e->type = Type::terror; | |
3994 } | |
3995 exps->data[i] = (void *)e; | |
3996 } | |
3997 | |
3998 expandTuples(exps); | |
3999 if (0 && exps->dim == 1) | |
4000 { | |
4001 return (Expression *)exps->data[0]; | |
4002 } | |
4003 type = new TypeTuple(exps); | |
4004 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
4005 return this; | |
4006 } | |
4007 | |
4008 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4009 { | |
4010 buf->writestring("tuple("); | |
4011 argsToCBuffer(buf, exps, hgs); | |
4012 buf->writeByte(')'); | |
4013 } | |
4014 | |
4015 int TupleExp::checkSideEffect(int flag) | |
4016 { int f = 0; | |
4017 | |
4018 for (int i = 0; i < exps->dim; i++) | |
4019 { Expression *e = (Expression *)exps->data[i]; | |
4020 | |
4021 f |= e->checkSideEffect(2); | |
4022 } | |
4023 if (flag == 0 && f == 0) | |
4024 Expression::checkSideEffect(0); | |
4025 return f; | |
4026 } | |
4027 | |
4028 void TupleExp::checkEscape() | |
4029 { | |
4030 for (size_t i = 0; i < exps->dim; i++) | |
4031 { Expression *e = (Expression *)exps->data[i]; | |
4032 e->checkEscape(); | |
4033 } | |
4034 } | |
4035 | |
4036 /******************************** FuncExp *********************************/ | |
4037 | |
4038 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4039 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4040 { | |
4041 this->fd = fd; | |
4042 } | |
4043 | |
4044 Expression *FuncExp::syntaxCopy() | |
4045 { | |
4046 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4047 } | |
4048 | |
4049 Expression *FuncExp::semantic(Scope *sc) | |
4050 { | |
4051 #if LOGSEMANTIC | |
4052 printf("FuncExp::semantic(%s)\n", toChars()); | |
4053 #endif | |
4054 if (!type) | |
4055 { | |
4056 fd->semantic(sc); | |
4057 fd->parent = sc->parent; | |
4058 if (global.errors) | |
4059 { | |
4060 if (!fd->type->next) | |
4061 fd->type->next = Type::terror; | |
4062 } | |
4063 else | |
4064 { | |
4065 fd->semantic2(sc); | |
4066 if (!global.errors) | |
4067 { | |
4068 fd->semantic3(sc); | |
4069 | |
4070 if (!global.errors && global.params.useInline) | |
4071 fd->inlineScan(); | |
4072 } | |
4073 } | |
4074 | |
4075 // Type is a "delegate to" or "pointer to" the function literal | |
4076 if (fd->isNested()) | |
4077 { | |
4078 type = new TypeDelegate(fd->type); | |
4079 type = type->semantic(loc, sc); | |
4080 } | |
4081 else | |
4082 { | |
4083 type = fd->type->pointerTo(); | |
4084 } | |
4085 } | |
4086 return this; | |
4087 } | |
4088 | |
4089 char *FuncExp::toChars() | |
4090 { | |
4091 return fd->toChars(); | |
4092 } | |
4093 | |
4094 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4095 { | |
4096 buf->writestring(fd->toChars()); | |
4097 } | |
4098 | |
4099 | |
4100 /******************************** DeclarationExp **************************/ | |
4101 | |
4102 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4103 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4104 { | |
4105 this->declaration = declaration; | |
4106 } | |
4107 | |
4108 Expression *DeclarationExp::syntaxCopy() | |
4109 { | |
4110 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4111 } | |
4112 | |
4113 Expression *DeclarationExp::semantic(Scope *sc) | |
4114 { | |
4115 if (type) | |
4116 return this; | |
4117 | |
4118 #if LOGSEMANTIC | |
4119 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4120 #endif | |
4121 | |
4122 /* This is here to support extern(linkage) declaration, | |
4123 * where the extern(linkage) winds up being an AttribDeclaration | |
4124 * wrapper. | |
4125 */ | |
4126 Dsymbol *s = declaration; | |
4127 | |
4128 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4129 if (ad) | |
4130 { | |
4131 if (ad->decl && ad->decl->dim == 1) | |
4132 s = (Dsymbol *)ad->decl->data[0]; | |
4133 } | |
4134 | |
4135 if (s->isVarDeclaration()) | |
4136 { // Do semantic() on initializer first, so: | |
4137 // int a = a; | |
4138 // will be illegal. | |
4139 declaration->semantic(sc); | |
4140 s->parent = sc->parent; | |
4141 } | |
4142 | |
4143 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4144 // Insert into both local scope and function scope. | |
4145 // Must be unique in both. | |
4146 if (s->ident) | |
4147 { | |
4148 if (!sc->insert(s)) | |
4149 error("declaration %s is already defined", s->toPrettyChars()); | |
4150 else if (sc->func) | |
4151 { VarDeclaration *v = s->isVarDeclaration(); | |
4152 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4153 !sc->func->localsymtab->insert(s)) | |
4154 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4155 else if (!global.params.useDeprecated) | |
4156 { // Disallow shadowing | |
4157 | |
4158 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4159 { Dsymbol *s2; | |
4160 | |
4161 if (scx->scopesym && scx->scopesym->symtab && | |
4162 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4163 s != s2) | |
4164 { | |
4165 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4166 } | |
4167 } | |
4168 } | |
4169 } | |
4170 } | |
4171 if (!s->isVarDeclaration()) | |
4172 { | |
4173 declaration->semantic(sc); | |
4174 s->parent = sc->parent; | |
4175 } | |
4176 if (!global.errors) | |
4177 { | |
4178 declaration->semantic2(sc); | |
4179 if (!global.errors) | |
4180 { | |
4181 declaration->semantic3(sc); | |
4182 | |
4183 if (!global.errors && global.params.useInline) | |
4184 declaration->inlineScan(); | |
4185 } | |
4186 } | |
4187 | |
4188 type = Type::tvoid; | |
4189 return this; | |
4190 } | |
4191 | |
4192 int DeclarationExp::checkSideEffect(int flag) | |
4193 { | |
4194 return 1; | |
4195 } | |
4196 | |
4197 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4198 { | |
4199 declaration->toCBuffer(buf, hgs); | |
4200 } | |
4201 | |
4202 | |
4203 /************************ TypeidExp ************************************/ | |
4204 | |
4205 /* | |
4206 * typeid(int) | |
4207 */ | |
4208 | |
4209 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4210 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4211 { | |
4212 this->typeidType = typeidType; | |
4213 } | |
4214 | |
4215 | |
4216 Expression *TypeidExp::syntaxCopy() | |
4217 { | |
4218 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4219 } | |
4220 | |
4221 | |
4222 Expression *TypeidExp::semantic(Scope *sc) | |
4223 { Expression *e; | |
4224 | |
4225 #if LOGSEMANTIC | |
4226 printf("TypeidExp::semantic()\n"); | |
4227 #endif | |
4228 typeidType = typeidType->semantic(loc, sc); | |
4229 e = typeidType->getTypeInfo(sc); | |
336 | 4230 if (e->loc.linnum == 0) |
4231 e->loc = loc; // so there's at least some line number info | |
159 | 4232 return e; |
4233 } | |
4234 | |
4235 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4236 { | |
4237 buf->writestring("typeid("); | |
4238 typeidType->toCBuffer(buf, NULL, hgs); | |
4239 buf->writeByte(')'); | |
4240 } | |
4241 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4242 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4243 #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
|
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 * __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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4248 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
|
4249 : 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
|
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 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
|
4252 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
|
4253 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4254 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4255 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4256 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
|
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 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4261 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4262 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
|
4263 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4264 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
|
4265 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
|
4266 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
|
4267 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4268 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
|
4269 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4270 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
|
4271 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
|
4272 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
|
4273 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4274 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4275 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
|
4276 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4277 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4278 |
159 | 4279 /************************************************************/ |
4280 | |
4281 HaltExp::HaltExp(Loc loc) | |
4282 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4283 { | |
4284 } | |
4285 | |
4286 Expression *HaltExp::semantic(Scope *sc) | |
4287 { | |
4288 #if LOGSEMANTIC | |
4289 printf("HaltExp::semantic()\n"); | |
4290 #endif | |
4291 type = Type::tvoid; | |
4292 return this; | |
4293 } | |
4294 | |
4295 int HaltExp::checkSideEffect(int flag) | |
4296 { | |
4297 return 1; | |
4298 } | |
4299 | |
4300 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4301 { | |
4302 buf->writestring("halt"); | |
4303 } | |
4304 | |
4305 /************************************************************/ | |
4306 | |
4307 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4308 Type *tspec, enum TOK tok2) | |
4309 : Expression(loc, TOKis, sizeof(IsExp)) | |
4310 { | |
4311 this->targ = targ; | |
4312 this->id = id; | |
4313 this->tok = tok; | |
4314 this->tspec = tspec; | |
4315 this->tok2 = tok2; | |
4316 } | |
4317 | |
4318 Expression *IsExp::syntaxCopy() | |
4319 { | |
4320 return new IsExp(loc, | |
4321 targ->syntaxCopy(), | |
4322 id, | |
4323 tok, | |
4324 tspec ? tspec->syntaxCopy() : NULL, | |
4325 tok2); | |
4326 } | |
4327 | |
4328 Expression *IsExp::semantic(Scope *sc) | |
4329 { Type *tded; | |
4330 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4331 /* 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
|
4332 * 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
|
4333 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4334 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4335 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4336 if (id && !(sc->flags & SCOPEstaticif)) |
4337 error("can only declare type aliases within static if conditionals"); | |
4338 | |
4339 unsigned errors_save = global.errors; | |
4340 global.errors = 0; | |
4341 global.gag++; // suppress printing of error messages | |
4342 targ = targ->semantic(loc, sc); | |
4343 global.gag--; | |
4344 unsigned gerrors = global.errors; | |
4345 global.errors = errors_save; | |
4346 | |
4347 if (gerrors) // if any errors happened | |
4348 { // then condition is false | |
4349 goto Lno; | |
4350 } | |
4351 else if (tok2 != TOKreserved) | |
4352 { | |
4353 switch (tok2) | |
4354 { | |
4355 case TOKtypedef: | |
4356 if (targ->ty != Ttypedef) | |
4357 goto Lno; | |
4358 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4359 break; | |
4360 | |
4361 case TOKstruct: | |
4362 if (targ->ty != Tstruct) | |
4363 goto Lno; | |
4364 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4365 goto Lno; | |
4366 tded = targ; | |
4367 break; | |
4368 | |
4369 case TOKunion: | |
4370 if (targ->ty != Tstruct) | |
4371 goto Lno; | |
4372 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4373 goto Lno; | |
4374 tded = targ; | |
4375 break; | |
4376 | |
4377 case TOKclass: | |
4378 if (targ->ty != Tclass) | |
4379 goto Lno; | |
4380 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4381 goto Lno; | |
4382 tded = targ; | |
4383 break; | |
4384 | |
4385 case TOKinterface: | |
4386 if (targ->ty != Tclass) | |
4387 goto Lno; | |
4388 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4389 goto Lno; | |
4390 tded = targ; | |
4391 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
|
4392 #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
|
4393 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
|
4394 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
|
4395 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
|
4396 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
|
4397 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4398 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4399 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
|
4400 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
|
4401 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
|
4402 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
|
4403 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4404 #endif |
159 | 4405 |
4406 case TOKsuper: | |
4407 // If class or interface, get the base class and interfaces | |
4408 if (targ->ty != Tclass) | |
4409 goto Lno; | |
4410 else | |
4411 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4412 Arguments *args = new Arguments; | |
4413 args->reserve(cd->baseclasses.dim); | |
4414 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4415 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4416 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4417 } | |
4418 tded = new TypeTuple(args); | |
4419 } | |
4420 break; | |
4421 | |
4422 case TOKenum: | |
4423 if (targ->ty != Tenum) | |
4424 goto Lno; | |
4425 tded = ((TypeEnum *)targ)->sym->memtype; | |
4426 break; | |
4427 | |
4428 case TOKdelegate: | |
4429 if (targ->ty != Tdelegate) | |
4430 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
|
4431 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4432 break; |
4433 | |
4434 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
|
4435 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4436 if (targ->ty != Tfunction) |
159 | 4437 goto Lno; |
4438 tded = targ; | |
4439 | |
4440 /* Generate tuple from function parameter types. | |
4441 */ | |
4442 assert(tded->ty == Tfunction); | |
4443 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4444 size_t dim = Argument::dim(params); | |
4445 Arguments *args = new Arguments; | |
4446 args->reserve(dim); | |
4447 for (size_t i = 0; i < dim; i++) | |
4448 { Argument *arg = Argument::getNth(params, i); | |
4449 assert(arg && arg->type); | |
4450 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4451 } | |
4452 tded = new TypeTuple(args); | |
4453 break; | |
4454 } | |
4455 case TOKreturn: | |
4456 /* Get the 'return type' for the function, | |
4457 * delegate, or pointer to function. | |
4458 */ | |
4459 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
|
4460 tded = ((TypeFunction *)targ)->next; |
159 | 4461 else if (targ->ty == Tdelegate) |
4462 tded = targ->next->next; | |
4463 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4464 tded = targ->next->next; | |
4465 else | |
4466 goto Lno; | |
4467 break; | |
4468 | |
4469 default: | |
4470 assert(0); | |
4471 } | |
4472 goto Lyes; | |
4473 } | |
4474 else if (id && tspec) | |
4475 { | |
4476 /* Evaluate to TRUE if targ matches tspec. | |
4477 * If TRUE, declare id as an alias for the specialized type. | |
4478 */ | |
4479 | |
4480 MATCH m; | |
4481 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4482 | |
4483 TemplateParameters parameters; | |
4484 parameters.setDim(1); | |
4485 parameters.data[0] = (void *)&tp; | |
4486 | |
4487 Objects dedtypes; | |
4488 dedtypes.setDim(1); | |
4489 dedtypes.data[0] = NULL; | |
4490 | |
4491 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4492 if (m == MATCHnomatch || | |
4493 (m != MATCHexact && tok == TOKequal)) | |
4494 goto Lno; | |
4495 else | |
4496 { | |
4497 assert(dedtypes.dim == 1); | |
4498 tded = (Type *)dedtypes.data[0]; | |
4499 if (!tded) | |
4500 tded = targ; | |
4501 goto Lyes; | |
4502 } | |
4503 } | |
4504 else if (id) | |
4505 { | |
4506 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4507 */ | |
4508 tded = targ; | |
4509 goto Lyes; | |
4510 } | |
4511 else if (tspec) | |
4512 { | |
4513 /* Evaluate to TRUE if targ matches tspec | |
4514 */ | |
4515 tspec = tspec->semantic(loc, sc); | |
4516 //printf("targ = %s\n", targ->toChars()); | |
4517 //printf("tspec = %s\n", tspec->toChars()); | |
4518 if (tok == TOKcolon) | |
4519 { if (targ->implicitConvTo(tspec)) | |
4520 goto Lyes; | |
4521 else | |
4522 goto Lno; | |
4523 } | |
4524 else /* == */ | |
4525 { if (targ->equals(tspec)) | |
4526 goto Lyes; | |
4527 else | |
4528 goto Lno; | |
4529 } | |
4530 } | |
4531 | |
4532 Lyes: | |
4533 if (id) | |
4534 { | |
4535 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4536 s->semantic(sc); | |
4537 sc->insert(s); | |
4538 if (sc->sd) | |
4539 s->addMember(sc, sc->sd, 1); | |
4540 } | |
4541 return new IntegerExp(1); | |
4542 | |
4543 Lno: | |
4544 return new IntegerExp(0); | |
4545 } | |
4546 | |
4547 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4548 { | |
4549 buf->writestring("is("); | |
4550 targ->toCBuffer(buf, id, hgs); | |
4551 if (tok2 != TOKreserved) | |
4552 { | |
4553 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4554 } | |
4555 else if (tspec) | |
4556 { | |
4557 if (tok == TOKcolon) | |
4558 buf->writestring(" : "); | |
4559 else | |
4560 buf->writestring(" == "); | |
4561 tspec->toCBuffer(buf, NULL, hgs); | |
4562 } | |
336 | 4563 #if DMDV2 |
159 | 4564 if (parameters) |
4565 { // First parameter is already output, so start with second | |
4566 for (int i = 1; i < parameters->dim; i++) | |
4567 { | |
4568 buf->writeByte(','); | |
4569 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4570 tp->toCBuffer(buf, hgs); | |
4571 } | |
4572 } | |
4573 #endif | |
4574 buf->writeByte(')'); | |
4575 } | |
4576 | |
4577 | |
4578 /************************************************************/ | |
4579 | |
4580 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4581 : Expression(loc, op, size) | |
4582 { | |
4583 this->e1 = e1; | |
4584 } | |
4585 | |
4586 Expression *UnaExp::syntaxCopy() | |
4587 { UnaExp *e; | |
4588 | |
4589 e = (UnaExp *)copy(); | |
4590 e->type = NULL; | |
4591 e->e1 = e->e1->syntaxCopy(); | |
4592 return e; | |
4593 } | |
4594 | |
4595 Expression *UnaExp::semantic(Scope *sc) | |
4596 { | |
4597 #if LOGSEMANTIC | |
4598 printf("UnaExp::semantic('%s')\n", toChars()); | |
4599 #endif | |
4600 e1 = e1->semantic(sc); | |
4601 // if (!e1->type) | |
4602 // error("%s has no value", e1->toChars()); | |
4603 return this; | |
4604 } | |
4605 | |
4606 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4607 { | |
4608 buf->writestring(Token::toChars(op)); | |
4609 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4610 } | |
4611 | |
4612 /************************************************************/ | |
4613 | |
4614 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4615 : Expression(loc, op, size) | |
4616 { | |
4617 this->e1 = e1; | |
4618 this->e2 = e2; | |
4619 } | |
4620 | |
4621 Expression *BinExp::syntaxCopy() | |
4622 { BinExp *e; | |
4623 | |
4624 e = (BinExp *)copy(); | |
4625 e->type = NULL; | |
4626 e->e1 = e->e1->syntaxCopy(); | |
4627 e->e2 = e->e2->syntaxCopy(); | |
4628 return e; | |
4629 } | |
4630 | |
4631 Expression *BinExp::semantic(Scope *sc) | |
4632 { | |
4633 #if LOGSEMANTIC | |
4634 printf("BinExp::semantic('%s')\n", toChars()); | |
4635 #endif | |
4636 e1 = e1->semantic(sc); | |
4637 if (!e1->type) | |
4638 { | |
4639 error("%s has no value", e1->toChars()); | |
4640 e1->type = Type::terror; | |
4641 } | |
4642 e2 = e2->semantic(sc); | |
4643 if (!e2->type) | |
4644 { | |
4645 error("%s has no value", e2->toChars()); | |
4646 e2->type = Type::terror; | |
4647 } | |
4648 assert(e1->type); | |
4649 return this; | |
4650 } | |
4651 | |
4652 Expression *BinExp::semanticp(Scope *sc) | |
4653 { | |
4654 BinExp::semantic(sc); | |
4655 e1 = resolveProperties(sc, e1); | |
4656 e2 = resolveProperties(sc, e2); | |
4657 return this; | |
4658 } | |
4659 | |
4660 /*************************** | |
4661 * Common semantic routine for some xxxAssignExp's. | |
4662 */ | |
4663 | |
4664 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4665 { Expression *e; | |
4666 | |
4667 if (!type) | |
4668 { | |
4669 BinExp::semantic(sc); | |
4670 e2 = resolveProperties(sc, e2); | |
4671 | |
4672 e = op_overload(sc); | |
4673 if (e) | |
4674 return e; | |
4675 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4676 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
|
4677 { // 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
|
4678 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
|
4679 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
|
4680 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
|
4681 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4682 |
159 | 4683 e1 = e1->modifiableLvalue(sc, e1); |
4684 e1->checkScalar(); | |
4685 type = e1->type; | |
4686 if (type->toBasetype()->ty == Tbool) | |
4687 { | |
4688 error("operator not allowed on bool expression %s", toChars()); | |
4689 } | |
4690 typeCombine(sc); | |
4691 e1->checkArithmetic(); | |
4692 e2->checkArithmetic(); | |
4693 | |
4694 if (op == TOKmodass && e2->type->iscomplex()) | |
4695 { error("cannot perform modulo complex arithmetic"); | |
4696 return new IntegerExp(0); | |
4697 } | |
4698 } | |
4699 return this; | |
4700 } | |
4701 | |
4702 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4703 { Expression *e; | |
4704 | |
4705 if (!type) | |
4706 { | |
4707 BinExp::semantic(sc); | |
4708 e2 = resolveProperties(sc, e2); | |
4709 | |
4710 e = op_overload(sc); | |
4711 if (e) | |
4712 return e; | |
4713 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4714 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
|
4715 { // 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
|
4716 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
|
4717 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
|
4718 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
|
4719 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4720 |
159 | 4721 e1 = e1->modifiableLvalue(sc, e1); |
4722 e1->checkScalar(); | |
4723 type = e1->type; | |
4724 if (type->toBasetype()->ty == Tbool) | |
4725 { | |
4726 e2 = e2->implicitCastTo(sc, type); | |
4727 } | |
4728 | |
4729 typeCombine(sc); | |
4730 e1->checkIntegral(); | |
4731 e2->checkIntegral(); | |
4732 } | |
4733 return this; | |
4734 } | |
4735 | |
4736 int BinExp::checkSideEffect(int flag) | |
4737 { | |
4738 if (op == TOKplusplus || | |
4739 op == TOKminusminus || | |
4740 op == TOKassign || | |
4741 op == TOKconstruct || | |
4742 op == TOKblit || | |
4743 op == TOKaddass || | |
4744 op == TOKminass || | |
4745 op == TOKcatass || | |
4746 op == TOKmulass || | |
4747 op == TOKdivass || | |
4748 op == TOKmodass || | |
4749 op == TOKshlass || | |
4750 op == TOKshrass || | |
4751 op == TOKushrass || | |
4752 op == TOKandass || | |
4753 op == TOKorass || | |
4754 op == TOKxorass || | |
4755 op == TOKin || | |
4756 op == TOKremove) | |
4757 return 1; | |
4758 return Expression::checkSideEffect(flag); | |
4759 } | |
4760 | |
4761 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4762 { | |
4763 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4764 buf->writeByte(' '); | |
4765 buf->writestring(Token::toChars(op)); | |
4766 buf->writeByte(' '); | |
4767 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
4768 } | |
4769 | |
4770 int BinExp::isunsigned() | |
4771 { | |
4772 return e1->type->isunsigned() || e2->type->isunsigned(); | |
4773 } | |
4774 | |
4775 void BinExp::incompatibleTypes() | |
4776 { | |
4777 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
4778 e1->toChars(), Token::toChars(op), e2->toChars(), | |
4779 e1->type->toChars(), e2->type->toChars()); | |
4780 } | |
4781 | |
4782 /************************************************************/ | |
4783 | |
4784 CompileExp::CompileExp(Loc loc, Expression *e) | |
4785 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
4786 { | |
4787 } | |
4788 | |
4789 Expression *CompileExp::semantic(Scope *sc) | |
4790 { | |
4791 #if LOGSEMANTIC | |
4792 printf("CompileExp::semantic('%s')\n", toChars()); | |
4793 #endif | |
4794 UnaExp::semantic(sc); | |
4795 e1 = resolveProperties(sc, e1); | |
4796 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
4797 if (e1->op != TOKstring) | |
4798 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
4799 type = Type::terror; | |
4800 return this; | |
4801 } | |
4802 StringExp *se = (StringExp *)e1; | |
4803 se = se->toUTF8(sc); | |
4804 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
4805 p.loc = loc; | |
4806 p.nextToken(); | |
4807 Expression *e = p.parseExpression(); | |
4808 if (p.token.value != TOKeof) | |
4809 error("incomplete mixin expression (%s)", se->toChars()); | |
4810 return e->semantic(sc); | |
4811 } | |
4812 | |
4813 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4814 { | |
4815 buf->writestring("mixin("); | |
4816 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4817 buf->writeByte(')'); | |
4818 } | |
4819 | |
4820 /************************************************************/ | |
4821 | |
4822 FileExp::FileExp(Loc loc, Expression *e) | |
4823 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
4824 { | |
4825 } | |
4826 | |
4827 Expression *FileExp::semantic(Scope *sc) | |
4828 { char *name; | |
4829 StringExp *se; | |
4830 | |
4831 #if LOGSEMANTIC | |
4832 printf("FileExp::semantic('%s')\n", toChars()); | |
4833 #endif | |
4834 UnaExp::semantic(sc); | |
4835 e1 = resolveProperties(sc, e1); | |
4836 e1 = e1->optimize(WANTvalue); | |
4837 if (e1->op != TOKstring) | |
4838 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
4839 goto Lerror; | |
4840 } | |
4841 se = (StringExp *)e1; | |
4842 se = se->toUTF8(sc); | |
4843 name = (char *)se->string; | |
4844 | |
4845 if (!global.params.fileImppath) | |
4846 { error("need -Jpath switch to import text file %s", name); | |
4847 goto Lerror; | |
4848 } | |
4849 | |
4850 if (name != FileName::name(name)) | |
4851 { error("use -Jpath switch to provide path for filename %s", name); | |
4852 goto Lerror; | |
4853 } | |
4854 | |
4855 name = FileName::searchPath(global.filePath, name, 0); | |
4856 if (!name) | |
4857 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
4858 goto Lerror; | |
4859 } | |
4860 | |
4861 if (global.params.verbose) | |
4862 printf("file %s\t(%s)\n", se->string, name); | |
4863 | |
4864 { File f(name); | |
4865 if (f.read()) | |
4866 { error("cannot read file %s", f.toChars()); | |
4867 goto Lerror; | |
4868 } | |
4869 else | |
4870 { | |
4871 f.ref = 1; | |
4872 se = new StringExp(loc, f.buffer, f.len); | |
4873 } | |
4874 } | |
4875 Lret: | |
4876 return se->semantic(sc); | |
4877 | |
4878 Lerror: | |
4879 se = new StringExp(loc, ""); | |
4880 goto Lret; | |
4881 } | |
4882 | |
4883 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4884 { | |
4885 buf->writestring("import("); | |
4886 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4887 buf->writeByte(')'); | |
4888 } | |
4889 | |
4890 /************************************************************/ | |
4891 | |
4892 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
4893 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
4894 { | |
4895 this->msg = msg; | |
4896 } | |
4897 | |
4898 Expression *AssertExp::syntaxCopy() | |
4899 { | |
4900 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
4901 msg ? msg->syntaxCopy() : NULL); | |
4902 return ae; | |
4903 } | |
4904 | |
4905 Expression *AssertExp::semantic(Scope *sc) | |
4906 { | |
4907 #if LOGSEMANTIC | |
4908 printf("AssertExp::semantic('%s')\n", toChars()); | |
4909 #endif | |
4910 UnaExp::semantic(sc); | |
4911 e1 = resolveProperties(sc, e1); | |
4912 // BUG: see if we can do compile time elimination of the Assert | |
4913 e1 = e1->optimize(WANTvalue); | |
4914 e1 = e1->checkToBoolean(); | |
4915 if (msg) | |
4916 { | |
4917 msg = msg->semantic(sc); | |
4918 msg = resolveProperties(sc, msg); | |
4919 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
4920 msg = msg->optimize(WANTvalue); | |
4921 } | |
4922 if (e1->isBool(FALSE)) | |
4923 { | |
4924 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
4925 fd->hasReturnExp |= 4; | |
4926 | |
4927 if (!global.params.useAssert) | |
4928 { Expression *e = new HaltExp(loc); | |
4929 e = e->semantic(sc); | |
4930 return e; | |
4931 } | |
4932 } | |
4933 type = Type::tvoid; | |
4934 return this; | |
4935 } | |
4936 | |
4937 int AssertExp::checkSideEffect(int flag) | |
4938 { | |
4939 return 1; | |
4940 } | |
4941 | |
4942 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4943 { | |
4944 buf->writestring("assert("); | |
4945 expToCBuffer(buf, hgs, e1, PREC_assign); | |
4946 if (msg) | |
4947 { | |
4948 buf->writeByte(','); | |
4949 expToCBuffer(buf, hgs, msg, PREC_assign); | |
4950 } | |
4951 buf->writeByte(')'); | |
4952 } | |
4953 | |
4954 /************************************************************/ | |
4955 | |
4956 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
4957 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
4958 { | |
4959 this->ident = ident; | |
4960 } | |
4961 | |
4962 Expression *DotIdExp::semantic(Scope *sc) | |
4963 { Expression *e; | |
4964 Expression *eleft; | |
4965 Expression *eright; | |
4966 | |
4967 #if LOGSEMANTIC | |
4968 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
4969 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
4970 #endif | |
4971 | |
4972 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
4973 | |
4974 #if 0 | |
4975 /* Don't do semantic analysis if we'll be converting | |
4976 * it to a string. | |
4977 */ | |
4978 if (ident == Id::stringof) | |
4979 { char *s = e1->toChars(); | |
4980 e = new StringExp(loc, s, strlen(s), 'c'); | |
4981 e = e->semantic(sc); | |
4982 return e; | |
4983 } | |
4984 #endif | |
4985 | |
4986 /* Special case: rewrite this.id and super.id | |
4987 * to be classtype.id and baseclasstype.id | |
4988 * if we have no this pointer. | |
4989 */ | |
4990 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
4991 { ClassDeclaration *cd; | |
4992 StructDeclaration *sd; | |
4993 AggregateDeclaration *ad; | |
4994 | |
4995 ad = sc->getStructClassScope(); | |
4996 if (ad) | |
4997 { | |
4998 cd = ad->isClassDeclaration(); | |
4999 if (cd) | |
5000 { | |
5001 if (e1->op == TOKthis) | |
5002 { | |
5003 e = new TypeDotIdExp(loc, cd->type, ident); | |
5004 return e->semantic(sc); | |
5005 } | |
5006 else if (cd->baseClass && e1->op == TOKsuper) | |
5007 { | |
5008 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
5009 return e->semantic(sc); | |
5010 } | |
5011 } | |
5012 else | |
5013 { | |
5014 sd = ad->isStructDeclaration(); | |
5015 if (sd) | |
5016 { | |
5017 if (e1->op == TOKthis) | |
5018 { | |
5019 e = new TypeDotIdExp(loc, sd->type, ident); | |
5020 return e->semantic(sc); | |
5021 } | |
5022 } | |
5023 } | |
5024 } | |
5025 } | |
5026 | |
5027 UnaExp::semantic(sc); | |
5028 | |
5029 if (e1->op == TOKdotexp) | |
5030 { | |
5031 DotExp *de = (DotExp *)e1; | |
5032 eleft = de->e1; | |
5033 eright = de->e2; | |
5034 } | |
5035 else | |
5036 { | |
5037 e1 = resolveProperties(sc, e1); | |
5038 eleft = NULL; | |
5039 eright = e1; | |
5040 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5041 #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
|
5042 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
|
5043 { /* '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
|
5044 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5045 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
|
5046 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
|
5047 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
|
5048 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
|
5049 { 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
|
5050 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
|
5051 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
|
5052 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
|
5053 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5054 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
|
5055 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
|
5056 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
|
5057 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5058 #endif |
159 | 5059 |
5060 if (e1->op == TOKtuple && ident == Id::length) | |
5061 { | |
5062 TupleExp *te = (TupleExp *)e1; | |
5063 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5064 return e; | |
5065 } | |
5066 | |
5067 if (eright->op == TOKimport) // also used for template alias's | |
5068 { | |
5069 Dsymbol *s; | |
5070 ScopeExp *ie = (ScopeExp *)eright; | |
5071 | |
5072 s = ie->sds->search(loc, ident, 0); | |
5073 if (s) | |
5074 { | |
5075 s = s->toAlias(); | |
5076 checkDeprecated(sc, s); | |
5077 | |
5078 EnumMember *em = s->isEnumMember(); | |
5079 if (em) | |
5080 { | |
5081 e = em->value; | |
5082 e = e->semantic(sc); | |
5083 return e; | |
5084 } | |
5085 | |
5086 VarDeclaration *v = s->isVarDeclaration(); | |
5087 if (v) | |
5088 { | |
5089 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5090 if (v->inuse) | |
5091 { | |
5092 error("circular reference to '%s'", v->toChars()); | |
5093 type = Type::tint32; | |
5094 return this; | |
5095 } | |
5096 type = v->type; | |
5097 if (v->isConst()) | |
5098 { | |
5099 if (v->init) | |
5100 { | |
5101 ExpInitializer *ei = v->init->isExpInitializer(); | |
5102 if (ei) | |
5103 { | |
5104 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5105 //ei->exp = ei->exp->semantic(sc); | |
5106 if (ei->exp->type == type) | |
5107 { | |
5108 e = ei->exp->copy(); // make copy so we can change loc | |
5109 e->loc = loc; | |
5110 return e; | |
5111 } | |
5112 } | |
5113 } | |
5114 else if (type->isscalar()) | |
5115 { | |
5116 e = type->defaultInit(); | |
5117 e->loc = loc; | |
5118 return e; | |
5119 } | |
5120 } | |
5121 if (v->needThis()) | |
5122 { | |
5123 if (!eleft) | |
5124 eleft = new ThisExp(loc); | |
5125 e = new DotVarExp(loc, eleft, v); | |
5126 e = e->semantic(sc); | |
5127 } | |
5128 else | |
5129 { | |
5130 e = new VarExp(loc, v); | |
5131 if (eleft) | |
5132 { e = new CommaExp(loc, eleft, e); | |
5133 e->type = v->type; | |
5134 } | |
5135 } | |
5136 return e->deref(); | |
5137 } | |
5138 | |
5139 FuncDeclaration *f = s->isFuncDeclaration(); | |
5140 if (f) | |
5141 { | |
5142 //printf("it's a function\n"); | |
5143 if (f->needThis()) | |
5144 { | |
5145 if (!eleft) | |
5146 eleft = new ThisExp(loc); | |
5147 e = new DotVarExp(loc, eleft, f); | |
5148 e = e->semantic(sc); | |
5149 } | |
5150 else | |
5151 { | |
5152 e = new VarExp(loc, f); | |
5153 if (eleft) | |
5154 { e = new CommaExp(loc, eleft, e); | |
5155 e->type = f->type; | |
5156 } | |
5157 } | |
5158 return e; | |
5159 } | |
5160 | |
5161 Type *t = s->getType(); | |
5162 if (t) | |
5163 { | |
5164 return new TypeExp(loc, t); | |
5165 } | |
5166 | |
336 | 5167 TupleDeclaration *tup = s->isTupleDeclaration(); |
5168 if (tup) | |
5169 { | |
5170 if (eleft) | |
5171 error("cannot have e.tuple"); | |
5172 e = new TupleExp(loc, tup); | |
5173 e = e->semantic(sc); | |
5174 return e; | |
5175 } | |
5176 | |
159 | 5177 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5178 if (sds) | |
5179 { | |
5180 //printf("it's a ScopeDsymbol\n"); | |
5181 e = new ScopeExp(loc, sds); | |
5182 e = e->semantic(sc); | |
5183 if (eleft) | |
5184 e = new DotExp(loc, eleft, e); | |
5185 return e; | |
5186 } | |
5187 | |
5188 Import *imp = s->isImport(); | |
5189 if (imp) | |
5190 { | |
5191 ScopeExp *ie; | |
5192 | |
5193 ie = new ScopeExp(loc, imp->pkg); | |
5194 return ie->semantic(sc); | |
5195 } | |
5196 | |
5197 // BUG: handle other cases like in IdentifierExp::semantic() | |
5198 #ifdef DEBUG | |
5199 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5200 #endif | |
5201 assert(0); | |
5202 } | |
5203 else if (ident == Id::stringof) | |
5204 { char *s = ie->toChars(); | |
5205 e = new StringExp(loc, s, strlen(s), 'c'); | |
5206 e = e->semantic(sc); | |
5207 return e; | |
5208 } | |
5209 error("undefined identifier %s", toChars()); | |
5210 type = Type::tvoid; | |
5211 return this; | |
5212 } | |
5213 else if (e1->type->ty == Tpointer && | |
5214 ident != Id::init && ident != Id::__sizeof && | |
5215 ident != Id::alignof && ident != Id::offsetof && | |
5216 ident != Id::mangleof && ident != Id::stringof) | |
5217 { | |
5218 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
|
5219 e->type = ((TypePointer *)e1->type)->next; |
159 | 5220 return e->type->dotExp(sc, e, ident); |
5221 } | |
5222 else | |
5223 { | |
5224 e = e1->type->dotExp(sc, e1, ident); | |
5225 e = e->semantic(sc); | |
5226 return e; | |
5227 } | |
5228 } | |
5229 | |
5230 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5231 { | |
5232 //printf("DotIdExp::toCBuffer()\n"); | |
5233 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5234 buf->writeByte('.'); | |
5235 buf->writestring(ident->toChars()); | |
5236 } | |
5237 | |
5238 /********************** DotTemplateExp ***********************************/ | |
5239 | |
5240 // Mainly just a placeholder | |
5241 | |
5242 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5243 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5244 | |
5245 { | |
5246 this->td = td; | |
5247 } | |
5248 | |
5249 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5250 { | |
5251 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5252 buf->writeByte('.'); | |
5253 buf->writestring(td->toChars()); | |
5254 } | |
5255 | |
5256 | |
5257 /************************************************************/ | |
5258 | |
5259 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5260 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5261 { | |
5262 //printf("DotVarExp()\n"); | |
5263 this->var = v; | |
5264 } | |
5265 | |
5266 Expression *DotVarExp::semantic(Scope *sc) | |
5267 { | |
5268 #if LOGSEMANTIC | |
5269 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5270 #endif | |
5271 if (!type) | |
5272 { | |
5273 var = var->toAlias()->isDeclaration(); | |
5274 | |
5275 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5276 if (tup) | |
5277 { /* Replace: | |
5278 * e1.tuple(a, b, c) | |
5279 * with: | |
5280 * tuple(e1.a, e1.b, e1.c) | |
5281 */ | |
5282 Expressions *exps = new Expressions; | |
5283 | |
5284 exps->reserve(tup->objects->dim); | |
5285 for (size_t i = 0; i < tup->objects->dim; i++) | |
5286 { Object *o = (Object *)tup->objects->data[i]; | |
5287 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5288 { | |
5289 error("%s is not an expression", o->toChars()); | |
5290 } | |
5291 else | |
5292 { | |
5293 Expression *e = (Expression *)o; | |
5294 if (e->op != TOKdsymbol) | |
5295 error("%s is not a member", e->toChars()); | |
5296 else | |
5297 { DsymbolExp *ve = (DsymbolExp *)e; | |
5298 | |
5299 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5300 exps->push(e); | |
5301 } | |
5302 } | |
5303 } | |
5304 Expression *e = new TupleExp(loc, exps); | |
5305 e = e->semantic(sc); | |
5306 return e; | |
5307 } | |
5308 | |
5309 e1 = e1->semantic(sc); | |
5310 type = var->type; | |
5311 if (!type && global.errors) | |
5312 { // var is goofed up, just return 0 | |
5313 return new IntegerExp(0); | |
5314 } | |
5315 assert(type); | |
5316 | |
5317 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5318 { | |
5319 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
336 | 5320 e1 = getRightThis(loc, sc, ad, e1, var); |
5321 if (!sc->noaccesscheck) | |
5322 accessCheck(loc, sc, e1, var); | |
5323 | |
5324 VarDeclaration *v = var->isVarDeclaration(); | |
5325 if (v && v->isConst()) | |
5326 { ExpInitializer *ei = v->getExpInitializer(); | |
5327 if (ei) | |
5328 { Expression *e = ei->exp->copy(); | |
5329 e = e->semantic(sc); | |
5330 return e; | |
159 | 5331 } |
5332 } | |
5333 } | |
5334 } | |
5335 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5336 return this; | |
5337 } | |
5338 | |
5339 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) | |
5340 { | |
5341 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5342 return this; | |
5343 } | |
5344 | |
5345 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5346 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5347 #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
|
5348 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
|
5349 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
|
5350 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
|
5351 #endif |
159 | 5352 |
5353 if (var->isCtorinit()) | |
5354 { // It's only modifiable if inside the right constructor | |
5355 Dsymbol *s = sc->func; | |
5356 while (1) | |
5357 { | |
5358 FuncDeclaration *fd = NULL; | |
5359 if (s) | |
5360 fd = s->isFuncDeclaration(); | |
5361 if (fd && | |
5362 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5363 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5364 fd->toParent() == var->toParent() && | |
5365 e1->op == TOKthis | |
5366 ) | |
5367 { | |
5368 VarDeclaration *v = var->isVarDeclaration(); | |
5369 assert(v); | |
5370 v->ctorinit = 1; | |
5371 //printf("setting ctorinit\n"); | |
5372 } | |
5373 else | |
5374 { | |
5375 if (s) | |
5376 { s = s->toParent2(); | |
5377 continue; | |
5378 } | |
5379 else | |
5380 { | |
5381 const char *p = var->isStatic() ? "static " : ""; | |
5382 error("can only initialize %sconst member %s inside %sconstructor", | |
5383 p, var->toChars(), p); | |
5384 } | |
5385 } | |
5386 break; | |
5387 } | |
5388 } | |
5389 return this; | |
5390 } | |
5391 | |
5392 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5393 { | |
5394 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5395 buf->writeByte('.'); | |
5396 buf->writestring(var->toChars()); | |
5397 } | |
5398 | |
5399 /************************************************************/ | |
5400 | |
5401 /* Things like: | |
5402 * foo.bar!(args) | |
5403 */ | |
5404 | |
5405 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5406 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5407 { | |
5408 //printf("DotTemplateInstanceExp()\n"); | |
5409 this->ti = ti; | |
5410 } | |
5411 | |
5412 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5413 { | |
5414 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5415 e1->syntaxCopy(), | |
5416 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5417 return de; | |
5418 } | |
5419 | |
5420 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5421 { Dsymbol *s; | |
5422 Dsymbol *s2; | |
5423 TemplateDeclaration *td; | |
5424 Expression *e; | |
5425 Identifier *id; | |
5426 Type *t1; | |
5427 Expression *eleft = NULL; | |
5428 Expression *eright; | |
5429 | |
5430 #if LOGSEMANTIC | |
5431 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5432 #endif | |
5433 //e1->print(); | |
5434 //print(); | |
5435 e1 = e1->semantic(sc); | |
5436 t1 = e1->type; | |
5437 if (t1) | |
5438 t1 = t1->toBasetype(); | |
5439 //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
|
5440 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5441 /* 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
|
5442 * 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
|
5443 * 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
|
5444 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5445 |
159 | 5446 if (e1->op == TOKdotexp) |
5447 { DotExp *de = (DotExp *)e1; | |
5448 eleft = de->e1; | |
5449 eright = de->e2; | |
5450 } | |
5451 else | |
5452 { eleft = NULL; | |
5453 eright = e1; | |
5454 } | |
5455 if (eright->op == TOKimport) | |
5456 { | |
5457 s = ((ScopeExp *)eright)->sds; | |
5458 } | |
5459 else if (e1->op == TOKtype) | |
5460 { | |
5461 s = t1->isClassHandle(); | |
5462 if (!s) | |
5463 { if (t1->ty == Tstruct) | |
5464 s = ((TypeStruct *)t1)->sym; | |
5465 else | |
5466 goto L1; | |
5467 } | |
5468 } | |
5469 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5470 { | |
5471 s = t1->toDsymbol(sc); | |
5472 eleft = e1; | |
5473 } | |
5474 else if (t1 && t1->ty == Tpointer) | |
5475 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5476 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 5477 if (t1->ty != Tstruct) |
5478 goto L1; | |
5479 s = t1->toDsymbol(sc); | |
5480 eleft = e1; | |
5481 } | |
5482 else | |
5483 { | |
5484 L1: | |
5485 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5486 goto Lerr; | |
5487 } | |
5488 | |
5489 assert(s); | |
5490 id = ti->name; | |
5491 s2 = s->search(loc, id, 0); | |
5492 if (!s2) | |
5493 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5494 goto Lerr; | |
5495 } | |
5496 s = s2; | |
5497 s->semantic(sc); | |
5498 s = s->toAlias(); | |
5499 td = s->isTemplateDeclaration(); | |
5500 if (!td) | |
5501 { | |
5502 error("%s is not a template", id->toChars()); | |
5503 goto Lerr; | |
5504 } | |
5505 if (global.errors) | |
5506 goto Lerr; | |
5507 | |
5508 ti->tempdecl = td; | |
5509 | |
5510 if (eleft) | |
5511 { Declaration *v; | |
5512 | |
5513 ti->semantic(sc); | |
5514 s = ti->inst->toAlias(); | |
5515 v = s->isDeclaration(); | |
5516 if (v) | |
5517 { e = new DotVarExp(loc, eleft, v); | |
5518 e = e->semantic(sc); | |
5519 return e; | |
5520 } | |
5521 } | |
5522 | |
5523 e = new ScopeExp(loc, ti); | |
5524 if (eleft) | |
5525 { | |
5526 e = new DotExp(loc, eleft, e); | |
5527 } | |
5528 e = e->semantic(sc); | |
5529 return e; | |
5530 | |
5531 Lerr: | |
336 | 5532 return new IntegerExp(loc, 0, Type::tint32); |
159 | 5533 } |
5534 | |
5535 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5536 { | |
5537 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5538 buf->writeByte('.'); | |
5539 ti->toCBuffer(buf, hgs); | |
5540 } | |
5541 | |
5542 /************************************************************/ | |
5543 | |
5544 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5545 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5546 { | |
5547 this->func = f; | |
5548 } | |
5549 | |
5550 Expression *DelegateExp::semantic(Scope *sc) | |
5551 { | |
5552 #if LOGSEMANTIC | |
5553 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5554 #endif | |
5555 if (!type) | |
5556 { | |
5557 e1 = e1->semantic(sc); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
5558 // LDC we need a copy as we store the LLVM tpye in TypeFunction, and delegate/members have different types for 'this' |
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
|
5559 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 5560 type = type->semantic(loc, sc); |
5561 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 5562 if (func->needThis()) |
5563 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 5564 } |
5565 return this; | |
5566 } | |
5567 | |
5568 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5569 { | |
5570 buf->writeByte('&'); | |
5571 if (!func->isNested()) | |
5572 { | |
5573 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5574 buf->writeByte('.'); | |
5575 } | |
5576 buf->writestring(func->toChars()); | |
5577 } | |
5578 | |
5579 /************************************************************/ | |
5580 | |
5581 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5582 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5583 { | |
5584 this->sym = s; | |
5585 this->type = s->getType(); | |
5586 } | |
5587 | |
5588 Expression *DotTypeExp::semantic(Scope *sc) | |
5589 { | |
5590 #if LOGSEMANTIC | |
5591 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5592 #endif | |
5593 UnaExp::semantic(sc); | |
5594 return this; | |
5595 } | |
5596 | |
5597 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5598 { | |
5599 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5600 buf->writeByte('.'); | |
5601 buf->writestring(sym->toChars()); | |
5602 } | |
5603 | |
5604 /************************************************************/ | |
5605 | |
5606 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5607 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5608 { | |
5609 this->arguments = exps; | |
5610 } | |
5611 | |
5612 CallExp::CallExp(Loc loc, Expression *e) | |
5613 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5614 { | |
5615 this->arguments = NULL; | |
5616 } | |
5617 | |
5618 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5619 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5620 { | |
5621 Expressions *arguments = new Expressions(); | |
5622 arguments->setDim(1); | |
5623 arguments->data[0] = (void *)earg1; | |
5624 | |
5625 this->arguments = arguments; | |
5626 } | |
5627 | |
5628 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5629 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5630 { | |
5631 Expressions *arguments = new Expressions(); | |
5632 arguments->setDim(2); | |
5633 arguments->data[0] = (void *)earg1; | |
5634 arguments->data[1] = (void *)earg2; | |
5635 | |
5636 this->arguments = arguments; | |
5637 } | |
5638 | |
5639 Expression *CallExp::syntaxCopy() | |
5640 { | |
5641 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5642 } | |
5643 | |
5644 | |
5645 Expression *CallExp::semantic(Scope *sc) | |
5646 { | |
5647 TypeFunction *tf; | |
5648 FuncDeclaration *f; | |
5649 int i; | |
5650 Type *t1; | |
5651 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
|
5652 Objects *targsi = NULL; // initial list of template arguments |
159 | 5653 |
5654 #if LOGSEMANTIC | |
5655 printf("CallExp::semantic() %s\n", toChars()); | |
5656 #endif | |
5657 if (type) | |
5658 return this; // semantic() already run | |
5659 #if 0 | |
5660 if (arguments && arguments->dim) | |
5661 { | |
5662 Expression *earg = (Expression *)arguments->data[0]; | |
5663 earg->print(); | |
5664 if (earg->type) earg->type->print(); | |
5665 } | |
5666 #endif | |
5667 | |
5668 if (e1->op == TOKdelegate) | |
5669 { DelegateExp *de = (DelegateExp *)e1; | |
5670 | |
5671 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5672 return semantic(sc); | |
5673 } | |
5674 | |
5675 /* Transform: | |
5676 * array.id(args) into id(array,args) | |
5677 * aa.remove(arg) into delete aa[arg] | |
5678 */ | |
5679 if (e1->op == TOKdot) | |
5680 { | |
5681 // BUG: we should handle array.a.b.c.e(args) too | |
5682 | |
5683 DotIdExp *dotid = (DotIdExp *)(e1); | |
5684 dotid->e1 = dotid->e1->semantic(sc); | |
5685 assert(dotid->e1); | |
5686 if (dotid->e1->type) | |
5687 { | |
5688 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
5689 if (e1ty == Taarray && dotid->ident == Id::remove) | |
5690 { | |
5691 if (!arguments || arguments->dim != 1) | |
5692 { error("expected key as argument to aa.remove()"); | |
5693 goto Lagain; | |
5694 } | |
5695 Expression *key = (Expression *)arguments->data[0]; | |
5696 key = key->semantic(sc); | |
5697 key = resolveProperties(sc, key); | |
5698 key->rvalue(); | |
5699 | |
5700 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
5701 key = key->implicitCastTo(sc, taa->index); | |
5702 key = key->implicitCastTo(sc, taa->key); | |
5703 | |
5704 return new RemoveExp(loc, dotid->e1, key); | |
5705 } | |
5706 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
5707 { | |
5708 if (!arguments) | |
5709 arguments = new Expressions(); | |
5710 arguments->shift(dotid->e1); | |
5711 e1 = new IdentifierExp(dotid->loc, dotid->ident); | |
5712 } | |
5713 } | |
5714 } | |
5715 | |
510
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 DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5717 /* 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
|
5718 * 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
|
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 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
|
5721 { 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
|
5722 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
|
5723 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
|
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 /* 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
|
5726 * 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
|
5727 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5728 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
|
5729 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
|
5730 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
|
5731 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
|
5732 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
|
5733 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
|
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 /* 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
|
5736 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5737 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
|
5738 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
|
5739 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
|
5740 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5741 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5742 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5743 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5744 /* 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
|
5745 * 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
|
5746 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5747 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
|
5748 { 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
|
5749 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
|
5750 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
|
5751 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5752 /* 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
|
5753 * 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
|
5754 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5755 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
|
5756 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
|
5757 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
|
5758 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
|
5759 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
|
5760 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
|
5761 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
|
5762 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5763 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
|
5764 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
|
5765 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
|
5766 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5767 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5768 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
|
5769 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5770 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5771 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5772 |
159 | 5773 istemp = 0; |
5774 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
|
5775 //printf("Lagain: %s\n", toChars()); |
159 | 5776 f = NULL; |
5777 if (e1->op == TOKthis || e1->op == TOKsuper) | |
5778 { | |
5779 // semantic() run later for these | |
5780 } | |
5781 else | |
5782 { | |
5783 UnaExp::semantic(sc); | |
5784 | |
5785 /* Look for e1 being a lazy parameter | |
5786 */ | |
5787 if (e1->op == TOKvar) | |
5788 { VarExp *ve = (VarExp *)e1; | |
5789 | |
5790 if (ve->var->storage_class & STClazy) | |
5791 { | |
5792 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
5793 TypeDelegate *t = new TypeDelegate(tf); | |
5794 ve->type = t->semantic(loc, sc); | |
5795 } | |
5796 } | |
5797 | |
5798 if (e1->op == TOKimport) | |
5799 { // Perhaps this should be moved to ScopeExp::semantic() | |
5800 ScopeExp *se = (ScopeExp *)e1; | |
5801 e1 = new DsymbolExp(loc, se->sds); | |
5802 e1 = e1->semantic(sc); | |
5803 } | |
5804 #if 1 // patch for #540 by Oskar Linde | |
5805 else if (e1->op == TOKdotexp) | |
5806 { | |
5807 DotExp *de = (DotExp *) e1; | |
5808 | |
5809 if (de->e2->op == TOKimport) | |
5810 { // This should *really* be moved to ScopeExp::semantic() | |
5811 ScopeExp *se = (ScopeExp *)de->e2; | |
5812 de->e2 = new DsymbolExp(loc, se->sds); | |
5813 de->e2 = de->e2->semantic(sc); | |
5814 } | |
5815 | |
5816 if (de->e2->op == TOKtemplate) | |
5817 { TemplateExp *te = (TemplateExp *) de->e2; | |
5818 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
5819 } | |
5820 } | |
5821 #endif | |
5822 } | |
5823 | |
5824 if (e1->op == TOKcomma) | |
5825 { | |
5826 CommaExp *ce = (CommaExp *)e1; | |
5827 | |
5828 e1 = ce->e2; | |
5829 e1->type = ce->type; | |
5830 ce->e2 = this; | |
5831 ce->type = NULL; | |
5832 return ce->semantic(sc); | |
5833 } | |
5834 | |
5835 t1 = NULL; | |
5836 if (e1->type) | |
5837 t1 = e1->type->toBasetype(); | |
5838 | |
5839 // Check for call operator overload | |
5840 if (t1) | |
5841 { AggregateDeclaration *ad; | |
5842 | |
5843 if (t1->ty == Tstruct) | |
5844 { | |
5845 ad = ((TypeStruct *)t1)->sym; | |
5846 if (search_function(ad, Id::call)) | |
5847 goto L1; // overload of opCall, therefore it's a call | |
5848 | |
5849 if (e1->op != TOKtype) | |
5850 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
5851 /* It's a struct literal | |
5852 */ | |
5853 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
5854 e = e->semantic(sc); | |
5855 e->type = e1->type; // in case e1->type was a typedef | |
5856 return e; | |
5857 } | |
5858 else if (t1->ty == Tclass) | |
5859 { | |
5860 ad = ((TypeClass *)t1)->sym; | |
5861 goto L1; | |
5862 L1: | |
5863 // Rewrite as e1.call(arguments) | |
5864 Expression *e = new DotIdExp(loc, e1, Id::call); | |
5865 e = new CallExp(loc, e, arguments); | |
5866 e = e->semantic(sc); | |
5867 return e; | |
5868 } | |
5869 } | |
5870 | |
5871 arrayExpressionSemantic(arguments, sc); | |
5872 preFunctionArguments(loc, sc, arguments); | |
5873 | |
5874 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
5875 e1->op == TOKdottd) | |
5876 { | |
5877 DotVarExp *dve; | |
5878 DotTemplateExp *dte; | |
5879 AggregateDeclaration *ad; | |
5880 UnaExp *ue = (UnaExp *)(e1); | |
336 | 5881 |
159 | 5882 if (e1->op == TOKdotvar) |
5883 { // Do overload resolution | |
5884 dve = (DotVarExp *)(e1); | |
5885 | |
5886 f = dve->var->isFuncDeclaration(); | |
5887 assert(f); | |
5888 f = f->overloadResolve(loc, arguments); | |
5889 | |
5890 ad = f->toParent()->isAggregateDeclaration(); | |
5891 } | |
5892 else | |
5893 { dte = (DotTemplateExp *)(e1); | |
5894 TemplateDeclaration *td = dte->td; | |
5895 assert(td); | |
5896 if (!arguments) | |
5897 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
5898 arguments = new Expressions(); | |
5899 f = td->deduceFunctionTemplate(sc, loc, NULL, arguments); | |
5900 if (!f) | |
5901 { type = Type::terror; | |
5902 return this; | |
5903 } | |
5904 ad = td->toParent()->isAggregateDeclaration(); | |
5905 } | |
336 | 5906 if (f->needThis()) |
5907 { | |
5908 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
5909 } | |
5910 | |
5911 /* Cannot call public functions from inside invariant | |
5912 * (because then the invariant would have infinite recursion) | |
159 | 5913 */ |
336 | 5914 if (sc->func && sc->func->isInvariantDeclaration() && |
5915 ue->e1->op == TOKthis && | |
5916 f->addPostInvariant() | |
159 | 5917 ) |
5918 { | |
336 | 5919 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 5920 } |
5921 | |
5922 checkDeprecated(sc, f); | |
5923 accessCheck(loc, sc, ue->e1, f); | |
5924 if (!f->needThis()) | |
5925 { | |
5926 VarExp *ve = new VarExp(loc, f); | |
5927 e1 = new CommaExp(loc, ue->e1, ve); | |
5928 e1->type = f->type; | |
5929 } | |
5930 else | |
5931 { | |
5932 if (e1->op == TOKdotvar) | |
5933 dve->var = f; | |
5934 else | |
5935 e1 = new DotVarExp(loc, dte->e1, f); | |
5936 e1->type = f->type; | |
5937 | |
5938 // See if we need to adjust the 'this' pointer | |
5939 AggregateDeclaration *ad = f->isThis(); | |
5940 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
5941 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
5942 ue->e1->op != TOKsuper) | |
5943 { | |
5944 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
5945 ue->e1 = ue->e1->semantic(sc); | |
5946 } | |
5947 } | |
5948 t1 = e1->type; | |
5949 } | |
5950 else if (e1->op == TOKsuper) | |
5951 { | |
5952 // Base class constructor call | |
5953 ClassDeclaration *cd = NULL; | |
5954 | |
5955 if (sc->func) | |
5956 cd = sc->func->toParent()->isClassDeclaration(); | |
5957 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
5958 { | |
5959 error("super class constructor call must be in a constructor"); | |
5960 type = Type::terror; | |
5961 return this; | |
5962 } | |
5963 else | |
5964 { | |
5965 f = cd->baseClass->ctor; | |
5966 if (!f) | |
5967 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
5968 type = Type::terror; | |
5969 return this; | |
5970 } | |
5971 else | |
5972 { | |
336 | 5973 if (!sc->intypeof) |
5974 { | |
159 | 5975 #if 0 |
336 | 5976 if (sc->callSuper & (CSXthis | CSXsuper)) |
5977 error("reference to this before super()"); | |
159 | 5978 #endif |
336 | 5979 if (sc->noctor || sc->callSuper & CSXlabel) |
5980 error("constructor calls not allowed in loops or after labels"); | |
5981 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
5982 error("multiple constructor calls"); | |
5983 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
5984 } | |
159 | 5985 |
5986 f = f->overloadResolve(loc, arguments); | |
5987 checkDeprecated(sc, f); | |
5988 e1 = new DotVarExp(e1->loc, e1, f); | |
5989 e1 = e1->semantic(sc); | |
5990 t1 = e1->type; | |
5991 } | |
5992 } | |
5993 } | |
5994 else if (e1->op == TOKthis) | |
5995 { | |
5996 // same class constructor call | |
5997 ClassDeclaration *cd = NULL; | |
5998 | |
5999 if (sc->func) | |
6000 cd = sc->func->toParent()->isClassDeclaration(); | |
6001 if (!cd || !sc->func->isCtorDeclaration()) | |
6002 { | |
6003 error("class constructor call must be in a constructor"); | |
6004 type = Type::terror; | |
6005 return this; | |
6006 } | |
6007 else | |
6008 { | |
336 | 6009 if (!sc->intypeof) |
6010 { | |
159 | 6011 #if 0 |
336 | 6012 if (sc->callSuper & (CSXthis | CSXsuper)) |
6013 error("reference to this before super()"); | |
159 | 6014 #endif |
336 | 6015 if (sc->noctor || sc->callSuper & CSXlabel) |
6016 error("constructor calls not allowed in loops or after labels"); | |
6017 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6018 error("multiple constructor calls"); | |
6019 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6020 } | |
159 | 6021 |
6022 f = cd->ctor; | |
6023 f = f->overloadResolve(loc, arguments); | |
6024 checkDeprecated(sc, f); | |
6025 e1 = new DotVarExp(e1->loc, e1, f); | |
6026 e1 = e1->semantic(sc); | |
6027 t1 = e1->type; | |
6028 | |
6029 // BUG: this should really be done by checking the static | |
6030 // call graph | |
6031 if (f == sc->func) | |
6032 error("cyclic constructor call"); | |
6033 } | |
6034 } | |
6035 else if (!t1) | |
6036 { | |
6037 error("function expected before (), not '%s'", e1->toChars()); | |
6038 type = Type::terror; | |
6039 return this; | |
6040 } | |
6041 else if (t1->ty != Tfunction) | |
6042 { | |
6043 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
|
6044 { 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
|
6045 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
|
6046 tf = (TypeFunction *)(td->next); |
159 | 6047 goto Lcheckargs; |
6048 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6049 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6050 { Expression *e; |
6051 | |
6052 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
|
6053 t1 = ((TypePointer *)t1)->next; |
159 | 6054 e->type = t1; |
6055 e1 = e; | |
6056 } | |
6057 else if (e1->op == TOKtemplate) | |
6058 { | |
6059 TemplateExp *te = (TemplateExp *)e1; | |
6060 f = te->td->deduceFunctionTemplate(sc, loc, NULL, arguments); | |
6061 if (!f) | |
6062 { type = Type::terror; | |
6063 return this; | |
6064 } | |
6065 if (f->needThis() && hasThis(sc)) | |
6066 { | |
6067 // Supply an implicit 'this', as in | |
6068 // this.ident | |
6069 | |
6070 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6071 goto Lagain; | |
6072 } | |
6073 | |
6074 e1 = new VarExp(loc, f); | |
6075 goto Lagain; | |
6076 } | |
6077 else | |
6078 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6079 type = Type::terror; | |
6080 return this; | |
6081 } | |
6082 } | |
6083 else if (e1->op == TOKvar) | |
6084 { | |
6085 // Do overload resolution | |
6086 VarExp *ve = (VarExp *)e1; | |
6087 | |
6088 f = ve->var->isFuncDeclaration(); | |
6089 assert(f); | |
6090 | |
6091 // Look to see if f is really a function template | |
6092 if (0 && !istemp && f->parent) | |
6093 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6094 | |
6095 if (ti && | |
6096 (ti->name == f->ident || | |
6097 ti->toAlias()->ident == f->ident) | |
6098 && | |
6099 ti->tempdecl) | |
6100 { | |
6101 /* This is so that one can refer to the enclosing | |
6102 * template, even if it has the same name as a member | |
6103 * of the template, if it has a !(arguments) | |
6104 */ | |
6105 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6106 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6107 tempdecl = tempdecl->overroot; // then get the start | |
6108 e1 = new TemplateExp(loc, tempdecl); | |
6109 istemp = 1; | |
6110 goto Lagain; | |
6111 } | |
6112 } | |
6113 | |
6114 f = f->overloadResolve(loc, arguments); | |
6115 checkDeprecated(sc, f); | |
6116 | |
6117 if (f->needThis() && hasThis(sc)) | |
6118 { | |
6119 // Supply an implicit 'this', as in | |
6120 // this.ident | |
6121 | |
6122 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6123 goto Lagain; | |
6124 } | |
6125 | |
6126 accessCheck(loc, sc, NULL, f); | |
6127 | |
6128 ve->var = f; | |
6129 ve->type = f->type; | |
6130 t1 = f->type; | |
6131 } | |
6132 assert(t1->ty == Tfunction); | |
6133 tf = (TypeFunction *)(t1); | |
6134 | |
6135 Lcheckargs: | |
6136 assert(tf->ty == Tfunction); | |
6137 type = tf->next; | |
6138 | |
6139 if (!arguments) | |
6140 arguments = new Expressions(); | |
6141 functionArguments(loc, sc, tf, arguments); | |
6142 | |
6143 assert(type); | |
6144 | |
6145 if (f && f->tintro) | |
6146 { | |
6147 Type *t = type; | |
6148 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
|
6149 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
|
6150 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6151 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
|
6152 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6153 type = tf->next; |
159 | 6154 return castTo(sc, t); |
6155 } | |
6156 } | |
6157 | |
6158 return this; | |
6159 } | |
6160 | |
6161 int CallExp::checkSideEffect(int flag) | |
6162 { | |
6163 return 1; | |
6164 } | |
6165 | |
6166 Expression *CallExp::toLvalue(Scope *sc, Expression *e) | |
6167 { | |
6168 if (type->toBasetype()->ty == Tstruct) | |
6169 return this; | |
6170 else | |
6171 return Expression::toLvalue(sc, e); | |
6172 } | |
6173 | |
6174 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6175 { int i; | |
6176 | |
6177 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6178 buf->writeByte('('); | |
6179 argsToCBuffer(buf, arguments, hgs); | |
6180 buf->writeByte(')'); | |
6181 } | |
6182 | |
6183 | |
6184 /************************************************************/ | |
6185 | |
6186 AddrExp::AddrExp(Loc loc, Expression *e) | |
6187 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6188 { | |
6189 } | |
6190 | |
6191 Expression *AddrExp::semantic(Scope *sc) | |
6192 { | |
6193 #if LOGSEMANTIC | |
6194 printf("AddrExp::semantic('%s')\n", toChars()); | |
6195 #endif | |
6196 if (!type) | |
6197 { | |
6198 UnaExp::semantic(sc); | |
6199 e1 = e1->toLvalue(sc, NULL); | |
6200 if (!e1->type) | |
6201 { | |
6202 error("cannot take address of %s", e1->toChars()); | |
6203 type = Type::tint32; | |
6204 return this; | |
6205 } | |
6206 type = e1->type->pointerTo(); | |
6207 | |
6208 // See if this should really be a delegate | |
6209 if (e1->op == TOKdotvar) | |
6210 { | |
6211 DotVarExp *dve = (DotVarExp *)e1; | |
6212 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6213 | |
6214 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
|
6215 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6216 e = e->semantic(sc); |
6217 return e; | |
6218 } | |
6219 } | |
6220 else if (e1->op == TOKvar) | |
6221 { | |
6222 VarExp *dve = (VarExp *)e1; | |
6223 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6224 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6225 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6226 // LDC |
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
|
6227 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
|
6228 { |
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
|
6229 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
|
6230 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
|
6231 } |
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
|
6232 |
159 | 6233 if (f && f->isNested()) |
6234 { Expression *e; | |
6235 | |
6236 e = new DelegateExp(loc, e1, f); | |
6237 e = e->semantic(sc); | |
6238 return e; | |
6239 } | |
6240 } | |
6241 else if (e1->op == TOKarray) | |
6242 { | |
6243 if (e1->type->toBasetype()->ty == Tbit) | |
6244 error("cannot take address of bit in array"); | |
6245 } | |
6246 return optimize(WANTvalue); | |
6247 } | |
6248 return this; | |
6249 } | |
6250 | |
6251 /************************************************************/ | |
6252 | |
6253 PtrExp::PtrExp(Loc loc, Expression *e) | |
6254 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6255 { | |
6256 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
|
6257 type = ((TypePointer *)e->type)->next; |
159 | 6258 } |
6259 | |
6260 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6261 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6262 { | |
6263 type = t; | |
6264 } | |
6265 | |
6266 Expression *PtrExp::semantic(Scope *sc) | |
6267 { Type *tb; | |
6268 | |
6269 #if LOGSEMANTIC | |
6270 printf("PtrExp::semantic('%s')\n", toChars()); | |
6271 #endif | |
6272 UnaExp::semantic(sc); | |
6273 e1 = resolveProperties(sc, e1); | |
6274 if (type) | |
6275 return this; | |
6276 if (!e1->type) | |
6277 printf("PtrExp::semantic('%s')\n", toChars()); | |
6278 tb = e1->type->toBasetype(); | |
6279 switch (tb->ty) | |
6280 { | |
6281 case Tpointer: | |
6282 type = tb->next; | |
6283 if (type->isbit()) | |
6284 { Expression *e; | |
6285 | |
6286 // Rewrite *p as p[0] | |
6287 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
6288 return e->semantic(sc); | |
6289 } | |
6290 break; | |
6291 | |
6292 case Tsarray: | |
6293 case Tarray: | |
6294 type = tb->next; | |
6295 e1 = e1->castTo(sc, type->pointerTo()); | |
6296 break; | |
6297 | |
6298 default: | |
6299 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
6300 type = Type::tint32; | |
6301 break; | |
6302 } | |
6303 rvalue(); | |
6304 return this; | |
6305 } | |
6306 | |
6307 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) | |
6308 { | |
6309 #if 0 | |
6310 tym = tybasic(e1->ET->Tty); | |
6311 if (!(tyscalar(tym) || | |
6312 tym == TYstruct || | |
6313 tym == TYarray && e->Eoper == TOKaddr)) | |
6314 synerr(EM_lvalue); // lvalue expected | |
6315 #endif | |
6316 return this; | |
6317 } | |
6318 | |
6319 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6320 { | |
6321 buf->writeByte('*'); | |
6322 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6323 } | |
6324 | |
6325 /************************************************************/ | |
6326 | |
6327 NegExp::NegExp(Loc loc, Expression *e) | |
6328 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
6329 { | |
6330 } | |
6331 | |
6332 Expression *NegExp::semantic(Scope *sc) | |
6333 { Expression *e; | |
6334 | |
6335 #if LOGSEMANTIC | |
6336 printf("NegExp::semantic('%s')\n", toChars()); | |
6337 #endif | |
6338 if (!type) | |
6339 { | |
6340 UnaExp::semantic(sc); | |
6341 e1 = resolveProperties(sc, e1); | |
6342 e = op_overload(sc); | |
6343 if (e) | |
6344 return e; | |
6345 | |
6346 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
|
6347 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
|
6348 e1->checkArithmetic(); |
159 | 6349 type = e1->type; |
6350 } | |
6351 return this; | |
6352 } | |
6353 | |
6354 /************************************************************/ | |
6355 | |
6356 UAddExp::UAddExp(Loc loc, Expression *e) | |
6357 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
6358 { | |
6359 } | |
6360 | |
6361 Expression *UAddExp::semantic(Scope *sc) | |
6362 { Expression *e; | |
6363 | |
6364 #if LOGSEMANTIC | |
6365 printf("UAddExp::semantic('%s')\n", toChars()); | |
6366 #endif | |
6367 assert(!type); | |
6368 UnaExp::semantic(sc); | |
6369 e1 = resolveProperties(sc, e1); | |
6370 e = op_overload(sc); | |
6371 if (e) | |
6372 return e; | |
6373 e1->checkNoBool(); | |
6374 e1->checkArithmetic(); | |
6375 return e1; | |
6376 } | |
6377 | |
6378 /************************************************************/ | |
6379 | |
6380 ComExp::ComExp(Loc loc, Expression *e) | |
6381 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6382 { | |
6383 } | |
6384 | |
6385 Expression *ComExp::semantic(Scope *sc) | |
6386 { Expression *e; | |
6387 | |
6388 if (!type) | |
6389 { | |
6390 UnaExp::semantic(sc); | |
6391 e1 = resolveProperties(sc, e1); | |
6392 e = op_overload(sc); | |
6393 if (e) | |
6394 return e; | |
6395 | |
6396 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
|
6397 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
|
6398 e1 = e1->checkIntegral(); |
159 | 6399 type = e1->type; |
6400 } | |
6401 return this; | |
6402 } | |
6403 | |
6404 /************************************************************/ | |
6405 | |
6406 NotExp::NotExp(Loc loc, Expression *e) | |
6407 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6408 { | |
6409 } | |
6410 | |
6411 Expression *NotExp::semantic(Scope *sc) | |
6412 { | |
6413 UnaExp::semantic(sc); | |
6414 e1 = resolveProperties(sc, e1); | |
6415 e1 = e1->checkToBoolean(); | |
6416 type = Type::tboolean; | |
6417 return this; | |
6418 } | |
6419 | |
6420 int NotExp::isBit() | |
6421 { | |
6422 return TRUE; | |
6423 } | |
6424 | |
6425 | |
6426 | |
6427 /************************************************************/ | |
6428 | |
6429 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6430 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6431 { | |
6432 type = t; | |
6433 } | |
6434 | |
6435 Expression *BoolExp::semantic(Scope *sc) | |
6436 { | |
6437 UnaExp::semantic(sc); | |
6438 e1 = resolveProperties(sc, e1); | |
6439 e1 = e1->checkToBoolean(); | |
6440 type = Type::tboolean; | |
6441 return this; | |
6442 } | |
6443 | |
6444 int BoolExp::isBit() | |
6445 { | |
6446 return TRUE; | |
6447 } | |
6448 | |
6449 /************************************************************/ | |
6450 | |
6451 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6452 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6453 { | |
6454 } | |
6455 | |
6456 Expression *DeleteExp::semantic(Scope *sc) | |
6457 { | |
6458 Type *tb; | |
6459 | |
6460 UnaExp::semantic(sc); | |
6461 e1 = resolveProperties(sc, e1); | |
6462 e1 = e1->toLvalue(sc, NULL); | |
6463 type = Type::tvoid; | |
6464 | |
6465 tb = e1->type->toBasetype(); | |
6466 switch (tb->ty) | |
6467 { case Tclass: | |
6468 { TypeClass *tc = (TypeClass *)tb; | |
6469 ClassDeclaration *cd = tc->sym; | |
6470 | |
6471 if (cd->isCOMinterface()) | |
6472 { /* Because COM classes are deleted by IUnknown.Release() | |
6473 */ | |
6474 error("cannot delete instance of COM interface %s", cd->toChars()); | |
6475 } | |
6476 break; | |
6477 } | |
6478 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
|
6479 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 6480 if (tb->ty == Tstruct) |
6481 { | |
6482 TypeStruct *ts = (TypeStruct *)tb; | |
6483 StructDeclaration *sd = ts->sym; | |
6484 FuncDeclaration *f = sd->aggDelete; | |
6485 | |
6486 if (f) | |
6487 { | |
6488 Type *tpv = Type::tvoid->pointerTo(); | |
6489 | |
6490 Expression *e = e1->castTo(sc, tpv); | |
6491 Expression *ec = new VarExp(loc, f); | |
6492 e = new CallExp(loc, ec, e); | |
6493 return e->semantic(sc); | |
6494 } | |
6495 } | |
6496 break; | |
6497 | |
6498 case Tarray: | |
6499 break; | |
6500 | |
6501 default: | |
6502 if (e1->op == TOKindex) | |
6503 { | |
6504 IndexExp *ae = (IndexExp *)(e1); | |
6505 Type *tb1 = ae->e1->type->toBasetype(); | |
6506 if (tb1->ty == Taarray) | |
6507 break; | |
6508 } | |
6509 error("cannot delete type %s", e1->type->toChars()); | |
6510 break; | |
6511 } | |
6512 | |
6513 if (e1->op == TOKindex) | |
6514 { | |
6515 IndexExp *ae = (IndexExp *)(e1); | |
6516 Type *tb1 = ae->e1->type->toBasetype(); | |
6517 if (tb1->ty == Taarray) | |
6518 { if (!global.params.useDeprecated) | |
6519 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6520 } | |
6521 } | |
6522 | |
6523 return this; | |
6524 } | |
6525 | |
6526 int DeleteExp::checkSideEffect(int flag) | |
6527 { | |
6528 return 1; | |
6529 } | |
6530 | |
6531 Expression *DeleteExp::checkToBoolean() | |
6532 { | |
6533 error("delete does not give a boolean result"); | |
6534 return this; | |
6535 } | |
6536 | |
6537 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6538 { | |
6539 buf->writestring("delete "); | |
6540 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6541 } | |
6542 | |
6543 /************************************************************/ | |
6544 | |
6545 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6546 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6547 { | |
6548 to = t; | |
6549 } | |
6550 | |
6551 Expression *CastExp::syntaxCopy() | |
6552 { | |
6553 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6554 } | |
6555 | |
6556 | |
6557 Expression *CastExp::semantic(Scope *sc) | |
6558 { Expression *e; | |
6559 BinExp *b; | |
6560 UnaExp *u; | |
6561 | |
6562 #if LOGSEMANTIC | |
6563 printf("CastExp::semantic('%s')\n", toChars()); | |
6564 #endif | |
6565 | |
6566 //static int x; assert(++x < 10); | |
6567 | |
6568 if (type) | |
6569 return this; | |
6570 UnaExp::semantic(sc); | |
6571 if (e1->type) // if not a tuple | |
6572 { | |
6573 e1 = resolveProperties(sc, e1); | |
6574 to = to->semantic(loc, sc); | |
6575 | |
6576 e = op_overload(sc); | |
6577 if (e) | |
6578 { | |
6579 return e->implicitCastTo(sc, to); | |
6580 } | |
6581 | |
6582 Type *tob = to->toBasetype(); | |
6583 if (tob->ty == Tstruct && | |
6584 !tob->equals(e1->type->toBasetype()) && | |
6585 ((TypeStruct *)to)->sym->search(0, Id::call, 0) | |
6586 ) | |
6587 { | |
6588 /* Look to replace: | |
6589 * cast(S)t | |
6590 * with: | |
6591 * S(t) | |
6592 */ | |
6593 | |
6594 // Rewrite as to.call(e1) | |
6595 e = new TypeExp(loc, to); | |
6596 e = new DotIdExp(loc, e, Id::call); | |
6597 e = new CallExp(loc, e, e1); | |
6598 e = e->semantic(sc); | |
6599 return e; | |
6600 } | |
6601 } | |
6602 e = e1->castTo(sc, to); | |
6603 return e; | |
6604 } | |
6605 | |
6606 int CastExp::checkSideEffect(int flag) | |
6607 { | |
6608 /* if not: | |
6609 * cast(void) | |
6610 * cast(classtype)func() | |
6611 */ | |
6612 if (!to->equals(Type::tvoid) && | |
6613 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
6614 return Expression::checkSideEffect(flag); | |
6615 return 1; | |
6616 } | |
6617 | |
6618 void CastExp::checkEscape() | |
6619 { Type *tb = type->toBasetype(); | |
6620 if (tb->ty == Tarray && e1->op == TOKvar && | |
6621 e1->type->toBasetype()->ty == Tsarray) | |
6622 { VarExp *ve = (VarExp *)e1; | |
6623 VarDeclaration *v = ve->var->isVarDeclaration(); | |
6624 if (v) | |
6625 { | |
6626 if (!v->isDataseg() && !v->isParameter()) | |
6627 error("escaping reference to local %s", v->toChars()); | |
6628 } | |
6629 } | |
6630 } | |
6631 | |
6632 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6633 { | |
6634 buf->writestring("cast("); | |
6635 to->toCBuffer(buf, NULL, hgs); | |
6636 buf->writeByte(')'); | |
6637 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6638 } | |
6639 | |
6640 | |
6641 /************************************************************/ | |
6642 | |
6643 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
6644 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
6645 { | |
6646 this->upr = upr; | |
6647 this->lwr = lwr; | |
6648 lengthVar = NULL; | |
6649 } | |
6650 | |
6651 Expression *SliceExp::syntaxCopy() | |
6652 { | |
6653 Expression *lwr = NULL; | |
6654 if (this->lwr) | |
6655 lwr = this->lwr->syntaxCopy(); | |
6656 | |
6657 Expression *upr = NULL; | |
6658 if (this->upr) | |
6659 upr = this->upr->syntaxCopy(); | |
6660 | |
6661 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
6662 } | |
6663 | |
6664 Expression *SliceExp::semantic(Scope *sc) | |
6665 { Expression *e; | |
6666 AggregateDeclaration *ad; | |
6667 //FuncDeclaration *fd; | |
6668 ScopeDsymbol *sym; | |
6669 | |
6670 #if LOGSEMANTIC | |
6671 printf("SliceExp::semantic('%s')\n", toChars()); | |
6672 #endif | |
6673 if (type) | |
6674 return this; | |
6675 | |
6676 UnaExp::semantic(sc); | |
6677 e1 = resolveProperties(sc, e1); | |
6678 | |
6679 e = this; | |
6680 | |
6681 Type *t = e1->type->toBasetype(); | |
6682 if (t->ty == Tpointer) | |
6683 { | |
6684 if (!lwr || !upr) | |
6685 error("need upper and lower bound to slice pointer"); | |
6686 } | |
6687 else if (t->ty == Tarray) | |
6688 { | |
6689 } | |
6690 else if (t->ty == Tsarray) | |
6691 { | |
6692 } | |
6693 else if (t->ty == Tclass) | |
6694 { | |
6695 ad = ((TypeClass *)t)->sym; | |
6696 goto L1; | |
6697 } | |
6698 else if (t->ty == Tstruct) | |
6699 { | |
6700 ad = ((TypeStruct *)t)->sym; | |
6701 | |
6702 L1: | |
6703 if (search_function(ad, Id::slice)) | |
6704 { | |
6705 // Rewrite as e1.slice(lwr, upr) | |
6706 e = new DotIdExp(loc, e1, Id::slice); | |
6707 | |
6708 if (lwr) | |
6709 { | |
6710 assert(upr); | |
6711 e = new CallExp(loc, e, lwr, upr); | |
6712 } | |
6713 else | |
6714 { assert(!upr); | |
6715 e = new CallExp(loc, e); | |
6716 } | |
6717 e = e->semantic(sc); | |
6718 return e; | |
6719 } | |
6720 goto Lerror; | |
6721 } | |
6722 else if (t->ty == Ttuple) | |
6723 { | |
6724 if (!lwr && !upr) | |
6725 return e1; | |
6726 if (!lwr || !upr) | |
6727 { error("need upper and lower bound to slice tuple"); | |
6728 goto Lerror; | |
6729 } | |
6730 } | |
6731 else | |
6732 goto Lerror; | |
6733 | |
6734 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
6735 { | |
6736 sym = new ArrayScopeSymbol(this); | |
6737 sym->loc = loc; | |
6738 sym->parent = sc->scopesym; | |
6739 sc = sc->push(sym); | |
6740 } | |
6741 | |
6742 if (lwr) | |
6743 { lwr = lwr->semantic(sc); | |
6744 lwr = resolveProperties(sc, lwr); | |
6745 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
6746 } | |
6747 if (upr) | |
6748 { upr = upr->semantic(sc); | |
6749 upr = resolveProperties(sc, upr); | |
6750 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
6751 } | |
6752 | |
6753 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
6754 sc->pop(); | |
6755 | |
6756 if (t->ty == Ttuple) | |
6757 { | |
6758 lwr = lwr->optimize(WANTvalue); | |
6759 upr = upr->optimize(WANTvalue); | |
6760 uinteger_t i1 = lwr->toUInteger(); | |
6761 uinteger_t i2 = upr->toUInteger(); | |
6762 | |
6763 size_t length; | |
6764 TupleExp *te; | |
6765 TypeTuple *tup; | |
6766 | |
6767 if (e1->op == TOKtuple) // slicing an expression tuple | |
6768 { te = (TupleExp *)e1; | |
6769 length = te->exps->dim; | |
6770 } | |
6771 else if (e1->op == TOKtype) // slicing a type tuple | |
6772 { tup = (TypeTuple *)t; | |
6773 length = Argument::dim(tup->arguments); | |
6774 } | |
6775 else | |
6776 assert(0); | |
6777 | |
6778 if (i1 <= i2 && i2 <= length) | |
6779 { size_t j1 = (size_t) i1; | |
6780 size_t j2 = (size_t) i2; | |
6781 | |
6782 if (e1->op == TOKtuple) | |
6783 { Expressions *exps = new Expressions; | |
6784 exps->setDim(j2 - j1); | |
6785 for (size_t i = 0; i < j2 - j1; i++) | |
6786 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
6787 exps->data[i] = (void *)e; | |
6788 } | |
6789 e = new TupleExp(loc, exps); | |
6790 } | |
6791 else | |
6792 { Arguments *args = new Arguments; | |
6793 args->reserve(j2 - j1); | |
6794 for (size_t i = j1; i < j2; i++) | |
6795 { Argument *arg = Argument::getNth(tup->arguments, i); | |
6796 args->push(arg); | |
6797 } | |
6798 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
6799 } | |
6800 e = e->semantic(sc); | |
6801 } | |
6802 else | |
6803 { | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
6804 error("string slice [%llu .. %llu] is out of bounds", i1, i2); |
159 | 6805 e = e1; |
6806 } | |
6807 return e; | |
6808 } | |
6809 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6810 type = t->nextOf()->arrayOf(); |
159 | 6811 return e; |
6812 | |
6813 Lerror: | |
6814 char *s; | |
6815 if (t->ty == Tvoid) | |
6816 s = e1->toChars(); | |
6817 else | |
6818 s = t->toChars(); | |
6819 error("%s cannot be sliced with []", s); | |
6820 type = Type::terror; | |
6821 return e; | |
6822 } | |
6823 | |
6824 void SliceExp::checkEscape() | |
6825 { | |
6826 e1->checkEscape(); | |
6827 } | |
6828 | |
6829 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) | |
6830 { | |
6831 return this; | |
6832 } | |
6833 | |
6834 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
6835 { | |
6836 error("slice expression %s is not a modifiable lvalue", toChars()); | |
6837 return this; | |
6838 } | |
6839 | |
6840 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6841 { | |
6842 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6843 buf->writeByte('['); | |
6844 if (upr || lwr) | |
6845 { | |
6846 if (lwr) | |
6847 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
6848 else | |
6849 buf->writeByte('0'); | |
6850 buf->writestring(".."); | |
6851 if (upr) | |
6852 expToCBuffer(buf, hgs, upr, PREC_assign); | |
6853 else | |
6854 buf->writestring("length"); // BUG: should be array.length | |
6855 } | |
6856 buf->writeByte(']'); | |
6857 } | |
6858 | |
6859 /********************** ArrayLength **************************************/ | |
6860 | |
6861 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
6862 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
6863 { | |
6864 } | |
6865 | |
6866 Expression *ArrayLengthExp::semantic(Scope *sc) | |
6867 { Expression *e; | |
6868 | |
6869 #if LOGSEMANTIC | |
6870 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
6871 #endif | |
6872 if (!type) | |
6873 { | |
6874 UnaExp::semantic(sc); | |
6875 e1 = resolveProperties(sc, e1); | |
6876 | |
6877 type = Type::tsize_t; | |
6878 } | |
6879 return this; | |
6880 } | |
6881 | |
6882 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6883 { | |
6884 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6885 buf->writestring(".length"); | |
6886 } | |
6887 | |
6888 /*********************** ArrayExp *************************************/ | |
6889 | |
6890 // e1 [ i1, i2, i3, ... ] | |
6891 | |
6892 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
6893 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
6894 { | |
6895 arguments = args; | |
6896 } | |
6897 | |
6898 Expression *ArrayExp::syntaxCopy() | |
6899 { | |
6900 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
6901 } | |
6902 | |
6903 Expression *ArrayExp::semantic(Scope *sc) | |
6904 { Expression *e; | |
6905 Type *t1; | |
6906 | |
6907 #if LOGSEMANTIC | |
6908 printf("ArrayExp::semantic('%s')\n", toChars()); | |
6909 #endif | |
6910 UnaExp::semantic(sc); | |
6911 e1 = resolveProperties(sc, e1); | |
6912 | |
6913 t1 = e1->type->toBasetype(); | |
6914 if (t1->ty != Tclass && t1->ty != Tstruct) | |
6915 { // Convert to IndexExp | |
6916 if (arguments->dim != 1) | |
6917 error("only one index allowed to index %s", t1->toChars()); | |
6918 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
6919 return e->semantic(sc); | |
6920 } | |
6921 | |
6922 // Run semantic() on each argument | |
6923 for (size_t i = 0; i < arguments->dim; i++) | |
6924 { e = (Expression *)arguments->data[i]; | |
6925 | |
6926 e = e->semantic(sc); | |
6927 if (!e->type) | |
6928 error("%s has no value", e->toChars()); | |
6929 arguments->data[i] = (void *)e; | |
6930 } | |
6931 | |
6932 expandTuples(arguments); | |
6933 assert(arguments && arguments->dim); | |
6934 | |
6935 e = op_overload(sc); | |
6936 if (!e) | |
6937 { error("no [] operator overload for type %s", e1->type->toChars()); | |
6938 e = e1; | |
6939 } | |
6940 return e; | |
6941 } | |
6942 | |
6943 | |
6944 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
6945 { | |
6946 if (type && type->toBasetype()->ty == Tvoid) | |
6947 error("voids have no value"); | |
6948 return this; | |
6949 } | |
6950 | |
6951 | |
6952 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6953 { int i; | |
6954 | |
6955 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6956 buf->writeByte('['); | |
6957 argsToCBuffer(buf, arguments, hgs); | |
6958 buf->writeByte(']'); | |
6959 } | |
6960 | |
6961 /************************* DotExp ***********************************/ | |
6962 | |
6963 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
6964 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
6965 { | |
6966 } | |
6967 | |
6968 Expression *DotExp::semantic(Scope *sc) | |
6969 { | |
6970 #if LOGSEMANTIC | |
6971 printf("DotExp::semantic('%s')\n", toChars()); | |
6972 if (type) printf("\ttype = %s\n", type->toChars()); | |
6973 #endif | |
6974 e1 = e1->semantic(sc); | |
6975 e2 = e2->semantic(sc); | |
6976 if (e2->op == TOKimport) | |
6977 { | |
6978 ScopeExp *se = (ScopeExp *)e2; | |
6979 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
6980 if (td) | |
6981 { Expression *e = new DotTemplateExp(loc, e1, td); | |
6982 e = e->semantic(sc); | |
6983 return e; | |
6984 } | |
6985 } | |
6986 if (!type) | |
6987 type = e2->type; | |
6988 return this; | |
6989 } | |
6990 | |
6991 | |
6992 /************************* CommaExp ***********************************/ | |
6993 | |
6994 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
6995 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
6996 { | |
6997 } | |
6998 | |
6999 Expression *CommaExp::semantic(Scope *sc) | |
7000 { | |
7001 if (!type) | |
7002 { BinExp::semanticp(sc); | |
7003 type = e2->type; | |
7004 } | |
7005 return this; | |
7006 } | |
7007 | |
7008 void CommaExp::checkEscape() | |
7009 { | |
7010 e2->checkEscape(); | |
7011 } | |
7012 | |
7013 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) | |
7014 { | |
7015 e2 = e2->toLvalue(sc, NULL); | |
7016 return this; | |
7017 } | |
7018 | |
7019 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7020 { | |
7021 e2 = e2->modifiableLvalue(sc, e); | |
7022 return this; | |
7023 } | |
7024 | |
7025 int CommaExp::isBool(int result) | |
7026 { | |
7027 return e2->isBool(result); | |
7028 } | |
7029 | |
7030 int CommaExp::checkSideEffect(int flag) | |
7031 { | |
7032 if (flag == 2) | |
7033 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7034 else | |
7035 { | |
7036 // Don't check e1 until we cast(void) the a,b code generation | |
7037 return e2->checkSideEffect(flag); | |
7038 } | |
7039 } | |
7040 | |
7041 /************************** IndexExp **********************************/ | |
7042 | |
7043 // e1 [ e2 ] | |
7044 | |
7045 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7046 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7047 { | |
7048 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7049 lengthVar = NULL; | |
7050 modifiable = 0; // assume it is an rvalue | |
7051 } | |
7052 | |
7053 Expression *IndexExp::semantic(Scope *sc) | |
7054 { Expression *e; | |
7055 BinExp *b; | |
7056 UnaExp *u; | |
7057 Type *t1; | |
7058 ScopeDsymbol *sym; | |
7059 | |
7060 #if LOGSEMANTIC | |
7061 printf("IndexExp::semantic('%s')\n", toChars()); | |
7062 #endif | |
7063 if (type) | |
7064 return this; | |
7065 if (!e1->type) | |
7066 e1 = e1->semantic(sc); | |
7067 assert(e1->type); // semantic() should already be run on it | |
7068 e = this; | |
7069 | |
7070 // Note that unlike C we do not implement the int[ptr] | |
7071 | |
7072 t1 = e1->type->toBasetype(); | |
7073 | |
7074 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7075 { // Create scope for 'length' variable | |
7076 sym = new ArrayScopeSymbol(this); | |
7077 sym->loc = loc; | |
7078 sym->parent = sc->scopesym; | |
7079 sc = sc->push(sym); | |
7080 } | |
7081 | |
7082 e2 = e2->semantic(sc); | |
7083 if (!e2->type) | |
7084 { | |
7085 error("%s has no value", e2->toChars()); | |
7086 e2->type = Type::terror; | |
7087 } | |
7088 e2 = resolveProperties(sc, e2); | |
7089 | |
7090 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7091 sc = sc->pop(); | |
7092 | |
7093 switch (t1->ty) | |
7094 { | |
7095 case Tpointer: | |
7096 case Tarray: | |
7097 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7098 e->type = t1->next; | |
7099 break; | |
7100 | |
7101 case Tsarray: | |
7102 { | |
7103 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7104 | |
7105 TypeSArray *tsa = (TypeSArray *)t1; | |
7106 | |
7107 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7108 // Do compile time array bounds checking if possible | |
7109 e2 = e2->optimize(WANTvalue); | |
7110 if (e2->op == TOKint64) | |
7111 { | |
7112 integer_t index = e2->toInteger(); | |
7113 integer_t length = tsa->dim->toInteger(); | |
7114 if (index < 0 || index >= length) | |
7115 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7116 index, length); | |
7117 } | |
7118 #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
|
7119 e->type = t1->nextOf(); |
159 | 7120 break; |
7121 } | |
7122 | |
7123 case Taarray: | |
7124 { TypeAArray *taa = (TypeAArray *)t1; | |
7125 | |
7126 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7127 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7128 type = taa->next; | |
7129 break; | |
7130 } | |
7131 | |
7132 case Ttuple: | |
7133 { | |
7134 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7135 e2 = e2->optimize(WANTvalue); | |
7136 uinteger_t index = e2->toUInteger(); | |
7137 size_t length; | |
7138 TupleExp *te; | |
7139 TypeTuple *tup; | |
7140 | |
7141 if (e1->op == TOKtuple) | |
7142 { te = (TupleExp *)e1; | |
7143 length = te->exps->dim; | |
7144 } | |
7145 else if (e1->op == TOKtype) | |
7146 { | |
7147 tup = (TypeTuple *)t1; | |
7148 length = Argument::dim(tup->arguments); | |
7149 } | |
7150 else | |
7151 assert(0); | |
7152 | |
7153 if (index < length) | |
7154 { | |
7155 | |
7156 if (e1->op == TOKtuple) | |
7157 e = (Expression *)te->exps->data[(size_t)index]; | |
7158 else | |
7159 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
7160 } | |
7161 else | |
7162 { | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7163 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
|
7164 index, length); |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7165 e = e1; |
159 | 7166 } |
7167 break; | |
7168 } | |
7169 | |
7170 default: | |
7171 error("%s must be an array or pointer type, not %s", | |
7172 e1->toChars(), e1->type->toChars()); | |
7173 type = Type::tint32; | |
7174 break; | |
7175 } | |
7176 return e; | |
7177 } | |
7178 | |
7179 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) | |
7180 { | |
7181 // if (type && type->toBasetype()->ty == Tvoid) | |
7182 // error("voids have no value"); | |
7183 return this; | |
7184 } | |
7185 | |
7186 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
7187 { | |
7188 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
7189 modifiable = 1; | |
7190 if (e1->op == TOKstring) | |
7191 error("string literals are immutable"); | |
7192 if (e1->type->toBasetype()->ty == Taarray) | |
7193 e1 = e1->modifiableLvalue(sc, e1); | |
7194 return toLvalue(sc, e); | |
7195 } | |
7196 | |
7197 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7198 { | |
7199 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7200 buf->writeByte('['); | |
7201 expToCBuffer(buf, hgs, e2, PREC_assign); | |
7202 buf->writeByte(']'); | |
7203 } | |
7204 | |
7205 | |
7206 /************************* PostExp ***********************************/ | |
7207 | |
7208 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
7209 : BinExp(loc, op, sizeof(PostExp), e, | |
7210 new IntegerExp(loc, 1, Type::tint32)) | |
7211 { | |
7212 } | |
7213 | |
7214 Expression *PostExp::semantic(Scope *sc) | |
7215 { Expression *e = this; | |
7216 | |
7217 if (!type) | |
7218 { | |
7219 BinExp::semantic(sc); | |
7220 e2 = resolveProperties(sc, e2); | |
7221 | |
7222 e = op_overload(sc); | |
7223 if (e) | |
7224 return e; | |
7225 | |
7226 e = this; | |
7227 e1 = e1->modifiableLvalue(sc, e1); | |
7228 e1->checkScalar(); | |
7229 e1->checkNoBool(); | |
7230 if (e1->type->ty == Tpointer) | |
7231 e = scaleFactor(sc); | |
7232 else | |
7233 e2 = e2->castTo(sc, e1->type); | |
7234 e->type = e1->type; | |
7235 } | |
7236 return e; | |
7237 } | |
7238 | |
7239 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7240 { | |
7241 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7242 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
7243 } | |
7244 | |
7245 /************************************************************/ | |
7246 | |
7247 /* Can be TOKconstruct too */ | |
7248 | |
7249 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
7250 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
7251 { | |
7252 ismemset = 0; | |
7253 } | |
7254 | |
7255 Expression *AssignExp::semantic(Scope *sc) | |
7256 { Type *t1; | |
7257 Expression *e1old = e1; | |
7258 | |
7259 #if LOGSEMANTIC | |
7260 printf("AssignExp::semantic('%s')\n", toChars()); | |
7261 #endif | |
7262 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
7263 | |
7264 /* Look for operator overloading of a[i]=value. | |
7265 * Do it before semantic() otherwise the a[i] will have been | |
7266 * converted to a.opIndex() already. | |
7267 */ | |
7268 if (e1->op == TOKarray) | |
7269 { Type *t1; | |
7270 ArrayExp *ae = (ArrayExp *)e1; | |
7271 AggregateDeclaration *ad; | |
7272 Identifier *id = Id::index; | |
7273 | |
7274 ae->e1 = ae->e1->semantic(sc); | |
7275 t1 = ae->e1->type->toBasetype(); | |
7276 if (t1->ty == Tstruct) | |
7277 { | |
7278 ad = ((TypeStruct *)t1)->sym; | |
7279 goto L1; | |
7280 } | |
7281 else if (t1->ty == Tclass) | |
7282 { | |
7283 ad = ((TypeClass *)t1)->sym; | |
7284 L1: | |
7285 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
7286 if (search_function(ad, Id::indexass)) | |
7287 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
7288 Expressions *a = (Expressions *)ae->arguments->copy(); | |
7289 | |
7290 a->insert(0, e2); | |
7291 e = new CallExp(loc, e, a); | |
7292 e = e->semantic(sc); | |
7293 return e; | |
7294 } | |
7295 else | |
7296 { | |
7297 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
7298 if (search_function(ad, id)) | |
7299 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
7300 | |
7301 if (1 || !global.params.useDeprecated) | |
7302 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
7303 | |
7304 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
7305 e = e->semantic(sc); | |
7306 return e; | |
7307 } | |
7308 } | |
7309 } | |
7310 } | |
7311 /* Look for operator overloading of a[i..j]=value. | |
7312 * Do it before semantic() otherwise the a[i..j] will have been | |
7313 * converted to a.opSlice() already. | |
7314 */ | |
7315 if (e1->op == TOKslice) | |
7316 { Type *t1; | |
7317 SliceExp *ae = (SliceExp *)e1; | |
7318 AggregateDeclaration *ad; | |
7319 Identifier *id = Id::index; | |
7320 | |
7321 ae->e1 = ae->e1->semantic(sc); | |
7322 ae->e1 = resolveProperties(sc, ae->e1); | |
7323 t1 = ae->e1->type->toBasetype(); | |
7324 if (t1->ty == Tstruct) | |
7325 { | |
7326 ad = ((TypeStruct *)t1)->sym; | |
7327 goto L2; | |
7328 } | |
7329 else if (t1->ty == Tclass) | |
7330 { | |
7331 ad = ((TypeClass *)t1)->sym; | |
7332 L2: | |
7333 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
7334 if (search_function(ad, Id::sliceass)) | |
7335 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
7336 Expressions *a = new Expressions(); | |
7337 | |
7338 a->push(e2); | |
7339 if (ae->lwr) | |
7340 { a->push(ae->lwr); | |
7341 assert(ae->upr); | |
7342 a->push(ae->upr); | |
7343 } | |
7344 else | |
7345 assert(!ae->upr); | |
7346 e = new CallExp(loc, e, a); | |
7347 e = e->semantic(sc); | |
7348 return e; | |
7349 } | |
7350 } | |
7351 } | |
7352 | |
7353 BinExp::semantic(sc); | |
7354 e2 = resolveProperties(sc, e2); | |
7355 assert(e1->type); | |
7356 | |
7357 /* Rewrite tuple assignment as a tuple of assignments. | |
7358 */ | |
7359 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
7360 { TupleExp *tup1 = (TupleExp *)e1; | |
7361 TupleExp *tup2 = (TupleExp *)e2; | |
7362 size_t dim = tup1->exps->dim; | |
7363 if (dim != tup2->exps->dim) | |
7364 { | |
7365 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
7366 } | |
7367 else | |
7368 { Expressions *exps = new Expressions; | |
7369 exps->setDim(dim); | |
7370 | |
7371 for (int i = 0; i < dim; i++) | |
7372 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
7373 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
7374 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
7375 } | |
7376 Expression *e = new TupleExp(loc, exps); | |
7377 e = e->semantic(sc); | |
7378 return e; | |
7379 } | |
7380 } | |
7381 | |
7382 t1 = e1->type->toBasetype(); | |
7383 | |
7384 if (t1->ty == Tfunction) | |
7385 { // Rewrite f=value to f(value) | |
7386 Expression *e; | |
7387 | |
7388 e = new CallExp(loc, e1, e2); | |
7389 e = e->semantic(sc); | |
7390 return e; | |
7391 } | |
7392 | |
7393 /* If it is an assignment from a 'foreign' type, | |
7394 * check for operator overloading. | |
7395 */ | |
7396 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7397 { | |
7398 if (!e2->type->implicitConvTo(e1->type)) | |
7399 { | |
7400 Expression *e = op_overload(sc); | |
7401 if (e) | |
7402 return e; | |
7403 } | |
7404 } | |
7405 | |
7406 e2->rvalue(); | |
7407 | |
7408 if (e1->op == TOKarraylength) | |
7409 { | |
7410 // e1 is not an lvalue, but we let code generator handle it | |
7411 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7412 | |
7413 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
7414 } | |
7415 else if (e1->op == TOKslice) | |
7416 ; | |
7417 else | |
7418 { // Try to do a decent error message with the expression | |
7419 // before it got constant folded | |
7420 e1 = e1->modifiableLvalue(sc, e1old); | |
7421 } | |
7422 | |
7423 if (e1->op == TOKslice && | |
7424 t1->nextOf() && | |
7425 e2->implicitConvTo(t1->nextOf()) | |
7426 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
7427 ) | |
7428 { // memset | |
7429 ismemset = 1; // make it easy for back end to tell what this is | |
7430 e2 = e2->implicitCastTo(sc, t1->next); | |
7431 } | |
7432 else if (t1->ty == Tsarray) | |
7433 { | |
7434 error("cannot assign to static array %s", e1->toChars()); | |
7435 } | |
7436 else | |
7437 { | |
7438 e2 = e2->implicitCastTo(sc, e1->type); | |
7439 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7440 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7441 /* 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
|
7442 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7443 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
|
7444 (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
|
7445 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
|
7446 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
|
7447 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
|
7448 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
|
7449 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7450 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
|
7451 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
|
7452 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7453 |
159 | 7454 type = e1->type; |
7455 assert(type); | |
7456 return this; | |
7457 } | |
7458 | |
7459 Expression *AssignExp::checkToBoolean() | |
7460 { | |
7461 // Things like: | |
7462 // if (a = b) ... | |
7463 // are usually mistakes. | |
7464 | |
7465 error("'=' does not give a boolean result"); | |
7466 return this; | |
7467 } | |
7468 | |
7469 /************************************************************/ | |
7470 | |
7471 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7472 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7473 { | |
7474 } | |
7475 | |
7476 Expression *AddAssignExp::semantic(Scope *sc) | |
7477 { Expression *e; | |
7478 | |
7479 if (type) | |
7480 return this; | |
7481 | |
7482 BinExp::semantic(sc); | |
7483 e2 = resolveProperties(sc, e2); | |
7484 | |
7485 e = op_overload(sc); | |
7486 if (e) | |
7487 return e; | |
7488 | |
7489 Type *tb1 = e1->type->toBasetype(); | |
7490 Type *tb2 = e2->type->toBasetype(); | |
7491 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7492 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
|
7493 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7494 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
|
7495 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
|
7496 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
|
7497 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7498 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7499 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7500 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
|
7501 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7502 |
159 | 7503 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
7504 (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
|
7505 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 7506 ) |
7507 { | |
7508 type = e1->type; | |
7509 e = this; | |
7510 } | |
7511 else | |
7512 { | |
7513 e1->checkScalar(); | |
7514 e1->checkNoBool(); | |
7515 if (tb1->ty == Tpointer && tb2->isintegral()) | |
7516 e = scaleFactor(sc); | |
7517 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
7518 { | |
7519 #if 0 | |
7520 // Need to rethink this | |
7521 if (e1->op != TOKvar) | |
7522 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
7523 VarDeclaration *v; | |
7524 Expression *ea; | |
7525 Expression *ex; | |
7526 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7527 Identifier *id = Lexer::uniqueId("__name"); |
159 | 7528 |
7529 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
7530 v->semantic(sc); | |
7531 if (!sc->insert(v)) | |
7532 assert(0); | |
7533 v->parent = sc->func; | |
7534 | |
7535 ea = new AddrExp(loc, e1); | |
7536 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
7537 | |
7538 ex = new VarExp(loc, v); | |
7539 ex = new PtrExp(loc, ex); | |
7540 e = new AddExp(loc, ex, e2); | |
7541 e = new CastExp(loc, e, e1->type); | |
7542 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
7543 | |
7544 e = new CommaExp(loc, ea, e); | |
7545 } | |
7546 else | |
7547 #endif | |
7548 { // Rewrite e1+=e2 to e1=e1+e2 | |
7549 // BUG: doesn't account for side effects in e1 | |
7550 // BUG: other assignment operators for bits aren't handled at all | |
7551 e = new AddExp(loc, e1, e2); | |
7552 e = new CastExp(loc, e, e1->type); | |
7553 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
7554 } | |
7555 e = e->semantic(sc); | |
7556 } | |
7557 else | |
7558 { | |
7559 type = e1->type; | |
7560 typeCombine(sc); | |
7561 e1->checkArithmetic(); | |
7562 e2->checkArithmetic(); | |
7563 if (type->isreal() || type->isimaginary()) | |
7564 { | |
7565 assert(global.errors || e2->type->isfloating()); | |
7566 e2 = e2->castTo(sc, e1->type); | |
7567 } | |
7568 e = this; | |
7569 } | |
7570 } | |
7571 return e; | |
7572 } | |
7573 | |
7574 /************************************************************/ | |
7575 | |
7576 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7577 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
7578 { | |
7579 } | |
7580 | |
7581 Expression *MinAssignExp::semantic(Scope *sc) | |
7582 { Expression *e; | |
7583 | |
7584 if (type) | |
7585 return this; | |
7586 | |
7587 BinExp::semantic(sc); | |
7588 e2 = resolveProperties(sc, e2); | |
7589 | |
7590 e = op_overload(sc); | |
7591 if (e) | |
7592 return e; | |
7593 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7594 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
|
7595 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7596 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
|
7597 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
|
7598 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
|
7599 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7600 |
159 | 7601 e1 = e1->modifiableLvalue(sc, e1); |
7602 e1->checkScalar(); | |
7603 e1->checkNoBool(); | |
7604 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
7605 e = scaleFactor(sc); | |
7606 else | |
7607 { | |
7608 e1 = e1->checkArithmetic(); | |
7609 e2 = e2->checkArithmetic(); | |
7610 type = e1->type; | |
7611 typeCombine(sc); | |
7612 if (type->isreal() || type->isimaginary()) | |
7613 { | |
7614 assert(e2->type->isfloating()); | |
7615 e2 = e2->castTo(sc, e1->type); | |
7616 } | |
7617 e = this; | |
7618 } | |
7619 return e; | |
7620 } | |
7621 | |
7622 /************************************************************/ | |
7623 | |
7624 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7625 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
7626 { | |
7627 } | |
7628 | |
7629 Expression *CatAssignExp::semantic(Scope *sc) | |
7630 { Expression *e; | |
7631 | |
7632 BinExp::semantic(sc); | |
7633 e2 = resolveProperties(sc, e2); | |
7634 | |
7635 e = op_overload(sc); | |
7636 if (e) | |
7637 return e; | |
7638 | |
7639 if (e1->op == TOKslice) | |
7640 { SliceExp *se = (SliceExp *)e1; | |
7641 | |
7642 if (se->e1->type->toBasetype()->ty == Tsarray) | |
7643 error("cannot append to static array %s", se->e1->type->toChars()); | |
7644 } | |
7645 | |
7646 e1 = e1->modifiableLvalue(sc, e1); | |
7647 | |
7648 Type *tb1 = e1->type->toBasetype(); | |
7649 Type *tb2 = e2->type->toBasetype(); | |
7650 | |
7651 e2->rvalue(); | |
7652 | |
7653 if ((tb1->ty == Tarray) && | |
7654 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
7655 e2->implicitConvTo(e1->type) | |
7656 //e1->type->next->equals(e2->type->next) | |
7657 ) | |
7658 { // Append array | |
7659 e2 = e2->castTo(sc, e1->type); | |
7660 type = e1->type; | |
7661 e = this; | |
7662 } | |
7663 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
|
7664 e2->implicitConvTo(tb1->nextOf()) |
159 | 7665 ) |
7666 { // 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
|
7667 e2 = e2->castTo(sc, tb1->nextOf()); |
159 | 7668 type = e1->type; |
7669 e = this; | |
7670 } | |
7671 else | |
7672 { | |
7673 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
7674 type = Type::tint32; | |
7675 e = this; | |
7676 } | |
7677 return e; | |
7678 } | |
7679 | |
7680 /************************************************************/ | |
7681 | |
7682 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7683 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
7684 { | |
7685 } | |
7686 | |
7687 Expression *MulAssignExp::semantic(Scope *sc) | |
7688 { Expression *e; | |
7689 | |
7690 BinExp::semantic(sc); | |
7691 e2 = resolveProperties(sc, e2); | |
7692 | |
7693 e = op_overload(sc); | |
7694 if (e) | |
7695 return e; | |
7696 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7697 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
|
7698 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7699 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
|
7700 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
|
7701 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
|
7702 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7703 |
159 | 7704 e1 = e1->modifiableLvalue(sc, e1); |
7705 e1->checkScalar(); | |
7706 e1->checkNoBool(); | |
7707 type = e1->type; | |
7708 typeCombine(sc); | |
7709 e1->checkArithmetic(); | |
7710 e2->checkArithmetic(); | |
7711 if (e2->type->isfloating()) | |
7712 { Type *t1; | |
7713 Type *t2; | |
7714 | |
7715 t1 = e1->type; | |
7716 t2 = e2->type; | |
7717 if (t1->isreal()) | |
7718 { | |
7719 if (t2->isimaginary() || t2->iscomplex()) | |
7720 { | |
7721 e2 = e2->castTo(sc, t1); | |
7722 } | |
7723 } | |
7724 else if (t1->isimaginary()) | |
7725 { | |
7726 if (t2->isimaginary() || t2->iscomplex()) | |
7727 { | |
7728 switch (t1->ty) | |
7729 { | |
7730 case Timaginary32: t2 = Type::tfloat32; break; | |
7731 case Timaginary64: t2 = Type::tfloat64; break; | |
7732 case Timaginary80: t2 = Type::tfloat80; break; | |
7733 default: | |
7734 assert(0); | |
7735 } | |
7736 e2 = e2->castTo(sc, t2); | |
7737 } | |
7738 } | |
7739 } | |
7740 return this; | |
7741 } | |
7742 | |
7743 /************************************************************/ | |
7744 | |
7745 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7746 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
7747 { | |
7748 } | |
7749 | |
7750 Expression *DivAssignExp::semantic(Scope *sc) | |
7751 { Expression *e; | |
7752 | |
7753 BinExp::semantic(sc); | |
7754 e2 = resolveProperties(sc, e2); | |
7755 | |
7756 e = op_overload(sc); | |
7757 if (e) | |
7758 return e; | |
7759 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7760 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
|
7761 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7762 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
|
7763 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
|
7764 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
|
7765 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7766 |
159 | 7767 e1 = e1->modifiableLvalue(sc, e1); |
7768 e1->checkScalar(); | |
7769 e1->checkNoBool(); | |
7770 type = e1->type; | |
7771 typeCombine(sc); | |
7772 e1->checkArithmetic(); | |
7773 e2->checkArithmetic(); | |
7774 if (e2->type->isimaginary()) | |
7775 { Type *t1; | |
7776 Type *t2; | |
7777 | |
7778 t1 = e1->type; | |
7779 if (t1->isreal()) | |
7780 { // x/iv = i(-x/v) | |
7781 // Therefore, the result is 0 | |
7782 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
7783 e2->type = t1; | |
7784 e = new AssignExp(loc, e1, e2); | |
7785 e->type = t1; | |
7786 return e; | |
7787 } | |
7788 else if (t1->isimaginary()) | |
7789 { Expression *e; | |
7790 | |
7791 switch (t1->ty) | |
7792 { | |
7793 case Timaginary32: t2 = Type::tfloat32; break; | |
7794 case Timaginary64: t2 = Type::tfloat64; break; | |
7795 case Timaginary80: t2 = Type::tfloat80; break; | |
7796 default: | |
7797 assert(0); | |
7798 } | |
7799 e2 = e2->castTo(sc, t2); | |
7800 e = new AssignExp(loc, e1, e2); | |
7801 e->type = t1; | |
7802 return e; | |
7803 } | |
7804 } | |
7805 return this; | |
7806 } | |
7807 | |
7808 /************************************************************/ | |
7809 | |
7810 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7811 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
7812 { | |
7813 } | |
7814 | |
7815 Expression *ModAssignExp::semantic(Scope *sc) | |
7816 { | |
7817 return commonSemanticAssign(sc); | |
7818 } | |
7819 | |
7820 /************************************************************/ | |
7821 | |
7822 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7823 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
7824 { | |
7825 } | |
7826 | |
7827 Expression *ShlAssignExp::semantic(Scope *sc) | |
7828 { Expression *e; | |
7829 | |
7830 //printf("ShlAssignExp::semantic()\n"); | |
7831 BinExp::semantic(sc); | |
7832 e2 = resolveProperties(sc, e2); | |
7833 | |
7834 e = op_overload(sc); | |
7835 if (e) | |
7836 return e; | |
7837 | |
7838 e1 = e1->modifiableLvalue(sc, e1); | |
7839 e1->checkScalar(); | |
7840 e1->checkNoBool(); | |
7841 type = e1->type; | |
7842 typeCombine(sc); | |
7843 e1->checkIntegral(); | |
7844 e2 = e2->checkIntegral(); | |
7845 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
7846 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 7847 return this; |
7848 } | |
7849 | |
7850 /************************************************************/ | |
7851 | |
7852 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7853 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
7854 { | |
7855 } | |
7856 | |
7857 Expression *ShrAssignExp::semantic(Scope *sc) | |
7858 { Expression *e; | |
7859 | |
7860 BinExp::semantic(sc); | |
7861 e2 = resolveProperties(sc, e2); | |
7862 | |
7863 e = op_overload(sc); | |
7864 if (e) | |
7865 return e; | |
7866 | |
7867 e1 = e1->modifiableLvalue(sc, e1); | |
7868 e1->checkScalar(); | |
7869 e1->checkNoBool(); | |
7870 type = e1->type; | |
7871 typeCombine(sc); | |
7872 e1->checkIntegral(); | |
7873 e2 = e2->checkIntegral(); | |
7874 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
7875 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 7876 return this; |
7877 } | |
7878 | |
7879 /************************************************************/ | |
7880 | |
7881 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7882 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
7883 { | |
7884 } | |
7885 | |
7886 Expression *UshrAssignExp::semantic(Scope *sc) | |
7887 { Expression *e; | |
7888 | |
7889 BinExp::semantic(sc); | |
7890 e2 = resolveProperties(sc, e2); | |
7891 | |
7892 e = op_overload(sc); | |
7893 if (e) | |
7894 return e; | |
7895 | |
7896 e1 = e1->modifiableLvalue(sc, e1); | |
7897 e1->checkScalar(); | |
7898 e1->checkNoBool(); | |
7899 type = e1->type; | |
7900 typeCombine(sc); | |
7901 e1->checkIntegral(); | |
7902 e2 = e2->checkIntegral(); | |
7903 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
7904 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 7905 return this; |
7906 } | |
7907 | |
7908 /************************************************************/ | |
7909 | |
7910 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7911 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
7912 { | |
7913 } | |
7914 | |
7915 Expression *AndAssignExp::semantic(Scope *sc) | |
7916 { | |
7917 return commonSemanticAssignIntegral(sc); | |
7918 } | |
7919 | |
7920 /************************************************************/ | |
7921 | |
7922 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7923 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
7924 { | |
7925 } | |
7926 | |
7927 Expression *OrAssignExp::semantic(Scope *sc) | |
7928 { | |
7929 return commonSemanticAssignIntegral(sc); | |
7930 } | |
7931 | |
7932 /************************************************************/ | |
7933 | |
7934 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7935 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
7936 { | |
7937 } | |
7938 | |
7939 Expression *XorAssignExp::semantic(Scope *sc) | |
7940 { | |
7941 return commonSemanticAssignIntegral(sc); | |
7942 } | |
7943 | |
7944 /************************* AddExp *****************************/ | |
7945 | |
7946 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
7947 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
7948 { | |
7949 } | |
7950 | |
7951 Expression *AddExp::semantic(Scope *sc) | |
7952 { Expression *e; | |
7953 | |
7954 #if LOGSEMANTIC | |
7955 printf("AddExp::semantic('%s')\n", toChars()); | |
7956 #endif | |
7957 if (!type) | |
7958 { | |
7959 BinExp::semanticp(sc); | |
7960 | |
7961 e = op_overload(sc); | |
7962 if (e) | |
7963 return e; | |
7964 | |
7965 Type *tb1 = e1->type->toBasetype(); | |
7966 Type *tb2 = e2->type->toBasetype(); | |
7967 | |
7968 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
7969 (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
|
7970 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 7971 ) |
7972 { | |
7973 type = e1->type; | |
7974 e = this; | |
7975 } | |
7976 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
7977 tb2->ty == Tpointer && e1->type->isintegral()) | |
7978 e = scaleFactor(sc); | |
7979 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
7980 { | |
7981 incompatibleTypes(); | |
7982 type = e1->type; | |
7983 e = this; | |
7984 } | |
7985 else | |
7986 { | |
7987 typeCombine(sc); | |
7988 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
7989 (e1->type->isimaginary() && e2->type->isreal())) | |
7990 { | |
7991 switch (type->toBasetype()->ty) | |
7992 { | |
7993 case Tfloat32: | |
7994 case Timaginary32: | |
7995 type = Type::tcomplex32; | |
7996 break; | |
7997 | |
7998 case Tfloat64: | |
7999 case Timaginary64: | |
8000 type = Type::tcomplex64; | |
8001 break; | |
8002 | |
8003 case Tfloat80: | |
8004 case Timaginary80: | |
8005 type = Type::tcomplex80; | |
8006 break; | |
8007 | |
8008 default: | |
8009 assert(0); | |
8010 } | |
8011 } | |
8012 e = this; | |
8013 } | |
8014 return e; | |
8015 } | |
8016 return this; | |
8017 } | |
8018 | |
8019 /************************************************************/ | |
8020 | |
8021 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8022 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8023 { | |
8024 } | |
8025 | |
8026 Expression *MinExp::semantic(Scope *sc) | |
8027 { Expression *e; | |
8028 Type *t1; | |
8029 Type *t2; | |
8030 | |
8031 #if LOGSEMANTIC | |
8032 printf("MinExp::semantic('%s')\n", toChars()); | |
8033 #endif | |
8034 if (type) | |
8035 return this; | |
8036 | |
8037 BinExp::semanticp(sc); | |
8038 | |
8039 e = op_overload(sc); | |
8040 if (e) | |
8041 return e; | |
8042 | |
8043 e = this; | |
8044 t1 = e1->type->toBasetype(); | |
8045 t2 = e2->type->toBasetype(); | |
8046 if (t1->ty == Tpointer) | |
8047 { | |
8048 if (t2->ty == Tpointer) | |
8049 { // Need to divide the result by the stride | |
8050 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8051 d_int64 stride; | |
8052 Expression *e; | |
8053 | |
8054 typeCombine(sc); // make sure pointer types are compatible | |
8055 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
|
8056 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
|
8057 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
|
8058 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8059 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
|
8060 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8061 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8062 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8063 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
|
8064 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
|
8065 } |
159 | 8066 return e; |
8067 } | |
8068 else if (t2->isintegral()) | |
8069 e = scaleFactor(sc); | |
8070 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
|
8071 { error("incompatible types for minus"); |
159 | 8072 return new IntegerExp(0); |
8073 } | |
8074 } | |
8075 else if (t2->ty == Tpointer) | |
8076 { | |
8077 type = e2->type; | |
8078 error("can't subtract pointer from %s", e1->type->toChars()); | |
8079 return new IntegerExp(0); | |
8080 } | |
8081 else | |
8082 { | |
8083 typeCombine(sc); | |
8084 t1 = e1->type->toBasetype(); | |
8085 t2 = e2->type->toBasetype(); | |
8086 if ((t1->isreal() && t2->isimaginary()) || | |
8087 (t1->isimaginary() && t2->isreal())) | |
8088 { | |
8089 switch (type->ty) | |
8090 { | |
8091 case Tfloat32: | |
8092 case Timaginary32: | |
8093 type = Type::tcomplex32; | |
8094 break; | |
8095 | |
8096 case Tfloat64: | |
8097 case Timaginary64: | |
8098 type = Type::tcomplex64; | |
8099 break; | |
8100 | |
8101 case Tfloat80: | |
8102 case Timaginary80: | |
8103 type = Type::tcomplex80; | |
8104 break; | |
8105 | |
8106 default: | |
8107 assert(0); | |
8108 } | |
8109 } | |
8110 } | |
8111 return e; | |
8112 } | |
8113 | |
8114 /************************* CatExp *****************************/ | |
8115 | |
8116 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
8117 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
8118 { | |
8119 } | |
8120 | |
8121 Expression *CatExp::semantic(Scope *sc) | |
8122 { Expression *e; | |
8123 | |
8124 //printf("CatExp::semantic() %s\n", toChars()); | |
8125 if (!type) | |
8126 { | |
8127 BinExp::semanticp(sc); | |
8128 e = op_overload(sc); | |
8129 if (e) | |
8130 return e; | |
8131 | |
8132 Type *tb1 = e1->type->toBasetype(); | |
8133 Type *tb2 = e2->type->toBasetype(); | |
8134 | |
8135 | |
8136 /* BUG: Should handle things like: | |
8137 * char c; | |
8138 * c ~ ' ' | |
8139 * ' ' ~ c; | |
8140 */ | |
8141 | |
8142 #if 0 | |
8143 e1->type->print(); | |
8144 e2->type->print(); | |
8145 #endif | |
8146 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
8147 e2->type->equals(tb1->next)) | |
8148 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8149 type = tb1->nextOf()->arrayOf(); |
159 | 8150 if (tb2->ty == Tarray) |
8151 { // Make e2 into [e2] | |
8152 e2 = new ArrayLiteralExp(e2->loc, e2); | |
8153 e2->type = type; | |
8154 } | |
8155 return this; | |
8156 } | |
8157 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
8158 e1->type->equals(tb2->next)) | |
8159 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8160 type = tb2->nextOf()->arrayOf(); |
159 | 8161 if (tb1->ty == Tarray) |
8162 { // Make e1 into [e1] | |
8163 e1 = new ArrayLiteralExp(e1->loc, e1); | |
8164 e1->type = type; | |
8165 } | |
8166 return this; | |
8167 } | |
8168 | |
8169 typeCombine(sc); | |
8170 | |
8171 if (type->toBasetype()->ty == Tsarray) | |
8172 type = type->toBasetype()->next->arrayOf(); | |
8173 #if 0 | |
8174 e1->type->print(); | |
8175 e2->type->print(); | |
8176 type->print(); | |
8177 print(); | |
8178 #endif | |
8179 if (e1->op == TOKstring && e2->op == TOKstring) | |
8180 e = optimize(WANTvalue); | |
8181 else if (e1->type->equals(e2->type) && | |
8182 (e1->type->toBasetype()->ty == Tarray || | |
8183 e1->type->toBasetype()->ty == Tsarray)) | |
8184 { | |
8185 e = this; | |
8186 } | |
8187 else | |
8188 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8189 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 8190 error("Can only concatenate arrays, not (%s ~ %s)", |
8191 e1->type->toChars(), e2->type->toChars()); | |
8192 type = Type::tint32; | |
8193 e = this; | |
8194 } | |
8195 e->type = e->type->semantic(loc, sc); | |
8196 return e; | |
8197 } | |
8198 return this; | |
8199 } | |
8200 | |
8201 /************************************************************/ | |
8202 | |
8203 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
8204 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
8205 { | |
8206 } | |
8207 | |
8208 Expression *MulExp::semantic(Scope *sc) | |
8209 { Expression *e; | |
8210 | |
8211 #if 0 | |
8212 printf("MulExp::semantic() %s\n", toChars()); | |
8213 #endif | |
8214 if (type) | |
8215 { | |
8216 return this; | |
8217 } | |
8218 | |
8219 BinExp::semanticp(sc); | |
8220 e = op_overload(sc); | |
8221 if (e) | |
8222 return e; | |
8223 | |
8224 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
|
8225 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
|
8226 { 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
|
8227 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
|
8228 } |
159 | 8229 if (type->isfloating()) |
8230 { Type *t1 = e1->type; | |
8231 Type *t2 = e2->type; | |
8232 | |
8233 if (t1->isreal()) | |
8234 { | |
8235 type = t2; | |
8236 } | |
8237 else if (t2->isreal()) | |
8238 { | |
8239 type = t1; | |
8240 } | |
8241 else if (t1->isimaginary()) | |
8242 { | |
8243 if (t2->isimaginary()) | |
8244 { Expression *e; | |
8245 | |
8246 switch (t1->ty) | |
8247 { | |
8248 case Timaginary32: type = Type::tfloat32; break; | |
8249 case Timaginary64: type = Type::tfloat64; break; | |
8250 case Timaginary80: type = Type::tfloat80; break; | |
8251 default: assert(0); | |
8252 } | |
8253 | |
8254 // iy * iv = -yv | |
8255 e1->type = type; | |
8256 e2->type = type; | |
8257 e = new NegExp(loc, this); | |
8258 e = e->semantic(sc); | |
8259 return e; | |
8260 } | |
8261 else | |
8262 type = t2; // t2 is complex | |
8263 } | |
8264 else if (t2->isimaginary()) | |
8265 { | |
8266 type = t1; // t1 is complex | |
8267 } | |
8268 } | |
8269 return this; | |
8270 } | |
8271 | |
8272 /************************************************************/ | |
8273 | |
8274 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
8275 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
8276 { | |
8277 } | |
8278 | |
8279 Expression *DivExp::semantic(Scope *sc) | |
8280 { Expression *e; | |
8281 | |
8282 if (type) | |
8283 return this; | |
8284 | |
8285 BinExp::semanticp(sc); | |
8286 e = op_overload(sc); | |
8287 if (e) | |
8288 return e; | |
8289 | |
8290 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
|
8291 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
|
8292 { 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
|
8293 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
|
8294 } |
159 | 8295 if (type->isfloating()) |
8296 { Type *t1 = e1->type; | |
8297 Type *t2 = e2->type; | |
8298 | |
8299 if (t1->isreal()) | |
8300 { | |
8301 type = t2; | |
8302 if (t2->isimaginary()) | |
8303 { Expression *e; | |
8304 | |
8305 // x/iv = i(-x/v) | |
8306 e2->type = t1; | |
8307 e = new NegExp(loc, this); | |
8308 e = e->semantic(sc); | |
8309 return e; | |
8310 } | |
8311 } | |
8312 else if (t2->isreal()) | |
8313 { | |
8314 type = t1; | |
8315 } | |
8316 else if (t1->isimaginary()) | |
8317 { | |
8318 if (t2->isimaginary()) | |
8319 { | |
8320 switch (t1->ty) | |
8321 { | |
8322 case Timaginary32: type = Type::tfloat32; break; | |
8323 case Timaginary64: type = Type::tfloat64; break; | |
8324 case Timaginary80: type = Type::tfloat80; break; | |
8325 default: assert(0); | |
8326 } | |
8327 } | |
8328 else | |
8329 type = t2; // t2 is complex | |
8330 } | |
8331 else if (t2->isimaginary()) | |
8332 { | |
8333 type = t1; // t1 is complex | |
8334 } | |
8335 } | |
8336 return this; | |
8337 } | |
8338 | |
8339 /************************************************************/ | |
8340 | |
8341 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
8342 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
8343 { | |
8344 } | |
8345 | |
8346 Expression *ModExp::semantic(Scope *sc) | |
8347 { Expression *e; | |
8348 | |
8349 if (type) | |
8350 return this; | |
8351 | |
8352 BinExp::semanticp(sc); | |
8353 e = op_overload(sc); | |
8354 if (e) | |
8355 return e; | |
8356 | |
8357 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
|
8358 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
|
8359 { 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
|
8360 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
|
8361 } |
159 | 8362 if (type->isfloating()) |
8363 { type = e1->type; | |
8364 if (e2->type->iscomplex()) | |
8365 { error("cannot perform modulo complex arithmetic"); | |
8366 return new IntegerExp(0); | |
8367 } | |
8368 } | |
8369 return this; | |
8370 } | |
8371 | |
8372 /************************************************************/ | |
8373 | |
8374 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
8375 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
8376 { | |
8377 } | |
8378 | |
8379 Expression *ShlExp::semantic(Scope *sc) | |
8380 { Expression *e; | |
8381 | |
8382 //printf("ShlExp::semantic(), type = %p\n", type); | |
8383 if (!type) | |
8384 { BinExp::semanticp(sc); | |
8385 e = op_overload(sc); | |
8386 if (e) | |
8387 return e; | |
8388 e1 = e1->checkIntegral(); | |
8389 e2 = e2->checkIntegral(); | |
8390 e1 = e1->integralPromotions(sc); | |
8391 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8392 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8393 type = e1->type; |
8394 } | |
8395 return this; | |
8396 } | |
8397 | |
8398 /************************************************************/ | |
8399 | |
8400 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
8401 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
8402 { | |
8403 } | |
8404 | |
8405 Expression *ShrExp::semantic(Scope *sc) | |
8406 { Expression *e; | |
8407 | |
8408 if (!type) | |
8409 { BinExp::semanticp(sc); | |
8410 e = op_overload(sc); | |
8411 if (e) | |
8412 return e; | |
8413 e1 = e1->checkIntegral(); | |
8414 e2 = e2->checkIntegral(); | |
8415 e1 = e1->integralPromotions(sc); | |
8416 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8417 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8418 type = e1->type; |
8419 } | |
8420 return this; | |
8421 } | |
8422 | |
8423 /************************************************************/ | |
8424 | |
8425 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
8426 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
8427 { | |
8428 } | |
8429 | |
8430 Expression *UshrExp::semantic(Scope *sc) | |
8431 { Expression *e; | |
8432 | |
8433 if (!type) | |
8434 { BinExp::semanticp(sc); | |
8435 e = op_overload(sc); | |
8436 if (e) | |
8437 return e; | |
8438 e1 = e1->checkIntegral(); | |
8439 e2 = e2->checkIntegral(); | |
8440 e1 = e1->integralPromotions(sc); | |
8441 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8442 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8443 type = e1->type; |
8444 } | |
8445 return this; | |
8446 } | |
8447 | |
8448 /************************************************************/ | |
8449 | |
8450 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8451 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8452 { | |
8453 } | |
8454 | |
8455 Expression *AndExp::semantic(Scope *sc) | |
8456 { Expression *e; | |
8457 | |
8458 if (!type) | |
8459 { BinExp::semanticp(sc); | |
8460 e = op_overload(sc); | |
8461 if (e) | |
8462 return e; | |
8463 if (e1->type->toBasetype()->ty == Tbool && | |
8464 e2->type->toBasetype()->ty == Tbool) | |
8465 { | |
8466 type = e1->type; | |
8467 e = this; | |
8468 } | |
8469 else | |
8470 { | |
8471 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
|
8472 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
|
8473 { 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
|
8474 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
|
8475 } |
159 | 8476 } |
8477 } | |
8478 return this; | |
8479 } | |
8480 | |
8481 /************************************************************/ | |
8482 | |
8483 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8484 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8485 { | |
8486 } | |
8487 | |
8488 Expression *OrExp::semantic(Scope *sc) | |
8489 { Expression *e; | |
8490 | |
8491 if (!type) | |
8492 { BinExp::semanticp(sc); | |
8493 e = op_overload(sc); | |
8494 if (e) | |
8495 return e; | |
8496 if (e1->type->toBasetype()->ty == Tbool && | |
8497 e2->type->toBasetype()->ty == Tbool) | |
8498 { | |
8499 type = e1->type; | |
8500 e = this; | |
8501 } | |
8502 else | |
8503 { | |
8504 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
|
8505 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
|
8506 { 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
|
8507 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
|
8508 } |
159 | 8509 } |
8510 } | |
8511 return this; | |
8512 } | |
8513 | |
8514 /************************************************************/ | |
8515 | |
8516 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
8517 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
8518 { | |
8519 } | |
8520 | |
8521 Expression *XorExp::semantic(Scope *sc) | |
8522 { Expression *e; | |
8523 | |
8524 if (!type) | |
8525 { BinExp::semanticp(sc); | |
8526 e = op_overload(sc); | |
8527 if (e) | |
8528 return e; | |
8529 if (e1->type->toBasetype()->ty == Tbool && | |
8530 e2->type->toBasetype()->ty == Tbool) | |
8531 { | |
8532 type = e1->type; | |
8533 e = this; | |
8534 } | |
8535 else | |
8536 { | |
8537 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
|
8538 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
|
8539 { 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
|
8540 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
|
8541 } |
159 | 8542 } |
8543 } | |
8544 return this; | |
8545 } | |
8546 | |
8547 | |
8548 /************************************************************/ | |
8549 | |
8550 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
8551 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
8552 { | |
8553 } | |
8554 | |
8555 Expression *OrOrExp::semantic(Scope *sc) | |
8556 { | |
8557 unsigned cs1; | |
8558 | |
8559 // same as for AndAnd | |
8560 e1 = e1->semantic(sc); | |
8561 e1 = resolveProperties(sc, e1); | |
8562 e1 = e1->checkToPointer(); | |
8563 e1 = e1->checkToBoolean(); | |
8564 cs1 = sc->callSuper; | |
8565 | |
8566 if (sc->flags & SCOPEstaticif) | |
8567 { | |
8568 /* If in static if, don't evaluate e2 if we don't have to. | |
8569 */ | |
8570 e1 = e1->optimize(WANTflags); | |
8571 if (e1->isBool(TRUE)) | |
8572 { | |
8573 return new IntegerExp(loc, 1, Type::tboolean); | |
8574 } | |
8575 } | |
8576 | |
8577 e2 = e2->semantic(sc); | |
8578 sc->mergeCallSuper(loc, cs1); | |
8579 e2 = resolveProperties(sc, e2); | |
8580 e2 = e2->checkToPointer(); | |
8581 | |
8582 type = Type::tboolean; | |
8583 if (e1->type->ty == Tvoid) | |
8584 type = Type::tvoid; | |
8585 if (e2->op == TOKtype || e2->op == TOKimport) | |
8586 error("%s is not an expression", e2->toChars()); | |
8587 return this; | |
8588 } | |
8589 | |
8590 Expression *OrOrExp::checkToBoolean() | |
8591 { | |
8592 e2 = e2->checkToBoolean(); | |
8593 return this; | |
8594 } | |
8595 | |
8596 int OrOrExp::isBit() | |
8597 { | |
8598 return TRUE; | |
8599 } | |
8600 | |
8601 int OrOrExp::checkSideEffect(int flag) | |
8602 { | |
8603 if (flag == 2) | |
8604 { | |
8605 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
8606 } | |
8607 else | |
8608 { e1->checkSideEffect(1); | |
8609 return e2->checkSideEffect(flag); | |
8610 } | |
8611 } | |
8612 | |
8613 /************************************************************/ | |
8614 | |
8615 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
8616 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
8617 { | |
8618 } | |
8619 | |
8620 Expression *AndAndExp::semantic(Scope *sc) | |
8621 { | |
8622 unsigned cs1; | |
8623 | |
8624 // same as for OrOr | |
8625 e1 = e1->semantic(sc); | |
8626 e1 = resolveProperties(sc, e1); | |
8627 e1 = e1->checkToPointer(); | |
8628 e1 = e1->checkToBoolean(); | |
8629 cs1 = sc->callSuper; | |
8630 | |
8631 if (sc->flags & SCOPEstaticif) | |
8632 { | |
8633 /* If in static if, don't evaluate e2 if we don't have to. | |
8634 */ | |
8635 e1 = e1->optimize(WANTflags); | |
8636 if (e1->isBool(FALSE)) | |
8637 { | |
8638 return new IntegerExp(loc, 0, Type::tboolean); | |
8639 } | |
8640 } | |
8641 | |
8642 e2 = e2->semantic(sc); | |
8643 sc->mergeCallSuper(loc, cs1); | |
8644 e2 = resolveProperties(sc, e2); | |
8645 e2 = e2->checkToPointer(); | |
8646 | |
8647 type = Type::tboolean; | |
8648 if (e1->type->ty == Tvoid) | |
8649 type = Type::tvoid; | |
8650 if (e2->op == TOKtype || e2->op == TOKimport) | |
8651 error("%s is not an expression", e2->toChars()); | |
8652 return this; | |
8653 } | |
8654 | |
8655 Expression *AndAndExp::checkToBoolean() | |
8656 { | |
8657 e2 = e2->checkToBoolean(); | |
8658 return this; | |
8659 } | |
8660 | |
8661 int AndAndExp::isBit() | |
8662 { | |
8663 return TRUE; | |
8664 } | |
8665 | |
8666 int AndAndExp::checkSideEffect(int flag) | |
8667 { | |
8668 if (flag == 2) | |
8669 { | |
8670 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
8671 } | |
8672 else | |
8673 { | |
8674 e1->checkSideEffect(1); | |
8675 return e2->checkSideEffect(flag); | |
8676 } | |
8677 } | |
8678 | |
8679 /************************************************************/ | |
8680 | |
8681 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
8682 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
8683 { | |
8684 } | |
8685 | |
8686 Expression *InExp::semantic(Scope *sc) | |
8687 { Expression *e; | |
8688 | |
8689 if (type) | |
8690 return this; | |
8691 | |
8692 BinExp::semanticp(sc); | |
8693 e = op_overload(sc); | |
8694 if (e) | |
8695 return e; | |
8696 | |
8697 //type = Type::tboolean; | |
8698 Type *t2b = e2->type->toBasetype(); | |
8699 if (t2b->ty != Taarray) | |
8700 { | |
8701 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
8702 type = Type::terror; | |
8703 } | |
8704 else | |
8705 { | |
8706 TypeAArray *ta = (TypeAArray *)t2b; | |
8707 | |
8708 // Convert key to type of key | |
8709 e1 = e1->implicitCastTo(sc, ta->index); | |
8710 | |
8711 // 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
|
8712 type = ta->nextOf()->pointerTo(); |
159 | 8713 } |
8714 return this; | |
8715 } | |
8716 | |
8717 int InExp::isBit() | |
8718 { | |
8719 return FALSE; | |
8720 } | |
8721 | |
8722 | |
8723 /************************************************************/ | |
8724 | |
8725 /* This deletes the key e1 from the associative array e2 | |
8726 */ | |
8727 | |
8728 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
8729 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
8730 { | |
8731 type = Type::tvoid; | |
8732 } | |
8733 | |
8734 /************************************************************/ | |
8735 | |
8736 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8737 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
8738 { | |
8739 } | |
8740 | |
8741 Expression *CmpExp::semantic(Scope *sc) | |
8742 { Expression *e; | |
8743 Type *t1; | |
8744 Type *t2; | |
8745 | |
8746 #if LOGSEMANTIC | |
8747 printf("CmpExp::semantic('%s')\n", toChars()); | |
8748 #endif | |
8749 if (type) | |
8750 return this; | |
8751 | |
8752 BinExp::semanticp(sc); | |
8753 | |
8754 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
8755 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
8756 { | |
8757 error("do not use null when comparing class types"); | |
8758 } | |
8759 | |
8760 e = op_overload(sc); | |
8761 if (e) | |
8762 { | |
8763 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
8764 e = e->semantic(sc); | |
8765 return e; | |
8766 } | |
8767 | |
8768 typeCombine(sc); | |
8769 type = Type::tboolean; | |
8770 | |
8771 // Special handling for array comparisons | |
8772 t1 = e1->type->toBasetype(); | |
8773 t2 = e2->type->toBasetype(); | |
8774 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
8775 (t2->ty == Tarray || t2->ty == Tsarray)) | |
8776 { | |
8777 if (!t1->next->equals(t2->next)) | |
8778 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
8779 e = this; | |
8780 } | |
8781 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
8782 (t1->ty == Tclass && t2->ty == Tclass)) | |
8783 { | |
8784 if (t2->ty == Tstruct) | |
8785 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
8786 else | |
8787 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
8788 e = this; | |
8789 } | |
8790 #if 1 | |
8791 else if (t1->iscomplex() || t2->iscomplex()) | |
8792 { | |
8793 error("compare not defined for complex operands"); | |
8794 e = new IntegerExp(0); | |
8795 } | |
8796 #endif | |
8797 else | |
8798 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
|
8799 //printf("CmpExp: %s\n", e->toChars()); |
159 | 8800 return e; |
8801 } | |
8802 | |
8803 int CmpExp::isBit() | |
8804 { | |
8805 return TRUE; | |
8806 } | |
8807 | |
8808 | |
8809 /************************************************************/ | |
8810 | |
8811 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8812 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
8813 { | |
8814 assert(op == TOKequal || op == TOKnotequal); | |
8815 } | |
8816 | |
8817 Expression *EqualExp::semantic(Scope *sc) | |
8818 { Expression *e; | |
8819 Type *t1; | |
8820 Type *t2; | |
8821 | |
8822 //printf("EqualExp::semantic('%s')\n", toChars()); | |
8823 if (type) | |
8824 return this; | |
8825 | |
8826 BinExp::semanticp(sc); | |
8827 | |
8828 /* Before checking for operator overloading, check to see if we're | |
8829 * comparing the addresses of two statics. If so, we can just see | |
8830 * if they are the same symbol. | |
8831 */ | |
8832 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
8833 { AddrExp *ae1 = (AddrExp *)e1; | |
8834 AddrExp *ae2 = (AddrExp *)e2; | |
8835 | |
8836 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
8837 { VarExp *ve1 = (VarExp *)ae1->e1; | |
8838 VarExp *ve2 = (VarExp *)ae2->e1; | |
8839 | |
8840 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
8841 { | |
8842 // They are the same, result is 'true' for ==, 'false' for != | |
8843 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
8844 return e; | |
8845 } | |
8846 } | |
8847 } | |
8848 | |
8849 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
8850 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
8851 { | |
8852 error("use '%s' instead of '%s' when comparing with null", | |
8853 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
8854 Token::toChars(op)); | |
8855 } | |
8856 | |
8857 //if (e2->op != TOKnull) | |
8858 { | |
8859 e = op_overload(sc); | |
8860 if (e) | |
8861 { | |
8862 if (op == TOKnotequal) | |
8863 { | |
8864 e = new NotExp(e->loc, e); | |
8865 e = e->semantic(sc); | |
8866 } | |
8867 return e; | |
8868 } | |
8869 } | |
8870 | |
8871 e = typeCombine(sc); | |
8872 type = Type::tboolean; | |
8873 | |
8874 // Special handling for array comparisons | |
8875 t1 = e1->type->toBasetype(); | |
8876 t2 = e2->type->toBasetype(); | |
8877 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
8878 (t2->ty == Tarray || t2->ty == Tsarray)) | |
8879 { | |
8880 if (!t1->next->equals(t2->next)) | |
8881 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
8882 } | |
8883 else | |
8884 { | |
8885 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
8886 { | |
8887 // Cast both to complex | |
8888 e1 = e1->castTo(sc, Type::tcomplex80); | |
8889 e2 = e2->castTo(sc, Type::tcomplex80); | |
8890 } | |
8891 } | |
8892 return e; | |
8893 } | |
8894 | |
8895 int EqualExp::isBit() | |
8896 { | |
8897 return TRUE; | |
8898 } | |
8899 | |
8900 | |
8901 | |
8902 /************************************************************/ | |
8903 | |
8904 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
8905 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
8906 { | |
8907 } | |
8908 | |
8909 Expression *IdentityExp::semantic(Scope *sc) | |
8910 { | |
8911 if (type) | |
8912 return this; | |
8913 | |
8914 BinExp::semanticp(sc); | |
8915 type = Type::tboolean; | |
8916 typeCombine(sc); | |
8917 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
8918 { | |
8919 // Cast both to complex | |
8920 e1 = e1->castTo(sc, Type::tcomplex80); | |
8921 e2 = e2->castTo(sc, Type::tcomplex80); | |
8922 } | |
8923 return this; | |
8924 } | |
8925 | |
8926 int IdentityExp::isBit() | |
8927 { | |
8928 return TRUE; | |
8929 } | |
8930 | |
8931 | |
8932 /****************************************************************/ | |
8933 | |
8934 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
8935 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
8936 { | |
8937 this->econd = econd; | |
8938 } | |
8939 | |
8940 Expression *CondExp::syntaxCopy() | |
8941 { | |
8942 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
8943 } | |
8944 | |
8945 | |
8946 Expression *CondExp::semantic(Scope *sc) | |
8947 { Type *t1; | |
8948 Type *t2; | |
8949 unsigned cs0; | |
8950 unsigned cs1; | |
8951 | |
8952 #if LOGSEMANTIC | |
8953 printf("CondExp::semantic('%s')\n", toChars()); | |
8954 #endif | |
8955 if (type) | |
8956 return this; | |
8957 | |
8958 econd = econd->semantic(sc); | |
8959 econd = resolveProperties(sc, econd); | |
8960 econd = econd->checkToPointer(); | |
8961 econd = econd->checkToBoolean(); | |
8962 | |
8963 #if 0 /* this cannot work right because the types of e1 and e2 | |
8964 * both contribute to the type of the result. | |
8965 */ | |
8966 if (sc->flags & SCOPEstaticif) | |
8967 { | |
8968 /* If in static if, don't evaluate what we don't have to. | |
8969 */ | |
8970 econd = econd->optimize(WANTflags); | |
8971 if (econd->isBool(TRUE)) | |
8972 { | |
8973 e1 = e1->semantic(sc); | |
8974 e1 = resolveProperties(sc, e1); | |
8975 return e1; | |
8976 } | |
8977 else if (econd->isBool(FALSE)) | |
8978 { | |
8979 e2 = e2->semantic(sc); | |
8980 e2 = resolveProperties(sc, e2); | |
8981 return e2; | |
8982 } | |
8983 } | |
8984 #endif | |
8985 | |
8986 | |
8987 cs0 = sc->callSuper; | |
8988 e1 = e1->semantic(sc); | |
8989 e1 = resolveProperties(sc, e1); | |
8990 cs1 = sc->callSuper; | |
8991 sc->callSuper = cs0; | |
8992 e2 = e2->semantic(sc); | |
8993 e2 = resolveProperties(sc, e2); | |
8994 sc->mergeCallSuper(loc, cs1); | |
8995 | |
8996 | |
8997 // If either operand is void, the result is void | |
8998 t1 = e1->type; | |
8999 t2 = e2->type; | |
9000 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
9001 type = Type::tvoid; | |
9002 else if (t1 == t2) | |
9003 type = t1; | |
9004 else | |
9005 { | |
9006 typeCombine(sc); | |
9007 switch (e1->type->toBasetype()->ty) | |
9008 { | |
9009 case Tcomplex32: | |
9010 case Tcomplex64: | |
9011 case Tcomplex80: | |
9012 e2 = e2->castTo(sc, e1->type); | |
9013 break; | |
9014 } | |
9015 switch (e2->type->toBasetype()->ty) | |
9016 { | |
9017 case Tcomplex32: | |
9018 case Tcomplex64: | |
9019 case Tcomplex80: | |
9020 e1 = e1->castTo(sc, e2->type); | |
9021 break; | |
9022 } | |
9023 } | |
9024 return this; | |
9025 } | |
9026 | |
9027 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) | |
9028 { | |
9029 PtrExp *e; | |
9030 | |
9031 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
9032 e = new PtrExp(loc, this, type); | |
9033 | |
9034 e1 = e1->addressOf(sc); | |
9035 //e1 = e1->toLvalue(sc, NULL); | |
9036 | |
9037 e2 = e2->addressOf(sc); | |
9038 //e2 = e2->toLvalue(sc, NULL); | |
9039 | |
9040 typeCombine(sc); | |
9041 | |
9042 type = e2->type; | |
9043 return e; | |
9044 } | |
9045 | |
9046 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
9047 { | |
9048 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
9049 return this; | |
9050 } | |
9051 | |
9052 void CondExp::checkEscape() | |
9053 { | |
9054 e1->checkEscape(); | |
9055 e2->checkEscape(); | |
9056 } | |
9057 | |
9058 | |
9059 Expression *CondExp::checkToBoolean() | |
9060 { | |
9061 e1 = e1->checkToBoolean(); | |
9062 e2 = e2->checkToBoolean(); | |
9063 return this; | |
9064 } | |
9065 | |
9066 int CondExp::checkSideEffect(int flag) | |
9067 { | |
9068 if (flag == 2) | |
9069 { | |
9070 return econd->checkSideEffect(2) || | |
9071 e1->checkSideEffect(2) || | |
9072 e2->checkSideEffect(2); | |
9073 } | |
9074 else | |
9075 { | |
9076 econd->checkSideEffect(1); | |
9077 e1->checkSideEffect(flag); | |
9078 return e2->checkSideEffect(flag); | |
9079 } | |
9080 } | |
9081 | |
9082 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
9083 { | |
9084 expToCBuffer(buf, hgs, econd, PREC_oror); | |
9085 buf->writestring(" ? "); | |
9086 expToCBuffer(buf, hgs, e1, PREC_expr); | |
9087 buf->writestring(" : "); | |
9088 expToCBuffer(buf, hgs, e2, PREC_cond); | |
9089 } | |
9090 | |
599
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9091 /************************************************************/ |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9092 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9093 #if IN_LLVM |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9094 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9095 // Strictly LDC specific stuff |
599
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9096 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9097 GEPExp::GEPExp(Loc loc, Expression* e, Identifier* id, unsigned idx) |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9098 : UnaExp(loc, TOKgep, sizeof(GEPExp), e) |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9099 { |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9100 index = idx; |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9101 ident = id; |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9102 } |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9103 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9104 void GEPExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9105 { |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9106 expToCBuffer(buf, hgs, e1, PREC_primary); |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9107 buf->writeByte('.'); |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9108 buf->writestring(ident->toChars()); |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9109 } |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9110 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9111 Expression* GEPExp::toLvalue(Scope* sc, Expression* e) |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9112 { |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9113 // GEP's are always lvalues, at least in the "LLVM sense" ... |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9114 return this; |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9115 } |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9116 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9117 #endif |