Mercurial > projects > ldc
annotate dmd/expression.c @ 1188:442ab244c455
Apply small includes patch for FreeBSD. Thanks kimelto!
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Thu, 02 Apr 2009 19:16:44 +0200 |
parents | ab186e535e72 |
children | e961851fb8be |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 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> | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
14 #include <math.h> |
159 | 15 #include <assert.h> |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
16 #if _MSC_VER |
159 | 17 #include <complex> |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
18 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
19 #endif |
159 | 20 |
21 #if _WIN32 && __DMC__ | |
22 extern "C" char * __cdecl __locale_decpoint; | |
23 #endif | |
24 | |
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
|
25 #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
|
26 #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
|
27 #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
|
28 #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
|
29 #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
|
30 |
536
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
31 #ifdef __APPLE__ |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
32 #ifndef isnan |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
33 int isnan(double); |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
34 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
35 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
36 |
159 | 37 #if IN_GCC |
38 // Issues with using -include total.h (defines integer_t) and then complex.h fails... | |
39 #undef integer_t | |
40 #endif | |
41 | |
42 #ifdef __APPLE__ | |
43 #define integer_t dmd_integer_t | |
44 #endif | |
45 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
46 #include "rmem.h" |
159 | 47 |
48 //#include "port.h" | |
49 #include "mtype.h" | |
50 #include "init.h" | |
51 #include "expression.h" | |
52 #include "template.h" | |
53 #include "utf.h" | |
54 #include "enum.h" | |
55 #include "scope.h" | |
56 #include "statement.h" | |
57 #include "declaration.h" | |
58 #include "aggregate.h" | |
59 #include "import.h" | |
60 #include "id.h" | |
61 #include "dsymbol.h" | |
62 #include "module.h" | |
63 #include "attrib.h" | |
64 #include "hdrgen.h" | |
65 #include "parse.h" | |
66 | |
67 Expression *createTypeInfoArray(Scope *sc, Expression *args[], int dim); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
68 Expression *expandVar(int result, VarDeclaration *v); |
159 | 69 |
70 #define LOGSEMANTIC 0 | |
71 | |
72 /********************************** | |
73 * Set operator precedence for each operator. | |
74 */ | |
75 | |
76 // Operator precedence - greater values are higher precedence | |
77 | |
78 enum PREC | |
79 { | |
80 PREC_zero, | |
81 PREC_expr, | |
82 PREC_assign, | |
83 PREC_cond, | |
84 PREC_oror, | |
85 PREC_andand, | |
86 PREC_or, | |
87 PREC_xor, | |
88 PREC_and, | |
89 PREC_equal, | |
90 PREC_rel, | |
91 PREC_shift, | |
92 PREC_add, | |
93 PREC_mul, | |
94 PREC_unary, | |
95 PREC_primary, | |
96 }; | |
97 | |
98 enum PREC precedence[TOKMAX]; | |
99 | |
100 void initPrecedence() | |
101 { | |
102 precedence[TOKdotvar] = PREC_primary; | |
103 precedence[TOKimport] = PREC_primary; | |
104 precedence[TOKidentifier] = PREC_primary; | |
105 precedence[TOKthis] = PREC_primary; | |
106 precedence[TOKsuper] = PREC_primary; | |
107 precedence[TOKint64] = PREC_primary; | |
108 precedence[TOKfloat64] = PREC_primary; | |
109 precedence[TOKnull] = PREC_primary; | |
110 precedence[TOKstring] = PREC_primary; | |
111 precedence[TOKarrayliteral] = PREC_primary; | |
112 precedence[TOKtypedot] = PREC_primary; | |
113 precedence[TOKtypeid] = PREC_primary; | |
114 precedence[TOKis] = PREC_primary; | |
115 precedence[TOKassert] = PREC_primary; | |
116 precedence[TOKfunction] = PREC_primary; | |
117 precedence[TOKvar] = PREC_primary; | |
336 | 118 #if DMDV2 |
119 precedence[TOKdefault] = PREC_primary; | |
120 #endif | |
159 | 121 |
122 // post | |
123 precedence[TOKdotti] = PREC_primary; | |
124 precedence[TOKdot] = PREC_primary; | |
125 // precedence[TOKarrow] = PREC_primary; | |
126 precedence[TOKplusplus] = PREC_primary; | |
127 precedence[TOKminusminus] = PREC_primary; | |
128 precedence[TOKcall] = PREC_primary; | |
129 precedence[TOKslice] = PREC_primary; | |
130 precedence[TOKarray] = PREC_primary; | |
131 | |
132 precedence[TOKaddress] = PREC_unary; | |
133 precedence[TOKstar] = PREC_unary; | |
134 precedence[TOKneg] = PREC_unary; | |
135 precedence[TOKuadd] = PREC_unary; | |
136 precedence[TOKnot] = PREC_unary; | |
137 precedence[TOKtobool] = PREC_add; | |
138 precedence[TOKtilde] = PREC_unary; | |
139 precedence[TOKdelete] = PREC_unary; | |
140 precedence[TOKnew] = PREC_unary; | |
141 precedence[TOKcast] = PREC_unary; | |
142 | |
143 precedence[TOKmul] = PREC_mul; | |
144 precedence[TOKdiv] = PREC_mul; | |
145 precedence[TOKmod] = PREC_mul; | |
146 | |
147 precedence[TOKadd] = PREC_add; | |
148 precedence[TOKmin] = PREC_add; | |
149 precedence[TOKcat] = PREC_add; | |
150 | |
151 precedence[TOKshl] = PREC_shift; | |
152 precedence[TOKshr] = PREC_shift; | |
153 precedence[TOKushr] = PREC_shift; | |
154 | |
155 precedence[TOKlt] = PREC_rel; | |
156 precedence[TOKle] = PREC_rel; | |
157 precedence[TOKgt] = PREC_rel; | |
158 precedence[TOKge] = PREC_rel; | |
159 precedence[TOKunord] = PREC_rel; | |
160 precedence[TOKlg] = PREC_rel; | |
161 precedence[TOKleg] = PREC_rel; | |
162 precedence[TOKule] = PREC_rel; | |
163 precedence[TOKul] = PREC_rel; | |
164 precedence[TOKuge] = PREC_rel; | |
165 precedence[TOKug] = PREC_rel; | |
166 precedence[TOKue] = PREC_rel; | |
167 precedence[TOKin] = PREC_rel; | |
168 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
169 #if 0 |
159 | 170 precedence[TOKequal] = PREC_equal; |
171 precedence[TOKnotequal] = PREC_equal; | |
172 precedence[TOKidentity] = PREC_equal; | |
173 precedence[TOKnotidentity] = PREC_equal; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
174 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
175 /* Note that we changed precedence, so that < and != have the same |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
176 * precedence. This change is in the parser, too. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
177 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
178 precedence[TOKequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
179 precedence[TOKnotequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
180 precedence[TOKidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
181 precedence[TOKnotidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
182 #endif |
159 | 183 |
184 precedence[TOKand] = PREC_and; | |
185 | |
186 precedence[TOKxor] = PREC_xor; | |
187 | |
188 precedence[TOKor] = PREC_or; | |
189 | |
190 precedence[TOKandand] = PREC_andand; | |
191 | |
192 precedence[TOKoror] = PREC_oror; | |
193 | |
194 precedence[TOKquestion] = PREC_cond; | |
195 | |
196 precedence[TOKassign] = PREC_assign; | |
197 precedence[TOKconstruct] = PREC_assign; | |
198 precedence[TOKblit] = PREC_assign; | |
199 precedence[TOKaddass] = PREC_assign; | |
200 precedence[TOKminass] = PREC_assign; | |
201 precedence[TOKcatass] = PREC_assign; | |
202 precedence[TOKmulass] = PREC_assign; | |
203 precedence[TOKdivass] = PREC_assign; | |
204 precedence[TOKmodass] = PREC_assign; | |
205 precedence[TOKshlass] = PREC_assign; | |
206 precedence[TOKshrass] = PREC_assign; | |
207 precedence[TOKushrass] = PREC_assign; | |
208 precedence[TOKandass] = PREC_assign; | |
209 precedence[TOKorass] = PREC_assign; | |
210 precedence[TOKxorass] = PREC_assign; | |
211 | |
212 precedence[TOKcomma] = PREC_expr; | |
213 } | |
214 | |
336 | 215 /************************************************************* |
216 * Given var, we need to get the | |
217 * right 'this' pointer if var is in an outer class, but our | |
218 * existing 'this' pointer is in an inner class. | |
219 * Input: | |
220 * e1 existing 'this' | |
221 * ad struct or class we need the correct 'this' for | |
222 * var the specific member of ad we're accessing | |
223 */ | |
224 | |
225 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, | |
226 Expression *e1, Declaration *var) | |
227 { | |
228 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars()); | |
229 L1: | |
230 Type *t = e1->type->toBasetype(); | |
231 //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars()); | |
232 | |
233 /* If e1 is not the 'this' pointer for ad | |
234 */ | |
235 if (ad && | |
236 !(t->ty == Tpointer && t->nextOf()->ty == Tstruct && | |
237 ((TypeStruct *)t->nextOf())->sym == ad) | |
238 && | |
239 !(t->ty == Tstruct && | |
240 ((TypeStruct *)t)->sym == ad) | |
241 ) | |
242 { | |
243 ClassDeclaration *cd = ad->isClassDeclaration(); | |
244 ClassDeclaration *tcd = t->isClassHandle(); | |
245 | |
246 /* e1 is the right this if ad is a base class of e1 | |
247 */ | |
248 if (!cd || !tcd || | |
249 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
250 ) | |
251 { | |
252 /* Only classes can be inner classes with an 'outer' | |
253 * member pointing to the enclosing class instance | |
254 */ | |
255 if (tcd && tcd->isNested()) | |
256 { /* e1 is the 'this' pointer for an inner class: tcd. | |
257 * Rewrite it as the 'this' pointer for the outer class. | |
258 */ | |
259 | |
260 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
261 e1->type = tcd->vthis->type; | |
262 // Do not call checkNestedRef() | |
263 //e1 = e1->semantic(sc); | |
264 | |
265 // Skip up over nested functions, and get the enclosing | |
266 // class type. | |
267 int n = 0; | |
268 Dsymbol *s; | |
269 for (s = tcd->toParent(); | |
270 s && s->isFuncDeclaration(); | |
271 s = s->toParent()) | |
272 { FuncDeclaration *f = s->isFuncDeclaration(); | |
273 if (f->vthis) | |
274 { | |
275 //printf("rewriting e1 to %s's this\n", f->toChars()); | |
276 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
|
277 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
278 // 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
|
279 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
|
280 |
336 | 281 e1 = new VarExp(loc, f->vthis); |
282 } | |
283 } | |
284 if (s && s->isClassDeclaration()) | |
285 { e1->type = s->isClassDeclaration()->type; | |
286 if (n > 1) | |
287 e1 = e1->semantic(sc); | |
288 } | |
289 else | |
290 e1 = e1->semantic(sc); | |
291 goto L1; | |
292 } | |
293 /* Can't find a path from e1 to ad | |
294 */ | |
295 e1->error("this for %s needs to be type %s not type %s", | |
296 var->toChars(), ad->toChars(), t->toChars()); | |
297 } | |
298 } | |
299 return e1; | |
300 } | |
301 | |
159 | 302 /***************************************** |
303 * Determine if 'this' is available. | |
304 * If it is, return the FuncDeclaration that has it. | |
305 */ | |
306 | |
307 FuncDeclaration *hasThis(Scope *sc) | |
308 { FuncDeclaration *fd; | |
309 FuncDeclaration *fdthis; | |
310 | |
311 //printf("hasThis()\n"); | |
312 fdthis = sc->parent->isFuncDeclaration(); | |
313 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
314 | |
315 // Go upwards until we find the enclosing member function | |
316 fd = fdthis; | |
317 while (1) | |
318 { | |
319 if (!fd) | |
320 { | |
321 goto Lno; | |
322 } | |
323 if (!fd->isNested()) | |
324 break; | |
325 | |
326 Dsymbol *parent = fd->parent; | |
327 while (parent) | |
328 { | |
329 TemplateInstance *ti = parent->isTemplateInstance(); | |
330 if (ti) | |
331 parent = ti->parent; | |
332 else | |
333 break; | |
334 } | |
335 | |
336 fd = fd->parent->isFuncDeclaration(); | |
337 } | |
338 | |
339 if (!fd->isThis()) | |
340 { //printf("test '%s'\n", fd->toChars()); | |
341 goto Lno; | |
342 } | |
343 | |
344 assert(fd->vthis); | |
345 return fd; | |
346 | |
347 Lno: | |
348 return NULL; // don't have 'this' available | |
349 } | |
350 | |
351 | |
352 /*************************************** | |
353 * Pull out any properties. | |
354 */ | |
355 | |
356 Expression *resolveProperties(Scope *sc, Expression *e) | |
357 { | |
358 //printf("resolveProperties(%s)\n", e->toChars()); | |
359 if (e->type) | |
360 { | |
361 Type *t = e->type->toBasetype(); | |
362 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
363 if (t->ty == Tfunction /*|| e->op == TOKoverloadset*/) |
159 | 364 { |
365 e = new CallExp(e->loc, e); | |
366 e = e->semantic(sc); | |
367 } | |
368 | |
369 /* Look for e being a lazy parameter; rewrite as delegate call | |
370 */ | |
371 else if (e->op == TOKvar) | |
372 { VarExp *ve = (VarExp *)e; | |
373 | |
374 if (ve->var->storage_class & STClazy) | |
375 { | |
376 e = new CallExp(e->loc, e); | |
377 e = e->semantic(sc); | |
378 } | |
379 } | |
380 | |
381 else if (e->op == TOKdotexp) | |
382 { | |
383 e->error("expression has no value"); | |
384 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
385 |
159 | 386 } |
387 return e; | |
388 } | |
389 | |
390 /****************************** | |
391 * Perform semantic() on an array of Expressions. | |
392 */ | |
393 | |
394 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
395 { | |
396 if (exps) | |
397 { | |
398 for (size_t i = 0; i < exps->dim; i++) | |
399 { Expression *e = (Expression *)exps->data[i]; | |
400 | |
401 e = e->semantic(sc); | |
402 exps->data[i] = (void *)e; | |
403 } | |
404 } | |
405 } | |
406 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
407 |
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 * 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
|
410 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
411 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
412 #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
|
413 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
|
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 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
|
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 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
|
418 { 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
|
419 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
|
420 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
|
421 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
422 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
423 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
|
424 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
425 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
426 |
159 | 427 /**************************************** |
428 * Expand tuples. | |
429 */ | |
430 | |
431 void expandTuples(Expressions *exps) | |
432 { | |
433 //printf("expandTuples()\n"); | |
434 if (exps) | |
435 { | |
436 for (size_t i = 0; i < exps->dim; i++) | |
437 { Expression *arg = (Expression *)exps->data[i]; | |
438 if (!arg) | |
439 continue; | |
440 | |
441 // Look for tuple with 0 members | |
442 if (arg->op == TOKtype) | |
443 { TypeExp *e = (TypeExp *)arg; | |
444 if (e->type->toBasetype()->ty == Ttuple) | |
445 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
446 | |
447 if (!tt->arguments || tt->arguments->dim == 0) | |
448 { | |
449 exps->remove(i); | |
450 if (i == exps->dim) | |
451 return; | |
452 i--; | |
453 continue; | |
454 } | |
455 } | |
456 } | |
457 | |
458 // Inline expand all the tuples | |
459 while (arg->op == TOKtuple) | |
460 { TupleExp *te = (TupleExp *)arg; | |
461 | |
462 exps->remove(i); // remove arg | |
463 exps->insert(i, te->exps); // replace with tuple contents | |
464 if (i == exps->dim) | |
465 return; // empty tuple, no more arguments | |
466 arg = (Expression *)exps->data[i]; | |
467 } | |
468 } | |
469 } | |
470 } | |
471 | |
472 /**************************************** | |
473 * Preprocess arguments to function. | |
474 */ | |
475 | |
476 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps) | |
477 { | |
478 if (exps) | |
479 { | |
480 expandTuples(exps); | |
481 | |
482 for (size_t i = 0; i < exps->dim; i++) | |
483 { Expression *arg = (Expression *)exps->data[i]; | |
484 | |
485 if (!arg->type) | |
486 { | |
487 #ifdef DEBUG | |
488 if (!global.gag) | |
489 printf("1: \n"); | |
490 #endif | |
491 arg->error("%s is not an expression", arg->toChars()); | |
492 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
493 } | |
494 | |
495 arg = resolveProperties(sc, arg); | |
496 exps->data[i] = (void *) arg; | |
497 | |
498 //arg->rvalue(); | |
499 #if 0 | |
500 if (arg->type->ty == Tfunction) | |
501 { | |
502 arg = new AddrExp(arg->loc, arg); | |
503 arg = arg->semantic(sc); | |
504 exps->data[i] = (void *) arg; | |
505 } | |
506 #endif | |
507 } | |
508 } | |
509 } | |
510 | |
336 | 511 /********************************************* |
512 * Call copy constructor for struct value argument. | |
513 */ | |
514 #if DMDV2 | |
515 Expression *callCpCtor(Loc loc, Scope *sc, Expression *e) | |
516 { | |
517 Type *tb = e->type->toBasetype(); | |
518 assert(tb->ty == Tstruct); | |
519 StructDeclaration *sd = ((TypeStruct *)tb)->sym; | |
520 if (sd->cpctor) | |
521 { | |
522 /* Create a variable tmp, and replace the argument e with: | |
523 * (tmp = e),tmp | |
524 * and let AssignExp() handle the construction. | |
525 * This is not the most efficent, ideally tmp would be constructed | |
526 * directly onto the stack. | |
527 */ | |
528 Identifier *idtmp = Lexer::uniqueId("__tmp"); | |
529 VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e)); | |
530 Expression *ae = new DeclarationExp(loc, tmp); | |
531 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
532 e = e->semantic(sc); | |
533 } | |
534 return e; | |
535 } | |
536 #endif | |
159 | 537 |
538 /**************************************** | |
539 * Now that we know the exact type of the function we're calling, | |
540 * 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
|
541 * 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
|
542 * 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
|
543 * 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
|
544 * 4. add hidden _arguments[] argument |
159 | 545 */ |
546 | |
547 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) | |
548 { | |
549 unsigned n; | |
550 | |
551 //printf("functionArguments()\n"); | |
552 assert(arguments); | |
553 size_t nargs = arguments ? arguments->dim : 0; | |
554 size_t nparams = Argument::dim(tf->parameters); | |
555 | |
556 if (nargs > nparams && tf->varargs == 0) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
557 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 558 |
559 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
560 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
561 int done = 0; |
159 | 562 for (size_t i = 0; i < n; i++) |
563 { | |
564 Expression *arg; | |
565 | |
566 if (i < nargs) | |
567 arg = (Expression *)arguments->data[i]; | |
568 else | |
569 arg = NULL; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
570 Type *tb; |
159 | 571 |
572 if (i < nparams) | |
573 { | |
574 Argument *p = Argument::getNth(tf->parameters, i); | |
575 | |
576 if (!arg) | |
577 { | |
578 if (!p->defaultArg) | |
579 { | |
580 if (tf->varargs == 2 && i + 1 == nparams) | |
581 goto L2; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
582 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 583 break; |
584 } | |
336 | 585 arg = p->defaultArg; |
586 #if DMDV2 | |
587 if (arg->op == TOKdefault) | |
588 { DefaultInitExp *de = (DefaultInitExp *)arg; | |
589 arg = de->resolve(loc, sc); | |
590 } | |
591 else | |
592 #endif | |
593 arg = arg->copy(); | |
159 | 594 arguments->push(arg); |
595 nargs++; | |
596 } | |
597 | |
598 if (tf->varargs == 2 && i + 1 == nparams) | |
599 { | |
600 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
601 if (arg->implicitConvTo(p->type)) | |
602 { | |
603 if (nargs != nparams) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
604 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 605 goto L1; |
606 } | |
607 L2: | |
608 Type *tb = p->type->toBasetype(); | |
609 Type *tret = p->isLazyArray(); | |
610 switch (tb->ty) | |
611 { | |
612 case Tsarray: | |
613 case Tarray: | |
614 { // Create a static array variable v of type arg->type | |
615 #ifdef IN_GCC | |
616 /* GCC 4.0 does not like zero length arrays used like | |
617 this; pass a null array value instead. Could also | |
618 just make a one-element array. */ | |
619 if (nargs - i == 0) | |
620 { | |
621 arg = new NullExp(loc); | |
622 break; | |
623 } | |
624 #endif | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
625 Identifier *id = Lexer::uniqueId("__arrayArg"); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
626 Type *t = new TypeSArray(((TypeArray *)tb)->next, new IntegerExp(nargs - i)); |
159 | 627 t = t->semantic(loc, sc); |
628 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
629 v->semantic(sc); | |
630 v->parent = sc->parent; | |
631 //sc->insert(v); | |
632 | |
633 Expression *c = new DeclarationExp(0, v); | |
634 c->type = v->type; | |
635 | |
636 for (size_t u = i; u < nargs; u++) | |
637 { Expression *a = (Expression *)arguments->data[u]; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
638 if (tret && !((TypeArray *)tb)->next->equals(a->type)) |
159 | 639 a = a->toDelegate(sc, tret); |
640 | |
641 Expression *e = new VarExp(loc, v); | |
642 e = new IndexExp(loc, e, new IntegerExp(u + 1 - nparams)); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
643 AssignExp *ae = new AssignExp(loc, e, a); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
644 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
645 ae->op = TOKconstruct; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
646 #endif |
159 | 647 if (c) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
648 c = new CommaExp(loc, c, ae); |
159 | 649 else |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
650 c = ae; |
159 | 651 } |
652 arg = new VarExp(loc, v); | |
653 if (c) | |
654 arg = new CommaExp(loc, c, arg); | |
655 break; | |
656 } | |
657 case Tclass: | |
658 { /* Set arg to be: | |
659 * new Tclass(arg0, arg1, ..., argn) | |
660 */ | |
661 Expressions *args = new Expressions(); | |
662 args->setDim(nargs - i); | |
663 for (size_t u = i; u < nargs; u++) | |
664 args->data[u - i] = arguments->data[u]; | |
665 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
666 break; | |
667 } | |
668 default: | |
669 if (!arg) | |
670 { error(loc, "not enough arguments"); | |
671 return; | |
672 } | |
673 break; | |
674 } | |
675 arg = arg->semantic(sc); | |
676 //printf("\targ = '%s'\n", arg->toChars()); | |
677 arguments->setDim(i + 1); | |
678 done = 1; | |
679 } | |
680 | |
681 L1: | |
682 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
683 arg = arg->implicitCastTo(sc, p->type); | |
684 if (p->storageClass & (STCout | STCref)) | |
685 { | |
686 // BUG: should check that argument to ref is type 'invariant' | |
687 // BUG: assignments to ref should also be type 'invariant' | |
688 arg = arg->modifiableLvalue(sc, arg); | |
689 | |
690 //if (arg->op == TOKslice) | |
691 //arg->error("cannot modify slice %s", arg->toChars()); | |
692 } | |
693 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
694 // LDC we don't want this! |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
695 #if !IN_LLVM |
159 | 696 // Convert static arrays to pointers |
697 tb = arg->type->toBasetype(); | |
698 if (tb->ty == Tsarray) | |
699 { | |
700 arg = arg->checkToPointer(); | |
701 } | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
702 #endif |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
703 |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
704 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
705 if (tb->ty == Tstruct && !(p->storageClass & (STCref | STCout))) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
706 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
707 arg = callCpCtor(loc, sc, arg); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
708 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
709 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
710 |
159 | 711 // Convert lazy argument to a delegate |
712 if (p->storageClass & STClazy) | |
713 { | |
714 arg = arg->toDelegate(sc, p->type); | |
715 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
716 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
717 /* Look for arguments that cannot 'escape' from the called |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
718 * function. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
719 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
720 if (!tf->parameterEscapes(p)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
721 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
722 /* Function literals can only appear once, so if this |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
723 * appearance was scoped, there cannot be any others. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
724 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
725 if (arg->op == TOKfunction) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
726 { FuncExp *fe = (FuncExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
727 fe->fd->tookAddressOf = 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
728 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
729 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
730 /* For passing a delegate to a scoped parameter, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
731 * this doesn't count as taking the address of it. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
732 * We only worry about 'escaping' references to the function. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
733 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
734 else if (arg->op == TOKdelegate) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
735 { DelegateExp *de = (DelegateExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
736 if (de->e1->op == TOKvar) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
737 { VarExp *ve = (VarExp *)de->e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
738 FuncDeclaration *f = ve->var->isFuncDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
739 if (f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
740 { f->tookAddressOf--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
741 //printf("tookAddressOf = %d\n", f->tookAddressOf); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
742 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
743 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
744 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
745 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
746 #endif |
159 | 747 } |
748 else | |
749 { | |
750 | |
751 // If not D linkage, do promotions | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
752 // LDC: don't do promotions on intrinsics |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
753 if (tf->linkage != LINKd && tf->linkage != LINKintrinsic) |
159 | 754 { |
755 // Promote bytes, words, etc., to ints | |
756 arg = arg->integralPromotions(sc); | |
757 | |
758 // Promote floats to doubles | |
759 switch (arg->type->ty) | |
760 { | |
761 case Tfloat32: | |
762 arg = arg->castTo(sc, Type::tfloat64); | |
763 break; | |
764 | |
765 case Timaginary32: | |
766 arg = arg->castTo(sc, Type::timaginary64); | |
767 break; | |
768 } | |
769 } | |
770 | |
771 // Convert static arrays to dynamic arrays | |
772 tb = arg->type->toBasetype(); | |
773 if (tb->ty == Tsarray) | |
774 { TypeSArray *ts = (TypeSArray *)tb; | |
336 | 775 Type *ta = ts->next->arrayOf(); |
159 | 776 if (ts->size(arg->loc) == 0) |
777 { arg = new NullExp(arg->loc); | |
778 arg->type = ta; | |
779 } | |
780 else | |
781 arg = arg->castTo(sc, ta); | |
782 } | |
336 | 783 #if DMDV2 |
784 if (tb->ty == Tstruct) | |
785 { | |
786 arg = callCpCtor(loc, sc, arg); | |
787 } | |
788 | |
789 // Give error for overloaded function addresses | |
790 if (arg->op == TOKsymoff) | |
791 { SymOffExp *se = (SymOffExp *)arg; | |
792 if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique()) | |
793 arg->error("function %s is overloaded", arg->toChars()); | |
794 } | |
795 #endif | |
159 | 796 arg->rvalue(); |
797 } | |
798 arg = arg->optimize(WANTvalue); | |
799 arguments->data[i] = (void *) arg; | |
800 if (done) | |
801 break; | |
802 } | |
803 | |
804 #if !IN_LLVM | |
805 // If D linkage and variadic, add _arguments[] as first argument | |
806 if (tf->linkage == LINKd && tf->varargs == 1) | |
807 { | |
808 Expression *e; | |
809 | |
810 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], | |
811 arguments->dim - nparams); | |
812 arguments->insert(0, e); | |
813 } | |
814 #endif | |
815 } | |
816 | |
817 /************************************************** | |
818 * Write expression out to buf, but wrap it | |
819 * in ( ) if its precedence is less than pr. | |
820 */ | |
821 | |
822 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
823 { | |
336 | 824 //if (precedence[e->op] == 0) e->dump(0); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
825 if (precedence[e->op] < pr || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
826 /* Despite precedence, we don't allow a<b<c expressions. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
827 * They must be parenthesized. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
828 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
829 (pr == PREC_rel && precedence[e->op] == pr)) |
159 | 830 { |
831 buf->writeByte('('); | |
832 e->toCBuffer(buf, hgs); | |
833 buf->writeByte(')'); | |
834 } | |
835 else | |
836 e->toCBuffer(buf, hgs); | |
837 } | |
838 | |
839 /************************************************** | |
840 * Write out argument list to buf. | |
841 */ | |
842 | |
843 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
844 { | |
845 if (arguments) | |
846 { | |
847 for (size_t i = 0; i < arguments->dim; i++) | |
848 { Expression *arg = (Expression *)arguments->data[i]; | |
849 | |
850 if (arg) | |
851 { if (i) | |
852 buf->writeByte(','); | |
853 expToCBuffer(buf, hgs, arg, PREC_assign); | |
854 } | |
855 } | |
856 } | |
857 } | |
858 | |
859 /************************************************** | |
860 * Write out argument types to buf. | |
861 */ | |
862 | |
863 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
864 { | |
865 if (arguments) | |
866 { OutBuffer argbuf; | |
867 | |
868 for (size_t i = 0; i < arguments->dim; i++) | |
869 { Expression *arg = (Expression *)arguments->data[i]; | |
870 | |
871 if (i) | |
872 buf->writeByte(','); | |
873 argbuf.reset(); | |
874 arg->type->toCBuffer2(&argbuf, hgs, 0); | |
875 buf->write(&argbuf); | |
876 } | |
877 } | |
878 } | |
879 | |
880 /******************************** Expression **************************/ | |
881 | |
882 Expression::Expression(Loc loc, enum TOK op, int size) | |
883 : loc(loc) | |
884 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
885 //printf("Expression::Expression(op = %d) this = %p\n", op, this); |
159 | 886 this->loc = loc; |
887 this->op = op; | |
888 this->size = size; | |
889 type = NULL; | |
1151
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
890 |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
891 #if IN_LLVM |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
892 cachedLvalue = NULL; |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
893 #endif |
159 | 894 } |
895 | |
896 Expression *Expression::syntaxCopy() | |
897 { | |
898 //printf("Expression::syntaxCopy()\n"); | |
899 //dump(0); | |
900 return copy(); | |
901 } | |
902 | |
903 /********************************* | |
904 * Does *not* do a deep copy. | |
905 */ | |
906 | |
907 Expression *Expression::copy() | |
908 { | |
909 Expression *e; | |
910 if (!size) | |
911 { | |
912 #ifdef DEBUG | |
913 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
914 printf("op = %d\n", op); | |
915 dump(0); | |
916 #endif | |
917 assert(0); | |
918 } | |
919 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
|
920 //printf("Expression::copy(op = %d) e = %p\n", op, e); |
159 | 921 return (Expression *)memcpy(e, this, size); |
922 } | |
923 | |
924 /************************** | |
925 * Semantically analyze Expression. | |
926 * Determine types, fold constants, etc. | |
927 */ | |
928 | |
929 Expression *Expression::semantic(Scope *sc) | |
930 { | |
931 #if LOGSEMANTIC | |
336 | 932 printf("Expression::semantic() %s\n", toChars()); |
159 | 933 #endif |
934 if (type) | |
935 type = type->semantic(loc, sc); | |
936 else | |
937 type = Type::tvoid; | |
938 return this; | |
939 } | |
940 | |
941 void Expression::print() | |
942 { | |
943 fprintf(stdmsg, "%s\n", toChars()); | |
944 fflush(stdmsg); | |
945 } | |
946 | |
947 char *Expression::toChars() | |
948 { OutBuffer *buf; | |
949 HdrGenState hgs; | |
950 | |
951 memset(&hgs, 0, sizeof(hgs)); | |
952 buf = new OutBuffer(); | |
953 toCBuffer(buf, &hgs); | |
954 return buf->toChars(); | |
955 } | |
956 | |
957 void Expression::error(const char *format, ...) | |
958 { | |
959 va_list ap; | |
960 va_start(ap, format); | |
961 ::verror(loc, format, ap); | |
962 va_end( ap ); | |
963 } | |
964 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
965 void Expression::warning(const char *format, ...) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
966 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
967 if (global.params.warnings && !global.gag) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
968 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
969 va_list ap; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
970 va_start(ap, format); |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1120
diff
changeset
|
971 ::vwarning(loc, format, ap); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
972 va_end( ap ); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
973 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
974 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
975 |
159 | 976 void Expression::rvalue() |
977 { | |
978 if (type && type->toBasetype()->ty == Tvoid) | |
979 { error("expression %s is void and has no value", toChars()); | |
980 #if 0 | |
981 dump(0); | |
982 halt(); | |
983 #endif | |
336 | 984 type = Type::tint32; |
159 | 985 } |
986 } | |
987 | |
988 Expression *Expression::combine(Expression *e1, Expression *e2) | |
989 { | |
990 if (e1) | |
991 { | |
992 if (e2) | |
993 { | |
994 e1 = new CommaExp(e1->loc, e1, e2); | |
995 e1->type = e2->type; | |
996 } | |
997 } | |
998 else | |
999 e1 = e2; | |
1000 return e1; | |
1001 } | |
1002 | |
1003 integer_t Expression::toInteger() | |
1004 { | |
1005 //printf("Expression %s\n", Token::toChars(op)); | |
1006 error("Integer constant expression expected instead of %s", toChars()); | |
1007 return 0; | |
1008 } | |
1009 | |
1010 uinteger_t Expression::toUInteger() | |
1011 { | |
1012 //printf("Expression %s\n", Token::toChars(op)); | |
1013 return (uinteger_t)toInteger(); | |
1014 } | |
1015 | |
1016 real_t Expression::toReal() | |
1017 { | |
1018 error("Floating point constant expression expected instead of %s", toChars()); | |
1019 return 0; | |
1020 } | |
1021 | |
1022 real_t Expression::toImaginary() | |
1023 { | |
1024 error("Floating point constant expression expected instead of %s", toChars()); | |
1025 return 0; | |
1026 } | |
1027 | |
1028 complex_t Expression::toComplex() | |
1029 { | |
1030 error("Floating point constant expression expected instead of %s", toChars()); | |
1031 #ifdef IN_GCC | |
1032 return complex_t(real_t(0)); // %% nicer | |
1033 #else | |
1034 return 0; | |
1035 #endif | |
1036 } | |
1037 | |
1038 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1039 { | |
1040 buf->writestring(Token::toChars(op)); | |
1041 } | |
1042 | |
1043 void Expression::toMangleBuffer(OutBuffer *buf) | |
1044 { | |
1045 error("expression %s is not a valid template value argument", toChars()); | |
1046 } | |
1047 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1048 /*************************************** |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1049 * Return !=0 if expression is an lvalue. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1050 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1051 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1052 int Expression::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1053 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1054 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1055 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1056 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1057 |
159 | 1058 /******************************* |
1059 * Give error if we're not an lvalue. | |
1060 * If we can, convert expression to be an lvalue. | |
1061 */ | |
1062 | |
1063 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
1064 { | |
1065 if (!e) | |
1066 e = this; | |
1067 else if (!loc.filename) | |
1068 loc = e->loc; | |
1069 error("%s is not an lvalue", e->toChars()); | |
1070 return this; | |
1071 } | |
1072 | |
1073 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
1074 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1075 //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
|
1076 |
159 | 1077 // See if this expression is a modifiable lvalue (i.e. not const) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1078 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1079 if (type && (!type->isMutable() || !type->isAssignable())) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1080 error("%s is not mutable", e->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1081 #endif |
159 | 1082 return toLvalue(sc, e); |
1083 } | |
1084 | |
1085 /************************************ | |
1086 * Detect cases where pointers to the stack can 'escape' the | |
1087 * lifetime of the stack frame. | |
1088 */ | |
1089 | |
1090 void Expression::checkEscape() | |
1091 { | |
1092 } | |
1093 | |
1094 void Expression::checkScalar() | |
1095 { | |
1096 if (!type->isscalar()) | |
1097 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
1098 } | |
1099 | |
1100 void Expression::checkNoBool() | |
1101 { | |
1102 if (type->toBasetype()->ty == Tbool) | |
1103 error("operation not allowed on bool '%s'", toChars()); | |
1104 } | |
1105 | |
1106 Expression *Expression::checkIntegral() | |
1107 { | |
1108 if (!type->isintegral()) | |
1109 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
1110 return new IntegerExp(0); | |
1111 } | |
1112 return this; | |
1113 } | |
1114 | |
1115 Expression *Expression::checkArithmetic() | |
1116 { | |
1117 if (!type->isintegral() && !type->isfloating()) | |
1118 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); | |
1119 return new IntegerExp(0); | |
1120 } | |
1121 return this; | |
1122 } | |
1123 | |
1124 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
1125 { | |
1126 s->checkDeprecated(loc, sc); | |
1127 } | |
1128 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1129 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1130 void Expression::checkPurity(Scope *sc, FuncDeclaration *f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1131 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1132 if (sc->func && sc->func->isPure() && !sc->intypeof && !f->isPure()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1133 error("pure function '%s' cannot call impure function '%s'\n", |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1134 sc->func->toChars(), f->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1135 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1136 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1137 |
159 | 1138 /******************************** |
1139 * Check for expressions that have no use. | |
1140 * Input: | |
1141 * flag 0 not going to use the result, so issue error message if no | |
1142 * side effects | |
1143 * 1 the result of the expression is used, but still check | |
1144 * for useless subexpressions | |
1145 * 2 do not issue error messages, just return !=0 if expression | |
1146 * has side effects | |
1147 */ | |
1148 | |
1149 int Expression::checkSideEffect(int flag) | |
1150 { | |
1151 if (flag == 0) | |
1152 { if (op == TOKimport) | |
1153 { | |
1154 error("%s has no effect", toChars()); | |
1155 } | |
1156 else | |
1157 error("%s has no effect in expression (%s)", | |
1158 Token::toChars(op), toChars()); | |
1159 } | |
1160 return 0; | |
1161 } | |
1162 | |
1163 /***************************** | |
1164 * Check that expression can be tested for true or false. | |
1165 */ | |
1166 | |
1167 Expression *Expression::checkToBoolean() | |
1168 { | |
1169 // Default is 'yes' - do nothing | |
1170 | |
1171 #ifdef DEBUG | |
1172 if (!type) | |
1173 dump(0); | |
1174 #endif | |
1175 | |
1176 if (!type->checkBoolean()) | |
1177 { | |
1178 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
1179 } | |
1180 return this; | |
1181 } | |
1182 | |
1183 /**************************** | |
1184 */ | |
1185 | |
1186 Expression *Expression::checkToPointer() | |
1187 { | |
1188 Expression *e; | |
1189 Type *tb; | |
1190 | |
1191 //printf("Expression::checkToPointer()\n"); | |
1192 e = this; | |
1193 | |
1194 // If C static array, convert to pointer | |
1195 tb = type->toBasetype(); | |
1196 if (tb->ty == Tsarray) | |
1197 { TypeSArray *ts = (TypeSArray *)tb; | |
1198 if (ts->size(loc) == 0) | |
1199 e = new NullExp(loc); | |
1200 else | |
1201 e = new AddrExp(loc, this); | |
336 | 1202 e->type = ts->next->pointerTo(); |
159 | 1203 } |
1204 return e; | |
1205 } | |
1206 | |
1207 /****************************** | |
1208 * Take address of expression. | |
1209 */ | |
1210 | |
1211 Expression *Expression::addressOf(Scope *sc) | |
1212 { | |
1213 Expression *e; | |
1214 | |
1215 //printf("Expression::addressOf()\n"); | |
1216 e = toLvalue(sc, NULL); | |
1217 e = new AddrExp(loc, e); | |
1218 e->type = type->pointerTo(); | |
1219 return e; | |
1220 } | |
1221 | |
1222 /****************************** | |
1223 * If this is a reference, dereference it. | |
1224 */ | |
1225 | |
1226 Expression *Expression::deref() | |
1227 { | |
1228 //printf("Expression::deref()\n"); | |
1229 if (type->ty == Treference) | |
1230 { Expression *e; | |
1231 | |
1232 e = new PtrExp(loc, this); | |
336 | 1233 e->type = ((TypeReference *)type)->next; |
159 | 1234 return e; |
1235 } | |
1236 return this; | |
1237 } | |
1238 | |
1239 /******************************** | |
1240 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
1241 */ | |
1242 | |
1243 int Expression::isBool(int result) | |
1244 { | |
1245 return FALSE; | |
1246 } | |
1247 | |
1248 /******************************** | |
1249 * Does this expression result in either a 1 or a 0? | |
1250 */ | |
1251 | |
1252 int Expression::isBit() | |
1253 { | |
1254 return FALSE; | |
1255 } | |
1256 | |
336 | 1257 /******************************** |
1258 * Can this expression throw an exception? | |
1259 * Valid only after semantic() pass. | |
1260 */ | |
1261 | |
1262 int Expression::canThrow() | |
1263 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1264 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1265 return FALSE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1266 #else |
336 | 1267 return TRUE; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1268 #endif |
336 | 1269 } |
1270 | |
1271 | |
1272 | |
159 | 1273 Expressions *Expression::arraySyntaxCopy(Expressions *exps) |
1274 { Expressions *a = NULL; | |
1275 | |
1276 if (exps) | |
1277 { | |
1278 a = new Expressions(); | |
1279 a->setDim(exps->dim); | |
1280 for (int i = 0; i < a->dim; i++) | |
1281 { Expression *e = (Expression *)exps->data[i]; | |
1282 | |
1283 e = e->syntaxCopy(); | |
1284 a->data[i] = e; | |
1285 } | |
1286 } | |
1287 return a; | |
1288 } | |
1289 | |
1290 /******************************** IntegerExp **************************/ | |
1291 | |
1292 IntegerExp::IntegerExp(Loc loc, integer_t value, Type *type) | |
1293 : Expression(loc, TOKint64, sizeof(IntegerExp)) | |
1294 { | |
1295 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1296 if (type && !type->isscalar()) | |
1297 { | |
336 | 1298 //printf("%s, loc = %d\n", toChars(), loc.linnum); |
159 | 1299 error("integral constant must be scalar type, not %s", type->toChars()); |
1300 type = Type::terror; | |
1301 } | |
1302 this->type = type; | |
1303 this->value = value; | |
1304 } | |
1305 | |
1306 IntegerExp::IntegerExp(integer_t value) | |
1307 : Expression(0, TOKint64, sizeof(IntegerExp)) | |
1308 { | |
1309 this->type = Type::tint32; | |
1310 this->value = value; | |
1311 } | |
1312 | |
1313 int IntegerExp::equals(Object *o) | |
1314 { IntegerExp *ne; | |
1315 | |
1316 if (this == o || | |
1317 (((Expression *)o)->op == TOKint64 && | |
1318 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1319 value == ne->value)) | |
1320 return 1; | |
1321 return 0; | |
1322 } | |
1323 | |
1324 char *IntegerExp::toChars() | |
1325 { | |
1326 #if 1 | |
1327 return Expression::toChars(); | |
1328 #else | |
1329 static char buffer[sizeof(value) * 3 + 1]; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1330 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1331 sprintf(buffer, "%jd", value); |
159 | 1332 return buffer; |
1333 #endif | |
1334 } | |
1335 | |
1336 integer_t IntegerExp::toInteger() | |
1337 { Type *t; | |
1338 | |
1339 t = type; | |
1340 while (t) | |
1341 { | |
1342 switch (t->ty) | |
1343 { | |
1344 case Tbit: | |
1345 case Tbool: value = (value != 0); break; | |
1346 case Tint8: value = (d_int8) value; break; | |
1347 case Tchar: | |
1348 case Tuns8: value = (d_uns8) value; break; | |
1349 case Tint16: value = (d_int16) value; break; | |
1350 case Twchar: | |
1351 case Tuns16: value = (d_uns16) value; break; | |
1352 case Tint32: value = (d_int32) value; break; | |
1353 case Tdchar: | |
1354 case Tuns32: value = (d_uns32) value; break; | |
1355 case Tint64: value = (d_int64) value; break; | |
1356 case Tuns64: value = (d_uns64) value; break; | |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1357 case Tpointer: |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1358 // FIXME: Other pointer widths than 32 and 64? |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1359 if (PTRSIZE == 4) |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1360 value = (d_uns32) value; |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1361 else |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1362 value = (d_uns64) value; |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1363 break; |
159 | 1364 |
1365 case Tenum: | |
1366 { | |
1367 TypeEnum *te = (TypeEnum *)t; | |
1368 t = te->sym->memtype; | |
1369 continue; | |
1370 } | |
1371 | |
1372 case Ttypedef: | |
1373 { | |
1374 TypeTypedef *tt = (TypeTypedef *)t; | |
1375 t = tt->sym->basetype; | |
1376 continue; | |
1377 } | |
1378 | |
1379 default: | |
336 | 1380 /* This can happen if errors, such as |
1381 * the type is painted on like in fromConstInitializer(). | |
1382 */ | |
1383 if (!global.errors) | |
1384 { type->print(); | |
1385 assert(0); | |
1386 } | |
159 | 1387 break; |
1388 } | |
1389 break; | |
1390 } | |
1391 return value; | |
1392 } | |
1393 | |
1394 real_t IntegerExp::toReal() | |
1395 { | |
1396 Type *t; | |
1397 | |
1398 toInteger(); | |
1399 t = type->toBasetype(); | |
1400 if (t->ty == Tuns64) | |
1401 return (real_t)(d_uns64)value; | |
1402 else | |
1403 return (real_t)(d_int64)value; | |
1404 } | |
1405 | |
1406 real_t IntegerExp::toImaginary() | |
1407 { | |
1408 return (real_t) 0; | |
1409 } | |
1410 | |
1411 complex_t IntegerExp::toComplex() | |
1412 { | |
1413 return toReal(); | |
1414 } | |
1415 | |
1416 int IntegerExp::isBool(int result) | |
1417 { | |
1418 return result ? value != 0 : value == 0; | |
1419 } | |
1420 | |
1421 Expression *IntegerExp::semantic(Scope *sc) | |
1422 { | |
1423 if (!type) | |
1424 { | |
1425 // Determine what the type of this number is | |
1426 integer_t number = value; | |
1427 | |
1428 if (number & 0x8000000000000000LL) | |
1429 type = Type::tuns64; | |
1430 else if (number & 0xFFFFFFFF80000000LL) | |
1431 type = Type::tint64; | |
1432 else | |
1433 type = Type::tint32; | |
1434 } | |
1435 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
|
1436 { 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
|
1437 type = type->semantic(loc, sc); |
159 | 1438 } |
1439 return this; | |
1440 } | |
1441 | |
1442 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1443 { | |
1444 if (!e) | |
1445 e = this; | |
1446 else if (!loc.filename) | |
1447 loc = e->loc; | |
1448 e->error("constant %s is not an lvalue", e->toChars()); | |
1449 return this; | |
1450 } | |
1451 | |
1452 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1453 { | |
1454 integer_t v = toInteger(); | |
1455 | |
1456 if (type) | |
1457 { Type *t = type; | |
1458 | |
1459 L1: | |
1460 switch (t->ty) | |
1461 { | |
1462 case Tenum: | |
1463 { TypeEnum *te = (TypeEnum *)t; | |
1464 buf->printf("cast(%s)", te->sym->toChars()); | |
1465 t = te->sym->memtype; | |
1466 goto L1; | |
1467 } | |
1468 | |
1469 case Ttypedef: | |
1470 { TypeTypedef *tt = (TypeTypedef *)t; | |
1471 buf->printf("cast(%s)", tt->sym->toChars()); | |
1472 t = tt->sym->basetype; | |
1473 goto L1; | |
1474 } | |
1475 | |
1476 case Twchar: // BUG: need to cast(wchar) | |
1477 case Tdchar: // BUG: need to cast(dchar) | |
1478 if ((uinteger_t)v > 0xFF) | |
1479 { | |
1480 buf->printf("'\\U%08x'", v); | |
1481 break; | |
1482 } | |
1483 case Tchar: | |
1484 if (v == '\'') | |
1485 buf->writestring("'\\''"); | |
1486 else if (isprint(v) && v != '\\') | |
1487 buf->printf("'%c'", (int)v); | |
1488 else | |
1489 buf->printf("'\\x%02x'", (int)v); | |
1490 break; | |
1491 | |
1492 case Tint8: | |
1493 buf->writestring("cast(byte)"); | |
1494 goto L2; | |
1495 | |
1496 case Tint16: | |
1497 buf->writestring("cast(short)"); | |
1498 goto L2; | |
1499 | |
1500 case Tint32: | |
1501 L2: | |
1502 buf->printf("%d", (int)v); | |
1503 break; | |
1504 | |
1505 case Tuns8: | |
1506 buf->writestring("cast(ubyte)"); | |
1507 goto L3; | |
1508 | |
1509 case Tuns16: | |
1510 buf->writestring("cast(ushort)"); | |
1511 goto L3; | |
1512 | |
1513 case Tuns32: | |
1514 L3: | |
1515 buf->printf("%du", (unsigned)v); | |
1516 break; | |
1517 | |
1518 case Tint64: | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1519 buf->printf("%jdL", v); |
159 | 1520 break; |
1521 | |
1522 case Tuns64: | |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1523 L4: |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1524 buf->printf("%juLU", v); |
159 | 1525 break; |
1526 | |
1527 case Tbit: | |
1528 case Tbool: | |
1529 buf->writestring((char *)(v ? "true" : "false")); | |
1530 break; | |
1531 | |
1532 case Tpointer: | |
1533 buf->writestring("cast("); | |
1534 buf->writestring(t->toChars()); | |
1535 buf->writeByte(')'); | |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1536 // FIXME: Other pointer widths than 32 and 64? |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1537 if (PTRSIZE == 4) |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1538 goto L3; |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1539 else |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1540 goto L4; |
159 | 1541 |
1542 default: | |
336 | 1543 /* This can happen if errors, such as |
1544 * the type is painted on like in fromConstInitializer(). | |
1545 */ | |
1546 if (!global.errors) | |
1547 { | |
159 | 1548 #ifdef DEBUG |
336 | 1549 t->print(); |
159 | 1550 #endif |
336 | 1551 assert(0); |
1552 } | |
1553 break; | |
159 | 1554 } |
1555 } | |
1556 else if (v & 0x8000000000000000LL) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1557 buf->printf("0x%jx", v); |
159 | 1558 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1559 buf->printf("%jd", v); |
159 | 1560 } |
1561 | |
1562 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1563 { | |
1564 if ((sinteger_t)value < 0) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1565 buf->printf("N%jd", -value); |
159 | 1566 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1567 buf->printf("%jd", value); |
159 | 1568 } |
1569 | |
1570 /******************************** RealExp **************************/ | |
1571 | |
1572 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1573 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1574 { | |
1575 //printf("RealExp::RealExp(%Lg)\n", value); | |
1576 this->value = value; | |
1577 this->type = type; | |
1578 } | |
1579 | |
1580 char *RealExp::toChars() | |
1581 { | |
1582 char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1583 | |
1584 #ifdef IN_GCC | |
1585 value.format(buffer, sizeof(buffer)); | |
1586 if (type->isimaginary()) | |
1587 strcat(buffer, "i"); | |
1588 #else | |
1589 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1590 #endif | |
1591 assert(strlen(buffer) < sizeof(buffer)); | |
1592 return mem.strdup(buffer); | |
1593 } | |
1594 | |
1595 integer_t RealExp::toInteger() | |
1596 { | |
1597 #ifdef IN_GCC | |
1598 return toReal().toInt(); | |
1599 #else | |
1600 return (sinteger_t) toReal(); | |
1601 #endif | |
1602 } | |
1603 | |
1604 uinteger_t RealExp::toUInteger() | |
1605 { | |
1606 #ifdef IN_GCC | |
1607 return (uinteger_t) toReal().toInt(); | |
1608 #else | |
1609 return (uinteger_t) toReal(); | |
1610 #endif | |
1611 } | |
1612 | |
1613 real_t RealExp::toReal() | |
1614 { | |
1615 return type->isreal() ? value : 0; | |
1616 } | |
1617 | |
1618 real_t RealExp::toImaginary() | |
1619 { | |
1620 return type->isreal() ? 0 : value; | |
1621 } | |
1622 | |
1623 complex_t RealExp::toComplex() | |
1624 { | |
1625 #ifdef __DMC__ | |
1626 return toReal() + toImaginary() * I; | |
1627 #else | |
1628 return complex_t(toReal(), toImaginary()); | |
1629 #endif | |
1630 } | |
1631 | |
1632 /******************************** | |
1633 * Test to see if two reals are the same. | |
1634 * Regard NaN's as equivalent. | |
1635 * Regard +0 and -0 as different. | |
1636 */ | |
1637 | |
1638 int RealEquals(real_t x1, real_t x2) | |
1639 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1640 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1641 return (__inline_isnan(x1) && __inline_isnan(x2)) || |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1642 #else |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1643 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
|
1644 (isnan(x1) && isnan(x2)) || |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1645 #endif |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1646 // 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
|
1647 (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
|
1648 // 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
|
1649 (x1 != 0. && x1 == x2); |
159 | 1650 } |
1651 | |
1652 int RealExp::equals(Object *o) | |
1653 { RealExp *ne; | |
1654 | |
1655 if (this == o || | |
1656 (((Expression *)o)->op == TOKfloat64 && | |
1657 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1658 RealEquals(value, ne->value) | |
1659 ) | |
1660 ) | |
1661 return 1; | |
1662 return 0; | |
1663 } | |
1664 | |
1665 Expression *RealExp::semantic(Scope *sc) | |
1666 { | |
1667 if (!type) | |
1668 type = Type::tfloat64; | |
1669 else | |
1670 type = type->semantic(loc, sc); | |
1671 return this; | |
1672 } | |
1673 | |
1674 int RealExp::isBool(int result) | |
1675 { | |
1676 #ifdef IN_GCC | |
1677 return result ? (! value.isZero()) : (value.isZero()); | |
1678 #else | |
1679 return result ? (value != 0) | |
1680 : (value == 0); | |
1681 #endif | |
1682 } | |
1683 | |
1684 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1685 { | |
1686 /* In order to get an exact representation, try converting it | |
1687 * to decimal then back again. If it matches, use it. | |
1688 * If it doesn't, fall back to hex, which is | |
1689 * always exact. | |
1690 */ | |
1691 char buffer[25]; | |
1692 sprintf(buffer, "%Lg", value); | |
1693 assert(strlen(buffer) < sizeof(buffer)); | |
1694 #if _WIN32 && __DMC__ | |
1695 char *save = __locale_decpoint; | |
1696 __locale_decpoint = "."; | |
1697 real_t r = strtold(buffer, NULL); | |
1698 __locale_decpoint = save; | |
1699 #else | |
1700 real_t r = strtold(buffer, NULL); | |
1701 #endif | |
1702 if (r == value) // if exact duplication | |
1703 buf->writestring(buffer); | |
1704 else | |
1705 buf->printf("%La", value); // ensure exact duplication | |
1706 | |
1707 if (type) | |
1708 { | |
1709 Type *t = type->toBasetype(); | |
1710 switch (t->ty) | |
1711 { | |
1712 case Tfloat32: | |
1713 case Timaginary32: | |
1714 case Tcomplex32: | |
1715 buf->writeByte('F'); | |
1716 break; | |
1717 | |
1718 case Tfloat80: | |
1719 case Timaginary80: | |
1720 case Tcomplex80: | |
1721 buf->writeByte('L'); | |
1722 break; | |
1723 | |
1724 default: | |
1725 break; | |
1726 } | |
1727 if (t->isimaginary()) | |
1728 buf->writeByte('i'); | |
1729 } | |
1730 } | |
1731 | |
1732 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1733 { | |
1734 floatToBuffer(buf, type, value); | |
1735 } | |
1736 | |
1737 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1738 { | |
1739 /* Rely on %A to get portable mangling. | |
1740 * Must munge result to get only identifier characters. | |
1741 * | |
1742 * Possible values from %A => mangled result | |
1743 * NAN => NAN | |
1744 * -INF => NINF | |
1745 * INF => INF | |
1746 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1747 * 0X1.9P+2 => 19P2 | |
1748 */ | |
1749 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1750 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1751 if (__inline_isnan(value)) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1752 #else |
159 | 1753 if (isnan(value)) |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1754 #endif |
159 | 1755 buf->writestring("NAN"); // no -NAN bugs |
1756 else | |
1757 { | |
1758 char buffer[32]; | |
1759 int n = sprintf(buffer, "%LA", value); | |
1760 assert(n > 0 && n < sizeof(buffer)); | |
1761 for (int i = 0; i < n; i++) | |
1762 { char c = buffer[i]; | |
1763 | |
1764 switch (c) | |
1765 { | |
1766 case '-': | |
1767 buf->writeByte('N'); | |
1768 break; | |
1769 | |
1770 case '+': | |
1771 case 'X': | |
1772 case '.': | |
1773 break; | |
1774 | |
1775 case '0': | |
1776 if (i < 2) | |
1777 break; // skip leading 0X | |
1778 default: | |
1779 buf->writeByte(c); | |
1780 break; | |
1781 } | |
1782 } | |
1783 } | |
1784 } | |
1785 | |
1786 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1787 { | |
1788 buf->writeByte('e'); | |
1789 realToMangleBuffer(buf, value); | |
1790 } | |
1791 | |
1792 | |
1793 /******************************** ComplexExp **************************/ | |
1794 | |
1795 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1796 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1797 { | |
1798 this->value = value; | |
1799 this->type = type; | |
1800 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1801 } | |
1802 | |
1803 char *ComplexExp::toChars() | |
1804 { | |
1805 char buffer[sizeof(value) * 3 + 8 + 1]; | |
1806 | |
1807 #ifdef IN_GCC | |
1808 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1809 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1810 creall(value).format(buf1, sizeof(buf1)); | |
1811 cimagl(value).format(buf2, sizeof(buf2)); | |
1812 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1813 #else | |
1814 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1815 assert(strlen(buffer) < sizeof(buffer)); | |
1816 #endif | |
1817 return mem.strdup(buffer); | |
1818 } | |
1819 | |
1820 integer_t ComplexExp::toInteger() | |
1821 { | |
1822 #ifdef IN_GCC | |
1823 return (sinteger_t) toReal().toInt(); | |
1824 #else | |
1825 return (sinteger_t) toReal(); | |
1826 #endif | |
1827 } | |
1828 | |
1829 uinteger_t ComplexExp::toUInteger() | |
1830 { | |
1831 #ifdef IN_GCC | |
1832 return (uinteger_t) toReal().toInt(); | |
1833 #else | |
1834 return (uinteger_t) toReal(); | |
1835 #endif | |
1836 } | |
1837 | |
1838 real_t ComplexExp::toReal() | |
1839 { | |
1840 return creall(value); | |
1841 } | |
1842 | |
1843 real_t ComplexExp::toImaginary() | |
1844 { | |
1845 return cimagl(value); | |
1846 } | |
1847 | |
1848 complex_t ComplexExp::toComplex() | |
1849 { | |
1850 return value; | |
1851 } | |
1852 | |
1853 int ComplexExp::equals(Object *o) | |
1854 { ComplexExp *ne; | |
1855 | |
1856 if (this == o || | |
1857 (((Expression *)o)->op == TOKcomplex80 && | |
1858 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1859 RealEquals(creall(value), creall(ne->value)) && | |
1860 RealEquals(cimagl(value), cimagl(ne->value)) | |
1861 ) | |
1862 ) | |
1863 return 1; | |
1864 return 0; | |
1865 } | |
1866 | |
1867 Expression *ComplexExp::semantic(Scope *sc) | |
1868 { | |
1869 if (!type) | |
1870 type = Type::tcomplex80; | |
1871 else | |
1872 type = type->semantic(loc, sc); | |
1873 return this; | |
1874 } | |
1875 | |
1876 int ComplexExp::isBool(int result) | |
1877 { | |
1878 if (result) | |
1879 return (bool)(value); | |
1880 else | |
1881 return !value; | |
1882 } | |
1883 | |
1884 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1885 { | |
1886 /* Print as: | |
1887 * (re+imi) | |
1888 */ | |
1889 #ifdef IN_GCC | |
1890 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1891 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1892 creall(value).format(buf1, sizeof(buf1)); | |
1893 cimagl(value).format(buf2, sizeof(buf2)); | |
1894 buf->printf("(%s+%si)", buf1, buf2); | |
1895 #else | |
1896 buf->writeByte('('); | |
1897 floatToBuffer(buf, type, creall(value)); | |
1898 buf->writeByte('+'); | |
1899 floatToBuffer(buf, type, cimagl(value)); | |
1900 buf->writestring("i)"); | |
1901 #endif | |
1902 } | |
1903 | |
1904 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
1905 { | |
1906 buf->writeByte('c'); | |
1907 real_t r = toReal(); | |
1908 realToMangleBuffer(buf, r); | |
1909 buf->writeByte('c'); // separate the two | |
1910 r = toImaginary(); | |
1911 realToMangleBuffer(buf, r); | |
1912 } | |
1913 | |
1914 /******************************** IdentifierExp **************************/ | |
1915 | |
1916 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
1917 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
1918 { | |
1919 this->ident = ident; | |
1920 } | |
1921 | |
1922 Expression *IdentifierExp::semantic(Scope *sc) | |
1923 { | |
1924 Dsymbol *s; | |
1925 Dsymbol *scopesym; | |
1926 | |
1927 #if LOGSEMANTIC | |
1928 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
1929 #endif | |
1930 s = sc->search(loc, ident, &scopesym); | |
1931 if (s) | |
1932 { Expression *e; | |
1933 WithScopeSymbol *withsym; | |
1934 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1935 /* 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
|
1936 */ |
159 | 1937 withsym = scopesym->isWithScopeSymbol(); |
1938 if (withsym) | |
1939 { | |
1940 s = s->toAlias(); | |
1941 | |
1942 // Same as wthis.ident | |
1943 if (s->needThis() || s->isTemplateDeclaration()) | |
1944 { | |
1945 e = new VarExp(loc, withsym->withstate->wthis); | |
1946 e = new DotIdExp(loc, e, ident); | |
1947 } | |
1948 else | |
1949 { Type *t = withsym->withstate->wthis->type; | |
1950 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
|
1951 t = ((TypePointer *)t)->next; |
159 | 1952 e = new TypeDotIdExp(loc, t, ident); |
1953 } | |
1954 } | |
1955 else | |
1956 { | |
1957 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
1958 { // Kludge to run semantic() here because | |
1959 // ArrayScopeSymbol::search() doesn't have access to sc. | |
1960 s->semantic(sc); | |
1961 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1962 /* If f is really a function template, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1963 * then replace f with the function template declaration. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1964 */ |
159 | 1965 FuncDeclaration *f = s->isFuncDeclaration(); |
1966 if (f && f->parent) | |
1967 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
1968 | |
1969 if (ti && | |
1970 !ti->isTemplateMixin() && | |
1971 (ti->name == f->ident || | |
1972 ti->toAlias()->ident == f->ident) | |
1973 && | |
1974 ti->tempdecl && ti->tempdecl->onemember) | |
1975 { | |
1976 TemplateDeclaration *tempdecl = ti->tempdecl; | |
1977 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
1978 tempdecl = tempdecl->overroot; // then get the start | |
1979 e = new TemplateExp(loc, tempdecl); | |
1980 e = e->semantic(sc); | |
1981 return e; | |
1982 } | |
1983 } | |
1984 e = new DsymbolExp(loc, s); | |
1985 } | |
1986 return e->semantic(sc); | |
1987 } | |
1988 error("undefined identifier %s", ident->toChars()); | |
1989 type = Type::terror; | |
1990 return this; | |
1991 } | |
1992 | |
1993 char *IdentifierExp::toChars() | |
1994 { | |
1995 return ident->toChars(); | |
1996 } | |
1997 | |
1998 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1999 { | |
2000 if (hgs->hdrgen) | |
2001 buf->writestring(ident->toHChars2()); | |
2002 else | |
2003 buf->writestring(ident->toChars()); | |
2004 } | |
2005 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2006 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2007 int IdentifierExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2008 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2009 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2010 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2011 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2012 |
159 | 2013 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) |
2014 { | |
2015 #if 0 | |
2016 tym = tybasic(e1->ET->Tty); | |
2017 if (!(tyscalar(tym) || | |
2018 tym == TYstruct || | |
2019 tym == TYarray && e->Eoper == TOKaddr)) | |
2020 synerr(EM_lvalue); // lvalue expected | |
2021 #endif | |
2022 return this; | |
2023 } | |
2024 | |
2025 /******************************** DollarExp **************************/ | |
2026 | |
2027 DollarExp::DollarExp(Loc loc) | |
2028 : IdentifierExp(loc, Id::dollar) | |
2029 { | |
2030 } | |
2031 | |
2032 /******************************** DsymbolExp **************************/ | |
2033 | |
2034 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
2035 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
2036 { | |
2037 this->s = s; | |
2038 } | |
2039 | |
2040 Expression *DsymbolExp::semantic(Scope *sc) | |
2041 { | |
2042 #if LOGSEMANTIC | |
2043 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
2044 #endif | |
2045 | |
2046 Lagain: | |
2047 EnumMember *em; | |
2048 Expression *e; | |
2049 VarDeclaration *v; | |
2050 FuncDeclaration *f; | |
2051 FuncLiteralDeclaration *fld; | |
2052 Declaration *d; | |
2053 ClassDeclaration *cd; | |
2054 ClassDeclaration *thiscd = NULL; | |
2055 Import *imp; | |
2056 Package *pkg; | |
2057 Type *t; | |
2058 | |
2059 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
2060 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
2061 if (type) | |
2062 return this; | |
2063 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
2064 checkDeprecated(sc, s); | |
2065 s = s->toAlias(); | |
2066 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
2067 if (!s->isFuncDeclaration()) | |
2068 checkDeprecated(sc, s); | |
2069 | |
2070 if (sc->func) | |
2071 thiscd = sc->func->parent->isClassDeclaration(); | |
2072 | |
2073 // BUG: This should happen after overload resolution for functions, not before | |
2074 if (s->needThis()) | |
2075 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2076 if (hasThis(sc) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2077 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2078 && !s->isFuncDeclaration() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2079 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2080 ) |
159 | 2081 { |
2082 // Supply an implicit 'this', as in | |
2083 // this.ident | |
2084 | |
2085 DotVarExp *de; | |
2086 | |
2087 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
2088 return de->semantic(sc); | |
2089 } | |
2090 } | |
2091 | |
2092 em = s->isEnumMember(); | |
2093 if (em) | |
2094 { | |
2095 e = em->value->copy(); | |
2096 e = e->semantic(sc); | |
2097 return e; | |
2098 } | |
2099 v = s->isVarDeclaration(); | |
2100 if (v) | |
2101 { | |
2102 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
2103 if (!type) | |
2104 { type = v->type; | |
2105 if (!v->type) | |
2106 { error("forward reference of %s", v->toChars()); | |
2107 type = Type::terror; | |
2108 } | |
2109 } | |
2110 if (v->isConst() && type->toBasetype()->ty != Tsarray) | |
2111 { | |
2112 if (v->init) | |
2113 { | |
2114 if (v->inuse) | |
2115 { | |
2116 error("circular reference to '%s'", v->toChars()); | |
2117 type = Type::tint32; | |
2118 return this; | |
2119 } | |
2120 ExpInitializer *ei = v->init->isExpInitializer(); | |
2121 if (ei) | |
2122 { | |
2123 e = ei->exp->copy(); // make copy so we can change loc | |
2124 if (e->op == TOKstring || !e->type) | |
2125 e = e->semantic(sc); | |
2126 e = e->implicitCastTo(sc, type); | |
2127 e->loc = loc; | |
2128 return e; | |
2129 } | |
2130 } | |
2131 else | |
2132 { | |
2133 e = type->defaultInit(); | |
2134 e->loc = loc; | |
2135 return e; | |
2136 } | |
2137 } | |
2138 e = new VarExp(loc, v); | |
2139 e->type = type; | |
2140 e = e->semantic(sc); | |
2141 return e->deref(); | |
2142 } | |
2143 fld = s->isFuncLiteralDeclaration(); | |
2144 if (fld) | |
2145 { //printf("'%s' is a function literal\n", fld->toChars()); | |
2146 e = new FuncExp(loc, fld); | |
2147 return e->semantic(sc); | |
2148 } | |
2149 f = s->isFuncDeclaration(); | |
2150 if (f) | |
2151 { //printf("'%s' is a function\n", f->toChars()); | |
2152 return new VarExp(loc, f); | |
2153 } | |
2154 cd = s->isClassDeclaration(); | |
2155 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
2156 { | |
2157 // We need to add an implicit 'this' if cd is this class or a base class. | |
2158 DotTypeExp *dte; | |
2159 | |
2160 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
2161 return dte->semantic(sc); | |
2162 } | |
2163 imp = s->isImport(); | |
2164 if (imp) | |
2165 { | |
2166 ScopeExp *ie; | |
2167 | |
2168 ie = new ScopeExp(loc, imp->pkg); | |
2169 return ie->semantic(sc); | |
2170 } | |
2171 pkg = s->isPackage(); | |
2172 if (pkg) | |
2173 { | |
2174 ScopeExp *ie; | |
2175 | |
2176 ie = new ScopeExp(loc, pkg); | |
2177 return ie->semantic(sc); | |
2178 } | |
2179 Module *mod = s->isModule(); | |
2180 if (mod) | |
2181 { | |
2182 ScopeExp *ie; | |
2183 | |
2184 ie = new ScopeExp(loc, mod); | |
2185 return ie->semantic(sc); | |
2186 } | |
2187 | |
2188 t = s->getType(); | |
2189 if (t) | |
2190 { | |
2191 return new TypeExp(loc, t); | |
2192 } | |
2193 | |
2194 TupleDeclaration *tup = s->isTupleDeclaration(); | |
2195 if (tup) | |
2196 { | |
2197 e = new TupleExp(loc, tup); | |
2198 e = e->semantic(sc); | |
2199 return e; | |
2200 } | |
2201 | |
2202 TemplateInstance *ti = s->isTemplateInstance(); | |
2203 if (ti && !global.errors) | |
2204 { if (!ti->semanticdone) | |
2205 ti->semantic(sc); | |
2206 s = ti->inst->toAlias(); | |
2207 if (!s->isTemplateInstance()) | |
2208 goto Lagain; | |
2209 e = new ScopeExp(loc, ti); | |
2210 e = e->semantic(sc); | |
2211 return e; | |
2212 } | |
2213 | |
2214 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
2215 if (td) | |
2216 { | |
2217 e = new TemplateExp(loc, td); | |
2218 e = e->semantic(sc); | |
2219 return e; | |
2220 } | |
2221 | |
2222 Lerr: | |
2223 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
2224 type = Type::terror; | |
2225 return this; | |
2226 } | |
2227 | |
2228 char *DsymbolExp::toChars() | |
2229 { | |
2230 return s->toChars(); | |
2231 } | |
2232 | |
2233 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2234 { | |
2235 buf->writestring(s->toChars()); | |
2236 } | |
2237 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2238 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2239 int DsymbolExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2240 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2241 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2242 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2243 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2244 |
159 | 2245 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) |
2246 { | |
2247 #if 0 | |
2248 tym = tybasic(e1->ET->Tty); | |
2249 if (!(tyscalar(tym) || | |
2250 tym == TYstruct || | |
2251 tym == TYarray && e->Eoper == TOKaddr)) | |
2252 synerr(EM_lvalue); // lvalue expected | |
2253 #endif | |
2254 return this; | |
2255 } | |
2256 | |
2257 /******************************** ThisExp **************************/ | |
2258 | |
2259 ThisExp::ThisExp(Loc loc) | |
2260 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
2261 { | |
2262 var = NULL; | |
2263 } | |
2264 | |
2265 Expression *ThisExp::semantic(Scope *sc) | |
2266 { FuncDeclaration *fd; | |
2267 FuncDeclaration *fdthis; | |
2268 int nested = 0; | |
2269 | |
2270 #if LOGSEMANTIC | |
2271 printf("ThisExp::semantic()\n"); | |
2272 #endif | |
2273 if (type) | |
2274 { //assert(global.errors || var); | |
2275 return this; | |
2276 } | |
2277 | |
2278 /* Special case for typeof(this) and typeof(super) since both | |
2279 * should work even if they are not inside a non-static member function | |
2280 */ | |
2281 if (sc->intypeof) | |
2282 { | |
2283 // Find enclosing struct or class | |
2284 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2285 { | |
2286 ClassDeclaration *cd; | |
2287 StructDeclaration *sd; | |
2288 | |
2289 if (!s) | |
2290 { | |
2291 error("%s is not in a struct or class scope", toChars()); | |
2292 goto Lerr; | |
2293 } | |
2294 cd = s->isClassDeclaration(); | |
2295 if (cd) | |
2296 { | |
2297 type = cd->type; | |
2298 return this; | |
2299 } | |
2300 sd = s->isStructDeclaration(); | |
2301 if (sd) | |
2302 { | |
2303 type = sd->type->pointerTo(); | |
2304 return this; | |
2305 } | |
2306 } | |
2307 } | |
2308 | |
2309 fdthis = sc->parent->isFuncDeclaration(); | |
2310 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
2311 if (!fd) | |
2312 goto Lerr; | |
2313 | |
2314 assert(fd->vthis); | |
2315 var = fd->vthis; | |
2316 assert(var->parent); | |
2317 type = var->type; | |
2318 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2319 #if 0 | |
2320 if (fd != fdthis) // if nested | |
2321 { | |
2322 fdthis->getLevel(loc, fd); | |
2323 fd->vthis->nestedref = 1; | |
2324 fd->nestedFrameRef = 1; | |
2325 } | |
2326 #endif | |
336 | 2327 if (!sc->intypeof) |
2328 sc->callSuper |= CSXthis; | |
159 | 2329 return this; |
2330 | |
2331 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
|
2332 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
|
2333 type = Type::terror; |
159 | 2334 return this; |
2335 } | |
2336 | |
2337 int ThisExp::isBool(int result) | |
2338 { | |
2339 return result ? TRUE : FALSE; | |
2340 } | |
2341 | |
2342 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2343 { | |
2344 buf->writestring("this"); | |
2345 } | |
2346 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2347 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2348 int ThisExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2349 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2350 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2351 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2352 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2353 |
159 | 2354 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) |
2355 { | |
2356 return this; | |
2357 } | |
2358 | |
2359 /******************************** SuperExp **************************/ | |
2360 | |
2361 SuperExp::SuperExp(Loc loc) | |
2362 : ThisExp(loc) | |
2363 { | |
2364 op = TOKsuper; | |
2365 } | |
2366 | |
2367 Expression *SuperExp::semantic(Scope *sc) | |
2368 { FuncDeclaration *fd; | |
2369 FuncDeclaration *fdthis; | |
2370 ClassDeclaration *cd; | |
2371 Dsymbol *s; | |
2372 | |
2373 #if LOGSEMANTIC | |
2374 printf("SuperExp::semantic('%s')\n", toChars()); | |
2375 #endif | |
2376 if (type) | |
2377 return this; | |
2378 | |
2379 /* Special case for typeof(this) and typeof(super) since both | |
2380 * should work even if they are not inside a non-static member function | |
2381 */ | |
2382 if (sc->intypeof) | |
2383 { | |
2384 // Find enclosing class | |
2385 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2386 { | |
2387 ClassDeclaration *cd; | |
2388 | |
2389 if (!s) | |
2390 { | |
2391 error("%s is not in a class scope", toChars()); | |
2392 goto Lerr; | |
2393 } | |
2394 cd = s->isClassDeclaration(); | |
2395 if (cd) | |
2396 { | |
2397 cd = cd->baseClass; | |
2398 if (!cd) | |
2399 { error("class %s has no 'super'", s->toChars()); | |
2400 goto Lerr; | |
2401 } | |
2402 type = cd->type; | |
2403 return this; | |
2404 } | |
2405 } | |
2406 } | |
2407 | |
2408 fdthis = sc->parent->isFuncDeclaration(); | |
2409 fd = hasThis(sc); | |
2410 if (!fd) | |
2411 goto Lerr; | |
2412 assert(fd->vthis); | |
2413 var = fd->vthis; | |
2414 assert(var->parent); | |
2415 | |
2416 s = fd->toParent(); | |
2417 while (s && s->isTemplateInstance()) | |
2418 s = s->toParent(); | |
2419 assert(s); | |
2420 cd = s->isClassDeclaration(); | |
2421 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2422 if (!cd) | |
2423 goto Lerr; | |
2424 if (!cd->baseClass) | |
2425 { | |
2426 error("no base class for %s", cd->toChars()); | |
2427 type = fd->vthis->type; | |
2428 } | |
2429 else | |
2430 { | |
2431 type = cd->baseClass->type; | |
2432 } | |
2433 | |
2434 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2435 #if 0 | |
2436 if (fd != fdthis) | |
2437 { | |
2438 fdthis->getLevel(loc, fd); | |
2439 fd->vthis->nestedref = 1; | |
2440 fd->nestedFrameRef = 1; | |
2441 } | |
2442 #endif | |
2443 | |
336 | 2444 if (!sc->intypeof) |
2445 sc->callSuper |= CSXsuper; | |
159 | 2446 return this; |
2447 | |
2448 | |
2449 Lerr: | |
2450 error("'super' is only allowed in non-static class member functions"); | |
2451 type = Type::tint32; | |
2452 return this; | |
2453 } | |
2454 | |
2455 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2456 { | |
2457 buf->writestring("super"); | |
2458 } | |
2459 | |
2460 | |
2461 /******************************** NullExp **************************/ | |
2462 | |
2463 NullExp::NullExp(Loc loc) | |
2464 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2465 { | |
2466 committed = 0; | |
2467 } | |
2468 | |
2469 Expression *NullExp::semantic(Scope *sc) | |
2470 { | |
2471 #if LOGSEMANTIC | |
2472 printf("NullExp::semantic('%s')\n", toChars()); | |
2473 #endif | |
2474 // NULL is the same as (void *)0 | |
2475 if (!type) | |
2476 type = Type::tvoid->pointerTo(); | |
2477 return this; | |
2478 } | |
2479 | |
2480 int NullExp::isBool(int result) | |
2481 { | |
2482 return result ? FALSE : TRUE; | |
2483 } | |
2484 | |
2485 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2486 { | |
2487 buf->writestring("null"); | |
2488 } | |
2489 | |
2490 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2491 { | |
2492 buf->writeByte('n'); | |
2493 } | |
2494 | |
2495 /******************************** StringExp **************************/ | |
2496 | |
2497 StringExp::StringExp(Loc loc, char *string) | |
2498 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2499 { | |
2500 this->string = string; | |
2501 this->len = strlen(string); | |
2502 this->sz = 1; | |
2503 this->committed = 0; | |
2504 this->postfix = 0; | |
2505 } | |
2506 | |
2507 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2508 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2509 { | |
2510 this->string = string; | |
2511 this->len = len; | |
2512 this->sz = 1; | |
2513 this->committed = 0; | |
2514 this->postfix = 0; | |
2515 } | |
2516 | |
2517 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2518 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2519 { | |
2520 this->string = string; | |
2521 this->len = len; | |
2522 this->sz = 1; | |
2523 this->committed = 0; | |
2524 this->postfix = postfix; | |
2525 } | |
2526 | |
2527 #if 0 | |
2528 Expression *StringExp::syntaxCopy() | |
2529 { | |
2530 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2531 return copy(); | |
2532 } | |
2533 #endif | |
2534 | |
2535 int StringExp::equals(Object *o) | |
2536 { | |
2537 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2538 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2539 { Expression *e = (Expression *)o; | |
2540 | |
2541 if (e->op == TOKstring) | |
2542 { | |
2543 return compare(o) == 0; | |
2544 } | |
2545 } | |
2546 return FALSE; | |
2547 } | |
2548 | |
2549 char *StringExp::toChars() | |
2550 { | |
2551 OutBuffer buf; | |
2552 HdrGenState hgs; | |
2553 char *p; | |
2554 | |
2555 memset(&hgs, 0, sizeof(hgs)); | |
2556 toCBuffer(&buf, &hgs); | |
2557 buf.writeByte(0); | |
2558 p = (char *)buf.data; | |
2559 buf.data = NULL; | |
2560 return p; | |
2561 } | |
2562 | |
2563 Expression *StringExp::semantic(Scope *sc) | |
2564 { | |
2565 #if LOGSEMANTIC | |
2566 printf("StringExp::semantic() %s\n", toChars()); | |
2567 #endif | |
2568 if (!type) | |
2569 { OutBuffer buffer; | |
2570 size_t newlen = 0; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2571 const char *p; |
159 | 2572 size_t u; |
2573 unsigned c; | |
2574 | |
2575 switch (postfix) | |
2576 { | |
2577 case 'd': | |
2578 for (u = 0; u < len;) | |
2579 { | |
2580 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2581 if (p) | |
2582 { error("%s", p); | |
2583 break; | |
2584 } | |
2585 else | |
2586 { buffer.write4(c); | |
2587 newlen++; | |
2588 } | |
2589 } | |
2590 buffer.write4(0); | |
2591 string = buffer.extractData(); | |
2592 len = newlen; | |
2593 sz = 4; | |
2594 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2595 committed = 1; | |
2596 break; | |
2597 | |
2598 case 'w': | |
2599 for (u = 0; u < len;) | |
2600 { | |
2601 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2602 if (p) | |
2603 { error("%s", p); | |
2604 break; | |
2605 } | |
2606 else | |
2607 { buffer.writeUTF16(c); | |
2608 newlen++; | |
2609 if (c >= 0x10000) | |
2610 newlen++; | |
2611 } | |
2612 } | |
2613 buffer.writeUTF16(0); | |
2614 string = buffer.extractData(); | |
2615 len = newlen; | |
2616 sz = 2; | |
2617 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2618 committed = 1; | |
2619 break; | |
2620 | |
2621 case 'c': | |
2622 committed = 1; | |
2623 default: | |
2624 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2625 break; | |
2626 } | |
2627 type = type->semantic(loc, sc); | |
2628 } | |
2629 return this; | |
2630 } | |
2631 | |
2632 /**************************************** | |
2633 * Convert string to char[]. | |
2634 */ | |
2635 | |
2636 StringExp *StringExp::toUTF8(Scope *sc) | |
2637 { | |
2638 if (sz != 1) | |
2639 { // Convert to UTF-8 string | |
2640 committed = 0; | |
2641 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2642 e = e->optimize(WANTvalue); | |
2643 assert(e->op == TOKstring); | |
2644 StringExp *se = (StringExp *)e; | |
2645 assert(se->sz == 1); | |
2646 return se; | |
2647 } | |
2648 return this; | |
2649 } | |
2650 | |
2651 int StringExp::compare(Object *obj) | |
2652 { | |
2653 // Used to sort case statement expressions so we can do an efficient lookup | |
2654 StringExp *se2 = (StringExp *)(obj); | |
2655 | |
2656 // This is a kludge so isExpression() in template.c will return 5 | |
2657 // for StringExp's. | |
2658 if (!se2) | |
2659 return 5; | |
2660 | |
2661 assert(se2->op == TOKstring); | |
2662 | |
2663 int len1 = len; | |
2664 int len2 = se2->len; | |
2665 | |
2666 if (len1 == len2) | |
2667 { | |
2668 switch (sz) | |
2669 { | |
2670 case 1: | |
2671 return strcmp((char *)string, (char *)se2->string); | |
2672 | |
2673 case 2: | |
2674 { unsigned u; | |
2675 d_wchar *s1 = (d_wchar *)string; | |
2676 d_wchar *s2 = (d_wchar *)se2->string; | |
2677 | |
2678 for (u = 0; u < len; u++) | |
2679 { | |
2680 if (s1[u] != s2[u]) | |
2681 return s1[u] - s2[u]; | |
2682 } | |
2683 } | |
2684 | |
2685 case 4: | |
2686 { unsigned u; | |
2687 d_dchar *s1 = (d_dchar *)string; | |
2688 d_dchar *s2 = (d_dchar *)se2->string; | |
2689 | |
2690 for (u = 0; u < len; u++) | |
2691 { | |
2692 if (s1[u] != s2[u]) | |
2693 return s1[u] - s2[u]; | |
2694 } | |
2695 } | |
2696 break; | |
2697 | |
2698 default: | |
2699 assert(0); | |
2700 } | |
2701 } | |
2702 return len1 - len2; | |
2703 } | |
2704 | |
2705 int StringExp::isBool(int result) | |
2706 { | |
2707 return result ? TRUE : FALSE; | |
2708 } | |
2709 | |
2710 unsigned StringExp::charAt(size_t i) | |
2711 { unsigned value; | |
2712 | |
2713 switch (sz) | |
2714 { | |
2715 case 1: | |
2716 value = ((unsigned char *)string)[i]; | |
2717 break; | |
2718 | |
2719 case 2: | |
2720 value = ((unsigned short *)string)[i]; | |
2721 break; | |
2722 | |
2723 case 4: | |
2724 value = ((unsigned int *)string)[i]; | |
2725 break; | |
2726 | |
2727 default: | |
2728 assert(0); | |
2729 break; | |
2730 } | |
2731 return value; | |
2732 } | |
2733 | |
2734 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2735 { | |
2736 buf->writeByte('"'); | |
2737 for (size_t i = 0; i < len; i++) | |
2738 { unsigned c = charAt(i); | |
2739 | |
2740 switch (c) | |
2741 { | |
2742 case '"': | |
2743 case '\\': | |
2744 if (!hgs->console) | |
2745 buf->writeByte('\\'); | |
2746 default: | |
2747 if (c <= 0xFF) | |
2748 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2749 buf->writeByte(c); | |
2750 else | |
2751 buf->printf("\\x%02x", c); | |
2752 } | |
2753 else if (c <= 0xFFFF) | |
2754 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2755 else | |
2756 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2757 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2758 break; | |
2759 } | |
2760 } | |
2761 buf->writeByte('"'); | |
2762 if (postfix) | |
2763 buf->writeByte(postfix); | |
2764 } | |
2765 | |
2766 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2767 { char m; | |
2768 OutBuffer tmp; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2769 const char *p; |
159 | 2770 unsigned c; |
2771 size_t u; | |
2772 unsigned char *q; | |
2773 unsigned qlen; | |
2774 | |
2775 /* Write string in UTF-8 format | |
2776 */ | |
2777 switch (sz) | |
2778 { case 1: | |
2779 m = 'a'; | |
2780 q = (unsigned char *)string; | |
2781 qlen = len; | |
2782 break; | |
2783 case 2: | |
2784 m = 'w'; | |
2785 for (u = 0; u < len; ) | |
2786 { | |
2787 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2788 if (p) | |
2789 error("%s", p); | |
2790 else | |
2791 tmp.writeUTF8(c); | |
2792 } | |
2793 q = tmp.data; | |
2794 qlen = tmp.offset; | |
2795 break; | |
2796 case 4: | |
2797 m = 'd'; | |
2798 for (u = 0; u < len; u++) | |
2799 { | |
2800 c = ((unsigned *)string)[u]; | |
2801 if (!utf_isValidDchar(c)) | |
2802 error("invalid UCS-32 char \\U%08x", c); | |
2803 else | |
2804 tmp.writeUTF8(c); | |
2805 } | |
2806 q = tmp.data; | |
2807 qlen = tmp.offset; | |
2808 break; | |
2809 default: | |
2810 assert(0); | |
2811 } | |
2812 buf->writeByte(m); | |
2813 buf->printf("%d_", qlen); | |
2814 for (size_t i = 0; i < qlen; i++) | |
2815 buf->printf("%02x", q[i]); | |
2816 } | |
2817 | |
2818 /************************ ArrayLiteralExp ************************************/ | |
2819 | |
2820 // [ e1, e2, e3, ... ] | |
2821 | |
2822 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2823 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2824 { | |
2825 this->elements = elements; | |
2826 } | |
2827 | |
2828 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2829 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2830 { | |
2831 elements = new Expressions; | |
2832 elements->push(e); | |
2833 } | |
2834 | |
2835 Expression *ArrayLiteralExp::syntaxCopy() | |
2836 { | |
2837 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2838 } | |
2839 | |
2840 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2841 { Expression *e; | |
2842 Type *t0 = NULL; | |
2843 | |
2844 #if LOGSEMANTIC | |
2845 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2846 #endif | |
2847 if (type) | |
2848 return this; | |
2849 | |
2850 // Run semantic() on each element | |
2851 for (int i = 0; i < elements->dim; i++) | |
2852 { e = (Expression *)elements->data[i]; | |
2853 e = e->semantic(sc); | |
2854 elements->data[i] = (void *)e; | |
2855 } | |
2856 expandTuples(elements); | |
2857 for (int i = 0; i < elements->dim; i++) | |
2858 { e = (Expression *)elements->data[i]; | |
2859 | |
2860 if (!e->type) | |
2861 error("%s has no value", e->toChars()); | |
2862 e = resolveProperties(sc, e); | |
2863 | |
2864 unsigned char committed = 1; | |
2865 if (e->op == TOKstring) | |
2866 committed = ((StringExp *)e)->committed; | |
2867 | |
2868 if (!t0) | |
2869 { t0 = e->type; | |
2870 // Convert any static arrays to dynamic arrays | |
2871 if (t0->ty == Tsarray) | |
2872 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2873 t0 = ((TypeSArray *)t0)->next->arrayOf(); |
159 | 2874 e = e->implicitCastTo(sc, t0); |
2875 } | |
2876 } | |
2877 else | |
2878 e = e->implicitCastTo(sc, t0); | |
2879 if (!committed && e->op == TOKstring) | |
2880 { StringExp *se = (StringExp *)e; | |
2881 se->committed = 0; | |
2882 } | |
2883 elements->data[i] = (void *)e; | |
2884 } | |
2885 | |
2886 if (!t0) | |
2887 t0 = Type::tvoid; | |
2888 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
2889 type = type->semantic(loc, sc); | |
2890 return this; | |
2891 } | |
2892 | |
2893 int ArrayLiteralExp::checkSideEffect(int flag) | |
2894 { int f = 0; | |
2895 | |
2896 for (size_t i = 0; i < elements->dim; i++) | |
2897 { Expression *e = (Expression *)elements->data[i]; | |
2898 | |
2899 f |= e->checkSideEffect(2); | |
2900 } | |
2901 if (flag == 0 && f == 0) | |
2902 Expression::checkSideEffect(0); | |
2903 return f; | |
2904 } | |
2905 | |
2906 int ArrayLiteralExp::isBool(int result) | |
2907 { | |
2908 size_t dim = elements ? elements->dim : 0; | |
2909 return result ? (dim != 0) : (dim == 0); | |
2910 } | |
2911 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2912 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2913 int ArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2914 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2915 return 1; // because it can fail allocating memory |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2916 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2917 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2918 |
159 | 2919 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2920 { | |
2921 buf->writeByte('['); | |
2922 argsToCBuffer(buf, elements, hgs); | |
2923 buf->writeByte(']'); | |
2924 } | |
2925 | |
2926 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2927 { | |
2928 size_t dim = elements ? elements->dim : 0; | |
2929 buf->printf("A%u", dim); | |
2930 for (size_t i = 0; i < dim; i++) | |
2931 { Expression *e = (Expression *)elements->data[i]; | |
2932 e->toMangleBuffer(buf); | |
2933 } | |
2934 } | |
2935 | |
2936 /************************ AssocArrayLiteralExp ************************************/ | |
2937 | |
2938 // [ key0 : value0, key1 : value1, ... ] | |
2939 | |
2940 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
2941 Expressions *keys, Expressions *values) | |
2942 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
2943 { | |
2944 assert(keys->dim == values->dim); | |
2945 this->keys = keys; | |
2946 this->values = values; | |
2947 } | |
2948 | |
2949 Expression *AssocArrayLiteralExp::syntaxCopy() | |
2950 { | |
2951 return new AssocArrayLiteralExp(loc, | |
2952 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
2953 } | |
2954 | |
2955 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
2956 { Expression *e; | |
2957 Type *tkey = NULL; | |
2958 Type *tvalue = NULL; | |
2959 | |
2960 #if LOGSEMANTIC | |
2961 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
2962 #endif | |
2963 | |
2964 // Run semantic() on each element | |
2965 for (size_t i = 0; i < keys->dim; i++) | |
2966 { Expression *key = (Expression *)keys->data[i]; | |
2967 Expression *value = (Expression *)values->data[i]; | |
2968 | |
2969 key = key->semantic(sc); | |
2970 value = value->semantic(sc); | |
2971 | |
2972 keys->data[i] = (void *)key; | |
2973 values->data[i] = (void *)value; | |
2974 } | |
2975 expandTuples(keys); | |
2976 expandTuples(values); | |
2977 if (keys->dim != values->dim) | |
2978 { | |
2979 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
2980 keys->setDim(0); | |
2981 values->setDim(0); | |
2982 } | |
2983 for (size_t i = 0; i < keys->dim; i++) | |
2984 { Expression *key = (Expression *)keys->data[i]; | |
2985 Expression *value = (Expression *)values->data[i]; | |
2986 | |
2987 if (!key->type) | |
2988 error("%s has no value", key->toChars()); | |
2989 if (!value->type) | |
2990 error("%s has no value", value->toChars()); | |
2991 key = resolveProperties(sc, key); | |
2992 value = resolveProperties(sc, value); | |
2993 | |
2994 if (!tkey) | |
2995 tkey = key->type; | |
2996 else | |
2997 key = key->implicitCastTo(sc, tkey); | |
2998 keys->data[i] = (void *)key; | |
2999 | |
3000 if (!tvalue) | |
3001 tvalue = value->type; | |
3002 else | |
3003 value = value->implicitCastTo(sc, tvalue); | |
3004 values->data[i] = (void *)value; | |
3005 } | |
3006 | |
3007 if (!tkey) | |
3008 tkey = Type::tvoid; | |
3009 if (!tvalue) | |
3010 tvalue = Type::tvoid; | |
3011 type = new TypeAArray(tvalue, tkey); | |
3012 type = type->semantic(loc, sc); | |
3013 return this; | |
3014 } | |
3015 | |
3016 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
3017 { int f = 0; | |
3018 | |
3019 for (size_t i = 0; i < keys->dim; i++) | |
3020 { Expression *key = (Expression *)keys->data[i]; | |
3021 Expression *value = (Expression *)values->data[i]; | |
3022 | |
3023 f |= key->checkSideEffect(2); | |
3024 f |= value->checkSideEffect(2); | |
3025 } | |
3026 if (flag == 0 && f == 0) | |
3027 Expression::checkSideEffect(0); | |
3028 return f; | |
3029 } | |
3030 | |
3031 int AssocArrayLiteralExp::isBool(int result) | |
3032 { | |
3033 size_t dim = keys->dim; | |
3034 return result ? (dim != 0) : (dim == 0); | |
3035 } | |
3036 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3037 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3038 int AssocArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3039 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3040 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3041 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3042 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3043 |
159 | 3044 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3045 { | |
3046 buf->writeByte('['); | |
3047 for (size_t i = 0; i < keys->dim; i++) | |
3048 { Expression *key = (Expression *)keys->data[i]; | |
3049 Expression *value = (Expression *)values->data[i]; | |
3050 | |
3051 if (i) | |
3052 buf->writeByte(','); | |
3053 expToCBuffer(buf, hgs, key, PREC_assign); | |
3054 buf->writeByte(':'); | |
3055 expToCBuffer(buf, hgs, value, PREC_assign); | |
3056 } | |
3057 buf->writeByte(']'); | |
3058 } | |
3059 | |
3060 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3061 { | |
3062 size_t dim = keys->dim; | |
3063 buf->printf("A%u", dim); | |
3064 for (size_t i = 0; i < dim; i++) | |
3065 { Expression *key = (Expression *)keys->data[i]; | |
3066 Expression *value = (Expression *)values->data[i]; | |
3067 | |
3068 key->toMangleBuffer(buf); | |
3069 value->toMangleBuffer(buf); | |
3070 } | |
3071 } | |
3072 | |
3073 /************************ StructLiteralExp ************************************/ | |
3074 | |
3075 // sd( e1, e2, e3, ... ) | |
3076 | |
3077 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
3078 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
3079 { | |
3080 this->sd = sd; | |
3081 this->elements = elements; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
3082 #if IN_DMD |
159 | 3083 this->sym = NULL; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
3084 #endif |
159 | 3085 this->soffset = 0; |
3086 this->fillHoles = 1; | |
3087 } | |
3088 | |
3089 Expression *StructLiteralExp::syntaxCopy() | |
3090 { | |
3091 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
3092 } | |
3093 | |
3094 Expression *StructLiteralExp::semantic(Scope *sc) | |
3095 { Expression *e; | |
3096 | |
3097 #if LOGSEMANTIC | |
3098 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
3099 #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
|
3100 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
|
3101 return this; |
159 | 3102 |
3103 // Run semantic() on each element | |
3104 for (size_t i = 0; i < elements->dim; i++) | |
3105 { e = (Expression *)elements->data[i]; | |
3106 if (!e) | |
3107 continue; | |
3108 e = e->semantic(sc); | |
3109 elements->data[i] = (void *)e; | |
3110 } | |
3111 expandTuples(elements); | |
3112 size_t offset = 0; | |
3113 for (size_t i = 0; i < elements->dim; i++) | |
3114 { e = (Expression *)elements->data[i]; | |
3115 if (!e) | |
3116 continue; | |
3117 | |
3118 if (!e->type) | |
3119 error("%s has no value", e->toChars()); | |
3120 e = resolveProperties(sc, e); | |
3121 if (i >= sd->fields.dim) | |
3122 { error("more initializers than fields of %s", sd->toChars()); | |
3123 break; | |
3124 } | |
3125 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3126 VarDeclaration *v = s->isVarDeclaration(); | |
3127 assert(v); | |
3128 if (v->offset < offset) | |
3129 error("overlapping initialization for %s", v->toChars()); | |
3130 offset = v->offset + v->type->size(); | |
3131 | |
3132 Type *telem = v->type; | |
3133 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
3134 { /* Static array initialization, as in: | |
3135 * T[3][5] = e; | |
3136 */ | |
3137 telem = telem->toBasetype()->nextOf(); | |
3138 } | |
3139 | |
3140 e = e->implicitCastTo(sc, telem); | |
3141 | |
3142 elements->data[i] = (void *)e; | |
3143 } | |
3144 | |
3145 /* Fill out remainder of elements[] with default initializers for fields[] | |
3146 */ | |
3147 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
3148 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3149 VarDeclaration *v = s->isVarDeclaration(); | |
3150 assert(v); | |
3151 | |
3152 if (v->offset < offset) | |
3153 { e = NULL; | |
3154 sd->hasUnions = 1; | |
3155 } | |
3156 else | |
3157 { | |
3158 if (v->init) | |
3159 { e = v->init->toExpression(); | |
3160 if (!e) | |
3161 error("cannot make expression out of initializer for %s", v->toChars()); | |
3162 } | |
3163 else | |
3164 { e = v->type->defaultInit(); | |
3165 e->loc = loc; | |
3166 } | |
3167 offset = v->offset + v->type->size(); | |
3168 } | |
3169 elements->push(e); | |
3170 } | |
3171 | |
3172 type = sd->type; | |
3173 return this; | |
3174 } | |
3175 | |
3176 /************************************** | |
3177 * Gets expression at offset of type. | |
3178 * Returns NULL if not found. | |
3179 */ | |
3180 | |
3181 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
|
3182 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3183 //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
|
3184 // /*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
|
3185 Expression *e = NULL; |
159 | 3186 int i = getFieldIndex(type, offset); |
3187 | |
3188 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
|
3189 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3190 //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
|
3191 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
|
3192 e = (Expression *)elements->data[i]; |
159 | 3193 if (e) |
3194 { | |
3195 e = e->copy(); | |
3196 e->type = type; | |
3197 } | |
3198 } | |
3199 return e; | |
3200 } | |
3201 | |
3202 /************************************ | |
3203 * Get index of field. | |
3204 * Returns -1 if not found. | |
3205 */ | |
3206 | |
3207 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
3208 { | |
3209 /* Find which field offset is by looking at the field offsets | |
3210 */ | |
3211 for (size_t i = 0; i < sd->fields.dim; i++) | |
3212 { | |
3213 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3214 VarDeclaration *v = s->isVarDeclaration(); | |
3215 assert(v); | |
3216 | |
3217 if (offset == v->offset && | |
3218 type->size() == v->type->size()) | |
3219 { Expression *e = (Expression *)elements->data[i]; | |
3220 if (e) | |
3221 { | |
3222 return i; | |
3223 } | |
3224 break; | |
3225 } | |
3226 } | |
3227 return -1; | |
3228 } | |
3229 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3230 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3231 int StructLiteralExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3232 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3233 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3234 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3235 #endif |
159 | 3236 |
1168
ab186e535e72
A different fix to #218 and DMD2682 that does not lead to constant folding regressions.
Christian Kamm <kamm incasoftware de>
parents:
1151
diff
changeset
|
3237 /* |
ab186e535e72
A different fix to #218 and DMD2682 that does not lead to constant folding regressions.
Christian Kamm <kamm incasoftware de>
parents:
1151
diff
changeset
|
3238 Removed in LDC. See declaration. |
159 | 3239 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) |
3240 { | |
3241 return this; | |
3242 } | |
1168
ab186e535e72
A different fix to #218 and DMD2682 that does not lead to constant folding regressions.
Christian Kamm <kamm incasoftware de>
parents:
1151
diff
changeset
|
3243 */ |
159 | 3244 |
3245 | |
3246 int StructLiteralExp::checkSideEffect(int flag) | |
3247 { int f = 0; | |
3248 | |
3249 for (size_t i = 0; i < elements->dim; i++) | |
3250 { Expression *e = (Expression *)elements->data[i]; | |
3251 if (!e) | |
3252 continue; | |
3253 | |
3254 f |= e->checkSideEffect(2); | |
3255 } | |
3256 if (flag == 0 && f == 0) | |
3257 Expression::checkSideEffect(0); | |
3258 return f; | |
3259 } | |
3260 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3261 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3262 int StructLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3263 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3264 return arrayExpressionCanThrow(elements); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3265 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3266 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3267 |
159 | 3268 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3269 { | |
3270 buf->writestring(sd->toChars()); | |
3271 buf->writeByte('('); | |
3272 argsToCBuffer(buf, elements, hgs); | |
3273 buf->writeByte(')'); | |
3274 } | |
3275 | |
3276 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3277 { | |
3278 size_t dim = elements ? elements->dim : 0; | |
3279 buf->printf("S%u", dim); | |
3280 for (size_t i = 0; i < dim; i++) | |
3281 { Expression *e = (Expression *)elements->data[i]; | |
3282 if (e) | |
3283 e->toMangleBuffer(buf); | |
3284 else | |
3285 buf->writeByte('v'); // 'v' for void | |
3286 } | |
3287 } | |
3288 | |
3289 /************************ TypeDotIdExp ************************************/ | |
3290 | |
3291 /* Things like: | |
3292 * int.size | |
3293 * foo.size | |
3294 * (foo).size | |
3295 * cast(foo).size | |
3296 */ | |
3297 | |
3298 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident) | |
3299 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp)) | |
3300 { | |
3301 this->type = type; | |
3302 this->ident = ident; | |
3303 } | |
3304 | |
3305 Expression *TypeDotIdExp::syntaxCopy() | |
3306 { | |
3307 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident); | |
3308 return te; | |
3309 } | |
3310 | |
3311 Expression *TypeDotIdExp::semantic(Scope *sc) | |
3312 { Expression *e; | |
3313 | |
3314 #if LOGSEMANTIC | |
3315 printf("TypeDotIdExp::semantic()\n"); | |
3316 #endif | |
3317 e = new DotIdExp(loc, new TypeExp(loc, type), ident); | |
3318 e = e->semantic(sc); | |
3319 return e; | |
3320 } | |
3321 | |
3322 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3323 { | |
3324 buf->writeByte('('); | |
3325 type->toCBuffer(buf, NULL, hgs); | |
3326 buf->writeByte(')'); | |
3327 buf->writeByte('.'); | |
3328 buf->writestring(ident->toChars()); | |
3329 } | |
3330 | |
3331 /************************************************************/ | |
3332 | |
3333 // Mainly just a placeholder | |
3334 | |
3335 TypeExp::TypeExp(Loc loc, Type *type) | |
3336 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
3337 { | |
3338 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
3339 this->type = type; | |
3340 } | |
3341 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3342 Expression *TypeExp::syntaxCopy() |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3343 { |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3344 //printf("TypeExp::syntaxCopy()\n"); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3345 return new TypeExp(loc, type->syntaxCopy()); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3346 } |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3347 |
159 | 3348 Expression *TypeExp::semantic(Scope *sc) |
3349 { | |
3350 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
3351 type = type->semantic(loc, sc); | |
3352 return this; | |
3353 } | |
3354 | |
3355 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3356 { | |
3357 type->toCBuffer(buf, NULL, hgs); | |
3358 } | |
3359 | |
3360 /************************************************************/ | |
3361 | |
3362 // Mainly just a placeholder | |
3363 | |
3364 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
3365 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
3366 { | |
3367 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
3368 //static int count; if (++count == 38) *(char*)0=0; | |
3369 this->sds = pkg; | |
3370 } | |
3371 | |
3372 Expression *ScopeExp::syntaxCopy() | |
3373 { | |
3374 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
3375 return se; | |
3376 } | |
3377 | |
3378 Expression *ScopeExp::semantic(Scope *sc) | |
3379 { | |
3380 TemplateInstance *ti; | |
3381 ScopeDsymbol *sds2; | |
3382 | |
3383 #if LOGSEMANTIC | |
3384 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
3385 #endif | |
3386 Lagain: | |
3387 ti = sds->isTemplateInstance(); | |
3388 if (ti && !global.errors) | |
3389 { Dsymbol *s; | |
3390 if (!ti->semanticdone) | |
3391 ti->semantic(sc); | |
3392 s = ti->inst->toAlias(); | |
3393 sds2 = s->isScopeDsymbol(); | |
3394 if (!sds2) | |
3395 { Expression *e; | |
3396 | |
3397 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
3398 if (ti->withsym) | |
3399 { | |
3400 // Same as wthis.s | |
3401 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3402 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3403 } | |
3404 else | |
3405 e = new DsymbolExp(loc, s); | |
3406 e = e->semantic(sc); | |
3407 //printf("-1ScopeExp::semantic()\n"); | |
3408 return e; | |
3409 } | |
3410 if (sds2 != sds) | |
3411 { | |
3412 sds = sds2; | |
3413 goto Lagain; | |
3414 } | |
3415 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3416 } | |
3417 else | |
3418 { | |
3419 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3420 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3421 sds->semantic(sc); | |
3422 } | |
3423 type = Type::tvoid; | |
3424 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3425 return this; | |
3426 } | |
3427 | |
3428 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3429 { | |
3430 if (sds->isTemplateInstance()) | |
3431 { | |
3432 sds->toCBuffer(buf, hgs); | |
3433 } | |
3434 else | |
3435 { | |
3436 buf->writestring(sds->kind()); | |
3437 buf->writestring(" "); | |
3438 buf->writestring(sds->toChars()); | |
3439 } | |
3440 } | |
3441 | |
3442 /********************** TemplateExp **************************************/ | |
3443 | |
3444 // Mainly just a placeholder | |
3445 | |
3446 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3447 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3448 { | |
3449 //printf("TemplateExp(): %s\n", td->toChars()); | |
3450 this->td = td; | |
3451 } | |
3452 | |
3453 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3454 { | |
3455 buf->writestring(td->toChars()); | |
3456 } | |
3457 | |
3458 void TemplateExp::rvalue() | |
3459 { | |
3460 error("template %s has no value", toChars()); | |
3461 } | |
3462 | |
3463 /********************** NewExp **************************************/ | |
3464 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3465 /* thisexp.new(newargs) newtype(arguments) */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3466 |
159 | 3467 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, |
3468 Type *newtype, Expressions *arguments) | |
3469 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3470 { | |
3471 this->thisexp = thisexp; | |
3472 this->newargs = newargs; | |
3473 this->newtype = newtype; | |
3474 this->arguments = arguments; | |
3475 member = NULL; | |
3476 allocator = NULL; | |
3477 onstack = 0; | |
3478 } | |
3479 | |
3480 Expression *NewExp::syntaxCopy() | |
3481 { | |
3482 return new NewExp(loc, | |
3483 thisexp ? thisexp->syntaxCopy() : NULL, | |
3484 arraySyntaxCopy(newargs), | |
3485 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3486 } | |
3487 | |
3488 | |
3489 Expression *NewExp::semantic(Scope *sc) | |
3490 { int i; | |
3491 Type *tb; | |
3492 ClassDeclaration *cdthis = NULL; | |
3493 | |
3494 #if LOGSEMANTIC | |
3495 printf("NewExp::semantic() %s\n", toChars()); | |
3496 if (thisexp) | |
3497 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3498 printf("\tnewtype: %s\n", newtype->toChars()); | |
3499 #endif | |
3500 if (type) // if semantic() already run | |
3501 return this; | |
3502 | |
3503 Lagain: | |
3504 if (thisexp) | |
3505 { thisexp = thisexp->semantic(sc); | |
3506 cdthis = thisexp->type->isClassHandle(); | |
3507 if (cdthis) | |
3508 { | |
3509 sc = sc->push(cdthis); | |
3510 type = newtype->semantic(loc, sc); | |
3511 sc = sc->pop(); | |
3512 } | |
3513 else | |
3514 { | |
3515 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3516 type = newtype->semantic(loc, sc); | |
3517 } | |
3518 } | |
3519 else | |
3520 type = newtype->semantic(loc, sc); | |
3521 newtype = type; // in case type gets cast to something else | |
3522 tb = type->toBasetype(); | |
3523 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3524 | |
3525 arrayExpressionSemantic(newargs, sc); | |
3526 preFunctionArguments(loc, sc, newargs); | |
3527 arrayExpressionSemantic(arguments, sc); | |
3528 preFunctionArguments(loc, sc, arguments); | |
3529 | |
3530 if (thisexp && tb->ty != Tclass) | |
3531 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3532 | |
3533 if (tb->ty == Tclass) | |
3534 { TypeFunction *tf; | |
3535 | |
3536 TypeClass *tc = (TypeClass *)(tb); | |
3537 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3538 if (cd->isInterfaceDeclaration()) | |
3539 error("cannot create instance of interface %s", cd->toChars()); | |
3540 else if (cd->isAbstract()) | |
3541 { error("cannot create instance of abstract class %s", cd->toChars()); | |
3542 for (int i = 0; i < cd->vtbl.dim; i++) | |
3543 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration(); | |
3544 if (fd && fd->isAbstract()) | |
3545 error("function %s is abstract", fd->toChars()); | |
3546 } | |
3547 } | |
3548 checkDeprecated(sc, cd); | |
3549 if (cd->isNested()) | |
3550 { /* We need a 'this' pointer for the nested class. | |
3551 * Ensure we have the right one. | |
3552 */ | |
3553 Dsymbol *s = cd->toParent2(); | |
3554 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
|
3555 FuncDeclaration *fdn = s->isFuncDeclaration(); |
159 | 3556 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3557 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3558 if (cdn) |
3559 { | |
3560 if (!cdthis) | |
3561 { | |
3562 // Supply an implicit 'this' and try again | |
3563 thisexp = new ThisExp(loc); | |
3564 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3565 { if (!sp) | |
3566 { | |
3567 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3568 break; | |
3569 } | |
3570 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3571 if (!cdp) | |
3572 continue; | |
3573 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3574 break; | |
3575 // Add a '.outer' and try again | |
3576 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3577 } | |
3578 if (!global.errors) | |
3579 goto Lagain; | |
3580 } | |
3581 if (cdthis) | |
3582 { | |
3583 //printf("cdthis = %s\n", cdthis->toChars()); | |
3584 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3585 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3586 } | |
3587 #if 0 | |
3588 else | |
3589 { | |
3590 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3591 { | |
3592 if (!sf) | |
3593 { | |
3594 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3595 break; | |
3596 } | |
3597 printf("sf = %s\n", sf->toChars()); | |
3598 AggregateDeclaration *ad = sf->isThis(); | |
3599 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3600 break; | |
3601 } | |
3602 } | |
3603 #endif | |
3604 } | |
3605 else if (thisexp) | |
3606 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
|
3607 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
|
3608 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3609 // 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
|
3610 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
|
3611 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3612 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
|
3613 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3614 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
|
3615 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
|
3616 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3617 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
|
3618 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3619 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3620 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3621 |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3622 } |
159 | 3623 } |
3624 else if (thisexp) | |
3625 error("e.new is only for allocating nested classes"); | |
3626 | |
3627 FuncDeclaration *f = cd->ctor; | |
3628 if (f) | |
3629 { | |
3630 assert(f); | |
3631 f = f->overloadResolve(loc, arguments); | |
3632 checkDeprecated(sc, f); | |
3633 member = f->isCtorDeclaration(); | |
3634 assert(member); | |
3635 | |
3636 cd->accessCheck(loc, sc, member); | |
3637 | |
3638 tf = (TypeFunction *)f->type; | |
3639 type = tf->next; | |
3640 | |
3641 if (!arguments) | |
3642 arguments = new Expressions(); | |
3643 functionArguments(loc, sc, tf, arguments); | |
3644 } | |
3645 else | |
3646 { | |
3647 if (arguments && arguments->dim) | |
3648 error("no constructor for %s", cd->toChars()); | |
3649 } | |
3650 | |
3651 if (cd->aggNew) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3652 { |
891
ea82e2077f9d
Change custom allocator new size argument to size_t instead of uint. See #175.
Christian Kamm <kamm incasoftware de>
parents:
876
diff
changeset
|
3653 // Prepend the size_t size argument to newargs[] |
ea82e2077f9d
Change custom allocator new size argument to size_t instead of uint. See #175.
Christian Kamm <kamm incasoftware de>
parents:
876
diff
changeset
|
3654 Expression *e = new IntegerExp(loc, cd->size(loc), Type::tsize_t); |
159 | 3655 if (!newargs) |
3656 newargs = new Expressions(); | |
3657 newargs->shift(e); | |
3658 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3659 f = cd->aggNew->overloadResolve(loc, newargs); |
159 | 3660 allocator = f->isNewDeclaration(); |
3661 assert(allocator); | |
3662 | |
3663 tf = (TypeFunction *)f->type; | |
3664 functionArguments(loc, sc, tf, newargs); | |
3665 } | |
3666 else | |
3667 { | |
3668 if (newargs && newargs->dim) | |
3669 error("no allocator for %s", cd->toChars()); | |
3670 } | |
3671 } | |
3672 else if (tb->ty == Tstruct) | |
3673 { | |
3674 TypeStruct *ts = (TypeStruct *)tb; | |
3675 StructDeclaration *sd = ts->sym; | |
3676 FuncDeclaration *f = sd->aggNew; | |
3677 TypeFunction *tf; | |
3678 | |
3679 if (arguments && arguments->dim) | |
3680 error("no constructor for %s", type->toChars()); | |
3681 | |
3682 if (f) | |
3683 { | |
3684 Expression *e; | |
3685 | |
3686 // Prepend the uint size argument to newargs[] | |
3687 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3688 if (!newargs) | |
3689 newargs = new Expressions(); | |
3690 newargs->shift(e); | |
3691 | |
3692 f = f->overloadResolve(loc, newargs); | |
3693 allocator = f->isNewDeclaration(); | |
3694 assert(allocator); | |
3695 | |
3696 tf = (TypeFunction *)f->type; | |
3697 functionArguments(loc, sc, tf, newargs); | |
3698 | |
3699 e = new VarExp(loc, f); | |
3700 e = new CallExp(loc, e, newargs); | |
3701 e = e->semantic(sc); | |
3702 e->type = type->pointerTo(); | |
3703 return e; | |
3704 } | |
3705 | |
3706 type = type->pointerTo(); | |
3707 } | |
3708 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3709 { | |
3710 for (size_t i = 0; i < arguments->dim; i++) | |
3711 { | |
3712 if (tb->ty != Tarray) | |
3713 { error("too many arguments for array"); | |
3714 arguments->dim = i; | |
3715 break; | |
3716 } | |
3717 | |
3718 Expression *arg = (Expression *)arguments->data[i]; | |
3719 arg = resolveProperties(sc, arg); | |
3720 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3721 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3722 error("negative array index %s", arg->toChars()); | |
3723 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
|
3724 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3725 } |
3726 } | |
3727 else if (tb->isscalar()) | |
3728 { | |
3729 if (arguments && arguments->dim) | |
3730 error("no constructor for %s", type->toChars()); | |
3731 | |
3732 type = type->pointerTo(); | |
3733 } | |
3734 else | |
3735 { | |
3736 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3737 type = type->pointerTo(); | |
3738 } | |
3739 | |
3740 //printf("NewExp: '%s'\n", toChars()); | |
3741 //printf("NewExp:type '%s'\n", type->toChars()); | |
3742 | |
3743 return this; | |
3744 } | |
3745 | |
3746 int NewExp::checkSideEffect(int flag) | |
3747 { | |
3748 return 1; | |
3749 } | |
3750 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3751 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3752 int NewExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3753 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3754 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3755 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3756 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3757 |
159 | 3758 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3759 { int i; | |
3760 | |
3761 if (thisexp) | |
3762 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3763 buf->writeByte('.'); | |
3764 } | |
3765 buf->writestring("new "); | |
3766 if (newargs && newargs->dim) | |
3767 { | |
3768 buf->writeByte('('); | |
3769 argsToCBuffer(buf, newargs, hgs); | |
3770 buf->writeByte(')'); | |
3771 } | |
3772 newtype->toCBuffer(buf, NULL, hgs); | |
3773 if (arguments && arguments->dim) | |
3774 { | |
3775 buf->writeByte('('); | |
3776 argsToCBuffer(buf, arguments, hgs); | |
3777 buf->writeByte(')'); | |
3778 } | |
3779 } | |
3780 | |
3781 /********************** NewAnonClassExp **************************************/ | |
3782 | |
3783 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3784 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3785 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3786 { | |
3787 this->thisexp = thisexp; | |
3788 this->newargs = newargs; | |
3789 this->cd = cd; | |
3790 this->arguments = arguments; | |
3791 } | |
3792 | |
3793 Expression *NewAnonClassExp::syntaxCopy() | |
3794 { | |
3795 return new NewAnonClassExp(loc, | |
3796 thisexp ? thisexp->syntaxCopy() : NULL, | |
3797 arraySyntaxCopy(newargs), | |
3798 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3799 arraySyntaxCopy(arguments)); | |
3800 } | |
3801 | |
3802 | |
3803 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3804 { | |
3805 #if LOGSEMANTIC | |
3806 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3807 //printf("thisexp = %p\n", thisexp); |
159 | 3808 //printf("type: %s\n", type->toChars()); |
3809 #endif | |
3810 | |
3811 Expression *d = new DeclarationExp(loc, cd); | |
3812 d = d->semantic(sc); | |
3813 | |
3814 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3815 | |
3816 Expression *c = new CommaExp(loc, d, n); | |
3817 return c->semantic(sc); | |
3818 } | |
3819 | |
3820 int NewAnonClassExp::checkSideEffect(int flag) | |
3821 { | |
3822 return 1; | |
3823 } | |
3824 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3825 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3826 int NewAnonClassExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3827 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3828 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3829 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3830 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3831 |
159 | 3832 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3833 { int i; | |
3834 | |
3835 if (thisexp) | |
3836 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3837 buf->writeByte('.'); | |
3838 } | |
3839 buf->writestring("new"); | |
3840 if (newargs && newargs->dim) | |
3841 { | |
3842 buf->writeByte('('); | |
3843 argsToCBuffer(buf, newargs, hgs); | |
3844 buf->writeByte(')'); | |
3845 } | |
3846 buf->writestring(" class "); | |
3847 if (arguments && arguments->dim) | |
3848 { | |
3849 buf->writeByte('('); | |
3850 argsToCBuffer(buf, arguments, hgs); | |
3851 buf->writeByte(')'); | |
3852 } | |
3853 //buf->writestring(" { }"); | |
3854 if (cd) | |
3855 { | |
3856 cd->toCBuffer(buf, hgs); | |
3857 } | |
3858 } | |
3859 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3860 /********************** SymbolExp **************************************/ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3861 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3862 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3863 SymbolExp::SymbolExp(Loc loc, enum TOK op, int size, Declaration *var, int hasOverloads) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3864 : Expression(loc, op, size) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3865 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3866 assert(var); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3867 this->var = var; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3868 this->hasOverloads = hasOverloads; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3869 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3870 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3871 |
159 | 3872 /********************** SymOffExp **************************************/ |
3873 | |
3874 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3875 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3876 { | |
3877 assert(var); | |
3878 this->var = var; | |
3879 this->offset = offset; | |
3880 VarDeclaration *v = var->isVarDeclaration(); | |
3881 if (v && v->needThis()) | |
3882 error("need 'this' for address of %s", v->toChars()); | |
3883 } | |
3884 | |
3885 Expression *SymOffExp::semantic(Scope *sc) | |
3886 { | |
3887 #if LOGSEMANTIC | |
3888 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3889 #endif | |
3890 //var->semantic(sc); | |
3891 if (!type) | |
3892 type = var->type->pointerTo(); | |
3893 VarDeclaration *v = var->isVarDeclaration(); | |
3894 if (v) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
3895 v->checkNestedReference(sc, loc); |
159 | 3896 return this; |
3897 } | |
3898 | |
3899 int SymOffExp::isBool(int result) | |
3900 { | |
3901 return result ? TRUE : FALSE; | |
3902 } | |
3903 | |
3904 void SymOffExp::checkEscape() | |
3905 { | |
3906 VarDeclaration *v = var->isVarDeclaration(); | |
3907 if (v) | |
3908 { | |
3909 if (!v->isDataseg()) | |
3910 error("escaping reference to local variable %s", v->toChars()); | |
3911 } | |
3912 } | |
3913 | |
3914 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3915 { | |
3916 if (offset) | |
3917 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3918 else | |
3919 buf->printf("& %s", var->toChars()); | |
3920 } | |
3921 | |
3922 /******************************** VarExp **************************/ | |
3923 | |
3924 VarExp::VarExp(Loc loc, Declaration *var) | |
3925 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3926 { | |
3927 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3928 this->var = var; | |
3929 this->type = var->type; | |
3930 } | |
3931 | |
3932 int VarExp::equals(Object *o) | |
3933 { VarExp *ne; | |
3934 | |
3935 if (this == o || | |
3936 (((Expression *)o)->op == TOKvar && | |
3937 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3938 var == ne->var)) | |
3939 return 1; | |
3940 return 0; | |
3941 } | |
3942 | |
3943 Expression *VarExp::semantic(Scope *sc) | |
3944 { FuncLiteralDeclaration *fd; | |
3945 | |
3946 #if LOGSEMANTIC | |
3947 printf("VarExp::semantic(%s)\n", toChars()); | |
3948 #endif | |
3949 if (!type) | |
3950 { type = var->type; | |
3951 #if 0 | |
3952 if (var->storage_class & STClazy) | |
3953 { | |
3954 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3955 type = new TypeDelegate(tf); | |
3956 type = type->semantic(loc, sc); | |
3957 } | |
3958 #endif | |
3959 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3960 /* Fix for 1161 doesn't work because it causes protection |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3961 * problems when instantiating imported templates passing private |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3962 * variables as alias template parameters. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3963 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3964 //accessCheck(loc, sc, NULL, var); |
159 | 3965 |
3966 VarDeclaration *v = var->isVarDeclaration(); | |
3967 if (v) | |
3968 { | |
3969 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3970 { | |
3971 ExpInitializer *ei = v->init->isExpInitializer(); | |
3972 if (ei) | |
3973 { | |
3974 //ei->exp->implicitCastTo(sc, type)->print(); | |
3975 return ei->exp->implicitCastTo(sc, type); | |
3976 } | |
3977 } | |
3978 v->checkNestedReference(sc, loc); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3979 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3980 if (sc->func && sc->func->isPure() && !sc->intypeof) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3981 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3982 if (v->isDataseg() && !v->isInvariant()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3983 error("pure function '%s' cannot access mutable static data '%s'", sc->func->toChars(), v->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3984 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3985 #endif |
159 | 3986 } |
3987 #if 0 | |
3988 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3989 { Expression *e; | |
3990 e = new FuncExp(loc, fd); | |
3991 e->type = type; | |
3992 return e; | |
3993 } | |
3994 #endif | |
3995 return this; | |
3996 } | |
3997 | |
3998 char *VarExp::toChars() | |
3999 { | |
4000 return var->toChars(); | |
4001 } | |
4002 | |
4003 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4004 { | |
4005 buf->writestring(var->toChars()); | |
4006 } | |
4007 | |
4008 void VarExp::checkEscape() | |
4009 { | |
4010 VarDeclaration *v = var->isVarDeclaration(); | |
4011 if (v) | |
4012 { Type *tb = v->type->toBasetype(); | |
4013 // if reference type | |
4014 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
4015 { | |
4016 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
4017 error("escaping reference to auto local %s", v->toChars()); | |
4018 else if (v->storage_class & STCvariadic) | |
4019 error("escaping reference to variadic parameter %s", v->toChars()); | |
4020 } | |
4021 } | |
4022 } | |
4023 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4024 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4025 int VarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4026 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4027 if (var->storage_class & STClazy) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4028 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4029 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4030 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4031 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4032 |
159 | 4033 Expression *VarExp::toLvalue(Scope *sc, Expression *e) |
4034 { | |
4035 #if 0 | |
4036 tym = tybasic(e1->ET->Tty); | |
4037 if (!(tyscalar(tym) || | |
4038 tym == TYstruct || | |
4039 tym == TYarray && e->Eoper == TOKaddr)) | |
4040 synerr(EM_lvalue); // lvalue expected | |
4041 #endif | |
4042 if (var->storage_class & STClazy) | |
4043 error("lazy variables cannot be lvalues"); | |
4044 return this; | |
4045 } | |
4046 | |
4047 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
4048 { | |
4049 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
4050 if (sc->incontract && var->isParameter()) | |
4051 error("cannot modify parameter '%s' in contract", var->toChars()); | |
4052 | |
4053 if (type && type->toBasetype()->ty == Tsarray) | |
4054 error("cannot change reference to static array '%s'", var->toChars()); | |
4055 | |
4056 VarDeclaration *v = var->isVarDeclaration(); | |
4057 if (v && v->canassign == 0 && | |
4058 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
4059 error("cannot modify final variable '%s'", var->toChars()); | |
4060 | |
4061 if (var->isCtorinit()) | |
4062 { // It's only modifiable if inside the right constructor | |
4063 Dsymbol *s = sc->func; | |
4064 while (1) | |
4065 { | |
4066 FuncDeclaration *fd = NULL; | |
4067 if (s) | |
4068 fd = s->isFuncDeclaration(); | |
4069 if (fd && | |
4070 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
4071 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
4072 fd->toParent() == var->toParent() | |
4073 ) | |
4074 { | |
4075 VarDeclaration *v = var->isVarDeclaration(); | |
4076 assert(v); | |
4077 v->ctorinit = 1; | |
4078 //printf("setting ctorinit\n"); | |
4079 } | |
4080 else | |
4081 { | |
4082 if (s) | |
4083 { s = s->toParent2(); | |
4084 continue; | |
4085 } | |
4086 else | |
4087 { | |
4088 const char *p = var->isStatic() ? "static " : ""; | |
4089 error("can only initialize %sconst %s inside %sconstructor", | |
4090 p, var->toChars(), p); | |
4091 } | |
4092 } | |
4093 break; | |
4094 } | |
4095 } | |
4096 | |
4097 // See if this expression is a modifiable lvalue (i.e. not const) | |
4098 return toLvalue(sc, e); | |
4099 } | |
4100 | |
4101 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4102 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4103 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4104 #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
|
4105 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
|
4106 : 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
|
4107 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4108 //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
|
4109 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
|
4110 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
|
4111 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4112 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4113 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
|
4114 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4115 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
|
4116 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4117 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4118 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
|
4119 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4120 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
|
4121 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4122 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4123 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4124 |
159 | 4125 /******************************** TupleExp **************************/ |
4126 | |
4127 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
4128 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4129 { | |
4130 //printf("TupleExp(this = %p)\n", this); | |
4131 this->exps = exps; | |
4132 this->type = NULL; | |
4133 } | |
4134 | |
4135 | |
4136 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
4137 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4138 { | |
4139 exps = new Expressions(); | |
4140 type = NULL; | |
4141 | |
4142 exps->reserve(tup->objects->dim); | |
4143 for (size_t i = 0; i < tup->objects->dim; i++) | |
4144 { Object *o = (Object *)tup->objects->data[i]; | |
4145 if (o->dyncast() == DYNCAST_EXPRESSION) | |
4146 { | |
4147 Expression *e = (Expression *)o; | |
4148 e = e->syntaxCopy(); | |
4149 exps->push(e); | |
4150 } | |
4151 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
4152 { | |
4153 Dsymbol *s = (Dsymbol *)o; | |
4154 Expression *e = new DsymbolExp(loc, s); | |
4155 exps->push(e); | |
4156 } | |
4157 else if (o->dyncast() == DYNCAST_TYPE) | |
4158 { | |
4159 Type *t = (Type *)o; | |
4160 Expression *e = new TypeExp(loc, t); | |
4161 exps->push(e); | |
4162 } | |
4163 else | |
4164 { | |
4165 error("%s is not an expression", o->toChars()); | |
4166 } | |
4167 } | |
4168 } | |
4169 | |
4170 int TupleExp::equals(Object *o) | |
4171 { TupleExp *ne; | |
4172 | |
4173 if (this == o) | |
4174 return 1; | |
4175 if (((Expression *)o)->op == TOKtuple) | |
4176 { | |
4177 TupleExp *te = (TupleExp *)o; | |
4178 if (exps->dim != te->exps->dim) | |
4179 return 0; | |
4180 for (size_t i = 0; i < exps->dim; i++) | |
4181 { Expression *e1 = (Expression *)exps->data[i]; | |
4182 Expression *e2 = (Expression *)te->exps->data[i]; | |
4183 | |
4184 if (!e1->equals(e2)) | |
4185 return 0; | |
4186 } | |
4187 return 1; | |
4188 } | |
4189 return 0; | |
4190 } | |
4191 | |
4192 Expression *TupleExp::syntaxCopy() | |
4193 { | |
4194 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
4195 } | |
4196 | |
4197 Expression *TupleExp::semantic(Scope *sc) | |
4198 { | |
4199 #if LOGSEMANTIC | |
4200 printf("+TupleExp::semantic(%s)\n", toChars()); | |
4201 #endif | |
4202 if (type) | |
4203 return this; | |
4204 | |
4205 // Run semantic() on each argument | |
4206 for (size_t i = 0; i < exps->dim; i++) | |
4207 { Expression *e = (Expression *)exps->data[i]; | |
4208 | |
4209 e = e->semantic(sc); | |
4210 if (!e->type) | |
4211 { error("%s has no value", e->toChars()); | |
4212 e->type = Type::terror; | |
4213 } | |
4214 exps->data[i] = (void *)e; | |
4215 } | |
4216 | |
4217 expandTuples(exps); | |
4218 if (0 && exps->dim == 1) | |
4219 { | |
4220 return (Expression *)exps->data[0]; | |
4221 } | |
4222 type = new TypeTuple(exps); | |
4223 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
4224 return this; | |
4225 } | |
4226 | |
4227 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4228 { | |
4229 buf->writestring("tuple("); | |
4230 argsToCBuffer(buf, exps, hgs); | |
4231 buf->writeByte(')'); | |
4232 } | |
4233 | |
4234 int TupleExp::checkSideEffect(int flag) | |
4235 { int f = 0; | |
4236 | |
4237 for (int i = 0; i < exps->dim; i++) | |
4238 { Expression *e = (Expression *)exps->data[i]; | |
4239 | |
4240 f |= e->checkSideEffect(2); | |
4241 } | |
4242 if (flag == 0 && f == 0) | |
4243 Expression::checkSideEffect(0); | |
4244 return f; | |
4245 } | |
4246 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4247 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4248 int TupleExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4249 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4250 return arrayExpressionCanThrow(exps); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4251 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4252 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4253 |
159 | 4254 void TupleExp::checkEscape() |
4255 { | |
4256 for (size_t i = 0; i < exps->dim; i++) | |
4257 { Expression *e = (Expression *)exps->data[i]; | |
4258 e->checkEscape(); | |
4259 } | |
4260 } | |
4261 | |
4262 /******************************** FuncExp *********************************/ | |
4263 | |
4264 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4265 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4266 { | |
4267 this->fd = fd; | |
4268 } | |
4269 | |
4270 Expression *FuncExp::syntaxCopy() | |
4271 { | |
4272 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4273 } | |
4274 | |
4275 Expression *FuncExp::semantic(Scope *sc) | |
4276 { | |
4277 #if LOGSEMANTIC | |
4278 printf("FuncExp::semantic(%s)\n", toChars()); | |
4279 #endif | |
4280 if (!type) | |
4281 { | |
4282 fd->semantic(sc); | |
4283 fd->parent = sc->parent; | |
4284 if (global.errors) | |
4285 { | |
4286 if (!fd->type->next) | |
4287 fd->type->next = Type::terror; | |
4288 } | |
4289 else | |
4290 { | |
4291 fd->semantic2(sc); | |
4292 if (!global.errors) | |
4293 { | |
4294 fd->semantic3(sc); | |
4295 | |
4296 if (!global.errors && global.params.useInline) | |
4297 fd->inlineScan(); | |
4298 } | |
4299 } | |
4300 | |
4301 // Type is a "delegate to" or "pointer to" the function literal | |
4302 if (fd->isNested()) | |
4303 { | |
4304 type = new TypeDelegate(fd->type); | |
4305 type = type->semantic(loc, sc); | |
4306 } | |
4307 else | |
4308 { | |
4309 type = fd->type->pointerTo(); | |
4310 } | |
4311 } | |
4312 return this; | |
4313 } | |
4314 | |
4315 char *FuncExp::toChars() | |
4316 { | |
4317 return fd->toChars(); | |
4318 } | |
4319 | |
4320 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4321 { | |
4322 buf->writestring(fd->toChars()); | |
4323 } | |
4324 | |
4325 | |
4326 /******************************** DeclarationExp **************************/ | |
4327 | |
4328 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4329 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4330 { | |
4331 this->declaration = declaration; | |
4332 } | |
4333 | |
4334 Expression *DeclarationExp::syntaxCopy() | |
4335 { | |
4336 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4337 } | |
4338 | |
4339 Expression *DeclarationExp::semantic(Scope *sc) | |
4340 { | |
4341 if (type) | |
4342 return this; | |
4343 | |
4344 #if LOGSEMANTIC | |
4345 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4346 #endif | |
4347 | |
4348 /* This is here to support extern(linkage) declaration, | |
4349 * where the extern(linkage) winds up being an AttribDeclaration | |
4350 * wrapper. | |
4351 */ | |
4352 Dsymbol *s = declaration; | |
4353 | |
4354 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4355 if (ad) | |
4356 { | |
4357 if (ad->decl && ad->decl->dim == 1) | |
4358 s = (Dsymbol *)ad->decl->data[0]; | |
4359 } | |
4360 | |
4361 if (s->isVarDeclaration()) | |
4362 { // Do semantic() on initializer first, so: | |
4363 // int a = a; | |
4364 // will be illegal. | |
4365 declaration->semantic(sc); | |
4366 s->parent = sc->parent; | |
4367 } | |
4368 | |
4369 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4370 // Insert into both local scope and function scope. | |
4371 // Must be unique in both. | |
4372 if (s->ident) | |
4373 { | |
4374 if (!sc->insert(s)) | |
4375 error("declaration %s is already defined", s->toPrettyChars()); | |
4376 else if (sc->func) | |
4377 { VarDeclaration *v = s->isVarDeclaration(); | |
4378 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4379 !sc->func->localsymtab->insert(s)) | |
4380 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4381 else if (!global.params.useDeprecated) | |
4382 { // Disallow shadowing | |
4383 | |
4384 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4385 { Dsymbol *s2; | |
4386 | |
4387 if (scx->scopesym && scx->scopesym->symtab && | |
4388 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4389 s != s2) | |
4390 { | |
4391 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4392 } | |
4393 } | |
4394 } | |
4395 } | |
4396 } | |
4397 if (!s->isVarDeclaration()) | |
4398 { | |
4399 declaration->semantic(sc); | |
4400 s->parent = sc->parent; | |
4401 } | |
4402 if (!global.errors) | |
4403 { | |
4404 declaration->semantic2(sc); | |
4405 if (!global.errors) | |
4406 { | |
4407 declaration->semantic3(sc); | |
4408 | |
4409 if (!global.errors && global.params.useInline) | |
4410 declaration->inlineScan(); | |
4411 } | |
4412 } | |
4413 | |
4414 type = Type::tvoid; | |
4415 return this; | |
4416 } | |
4417 | |
4418 int DeclarationExp::checkSideEffect(int flag) | |
4419 { | |
4420 return 1; | |
4421 } | |
4422 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4423 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4424 int DeclarationExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4425 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4426 VarDeclaration *v = declaration->isVarDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4427 if (v && v->init) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4428 { ExpInitializer *ie = v->init->isExpInitializer(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4429 return ie && ie->exp->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4430 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4431 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4432 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4433 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4434 |
159 | 4435 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4436 { | |
4437 declaration->toCBuffer(buf, hgs); | |
4438 } | |
4439 | |
4440 | |
4441 /************************ TypeidExp ************************************/ | |
4442 | |
4443 /* | |
4444 * typeid(int) | |
4445 */ | |
4446 | |
4447 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4448 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4449 { | |
4450 this->typeidType = typeidType; | |
4451 } | |
4452 | |
4453 | |
4454 Expression *TypeidExp::syntaxCopy() | |
4455 { | |
4456 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4457 } | |
4458 | |
4459 | |
4460 Expression *TypeidExp::semantic(Scope *sc) | |
4461 { Expression *e; | |
4462 | |
4463 #if LOGSEMANTIC | |
4464 printf("TypeidExp::semantic()\n"); | |
4465 #endif | |
4466 typeidType = typeidType->semantic(loc, sc); | |
4467 e = typeidType->getTypeInfo(sc); | |
336 | 4468 if (e->loc.linnum == 0) |
4469 e->loc = loc; // so there's at least some line number info | |
159 | 4470 return e; |
4471 } | |
4472 | |
4473 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4474 { | |
4475 buf->writestring("typeid("); | |
4476 typeidType->toCBuffer(buf, NULL, hgs); | |
4477 buf->writeByte(')'); | |
4478 } | |
4479 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4480 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4481 #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
|
4482 /* |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4483 * __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
|
4484 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4485 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4486 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
|
4487 : 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
|
4488 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4489 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
|
4490 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
|
4491 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4492 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4493 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4494 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
|
4495 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4496 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
|
4497 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4498 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4499 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4500 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
|
4501 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4502 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
|
4503 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
|
4504 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
|
4505 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4506 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
|
4507 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4508 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
|
4509 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
|
4510 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
|
4511 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4512 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4513 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
|
4514 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4515 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4516 |
159 | 4517 /************************************************************/ |
4518 | |
4519 HaltExp::HaltExp(Loc loc) | |
4520 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4521 { | |
4522 } | |
4523 | |
4524 Expression *HaltExp::semantic(Scope *sc) | |
4525 { | |
4526 #if LOGSEMANTIC | |
4527 printf("HaltExp::semantic()\n"); | |
4528 #endif | |
4529 type = Type::tvoid; | |
4530 return this; | |
4531 } | |
4532 | |
4533 int HaltExp::checkSideEffect(int flag) | |
4534 { | |
4535 return 1; | |
4536 } | |
4537 | |
4538 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4539 { | |
4540 buf->writestring("halt"); | |
4541 } | |
4542 | |
4543 /************************************************************/ | |
4544 | |
4545 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4546 Type *tspec, enum TOK tok2) | |
4547 : Expression(loc, TOKis, sizeof(IsExp)) | |
4548 { | |
4549 this->targ = targ; | |
4550 this->id = id; | |
4551 this->tok = tok; | |
4552 this->tspec = tspec; | |
4553 this->tok2 = tok2; | |
4554 } | |
4555 | |
4556 Expression *IsExp::syntaxCopy() | |
4557 { | |
4558 return new IsExp(loc, | |
4559 targ->syntaxCopy(), | |
4560 id, | |
4561 tok, | |
4562 tspec ? tspec->syntaxCopy() : NULL, | |
4563 tok2); | |
4564 } | |
4565 | |
4566 Expression *IsExp::semantic(Scope *sc) | |
4567 { Type *tded; | |
4568 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4569 /* 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
|
4570 * 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
|
4571 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4572 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4573 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4574 if (id && !(sc->flags & SCOPEstaticif)) |
4575 error("can only declare type aliases within static if conditionals"); | |
4576 | |
4577 unsigned errors_save = global.errors; | |
4578 global.errors = 0; | |
4579 global.gag++; // suppress printing of error messages | |
4580 targ = targ->semantic(loc, sc); | |
4581 global.gag--; | |
4582 unsigned gerrors = global.errors; | |
4583 global.errors = errors_save; | |
4584 | |
4585 if (gerrors) // if any errors happened | |
4586 { // then condition is false | |
4587 goto Lno; | |
4588 } | |
4589 else if (tok2 != TOKreserved) | |
4590 { | |
4591 switch (tok2) | |
4592 { | |
4593 case TOKtypedef: | |
4594 if (targ->ty != Ttypedef) | |
4595 goto Lno; | |
4596 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4597 break; | |
4598 | |
4599 case TOKstruct: | |
4600 if (targ->ty != Tstruct) | |
4601 goto Lno; | |
4602 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4603 goto Lno; | |
4604 tded = targ; | |
4605 break; | |
4606 | |
4607 case TOKunion: | |
4608 if (targ->ty != Tstruct) | |
4609 goto Lno; | |
4610 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4611 goto Lno; | |
4612 tded = targ; | |
4613 break; | |
4614 | |
4615 case TOKclass: | |
4616 if (targ->ty != Tclass) | |
4617 goto Lno; | |
4618 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4619 goto Lno; | |
4620 tded = targ; | |
4621 break; | |
4622 | |
4623 case TOKinterface: | |
4624 if (targ->ty != Tclass) | |
4625 goto Lno; | |
4626 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4627 goto Lno; | |
4628 tded = targ; | |
4629 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
|
4630 #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
|
4631 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
|
4632 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
|
4633 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
|
4634 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
|
4635 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4636 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4637 case TOKinvariant: |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4638 case TOKimmutable: |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4639 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
|
4640 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
|
4641 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
|
4642 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4643 #endif |
159 | 4644 |
4645 case TOKsuper: | |
4646 // If class or interface, get the base class and interfaces | |
4647 if (targ->ty != Tclass) | |
4648 goto Lno; | |
4649 else | |
4650 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4651 Arguments *args = new Arguments; | |
4652 args->reserve(cd->baseclasses.dim); | |
4653 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4654 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4655 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4656 } | |
4657 tded = new TypeTuple(args); | |
4658 } | |
4659 break; | |
4660 | |
4661 case TOKenum: | |
4662 if (targ->ty != Tenum) | |
4663 goto Lno; | |
4664 tded = ((TypeEnum *)targ)->sym->memtype; | |
4665 break; | |
4666 | |
4667 case TOKdelegate: | |
4668 if (targ->ty != Tdelegate) | |
4669 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
|
4670 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4671 break; |
4672 | |
4673 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
|
4674 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4675 if (targ->ty != Tfunction) |
159 | 4676 goto Lno; |
4677 tded = targ; | |
4678 | |
4679 /* Generate tuple from function parameter types. | |
4680 */ | |
4681 assert(tded->ty == Tfunction); | |
4682 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4683 size_t dim = Argument::dim(params); | |
4684 Arguments *args = new Arguments; | |
4685 args->reserve(dim); | |
4686 for (size_t i = 0; i < dim; i++) | |
4687 { Argument *arg = Argument::getNth(params, i); | |
4688 assert(arg && arg->type); | |
4689 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4690 } | |
4691 tded = new TypeTuple(args); | |
4692 break; | |
4693 } | |
4694 case TOKreturn: | |
4695 /* Get the 'return type' for the function, | |
4696 * delegate, or pointer to function. | |
4697 */ | |
4698 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
|
4699 tded = ((TypeFunction *)targ)->next; |
159 | 4700 else if (targ->ty == Tdelegate) |
4701 tded = targ->next->next; | |
4702 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4703 tded = targ->next->next; | |
4704 else | |
4705 goto Lno; | |
4706 break; | |
4707 | |
4708 default: | |
4709 assert(0); | |
4710 } | |
4711 goto Lyes; | |
4712 } | |
4713 else if (id && tspec) | |
4714 { | |
4715 /* Evaluate to TRUE if targ matches tspec. | |
4716 * If TRUE, declare id as an alias for the specialized type. | |
4717 */ | |
4718 | |
4719 MATCH m; | |
4720 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4721 | |
4722 TemplateParameters parameters; | |
4723 parameters.setDim(1); | |
4724 parameters.data[0] = (void *)&tp; | |
4725 | |
4726 Objects dedtypes; | |
4727 dedtypes.setDim(1); | |
4728 dedtypes.data[0] = NULL; | |
4729 | |
4730 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4731 if (m == MATCHnomatch || | |
4732 (m != MATCHexact && tok == TOKequal)) | |
4733 goto Lno; | |
4734 else | |
4735 { | |
4736 assert(dedtypes.dim == 1); | |
4737 tded = (Type *)dedtypes.data[0]; | |
4738 if (!tded) | |
4739 tded = targ; | |
4740 goto Lyes; | |
4741 } | |
4742 } | |
4743 else if (id) | |
4744 { | |
4745 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4746 */ | |
4747 tded = targ; | |
4748 goto Lyes; | |
4749 } | |
4750 else if (tspec) | |
4751 { | |
4752 /* Evaluate to TRUE if targ matches tspec | |
4753 */ | |
4754 tspec = tspec->semantic(loc, sc); | |
4755 //printf("targ = %s\n", targ->toChars()); | |
4756 //printf("tspec = %s\n", tspec->toChars()); | |
4757 if (tok == TOKcolon) | |
4758 { if (targ->implicitConvTo(tspec)) | |
4759 goto Lyes; | |
4760 else | |
4761 goto Lno; | |
4762 } | |
4763 else /* == */ | |
4764 { if (targ->equals(tspec)) | |
4765 goto Lyes; | |
4766 else | |
4767 goto Lno; | |
4768 } | |
4769 } | |
4770 | |
4771 Lyes: | |
4772 if (id) | |
4773 { | |
4774 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4775 s->semantic(sc); | |
4776 sc->insert(s); | |
4777 if (sc->sd) | |
4778 s->addMember(sc, sc->sd, 1); | |
4779 } | |
4780 return new IntegerExp(1); | |
4781 | |
4782 Lno: | |
4783 return new IntegerExp(0); | |
4784 } | |
4785 | |
4786 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4787 { | |
4788 buf->writestring("is("); | |
4789 targ->toCBuffer(buf, id, hgs); | |
4790 if (tok2 != TOKreserved) | |
4791 { | |
4792 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4793 } | |
4794 else if (tspec) | |
4795 { | |
4796 if (tok == TOKcolon) | |
4797 buf->writestring(" : "); | |
4798 else | |
4799 buf->writestring(" == "); | |
4800 tspec->toCBuffer(buf, NULL, hgs); | |
4801 } | |
336 | 4802 #if DMDV2 |
159 | 4803 if (parameters) |
4804 { // First parameter is already output, so start with second | |
4805 for (int i = 1; i < parameters->dim; i++) | |
4806 { | |
4807 buf->writeByte(','); | |
4808 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4809 tp->toCBuffer(buf, hgs); | |
4810 } | |
4811 } | |
4812 #endif | |
4813 buf->writeByte(')'); | |
4814 } | |
4815 | |
4816 | |
4817 /************************************************************/ | |
4818 | |
4819 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4820 : Expression(loc, op, size) | |
4821 { | |
4822 this->e1 = e1; | |
4823 } | |
4824 | |
4825 Expression *UnaExp::syntaxCopy() | |
4826 { UnaExp *e; | |
4827 | |
4828 e = (UnaExp *)copy(); | |
4829 e->type = NULL; | |
4830 e->e1 = e->e1->syntaxCopy(); | |
4831 return e; | |
4832 } | |
4833 | |
4834 Expression *UnaExp::semantic(Scope *sc) | |
4835 { | |
4836 #if LOGSEMANTIC | |
4837 printf("UnaExp::semantic('%s')\n", toChars()); | |
4838 #endif | |
4839 e1 = e1->semantic(sc); | |
4840 // if (!e1->type) | |
4841 // error("%s has no value", e1->toChars()); | |
4842 return this; | |
4843 } | |
4844 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4845 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4846 int UnaExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4847 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4848 return e1->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4849 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4850 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4851 |
159 | 4852 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4853 { | |
4854 buf->writestring(Token::toChars(op)); | |
4855 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4856 } | |
4857 | |
4858 /************************************************************/ | |
4859 | |
4860 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4861 : Expression(loc, op, size) | |
4862 { | |
4863 this->e1 = e1; | |
4864 this->e2 = e2; | |
4865 } | |
4866 | |
4867 Expression *BinExp::syntaxCopy() | |
4868 { BinExp *e; | |
4869 | |
4870 e = (BinExp *)copy(); | |
4871 e->type = NULL; | |
4872 e->e1 = e->e1->syntaxCopy(); | |
4873 e->e2 = e->e2->syntaxCopy(); | |
4874 return e; | |
4875 } | |
4876 | |
4877 Expression *BinExp::semantic(Scope *sc) | |
4878 { | |
4879 #if LOGSEMANTIC | |
4880 printf("BinExp::semantic('%s')\n", toChars()); | |
4881 #endif | |
4882 e1 = e1->semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4883 if (!e1->type && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4884 !(op == TOKassign && e1->op == TOKdottd)) // a.template = e2 |
159 | 4885 { |
4886 error("%s has no value", e1->toChars()); | |
4887 e1->type = Type::terror; | |
4888 } | |
4889 e2 = e2->semantic(sc); | |
4890 if (!e2->type) | |
4891 { | |
4892 error("%s has no value", e2->toChars()); | |
4893 e2->type = Type::terror; | |
4894 } | |
4895 return this; | |
4896 } | |
4897 | |
4898 Expression *BinExp::semanticp(Scope *sc) | |
4899 { | |
4900 BinExp::semantic(sc); | |
4901 e1 = resolveProperties(sc, e1); | |
4902 e2 = resolveProperties(sc, e2); | |
4903 return this; | |
4904 } | |
4905 | |
4906 /*************************** | |
4907 * Common semantic routine for some xxxAssignExp's. | |
4908 */ | |
4909 | |
4910 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4911 { Expression *e; | |
4912 | |
4913 if (!type) | |
4914 { | |
4915 BinExp::semantic(sc); | |
4916 e2 = resolveProperties(sc, e2); | |
4917 | |
4918 e = op_overload(sc); | |
4919 if (e) | |
4920 return e; | |
4921 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4922 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
|
4923 { // 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
|
4924 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
|
4925 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
|
4926 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
|
4927 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4928 |
159 | 4929 e1 = e1->modifiableLvalue(sc, e1); |
4930 e1->checkScalar(); | |
4931 type = e1->type; | |
4932 if (type->toBasetype()->ty == Tbool) | |
4933 { | |
4934 error("operator not allowed on bool expression %s", toChars()); | |
4935 } | |
4936 typeCombine(sc); | |
4937 e1->checkArithmetic(); | |
4938 e2->checkArithmetic(); | |
4939 | |
4940 if (op == TOKmodass && e2->type->iscomplex()) | |
4941 { error("cannot perform modulo complex arithmetic"); | |
4942 return new IntegerExp(0); | |
4943 } | |
4944 } | |
4945 return this; | |
4946 } | |
4947 | |
4948 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4949 { Expression *e; | |
4950 | |
4951 if (!type) | |
4952 { | |
4953 BinExp::semantic(sc); | |
4954 e2 = resolveProperties(sc, e2); | |
4955 | |
4956 e = op_overload(sc); | |
4957 if (e) | |
4958 return e; | |
4959 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4960 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
|
4961 { // 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
|
4962 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
|
4963 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
|
4964 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
|
4965 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4966 |
159 | 4967 e1 = e1->modifiableLvalue(sc, e1); |
4968 e1->checkScalar(); | |
4969 type = e1->type; | |
4970 if (type->toBasetype()->ty == Tbool) | |
4971 { | |
4972 e2 = e2->implicitCastTo(sc, type); | |
4973 } | |
4974 | |
4975 typeCombine(sc); | |
4976 e1->checkIntegral(); | |
4977 e2->checkIntegral(); | |
4978 } | |
4979 return this; | |
4980 } | |
4981 | |
4982 int BinExp::checkSideEffect(int flag) | |
4983 { | |
4984 if (op == TOKplusplus || | |
4985 op == TOKminusminus || | |
4986 op == TOKassign || | |
4987 op == TOKconstruct || | |
4988 op == TOKblit || | |
4989 op == TOKaddass || | |
4990 op == TOKminass || | |
4991 op == TOKcatass || | |
4992 op == TOKmulass || | |
4993 op == TOKdivass || | |
4994 op == TOKmodass || | |
4995 op == TOKshlass || | |
4996 op == TOKshrass || | |
4997 op == TOKushrass || | |
4998 op == TOKandass || | |
4999 op == TOKorass || | |
5000 op == TOKxorass || | |
5001 op == TOKin || | |
5002 op == TOKremove) | |
5003 return 1; | |
5004 return Expression::checkSideEffect(flag); | |
5005 } | |
5006 | |
5007 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5008 { | |
5009 expToCBuffer(buf, hgs, e1, precedence[op]); | |
5010 buf->writeByte(' '); | |
5011 buf->writestring(Token::toChars(op)); | |
5012 buf->writeByte(' '); | |
5013 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
5014 } | |
5015 | |
5016 int BinExp::isunsigned() | |
5017 { | |
5018 return e1->type->isunsigned() || e2->type->isunsigned(); | |
5019 } | |
5020 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5021 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5022 int BinExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5023 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5024 return e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5025 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5026 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5027 |
159 | 5028 void BinExp::incompatibleTypes() |
5029 { | |
5030 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
5031 e1->toChars(), Token::toChars(op), e2->toChars(), | |
5032 e1->type->toChars(), e2->type->toChars()); | |
5033 } | |
5034 | |
5035 /************************************************************/ | |
5036 | |
5037 CompileExp::CompileExp(Loc loc, Expression *e) | |
5038 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
5039 { | |
5040 } | |
5041 | |
5042 Expression *CompileExp::semantic(Scope *sc) | |
5043 { | |
5044 #if LOGSEMANTIC | |
5045 printf("CompileExp::semantic('%s')\n", toChars()); | |
5046 #endif | |
5047 UnaExp::semantic(sc); | |
5048 e1 = resolveProperties(sc, e1); | |
5049 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
5050 if (e1->op != TOKstring) | |
5051 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
5052 type = Type::terror; | |
5053 return this; | |
5054 } | |
5055 StringExp *se = (StringExp *)e1; | |
5056 se = se->toUTF8(sc); | |
5057 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
5058 p.loc = loc; | |
5059 p.nextToken(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5060 //printf("p.loc.linnum = %d\n", p.loc.linnum); |
159 | 5061 Expression *e = p.parseExpression(); |
5062 if (p.token.value != TOKeof) | |
5063 error("incomplete mixin expression (%s)", se->toChars()); | |
5064 return e->semantic(sc); | |
5065 } | |
5066 | |
5067 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5068 { | |
5069 buf->writestring("mixin("); | |
5070 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5071 buf->writeByte(')'); | |
5072 } | |
5073 | |
5074 /************************************************************/ | |
5075 | |
5076 FileExp::FileExp(Loc loc, Expression *e) | |
5077 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
5078 { | |
5079 } | |
5080 | |
5081 Expression *FileExp::semantic(Scope *sc) | |
5082 { char *name; | |
5083 StringExp *se; | |
5084 | |
5085 #if LOGSEMANTIC | |
5086 printf("FileExp::semantic('%s')\n", toChars()); | |
5087 #endif | |
5088 UnaExp::semantic(sc); | |
5089 e1 = resolveProperties(sc, e1); | |
5090 e1 = e1->optimize(WANTvalue); | |
5091 if (e1->op != TOKstring) | |
5092 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
5093 goto Lerror; | |
5094 } | |
5095 se = (StringExp *)e1; | |
5096 se = se->toUTF8(sc); | |
5097 name = (char *)se->string; | |
5098 | |
5099 if (!global.params.fileImppath) | |
5100 { error("need -Jpath switch to import text file %s", name); | |
5101 goto Lerror; | |
5102 } | |
5103 | |
5104 if (name != FileName::name(name)) | |
5105 { error("use -Jpath switch to provide path for filename %s", name); | |
5106 goto Lerror; | |
5107 } | |
5108 | |
5109 name = FileName::searchPath(global.filePath, name, 0); | |
5110 if (!name) | |
5111 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
5112 goto Lerror; | |
5113 } | |
5114 | |
5115 if (global.params.verbose) | |
794
661384d6a936
Fix warnings on x86-64. By fvbommel.
Christian Kamm <kamm incasoftware de>
parents:
779
diff
changeset
|
5116 printf("file %s\t(%s)\n", (char*)se->string, name); |
159 | 5117 |
5118 { File f(name); | |
5119 if (f.read()) | |
5120 { error("cannot read file %s", f.toChars()); | |
5121 goto Lerror; | |
5122 } | |
5123 else | |
5124 { | |
5125 f.ref = 1; | |
5126 se = new StringExp(loc, f.buffer, f.len); | |
5127 } | |
5128 } | |
5129 Lret: | |
5130 return se->semantic(sc); | |
5131 | |
5132 Lerror: | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5133 se = new StringExp(loc, (char *)""); |
159 | 5134 goto Lret; |
5135 } | |
5136 | |
5137 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5138 { | |
5139 buf->writestring("import("); | |
5140 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5141 buf->writeByte(')'); | |
5142 } | |
5143 | |
5144 /************************************************************/ | |
5145 | |
5146 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
5147 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
5148 { | |
5149 this->msg = msg; | |
5150 } | |
5151 | |
5152 Expression *AssertExp::syntaxCopy() | |
5153 { | |
5154 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
5155 msg ? msg->syntaxCopy() : NULL); | |
5156 return ae; | |
5157 } | |
5158 | |
5159 Expression *AssertExp::semantic(Scope *sc) | |
5160 { | |
5161 #if LOGSEMANTIC | |
5162 printf("AssertExp::semantic('%s')\n", toChars()); | |
5163 #endif | |
5164 UnaExp::semantic(sc); | |
5165 e1 = resolveProperties(sc, e1); | |
5166 // BUG: see if we can do compile time elimination of the Assert | |
5167 e1 = e1->optimize(WANTvalue); | |
5168 e1 = e1->checkToBoolean(); | |
5169 if (msg) | |
5170 { | |
5171 msg = msg->semantic(sc); | |
5172 msg = resolveProperties(sc, msg); | |
5173 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
5174 msg = msg->optimize(WANTvalue); | |
5175 } | |
5176 if (e1->isBool(FALSE)) | |
5177 { | |
5178 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
5179 fd->hasReturnExp |= 4; | |
5180 | |
5181 if (!global.params.useAssert) | |
5182 { Expression *e = new HaltExp(loc); | |
5183 e = e->semantic(sc); | |
5184 return e; | |
5185 } | |
5186 } | |
5187 type = Type::tvoid; | |
5188 return this; | |
5189 } | |
5190 | |
5191 int AssertExp::checkSideEffect(int flag) | |
5192 { | |
5193 return 1; | |
5194 } | |
5195 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5196 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5197 int AssertExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5198 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5199 return (global.params.useAssert != 0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5200 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5201 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5202 |
159 | 5203 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
5204 { | |
5205 buf->writestring("assert("); | |
5206 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5207 if (msg) | |
5208 { | |
5209 buf->writeByte(','); | |
5210 expToCBuffer(buf, hgs, msg, PREC_assign); | |
5211 } | |
5212 buf->writeByte(')'); | |
5213 } | |
5214 | |
5215 /************************************************************/ | |
5216 | |
5217 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
5218 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
5219 { | |
5220 this->ident = ident; | |
5221 } | |
5222 | |
5223 Expression *DotIdExp::semantic(Scope *sc) | |
5224 { Expression *e; | |
5225 Expression *eleft; | |
5226 Expression *eright; | |
5227 | |
5228 #if LOGSEMANTIC | |
5229 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
5230 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
5231 #endif | |
5232 | |
5233 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
5234 | |
5235 #if 0 | |
5236 /* Don't do semantic analysis if we'll be converting | |
5237 * it to a string. | |
5238 */ | |
5239 if (ident == Id::stringof) | |
5240 { char *s = e1->toChars(); | |
5241 e = new StringExp(loc, s, strlen(s), 'c'); | |
5242 e = e->semantic(sc); | |
5243 return e; | |
5244 } | |
5245 #endif | |
5246 | |
5247 /* Special case: rewrite this.id and super.id | |
5248 * to be classtype.id and baseclasstype.id | |
5249 * if we have no this pointer. | |
5250 */ | |
5251 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
5252 { ClassDeclaration *cd; | |
5253 StructDeclaration *sd; | |
5254 AggregateDeclaration *ad; | |
5255 | |
5256 ad = sc->getStructClassScope(); | |
5257 if (ad) | |
5258 { | |
5259 cd = ad->isClassDeclaration(); | |
5260 if (cd) | |
5261 { | |
5262 if (e1->op == TOKthis) | |
5263 { | |
5264 e = new TypeDotIdExp(loc, cd->type, ident); | |
5265 return e->semantic(sc); | |
5266 } | |
5267 else if (cd->baseClass && e1->op == TOKsuper) | |
5268 { | |
5269 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
5270 return e->semantic(sc); | |
5271 } | |
5272 } | |
5273 else | |
5274 { | |
5275 sd = ad->isStructDeclaration(); | |
5276 if (sd) | |
5277 { | |
5278 if (e1->op == TOKthis) | |
5279 { | |
5280 e = new TypeDotIdExp(loc, sd->type, ident); | |
5281 return e->semantic(sc); | |
5282 } | |
5283 } | |
5284 } | |
5285 } | |
5286 } | |
5287 | |
5288 UnaExp::semantic(sc); | |
5289 | |
5290 if (e1->op == TOKdotexp) | |
5291 { | |
5292 DotExp *de = (DotExp *)e1; | |
5293 eleft = de->e1; | |
5294 eright = de->e2; | |
5295 } | |
5296 else | |
5297 { | |
5298 e1 = resolveProperties(sc, e1); | |
5299 eleft = NULL; | |
5300 eright = e1; | |
5301 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5302 #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
|
5303 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
|
5304 { /* '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
|
5305 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5306 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
|
5307 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
|
5308 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
|
5309 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
|
5310 { 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
|
5311 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
|
5312 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
|
5313 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
|
5314 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5315 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
|
5316 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
|
5317 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
|
5318 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5319 #endif |
159 | 5320 |
5321 if (e1->op == TOKtuple && ident == Id::length) | |
5322 { | |
5323 TupleExp *te = (TupleExp *)e1; | |
5324 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5325 return e; | |
5326 } | |
5327 | |
5328 if (eright->op == TOKimport) // also used for template alias's | |
5329 { | |
5330 ScopeExp *ie = (ScopeExp *)eright; | |
5331 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5332 /* Disable access to another module's private imports. |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5333 * The check for 'is sds our current module' is because |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5334 * the current module should have access to its own imports. |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5335 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5336 Dsymbol *s = ie->sds->search(loc, ident, |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5337 (ie->sds->isModule() && ie->sds != sc->module) ? 1 : 0); |
159 | 5338 if (s) |
5339 { | |
5340 s = s->toAlias(); | |
5341 checkDeprecated(sc, s); | |
5342 | |
5343 EnumMember *em = s->isEnumMember(); | |
5344 if (em) | |
5345 { | |
5346 e = em->value; | |
5347 e = e->semantic(sc); | |
5348 return e; | |
5349 } | |
5350 | |
5351 VarDeclaration *v = s->isVarDeclaration(); | |
5352 if (v) | |
5353 { | |
5354 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5355 if (v->inuse) | |
5356 { | |
5357 error("circular reference to '%s'", v->toChars()); | |
5358 type = Type::tint32; | |
5359 return this; | |
5360 } | |
5361 type = v->type; | |
5362 if (v->isConst()) | |
5363 { | |
5364 if (v->init) | |
5365 { | |
5366 ExpInitializer *ei = v->init->isExpInitializer(); | |
5367 if (ei) | |
5368 { | |
5369 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5370 //ei->exp = ei->exp->semantic(sc); | |
5371 if (ei->exp->type == type) | |
5372 { | |
5373 e = ei->exp->copy(); // make copy so we can change loc | |
5374 e->loc = loc; | |
5375 return e; | |
5376 } | |
5377 } | |
5378 } | |
5379 else if (type->isscalar()) | |
5380 { | |
5381 e = type->defaultInit(); | |
5382 e->loc = loc; | |
5383 return e; | |
5384 } | |
5385 } | |
5386 if (v->needThis()) | |
5387 { | |
5388 if (!eleft) | |
5389 eleft = new ThisExp(loc); | |
5390 e = new DotVarExp(loc, eleft, v); | |
5391 e = e->semantic(sc); | |
5392 } | |
5393 else | |
5394 { | |
5395 e = new VarExp(loc, v); | |
5396 if (eleft) | |
5397 { e = new CommaExp(loc, eleft, e); | |
5398 e->type = v->type; | |
5399 } | |
5400 } | |
5401 return e->deref(); | |
5402 } | |
5403 | |
5404 FuncDeclaration *f = s->isFuncDeclaration(); | |
5405 if (f) | |
5406 { | |
5407 //printf("it's a function\n"); | |
5408 if (f->needThis()) | |
5409 { | |
5410 if (!eleft) | |
5411 eleft = new ThisExp(loc); | |
5412 e = new DotVarExp(loc, eleft, f); | |
5413 e = e->semantic(sc); | |
5414 } | |
5415 else | |
5416 { | |
5417 e = new VarExp(loc, f); | |
5418 if (eleft) | |
5419 { e = new CommaExp(loc, eleft, e); | |
5420 e->type = f->type; | |
5421 } | |
5422 } | |
5423 return e; | |
5424 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5425 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5426 OverloadSet *o = s->isOverloadSet(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5427 if (o) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5428 { //printf("'%s' is an overload set\n", o->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5429 return new OverExp(o); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5430 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5431 #endif |
159 | 5432 |
5433 Type *t = s->getType(); | |
5434 if (t) | |
5435 { | |
5436 return new TypeExp(loc, t); | |
5437 } | |
5438 | |
336 | 5439 TupleDeclaration *tup = s->isTupleDeclaration(); |
5440 if (tup) | |
5441 { | |
5442 if (eleft) | |
5443 error("cannot have e.tuple"); | |
5444 e = new TupleExp(loc, tup); | |
5445 e = e->semantic(sc); | |
5446 return e; | |
5447 } | |
5448 | |
159 | 5449 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5450 if (sds) | |
5451 { | |
5452 //printf("it's a ScopeDsymbol\n"); | |
5453 e = new ScopeExp(loc, sds); | |
5454 e = e->semantic(sc); | |
5455 if (eleft) | |
5456 e = new DotExp(loc, eleft, e); | |
5457 return e; | |
5458 } | |
5459 | |
5460 Import *imp = s->isImport(); | |
5461 if (imp) | |
5462 { | |
5463 ScopeExp *ie; | |
5464 | |
5465 ie = new ScopeExp(loc, imp->pkg); | |
5466 return ie->semantic(sc); | |
5467 } | |
5468 | |
5469 // BUG: handle other cases like in IdentifierExp::semantic() | |
5470 #ifdef DEBUG | |
5471 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5472 #endif | |
5473 assert(0); | |
5474 } | |
5475 else if (ident == Id::stringof) | |
5476 { char *s = ie->toChars(); | |
5477 e = new StringExp(loc, s, strlen(s), 'c'); | |
5478 e = e->semantic(sc); | |
5479 return e; | |
5480 } | |
5481 error("undefined identifier %s", toChars()); | |
5482 type = Type::tvoid; | |
5483 return this; | |
5484 } | |
5485 else if (e1->type->ty == Tpointer && | |
5486 ident != Id::init && ident != Id::__sizeof && | |
5487 ident != Id::alignof && ident != Id::offsetof && | |
5488 ident != Id::mangleof && ident != Id::stringof) | |
5489 { | |
5490 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
|
5491 e->type = ((TypePointer *)e1->type)->next; |
159 | 5492 return e->type->dotExp(sc, e, ident); |
5493 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5494 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5495 else if (t1b->ty == Tarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5496 t1b->ty == Tsarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5497 t1b->ty == Taarray) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5498 { /* If ident is not a valid property, rewrite: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5499 * e1.ident |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5500 * as: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5501 * .ident(e1) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5502 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5503 unsigned errors = global.errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5504 global.gag++; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5505 e = e1->type->dotExp(sc, e1, ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5506 global.gag--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5507 if (errors != global.errors) // if failed to find the property |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5508 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5509 global.errors = errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5510 e = new DotIdExp(loc, new IdentifierExp(loc, Id::empty), ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5511 e = new CallExp(loc, e, e1); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5512 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5513 e = e->semantic(sc); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5514 return e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5515 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5516 #endif |
159 | 5517 else |
5518 { | |
5519 e = e1->type->dotExp(sc, e1, ident); | |
5520 e = e->semantic(sc); | |
5521 return e; | |
5522 } | |
5523 } | |
5524 | |
5525 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5526 { | |
5527 //printf("DotIdExp::toCBuffer()\n"); | |
5528 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5529 buf->writeByte('.'); | |
5530 buf->writestring(ident->toChars()); | |
5531 } | |
5532 | |
5533 /********************** DotTemplateExp ***********************************/ | |
5534 | |
5535 // Mainly just a placeholder | |
5536 | |
5537 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5538 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5539 | |
5540 { | |
5541 this->td = td; | |
5542 } | |
5543 | |
5544 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5545 { | |
5546 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5547 buf->writeByte('.'); | |
5548 buf->writestring(td->toChars()); | |
5549 } | |
5550 | |
5551 | |
5552 /************************************************************/ | |
5553 | |
5554 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5555 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5556 { | |
5557 //printf("DotVarExp()\n"); | |
5558 this->var = v; | |
5559 } | |
5560 | |
5561 Expression *DotVarExp::semantic(Scope *sc) | |
5562 { | |
5563 #if LOGSEMANTIC | |
5564 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5565 #endif | |
5566 if (!type) | |
5567 { | |
5568 var = var->toAlias()->isDeclaration(); | |
5569 | |
5570 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5571 if (tup) | |
5572 { /* Replace: | |
5573 * e1.tuple(a, b, c) | |
5574 * with: | |
5575 * tuple(e1.a, e1.b, e1.c) | |
5576 */ | |
5577 Expressions *exps = new Expressions; | |
5578 | |
5579 exps->reserve(tup->objects->dim); | |
5580 for (size_t i = 0; i < tup->objects->dim; i++) | |
5581 { Object *o = (Object *)tup->objects->data[i]; | |
5582 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5583 { | |
5584 error("%s is not an expression", o->toChars()); | |
5585 } | |
5586 else | |
5587 { | |
5588 Expression *e = (Expression *)o; | |
5589 if (e->op != TOKdsymbol) | |
5590 error("%s is not a member", e->toChars()); | |
5591 else | |
5592 { DsymbolExp *ve = (DsymbolExp *)e; | |
5593 | |
5594 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5595 exps->push(e); | |
5596 } | |
5597 } | |
5598 } | |
5599 Expression *e = new TupleExp(loc, exps); | |
5600 e = e->semantic(sc); | |
5601 return e; | |
5602 } | |
5603 | |
5604 e1 = e1->semantic(sc); | |
5605 type = var->type; | |
5606 if (!type && global.errors) | |
5607 { // var is goofed up, just return 0 | |
5608 return new IntegerExp(0); | |
5609 } | |
5610 assert(type); | |
5611 | |
5612 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5613 { | |
5614 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
336 | 5615 e1 = getRightThis(loc, sc, ad, e1, var); |
5616 if (!sc->noaccesscheck) | |
5617 accessCheck(loc, sc, e1, var); | |
5618 | |
5619 VarDeclaration *v = var->isVarDeclaration(); | |
5620 if (v && v->isConst()) | |
5621 { ExpInitializer *ei = v->getExpInitializer(); | |
5622 if (ei) | |
5623 { Expression *e = ei->exp->copy(); | |
5624 e = e->semantic(sc); | |
5625 return e; | |
159 | 5626 } |
5627 } | |
5628 } | |
5629 } | |
5630 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5631 return this; | |
5632 } | |
5633 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5634 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5635 int DotVarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5636 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5637 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5638 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5639 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5640 |
159 | 5641 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) |
5642 { | |
5643 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5644 return this; | |
5645 } | |
5646 | |
5647 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5648 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5649 #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
|
5650 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
|
5651 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
|
5652 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
|
5653 #endif |
159 | 5654 |
5655 if (var->isCtorinit()) | |
5656 { // It's only modifiable if inside the right constructor | |
5657 Dsymbol *s = sc->func; | |
5658 while (1) | |
5659 { | |
5660 FuncDeclaration *fd = NULL; | |
5661 if (s) | |
5662 fd = s->isFuncDeclaration(); | |
5663 if (fd && | |
5664 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5665 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5666 fd->toParent() == var->toParent() && | |
5667 e1->op == TOKthis | |
5668 ) | |
5669 { | |
5670 VarDeclaration *v = var->isVarDeclaration(); | |
5671 assert(v); | |
5672 v->ctorinit = 1; | |
5673 //printf("setting ctorinit\n"); | |
5674 } | |
5675 else | |
5676 { | |
5677 if (s) | |
5678 { s = s->toParent2(); | |
5679 continue; | |
5680 } | |
5681 else | |
5682 { | |
5683 const char *p = var->isStatic() ? "static " : ""; | |
5684 error("can only initialize %sconst member %s inside %sconstructor", | |
5685 p, var->toChars(), p); | |
5686 } | |
5687 } | |
5688 break; | |
5689 } | |
5690 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5691 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5692 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5693 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5694 Type *t1 = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5695 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5696 if (!t1->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5697 (t1->ty == Tpointer && !t1->nextOf()->isMutable()) || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5698 !var->type->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5699 !var->type->isAssignable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5700 var->storage_class & STCmanifest |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5701 ) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5702 error("cannot modify const/invariant %s", toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5703 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5704 #endif |
159 | 5705 return this; |
5706 } | |
5707 | |
5708 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5709 { | |
5710 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5711 buf->writeByte('.'); | |
5712 buf->writestring(var->toChars()); | |
5713 } | |
5714 | |
5715 /************************************************************/ | |
5716 | |
5717 /* Things like: | |
5718 * foo.bar!(args) | |
5719 */ | |
5720 | |
5721 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5722 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5723 { | |
5724 //printf("DotTemplateInstanceExp()\n"); | |
5725 this->ti = ti; | |
5726 } | |
5727 | |
5728 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5729 { | |
5730 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5731 e1->syntaxCopy(), | |
5732 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5733 return de; | |
5734 } | |
5735 | |
5736 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5737 { Dsymbol *s; | |
5738 Dsymbol *s2; | |
5739 TemplateDeclaration *td; | |
5740 Expression *e; | |
5741 Identifier *id; | |
5742 Type *t1; | |
5743 Expression *eleft = NULL; | |
5744 Expression *eright; | |
5745 | |
5746 #if LOGSEMANTIC | |
5747 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5748 #endif | |
5749 //e1->print(); | |
5750 //print(); | |
5751 e1 = e1->semantic(sc); | |
5752 t1 = e1->type; | |
5753 if (t1) | |
5754 t1 = t1->toBasetype(); | |
5755 //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
|
5756 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5757 /* 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
|
5758 * 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
|
5759 * 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
|
5760 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5761 |
159 | 5762 if (e1->op == TOKdotexp) |
5763 { DotExp *de = (DotExp *)e1; | |
5764 eleft = de->e1; | |
5765 eright = de->e2; | |
5766 } | |
5767 else | |
5768 { eleft = NULL; | |
5769 eright = e1; | |
5770 } | |
5771 if (eright->op == TOKimport) | |
5772 { | |
5773 s = ((ScopeExp *)eright)->sds; | |
5774 } | |
5775 else if (e1->op == TOKtype) | |
5776 { | |
5777 s = t1->isClassHandle(); | |
5778 if (!s) | |
5779 { if (t1->ty == Tstruct) | |
5780 s = ((TypeStruct *)t1)->sym; | |
5781 else | |
5782 goto L1; | |
5783 } | |
5784 } | |
5785 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5786 { | |
5787 s = t1->toDsymbol(sc); | |
5788 eleft = e1; | |
5789 } | |
5790 else if (t1 && t1->ty == Tpointer) | |
5791 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5792 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 5793 if (t1->ty != Tstruct) |
5794 goto L1; | |
5795 s = t1->toDsymbol(sc); | |
5796 eleft = e1; | |
5797 } | |
5798 else | |
5799 { | |
5800 L1: | |
5801 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5802 goto Lerr; | |
5803 } | |
5804 | |
5805 assert(s); | |
5806 id = ti->name; | |
5807 s2 = s->search(loc, id, 0); | |
5808 if (!s2) | |
5809 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5810 goto Lerr; | |
5811 } | |
5812 s = s2; | |
5813 s->semantic(sc); | |
5814 s = s->toAlias(); | |
5815 td = s->isTemplateDeclaration(); | |
5816 if (!td) | |
5817 { | |
5818 error("%s is not a template", id->toChars()); | |
5819 goto Lerr; | |
5820 } | |
5821 if (global.errors) | |
5822 goto Lerr; | |
5823 | |
5824 ti->tempdecl = td; | |
5825 | |
5826 if (eleft) | |
5827 { Declaration *v; | |
5828 | |
5829 ti->semantic(sc); | |
5830 s = ti->inst->toAlias(); | |
5831 v = s->isDeclaration(); | |
5832 if (v) | |
5833 { e = new DotVarExp(loc, eleft, v); | |
5834 e = e->semantic(sc); | |
5835 return e; | |
5836 } | |
5837 } | |
5838 | |
5839 e = new ScopeExp(loc, ti); | |
5840 if (eleft) | |
5841 { | |
5842 e = new DotExp(loc, eleft, e); | |
5843 } | |
5844 e = e->semantic(sc); | |
5845 return e; | |
5846 | |
5847 Lerr: | |
336 | 5848 return new IntegerExp(loc, 0, Type::tint32); |
159 | 5849 } |
5850 | |
5851 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5852 { | |
5853 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5854 buf->writeByte('.'); | |
5855 ti->toCBuffer(buf, hgs); | |
5856 } | |
5857 | |
5858 /************************************************************/ | |
5859 | |
5860 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5861 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5862 { | |
5863 this->func = f; | |
5864 } | |
5865 | |
5866 Expression *DelegateExp::semantic(Scope *sc) | |
5867 { | |
5868 #if LOGSEMANTIC | |
5869 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5870 #endif | |
5871 if (!type) | |
5872 { | |
5873 e1 = e1->semantic(sc); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
5874 // 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
|
5875 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 5876 type = type->semantic(loc, sc); |
5877 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 5878 if (func->needThis()) |
5879 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 5880 } |
5881 return this; | |
5882 } | |
5883 | |
5884 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5885 { | |
5886 buf->writeByte('&'); | |
5887 if (!func->isNested()) | |
5888 { | |
5889 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5890 buf->writeByte('.'); | |
5891 } | |
5892 buf->writestring(func->toChars()); | |
5893 } | |
5894 | |
5895 /************************************************************/ | |
5896 | |
5897 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5898 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5899 { | |
5900 this->sym = s; | |
5901 this->type = s->getType(); | |
5902 } | |
5903 | |
5904 Expression *DotTypeExp::semantic(Scope *sc) | |
5905 { | |
5906 #if LOGSEMANTIC | |
5907 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5908 #endif | |
5909 UnaExp::semantic(sc); | |
5910 return this; | |
5911 } | |
5912 | |
5913 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5914 { | |
5915 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5916 buf->writeByte('.'); | |
5917 buf->writestring(sym->toChars()); | |
5918 } | |
5919 | |
5920 /************************************************************/ | |
5921 | |
5922 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5923 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5924 { | |
5925 this->arguments = exps; | |
5926 } | |
5927 | |
5928 CallExp::CallExp(Loc loc, Expression *e) | |
5929 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5930 { | |
5931 this->arguments = NULL; | |
5932 } | |
5933 | |
5934 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5935 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5936 { | |
5937 Expressions *arguments = new Expressions(); | |
5938 arguments->setDim(1); | |
5939 arguments->data[0] = (void *)earg1; | |
5940 | |
5941 this->arguments = arguments; | |
5942 } | |
5943 | |
5944 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5945 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5946 { | |
5947 Expressions *arguments = new Expressions(); | |
5948 arguments->setDim(2); | |
5949 arguments->data[0] = (void *)earg1; | |
5950 arguments->data[1] = (void *)earg2; | |
5951 | |
5952 this->arguments = arguments; | |
5953 } | |
5954 | |
5955 Expression *CallExp::syntaxCopy() | |
5956 { | |
5957 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5958 } | |
5959 | |
5960 | |
5961 Expression *CallExp::semantic(Scope *sc) | |
5962 { | |
5963 TypeFunction *tf; | |
5964 FuncDeclaration *f; | |
5965 int i; | |
5966 Type *t1; | |
5967 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
|
5968 Objects *targsi = NULL; // initial list of template arguments |
159 | 5969 |
5970 #if LOGSEMANTIC | |
5971 printf("CallExp::semantic() %s\n", toChars()); | |
5972 #endif | |
5973 if (type) | |
5974 return this; // semantic() already run | |
5975 #if 0 | |
5976 if (arguments && arguments->dim) | |
5977 { | |
5978 Expression *earg = (Expression *)arguments->data[0]; | |
5979 earg->print(); | |
5980 if (earg->type) earg->type->print(); | |
5981 } | |
5982 #endif | |
5983 | |
5984 if (e1->op == TOKdelegate) | |
5985 { DelegateExp *de = (DelegateExp *)e1; | |
5986 | |
5987 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5988 return semantic(sc); | |
5989 } | |
5990 | |
5991 /* Transform: | |
5992 * array.id(args) into id(array,args) | |
5993 * aa.remove(arg) into delete aa[arg] | |
5994 */ | |
5995 if (e1->op == TOKdot) | |
5996 { | |
5997 // BUG: we should handle array.a.b.c.e(args) too | |
5998 | |
5999 DotIdExp *dotid = (DotIdExp *)(e1); | |
6000 dotid->e1 = dotid->e1->semantic(sc); | |
6001 assert(dotid->e1); | |
6002 if (dotid->e1->type) | |
6003 { | |
6004 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
6005 if (e1ty == Taarray && dotid->ident == Id::remove) | |
6006 { | |
6007 if (!arguments || arguments->dim != 1) | |
6008 { error("expected key as argument to aa.remove()"); | |
6009 goto Lagain; | |
6010 } | |
6011 Expression *key = (Expression *)arguments->data[0]; | |
6012 key = key->semantic(sc); | |
6013 key = resolveProperties(sc, key); | |
6014 key->rvalue(); | |
6015 | |
6016 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
6017 key = key->implicitCastTo(sc, taa->index); | |
6018 key = key->implicitCastTo(sc, taa->key); | |
6019 | |
6020 return new RemoveExp(loc, dotid->e1, key); | |
6021 } | |
6022 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
6023 { | |
6024 if (!arguments) | |
6025 arguments = new Expressions(); | |
6026 arguments->shift(dotid->e1); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6027 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6028 e1 = new DotIdExp(dotid->loc, new IdentifierExp(dotid->loc, Id::empty), dotid->ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6029 #else |
159 | 6030 e1 = new IdentifierExp(dotid->loc, dotid->ident); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6031 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6032 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6033 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6034 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6035 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6036 #if 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
|
6037 /* 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
|
6038 * 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
|
6039 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6040 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
|
6041 { 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
|
6042 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
|
6043 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
|
6044 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6045 /* 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
|
6046 * 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
|
6047 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6048 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
|
6049 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
|
6050 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
|
6051 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
|
6052 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
|
6053 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
|
6054 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6055 /* 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
|
6056 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6057 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
|
6058 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
|
6059 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
|
6060 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6061 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6062 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6063 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6064 /* 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
|
6065 * 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
|
6066 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6067 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
|
6068 { 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
|
6069 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
|
6070 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
|
6071 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6072 /* 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
|
6073 * 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
|
6074 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6075 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
|
6076 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
|
6077 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
|
6078 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
|
6079 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
|
6080 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
|
6081 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
|
6082 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6083 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
|
6084 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
|
6085 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
|
6086 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6087 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6088 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
|
6089 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6090 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6091 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6092 |
159 | 6093 istemp = 0; |
6094 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
|
6095 //printf("Lagain: %s\n", toChars()); |
159 | 6096 f = NULL; |
6097 if (e1->op == TOKthis || e1->op == TOKsuper) | |
6098 { | |
6099 // semantic() run later for these | |
6100 } | |
6101 else | |
6102 { | |
6103 UnaExp::semantic(sc); | |
6104 | |
6105 /* Look for e1 being a lazy parameter | |
6106 */ | |
6107 if (e1->op == TOKvar) | |
6108 { VarExp *ve = (VarExp *)e1; | |
6109 | |
6110 if (ve->var->storage_class & STClazy) | |
6111 { | |
6112 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
6113 TypeDelegate *t = new TypeDelegate(tf); | |
6114 ve->type = t->semantic(loc, sc); | |
6115 } | |
6116 } | |
6117 | |
6118 if (e1->op == TOKimport) | |
6119 { // Perhaps this should be moved to ScopeExp::semantic() | |
6120 ScopeExp *se = (ScopeExp *)e1; | |
6121 e1 = new DsymbolExp(loc, se->sds); | |
6122 e1 = e1->semantic(sc); | |
6123 } | |
6124 #if 1 // patch for #540 by Oskar Linde | |
6125 else if (e1->op == TOKdotexp) | |
6126 { | |
6127 DotExp *de = (DotExp *) e1; | |
6128 | |
6129 if (de->e2->op == TOKimport) | |
6130 { // This should *really* be moved to ScopeExp::semantic() | |
6131 ScopeExp *se = (ScopeExp *)de->e2; | |
6132 de->e2 = new DsymbolExp(loc, se->sds); | |
6133 de->e2 = de->e2->semantic(sc); | |
6134 } | |
6135 | |
6136 if (de->e2->op == TOKtemplate) | |
6137 { TemplateExp *te = (TemplateExp *) de->e2; | |
6138 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
6139 } | |
6140 } | |
6141 #endif | |
6142 } | |
6143 | |
6144 if (e1->op == TOKcomma) | |
6145 { | |
6146 CommaExp *ce = (CommaExp *)e1; | |
6147 | |
6148 e1 = ce->e2; | |
6149 e1->type = ce->type; | |
6150 ce->e2 = this; | |
6151 ce->type = NULL; | |
6152 return ce->semantic(sc); | |
6153 } | |
6154 | |
6155 t1 = NULL; | |
6156 if (e1->type) | |
6157 t1 = e1->type->toBasetype(); | |
6158 | |
6159 // Check for call operator overload | |
6160 if (t1) | |
6161 { AggregateDeclaration *ad; | |
6162 | |
6163 if (t1->ty == Tstruct) | |
6164 { | |
6165 ad = ((TypeStruct *)t1)->sym; | |
6166 if (search_function(ad, Id::call)) | |
6167 goto L1; // overload of opCall, therefore it's a call | |
6168 | |
6169 if (e1->op != TOKtype) | |
6170 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
6171 /* It's a struct literal | |
6172 */ | |
6173 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
6174 e = e->semantic(sc); | |
6175 e->type = e1->type; // in case e1->type was a typedef | |
6176 return e; | |
6177 } | |
6178 else if (t1->ty == Tclass) | |
6179 { | |
6180 ad = ((TypeClass *)t1)->sym; | |
6181 goto L1; | |
6182 L1: | |
6183 // Rewrite as e1.call(arguments) | |
6184 Expression *e = new DotIdExp(loc, e1, Id::call); | |
6185 e = new CallExp(loc, e, arguments); | |
6186 e = e->semantic(sc); | |
6187 return e; | |
6188 } | |
6189 } | |
6190 | |
6191 arrayExpressionSemantic(arguments, sc); | |
6192 preFunctionArguments(loc, sc, arguments); | |
6193 | |
6194 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
6195 e1->op == TOKdottd) | |
6196 { | |
6197 DotVarExp *dve; | |
6198 DotTemplateExp *dte; | |
6199 AggregateDeclaration *ad; | |
6200 UnaExp *ue = (UnaExp *)(e1); | |
336 | 6201 |
159 | 6202 if (e1->op == TOKdotvar) |
6203 { // Do overload resolution | |
6204 dve = (DotVarExp *)(e1); | |
6205 | |
6206 f = dve->var->isFuncDeclaration(); | |
6207 assert(f); | |
6208 f = f->overloadResolve(loc, arguments); | |
6209 | |
6210 ad = f->toParent()->isAggregateDeclaration(); | |
6211 } | |
6212 else | |
6213 { dte = (DotTemplateExp *)(e1); | |
6214 TemplateDeclaration *td = dte->td; | |
6215 assert(td); | |
6216 if (!arguments) | |
6217 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
6218 arguments = new Expressions(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6219 f = td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6220 if (!f) |
6221 { type = Type::terror; | |
6222 return this; | |
6223 } | |
6224 ad = td->toParent()->isAggregateDeclaration(); | |
6225 } | |
336 | 6226 if (f->needThis()) |
6227 { | |
6228 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
6229 } | |
6230 | |
6231 /* Cannot call public functions from inside invariant | |
6232 * (because then the invariant would have infinite recursion) | |
159 | 6233 */ |
336 | 6234 if (sc->func && sc->func->isInvariantDeclaration() && |
6235 ue->e1->op == TOKthis && | |
6236 f->addPostInvariant() | |
159 | 6237 ) |
6238 { | |
336 | 6239 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 6240 } |
6241 | |
6242 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6243 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6244 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6245 #endif |
159 | 6246 accessCheck(loc, sc, ue->e1, f); |
6247 if (!f->needThis()) | |
6248 { | |
6249 VarExp *ve = new VarExp(loc, f); | |
6250 e1 = new CommaExp(loc, ue->e1, ve); | |
6251 e1->type = f->type; | |
6252 } | |
6253 else | |
6254 { | |
6255 if (e1->op == TOKdotvar) | |
6256 dve->var = f; | |
6257 else | |
6258 e1 = new DotVarExp(loc, dte->e1, f); | |
6259 e1->type = f->type; | |
6260 | |
6261 // See if we need to adjust the 'this' pointer | |
6262 AggregateDeclaration *ad = f->isThis(); | |
6263 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
6264 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
6265 ue->e1->op != TOKsuper) | |
6266 { | |
6267 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
6268 ue->e1 = ue->e1->semantic(sc); | |
6269 } | |
6270 } | |
6271 t1 = e1->type; | |
6272 } | |
6273 else if (e1->op == TOKsuper) | |
6274 { | |
6275 // Base class constructor call | |
6276 ClassDeclaration *cd = NULL; | |
6277 | |
6278 if (sc->func) | |
6279 cd = sc->func->toParent()->isClassDeclaration(); | |
6280 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
6281 { | |
6282 error("super class constructor call must be in a constructor"); | |
6283 type = Type::terror; | |
6284 return this; | |
6285 } | |
6286 else | |
6287 { | |
6288 f = cd->baseClass->ctor; | |
6289 if (!f) | |
6290 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
6291 type = Type::terror; | |
6292 return this; | |
6293 } | |
6294 else | |
6295 { | |
336 | 6296 if (!sc->intypeof) |
6297 { | |
159 | 6298 #if 0 |
336 | 6299 if (sc->callSuper & (CSXthis | CSXsuper)) |
6300 error("reference to this before super()"); | |
159 | 6301 #endif |
336 | 6302 if (sc->noctor || sc->callSuper & CSXlabel) |
6303 error("constructor calls not allowed in loops or after labels"); | |
6304 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6305 error("multiple constructor calls"); | |
6306 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
6307 } | |
159 | 6308 |
6309 f = f->overloadResolve(loc, arguments); | |
6310 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6311 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6312 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6313 #endif |
159 | 6314 e1 = new DotVarExp(e1->loc, e1, f); |
6315 e1 = e1->semantic(sc); | |
6316 t1 = e1->type; | |
6317 } | |
6318 } | |
6319 } | |
6320 else if (e1->op == TOKthis) | |
6321 { | |
6322 // same class constructor call | |
6323 ClassDeclaration *cd = NULL; | |
6324 | |
6325 if (sc->func) | |
6326 cd = sc->func->toParent()->isClassDeclaration(); | |
6327 if (!cd || !sc->func->isCtorDeclaration()) | |
6328 { | |
6329 error("class constructor call must be in a constructor"); | |
6330 type = Type::terror; | |
6331 return this; | |
6332 } | |
6333 else | |
6334 { | |
336 | 6335 if (!sc->intypeof) |
6336 { | |
159 | 6337 #if 0 |
336 | 6338 if (sc->callSuper & (CSXthis | CSXsuper)) |
6339 error("reference to this before super()"); | |
159 | 6340 #endif |
336 | 6341 if (sc->noctor || sc->callSuper & CSXlabel) |
6342 error("constructor calls not allowed in loops or after labels"); | |
6343 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6344 error("multiple constructor calls"); | |
6345 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6346 } | |
159 | 6347 |
6348 f = cd->ctor; | |
6349 f = f->overloadResolve(loc, arguments); | |
6350 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6351 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6352 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6353 #endif |
159 | 6354 e1 = new DotVarExp(e1->loc, e1, f); |
6355 e1 = e1->semantic(sc); | |
6356 t1 = e1->type; | |
6357 | |
6358 // BUG: this should really be done by checking the static | |
6359 // call graph | |
6360 if (f == sc->func) | |
6361 error("cyclic constructor call"); | |
6362 } | |
6363 } | |
6364 else if (!t1) | |
6365 { | |
6366 error("function expected before (), not '%s'", e1->toChars()); | |
6367 type = Type::terror; | |
6368 return this; | |
6369 } | |
6370 else if (t1->ty != Tfunction) | |
6371 { | |
6372 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
|
6373 { 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
|
6374 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
|
6375 tf = (TypeFunction *)(td->next); |
159 | 6376 goto Lcheckargs; |
6377 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6378 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6379 { Expression *e; |
6380 | |
6381 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
|
6382 t1 = ((TypePointer *)t1)->next; |
159 | 6383 e->type = t1; |
6384 e1 = e; | |
6385 } | |
6386 else if (e1->op == TOKtemplate) | |
6387 { | |
6388 TemplateExp *te = (TemplateExp *)e1; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6389 f = te->td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6390 if (!f) |
6391 { type = Type::terror; | |
6392 return this; | |
6393 } | |
6394 if (f->needThis() && hasThis(sc)) | |
6395 { | |
6396 // Supply an implicit 'this', as in | |
6397 // this.ident | |
6398 | |
6399 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6400 goto Lagain; | |
6401 } | |
6402 | |
6403 e1 = new VarExp(loc, f); | |
6404 goto Lagain; | |
6405 } | |
6406 else | |
6407 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6408 type = Type::terror; | |
6409 return this; | |
6410 } | |
6411 } | |
6412 else if (e1->op == TOKvar) | |
6413 { | |
6414 // Do overload resolution | |
6415 VarExp *ve = (VarExp *)e1; | |
6416 | |
6417 f = ve->var->isFuncDeclaration(); | |
6418 assert(f); | |
6419 | |
6420 // Look to see if f is really a function template | |
6421 if (0 && !istemp && f->parent) | |
6422 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6423 | |
6424 if (ti && | |
6425 (ti->name == f->ident || | |
6426 ti->toAlias()->ident == f->ident) | |
6427 && | |
6428 ti->tempdecl) | |
6429 { | |
6430 /* This is so that one can refer to the enclosing | |
6431 * template, even if it has the same name as a member | |
6432 * of the template, if it has a !(arguments) | |
6433 */ | |
6434 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6435 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6436 tempdecl = tempdecl->overroot; // then get the start | |
6437 e1 = new TemplateExp(loc, tempdecl); | |
6438 istemp = 1; | |
6439 goto Lagain; | |
6440 } | |
6441 } | |
6442 | |
6443 f = f->overloadResolve(loc, arguments); | |
6444 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6445 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6446 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6447 #endif |
159 | 6448 |
6449 if (f->needThis() && hasThis(sc)) | |
6450 { | |
6451 // Supply an implicit 'this', as in | |
6452 // this.ident | |
6453 | |
6454 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6455 goto Lagain; | |
6456 } | |
6457 | |
6458 accessCheck(loc, sc, NULL, f); | |
6459 | |
6460 ve->var = f; | |
6461 ve->type = f->type; | |
6462 t1 = f->type; | |
6463 } | |
6464 assert(t1->ty == Tfunction); | |
6465 tf = (TypeFunction *)(t1); | |
6466 | |
6467 Lcheckargs: | |
6468 assert(tf->ty == Tfunction); | |
6469 type = tf->next; | |
6470 | |
6471 if (!arguments) | |
6472 arguments = new Expressions(); | |
6473 functionArguments(loc, sc, tf, arguments); | |
6474 | |
6475 assert(type); | |
6476 | |
6477 if (f && f->tintro) | |
6478 { | |
6479 Type *t = type; | |
6480 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
|
6481 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
|
6482 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6483 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
|
6484 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6485 type = tf->next; |
159 | 6486 return castTo(sc, t); |
6487 } | |
6488 } | |
6489 | |
6490 return this; | |
6491 } | |
6492 | |
6493 int CallExp::checkSideEffect(int flag) | |
6494 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6495 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6496 if (flag != 2) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6497 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6498 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6499 if (e1->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6500 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6501 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6502 /* If any of the arguments have side effects, this expression does |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6503 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6504 for (size_t i = 0; i < arguments->dim; i++) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6505 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6506 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6507 if (e->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6508 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6509 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6510 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6511 /* If calling a function or delegate that is typed as pure, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6512 * then this expression has no side effects. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6513 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6514 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6515 if (t->ty == Tfunction && ((TypeFunction *)t)->ispure) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6516 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6517 if (t->ty == Tdelegate && ((TypeFunction *)((TypeDelegate *)t)->next)->ispure) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6518 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6519 #endif |
159 | 6520 return 1; |
6521 } | |
6522 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6523 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6524 int CallExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6525 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6526 if (e1->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6527 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6528 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6529 /* If any of the arguments can throw, then this expression can throw |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6530 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6531 for (size_t i = 0; i < arguments->dim; i++) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6532 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6533 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6534 if (e->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6535 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6536 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6537 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6538 /* If calling a function or delegate that is typed as nothrow, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6539 * then this expression cannot throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6540 * Note that pure functions can throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6541 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6542 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6543 if (t->ty == Tfunction && ((TypeFunction *)t)->isnothrow) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6544 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6545 if (t->ty == Tdelegate && ((TypeFunction *)((TypeDelegate *)t)->next)->isnothrow) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6546 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6547 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6548 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6549 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6550 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6551 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6552 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6553 int CallExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6554 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6555 if (type->toBasetype()->ty == Tstruct) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6556 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6557 Type *tb = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6558 if (tb->ty == Tfunction && ((TypeFunction *)tb)->isref) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6559 return 1; // function returns a reference |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6560 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6561 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6562 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6563 |
159 | 6564 Expression *CallExp::toLvalue(Scope *sc, Expression *e) |
6565 { | |
6566 if (type->toBasetype()->ty == Tstruct) | |
6567 return this; | |
6568 else | |
6569 return Expression::toLvalue(sc, e); | |
6570 } | |
6571 | |
6572 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6573 { int i; | |
6574 | |
6575 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6576 buf->writeByte('('); | |
6577 argsToCBuffer(buf, arguments, hgs); | |
6578 buf->writeByte(')'); | |
6579 } | |
6580 | |
6581 | |
6582 /************************************************************/ | |
6583 | |
6584 AddrExp::AddrExp(Loc loc, Expression *e) | |
6585 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6586 { | |
6587 } | |
6588 | |
6589 Expression *AddrExp::semantic(Scope *sc) | |
6590 { | |
6591 #if LOGSEMANTIC | |
6592 printf("AddrExp::semantic('%s')\n", toChars()); | |
6593 #endif | |
6594 if (!type) | |
6595 { | |
6596 UnaExp::semantic(sc); | |
6597 e1 = e1->toLvalue(sc, NULL); | |
6598 if (!e1->type) | |
6599 { | |
6600 error("cannot take address of %s", e1->toChars()); | |
6601 type = Type::tint32; | |
6602 return this; | |
6603 } | |
6604 type = e1->type->pointerTo(); | |
6605 | |
6606 // See if this should really be a delegate | |
6607 if (e1->op == TOKdotvar) | |
6608 { | |
6609 DotVarExp *dve = (DotVarExp *)e1; | |
6610 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6611 | |
6612 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
|
6613 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6614 e = e->semantic(sc); |
6615 return e; | |
6616 } | |
6617 } | |
6618 else if (e1->op == TOKvar) | |
6619 { | |
6620 VarExp *dve = (VarExp *)e1; | |
6621 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6622 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6623 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6624 // 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
|
6625 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
|
6626 { |
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
|
6627 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
|
6628 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
|
6629 } |
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
|
6630 |
159 | 6631 if (f && f->isNested()) |
6632 { Expression *e; | |
6633 | |
6634 e = new DelegateExp(loc, e1, f); | |
6635 e = e->semantic(sc); | |
6636 return e; | |
6637 } | |
6638 } | |
6639 else if (e1->op == TOKarray) | |
6640 { | |
6641 if (e1->type->toBasetype()->ty == Tbit) | |
6642 error("cannot take address of bit in array"); | |
6643 } | |
6644 return optimize(WANTvalue); | |
6645 } | |
6646 return this; | |
6647 } | |
6648 | |
6649 /************************************************************/ | |
6650 | |
6651 PtrExp::PtrExp(Loc loc, Expression *e) | |
6652 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6653 { | |
6654 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
|
6655 type = ((TypePointer *)e->type)->next; |
159 | 6656 } |
6657 | |
6658 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6659 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6660 { | |
6661 type = t; | |
6662 } | |
6663 | |
6664 Expression *PtrExp::semantic(Scope *sc) | |
6665 { Type *tb; | |
6666 | |
6667 #if LOGSEMANTIC | |
6668 printf("PtrExp::semantic('%s')\n", toChars()); | |
6669 #endif | |
6670 UnaExp::semantic(sc); | |
6671 e1 = resolveProperties(sc, e1); | |
6672 if (type) | |
6673 return this; | |
6674 if (!e1->type) | |
6675 printf("PtrExp::semantic('%s')\n", toChars()); | |
6676 tb = e1->type->toBasetype(); | |
6677 switch (tb->ty) | |
6678 { | |
6679 case Tpointer: | |
6680 type = tb->next; | |
6681 if (type->isbit()) | |
6682 { Expression *e; | |
6683 | |
6684 // Rewrite *p as p[0] | |
6685 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
6686 return e->semantic(sc); | |
6687 } | |
6688 break; | |
6689 | |
6690 case Tsarray: | |
6691 case Tarray: | |
6692 type = tb->next; | |
6693 e1 = e1->castTo(sc, type->pointerTo()); | |
6694 break; | |
6695 | |
6696 default: | |
6697 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
6698 type = Type::tint32; | |
6699 break; | |
6700 } | |
6701 rvalue(); | |
6702 return this; | |
6703 } | |
6704 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6705 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6706 int PtrExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6707 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6708 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6709 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6710 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6711 |
159 | 6712 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) |
6713 { | |
6714 #if 0 | |
6715 tym = tybasic(e1->ET->Tty); | |
6716 if (!(tyscalar(tym) || | |
6717 tym == TYstruct || | |
6718 tym == TYarray && e->Eoper == TOKaddr)) | |
6719 synerr(EM_lvalue); // lvalue expected | |
6720 #endif | |
6721 return this; | |
6722 } | |
6723 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6724 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6725 Expression *PtrExp::modifiableLvalue(Scope *sc, Expression *e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6726 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6727 //printf("PtrExp::modifiableLvalue() %s, type %s\n", toChars(), type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6728 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6729 if (e1->op == TOKsymoff) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6730 { SymOffExp *se = (SymOffExp *)e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6731 se->var->checkModify(loc, sc, type); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6732 //return toLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6733 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6734 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6735 return Expression::modifiableLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6736 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6737 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6738 |
159 | 6739 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
6740 { | |
6741 buf->writeByte('*'); | |
6742 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6743 } | |
6744 | |
6745 /************************************************************/ | |
6746 | |
6747 NegExp::NegExp(Loc loc, Expression *e) | |
6748 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
6749 { | |
6750 } | |
6751 | |
6752 Expression *NegExp::semantic(Scope *sc) | |
6753 { Expression *e; | |
6754 | |
6755 #if LOGSEMANTIC | |
6756 printf("NegExp::semantic('%s')\n", toChars()); | |
6757 #endif | |
6758 if (!type) | |
6759 { | |
6760 UnaExp::semantic(sc); | |
6761 e1 = resolveProperties(sc, e1); | |
6762 e = op_overload(sc); | |
6763 if (e) | |
6764 return e; | |
6765 | |
6766 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
|
6767 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
|
6768 e1->checkArithmetic(); |
159 | 6769 type = e1->type; |
6770 } | |
6771 return this; | |
6772 } | |
6773 | |
6774 /************************************************************/ | |
6775 | |
6776 UAddExp::UAddExp(Loc loc, Expression *e) | |
6777 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
6778 { | |
6779 } | |
6780 | |
6781 Expression *UAddExp::semantic(Scope *sc) | |
6782 { Expression *e; | |
6783 | |
6784 #if LOGSEMANTIC | |
6785 printf("UAddExp::semantic('%s')\n", toChars()); | |
6786 #endif | |
6787 assert(!type); | |
6788 UnaExp::semantic(sc); | |
6789 e1 = resolveProperties(sc, e1); | |
6790 e = op_overload(sc); | |
6791 if (e) | |
6792 return e; | |
6793 e1->checkNoBool(); | |
6794 e1->checkArithmetic(); | |
6795 return e1; | |
6796 } | |
6797 | |
6798 /************************************************************/ | |
6799 | |
6800 ComExp::ComExp(Loc loc, Expression *e) | |
6801 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6802 { | |
6803 } | |
6804 | |
6805 Expression *ComExp::semantic(Scope *sc) | |
6806 { Expression *e; | |
6807 | |
6808 if (!type) | |
6809 { | |
6810 UnaExp::semantic(sc); | |
6811 e1 = resolveProperties(sc, e1); | |
6812 e = op_overload(sc); | |
6813 if (e) | |
6814 return e; | |
6815 | |
6816 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
|
6817 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
|
6818 e1 = e1->checkIntegral(); |
159 | 6819 type = e1->type; |
6820 } | |
6821 return this; | |
6822 } | |
6823 | |
6824 /************************************************************/ | |
6825 | |
6826 NotExp::NotExp(Loc loc, Expression *e) | |
6827 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6828 { | |
6829 } | |
6830 | |
6831 Expression *NotExp::semantic(Scope *sc) | |
6832 { | |
6833 UnaExp::semantic(sc); | |
6834 e1 = resolveProperties(sc, e1); | |
6835 e1 = e1->checkToBoolean(); | |
6836 type = Type::tboolean; | |
6837 return this; | |
6838 } | |
6839 | |
6840 int NotExp::isBit() | |
6841 { | |
6842 return TRUE; | |
6843 } | |
6844 | |
6845 | |
6846 | |
6847 /************************************************************/ | |
6848 | |
6849 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6850 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6851 { | |
6852 type = t; | |
6853 } | |
6854 | |
6855 Expression *BoolExp::semantic(Scope *sc) | |
6856 { | |
6857 UnaExp::semantic(sc); | |
6858 e1 = resolveProperties(sc, e1); | |
6859 e1 = e1->checkToBoolean(); | |
6860 type = Type::tboolean; | |
6861 return this; | |
6862 } | |
6863 | |
6864 int BoolExp::isBit() | |
6865 { | |
6866 return TRUE; | |
6867 } | |
6868 | |
6869 /************************************************************/ | |
6870 | |
6871 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6872 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6873 { | |
6874 } | |
6875 | |
6876 Expression *DeleteExp::semantic(Scope *sc) | |
6877 { | |
6878 Type *tb; | |
6879 | |
6880 UnaExp::semantic(sc); | |
6881 e1 = resolveProperties(sc, e1); | |
6882 e1 = e1->toLvalue(sc, NULL); | |
6883 type = Type::tvoid; | |
6884 | |
6885 tb = e1->type->toBasetype(); | |
6886 switch (tb->ty) | |
6887 { case Tclass: | |
6888 { TypeClass *tc = (TypeClass *)tb; | |
6889 ClassDeclaration *cd = tc->sym; | |
6890 | |
6891 if (cd->isCOMinterface()) | |
6892 { /* Because COM classes are deleted by IUnknown.Release() | |
6893 */ | |
6894 error("cannot delete instance of COM interface %s", cd->toChars()); | |
6895 } | |
6896 break; | |
6897 } | |
6898 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
|
6899 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 6900 if (tb->ty == Tstruct) |
6901 { | |
6902 TypeStruct *ts = (TypeStruct *)tb; | |
6903 StructDeclaration *sd = ts->sym; | |
6904 FuncDeclaration *f = sd->aggDelete; | |
6905 | |
6906 if (f) | |
6907 { | |
6908 Type *tpv = Type::tvoid->pointerTo(); | |
6909 | |
6910 Expression *e = e1->castTo(sc, tpv); | |
6911 Expression *ec = new VarExp(loc, f); | |
6912 e = new CallExp(loc, ec, e); | |
6913 return e->semantic(sc); | |
6914 } | |
6915 } | |
6916 break; | |
6917 | |
6918 case Tarray: | |
6919 break; | |
6920 | |
6921 default: | |
6922 if (e1->op == TOKindex) | |
6923 { | |
6924 IndexExp *ae = (IndexExp *)(e1); | |
6925 Type *tb1 = ae->e1->type->toBasetype(); | |
6926 if (tb1->ty == Taarray) | |
6927 break; | |
6928 } | |
6929 error("cannot delete type %s", e1->type->toChars()); | |
6930 break; | |
6931 } | |
6932 | |
6933 if (e1->op == TOKindex) | |
6934 { | |
6935 IndexExp *ae = (IndexExp *)(e1); | |
6936 Type *tb1 = ae->e1->type->toBasetype(); | |
6937 if (tb1->ty == Taarray) | |
6938 { if (!global.params.useDeprecated) | |
6939 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6940 } | |
6941 } | |
6942 | |
6943 return this; | |
6944 } | |
6945 | |
6946 int DeleteExp::checkSideEffect(int flag) | |
6947 { | |
6948 return 1; | |
6949 } | |
6950 | |
6951 Expression *DeleteExp::checkToBoolean() | |
6952 { | |
6953 error("delete does not give a boolean result"); | |
6954 return this; | |
6955 } | |
6956 | |
6957 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6958 { | |
6959 buf->writestring("delete "); | |
6960 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6961 } | |
6962 | |
6963 /************************************************************/ | |
6964 | |
6965 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6966 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6967 { | |
6968 to = t; | |
6969 } | |
6970 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6971 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6972 /* For cast(const) and cast(immutable) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6973 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6974 CastExp::CastExp(Loc loc, Expression *e, unsigned mod) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6975 : UnaExp(loc, TOKcast, sizeof(CastExp), e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6976 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6977 to = NULL; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6978 this->mod = mod; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6979 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6980 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6981 |
159 | 6982 Expression *CastExp::syntaxCopy() |
6983 { | |
6984 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6985 } | |
6986 | |
6987 | |
6988 Expression *CastExp::semantic(Scope *sc) | |
6989 { Expression *e; | |
6990 BinExp *b; | |
6991 UnaExp *u; | |
6992 | |
6993 #if LOGSEMANTIC | |
6994 printf("CastExp::semantic('%s')\n", toChars()); | |
6995 #endif | |
6996 | |
6997 //static int x; assert(++x < 10); | |
6998 | |
6999 if (type) | |
7000 return this; | |
7001 UnaExp::semantic(sc); | |
7002 if (e1->type) // if not a tuple | |
7003 { | |
7004 e1 = resolveProperties(sc, e1); | |
7005 to = to->semantic(loc, sc); | |
7006 | |
7007 e = op_overload(sc); | |
7008 if (e) | |
7009 { | |
7010 return e->implicitCastTo(sc, to); | |
7011 } | |
7012 | |
7013 Type *tob = to->toBasetype(); | |
7014 if (tob->ty == Tstruct && | |
7015 !tob->equals(e1->type->toBasetype()) && | |
7016 ((TypeStruct *)to)->sym->search(0, Id::call, 0) | |
7017 ) | |
7018 { | |
7019 /* Look to replace: | |
7020 * cast(S)t | |
7021 * with: | |
7022 * S(t) | |
7023 */ | |
7024 | |
7025 // Rewrite as to.call(e1) | |
7026 e = new TypeExp(loc, to); | |
7027 e = new DotIdExp(loc, e, Id::call); | |
7028 e = new CallExp(loc, e, e1); | |
7029 e = e->semantic(sc); | |
7030 return e; | |
7031 } | |
7032 } | |
7033 e = e1->castTo(sc, to); | |
7034 return e; | |
7035 } | |
7036 | |
7037 int CastExp::checkSideEffect(int flag) | |
7038 { | |
7039 /* if not: | |
7040 * cast(void) | |
7041 * cast(classtype)func() | |
7042 */ | |
7043 if (!to->equals(Type::tvoid) && | |
7044 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
7045 return Expression::checkSideEffect(flag); | |
7046 return 1; | |
7047 } | |
7048 | |
7049 void CastExp::checkEscape() | |
7050 { Type *tb = type->toBasetype(); | |
7051 if (tb->ty == Tarray && e1->op == TOKvar && | |
7052 e1->type->toBasetype()->ty == Tsarray) | |
7053 { VarExp *ve = (VarExp *)e1; | |
7054 VarDeclaration *v = ve->var->isVarDeclaration(); | |
7055 if (v) | |
7056 { | |
7057 if (!v->isDataseg() && !v->isParameter()) | |
7058 error("escaping reference to local %s", v->toChars()); | |
7059 } | |
7060 } | |
7061 } | |
7062 | |
7063 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7064 { | |
7065 buf->writestring("cast("); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7066 #if DMDV1 |
159 | 7067 to->toCBuffer(buf, NULL, hgs); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7068 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7069 if (to) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7070 to->toCBuffer(buf, NULL, hgs); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7071 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7072 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7073 switch (mod) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7074 { case 0: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7075 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7076 case MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7077 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7078 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7079 case MODinvariant: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7080 buf->writestring(Token::tochars[TOKimmutable]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7081 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7082 case MODshared: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7083 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7084 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7085 case MODshared | MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7086 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7087 buf->writeByte(' '); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7088 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7089 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7090 default: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7091 assert(0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7092 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7093 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7094 #endif |
159 | 7095 buf->writeByte(')'); |
7096 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7097 } | |
7098 | |
7099 | |
7100 /************************************************************/ | |
7101 | |
7102 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
7103 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
7104 { | |
7105 this->upr = upr; | |
7106 this->lwr = lwr; | |
7107 lengthVar = NULL; | |
7108 } | |
7109 | |
7110 Expression *SliceExp::syntaxCopy() | |
7111 { | |
7112 Expression *lwr = NULL; | |
7113 if (this->lwr) | |
7114 lwr = this->lwr->syntaxCopy(); | |
7115 | |
7116 Expression *upr = NULL; | |
7117 if (this->upr) | |
7118 upr = this->upr->syntaxCopy(); | |
7119 | |
7120 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
7121 } | |
7122 | |
7123 Expression *SliceExp::semantic(Scope *sc) | |
7124 { Expression *e; | |
7125 AggregateDeclaration *ad; | |
7126 //FuncDeclaration *fd; | |
7127 ScopeDsymbol *sym; | |
7128 | |
7129 #if LOGSEMANTIC | |
7130 printf("SliceExp::semantic('%s')\n", toChars()); | |
7131 #endif | |
7132 if (type) | |
7133 return this; | |
7134 | |
7135 UnaExp::semantic(sc); | |
7136 e1 = resolveProperties(sc, e1); | |
7137 | |
7138 e = this; | |
7139 | |
7140 Type *t = e1->type->toBasetype(); | |
7141 if (t->ty == Tpointer) | |
7142 { | |
7143 if (!lwr || !upr) | |
7144 error("need upper and lower bound to slice pointer"); | |
7145 } | |
7146 else if (t->ty == Tarray) | |
7147 { | |
7148 } | |
7149 else if (t->ty == Tsarray) | |
7150 { | |
7151 } | |
7152 else if (t->ty == Tclass) | |
7153 { | |
7154 ad = ((TypeClass *)t)->sym; | |
7155 goto L1; | |
7156 } | |
7157 else if (t->ty == Tstruct) | |
7158 { | |
7159 ad = ((TypeStruct *)t)->sym; | |
7160 | |
7161 L1: | |
7162 if (search_function(ad, Id::slice)) | |
7163 { | |
7164 // Rewrite as e1.slice(lwr, upr) | |
7165 e = new DotIdExp(loc, e1, Id::slice); | |
7166 | |
7167 if (lwr) | |
7168 { | |
7169 assert(upr); | |
7170 e = new CallExp(loc, e, lwr, upr); | |
7171 } | |
7172 else | |
7173 { assert(!upr); | |
7174 e = new CallExp(loc, e); | |
7175 } | |
7176 e = e->semantic(sc); | |
7177 return e; | |
7178 } | |
7179 goto Lerror; | |
7180 } | |
7181 else if (t->ty == Ttuple) | |
7182 { | |
7183 if (!lwr && !upr) | |
7184 return e1; | |
7185 if (!lwr || !upr) | |
7186 { error("need upper and lower bound to slice tuple"); | |
7187 goto Lerror; | |
7188 } | |
7189 } | |
7190 else | |
7191 goto Lerror; | |
7192 | |
7193 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7194 { | |
7195 sym = new ArrayScopeSymbol(this); | |
7196 sym->loc = loc; | |
7197 sym->parent = sc->scopesym; | |
7198 sc = sc->push(sym); | |
7199 } | |
7200 | |
7201 if (lwr) | |
7202 { lwr = lwr->semantic(sc); | |
7203 lwr = resolveProperties(sc, lwr); | |
7204 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
7205 } | |
7206 if (upr) | |
7207 { upr = upr->semantic(sc); | |
7208 upr = resolveProperties(sc, upr); | |
7209 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
7210 } | |
7211 | |
7212 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7213 sc->pop(); | |
7214 | |
7215 if (t->ty == Ttuple) | |
7216 { | |
7217 lwr = lwr->optimize(WANTvalue); | |
7218 upr = upr->optimize(WANTvalue); | |
7219 uinteger_t i1 = lwr->toUInteger(); | |
7220 uinteger_t i2 = upr->toUInteger(); | |
7221 | |
7222 size_t length; | |
7223 TupleExp *te; | |
7224 TypeTuple *tup; | |
7225 | |
7226 if (e1->op == TOKtuple) // slicing an expression tuple | |
7227 { te = (TupleExp *)e1; | |
7228 length = te->exps->dim; | |
7229 } | |
7230 else if (e1->op == TOKtype) // slicing a type tuple | |
7231 { tup = (TypeTuple *)t; | |
7232 length = Argument::dim(tup->arguments); | |
7233 } | |
7234 else | |
7235 assert(0); | |
7236 | |
7237 if (i1 <= i2 && i2 <= length) | |
7238 { size_t j1 = (size_t) i1; | |
7239 size_t j2 = (size_t) i2; | |
7240 | |
7241 if (e1->op == TOKtuple) | |
7242 { Expressions *exps = new Expressions; | |
7243 exps->setDim(j2 - j1); | |
7244 for (size_t i = 0; i < j2 - j1; i++) | |
7245 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
7246 exps->data[i] = (void *)e; | |
7247 } | |
7248 e = new TupleExp(loc, exps); | |
7249 } | |
7250 else | |
7251 { Arguments *args = new Arguments; | |
7252 args->reserve(j2 - j1); | |
7253 for (size_t i = j1; i < j2; i++) | |
7254 { Argument *arg = Argument::getNth(tup->arguments, i); | |
7255 args->push(arg); | |
7256 } | |
7257 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
7258 } | |
7259 e = e->semantic(sc); | |
7260 } | |
7261 else | |
7262 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7263 error("string slice [%ju .. %ju] is out of bounds", i1, i2); |
159 | 7264 e = e1; |
7265 } | |
7266 return e; | |
7267 } | |
7268 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7269 if (t->ty == Tarray) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7270 type = e1->type; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7271 else |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7272 type = t->nextOf()->arrayOf(); |
159 | 7273 return e; |
7274 | |
7275 Lerror: | |
7276 char *s; | |
7277 if (t->ty == Tvoid) | |
7278 s = e1->toChars(); | |
7279 else | |
7280 s = t->toChars(); | |
7281 error("%s cannot be sliced with []", s); | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
7282 e = new IntegerExp(0); |
159 | 7283 return e; |
7284 } | |
7285 | |
7286 void SliceExp::checkEscape() | |
7287 { | |
7288 e1->checkEscape(); | |
7289 } | |
7290 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7291 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7292 int SliceExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7293 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7294 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7295 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7296 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7297 |
159 | 7298 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) |
7299 { | |
7300 return this; | |
7301 } | |
7302 | |
7303 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
7304 { | |
7305 error("slice expression %s is not a modifiable lvalue", toChars()); | |
7306 return this; | |
7307 } | |
7308 | |
7309 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7310 { | |
7311 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7312 buf->writeByte('['); | |
7313 if (upr || lwr) | |
7314 { | |
7315 if (lwr) | |
7316 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
7317 else | |
7318 buf->writeByte('0'); | |
7319 buf->writestring(".."); | |
7320 if (upr) | |
7321 expToCBuffer(buf, hgs, upr, PREC_assign); | |
7322 else | |
7323 buf->writestring("length"); // BUG: should be array.length | |
7324 } | |
7325 buf->writeByte(']'); | |
7326 } | |
7327 | |
7328 /********************** ArrayLength **************************************/ | |
7329 | |
7330 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
7331 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
7332 { | |
7333 } | |
7334 | |
7335 Expression *ArrayLengthExp::semantic(Scope *sc) | |
7336 { Expression *e; | |
7337 | |
7338 #if LOGSEMANTIC | |
7339 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
7340 #endif | |
7341 if (!type) | |
7342 { | |
7343 UnaExp::semantic(sc); | |
7344 e1 = resolveProperties(sc, e1); | |
7345 | |
7346 type = Type::tsize_t; | |
7347 } | |
7348 return this; | |
7349 } | |
7350 | |
7351 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7352 { | |
7353 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7354 buf->writestring(".length"); | |
7355 } | |
7356 | |
7357 /*********************** ArrayExp *************************************/ | |
7358 | |
7359 // e1 [ i1, i2, i3, ... ] | |
7360 | |
7361 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
7362 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
7363 { | |
7364 arguments = args; | |
7365 } | |
7366 | |
7367 Expression *ArrayExp::syntaxCopy() | |
7368 { | |
7369 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
7370 } | |
7371 | |
7372 Expression *ArrayExp::semantic(Scope *sc) | |
7373 { Expression *e; | |
7374 Type *t1; | |
7375 | |
7376 #if LOGSEMANTIC | |
7377 printf("ArrayExp::semantic('%s')\n", toChars()); | |
7378 #endif | |
7379 UnaExp::semantic(sc); | |
7380 e1 = resolveProperties(sc, e1); | |
7381 | |
7382 t1 = e1->type->toBasetype(); | |
7383 if (t1->ty != Tclass && t1->ty != Tstruct) | |
7384 { // Convert to IndexExp | |
7385 if (arguments->dim != 1) | |
7386 error("only one index allowed to index %s", t1->toChars()); | |
7387 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
7388 return e->semantic(sc); | |
7389 } | |
7390 | |
7391 // Run semantic() on each argument | |
7392 for (size_t i = 0; i < arguments->dim; i++) | |
7393 { e = (Expression *)arguments->data[i]; | |
7394 | |
7395 e = e->semantic(sc); | |
7396 if (!e->type) | |
7397 error("%s has no value", e->toChars()); | |
7398 arguments->data[i] = (void *)e; | |
7399 } | |
7400 | |
7401 expandTuples(arguments); | |
7402 assert(arguments && arguments->dim); | |
7403 | |
7404 e = op_overload(sc); | |
7405 if (!e) | |
7406 { error("no [] operator overload for type %s", e1->type->toChars()); | |
7407 e = e1; | |
7408 } | |
7409 return e; | |
7410 } | |
7411 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7412 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7413 int ArrayExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7414 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7415 if (type && type->toBasetype()->ty == Tvoid) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7416 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7417 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7418 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7419 #endif |
159 | 7420 |
7421 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
7422 { | |
7423 if (type && type->toBasetype()->ty == Tvoid) | |
7424 error("voids have no value"); | |
7425 return this; | |
7426 } | |
7427 | |
7428 | |
7429 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7430 { int i; | |
7431 | |
7432 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7433 buf->writeByte('['); | |
7434 argsToCBuffer(buf, arguments, hgs); | |
7435 buf->writeByte(']'); | |
7436 } | |
7437 | |
7438 /************************* DotExp ***********************************/ | |
7439 | |
7440 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
7441 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
7442 { | |
7443 } | |
7444 | |
7445 Expression *DotExp::semantic(Scope *sc) | |
7446 { | |
7447 #if LOGSEMANTIC | |
7448 printf("DotExp::semantic('%s')\n", toChars()); | |
7449 if (type) printf("\ttype = %s\n", type->toChars()); | |
7450 #endif | |
7451 e1 = e1->semantic(sc); | |
7452 e2 = e2->semantic(sc); | |
7453 if (e2->op == TOKimport) | |
7454 { | |
7455 ScopeExp *se = (ScopeExp *)e2; | |
7456 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
7457 if (td) | |
7458 { Expression *e = new DotTemplateExp(loc, e1, td); | |
7459 e = e->semantic(sc); | |
7460 return e; | |
7461 } | |
7462 } | |
7463 if (!type) | |
7464 type = e2->type; | |
7465 return this; | |
7466 } | |
7467 | |
7468 | |
7469 /************************* CommaExp ***********************************/ | |
7470 | |
7471 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
7472 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
7473 { | |
7474 } | |
7475 | |
7476 Expression *CommaExp::semantic(Scope *sc) | |
7477 { | |
7478 if (!type) | |
7479 { BinExp::semanticp(sc); | |
7480 type = e2->type; | |
7481 } | |
7482 return this; | |
7483 } | |
7484 | |
7485 void CommaExp::checkEscape() | |
7486 { | |
7487 e2->checkEscape(); | |
7488 } | |
7489 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7490 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7491 int CommaExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7492 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7493 return e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7494 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7495 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7496 |
159 | 7497 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) |
7498 { | |
7499 e2 = e2->toLvalue(sc, NULL); | |
7500 return this; | |
7501 } | |
7502 | |
7503 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7504 { | |
7505 e2 = e2->modifiableLvalue(sc, e); | |
7506 return this; | |
7507 } | |
7508 | |
7509 int CommaExp::isBool(int result) | |
7510 { | |
7511 return e2->isBool(result); | |
7512 } | |
7513 | |
7514 int CommaExp::checkSideEffect(int flag) | |
7515 { | |
7516 if (flag == 2) | |
7517 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7518 else | |
7519 { | |
7520 // Don't check e1 until we cast(void) the a,b code generation | |
7521 return e2->checkSideEffect(flag); | |
7522 } | |
7523 } | |
7524 | |
7525 /************************** IndexExp **********************************/ | |
7526 | |
7527 // e1 [ e2 ] | |
7528 | |
7529 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7530 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7531 { | |
7532 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7533 lengthVar = NULL; | |
7534 modifiable = 0; // assume it is an rvalue | |
7535 } | |
7536 | |
7537 Expression *IndexExp::semantic(Scope *sc) | |
7538 { Expression *e; | |
7539 BinExp *b; | |
7540 UnaExp *u; | |
7541 Type *t1; | |
7542 ScopeDsymbol *sym; | |
7543 | |
7544 #if LOGSEMANTIC | |
7545 printf("IndexExp::semantic('%s')\n", toChars()); | |
7546 #endif | |
7547 if (type) | |
7548 return this; | |
7549 if (!e1->type) | |
7550 e1 = e1->semantic(sc); | |
7551 assert(e1->type); // semantic() should already be run on it | |
7552 e = this; | |
7553 | |
7554 // Note that unlike C we do not implement the int[ptr] | |
7555 | |
7556 t1 = e1->type->toBasetype(); | |
7557 | |
7558 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7559 { // Create scope for 'length' variable | |
7560 sym = new ArrayScopeSymbol(this); | |
7561 sym->loc = loc; | |
7562 sym->parent = sc->scopesym; | |
7563 sc = sc->push(sym); | |
7564 } | |
7565 | |
7566 e2 = e2->semantic(sc); | |
7567 if (!e2->type) | |
7568 { | |
7569 error("%s has no value", e2->toChars()); | |
7570 e2->type = Type::terror; | |
7571 } | |
7572 e2 = resolveProperties(sc, e2); | |
7573 | |
7574 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7575 sc = sc->pop(); | |
7576 | |
7577 switch (t1->ty) | |
7578 { | |
7579 case Tpointer: | |
7580 case Tarray: | |
7581 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7582 e->type = t1->next; | |
7583 break; | |
7584 | |
7585 case Tsarray: | |
7586 { | |
7587 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7588 | |
7589 TypeSArray *tsa = (TypeSArray *)t1; | |
7590 | |
7591 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7592 // Do compile time array bounds checking if possible | |
7593 e2 = e2->optimize(WANTvalue); | |
7594 if (e2->op == TOKint64) | |
7595 { | |
7596 integer_t index = e2->toInteger(); | |
7597 integer_t length = tsa->dim->toInteger(); | |
7598 if (index < 0 || index >= length) | |
7599 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7600 index, length); | |
7601 } | |
7602 #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
|
7603 e->type = t1->nextOf(); |
159 | 7604 break; |
7605 } | |
7606 | |
7607 case Taarray: | |
7608 { TypeAArray *taa = (TypeAArray *)t1; | |
7609 | |
7610 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7611 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7612 type = taa->next; | |
7613 break; | |
7614 } | |
7615 | |
7616 case Ttuple: | |
7617 { | |
7618 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7619 e2 = e2->optimize(WANTvalue); | |
7620 uinteger_t index = e2->toUInteger(); | |
7621 size_t length; | |
7622 TupleExp *te; | |
7623 TypeTuple *tup; | |
7624 | |
7625 if (e1->op == TOKtuple) | |
7626 { te = (TupleExp *)e1; | |
7627 length = te->exps->dim; | |
7628 } | |
7629 else if (e1->op == TOKtype) | |
7630 { | |
7631 tup = (TypeTuple *)t1; | |
7632 length = Argument::dim(tup->arguments); | |
7633 } | |
7634 else | |
7635 assert(0); | |
7636 | |
7637 if (index < length) | |
7638 { | |
7639 | |
7640 if (e1->op == TOKtuple) | |
7641 e = (Expression *)te->exps->data[(size_t)index]; | |
7642 else | |
7643 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
7644 } | |
7645 else | |
7646 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7647 error("array index [%ju] is outside array bounds [0 .. %zu]", |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7648 index, length); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7649 e = e1; |
159 | 7650 } |
7651 break; | |
7652 } | |
7653 | |
7654 default: | |
7655 error("%s must be an array or pointer type, not %s", | |
7656 e1->toChars(), e1->type->toChars()); | |
7657 type = Type::tint32; | |
7658 break; | |
7659 } | |
7660 return e; | |
7661 } | |
7662 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7663 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7664 int IndexExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7665 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7666 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7667 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7668 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7669 |
159 | 7670 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) |
7671 { | |
7672 // if (type && type->toBasetype()->ty == Tvoid) | |
7673 // error("voids have no value"); | |
7674 return this; | |
7675 } | |
7676 | |
7677 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
7678 { | |
7679 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
7680 modifiable = 1; | |
7681 if (e1->op == TOKstring) | |
7682 error("string literals are immutable"); | |
7683 if (e1->type->toBasetype()->ty == Taarray) | |
7684 e1 = e1->modifiableLvalue(sc, e1); | |
7685 return toLvalue(sc, e); | |
7686 } | |
7687 | |
7688 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7689 { | |
7690 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7691 buf->writeByte('['); | |
7692 expToCBuffer(buf, hgs, e2, PREC_assign); | |
7693 buf->writeByte(']'); | |
7694 } | |
7695 | |
7696 | |
7697 /************************* PostExp ***********************************/ | |
7698 | |
7699 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
7700 : BinExp(loc, op, sizeof(PostExp), e, | |
7701 new IntegerExp(loc, 1, Type::tint32)) | |
7702 { | |
7703 } | |
7704 | |
7705 Expression *PostExp::semantic(Scope *sc) | |
7706 { Expression *e = this; | |
7707 | |
7708 if (!type) | |
7709 { | |
7710 BinExp::semantic(sc); | |
7711 e2 = resolveProperties(sc, e2); | |
7712 | |
7713 e = op_overload(sc); | |
7714 if (e) | |
7715 return e; | |
7716 | |
7717 e = this; | |
7718 e1 = e1->modifiableLvalue(sc, e1); | |
7719 e1->checkScalar(); | |
7720 e1->checkNoBool(); | |
7721 if (e1->type->ty == Tpointer) | |
7722 e = scaleFactor(sc); | |
7723 else | |
7724 e2 = e2->castTo(sc, e1->type); | |
7725 e->type = e1->type; | |
7726 } | |
7727 return e; | |
7728 } | |
7729 | |
7730 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7731 { | |
7732 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7733 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
7734 } | |
7735 | |
7736 /************************************************************/ | |
7737 | |
7738 /* Can be TOKconstruct too */ | |
7739 | |
7740 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
7741 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
7742 { | |
7743 ismemset = 0; | |
7744 } | |
7745 | |
7746 Expression *AssignExp::semantic(Scope *sc) | |
7747 { Type *t1; | |
7748 Expression *e1old = e1; | |
7749 | |
7750 #if LOGSEMANTIC | |
7751 printf("AssignExp::semantic('%s')\n", toChars()); | |
7752 #endif | |
7753 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
7754 | |
7755 /* Look for operator overloading of a[i]=value. | |
7756 * Do it before semantic() otherwise the a[i] will have been | |
7757 * converted to a.opIndex() already. | |
7758 */ | |
7759 if (e1->op == TOKarray) | |
7760 { Type *t1; | |
7761 ArrayExp *ae = (ArrayExp *)e1; | |
7762 AggregateDeclaration *ad; | |
7763 Identifier *id = Id::index; | |
7764 | |
7765 ae->e1 = ae->e1->semantic(sc); | |
7766 t1 = ae->e1->type->toBasetype(); | |
7767 if (t1->ty == Tstruct) | |
7768 { | |
7769 ad = ((TypeStruct *)t1)->sym; | |
7770 goto L1; | |
7771 } | |
7772 else if (t1->ty == Tclass) | |
7773 { | |
7774 ad = ((TypeClass *)t1)->sym; | |
7775 L1: | |
7776 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
7777 if (search_function(ad, Id::indexass)) | |
7778 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
7779 Expressions *a = (Expressions *)ae->arguments->copy(); | |
7780 | |
7781 a->insert(0, e2); | |
7782 e = new CallExp(loc, e, a); | |
7783 e = e->semantic(sc); | |
7784 return e; | |
7785 } | |
7786 else | |
7787 { | |
7788 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
7789 if (search_function(ad, id)) | |
7790 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
7791 | |
7792 if (1 || !global.params.useDeprecated) | |
7793 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
7794 | |
7795 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
7796 e = e->semantic(sc); | |
7797 return e; | |
7798 } | |
7799 } | |
7800 } | |
7801 } | |
7802 /* Look for operator overloading of a[i..j]=value. | |
7803 * Do it before semantic() otherwise the a[i..j] will have been | |
7804 * converted to a.opSlice() already. | |
7805 */ | |
7806 if (e1->op == TOKslice) | |
7807 { Type *t1; | |
7808 SliceExp *ae = (SliceExp *)e1; | |
7809 AggregateDeclaration *ad; | |
7810 Identifier *id = Id::index; | |
7811 | |
7812 ae->e1 = ae->e1->semantic(sc); | |
7813 ae->e1 = resolveProperties(sc, ae->e1); | |
7814 t1 = ae->e1->type->toBasetype(); | |
7815 if (t1->ty == Tstruct) | |
7816 { | |
7817 ad = ((TypeStruct *)t1)->sym; | |
7818 goto L2; | |
7819 } | |
7820 else if (t1->ty == Tclass) | |
7821 { | |
7822 ad = ((TypeClass *)t1)->sym; | |
7823 L2: | |
7824 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
7825 if (search_function(ad, Id::sliceass)) | |
7826 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
7827 Expressions *a = new Expressions(); | |
7828 | |
7829 a->push(e2); | |
7830 if (ae->lwr) | |
7831 { a->push(ae->lwr); | |
7832 assert(ae->upr); | |
7833 a->push(ae->upr); | |
7834 } | |
7835 else | |
7836 assert(!ae->upr); | |
7837 e = new CallExp(loc, e, a); | |
7838 e = e->semantic(sc); | |
7839 return e; | |
7840 } | |
7841 } | |
7842 } | |
7843 | |
7844 BinExp::semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7845 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7846 if (e1->op == TOKdottd) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7847 { // Rewrite a.b=e2, when b is a template, as a.b(e2) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7848 Expression *e = new CallExp(loc, e1, e2); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7849 e = e->semantic(sc); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7850 return e; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7851 } |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7852 |
159 | 7853 e2 = resolveProperties(sc, e2); |
7854 assert(e1->type); | |
7855 | |
7856 /* Rewrite tuple assignment as a tuple of assignments. | |
7857 */ | |
7858 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
7859 { TupleExp *tup1 = (TupleExp *)e1; | |
7860 TupleExp *tup2 = (TupleExp *)e2; | |
7861 size_t dim = tup1->exps->dim; | |
7862 if (dim != tup2->exps->dim) | |
7863 { | |
7864 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
7865 } | |
7866 else | |
7867 { Expressions *exps = new Expressions; | |
7868 exps->setDim(dim); | |
7869 | |
7870 for (int i = 0; i < dim; i++) | |
7871 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
7872 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
7873 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
7874 } | |
7875 Expression *e = new TupleExp(loc, exps); | |
7876 e = e->semantic(sc); | |
7877 return e; | |
7878 } | |
7879 } | |
7880 | |
7881 t1 = e1->type->toBasetype(); | |
7882 | |
7883 if (t1->ty == Tfunction) | |
7884 { // Rewrite f=value to f(value) | |
7885 Expression *e; | |
7886 | |
7887 e = new CallExp(loc, e1, e2); | |
7888 e = e->semantic(sc); | |
7889 return e; | |
7890 } | |
7891 | |
7892 /* If it is an assignment from a 'foreign' type, | |
7893 * check for operator overloading. | |
7894 */ | |
7895 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7896 { | |
7897 if (!e2->type->implicitConvTo(e1->type)) | |
7898 { | |
7899 Expression *e = op_overload(sc); | |
7900 if (e) | |
7901 return e; | |
7902 } | |
7903 } | |
7904 | |
7905 e2->rvalue(); | |
7906 | |
7907 if (e1->op == TOKarraylength) | |
7908 { | |
7909 // e1 is not an lvalue, but we let code generator handle it | |
7910 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7911 | |
7912 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
7913 } | |
7914 else if (e1->op == TOKslice) | |
7915 ; | |
7916 else | |
7917 { // Try to do a decent error message with the expression | |
7918 // before it got constant folded | |
7919 e1 = e1->modifiableLvalue(sc, e1old); | |
7920 } | |
7921 | |
7922 if (e1->op == TOKslice && | |
7923 t1->nextOf() && | |
7924 e2->implicitConvTo(t1->nextOf()) | |
7925 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
7926 ) | |
7927 { // memset | |
7928 ismemset = 1; // make it easy for back end to tell what this is | |
7929 e2 = e2->implicitCastTo(sc, t1->next); | |
7930 } | |
7931 else if (t1->ty == Tsarray) | |
7932 { | |
7933 error("cannot assign to static array %s", e1->toChars()); | |
7934 } | |
7935 else | |
7936 { | |
7937 e2 = e2->implicitCastTo(sc, e1->type); | |
7938 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7939 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7940 /* 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
|
7941 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7942 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
|
7943 (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
|
7944 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
|
7945 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
|
7946 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
|
7947 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
|
7948 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7949 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
|
7950 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
|
7951 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7952 |
159 | 7953 type = e1->type; |
7954 assert(type); | |
7955 return this; | |
7956 } | |
7957 | |
7958 Expression *AssignExp::checkToBoolean() | |
7959 { | |
7960 // Things like: | |
7961 // if (a = b) ... | |
7962 // are usually mistakes. | |
7963 | |
7964 error("'=' does not give a boolean result"); | |
7965 return this; | |
7966 } | |
7967 | |
7968 /************************************************************/ | |
7969 | |
7970 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7971 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7972 { | |
7973 } | |
7974 | |
7975 Expression *AddAssignExp::semantic(Scope *sc) | |
7976 { Expression *e; | |
7977 | |
7978 if (type) | |
7979 return this; | |
7980 | |
7981 BinExp::semantic(sc); | |
7982 e2 = resolveProperties(sc, e2); | |
7983 | |
7984 e = op_overload(sc); | |
7985 if (e) | |
7986 return e; | |
7987 | |
7988 Type *tb1 = e1->type->toBasetype(); | |
7989 Type *tb2 = e2->type->toBasetype(); | |
7990 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7991 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
|
7992 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7993 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
|
7994 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
|
7995 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
|
7996 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7997 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7998 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7999 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
|
8000 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8001 |
159 | 8002 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
8003 (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
|
8004 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8005 ) |
8006 { | |
8007 type = e1->type; | |
8008 e = this; | |
8009 } | |
8010 else | |
8011 { | |
8012 e1->checkScalar(); | |
8013 e1->checkNoBool(); | |
8014 if (tb1->ty == Tpointer && tb2->isintegral()) | |
8015 e = scaleFactor(sc); | |
8016 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
8017 { | |
8018 #if 0 | |
8019 // Need to rethink this | |
8020 if (e1->op != TOKvar) | |
8021 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
8022 VarDeclaration *v; | |
8023 Expression *ea; | |
8024 Expression *ex; | |
8025 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8026 Identifier *id = Lexer::uniqueId("__name"); |
159 | 8027 |
8028 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
8029 v->semantic(sc); | |
8030 if (!sc->insert(v)) | |
8031 assert(0); | |
8032 v->parent = sc->func; | |
8033 | |
8034 ea = new AddrExp(loc, e1); | |
8035 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
8036 | |
8037 ex = new VarExp(loc, v); | |
8038 ex = new PtrExp(loc, ex); | |
8039 e = new AddExp(loc, ex, e2); | |
8040 e = new CastExp(loc, e, e1->type); | |
8041 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
8042 | |
8043 e = new CommaExp(loc, ea, e); | |
8044 } | |
8045 else | |
8046 #endif | |
8047 { // Rewrite e1+=e2 to e1=e1+e2 | |
8048 // BUG: doesn't account for side effects in e1 | |
8049 // BUG: other assignment operators for bits aren't handled at all | |
8050 e = new AddExp(loc, e1, e2); | |
8051 e = new CastExp(loc, e, e1->type); | |
8052 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
8053 } | |
8054 e = e->semantic(sc); | |
8055 } | |
8056 else | |
8057 { | |
8058 type = e1->type; | |
8059 typeCombine(sc); | |
8060 e1->checkArithmetic(); | |
8061 e2->checkArithmetic(); | |
8062 if (type->isreal() || type->isimaginary()) | |
8063 { | |
8064 assert(global.errors || e2->type->isfloating()); | |
8065 e2 = e2->castTo(sc, e1->type); | |
8066 } | |
8067 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8068 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8069 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8070 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8071 } |
8072 } | |
8073 return e; | |
8074 } | |
8075 | |
8076 /************************************************************/ | |
8077 | |
8078 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8079 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
8080 { | |
8081 } | |
8082 | |
8083 Expression *MinAssignExp::semantic(Scope *sc) | |
8084 { Expression *e; | |
8085 | |
8086 if (type) | |
8087 return this; | |
8088 | |
8089 BinExp::semantic(sc); | |
8090 e2 = resolveProperties(sc, e2); | |
8091 | |
8092 e = op_overload(sc); | |
8093 if (e) | |
8094 return e; | |
8095 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8096 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
|
8097 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8098 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
|
8099 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
|
8100 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
|
8101 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8102 |
159 | 8103 e1 = e1->modifiableLvalue(sc, e1); |
8104 e1->checkScalar(); | |
8105 e1->checkNoBool(); | |
8106 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
8107 e = scaleFactor(sc); | |
8108 else | |
8109 { | |
8110 e1 = e1->checkArithmetic(); | |
8111 e2 = e2->checkArithmetic(); | |
8112 type = e1->type; | |
8113 typeCombine(sc); | |
8114 if (type->isreal() || type->isimaginary()) | |
8115 { | |
8116 assert(e2->type->isfloating()); | |
8117 e2 = e2->castTo(sc, e1->type); | |
8118 } | |
8119 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8120 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8121 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8122 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8123 } |
8124 return e; | |
8125 } | |
8126 | |
8127 /************************************************************/ | |
8128 | |
8129 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8130 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
8131 { | |
8132 } | |
8133 | |
8134 Expression *CatAssignExp::semantic(Scope *sc) | |
8135 { Expression *e; | |
8136 | |
8137 BinExp::semantic(sc); | |
8138 e2 = resolveProperties(sc, e2); | |
8139 | |
8140 e = op_overload(sc); | |
8141 if (e) | |
8142 return e; | |
8143 | |
8144 if (e1->op == TOKslice) | |
8145 { SliceExp *se = (SliceExp *)e1; | |
8146 | |
8147 if (se->e1->type->toBasetype()->ty == Tsarray) | |
8148 error("cannot append to static array %s", se->e1->type->toChars()); | |
8149 } | |
8150 | |
8151 e1 = e1->modifiableLvalue(sc, e1); | |
8152 | |
8153 Type *tb1 = e1->type->toBasetype(); | |
8154 Type *tb2 = e2->type->toBasetype(); | |
8155 | |
8156 e2->rvalue(); | |
8157 | |
8158 if ((tb1->ty == Tarray) && | |
8159 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
8160 e2->implicitConvTo(e1->type) | |
8161 //e1->type->next->equals(e2->type->next) | |
8162 ) | |
8163 { // Append array | |
8164 e2 = e2->castTo(sc, e1->type); | |
8165 type = e1->type; | |
8166 e = this; | |
8167 } | |
8168 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
|
8169 e2->implicitConvTo(tb1->nextOf()) |
159 | 8170 ) |
8171 { // 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
|
8172 e2 = e2->castTo(sc, tb1->nextOf()); |
159 | 8173 type = e1->type; |
8174 e = this; | |
8175 } | |
8176 else | |
8177 { | |
8178 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
8179 type = Type::tint32; | |
8180 e = this; | |
8181 } | |
8182 return e; | |
8183 } | |
8184 | |
8185 /************************************************************/ | |
8186 | |
8187 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8188 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
8189 { | |
8190 } | |
8191 | |
8192 Expression *MulAssignExp::semantic(Scope *sc) | |
8193 { Expression *e; | |
8194 | |
8195 BinExp::semantic(sc); | |
8196 e2 = resolveProperties(sc, e2); | |
8197 | |
8198 e = op_overload(sc); | |
8199 if (e) | |
8200 return e; | |
8201 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8202 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
|
8203 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8204 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
|
8205 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
|
8206 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
|
8207 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8208 |
159 | 8209 e1 = e1->modifiableLvalue(sc, e1); |
8210 e1->checkScalar(); | |
8211 e1->checkNoBool(); | |
8212 type = e1->type; | |
8213 typeCombine(sc); | |
8214 e1->checkArithmetic(); | |
8215 e2->checkArithmetic(); | |
8216 if (e2->type->isfloating()) | |
8217 { Type *t1; | |
8218 Type *t2; | |
8219 | |
8220 t1 = e1->type; | |
8221 t2 = e2->type; | |
8222 if (t1->isreal()) | |
8223 { | |
8224 if (t2->isimaginary() || t2->iscomplex()) | |
8225 { | |
8226 e2 = e2->castTo(sc, t1); | |
8227 } | |
8228 } | |
8229 else if (t1->isimaginary()) | |
8230 { | |
8231 if (t2->isimaginary() || t2->iscomplex()) | |
8232 { | |
8233 switch (t1->ty) | |
8234 { | |
8235 case Timaginary32: t2 = Type::tfloat32; break; | |
8236 case Timaginary64: t2 = Type::tfloat64; break; | |
8237 case Timaginary80: t2 = Type::tfloat80; break; | |
8238 default: | |
8239 assert(0); | |
8240 } | |
8241 e2 = e2->castTo(sc, t2); | |
8242 } | |
8243 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8244 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8245 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8246 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8247 } |
8248 return this; | |
8249 } | |
8250 | |
8251 /************************************************************/ | |
8252 | |
8253 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8254 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
8255 { | |
8256 } | |
8257 | |
8258 Expression *DivAssignExp::semantic(Scope *sc) | |
8259 { Expression *e; | |
8260 | |
8261 BinExp::semantic(sc); | |
8262 e2 = resolveProperties(sc, e2); | |
8263 | |
8264 e = op_overload(sc); | |
8265 if (e) | |
8266 return e; | |
8267 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8268 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
|
8269 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8270 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
|
8271 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
|
8272 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
|
8273 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8274 |
159 | 8275 e1 = e1->modifiableLvalue(sc, e1); |
8276 e1->checkScalar(); | |
8277 e1->checkNoBool(); | |
8278 type = e1->type; | |
8279 typeCombine(sc); | |
8280 e1->checkArithmetic(); | |
8281 e2->checkArithmetic(); | |
8282 if (e2->type->isimaginary()) | |
8283 { Type *t1; | |
8284 Type *t2; | |
8285 | |
8286 t1 = e1->type; | |
8287 if (t1->isreal()) | |
8288 { // x/iv = i(-x/v) | |
8289 // Therefore, the result is 0 | |
8290 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
8291 e2->type = t1; | |
8292 e = new AssignExp(loc, e1, e2); | |
8293 e->type = t1; | |
8294 return e; | |
8295 } | |
8296 else if (t1->isimaginary()) | |
8297 { Expression *e; | |
8298 | |
8299 switch (t1->ty) | |
8300 { | |
8301 case Timaginary32: t2 = Type::tfloat32; break; | |
8302 case Timaginary64: t2 = Type::tfloat64; break; | |
8303 case Timaginary80: t2 = Type::tfloat80; break; | |
8304 default: | |
8305 assert(0); | |
8306 } | |
8307 e2 = e2->castTo(sc, t2); | |
8308 e = new AssignExp(loc, e1, e2); | |
8309 e->type = t1; | |
8310 return e; | |
8311 } | |
8312 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8313 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8314 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8315 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8316 |
159 | 8317 return this; |
8318 } | |
8319 | |
8320 /************************************************************/ | |
8321 | |
8322 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8323 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
8324 { | |
8325 } | |
8326 | |
8327 Expression *ModAssignExp::semantic(Scope *sc) | |
8328 { | |
8329 return commonSemanticAssign(sc); | |
8330 } | |
8331 | |
8332 /************************************************************/ | |
8333 | |
8334 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8335 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
8336 { | |
8337 } | |
8338 | |
8339 Expression *ShlAssignExp::semantic(Scope *sc) | |
8340 { Expression *e; | |
8341 | |
8342 //printf("ShlAssignExp::semantic()\n"); | |
8343 BinExp::semantic(sc); | |
8344 e2 = resolveProperties(sc, e2); | |
8345 | |
8346 e = op_overload(sc); | |
8347 if (e) | |
8348 return e; | |
8349 | |
8350 e1 = e1->modifiableLvalue(sc, e1); | |
8351 e1->checkScalar(); | |
8352 e1->checkNoBool(); | |
8353 type = e1->type; | |
8354 typeCombine(sc); | |
8355 e1->checkIntegral(); | |
8356 e2 = e2->checkIntegral(); | |
8357 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8358 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8359 return this; |
8360 } | |
8361 | |
8362 /************************************************************/ | |
8363 | |
8364 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8365 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
8366 { | |
8367 } | |
8368 | |
8369 Expression *ShrAssignExp::semantic(Scope *sc) | |
8370 { Expression *e; | |
8371 | |
8372 BinExp::semantic(sc); | |
8373 e2 = resolveProperties(sc, e2); | |
8374 | |
8375 e = op_overload(sc); | |
8376 if (e) | |
8377 return e; | |
8378 | |
8379 e1 = e1->modifiableLvalue(sc, e1); | |
8380 e1->checkScalar(); | |
8381 e1->checkNoBool(); | |
8382 type = e1->type; | |
8383 typeCombine(sc); | |
8384 e1->checkIntegral(); | |
8385 e2 = e2->checkIntegral(); | |
8386 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8387 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8388 return this; |
8389 } | |
8390 | |
8391 /************************************************************/ | |
8392 | |
8393 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8394 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
8395 { | |
8396 } | |
8397 | |
8398 Expression *UshrAssignExp::semantic(Scope *sc) | |
8399 { Expression *e; | |
8400 | |
8401 BinExp::semantic(sc); | |
8402 e2 = resolveProperties(sc, e2); | |
8403 | |
8404 e = op_overload(sc); | |
8405 if (e) | |
8406 return e; | |
8407 | |
8408 e1 = e1->modifiableLvalue(sc, e1); | |
8409 e1->checkScalar(); | |
8410 e1->checkNoBool(); | |
8411 type = e1->type; | |
8412 typeCombine(sc); | |
8413 e1->checkIntegral(); | |
8414 e2 = e2->checkIntegral(); | |
8415 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8416 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8417 return this; |
8418 } | |
8419 | |
8420 /************************************************************/ | |
8421 | |
8422 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8423 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
8424 { | |
8425 } | |
8426 | |
8427 Expression *AndAssignExp::semantic(Scope *sc) | |
8428 { | |
8429 return commonSemanticAssignIntegral(sc); | |
8430 } | |
8431 | |
8432 /************************************************************/ | |
8433 | |
8434 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8435 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
8436 { | |
8437 } | |
8438 | |
8439 Expression *OrAssignExp::semantic(Scope *sc) | |
8440 { | |
8441 return commonSemanticAssignIntegral(sc); | |
8442 } | |
8443 | |
8444 /************************************************************/ | |
8445 | |
8446 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8447 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
8448 { | |
8449 } | |
8450 | |
8451 Expression *XorAssignExp::semantic(Scope *sc) | |
8452 { | |
8453 return commonSemanticAssignIntegral(sc); | |
8454 } | |
8455 | |
8456 /************************* AddExp *****************************/ | |
8457 | |
8458 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
8459 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
8460 { | |
8461 } | |
8462 | |
8463 Expression *AddExp::semantic(Scope *sc) | |
8464 { Expression *e; | |
8465 | |
8466 #if LOGSEMANTIC | |
8467 printf("AddExp::semantic('%s')\n", toChars()); | |
8468 #endif | |
8469 if (!type) | |
8470 { | |
8471 BinExp::semanticp(sc); | |
8472 | |
8473 e = op_overload(sc); | |
8474 if (e) | |
8475 return e; | |
8476 | |
8477 Type *tb1 = e1->type->toBasetype(); | |
8478 Type *tb2 = e2->type->toBasetype(); | |
8479 | |
8480 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
8481 (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
|
8482 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8483 ) |
8484 { | |
8485 type = e1->type; | |
8486 e = this; | |
8487 } | |
8488 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
8489 tb2->ty == Tpointer && e1->type->isintegral()) | |
8490 e = scaleFactor(sc); | |
8491 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
8492 { | |
8493 incompatibleTypes(); | |
8494 type = e1->type; | |
8495 e = this; | |
8496 } | |
8497 else | |
8498 { | |
8499 typeCombine(sc); | |
8500 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
8501 (e1->type->isimaginary() && e2->type->isreal())) | |
8502 { | |
8503 switch (type->toBasetype()->ty) | |
8504 { | |
8505 case Tfloat32: | |
8506 case Timaginary32: | |
8507 type = Type::tcomplex32; | |
8508 break; | |
8509 | |
8510 case Tfloat64: | |
8511 case Timaginary64: | |
8512 type = Type::tcomplex64; | |
8513 break; | |
8514 | |
8515 case Tfloat80: | |
8516 case Timaginary80: | |
8517 type = Type::tcomplex80; | |
8518 break; | |
8519 | |
8520 default: | |
8521 assert(0); | |
8522 } | |
8523 } | |
8524 e = this; | |
8525 } | |
8526 return e; | |
8527 } | |
8528 return this; | |
8529 } | |
8530 | |
8531 /************************************************************/ | |
8532 | |
8533 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8534 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8535 { | |
8536 } | |
8537 | |
8538 Expression *MinExp::semantic(Scope *sc) | |
8539 { Expression *e; | |
8540 Type *t1; | |
8541 Type *t2; | |
8542 | |
8543 #if LOGSEMANTIC | |
8544 printf("MinExp::semantic('%s')\n", toChars()); | |
8545 #endif | |
8546 if (type) | |
8547 return this; | |
8548 | |
8549 BinExp::semanticp(sc); | |
8550 | |
8551 e = op_overload(sc); | |
8552 if (e) | |
8553 return e; | |
8554 | |
8555 e = this; | |
8556 t1 = e1->type->toBasetype(); | |
8557 t2 = e2->type->toBasetype(); | |
8558 if (t1->ty == Tpointer) | |
8559 { | |
8560 if (t2->ty == Tpointer) | |
8561 { // Need to divide the result by the stride | |
8562 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8563 d_int64 stride; | |
8564 Expression *e; | |
8565 | |
8566 typeCombine(sc); // make sure pointer types are compatible | |
8567 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
|
8568 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
|
8569 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
|
8570 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8571 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
|
8572 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8573 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8574 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8575 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
|
8576 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
|
8577 } |
159 | 8578 return e; |
8579 } | |
8580 else if (t2->isintegral()) | |
8581 e = scaleFactor(sc); | |
8582 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
|
8583 { error("incompatible types for minus"); |
159 | 8584 return new IntegerExp(0); |
8585 } | |
8586 } | |
8587 else if (t2->ty == Tpointer) | |
8588 { | |
8589 type = e2->type; | |
8590 error("can't subtract pointer from %s", e1->type->toChars()); | |
8591 return new IntegerExp(0); | |
8592 } | |
8593 else | |
8594 { | |
8595 typeCombine(sc); | |
8596 t1 = e1->type->toBasetype(); | |
8597 t2 = e2->type->toBasetype(); | |
8598 if ((t1->isreal() && t2->isimaginary()) || | |
8599 (t1->isimaginary() && t2->isreal())) | |
8600 { | |
8601 switch (type->ty) | |
8602 { | |
8603 case Tfloat32: | |
8604 case Timaginary32: | |
8605 type = Type::tcomplex32; | |
8606 break; | |
8607 | |
8608 case Tfloat64: | |
8609 case Timaginary64: | |
8610 type = Type::tcomplex64; | |
8611 break; | |
8612 | |
8613 case Tfloat80: | |
8614 case Timaginary80: | |
8615 type = Type::tcomplex80; | |
8616 break; | |
8617 | |
8618 default: | |
8619 assert(0); | |
8620 } | |
8621 } | |
8622 } | |
8623 return e; | |
8624 } | |
8625 | |
8626 /************************* CatExp *****************************/ | |
8627 | |
8628 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
8629 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
8630 { | |
8631 } | |
8632 | |
8633 Expression *CatExp::semantic(Scope *sc) | |
8634 { Expression *e; | |
8635 | |
8636 //printf("CatExp::semantic() %s\n", toChars()); | |
8637 if (!type) | |
8638 { | |
8639 BinExp::semanticp(sc); | |
8640 e = op_overload(sc); | |
8641 if (e) | |
8642 return e; | |
8643 | |
8644 Type *tb1 = e1->type->toBasetype(); | |
8645 Type *tb2 = e2->type->toBasetype(); | |
8646 | |
8647 | |
8648 /* BUG: Should handle things like: | |
8649 * char c; | |
8650 * c ~ ' ' | |
8651 * ' ' ~ c; | |
8652 */ | |
8653 | |
8654 #if 0 | |
8655 e1->type->print(); | |
8656 e2->type->print(); | |
8657 #endif | |
8658 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
8659 e2->type->equals(tb1->next)) | |
8660 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8661 type = tb1->nextOf()->arrayOf(); |
159 | 8662 if (tb2->ty == Tarray) |
8663 { // Make e2 into [e2] | |
8664 e2 = new ArrayLiteralExp(e2->loc, e2); | |
8665 e2->type = type; | |
8666 } | |
8667 return this; | |
8668 } | |
8669 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
8670 e1->type->equals(tb2->next)) | |
8671 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8672 type = tb2->nextOf()->arrayOf(); |
159 | 8673 if (tb1->ty == Tarray) |
8674 { // Make e1 into [e1] | |
8675 e1 = new ArrayLiteralExp(e1->loc, e1); | |
8676 e1->type = type; | |
8677 } | |
8678 return this; | |
8679 } | |
8680 | |
8681 typeCombine(sc); | |
8682 | |
8683 if (type->toBasetype()->ty == Tsarray) | |
8684 type = type->toBasetype()->next->arrayOf(); | |
8685 #if 0 | |
8686 e1->type->print(); | |
8687 e2->type->print(); | |
8688 type->print(); | |
8689 print(); | |
8690 #endif | |
8691 if (e1->op == TOKstring && e2->op == TOKstring) | |
8692 e = optimize(WANTvalue); | |
8693 else if (e1->type->equals(e2->type) && | |
8694 (e1->type->toBasetype()->ty == Tarray || | |
8695 e1->type->toBasetype()->ty == Tsarray)) | |
8696 { | |
8697 e = this; | |
8698 } | |
8699 else | |
8700 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8701 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 8702 error("Can only concatenate arrays, not (%s ~ %s)", |
8703 e1->type->toChars(), e2->type->toChars()); | |
8704 type = Type::tint32; | |
8705 e = this; | |
8706 } | |
8707 e->type = e->type->semantic(loc, sc); | |
8708 return e; | |
8709 } | |
8710 return this; | |
8711 } | |
8712 | |
8713 /************************************************************/ | |
8714 | |
8715 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
8716 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
8717 { | |
8718 } | |
8719 | |
8720 Expression *MulExp::semantic(Scope *sc) | |
8721 { Expression *e; | |
8722 | |
8723 #if 0 | |
8724 printf("MulExp::semantic() %s\n", toChars()); | |
8725 #endif | |
8726 if (type) | |
8727 { | |
8728 return this; | |
8729 } | |
8730 | |
8731 BinExp::semanticp(sc); | |
8732 e = op_overload(sc); | |
8733 if (e) | |
8734 return e; | |
8735 | |
8736 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
|
8737 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
|
8738 { 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
|
8739 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
|
8740 } |
159 | 8741 if (type->isfloating()) |
8742 { Type *t1 = e1->type; | |
8743 Type *t2 = e2->type; | |
8744 | |
8745 if (t1->isreal()) | |
8746 { | |
8747 type = t2; | |
8748 } | |
8749 else if (t2->isreal()) | |
8750 { | |
8751 type = t1; | |
8752 } | |
8753 else if (t1->isimaginary()) | |
8754 { | |
8755 if (t2->isimaginary()) | |
8756 { Expression *e; | |
8757 | |
8758 switch (t1->ty) | |
8759 { | |
8760 case Timaginary32: type = Type::tfloat32; break; | |
8761 case Timaginary64: type = Type::tfloat64; break; | |
8762 case Timaginary80: type = Type::tfloat80; break; | |
8763 default: assert(0); | |
8764 } | |
8765 | |
8766 // iy * iv = -yv | |
8767 e1->type = type; | |
8768 e2->type = type; | |
8769 e = new NegExp(loc, this); | |
8770 e = e->semantic(sc); | |
8771 return e; | |
8772 } | |
8773 else | |
8774 type = t2; // t2 is complex | |
8775 } | |
8776 else if (t2->isimaginary()) | |
8777 { | |
8778 type = t1; // t1 is complex | |
8779 } | |
8780 } | |
8781 return this; | |
8782 } | |
8783 | |
8784 /************************************************************/ | |
8785 | |
8786 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
8787 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
8788 { | |
8789 } | |
8790 | |
8791 Expression *DivExp::semantic(Scope *sc) | |
8792 { Expression *e; | |
8793 | |
8794 if (type) | |
8795 return this; | |
8796 | |
8797 BinExp::semanticp(sc); | |
8798 e = op_overload(sc); | |
8799 if (e) | |
8800 return e; | |
8801 | |
8802 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
|
8803 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
|
8804 { 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
|
8805 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
|
8806 } |
159 | 8807 if (type->isfloating()) |
8808 { Type *t1 = e1->type; | |
8809 Type *t2 = e2->type; | |
8810 | |
8811 if (t1->isreal()) | |
8812 { | |
8813 type = t2; | |
8814 if (t2->isimaginary()) | |
8815 { Expression *e; | |
8816 | |
8817 // x/iv = i(-x/v) | |
8818 e2->type = t1; | |
8819 e = new NegExp(loc, this); | |
8820 e = e->semantic(sc); | |
8821 return e; | |
8822 } | |
8823 } | |
8824 else if (t2->isreal()) | |
8825 { | |
8826 type = t1; | |
8827 } | |
8828 else if (t1->isimaginary()) | |
8829 { | |
8830 if (t2->isimaginary()) | |
8831 { | |
8832 switch (t1->ty) | |
8833 { | |
8834 case Timaginary32: type = Type::tfloat32; break; | |
8835 case Timaginary64: type = Type::tfloat64; break; | |
8836 case Timaginary80: type = Type::tfloat80; break; | |
8837 default: assert(0); | |
8838 } | |
8839 } | |
8840 else | |
8841 type = t2; // t2 is complex | |
8842 } | |
8843 else if (t2->isimaginary()) | |
8844 { | |
8845 type = t1; // t1 is complex | |
8846 } | |
8847 } | |
8848 return this; | |
8849 } | |
8850 | |
8851 /************************************************************/ | |
8852 | |
8853 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
8854 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
8855 { | |
8856 } | |
8857 | |
8858 Expression *ModExp::semantic(Scope *sc) | |
8859 { Expression *e; | |
8860 | |
8861 if (type) | |
8862 return this; | |
8863 | |
8864 BinExp::semanticp(sc); | |
8865 e = op_overload(sc); | |
8866 if (e) | |
8867 return e; | |
8868 | |
8869 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
|
8870 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
|
8871 { 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
|
8872 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
|
8873 } |
159 | 8874 if (type->isfloating()) |
8875 { type = e1->type; | |
8876 if (e2->type->iscomplex()) | |
8877 { error("cannot perform modulo complex arithmetic"); | |
8878 return new IntegerExp(0); | |
8879 } | |
8880 } | |
8881 return this; | |
8882 } | |
8883 | |
8884 /************************************************************/ | |
8885 | |
8886 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
8887 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
8888 { | |
8889 } | |
8890 | |
8891 Expression *ShlExp::semantic(Scope *sc) | |
8892 { Expression *e; | |
8893 | |
8894 //printf("ShlExp::semantic(), type = %p\n", type); | |
8895 if (!type) | |
8896 { BinExp::semanticp(sc); | |
8897 e = op_overload(sc); | |
8898 if (e) | |
8899 return e; | |
8900 e1 = e1->checkIntegral(); | |
8901 e2 = e2->checkIntegral(); | |
8902 e1 = e1->integralPromotions(sc); | |
8903 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8904 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8905 type = e1->type; |
8906 } | |
8907 return this; | |
8908 } | |
8909 | |
8910 /************************************************************/ | |
8911 | |
8912 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
8913 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
8914 { | |
8915 } | |
8916 | |
8917 Expression *ShrExp::semantic(Scope *sc) | |
8918 { Expression *e; | |
8919 | |
8920 if (!type) | |
8921 { BinExp::semanticp(sc); | |
8922 e = op_overload(sc); | |
8923 if (e) | |
8924 return e; | |
8925 e1 = e1->checkIntegral(); | |
8926 e2 = e2->checkIntegral(); | |
8927 e1 = e1->integralPromotions(sc); | |
8928 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8929 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8930 type = e1->type; |
8931 } | |
8932 return this; | |
8933 } | |
8934 | |
8935 /************************************************************/ | |
8936 | |
8937 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
8938 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
8939 { | |
8940 } | |
8941 | |
8942 Expression *UshrExp::semantic(Scope *sc) | |
8943 { Expression *e; | |
8944 | |
8945 if (!type) | |
8946 { BinExp::semanticp(sc); | |
8947 e = op_overload(sc); | |
8948 if (e) | |
8949 return e; | |
8950 e1 = e1->checkIntegral(); | |
8951 e2 = e2->checkIntegral(); | |
8952 e1 = e1->integralPromotions(sc); | |
8953 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8954 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8955 type = e1->type; |
8956 } | |
8957 return this; | |
8958 } | |
8959 | |
8960 /************************************************************/ | |
8961 | |
8962 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8963 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8964 { | |
8965 } | |
8966 | |
8967 Expression *AndExp::semantic(Scope *sc) | |
8968 { Expression *e; | |
8969 | |
8970 if (!type) | |
8971 { BinExp::semanticp(sc); | |
8972 e = op_overload(sc); | |
8973 if (e) | |
8974 return e; | |
8975 if (e1->type->toBasetype()->ty == Tbool && | |
8976 e2->type->toBasetype()->ty == Tbool) | |
8977 { | |
8978 type = e1->type; | |
8979 e = this; | |
8980 } | |
8981 else | |
8982 { | |
8983 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
|
8984 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
|
8985 { 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
|
8986 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
|
8987 } |
159 | 8988 } |
8989 } | |
8990 return this; | |
8991 } | |
8992 | |
8993 /************************************************************/ | |
8994 | |
8995 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8996 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8997 { | |
8998 } | |
8999 | |
9000 Expression *OrExp::semantic(Scope *sc) | |
9001 { Expression *e; | |
9002 | |
9003 if (!type) | |
9004 { BinExp::semanticp(sc); | |
9005 e = op_overload(sc); | |
9006 if (e) | |
9007 return e; | |
9008 if (e1->type->toBasetype()->ty == Tbool && | |
9009 e2->type->toBasetype()->ty == Tbool) | |
9010 { | |
9011 type = e1->type; | |
9012 e = this; | |
9013 } | |
9014 else | |
9015 { | |
9016 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
|
9017 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
|
9018 { 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
|
9019 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
|
9020 } |
159 | 9021 } |
9022 } | |
9023 return this; | |
9024 } | |
9025 | |
9026 /************************************************************/ | |
9027 | |
9028 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
9029 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
9030 { | |
9031 } | |
9032 | |
9033 Expression *XorExp::semantic(Scope *sc) | |
9034 { Expression *e; | |
9035 | |
9036 if (!type) | |
9037 { BinExp::semanticp(sc); | |
9038 e = op_overload(sc); | |
9039 if (e) | |
9040 return e; | |
9041 if (e1->type->toBasetype()->ty == Tbool && | |
9042 e2->type->toBasetype()->ty == Tbool) | |
9043 { | |
9044 type = e1->type; | |
9045 e = this; | |
9046 } | |
9047 else | |
9048 { | |
9049 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
|
9050 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
|
9051 { 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
|
9052 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
|
9053 } |
159 | 9054 } |
9055 } | |
9056 return this; | |
9057 } | |
9058 | |
9059 | |
9060 /************************************************************/ | |
9061 | |
9062 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
9063 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
9064 { | |
9065 } | |
9066 | |
9067 Expression *OrOrExp::semantic(Scope *sc) | |
9068 { | |
9069 unsigned cs1; | |
9070 | |
9071 // same as for AndAnd | |
9072 e1 = e1->semantic(sc); | |
9073 e1 = resolveProperties(sc, e1); | |
9074 e1 = e1->checkToPointer(); | |
9075 e1 = e1->checkToBoolean(); | |
9076 cs1 = sc->callSuper; | |
9077 | |
9078 if (sc->flags & SCOPEstaticif) | |
9079 { | |
9080 /* If in static if, don't evaluate e2 if we don't have to. | |
9081 */ | |
9082 e1 = e1->optimize(WANTflags); | |
9083 if (e1->isBool(TRUE)) | |
9084 { | |
9085 return new IntegerExp(loc, 1, Type::tboolean); | |
9086 } | |
9087 } | |
9088 | |
9089 e2 = e2->semantic(sc); | |
9090 sc->mergeCallSuper(loc, cs1); | |
9091 e2 = resolveProperties(sc, e2); | |
9092 e2 = e2->checkToPointer(); | |
9093 | |
9094 type = Type::tboolean; | |
779
ef7fe998f9b3
Fix return type of OrOr and AndAndExp being set incorrectly if rhs is void. Thanks wilsonk!
Christian Kamm <kamm incasoftware de>
parents:
723
diff
changeset
|
9095 if (e2->type->ty == Tvoid) |
159 | 9096 type = Type::tvoid; |
9097 if (e2->op == TOKtype || e2->op == TOKimport) | |
9098 error("%s is not an expression", e2->toChars()); | |
9099 return this; | |
9100 } | |
9101 | |
9102 Expression *OrOrExp::checkToBoolean() | |
9103 { | |
9104 e2 = e2->checkToBoolean(); | |
9105 return this; | |
9106 } | |
9107 | |
9108 int OrOrExp::isBit() | |
9109 { | |
9110 return TRUE; | |
9111 } | |
9112 | |
9113 int OrOrExp::checkSideEffect(int flag) | |
9114 { | |
9115 if (flag == 2) | |
9116 { | |
9117 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9118 } | |
9119 else | |
9120 { e1->checkSideEffect(1); | |
9121 return e2->checkSideEffect(flag); | |
9122 } | |
9123 } | |
9124 | |
9125 /************************************************************/ | |
9126 | |
9127 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
9128 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
9129 { | |
9130 } | |
9131 | |
9132 Expression *AndAndExp::semantic(Scope *sc) | |
9133 { | |
9134 unsigned cs1; | |
9135 | |
9136 // same as for OrOr | |
9137 e1 = e1->semantic(sc); | |
9138 e1 = resolveProperties(sc, e1); | |
9139 e1 = e1->checkToPointer(); | |
9140 e1 = e1->checkToBoolean(); | |
9141 cs1 = sc->callSuper; | |
9142 | |
9143 if (sc->flags & SCOPEstaticif) | |
9144 { | |
9145 /* If in static if, don't evaluate e2 if we don't have to. | |
9146 */ | |
9147 e1 = e1->optimize(WANTflags); | |
9148 if (e1->isBool(FALSE)) | |
9149 { | |
9150 return new IntegerExp(loc, 0, Type::tboolean); | |
9151 } | |
9152 } | |
9153 | |
9154 e2 = e2->semantic(sc); | |
9155 sc->mergeCallSuper(loc, cs1); | |
9156 e2 = resolveProperties(sc, e2); | |
9157 e2 = e2->checkToPointer(); | |
9158 | |
9159 type = Type::tboolean; | |
779
ef7fe998f9b3
Fix return type of OrOr and AndAndExp being set incorrectly if rhs is void. Thanks wilsonk!
Christian Kamm <kamm incasoftware de>
parents:
723
diff
changeset
|
9160 if (e2->type->ty == Tvoid) |
159 | 9161 type = Type::tvoid; |
9162 if (e2->op == TOKtype || e2->op == TOKimport) | |
9163 error("%s is not an expression", e2->toChars()); | |
9164 return this; | |
9165 } | |
9166 | |
9167 Expression *AndAndExp::checkToBoolean() | |
9168 { | |
9169 e2 = e2->checkToBoolean(); | |
9170 return this; | |
9171 } | |
9172 | |
9173 int AndAndExp::isBit() | |
9174 { | |
9175 return TRUE; | |
9176 } | |
9177 | |
9178 int AndAndExp::checkSideEffect(int flag) | |
9179 { | |
9180 if (flag == 2) | |
9181 { | |
9182 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9183 } | |
9184 else | |
9185 { | |
9186 e1->checkSideEffect(1); | |
9187 return e2->checkSideEffect(flag); | |
9188 } | |
9189 } | |
9190 | |
9191 /************************************************************/ | |
9192 | |
9193 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
9194 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
9195 { | |
9196 } | |
9197 | |
9198 Expression *InExp::semantic(Scope *sc) | |
9199 { Expression *e; | |
9200 | |
9201 if (type) | |
9202 return this; | |
9203 | |
9204 BinExp::semanticp(sc); | |
9205 e = op_overload(sc); | |
9206 if (e) | |
9207 return e; | |
9208 | |
9209 //type = Type::tboolean; | |
9210 Type *t2b = e2->type->toBasetype(); | |
9211 if (t2b->ty != Taarray) | |
9212 { | |
9213 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
9214 type = Type::terror; | |
9215 } | |
9216 else | |
9217 { | |
9218 TypeAArray *ta = (TypeAArray *)t2b; | |
9219 | |
9220 // Convert key to type of key | |
9221 e1 = e1->implicitCastTo(sc, ta->index); | |
9222 | |
9223 // 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
|
9224 type = ta->nextOf()->pointerTo(); |
159 | 9225 } |
9226 return this; | |
9227 } | |
9228 | |
9229 int InExp::isBit() | |
9230 { | |
9231 return FALSE; | |
9232 } | |
9233 | |
9234 | |
9235 /************************************************************/ | |
9236 | |
9237 /* This deletes the key e1 from the associative array e2 | |
9238 */ | |
9239 | |
9240 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
9241 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
9242 { | |
9243 type = Type::tvoid; | |
9244 } | |
9245 | |
9246 /************************************************************/ | |
9247 | |
9248 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9249 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
9250 { | |
9251 } | |
9252 | |
9253 Expression *CmpExp::semantic(Scope *sc) | |
9254 { Expression *e; | |
9255 Type *t1; | |
9256 Type *t2; | |
9257 | |
9258 #if LOGSEMANTIC | |
9259 printf("CmpExp::semantic('%s')\n", toChars()); | |
9260 #endif | |
9261 if (type) | |
9262 return this; | |
9263 | |
9264 BinExp::semanticp(sc); | |
9265 | |
9266 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9267 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9268 { | |
9269 error("do not use null when comparing class types"); | |
9270 } | |
9271 | |
9272 e = op_overload(sc); | |
9273 if (e) | |
9274 { | |
9275 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
9276 e = e->semantic(sc); | |
9277 return e; | |
9278 } | |
9279 | |
9280 typeCombine(sc); | |
9281 type = Type::tboolean; | |
9282 | |
9283 // Special handling for array comparisons | |
9284 t1 = e1->type->toBasetype(); | |
9285 t2 = e2->type->toBasetype(); | |
9286 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9287 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9288 { | |
9289 if (!t1->next->equals(t2->next)) | |
9290 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9291 e = this; | |
9292 } | |
9293 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
9294 (t1->ty == Tclass && t2->ty == Tclass)) | |
9295 { | |
9296 if (t2->ty == Tstruct) | |
9297 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
9298 else | |
9299 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
9300 e = this; | |
9301 } | |
9302 #if 1 | |
9303 else if (t1->iscomplex() || t2->iscomplex()) | |
9304 { | |
9305 error("compare not defined for complex operands"); | |
9306 e = new IntegerExp(0); | |
9307 } | |
9308 #endif | |
9309 else | |
9310 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
|
9311 //printf("CmpExp: %s\n", e->toChars()); |
159 | 9312 return e; |
9313 } | |
9314 | |
9315 int CmpExp::isBit() | |
9316 { | |
9317 return TRUE; | |
9318 } | |
9319 | |
9320 | |
9321 /************************************************************/ | |
9322 | |
9323 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9324 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
9325 { | |
9326 assert(op == TOKequal || op == TOKnotequal); | |
9327 } | |
9328 | |
9329 Expression *EqualExp::semantic(Scope *sc) | |
9330 { Expression *e; | |
9331 Type *t1; | |
9332 Type *t2; | |
9333 | |
9334 //printf("EqualExp::semantic('%s')\n", toChars()); | |
9335 if (type) | |
9336 return this; | |
9337 | |
9338 BinExp::semanticp(sc); | |
9339 | |
9340 /* Before checking for operator overloading, check to see if we're | |
9341 * comparing the addresses of two statics. If so, we can just see | |
9342 * if they are the same symbol. | |
9343 */ | |
9344 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
9345 { AddrExp *ae1 = (AddrExp *)e1; | |
9346 AddrExp *ae2 = (AddrExp *)e2; | |
9347 | |
9348 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
9349 { VarExp *ve1 = (VarExp *)ae1->e1; | |
9350 VarExp *ve2 = (VarExp *)ae2->e1; | |
9351 | |
9352 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
9353 { | |
9354 // They are the same, result is 'true' for ==, 'false' for != | |
9355 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
9356 return e; | |
9357 } | |
9358 } | |
9359 } | |
9360 | |
9361 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9362 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9363 { | |
9364 error("use '%s' instead of '%s' when comparing with null", | |
9365 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
9366 Token::toChars(op)); | |
9367 } | |
9368 | |
9369 //if (e2->op != TOKnull) | |
9370 { | |
9371 e = op_overload(sc); | |
9372 if (e) | |
9373 { | |
9374 if (op == TOKnotequal) | |
9375 { | |
9376 e = new NotExp(e->loc, e); | |
9377 e = e->semantic(sc); | |
9378 } | |
9379 return e; | |
9380 } | |
9381 } | |
9382 | |
9383 e = typeCombine(sc); | |
9384 type = Type::tboolean; | |
9385 | |
9386 // Special handling for array comparisons | |
9387 t1 = e1->type->toBasetype(); | |
9388 t2 = e2->type->toBasetype(); | |
9389 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9390 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9391 { | |
9392 if (!t1->next->equals(t2->next)) | |
9393 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9394 } | |
9395 else | |
9396 { | |
9397 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9398 { | |
9399 // Cast both to complex | |
9400 e1 = e1->castTo(sc, Type::tcomplex80); | |
9401 e2 = e2->castTo(sc, Type::tcomplex80); | |
9402 } | |
9403 } | |
9404 return e; | |
9405 } | |
9406 | |
9407 int EqualExp::isBit() | |
9408 { | |
9409 return TRUE; | |
9410 } | |
9411 | |
9412 | |
9413 | |
9414 /************************************************************/ | |
9415 | |
9416 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9417 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
9418 { | |
9419 } | |
9420 | |
9421 Expression *IdentityExp::semantic(Scope *sc) | |
9422 { | |
9423 if (type) | |
9424 return this; | |
9425 | |
9426 BinExp::semanticp(sc); | |
9427 type = Type::tboolean; | |
9428 typeCombine(sc); | |
9429 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9430 { | |
9431 // Cast both to complex | |
9432 e1 = e1->castTo(sc, Type::tcomplex80); | |
9433 e2 = e2->castTo(sc, Type::tcomplex80); | |
9434 } | |
9435 return this; | |
9436 } | |
9437 | |
9438 int IdentityExp::isBit() | |
9439 { | |
9440 return TRUE; | |
9441 } | |
9442 | |
9443 | |
9444 /****************************************************************/ | |
9445 | |
9446 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
9447 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
9448 { | |
9449 this->econd = econd; | |
9450 } | |
9451 | |
9452 Expression *CondExp::syntaxCopy() | |
9453 { | |
9454 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
9455 } | |
9456 | |
9457 | |
9458 Expression *CondExp::semantic(Scope *sc) | |
9459 { Type *t1; | |
9460 Type *t2; | |
9461 unsigned cs0; | |
9462 unsigned cs1; | |
9463 | |
9464 #if LOGSEMANTIC | |
9465 printf("CondExp::semantic('%s')\n", toChars()); | |
9466 #endif | |
9467 if (type) | |
9468 return this; | |
9469 | |
9470 econd = econd->semantic(sc); | |
9471 econd = resolveProperties(sc, econd); | |
9472 econd = econd->checkToPointer(); | |
9473 econd = econd->checkToBoolean(); | |
9474 | |
9475 #if 0 /* this cannot work right because the types of e1 and e2 | |
9476 * both contribute to the type of the result. | |
9477 */ | |
9478 if (sc->flags & SCOPEstaticif) | |
9479 { | |
9480 /* If in static if, don't evaluate what we don't have to. | |
9481 */ | |
9482 econd = econd->optimize(WANTflags); | |
9483 if (econd->isBool(TRUE)) | |
9484 { | |
9485 e1 = e1->semantic(sc); | |
9486 e1 = resolveProperties(sc, e1); | |
9487 return e1; | |
9488 } | |
9489 else if (econd->isBool(FALSE)) | |
9490 { | |
9491 e2 = e2->semantic(sc); | |
9492 e2 = resolveProperties(sc, e2); | |
9493 return e2; | |
9494 } | |
9495 } | |
9496 #endif | |
9497 | |
9498 | |
9499 cs0 = sc->callSuper; | |
9500 e1 = e1->semantic(sc); | |
9501 e1 = resolveProperties(sc, e1); | |
9502 cs1 = sc->callSuper; | |
9503 sc->callSuper = cs0; | |
9504 e2 = e2->semantic(sc); | |
9505 e2 = resolveProperties(sc, e2); | |
9506 sc->mergeCallSuper(loc, cs1); | |
9507 | |
9508 | |
9509 // If either operand is void, the result is void | |
9510 t1 = e1->type; | |
9511 t2 = e2->type; | |
9512 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
9513 type = Type::tvoid; | |
9514 else if (t1 == t2) | |
9515 type = t1; | |
9516 else | |
9517 { | |
9518 typeCombine(sc); | |
9519 switch (e1->type->toBasetype()->ty) | |
9520 { | |
9521 case Tcomplex32: | |
9522 case Tcomplex64: | |
9523 case Tcomplex80: | |
9524 e2 = e2->castTo(sc, e1->type); | |
9525 break; | |
9526 } | |
9527 switch (e2->type->toBasetype()->ty) | |
9528 { | |
9529 case Tcomplex32: | |
9530 case Tcomplex64: | |
9531 case Tcomplex80: | |
9532 e1 = e1->castTo(sc, e2->type); | |
9533 break; | |
9534 } | |
9535 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9536 #if 0 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9537 printf("res: %s\n", type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9538 printf("e1 : %s\n", e1->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9539 printf("e2 : %s\n", e2->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9540 #endif |
159 | 9541 return this; |
9542 } | |
9543 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9544 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9545 int CondExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9546 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9547 return e1->isLvalue() && e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9548 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9549 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9550 |
159 | 9551 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) |
9552 { | |
9553 PtrExp *e; | |
9554 | |
9555 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
9556 e = new PtrExp(loc, this, type); | |
9557 | |
9558 e1 = e1->addressOf(sc); | |
9559 //e1 = e1->toLvalue(sc, NULL); | |
9560 | |
9561 e2 = e2->addressOf(sc); | |
9562 //e2 = e2->toLvalue(sc, NULL); | |
9563 | |
9564 typeCombine(sc); | |
9565 | |
9566 type = e2->type; | |
9567 return e; | |
9568 } | |
9569 | |
9570 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
9571 { | |
9572 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
9573 return this; | |
9574 } | |
9575 | |
9576 void CondExp::checkEscape() | |
9577 { | |
9578 e1->checkEscape(); | |
9579 e2->checkEscape(); | |
9580 } | |
9581 | |
9582 | |
9583 Expression *CondExp::checkToBoolean() | |
9584 { | |
9585 e1 = e1->checkToBoolean(); | |
9586 e2 = e2->checkToBoolean(); | |
9587 return this; | |
9588 } | |
9589 | |
9590 int CondExp::checkSideEffect(int flag) | |
9591 { | |
9592 if (flag == 2) | |
9593 { | |
9594 return econd->checkSideEffect(2) || | |
9595 e1->checkSideEffect(2) || | |
9596 e2->checkSideEffect(2); | |
9597 } | |
9598 else | |
9599 { | |
9600 econd->checkSideEffect(1); | |
9601 e1->checkSideEffect(flag); | |
9602 return e2->checkSideEffect(flag); | |
9603 } | |
9604 } | |
9605 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9606 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9607 int CondExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9608 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9609 return econd->canThrow() || e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9610 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9611 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9612 |
159 | 9613 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
9614 { | |
9615 expToCBuffer(buf, hgs, econd, PREC_oror); | |
9616 buf->writestring(" ? "); | |
9617 expToCBuffer(buf, hgs, e1, PREC_expr); | |
9618 buf->writestring(" : "); | |
9619 expToCBuffer(buf, hgs, e2, PREC_cond); | |
9620 } | |
9621 | |
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
|
9622 /************************************************************/ |
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
|
9623 |
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
|
9624 #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
|
9625 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9626 // 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
|
9627 |
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
|
9628 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
|
9629 : 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
|
9630 { |
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
|
9631 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
|
9632 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
|
9633 } |
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
|
9634 |
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
|
9635 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
|
9636 { |
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
|
9637 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
|
9638 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
|
9639 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
|
9640 } |
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
|
9641 |
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
|
9642 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
|
9643 { |
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
|
9644 // 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
|
9645 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
|
9646 } |
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
|
9647 |
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
|
9648 #endif |