Mercurial > projects > ldc
annotate dmd/expression.c @ 1257:7af860e4f403
Changes for mingw to compile properly
author | Kelly Wilson <wilsonk cpsc.ucalgary.ca> |
---|---|
date | Wed, 22 Apr 2009 14:49:49 -0600 |
parents | 79758fd2f48a |
children | 192529570813 |
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 |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
37 #include "rmem.h" |
159 | 38 |
39 //#include "port.h" | |
40 #include "mtype.h" | |
41 #include "init.h" | |
42 #include "expression.h" | |
43 #include "template.h" | |
44 #include "utf.h" | |
45 #include "enum.h" | |
46 #include "scope.h" | |
47 #include "statement.h" | |
48 #include "declaration.h" | |
49 #include "aggregate.h" | |
50 #include "import.h" | |
51 #include "id.h" | |
52 #include "dsymbol.h" | |
53 #include "module.h" | |
54 #include "attrib.h" | |
55 #include "hdrgen.h" | |
56 #include "parse.h" | |
57 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
58 Expression *expandVar(int result, VarDeclaration *v); |
159 | 59 |
60 #define LOGSEMANTIC 0 | |
61 | |
62 /********************************** | |
63 * Set operator precedence for each operator. | |
64 */ | |
65 | |
66 // Operator precedence - greater values are higher precedence | |
67 | |
68 enum PREC | |
69 { | |
70 PREC_zero, | |
71 PREC_expr, | |
72 PREC_assign, | |
73 PREC_cond, | |
74 PREC_oror, | |
75 PREC_andand, | |
76 PREC_or, | |
77 PREC_xor, | |
78 PREC_and, | |
79 PREC_equal, | |
80 PREC_rel, | |
81 PREC_shift, | |
82 PREC_add, | |
83 PREC_mul, | |
84 PREC_unary, | |
85 PREC_primary, | |
86 }; | |
87 | |
88 enum PREC precedence[TOKMAX]; | |
89 | |
90 void initPrecedence() | |
91 { | |
92 precedence[TOKdotvar] = PREC_primary; | |
93 precedence[TOKimport] = PREC_primary; | |
94 precedence[TOKidentifier] = PREC_primary; | |
95 precedence[TOKthis] = PREC_primary; | |
96 precedence[TOKsuper] = PREC_primary; | |
97 precedence[TOKint64] = PREC_primary; | |
98 precedence[TOKfloat64] = PREC_primary; | |
99 precedence[TOKnull] = PREC_primary; | |
100 precedence[TOKstring] = PREC_primary; | |
101 precedence[TOKarrayliteral] = PREC_primary; | |
102 precedence[TOKtypedot] = PREC_primary; | |
103 precedence[TOKtypeid] = PREC_primary; | |
104 precedence[TOKis] = PREC_primary; | |
105 precedence[TOKassert] = PREC_primary; | |
106 precedence[TOKfunction] = PREC_primary; | |
107 precedence[TOKvar] = PREC_primary; | |
336 | 108 #if DMDV2 |
109 precedence[TOKdefault] = PREC_primary; | |
110 #endif | |
159 | 111 |
112 // post | |
113 precedence[TOKdotti] = PREC_primary; | |
114 precedence[TOKdot] = PREC_primary; | |
115 // precedence[TOKarrow] = PREC_primary; | |
116 precedence[TOKplusplus] = PREC_primary; | |
117 precedence[TOKminusminus] = PREC_primary; | |
118 precedence[TOKcall] = PREC_primary; | |
119 precedence[TOKslice] = PREC_primary; | |
120 precedence[TOKarray] = PREC_primary; | |
121 | |
122 precedence[TOKaddress] = PREC_unary; | |
123 precedence[TOKstar] = PREC_unary; | |
124 precedence[TOKneg] = PREC_unary; | |
125 precedence[TOKuadd] = PREC_unary; | |
126 precedence[TOKnot] = PREC_unary; | |
127 precedence[TOKtobool] = PREC_add; | |
128 precedence[TOKtilde] = PREC_unary; | |
129 precedence[TOKdelete] = PREC_unary; | |
130 precedence[TOKnew] = PREC_unary; | |
131 precedence[TOKcast] = PREC_unary; | |
132 | |
133 precedence[TOKmul] = PREC_mul; | |
134 precedence[TOKdiv] = PREC_mul; | |
135 precedence[TOKmod] = PREC_mul; | |
136 | |
137 precedence[TOKadd] = PREC_add; | |
138 precedence[TOKmin] = PREC_add; | |
139 precedence[TOKcat] = PREC_add; | |
140 | |
141 precedence[TOKshl] = PREC_shift; | |
142 precedence[TOKshr] = PREC_shift; | |
143 precedence[TOKushr] = PREC_shift; | |
144 | |
145 precedence[TOKlt] = PREC_rel; | |
146 precedence[TOKle] = PREC_rel; | |
147 precedence[TOKgt] = PREC_rel; | |
148 precedence[TOKge] = PREC_rel; | |
149 precedence[TOKunord] = PREC_rel; | |
150 precedence[TOKlg] = PREC_rel; | |
151 precedence[TOKleg] = PREC_rel; | |
152 precedence[TOKule] = PREC_rel; | |
153 precedence[TOKul] = PREC_rel; | |
154 precedence[TOKuge] = PREC_rel; | |
155 precedence[TOKug] = PREC_rel; | |
156 precedence[TOKue] = PREC_rel; | |
157 precedence[TOKin] = PREC_rel; | |
158 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
159 #if 0 |
159 | 160 precedence[TOKequal] = PREC_equal; |
161 precedence[TOKnotequal] = PREC_equal; | |
162 precedence[TOKidentity] = PREC_equal; | |
163 precedence[TOKnotidentity] = PREC_equal; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
164 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
165 /* 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
|
166 * 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
|
167 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
168 precedence[TOKequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
169 precedence[TOKnotequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
170 precedence[TOKidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
171 precedence[TOKnotidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
172 #endif |
159 | 173 |
174 precedence[TOKand] = PREC_and; | |
175 | |
176 precedence[TOKxor] = PREC_xor; | |
177 | |
178 precedence[TOKor] = PREC_or; | |
179 | |
180 precedence[TOKandand] = PREC_andand; | |
181 | |
182 precedence[TOKoror] = PREC_oror; | |
183 | |
184 precedence[TOKquestion] = PREC_cond; | |
185 | |
186 precedence[TOKassign] = PREC_assign; | |
187 precedence[TOKconstruct] = PREC_assign; | |
188 precedence[TOKblit] = PREC_assign; | |
189 precedence[TOKaddass] = PREC_assign; | |
190 precedence[TOKminass] = PREC_assign; | |
191 precedence[TOKcatass] = PREC_assign; | |
192 precedence[TOKmulass] = PREC_assign; | |
193 precedence[TOKdivass] = PREC_assign; | |
194 precedence[TOKmodass] = PREC_assign; | |
195 precedence[TOKshlass] = PREC_assign; | |
196 precedence[TOKshrass] = PREC_assign; | |
197 precedence[TOKushrass] = PREC_assign; | |
198 precedence[TOKandass] = PREC_assign; | |
199 precedence[TOKorass] = PREC_assign; | |
200 precedence[TOKxorass] = PREC_assign; | |
201 | |
202 precedence[TOKcomma] = PREC_expr; | |
203 } | |
204 | |
336 | 205 /************************************************************* |
206 * Given var, we need to get the | |
207 * right 'this' pointer if var is in an outer class, but our | |
208 * existing 'this' pointer is in an inner class. | |
209 * Input: | |
210 * e1 existing 'this' | |
211 * ad struct or class we need the correct 'this' for | |
212 * var the specific member of ad we're accessing | |
213 */ | |
214 | |
215 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, | |
216 Expression *e1, Declaration *var) | |
217 { | |
218 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars()); | |
219 L1: | |
220 Type *t = e1->type->toBasetype(); | |
221 //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars()); | |
222 | |
223 /* If e1 is not the 'this' pointer for ad | |
224 */ | |
225 if (ad && | |
226 !(t->ty == Tpointer && t->nextOf()->ty == Tstruct && | |
227 ((TypeStruct *)t->nextOf())->sym == ad) | |
228 && | |
229 !(t->ty == Tstruct && | |
230 ((TypeStruct *)t)->sym == ad) | |
231 ) | |
232 { | |
233 ClassDeclaration *cd = ad->isClassDeclaration(); | |
234 ClassDeclaration *tcd = t->isClassHandle(); | |
235 | |
236 /* e1 is the right this if ad is a base class of e1 | |
237 */ | |
238 if (!cd || !tcd || | |
239 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
240 ) | |
241 { | |
242 /* Only classes can be inner classes with an 'outer' | |
243 * member pointing to the enclosing class instance | |
244 */ | |
245 if (tcd && tcd->isNested()) | |
246 { /* e1 is the 'this' pointer for an inner class: tcd. | |
247 * Rewrite it as the 'this' pointer for the outer class. | |
248 */ | |
249 | |
250 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
251 e1->type = tcd->vthis->type; | |
252 // Do not call checkNestedRef() | |
253 //e1 = e1->semantic(sc); | |
254 | |
255 // Skip up over nested functions, and get the enclosing | |
256 // class type. | |
257 int n = 0; | |
258 Dsymbol *s; | |
259 for (s = tcd->toParent(); | |
260 s && s->isFuncDeclaration(); | |
261 s = s->toParent()) | |
262 { FuncDeclaration *f = s->isFuncDeclaration(); | |
263 if (f->vthis) | |
264 { | |
265 //printf("rewriting e1 to %s's this\n", f->toChars()); | |
266 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
|
267 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
268 // 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
|
269 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
|
270 |
336 | 271 e1 = new VarExp(loc, f->vthis); |
272 } | |
273 } | |
274 if (s && s->isClassDeclaration()) | |
275 { e1->type = s->isClassDeclaration()->type; | |
276 if (n > 1) | |
277 e1 = e1->semantic(sc); | |
278 } | |
279 else | |
280 e1 = e1->semantic(sc); | |
281 goto L1; | |
282 } | |
283 /* Can't find a path from e1 to ad | |
284 */ | |
285 e1->error("this for %s needs to be type %s not type %s", | |
286 var->toChars(), ad->toChars(), t->toChars()); | |
287 } | |
288 } | |
289 return e1; | |
290 } | |
291 | |
159 | 292 /***************************************** |
293 * Determine if 'this' is available. | |
294 * If it is, return the FuncDeclaration that has it. | |
295 */ | |
296 | |
297 FuncDeclaration *hasThis(Scope *sc) | |
298 { FuncDeclaration *fd; | |
299 FuncDeclaration *fdthis; | |
300 | |
301 //printf("hasThis()\n"); | |
302 fdthis = sc->parent->isFuncDeclaration(); | |
303 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
304 | |
305 // Go upwards until we find the enclosing member function | |
306 fd = fdthis; | |
307 while (1) | |
308 { | |
309 if (!fd) | |
310 { | |
311 goto Lno; | |
312 } | |
313 if (!fd->isNested()) | |
314 break; | |
315 | |
316 Dsymbol *parent = fd->parent; | |
317 while (parent) | |
318 { | |
319 TemplateInstance *ti = parent->isTemplateInstance(); | |
320 if (ti) | |
321 parent = ti->parent; | |
322 else | |
323 break; | |
324 } | |
325 | |
326 fd = fd->parent->isFuncDeclaration(); | |
327 } | |
328 | |
329 if (!fd->isThis()) | |
330 { //printf("test '%s'\n", fd->toChars()); | |
331 goto Lno; | |
332 } | |
333 | |
334 assert(fd->vthis); | |
335 return fd; | |
336 | |
337 Lno: | |
338 return NULL; // don't have 'this' available | |
339 } | |
340 | |
341 | |
342 /*************************************** | |
343 * Pull out any properties. | |
344 */ | |
345 | |
346 Expression *resolveProperties(Scope *sc, Expression *e) | |
347 { | |
348 //printf("resolveProperties(%s)\n", e->toChars()); | |
349 if (e->type) | |
350 { | |
351 Type *t = e->type->toBasetype(); | |
352 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
353 if (t->ty == Tfunction /*|| e->op == TOKoverloadset*/) |
159 | 354 { |
355 e = new CallExp(e->loc, e); | |
356 e = e->semantic(sc); | |
357 } | |
358 | |
359 /* Look for e being a lazy parameter; rewrite as delegate call | |
360 */ | |
361 else if (e->op == TOKvar) | |
362 { VarExp *ve = (VarExp *)e; | |
363 | |
364 if (ve->var->storage_class & STClazy) | |
365 { | |
366 e = new CallExp(e->loc, e); | |
367 e = e->semantic(sc); | |
368 } | |
369 } | |
370 | |
371 else if (e->op == TOKdotexp) | |
372 { | |
373 e->error("expression has no value"); | |
374 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
375 |
159 | 376 } |
377 return e; | |
378 } | |
379 | |
380 /****************************** | |
381 * Perform semantic() on an array of Expressions. | |
382 */ | |
383 | |
384 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
385 { | |
386 if (exps) | |
387 { | |
388 for (size_t i = 0; i < exps->dim; i++) | |
389 { Expression *e = (Expression *)exps->data[i]; | |
390 | |
391 e = e->semantic(sc); | |
392 exps->data[i] = (void *)e; | |
393 } | |
394 } | |
395 } | |
396 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
397 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
398 /****************************** |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
399 * 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
|
400 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
401 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
402 #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
|
403 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
|
404 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
405 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
|
406 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
407 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
|
408 { 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
|
409 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
|
410 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
|
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 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
413 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
|
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 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
416 |
159 | 417 /**************************************** |
418 * Expand tuples. | |
419 */ | |
420 | |
421 void expandTuples(Expressions *exps) | |
422 { | |
423 //printf("expandTuples()\n"); | |
424 if (exps) | |
425 { | |
426 for (size_t i = 0; i < exps->dim; i++) | |
427 { Expression *arg = (Expression *)exps->data[i]; | |
428 if (!arg) | |
429 continue; | |
430 | |
431 // Look for tuple with 0 members | |
432 if (arg->op == TOKtype) | |
433 { TypeExp *e = (TypeExp *)arg; | |
434 if (e->type->toBasetype()->ty == Ttuple) | |
435 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
436 | |
437 if (!tt->arguments || tt->arguments->dim == 0) | |
438 { | |
439 exps->remove(i); | |
440 if (i == exps->dim) | |
441 return; | |
442 i--; | |
443 continue; | |
444 } | |
445 } | |
446 } | |
447 | |
448 // Inline expand all the tuples | |
449 while (arg->op == TOKtuple) | |
450 { TupleExp *te = (TupleExp *)arg; | |
451 | |
452 exps->remove(i); // remove arg | |
453 exps->insert(i, te->exps); // replace with tuple contents | |
454 if (i == exps->dim) | |
455 return; // empty tuple, no more arguments | |
456 arg = (Expression *)exps->data[i]; | |
457 } | |
458 } | |
459 } | |
460 } | |
461 | |
462 /**************************************** | |
463 * Preprocess arguments to function. | |
464 */ | |
465 | |
466 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps) | |
467 { | |
468 if (exps) | |
469 { | |
470 expandTuples(exps); | |
471 | |
472 for (size_t i = 0; i < exps->dim; i++) | |
473 { Expression *arg = (Expression *)exps->data[i]; | |
474 | |
475 if (!arg->type) | |
476 { | |
477 #ifdef DEBUG | |
478 if (!global.gag) | |
479 printf("1: \n"); | |
480 #endif | |
481 arg->error("%s is not an expression", arg->toChars()); | |
482 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
483 } | |
484 | |
485 arg = resolveProperties(sc, arg); | |
486 exps->data[i] = (void *) arg; | |
487 | |
488 //arg->rvalue(); | |
489 #if 0 | |
490 if (arg->type->ty == Tfunction) | |
491 { | |
492 arg = new AddrExp(arg->loc, arg); | |
493 arg = arg->semantic(sc); | |
494 exps->data[i] = (void *) arg; | |
495 } | |
496 #endif | |
497 } | |
498 } | |
499 } | |
500 | |
336 | 501 /********************************************* |
502 * Call copy constructor for struct value argument. | |
503 */ | |
504 #if DMDV2 | |
505 Expression *callCpCtor(Loc loc, Scope *sc, Expression *e) | |
506 { | |
507 Type *tb = e->type->toBasetype(); | |
508 assert(tb->ty == Tstruct); | |
509 StructDeclaration *sd = ((TypeStruct *)tb)->sym; | |
510 if (sd->cpctor) | |
511 { | |
512 /* Create a variable tmp, and replace the argument e with: | |
513 * (tmp = e),tmp | |
514 * and let AssignExp() handle the construction. | |
515 * This is not the most efficent, ideally tmp would be constructed | |
516 * directly onto the stack. | |
517 */ | |
518 Identifier *idtmp = Lexer::uniqueId("__tmp"); | |
519 VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e)); | |
520 Expression *ae = new DeclarationExp(loc, tmp); | |
521 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
522 e = e->semantic(sc); | |
523 } | |
524 return e; | |
525 } | |
526 #endif | |
159 | 527 |
528 /**************************************** | |
529 * Now that we know the exact type of the function we're calling, | |
530 * 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
|
531 * 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
|
532 * 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
|
533 * 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
|
534 * 4. add hidden _arguments[] argument |
159 | 535 */ |
536 | |
537 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) | |
538 { | |
539 unsigned n; | |
540 | |
541 //printf("functionArguments()\n"); | |
542 assert(arguments); | |
543 size_t nargs = arguments ? arguments->dim : 0; | |
544 size_t nparams = Argument::dim(tf->parameters); | |
545 | |
546 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
|
547 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 548 |
549 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
550 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
551 int done = 0; |
159 | 552 for (size_t i = 0; i < n; i++) |
553 { | |
554 Expression *arg; | |
555 | |
556 if (i < nargs) | |
557 arg = (Expression *)arguments->data[i]; | |
558 else | |
559 arg = NULL; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
560 Type *tb; |
159 | 561 |
562 if (i < nparams) | |
563 { | |
564 Argument *p = Argument::getNth(tf->parameters, i); | |
565 | |
566 if (!arg) | |
567 { | |
568 if (!p->defaultArg) | |
569 { | |
570 if (tf->varargs == 2 && i + 1 == nparams) | |
571 goto L2; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
572 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 573 break; |
574 } | |
336 | 575 arg = p->defaultArg; |
576 #if DMDV2 | |
577 if (arg->op == TOKdefault) | |
578 { DefaultInitExp *de = (DefaultInitExp *)arg; | |
579 arg = de->resolve(loc, sc); | |
580 } | |
581 else | |
582 #endif | |
583 arg = arg->copy(); | |
159 | 584 arguments->push(arg); |
585 nargs++; | |
586 } | |
587 | |
588 if (tf->varargs == 2 && i + 1 == nparams) | |
589 { | |
590 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
591 if (arg->implicitConvTo(p->type)) | |
592 { | |
593 if (nargs != nparams) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
594 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 595 goto L1; |
596 } | |
597 L2: | |
598 Type *tb = p->type->toBasetype(); | |
599 Type *tret = p->isLazyArray(); | |
600 switch (tb->ty) | |
601 { | |
602 case Tsarray: | |
603 case Tarray: | |
604 { // Create a static array variable v of type arg->type | |
605 #ifdef IN_GCC | |
606 /* GCC 4.0 does not like zero length arrays used like | |
607 this; pass a null array value instead. Could also | |
608 just make a one-element array. */ | |
609 if (nargs - i == 0) | |
610 { | |
611 arg = new NullExp(loc); | |
612 break; | |
613 } | |
614 #endif | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
615 Identifier *id = Lexer::uniqueId("__arrayArg"); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
616 Type *t = new TypeSArray(((TypeArray *)tb)->next, new IntegerExp(nargs - i)); |
159 | 617 t = t->semantic(loc, sc); |
618 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
619 v->semantic(sc); | |
620 v->parent = sc->parent; | |
621 //sc->insert(v); | |
622 | |
623 Expression *c = new DeclarationExp(0, v); | |
624 c->type = v->type; | |
625 | |
626 for (size_t u = i; u < nargs; u++) | |
627 { Expression *a = (Expression *)arguments->data[u]; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
628 if (tret && !((TypeArray *)tb)->next->equals(a->type)) |
159 | 629 a = a->toDelegate(sc, tret); |
630 | |
631 Expression *e = new VarExp(loc, v); | |
632 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
|
633 AssignExp *ae = new AssignExp(loc, e, a); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
634 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
635 ae->op = TOKconstruct; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
636 #endif |
159 | 637 if (c) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
638 c = new CommaExp(loc, c, ae); |
159 | 639 else |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
640 c = ae; |
159 | 641 } |
642 arg = new VarExp(loc, v); | |
643 if (c) | |
644 arg = new CommaExp(loc, c, arg); | |
645 break; | |
646 } | |
647 case Tclass: | |
648 { /* Set arg to be: | |
649 * new Tclass(arg0, arg1, ..., argn) | |
650 */ | |
651 Expressions *args = new Expressions(); | |
652 args->setDim(nargs - i); | |
653 for (size_t u = i; u < nargs; u++) | |
654 args->data[u - i] = arguments->data[u]; | |
655 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
656 break; | |
657 } | |
658 default: | |
659 if (!arg) | |
660 { error(loc, "not enough arguments"); | |
661 return; | |
662 } | |
663 break; | |
664 } | |
665 arg = arg->semantic(sc); | |
666 //printf("\targ = '%s'\n", arg->toChars()); | |
667 arguments->setDim(i + 1); | |
668 done = 1; | |
669 } | |
670 | |
671 L1: | |
672 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
673 arg = arg->implicitCastTo(sc, p->type); | |
674 if (p->storageClass & (STCout | STCref)) | |
675 { | |
676 // BUG: should check that argument to ref is type 'invariant' | |
677 // BUG: assignments to ref should also be type 'invariant' | |
678 arg = arg->modifiableLvalue(sc, arg); | |
679 | |
680 //if (arg->op == TOKslice) | |
681 //arg->error("cannot modify slice %s", arg->toChars()); | |
682 } | |
683 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
684 // 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
|
685 #if !IN_LLVM |
159 | 686 // Convert static arrays to pointers |
687 tb = arg->type->toBasetype(); | |
688 if (tb->ty == Tsarray) | |
689 { | |
690 arg = arg->checkToPointer(); | |
691 } | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
692 #endif |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
693 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
694 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
|
695 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
696 arg = callCpCtor(loc, sc, arg); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
697 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
698 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
699 |
159 | 700 // Convert lazy argument to a delegate |
701 if (p->storageClass & STClazy) | |
702 { | |
703 arg = arg->toDelegate(sc, p->type); | |
704 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
705 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
706 /* 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
|
707 * function. |
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 if (!tf->parameterEscapes(p)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
710 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
711 /* 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
|
712 * 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
|
713 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
714 if (arg->op == TOKfunction) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
715 { FuncExp *fe = (FuncExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
716 fe->fd->tookAddressOf = 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
717 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
718 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
719 /* 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
|
720 * 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
|
721 * 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
|
722 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
723 else if (arg->op == TOKdelegate) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
724 { DelegateExp *de = (DelegateExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
725 if (de->e1->op == TOKvar) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
726 { VarExp *ve = (VarExp *)de->e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
727 FuncDeclaration *f = ve->var->isFuncDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
728 if (f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
729 { f->tookAddressOf--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
730 //printf("tookAddressOf = %d\n", f->tookAddressOf); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
731 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
732 } |
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 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
735 #endif |
159 | 736 } |
737 else | |
738 { | |
739 | |
740 // 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
|
741 // 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
|
742 if (tf->linkage != LINKd && tf->linkage != LINKintrinsic) |
159 | 743 { |
744 // Promote bytes, words, etc., to ints | |
745 arg = arg->integralPromotions(sc); | |
746 | |
747 // Promote floats to doubles | |
748 switch (arg->type->ty) | |
749 { | |
750 case Tfloat32: | |
751 arg = arg->castTo(sc, Type::tfloat64); | |
752 break; | |
753 | |
754 case Timaginary32: | |
755 arg = arg->castTo(sc, Type::timaginary64); | |
756 break; | |
757 } | |
758 } | |
759 | |
760 // Convert static arrays to dynamic arrays | |
761 tb = arg->type->toBasetype(); | |
762 if (tb->ty == Tsarray) | |
763 { TypeSArray *ts = (TypeSArray *)tb; | |
336 | 764 Type *ta = ts->next->arrayOf(); |
159 | 765 if (ts->size(arg->loc) == 0) |
766 { arg = new NullExp(arg->loc); | |
767 arg->type = ta; | |
768 } | |
769 else | |
770 arg = arg->castTo(sc, ta); | |
771 } | |
336 | 772 #if DMDV2 |
773 if (tb->ty == Tstruct) | |
774 { | |
775 arg = callCpCtor(loc, sc, arg); | |
776 } | |
777 | |
778 // Give error for overloaded function addresses | |
779 if (arg->op == TOKsymoff) | |
780 { SymOffExp *se = (SymOffExp *)arg; | |
781 if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique()) | |
782 arg->error("function %s is overloaded", arg->toChars()); | |
783 } | |
784 #endif | |
159 | 785 arg->rvalue(); |
786 } | |
787 arg = arg->optimize(WANTvalue); | |
788 arguments->data[i] = (void *) arg; | |
789 if (done) | |
790 break; | |
791 } | |
792 | |
793 #if !IN_LLVM | |
794 // If D linkage and variadic, add _arguments[] as first argument | |
795 if (tf->linkage == LINKd && tf->varargs == 1) | |
796 { | |
797 Expression *e; | |
798 | |
799 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], | |
800 arguments->dim - nparams); | |
801 arguments->insert(0, e); | |
802 } | |
803 #endif | |
804 } | |
805 | |
806 /************************************************** | |
807 * Write expression out to buf, but wrap it | |
808 * in ( ) if its precedence is less than pr. | |
809 */ | |
810 | |
811 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
812 { | |
336 | 813 //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
|
814 if (precedence[e->op] < pr || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
815 /* 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
|
816 * They must be parenthesized. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
817 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
818 (pr == PREC_rel && precedence[e->op] == pr)) |
159 | 819 { |
820 buf->writeByte('('); | |
821 e->toCBuffer(buf, hgs); | |
822 buf->writeByte(')'); | |
823 } | |
824 else | |
825 e->toCBuffer(buf, hgs); | |
826 } | |
827 | |
828 /************************************************** | |
829 * Write out argument list to buf. | |
830 */ | |
831 | |
832 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
833 { | |
834 if (arguments) | |
835 { | |
836 for (size_t i = 0; i < arguments->dim; i++) | |
837 { Expression *arg = (Expression *)arguments->data[i]; | |
838 | |
839 if (arg) | |
840 { if (i) | |
841 buf->writeByte(','); | |
842 expToCBuffer(buf, hgs, arg, PREC_assign); | |
843 } | |
844 } | |
845 } | |
846 } | |
847 | |
848 /************************************************** | |
849 * Write out argument types to buf. | |
850 */ | |
851 | |
852 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
853 { | |
854 if (arguments) | |
855 { OutBuffer argbuf; | |
856 | |
857 for (size_t i = 0; i < arguments->dim; i++) | |
858 { Expression *arg = (Expression *)arguments->data[i]; | |
859 | |
860 if (i) | |
861 buf->writeByte(','); | |
862 argbuf.reset(); | |
863 arg->type->toCBuffer2(&argbuf, hgs, 0); | |
864 buf->write(&argbuf); | |
865 } | |
866 } | |
867 } | |
868 | |
869 /******************************** Expression **************************/ | |
870 | |
871 Expression::Expression(Loc loc, enum TOK op, int size) | |
872 : loc(loc) | |
873 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
874 //printf("Expression::Expression(op = %d) this = %p\n", op, this); |
159 | 875 this->loc = loc; |
876 this->op = op; | |
877 this->size = size; | |
878 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
|
879 |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
880 #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
|
881 cachedLvalue = NULL; |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
882 #endif |
159 | 883 } |
884 | |
885 Expression *Expression::syntaxCopy() | |
886 { | |
887 //printf("Expression::syntaxCopy()\n"); | |
888 //dump(0); | |
889 return copy(); | |
890 } | |
891 | |
892 /********************************* | |
893 * Does *not* do a deep copy. | |
894 */ | |
895 | |
896 Expression *Expression::copy() | |
897 { | |
898 Expression *e; | |
899 if (!size) | |
900 { | |
901 #ifdef DEBUG | |
902 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
903 printf("op = %d\n", op); | |
904 dump(0); | |
905 #endif | |
906 assert(0); | |
907 } | |
908 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
|
909 //printf("Expression::copy(op = %d) e = %p\n", op, e); |
159 | 910 return (Expression *)memcpy(e, this, size); |
911 } | |
912 | |
913 /************************** | |
914 * Semantically analyze Expression. | |
915 * Determine types, fold constants, etc. | |
916 */ | |
917 | |
918 Expression *Expression::semantic(Scope *sc) | |
919 { | |
920 #if LOGSEMANTIC | |
336 | 921 printf("Expression::semantic() %s\n", toChars()); |
159 | 922 #endif |
923 if (type) | |
924 type = type->semantic(loc, sc); | |
925 else | |
926 type = Type::tvoid; | |
927 return this; | |
928 } | |
929 | |
930 void Expression::print() | |
931 { | |
932 fprintf(stdmsg, "%s\n", toChars()); | |
933 fflush(stdmsg); | |
934 } | |
935 | |
936 char *Expression::toChars() | |
937 { OutBuffer *buf; | |
938 HdrGenState hgs; | |
939 | |
940 memset(&hgs, 0, sizeof(hgs)); | |
941 buf = new OutBuffer(); | |
942 toCBuffer(buf, &hgs); | |
943 return buf->toChars(); | |
944 } | |
945 | |
946 void Expression::error(const char *format, ...) | |
947 { | |
948 va_list ap; | |
949 va_start(ap, format); | |
950 ::verror(loc, format, ap); | |
951 va_end( ap ); | |
952 } | |
953 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
954 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
|
955 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
956 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
|
957 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
958 va_list ap; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
959 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
|
960 ::vwarning(loc, format, ap); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
961 va_end( ap ); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
962 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
963 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
964 |
159 | 965 void Expression::rvalue() |
966 { | |
967 if (type && type->toBasetype()->ty == Tvoid) | |
968 { error("expression %s is void and has no value", toChars()); | |
969 #if 0 | |
970 dump(0); | |
971 halt(); | |
972 #endif | |
336 | 973 type = Type::tint32; |
159 | 974 } |
975 } | |
976 | |
977 Expression *Expression::combine(Expression *e1, Expression *e2) | |
978 { | |
979 if (e1) | |
980 { | |
981 if (e2) | |
982 { | |
983 e1 = new CommaExp(e1->loc, e1, e2); | |
984 e1->type = e2->type; | |
985 } | |
986 } | |
987 else | |
988 e1 = e2; | |
989 return e1; | |
990 } | |
991 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
992 dinteger_t Expression::toInteger() |
159 | 993 { |
994 //printf("Expression %s\n", Token::toChars(op)); | |
995 error("Integer constant expression expected instead of %s", toChars()); | |
996 return 0; | |
997 } | |
998 | |
999 uinteger_t Expression::toUInteger() | |
1000 { | |
1001 //printf("Expression %s\n", Token::toChars(op)); | |
1002 return (uinteger_t)toInteger(); | |
1003 } | |
1004 | |
1005 real_t Expression::toReal() | |
1006 { | |
1007 error("Floating point constant expression expected instead of %s", toChars()); | |
1008 return 0; | |
1009 } | |
1010 | |
1011 real_t Expression::toImaginary() | |
1012 { | |
1013 error("Floating point constant expression expected instead of %s", toChars()); | |
1014 return 0; | |
1015 } | |
1016 | |
1017 complex_t Expression::toComplex() | |
1018 { | |
1019 error("Floating point constant expression expected instead of %s", toChars()); | |
1020 #ifdef IN_GCC | |
1021 return complex_t(real_t(0)); // %% nicer | |
1022 #else | |
1023 return 0; | |
1024 #endif | |
1025 } | |
1026 | |
1027 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1028 { | |
1029 buf->writestring(Token::toChars(op)); | |
1030 } | |
1031 | |
1032 void Expression::toMangleBuffer(OutBuffer *buf) | |
1033 { | |
1034 error("expression %s is not a valid template value argument", toChars()); | |
1035 } | |
1036 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1037 /*************************************** |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1038 * Return !=0 if expression is an lvalue. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1039 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1040 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1041 int Expression::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1042 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1043 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1044 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1045 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1046 |
159 | 1047 /******************************* |
1048 * Give error if we're not an lvalue. | |
1049 * If we can, convert expression to be an lvalue. | |
1050 */ | |
1051 | |
1052 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
1053 { | |
1054 if (!e) | |
1055 e = this; | |
1056 else if (!loc.filename) | |
1057 loc = e->loc; | |
1058 error("%s is not an lvalue", e->toChars()); | |
1059 return this; | |
1060 } | |
1061 | |
1062 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
1063 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1064 //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
|
1065 |
159 | 1066 // 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
|
1067 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1068 if (type && (!type->isMutable() || !type->isAssignable())) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1069 error("%s is not mutable", e->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1070 #endif |
159 | 1071 return toLvalue(sc, e); |
1072 } | |
1073 | |
1074 /************************************ | |
1075 * Detect cases where pointers to the stack can 'escape' the | |
1076 * lifetime of the stack frame. | |
1077 */ | |
1078 | |
1079 void Expression::checkEscape() | |
1080 { | |
1081 } | |
1082 | |
1083 void Expression::checkScalar() | |
1084 { | |
1085 if (!type->isscalar()) | |
1086 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
1087 } | |
1088 | |
1089 void Expression::checkNoBool() | |
1090 { | |
1091 if (type->toBasetype()->ty == Tbool) | |
1092 error("operation not allowed on bool '%s'", toChars()); | |
1093 } | |
1094 | |
1095 Expression *Expression::checkIntegral() | |
1096 { | |
1097 if (!type->isintegral()) | |
1098 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
1099 return new IntegerExp(0); | |
1100 } | |
1101 return this; | |
1102 } | |
1103 | |
1104 Expression *Expression::checkArithmetic() | |
1105 { | |
1106 if (!type->isintegral() && !type->isfloating()) | |
1107 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); | |
1108 return new IntegerExp(0); | |
1109 } | |
1110 return this; | |
1111 } | |
1112 | |
1113 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
1114 { | |
1115 s->checkDeprecated(loc, sc); | |
1116 } | |
1117 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1118 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1119 void Expression::checkPurity(Scope *sc, FuncDeclaration *f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1120 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1121 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
|
1122 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
|
1123 sc->func->toChars(), f->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1124 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1125 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1126 |
159 | 1127 /******************************** |
1128 * Check for expressions that have no use. | |
1129 * Input: | |
1130 * flag 0 not going to use the result, so issue error message if no | |
1131 * side effects | |
1132 * 1 the result of the expression is used, but still check | |
1133 * for useless subexpressions | |
1134 * 2 do not issue error messages, just return !=0 if expression | |
1135 * has side effects | |
1136 */ | |
1137 | |
1138 int Expression::checkSideEffect(int flag) | |
1139 { | |
1140 if (flag == 0) | |
1141 { if (op == TOKimport) | |
1142 { | |
1143 error("%s has no effect", toChars()); | |
1144 } | |
1145 else | |
1146 error("%s has no effect in expression (%s)", | |
1147 Token::toChars(op), toChars()); | |
1148 } | |
1149 return 0; | |
1150 } | |
1151 | |
1152 /***************************** | |
1153 * Check that expression can be tested for true or false. | |
1154 */ | |
1155 | |
1156 Expression *Expression::checkToBoolean() | |
1157 { | |
1158 // Default is 'yes' - do nothing | |
1159 | |
1160 #ifdef DEBUG | |
1161 if (!type) | |
1162 dump(0); | |
1163 #endif | |
1164 | |
1165 if (!type->checkBoolean()) | |
1166 { | |
1167 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
1168 } | |
1169 return this; | |
1170 } | |
1171 | |
1172 /**************************** | |
1173 */ | |
1174 | |
1175 Expression *Expression::checkToPointer() | |
1176 { | |
1177 Expression *e; | |
1178 Type *tb; | |
1179 | |
1180 //printf("Expression::checkToPointer()\n"); | |
1181 e = this; | |
1182 | |
1183 // If C static array, convert to pointer | |
1184 tb = type->toBasetype(); | |
1185 if (tb->ty == Tsarray) | |
1186 { TypeSArray *ts = (TypeSArray *)tb; | |
1187 if (ts->size(loc) == 0) | |
1188 e = new NullExp(loc); | |
1189 else | |
1190 e = new AddrExp(loc, this); | |
336 | 1191 e->type = ts->next->pointerTo(); |
159 | 1192 } |
1193 return e; | |
1194 } | |
1195 | |
1196 /****************************** | |
1197 * Take address of expression. | |
1198 */ | |
1199 | |
1200 Expression *Expression::addressOf(Scope *sc) | |
1201 { | |
1202 Expression *e; | |
1203 | |
1204 //printf("Expression::addressOf()\n"); | |
1205 e = toLvalue(sc, NULL); | |
1206 e = new AddrExp(loc, e); | |
1207 e->type = type->pointerTo(); | |
1208 return e; | |
1209 } | |
1210 | |
1211 /****************************** | |
1212 * If this is a reference, dereference it. | |
1213 */ | |
1214 | |
1215 Expression *Expression::deref() | |
1216 { | |
1217 //printf("Expression::deref()\n"); | |
1218 if (type->ty == Treference) | |
1219 { Expression *e; | |
1220 | |
1221 e = new PtrExp(loc, this); | |
336 | 1222 e->type = ((TypeReference *)type)->next; |
159 | 1223 return e; |
1224 } | |
1225 return this; | |
1226 } | |
1227 | |
1228 /******************************** | |
1229 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
1230 */ | |
1231 | |
1232 int Expression::isBool(int result) | |
1233 { | |
1234 return FALSE; | |
1235 } | |
1236 | |
1237 /******************************** | |
1238 * Does this expression result in either a 1 or a 0? | |
1239 */ | |
1240 | |
1241 int Expression::isBit() | |
1242 { | |
1243 return FALSE; | |
1244 } | |
1245 | |
336 | 1246 /******************************** |
1247 * Can this expression throw an exception? | |
1248 * Valid only after semantic() pass. | |
1249 */ | |
1250 | |
1251 int Expression::canThrow() | |
1252 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1253 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1254 return FALSE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1255 #else |
336 | 1256 return TRUE; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1257 #endif |
336 | 1258 } |
1259 | |
1260 | |
1261 | |
159 | 1262 Expressions *Expression::arraySyntaxCopy(Expressions *exps) |
1263 { Expressions *a = NULL; | |
1264 | |
1265 if (exps) | |
1266 { | |
1267 a = new Expressions(); | |
1268 a->setDim(exps->dim); | |
1269 for (int i = 0; i < a->dim; i++) | |
1270 { Expression *e = (Expression *)exps->data[i]; | |
1271 | |
1272 e = e->syntaxCopy(); | |
1273 a->data[i] = e; | |
1274 } | |
1275 } | |
1276 return a; | |
1277 } | |
1278 | |
1279 /******************************** IntegerExp **************************/ | |
1280 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1281 IntegerExp::IntegerExp(Loc loc, dinteger_t value, Type *type) |
159 | 1282 : Expression(loc, TOKint64, sizeof(IntegerExp)) |
1283 { | |
1284 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1285 if (type && !type->isscalar()) | |
1286 { | |
336 | 1287 //printf("%s, loc = %d\n", toChars(), loc.linnum); |
159 | 1288 error("integral constant must be scalar type, not %s", type->toChars()); |
1289 type = Type::terror; | |
1290 } | |
1291 this->type = type; | |
1292 this->value = value; | |
1293 } | |
1294 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1295 IntegerExp::IntegerExp(dinteger_t value) |
159 | 1296 : Expression(0, TOKint64, sizeof(IntegerExp)) |
1297 { | |
1298 this->type = Type::tint32; | |
1299 this->value = value; | |
1300 } | |
1301 | |
1302 int IntegerExp::equals(Object *o) | |
1303 { IntegerExp *ne; | |
1304 | |
1305 if (this == o || | |
1306 (((Expression *)o)->op == TOKint64 && | |
1307 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1308 value == ne->value)) | |
1309 return 1; | |
1310 return 0; | |
1311 } | |
1312 | |
1313 char *IntegerExp::toChars() | |
1314 { | |
1315 #if 1 | |
1316 return Expression::toChars(); | |
1317 #else | |
1318 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
|
1319 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1320 sprintf(buffer, "%jd", value); |
159 | 1321 return buffer; |
1322 #endif | |
1323 } | |
1324 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1325 dinteger_t IntegerExp::toInteger() |
159 | 1326 { Type *t; |
1327 | |
1328 t = type; | |
1329 while (t) | |
1330 { | |
1331 switch (t->ty) | |
1332 { | |
1333 case Tbit: | |
1334 case Tbool: value = (value != 0); break; | |
1335 case Tint8: value = (d_int8) value; break; | |
1336 case Tchar: | |
1337 case Tuns8: value = (d_uns8) value; break; | |
1338 case Tint16: value = (d_int16) value; break; | |
1339 case Twchar: | |
1340 case Tuns16: value = (d_uns16) value; break; | |
1341 case Tint32: value = (d_int32) value; break; | |
1342 case Tdchar: | |
1343 case Tuns32: value = (d_uns32) value; break; | |
1344 case Tint64: value = (d_int64) value; break; | |
1345 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
|
1346 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
|
1347 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
|
1348 value = (d_uns32) value; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1349 else if (PTRSIZE == 8) |
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
|
1350 value = (d_uns64) value; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1351 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1352 assert(0); |
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
|
1353 break; |
159 | 1354 |
1355 case Tenum: | |
1356 { | |
1357 TypeEnum *te = (TypeEnum *)t; | |
1358 t = te->sym->memtype; | |
1359 continue; | |
1360 } | |
1361 | |
1362 case Ttypedef: | |
1363 { | |
1364 TypeTypedef *tt = (TypeTypedef *)t; | |
1365 t = tt->sym->basetype; | |
1366 continue; | |
1367 } | |
1368 | |
1369 default: | |
336 | 1370 /* This can happen if errors, such as |
1371 * the type is painted on like in fromConstInitializer(). | |
1372 */ | |
1373 if (!global.errors) | |
1374 { type->print(); | |
1375 assert(0); | |
1376 } | |
159 | 1377 break; |
1378 } | |
1379 break; | |
1380 } | |
1381 return value; | |
1382 } | |
1383 | |
1384 real_t IntegerExp::toReal() | |
1385 { | |
1386 Type *t; | |
1387 | |
1388 toInteger(); | |
1389 t = type->toBasetype(); | |
1390 if (t->ty == Tuns64) | |
1391 return (real_t)(d_uns64)value; | |
1392 else | |
1393 return (real_t)(d_int64)value; | |
1394 } | |
1395 | |
1396 real_t IntegerExp::toImaginary() | |
1397 { | |
1398 return (real_t) 0; | |
1399 } | |
1400 | |
1401 complex_t IntegerExp::toComplex() | |
1402 { | |
1403 return toReal(); | |
1404 } | |
1405 | |
1406 int IntegerExp::isBool(int result) | |
1407 { | |
1408 return result ? value != 0 : value == 0; | |
1409 } | |
1410 | |
1411 Expression *IntegerExp::semantic(Scope *sc) | |
1412 { | |
1413 if (!type) | |
1414 { | |
1415 // Determine what the type of this number is | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1416 dinteger_t number = value; |
159 | 1417 |
1418 if (number & 0x8000000000000000LL) | |
1419 type = Type::tuns64; | |
1420 else if (number & 0xFFFFFFFF80000000LL) | |
1421 type = Type::tint64; | |
1422 else | |
1423 type = Type::tint32; | |
1424 } | |
1425 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
|
1426 { 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
|
1427 type = type->semantic(loc, sc); |
159 | 1428 } |
1429 return this; | |
1430 } | |
1431 | |
1432 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1433 { | |
1434 if (!e) | |
1435 e = this; | |
1436 else if (!loc.filename) | |
1437 loc = e->loc; | |
1438 e->error("constant %s is not an lvalue", e->toChars()); | |
1439 return this; | |
1440 } | |
1441 | |
1442 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1443 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1444 dinteger_t v = toInteger(); |
159 | 1445 |
1446 if (type) | |
1447 { Type *t = type; | |
1448 | |
1449 L1: | |
1450 switch (t->ty) | |
1451 { | |
1452 case Tenum: | |
1453 { TypeEnum *te = (TypeEnum *)t; | |
1454 buf->printf("cast(%s)", te->sym->toChars()); | |
1455 t = te->sym->memtype; | |
1456 goto L1; | |
1457 } | |
1458 | |
1459 case Ttypedef: | |
1460 { TypeTypedef *tt = (TypeTypedef *)t; | |
1461 buf->printf("cast(%s)", tt->sym->toChars()); | |
1462 t = tt->sym->basetype; | |
1463 goto L1; | |
1464 } | |
1465 | |
1466 case Twchar: // BUG: need to cast(wchar) | |
1467 case Tdchar: // BUG: need to cast(dchar) | |
1468 if ((uinteger_t)v > 0xFF) | |
1469 { | |
1470 buf->printf("'\\U%08x'", v); | |
1471 break; | |
1472 } | |
1473 case Tchar: | |
1474 if (v == '\'') | |
1475 buf->writestring("'\\''"); | |
1476 else if (isprint(v) && v != '\\') | |
1477 buf->printf("'%c'", (int)v); | |
1478 else | |
1479 buf->printf("'\\x%02x'", (int)v); | |
1480 break; | |
1481 | |
1482 case Tint8: | |
1483 buf->writestring("cast(byte)"); | |
1484 goto L2; | |
1485 | |
1486 case Tint16: | |
1487 buf->writestring("cast(short)"); | |
1488 goto L2; | |
1489 | |
1490 case Tint32: | |
1491 L2: | |
1492 buf->printf("%d", (int)v); | |
1493 break; | |
1494 | |
1495 case Tuns8: | |
1496 buf->writestring("cast(ubyte)"); | |
1497 goto L3; | |
1498 | |
1499 case Tuns16: | |
1500 buf->writestring("cast(ushort)"); | |
1501 goto L3; | |
1502 | |
1503 case Tuns32: | |
1504 L3: | |
1505 buf->printf("%du", (unsigned)v); | |
1506 break; | |
1507 | |
1508 case Tint64: | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1509 buf->printf("%jdL", v); |
159 | 1510 break; |
1511 | |
1512 case Tuns64: | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1513 L4: |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1514 buf->printf("%juLU", v); |
159 | 1515 break; |
1516 | |
1517 case Tbit: | |
1518 case Tbool: | |
1519 buf->writestring((char *)(v ? "true" : "false")); | |
1520 break; | |
1521 | |
1522 case Tpointer: | |
1523 buf->writestring("cast("); | |
1524 buf->writestring(t->toChars()); | |
1525 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
|
1526 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
|
1527 goto L3; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1528 else if (PTRSIZE == 8) |
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
|
1529 goto L4; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1530 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1531 assert(0); |
159 | 1532 |
1533 default: | |
336 | 1534 /* This can happen if errors, such as |
1535 * the type is painted on like in fromConstInitializer(). | |
1536 */ | |
1537 if (!global.errors) | |
1538 { | |
159 | 1539 #ifdef DEBUG |
336 | 1540 t->print(); |
159 | 1541 #endif |
336 | 1542 assert(0); |
1543 } | |
1544 break; | |
159 | 1545 } |
1546 } | |
1547 else if (v & 0x8000000000000000LL) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1548 buf->printf("0x%jx", v); |
159 | 1549 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1550 buf->printf("%jd", v); |
159 | 1551 } |
1552 | |
1553 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1554 { | |
1555 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
|
1556 buf->printf("N%jd", -value); |
159 | 1557 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1558 buf->printf("%jd", value); |
159 | 1559 } |
1560 | |
1561 /******************************** RealExp **************************/ | |
1562 | |
1563 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1564 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1565 { | |
1566 //printf("RealExp::RealExp(%Lg)\n", value); | |
1567 this->value = value; | |
1568 this->type = type; | |
1569 } | |
1570 | |
1571 char *RealExp::toChars() | |
1572 { | |
1573 char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1574 | |
1575 #ifdef IN_GCC | |
1576 value.format(buffer, sizeof(buffer)); | |
1577 if (type->isimaginary()) | |
1578 strcat(buffer, "i"); | |
1579 #else | |
1580 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1581 #endif | |
1582 assert(strlen(buffer) < sizeof(buffer)); | |
1583 return mem.strdup(buffer); | |
1584 } | |
1585 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1586 dinteger_t RealExp::toInteger() |
159 | 1587 { |
1588 #ifdef IN_GCC | |
1589 return toReal().toInt(); | |
1590 #else | |
1591 return (sinteger_t) toReal(); | |
1592 #endif | |
1593 } | |
1594 | |
1595 uinteger_t RealExp::toUInteger() | |
1596 { | |
1597 #ifdef IN_GCC | |
1598 return (uinteger_t) toReal().toInt(); | |
1599 #else | |
1600 return (uinteger_t) toReal(); | |
1601 #endif | |
1602 } | |
1603 | |
1604 real_t RealExp::toReal() | |
1605 { | |
1606 return type->isreal() ? value : 0; | |
1607 } | |
1608 | |
1609 real_t RealExp::toImaginary() | |
1610 { | |
1611 return type->isreal() ? 0 : value; | |
1612 } | |
1613 | |
1614 complex_t RealExp::toComplex() | |
1615 { | |
1616 #ifdef __DMC__ | |
1617 return toReal() + toImaginary() * I; | |
1618 #else | |
1619 return complex_t(toReal(), toImaginary()); | |
1620 #endif | |
1621 } | |
1622 | |
1623 /******************************** | |
1624 * Test to see if two reals are the same. | |
1625 * Regard NaN's as equivalent. | |
1626 * Regard +0 and -0 as different. | |
1627 */ | |
1628 | |
1629 int RealEquals(real_t x1, real_t x2) | |
1630 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1631 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1632 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
|
1633 #else |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1634 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
|
1635 (isnan(x1) && isnan(x2)) || |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1636 #endif |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1637 // 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
|
1638 (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
|
1639 // 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
|
1640 (x1 != 0. && x1 == x2); |
159 | 1641 } |
1642 | |
1643 int RealExp::equals(Object *o) | |
1644 { RealExp *ne; | |
1645 | |
1646 if (this == o || | |
1647 (((Expression *)o)->op == TOKfloat64 && | |
1648 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1649 RealEquals(value, ne->value) | |
1650 ) | |
1651 ) | |
1652 return 1; | |
1653 return 0; | |
1654 } | |
1655 | |
1656 Expression *RealExp::semantic(Scope *sc) | |
1657 { | |
1658 if (!type) | |
1659 type = Type::tfloat64; | |
1660 else | |
1661 type = type->semantic(loc, sc); | |
1662 return this; | |
1663 } | |
1664 | |
1665 int RealExp::isBool(int result) | |
1666 { | |
1667 #ifdef IN_GCC | |
1668 return result ? (! value.isZero()) : (value.isZero()); | |
1669 #else | |
1670 return result ? (value != 0) | |
1671 : (value == 0); | |
1672 #endif | |
1673 } | |
1674 | |
1675 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1676 { | |
1677 /* In order to get an exact representation, try converting it | |
1678 * to decimal then back again. If it matches, use it. | |
1679 * If it doesn't, fall back to hex, which is | |
1680 * always exact. | |
1681 */ | |
1682 char buffer[25]; | |
1683 sprintf(buffer, "%Lg", value); | |
1684 assert(strlen(buffer) < sizeof(buffer)); | |
1685 #if _WIN32 && __DMC__ | |
1686 char *save = __locale_decpoint; | |
1687 __locale_decpoint = "."; | |
1688 real_t r = strtold(buffer, NULL); | |
1689 __locale_decpoint = save; | |
1690 #else | |
1691 real_t r = strtold(buffer, NULL); | |
1692 #endif | |
1693 if (r == value) // if exact duplication | |
1694 buf->writestring(buffer); | |
1695 else | |
1696 buf->printf("%La", value); // ensure exact duplication | |
1697 | |
1698 if (type) | |
1699 { | |
1700 Type *t = type->toBasetype(); | |
1701 switch (t->ty) | |
1702 { | |
1703 case Tfloat32: | |
1704 case Timaginary32: | |
1705 case Tcomplex32: | |
1706 buf->writeByte('F'); | |
1707 break; | |
1708 | |
1709 case Tfloat80: | |
1710 case Timaginary80: | |
1711 case Tcomplex80: | |
1712 buf->writeByte('L'); | |
1713 break; | |
1714 | |
1715 default: | |
1716 break; | |
1717 } | |
1718 if (t->isimaginary()) | |
1719 buf->writeByte('i'); | |
1720 } | |
1721 } | |
1722 | |
1723 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1724 { | |
1725 floatToBuffer(buf, type, value); | |
1726 } | |
1727 | |
1728 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1729 { | |
1730 /* Rely on %A to get portable mangling. | |
1731 * Must munge result to get only identifier characters. | |
1732 * | |
1733 * Possible values from %A => mangled result | |
1734 * NAN => NAN | |
1735 * -INF => NINF | |
1736 * INF => INF | |
1737 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1738 * 0X1.9P+2 => 19P2 | |
1739 */ | |
1740 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1741 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1742 if (__inline_isnan(value)) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1743 #else |
159 | 1744 if (isnan(value)) |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1745 #endif |
159 | 1746 buf->writestring("NAN"); // no -NAN bugs |
1747 else | |
1748 { | |
1749 char buffer[32]; | |
1750 int n = sprintf(buffer, "%LA", value); | |
1751 assert(n > 0 && n < sizeof(buffer)); | |
1752 for (int i = 0; i < n; i++) | |
1753 { char c = buffer[i]; | |
1754 | |
1755 switch (c) | |
1756 { | |
1757 case '-': | |
1758 buf->writeByte('N'); | |
1759 break; | |
1760 | |
1761 case '+': | |
1762 case 'X': | |
1763 case '.': | |
1764 break; | |
1765 | |
1766 case '0': | |
1767 if (i < 2) | |
1768 break; // skip leading 0X | |
1769 default: | |
1770 buf->writeByte(c); | |
1771 break; | |
1772 } | |
1773 } | |
1774 } | |
1775 } | |
1776 | |
1777 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1778 { | |
1779 buf->writeByte('e'); | |
1780 realToMangleBuffer(buf, value); | |
1781 } | |
1782 | |
1783 | |
1784 /******************************** ComplexExp **************************/ | |
1785 | |
1786 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1787 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1788 { | |
1789 this->value = value; | |
1790 this->type = type; | |
1791 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1792 } | |
1793 | |
1794 char *ComplexExp::toChars() | |
1795 { | |
1796 char buffer[sizeof(value) * 3 + 8 + 1]; | |
1797 | |
1798 #ifdef IN_GCC | |
1799 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1800 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1801 creall(value).format(buf1, sizeof(buf1)); | |
1802 cimagl(value).format(buf2, sizeof(buf2)); | |
1803 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1804 #else | |
1805 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1806 assert(strlen(buffer) < sizeof(buffer)); | |
1807 #endif | |
1808 return mem.strdup(buffer); | |
1809 } | |
1810 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1811 dinteger_t ComplexExp::toInteger() |
159 | 1812 { |
1813 #ifdef IN_GCC | |
1814 return (sinteger_t) toReal().toInt(); | |
1815 #else | |
1816 return (sinteger_t) toReal(); | |
1817 #endif | |
1818 } | |
1819 | |
1820 uinteger_t ComplexExp::toUInteger() | |
1821 { | |
1822 #ifdef IN_GCC | |
1823 return (uinteger_t) toReal().toInt(); | |
1824 #else | |
1825 return (uinteger_t) toReal(); | |
1826 #endif | |
1827 } | |
1828 | |
1829 real_t ComplexExp::toReal() | |
1830 { | |
1831 return creall(value); | |
1832 } | |
1833 | |
1834 real_t ComplexExp::toImaginary() | |
1835 { | |
1836 return cimagl(value); | |
1837 } | |
1838 | |
1839 complex_t ComplexExp::toComplex() | |
1840 { | |
1841 return value; | |
1842 } | |
1843 | |
1844 int ComplexExp::equals(Object *o) | |
1845 { ComplexExp *ne; | |
1846 | |
1847 if (this == o || | |
1848 (((Expression *)o)->op == TOKcomplex80 && | |
1849 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1850 RealEquals(creall(value), creall(ne->value)) && | |
1851 RealEquals(cimagl(value), cimagl(ne->value)) | |
1852 ) | |
1853 ) | |
1854 return 1; | |
1855 return 0; | |
1856 } | |
1857 | |
1858 Expression *ComplexExp::semantic(Scope *sc) | |
1859 { | |
1860 if (!type) | |
1861 type = Type::tcomplex80; | |
1862 else | |
1863 type = type->semantic(loc, sc); | |
1864 return this; | |
1865 } | |
1866 | |
1867 int ComplexExp::isBool(int result) | |
1868 { | |
1869 if (result) | |
1870 return (bool)(value); | |
1871 else | |
1872 return !value; | |
1873 } | |
1874 | |
1875 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1876 { | |
1877 /* Print as: | |
1878 * (re+imi) | |
1879 */ | |
1880 #ifdef IN_GCC | |
1881 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1882 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1883 creall(value).format(buf1, sizeof(buf1)); | |
1884 cimagl(value).format(buf2, sizeof(buf2)); | |
1885 buf->printf("(%s+%si)", buf1, buf2); | |
1886 #else | |
1887 buf->writeByte('('); | |
1888 floatToBuffer(buf, type, creall(value)); | |
1889 buf->writeByte('+'); | |
1890 floatToBuffer(buf, type, cimagl(value)); | |
1891 buf->writestring("i)"); | |
1892 #endif | |
1893 } | |
1894 | |
1895 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
1896 { | |
1897 buf->writeByte('c'); | |
1898 real_t r = toReal(); | |
1899 realToMangleBuffer(buf, r); | |
1900 buf->writeByte('c'); // separate the two | |
1901 r = toImaginary(); | |
1902 realToMangleBuffer(buf, r); | |
1903 } | |
1904 | |
1905 /******************************** IdentifierExp **************************/ | |
1906 | |
1907 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
1908 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
1909 { | |
1910 this->ident = ident; | |
1911 } | |
1912 | |
1913 Expression *IdentifierExp::semantic(Scope *sc) | |
1914 { | |
1915 Dsymbol *s; | |
1916 Dsymbol *scopesym; | |
1917 | |
1918 #if LOGSEMANTIC | |
1919 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
1920 #endif | |
1921 s = sc->search(loc, ident, &scopesym); | |
1922 if (s) | |
1923 { Expression *e; | |
1924 WithScopeSymbol *withsym; | |
1925 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1926 /* 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
|
1927 */ |
159 | 1928 withsym = scopesym->isWithScopeSymbol(); |
1929 if (withsym) | |
1930 { | |
1931 s = s->toAlias(); | |
1932 | |
1933 // Same as wthis.ident | |
1934 if (s->needThis() || s->isTemplateDeclaration()) | |
1935 { | |
1936 e = new VarExp(loc, withsym->withstate->wthis); | |
1937 e = new DotIdExp(loc, e, ident); | |
1938 } | |
1939 else | |
1940 { Type *t = withsym->withstate->wthis->type; | |
1941 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
|
1942 t = ((TypePointer *)t)->next; |
159 | 1943 e = new TypeDotIdExp(loc, t, ident); |
1944 } | |
1945 } | |
1946 else | |
1947 { | |
1948 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
1949 { // Kludge to run semantic() here because | |
1950 // ArrayScopeSymbol::search() doesn't have access to sc. | |
1951 s->semantic(sc); | |
1952 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1953 /* If f is really a function template, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1954 * 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
|
1955 */ |
159 | 1956 FuncDeclaration *f = s->isFuncDeclaration(); |
1957 if (f && f->parent) | |
1958 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
1959 | |
1960 if (ti && | |
1961 !ti->isTemplateMixin() && | |
1962 (ti->name == f->ident || | |
1963 ti->toAlias()->ident == f->ident) | |
1964 && | |
1965 ti->tempdecl && ti->tempdecl->onemember) | |
1966 { | |
1967 TemplateDeclaration *tempdecl = ti->tempdecl; | |
1968 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
1969 tempdecl = tempdecl->overroot; // then get the start | |
1970 e = new TemplateExp(loc, tempdecl); | |
1971 e = e->semantic(sc); | |
1972 return e; | |
1973 } | |
1974 } | |
1975 e = new DsymbolExp(loc, s); | |
1976 } | |
1977 return e->semantic(sc); | |
1978 } | |
1979 error("undefined identifier %s", ident->toChars()); | |
1980 type = Type::terror; | |
1981 return this; | |
1982 } | |
1983 | |
1984 char *IdentifierExp::toChars() | |
1985 { | |
1986 return ident->toChars(); | |
1987 } | |
1988 | |
1989 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1990 { | |
1991 if (hgs->hdrgen) | |
1992 buf->writestring(ident->toHChars2()); | |
1993 else | |
1994 buf->writestring(ident->toChars()); | |
1995 } | |
1996 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1997 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1998 int IdentifierExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1999 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2000 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2001 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2002 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2003 |
159 | 2004 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) |
2005 { | |
2006 #if 0 | |
2007 tym = tybasic(e1->ET->Tty); | |
2008 if (!(tyscalar(tym) || | |
2009 tym == TYstruct || | |
2010 tym == TYarray && e->Eoper == TOKaddr)) | |
2011 synerr(EM_lvalue); // lvalue expected | |
2012 #endif | |
2013 return this; | |
2014 } | |
2015 | |
2016 /******************************** DollarExp **************************/ | |
2017 | |
2018 DollarExp::DollarExp(Loc loc) | |
2019 : IdentifierExp(loc, Id::dollar) | |
2020 { | |
2021 } | |
2022 | |
2023 /******************************** DsymbolExp **************************/ | |
2024 | |
2025 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
2026 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
2027 { | |
2028 this->s = s; | |
2029 } | |
2030 | |
2031 Expression *DsymbolExp::semantic(Scope *sc) | |
2032 { | |
2033 #if LOGSEMANTIC | |
2034 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
2035 #endif | |
2036 | |
2037 Lagain: | |
2038 EnumMember *em; | |
2039 Expression *e; | |
2040 VarDeclaration *v; | |
2041 FuncDeclaration *f; | |
2042 FuncLiteralDeclaration *fld; | |
2043 Declaration *d; | |
2044 ClassDeclaration *cd; | |
2045 ClassDeclaration *thiscd = NULL; | |
2046 Import *imp; | |
2047 Package *pkg; | |
2048 Type *t; | |
2049 | |
2050 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
2051 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
2052 if (type) | |
2053 return this; | |
2054 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
2055 checkDeprecated(sc, s); | |
2056 s = s->toAlias(); | |
2057 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
2058 if (!s->isFuncDeclaration()) | |
2059 checkDeprecated(sc, s); | |
2060 | |
2061 if (sc->func) | |
2062 thiscd = sc->func->parent->isClassDeclaration(); | |
2063 | |
2064 // BUG: This should happen after overload resolution for functions, not before | |
2065 if (s->needThis()) | |
2066 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2067 if (hasThis(sc) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2068 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2069 && !s->isFuncDeclaration() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2070 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2071 ) |
159 | 2072 { |
2073 // Supply an implicit 'this', as in | |
2074 // this.ident | |
2075 | |
2076 DotVarExp *de; | |
2077 | |
2078 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
2079 return de->semantic(sc); | |
2080 } | |
2081 } | |
2082 | |
2083 em = s->isEnumMember(); | |
2084 if (em) | |
2085 { | |
2086 e = em->value->copy(); | |
2087 e = e->semantic(sc); | |
2088 return e; | |
2089 } | |
2090 v = s->isVarDeclaration(); | |
2091 if (v) | |
2092 { | |
2093 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
2094 if (!type) | |
2095 { type = v->type; | |
2096 if (!v->type) | |
2097 { error("forward reference of %s", v->toChars()); | |
2098 type = Type::terror; | |
2099 } | |
2100 } | |
2101 if (v->isConst() && type->toBasetype()->ty != Tsarray) | |
2102 { | |
2103 if (v->init) | |
2104 { | |
2105 if (v->inuse) | |
2106 { | |
2107 error("circular reference to '%s'", v->toChars()); | |
2108 type = Type::tint32; | |
2109 return this; | |
2110 } | |
2111 ExpInitializer *ei = v->init->isExpInitializer(); | |
2112 if (ei) | |
2113 { | |
2114 e = ei->exp->copy(); // make copy so we can change loc | |
2115 if (e->op == TOKstring || !e->type) | |
2116 e = e->semantic(sc); | |
2117 e = e->implicitCastTo(sc, type); | |
2118 e->loc = loc; | |
2119 return e; | |
2120 } | |
2121 } | |
2122 else | |
2123 { | |
2124 e = type->defaultInit(); | |
2125 e->loc = loc; | |
2126 return e; | |
2127 } | |
2128 } | |
2129 e = new VarExp(loc, v); | |
2130 e->type = type; | |
2131 e = e->semantic(sc); | |
2132 return e->deref(); | |
2133 } | |
2134 fld = s->isFuncLiteralDeclaration(); | |
2135 if (fld) | |
2136 { //printf("'%s' is a function literal\n", fld->toChars()); | |
2137 e = new FuncExp(loc, fld); | |
2138 return e->semantic(sc); | |
2139 } | |
2140 f = s->isFuncDeclaration(); | |
2141 if (f) | |
2142 { //printf("'%s' is a function\n", f->toChars()); | |
2143 return new VarExp(loc, f); | |
2144 } | |
2145 cd = s->isClassDeclaration(); | |
2146 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
2147 { | |
2148 // We need to add an implicit 'this' if cd is this class or a base class. | |
2149 DotTypeExp *dte; | |
2150 | |
2151 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
2152 return dte->semantic(sc); | |
2153 } | |
2154 imp = s->isImport(); | |
2155 if (imp) | |
2156 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2157 if (!imp->pkg) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2158 { error("forward reference of import %s", imp->toChars()); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2159 return this; |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2160 } |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2161 ScopeExp *ie = new ScopeExp(loc, imp->pkg); |
159 | 2162 return ie->semantic(sc); |
2163 } | |
2164 pkg = s->isPackage(); | |
2165 if (pkg) | |
2166 { | |
2167 ScopeExp *ie; | |
2168 | |
2169 ie = new ScopeExp(loc, pkg); | |
2170 return ie->semantic(sc); | |
2171 } | |
2172 Module *mod = s->isModule(); | |
2173 if (mod) | |
2174 { | |
2175 ScopeExp *ie; | |
2176 | |
2177 ie = new ScopeExp(loc, mod); | |
2178 return ie->semantic(sc); | |
2179 } | |
2180 | |
2181 t = s->getType(); | |
2182 if (t) | |
2183 { | |
2184 return new TypeExp(loc, t); | |
2185 } | |
2186 | |
2187 TupleDeclaration *tup = s->isTupleDeclaration(); | |
2188 if (tup) | |
2189 { | |
2190 e = new TupleExp(loc, tup); | |
2191 e = e->semantic(sc); | |
2192 return e; | |
2193 } | |
2194 | |
2195 TemplateInstance *ti = s->isTemplateInstance(); | |
2196 if (ti && !global.errors) | |
2197 { if (!ti->semanticdone) | |
2198 ti->semantic(sc); | |
2199 s = ti->inst->toAlias(); | |
2200 if (!s->isTemplateInstance()) | |
2201 goto Lagain; | |
2202 e = new ScopeExp(loc, ti); | |
2203 e = e->semantic(sc); | |
2204 return e; | |
2205 } | |
2206 | |
2207 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
2208 if (td) | |
2209 { | |
2210 e = new TemplateExp(loc, td); | |
2211 e = e->semantic(sc); | |
2212 return e; | |
2213 } | |
2214 | |
2215 Lerr: | |
2216 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
2217 type = Type::terror; | |
2218 return this; | |
2219 } | |
2220 | |
2221 char *DsymbolExp::toChars() | |
2222 { | |
2223 return s->toChars(); | |
2224 } | |
2225 | |
2226 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2227 { | |
2228 buf->writestring(s->toChars()); | |
2229 } | |
2230 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2231 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2232 int DsymbolExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2233 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2234 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2235 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2236 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2237 |
159 | 2238 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) |
2239 { | |
2240 #if 0 | |
2241 tym = tybasic(e1->ET->Tty); | |
2242 if (!(tyscalar(tym) || | |
2243 tym == TYstruct || | |
2244 tym == TYarray && e->Eoper == TOKaddr)) | |
2245 synerr(EM_lvalue); // lvalue expected | |
2246 #endif | |
2247 return this; | |
2248 } | |
2249 | |
2250 /******************************** ThisExp **************************/ | |
2251 | |
2252 ThisExp::ThisExp(Loc loc) | |
2253 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
2254 { | |
2255 var = NULL; | |
2256 } | |
2257 | |
2258 Expression *ThisExp::semantic(Scope *sc) | |
2259 { FuncDeclaration *fd; | |
2260 FuncDeclaration *fdthis; | |
2261 int nested = 0; | |
2262 | |
2263 #if LOGSEMANTIC | |
2264 printf("ThisExp::semantic()\n"); | |
2265 #endif | |
2266 if (type) | |
2267 { //assert(global.errors || var); | |
2268 return this; | |
2269 } | |
2270 | |
2271 /* Special case for typeof(this) and typeof(super) since both | |
2272 * should work even if they are not inside a non-static member function | |
2273 */ | |
2274 if (sc->intypeof) | |
2275 { | |
2276 // Find enclosing struct or class | |
2277 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2278 { | |
2279 ClassDeclaration *cd; | |
2280 StructDeclaration *sd; | |
2281 | |
2282 if (!s) | |
2283 { | |
2284 error("%s is not in a struct or class scope", toChars()); | |
2285 goto Lerr; | |
2286 } | |
2287 cd = s->isClassDeclaration(); | |
2288 if (cd) | |
2289 { | |
2290 type = cd->type; | |
2291 return this; | |
2292 } | |
2293 sd = s->isStructDeclaration(); | |
2294 if (sd) | |
2295 { | |
2296 type = sd->type->pointerTo(); | |
2297 return this; | |
2298 } | |
2299 } | |
2300 } | |
2301 | |
2302 fdthis = sc->parent->isFuncDeclaration(); | |
2303 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
2304 if (!fd) | |
2305 goto Lerr; | |
2306 | |
2307 assert(fd->vthis); | |
2308 var = fd->vthis; | |
2309 assert(var->parent); | |
2310 type = var->type; | |
2311 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2312 #if 0 | |
2313 if (fd != fdthis) // if nested | |
2314 { | |
2315 fdthis->getLevel(loc, fd); | |
2316 fd->vthis->nestedref = 1; | |
2317 fd->nestedFrameRef = 1; | |
2318 } | |
2319 #endif | |
336 | 2320 if (!sc->intypeof) |
2321 sc->callSuper |= CSXthis; | |
159 | 2322 return this; |
2323 | |
2324 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
|
2325 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
|
2326 type = Type::terror; |
159 | 2327 return this; |
2328 } | |
2329 | |
2330 int ThisExp::isBool(int result) | |
2331 { | |
2332 return result ? TRUE : FALSE; | |
2333 } | |
2334 | |
2335 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2336 { | |
2337 buf->writestring("this"); | |
2338 } | |
2339 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2340 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2341 int ThisExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2342 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2343 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2344 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2345 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2346 |
159 | 2347 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) |
2348 { | |
2349 return this; | |
2350 } | |
2351 | |
2352 /******************************** SuperExp **************************/ | |
2353 | |
2354 SuperExp::SuperExp(Loc loc) | |
2355 : ThisExp(loc) | |
2356 { | |
2357 op = TOKsuper; | |
2358 } | |
2359 | |
2360 Expression *SuperExp::semantic(Scope *sc) | |
2361 { FuncDeclaration *fd; | |
2362 FuncDeclaration *fdthis; | |
2363 ClassDeclaration *cd; | |
2364 Dsymbol *s; | |
2365 | |
2366 #if LOGSEMANTIC | |
2367 printf("SuperExp::semantic('%s')\n", toChars()); | |
2368 #endif | |
2369 if (type) | |
2370 return this; | |
2371 | |
2372 /* Special case for typeof(this) and typeof(super) since both | |
2373 * should work even if they are not inside a non-static member function | |
2374 */ | |
2375 if (sc->intypeof) | |
2376 { | |
2377 // Find enclosing class | |
2378 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2379 { | |
2380 ClassDeclaration *cd; | |
2381 | |
2382 if (!s) | |
2383 { | |
2384 error("%s is not in a class scope", toChars()); | |
2385 goto Lerr; | |
2386 } | |
2387 cd = s->isClassDeclaration(); | |
2388 if (cd) | |
2389 { | |
2390 cd = cd->baseClass; | |
2391 if (!cd) | |
2392 { error("class %s has no 'super'", s->toChars()); | |
2393 goto Lerr; | |
2394 } | |
2395 type = cd->type; | |
2396 return this; | |
2397 } | |
2398 } | |
2399 } | |
2400 | |
2401 fdthis = sc->parent->isFuncDeclaration(); | |
2402 fd = hasThis(sc); | |
2403 if (!fd) | |
2404 goto Lerr; | |
2405 assert(fd->vthis); | |
2406 var = fd->vthis; | |
2407 assert(var->parent); | |
2408 | |
2409 s = fd->toParent(); | |
2410 while (s && s->isTemplateInstance()) | |
2411 s = s->toParent(); | |
2412 assert(s); | |
2413 cd = s->isClassDeclaration(); | |
2414 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2415 if (!cd) | |
2416 goto Lerr; | |
2417 if (!cd->baseClass) | |
2418 { | |
2419 error("no base class for %s", cd->toChars()); | |
2420 type = fd->vthis->type; | |
2421 } | |
2422 else | |
2423 { | |
2424 type = cd->baseClass->type; | |
2425 } | |
2426 | |
2427 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2428 #if 0 | |
2429 if (fd != fdthis) | |
2430 { | |
2431 fdthis->getLevel(loc, fd); | |
2432 fd->vthis->nestedref = 1; | |
2433 fd->nestedFrameRef = 1; | |
2434 } | |
2435 #endif | |
2436 | |
336 | 2437 if (!sc->intypeof) |
2438 sc->callSuper |= CSXsuper; | |
159 | 2439 return this; |
2440 | |
2441 | |
2442 Lerr: | |
2443 error("'super' is only allowed in non-static class member functions"); | |
2444 type = Type::tint32; | |
2445 return this; | |
2446 } | |
2447 | |
2448 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2449 { | |
2450 buf->writestring("super"); | |
2451 } | |
2452 | |
2453 | |
2454 /******************************** NullExp **************************/ | |
2455 | |
2456 NullExp::NullExp(Loc loc) | |
2457 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2458 { | |
2459 committed = 0; | |
2460 } | |
2461 | |
2462 Expression *NullExp::semantic(Scope *sc) | |
2463 { | |
2464 #if LOGSEMANTIC | |
2465 printf("NullExp::semantic('%s')\n", toChars()); | |
2466 #endif | |
2467 // NULL is the same as (void *)0 | |
2468 if (!type) | |
2469 type = Type::tvoid->pointerTo(); | |
2470 return this; | |
2471 } | |
2472 | |
2473 int NullExp::isBool(int result) | |
2474 { | |
2475 return result ? FALSE : TRUE; | |
2476 } | |
2477 | |
2478 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2479 { | |
2480 buf->writestring("null"); | |
2481 } | |
2482 | |
2483 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2484 { | |
2485 buf->writeByte('n'); | |
2486 } | |
2487 | |
2488 /******************************** StringExp **************************/ | |
2489 | |
2490 StringExp::StringExp(Loc loc, char *string) | |
2491 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2492 { | |
2493 this->string = string; | |
2494 this->len = strlen(string); | |
2495 this->sz = 1; | |
2496 this->committed = 0; | |
2497 this->postfix = 0; | |
2498 } | |
2499 | |
2500 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2501 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2502 { | |
2503 this->string = string; | |
2504 this->len = len; | |
2505 this->sz = 1; | |
2506 this->committed = 0; | |
2507 this->postfix = 0; | |
2508 } | |
2509 | |
2510 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2511 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2512 { | |
2513 this->string = string; | |
2514 this->len = len; | |
2515 this->sz = 1; | |
2516 this->committed = 0; | |
2517 this->postfix = postfix; | |
2518 } | |
2519 | |
2520 #if 0 | |
2521 Expression *StringExp::syntaxCopy() | |
2522 { | |
2523 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2524 return copy(); | |
2525 } | |
2526 #endif | |
2527 | |
2528 int StringExp::equals(Object *o) | |
2529 { | |
2530 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2531 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2532 { Expression *e = (Expression *)o; | |
2533 | |
2534 if (e->op == TOKstring) | |
2535 { | |
2536 return compare(o) == 0; | |
2537 } | |
2538 } | |
2539 return FALSE; | |
2540 } | |
2541 | |
2542 char *StringExp::toChars() | |
2543 { | |
2544 OutBuffer buf; | |
2545 HdrGenState hgs; | |
2546 char *p; | |
2547 | |
2548 memset(&hgs, 0, sizeof(hgs)); | |
2549 toCBuffer(&buf, &hgs); | |
2550 buf.writeByte(0); | |
2551 p = (char *)buf.data; | |
2552 buf.data = NULL; | |
2553 return p; | |
2554 } | |
2555 | |
2556 Expression *StringExp::semantic(Scope *sc) | |
2557 { | |
2558 #if LOGSEMANTIC | |
2559 printf("StringExp::semantic() %s\n", toChars()); | |
2560 #endif | |
2561 if (!type) | |
2562 { OutBuffer buffer; | |
2563 size_t newlen = 0; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2564 const char *p; |
159 | 2565 size_t u; |
2566 unsigned c; | |
2567 | |
2568 switch (postfix) | |
2569 { | |
2570 case 'd': | |
2571 for (u = 0; u < len;) | |
2572 { | |
2573 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2574 if (p) | |
2575 { error("%s", p); | |
2576 break; | |
2577 } | |
2578 else | |
2579 { buffer.write4(c); | |
2580 newlen++; | |
2581 } | |
2582 } | |
2583 buffer.write4(0); | |
2584 string = buffer.extractData(); | |
2585 len = newlen; | |
2586 sz = 4; | |
2587 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2588 committed = 1; | |
2589 break; | |
2590 | |
2591 case 'w': | |
2592 for (u = 0; u < len;) | |
2593 { | |
2594 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2595 if (p) | |
2596 { error("%s", p); | |
2597 break; | |
2598 } | |
2599 else | |
2600 { buffer.writeUTF16(c); | |
2601 newlen++; | |
2602 if (c >= 0x10000) | |
2603 newlen++; | |
2604 } | |
2605 } | |
2606 buffer.writeUTF16(0); | |
2607 string = buffer.extractData(); | |
2608 len = newlen; | |
2609 sz = 2; | |
2610 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2611 committed = 1; | |
2612 break; | |
2613 | |
2614 case 'c': | |
2615 committed = 1; | |
2616 default: | |
2617 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2618 break; | |
2619 } | |
2620 type = type->semantic(loc, sc); | |
2621 } | |
2622 return this; | |
2623 } | |
2624 | |
2625 /**************************************** | |
2626 * Convert string to char[]. | |
2627 */ | |
2628 | |
2629 StringExp *StringExp::toUTF8(Scope *sc) | |
2630 { | |
2631 if (sz != 1) | |
2632 { // Convert to UTF-8 string | |
2633 committed = 0; | |
2634 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2635 e = e->optimize(WANTvalue); | |
2636 assert(e->op == TOKstring); | |
2637 StringExp *se = (StringExp *)e; | |
2638 assert(se->sz == 1); | |
2639 return se; | |
2640 } | |
2641 return this; | |
2642 } | |
2643 | |
2644 int StringExp::compare(Object *obj) | |
2645 { | |
2646 // Used to sort case statement expressions so we can do an efficient lookup | |
2647 StringExp *se2 = (StringExp *)(obj); | |
2648 | |
2649 // This is a kludge so isExpression() in template.c will return 5 | |
2650 // for StringExp's. | |
2651 if (!se2) | |
2652 return 5; | |
2653 | |
2654 assert(se2->op == TOKstring); | |
2655 | |
2656 int len1 = len; | |
2657 int len2 = se2->len; | |
2658 | |
2659 if (len1 == len2) | |
2660 { | |
2661 switch (sz) | |
2662 { | |
2663 case 1: | |
2664 return strcmp((char *)string, (char *)se2->string); | |
2665 | |
2666 case 2: | |
2667 { unsigned u; | |
2668 d_wchar *s1 = (d_wchar *)string; | |
2669 d_wchar *s2 = (d_wchar *)se2->string; | |
2670 | |
2671 for (u = 0; u < len; u++) | |
2672 { | |
2673 if (s1[u] != s2[u]) | |
2674 return s1[u] - s2[u]; | |
2675 } | |
2676 } | |
2677 | |
2678 case 4: | |
2679 { unsigned u; | |
2680 d_dchar *s1 = (d_dchar *)string; | |
2681 d_dchar *s2 = (d_dchar *)se2->string; | |
2682 | |
2683 for (u = 0; u < len; u++) | |
2684 { | |
2685 if (s1[u] != s2[u]) | |
2686 return s1[u] - s2[u]; | |
2687 } | |
2688 } | |
2689 break; | |
2690 | |
2691 default: | |
2692 assert(0); | |
2693 } | |
2694 } | |
2695 return len1 - len2; | |
2696 } | |
2697 | |
2698 int StringExp::isBool(int result) | |
2699 { | |
2700 return result ? TRUE : FALSE; | |
2701 } | |
2702 | |
2703 unsigned StringExp::charAt(size_t i) | |
2704 { unsigned value; | |
2705 | |
2706 switch (sz) | |
2707 { | |
2708 case 1: | |
2709 value = ((unsigned char *)string)[i]; | |
2710 break; | |
2711 | |
2712 case 2: | |
2713 value = ((unsigned short *)string)[i]; | |
2714 break; | |
2715 | |
2716 case 4: | |
2717 value = ((unsigned int *)string)[i]; | |
2718 break; | |
2719 | |
2720 default: | |
2721 assert(0); | |
2722 break; | |
2723 } | |
2724 return value; | |
2725 } | |
2726 | |
2727 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2728 { | |
2729 buf->writeByte('"'); | |
2730 for (size_t i = 0; i < len; i++) | |
2731 { unsigned c = charAt(i); | |
2732 | |
2733 switch (c) | |
2734 { | |
2735 case '"': | |
2736 case '\\': | |
2737 if (!hgs->console) | |
2738 buf->writeByte('\\'); | |
2739 default: | |
2740 if (c <= 0xFF) | |
2741 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2742 buf->writeByte(c); | |
2743 else | |
2744 buf->printf("\\x%02x", c); | |
2745 } | |
2746 else if (c <= 0xFFFF) | |
2747 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2748 else | |
2749 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2750 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2751 break; | |
2752 } | |
2753 } | |
2754 buf->writeByte('"'); | |
2755 if (postfix) | |
2756 buf->writeByte(postfix); | |
2757 } | |
2758 | |
2759 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2760 { char m; | |
2761 OutBuffer tmp; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2762 const char *p; |
159 | 2763 unsigned c; |
2764 size_t u; | |
2765 unsigned char *q; | |
2766 unsigned qlen; | |
2767 | |
2768 /* Write string in UTF-8 format | |
2769 */ | |
2770 switch (sz) | |
2771 { case 1: | |
2772 m = 'a'; | |
2773 q = (unsigned char *)string; | |
2774 qlen = len; | |
2775 break; | |
2776 case 2: | |
2777 m = 'w'; | |
2778 for (u = 0; u < len; ) | |
2779 { | |
2780 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2781 if (p) | |
2782 error("%s", p); | |
2783 else | |
2784 tmp.writeUTF8(c); | |
2785 } | |
2786 q = tmp.data; | |
2787 qlen = tmp.offset; | |
2788 break; | |
2789 case 4: | |
2790 m = 'd'; | |
2791 for (u = 0; u < len; u++) | |
2792 { | |
2793 c = ((unsigned *)string)[u]; | |
2794 if (!utf_isValidDchar(c)) | |
2795 error("invalid UCS-32 char \\U%08x", c); | |
2796 else | |
2797 tmp.writeUTF8(c); | |
2798 } | |
2799 q = tmp.data; | |
2800 qlen = tmp.offset; | |
2801 break; | |
2802 default: | |
2803 assert(0); | |
2804 } | |
2805 buf->writeByte(m); | |
2806 buf->printf("%d_", qlen); | |
2807 for (size_t i = 0; i < qlen; i++) | |
2808 buf->printf("%02x", q[i]); | |
2809 } | |
2810 | |
2811 /************************ ArrayLiteralExp ************************************/ | |
2812 | |
2813 // [ e1, e2, e3, ... ] | |
2814 | |
2815 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2816 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2817 { | |
2818 this->elements = elements; | |
2819 } | |
2820 | |
2821 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2822 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2823 { | |
2824 elements = new Expressions; | |
2825 elements->push(e); | |
2826 } | |
2827 | |
2828 Expression *ArrayLiteralExp::syntaxCopy() | |
2829 { | |
2830 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2831 } | |
2832 | |
2833 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2834 { Expression *e; | |
2835 Type *t0 = NULL; | |
2836 | |
2837 #if LOGSEMANTIC | |
2838 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2839 #endif | |
2840 if (type) | |
2841 return this; | |
2842 | |
2843 // Run semantic() on each element | |
2844 for (int i = 0; i < elements->dim; i++) | |
2845 { e = (Expression *)elements->data[i]; | |
2846 e = e->semantic(sc); | |
2847 elements->data[i] = (void *)e; | |
2848 } | |
2849 expandTuples(elements); | |
2850 for (int i = 0; i < elements->dim; i++) | |
2851 { e = (Expression *)elements->data[i]; | |
2852 | |
2853 if (!e->type) | |
2854 error("%s has no value", e->toChars()); | |
2855 e = resolveProperties(sc, e); | |
2856 | |
2857 unsigned char committed = 1; | |
2858 if (e->op == TOKstring) | |
2859 committed = ((StringExp *)e)->committed; | |
2860 | |
2861 if (!t0) | |
2862 { t0 = e->type; | |
2863 // Convert any static arrays to dynamic arrays | |
2864 if (t0->ty == Tsarray) | |
2865 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2866 t0 = ((TypeSArray *)t0)->next->arrayOf(); |
159 | 2867 e = e->implicitCastTo(sc, t0); |
2868 } | |
2869 } | |
2870 else | |
2871 e = e->implicitCastTo(sc, t0); | |
2872 if (!committed && e->op == TOKstring) | |
2873 { StringExp *se = (StringExp *)e; | |
2874 se->committed = 0; | |
2875 } | |
2876 elements->data[i] = (void *)e; | |
2877 } | |
2878 | |
2879 if (!t0) | |
2880 t0 = Type::tvoid; | |
2881 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
2882 type = type->semantic(loc, sc); | |
2883 return this; | |
2884 } | |
2885 | |
2886 int ArrayLiteralExp::checkSideEffect(int flag) | |
2887 { int f = 0; | |
2888 | |
2889 for (size_t i = 0; i < elements->dim; i++) | |
2890 { Expression *e = (Expression *)elements->data[i]; | |
2891 | |
2892 f |= e->checkSideEffect(2); | |
2893 } | |
2894 if (flag == 0 && f == 0) | |
2895 Expression::checkSideEffect(0); | |
2896 return f; | |
2897 } | |
2898 | |
2899 int ArrayLiteralExp::isBool(int result) | |
2900 { | |
2901 size_t dim = elements ? elements->dim : 0; | |
2902 return result ? (dim != 0) : (dim == 0); | |
2903 } | |
2904 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2905 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2906 int ArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2907 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2908 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
|
2909 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2910 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2911 |
159 | 2912 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2913 { | |
2914 buf->writeByte('['); | |
2915 argsToCBuffer(buf, elements, hgs); | |
2916 buf->writeByte(']'); | |
2917 } | |
2918 | |
2919 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2920 { | |
2921 size_t dim = elements ? elements->dim : 0; | |
2922 buf->printf("A%u", dim); | |
2923 for (size_t i = 0; i < dim; i++) | |
2924 { Expression *e = (Expression *)elements->data[i]; | |
2925 e->toMangleBuffer(buf); | |
2926 } | |
2927 } | |
2928 | |
2929 /************************ AssocArrayLiteralExp ************************************/ | |
2930 | |
2931 // [ key0 : value0, key1 : value1, ... ] | |
2932 | |
2933 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
2934 Expressions *keys, Expressions *values) | |
2935 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
2936 { | |
2937 assert(keys->dim == values->dim); | |
2938 this->keys = keys; | |
2939 this->values = values; | |
2940 } | |
2941 | |
2942 Expression *AssocArrayLiteralExp::syntaxCopy() | |
2943 { | |
2944 return new AssocArrayLiteralExp(loc, | |
2945 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
2946 } | |
2947 | |
2948 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
2949 { Expression *e; | |
2950 Type *tkey = NULL; | |
2951 Type *tvalue = NULL; | |
2952 | |
2953 #if LOGSEMANTIC | |
2954 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
2955 #endif | |
2956 | |
2957 // Run semantic() on each element | |
2958 for (size_t i = 0; i < keys->dim; i++) | |
2959 { Expression *key = (Expression *)keys->data[i]; | |
2960 Expression *value = (Expression *)values->data[i]; | |
2961 | |
2962 key = key->semantic(sc); | |
2963 value = value->semantic(sc); | |
2964 | |
2965 keys->data[i] = (void *)key; | |
2966 values->data[i] = (void *)value; | |
2967 } | |
2968 expandTuples(keys); | |
2969 expandTuples(values); | |
2970 if (keys->dim != values->dim) | |
2971 { | |
2972 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
2973 keys->setDim(0); | |
2974 values->setDim(0); | |
2975 } | |
2976 for (size_t i = 0; i < keys->dim; i++) | |
2977 { Expression *key = (Expression *)keys->data[i]; | |
2978 Expression *value = (Expression *)values->data[i]; | |
2979 | |
2980 if (!key->type) | |
2981 error("%s has no value", key->toChars()); | |
2982 if (!value->type) | |
2983 error("%s has no value", value->toChars()); | |
2984 key = resolveProperties(sc, key); | |
2985 value = resolveProperties(sc, value); | |
2986 | |
2987 if (!tkey) | |
2988 tkey = key->type; | |
2989 else | |
2990 key = key->implicitCastTo(sc, tkey); | |
2991 keys->data[i] = (void *)key; | |
2992 | |
2993 if (!tvalue) | |
2994 tvalue = value->type; | |
2995 else | |
2996 value = value->implicitCastTo(sc, tvalue); | |
2997 values->data[i] = (void *)value; | |
2998 } | |
2999 | |
3000 if (!tkey) | |
3001 tkey = Type::tvoid; | |
3002 if (!tvalue) | |
3003 tvalue = Type::tvoid; | |
3004 type = new TypeAArray(tvalue, tkey); | |
3005 type = type->semantic(loc, sc); | |
3006 return this; | |
3007 } | |
3008 | |
3009 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
3010 { int f = 0; | |
3011 | |
3012 for (size_t i = 0; i < keys->dim; i++) | |
3013 { Expression *key = (Expression *)keys->data[i]; | |
3014 Expression *value = (Expression *)values->data[i]; | |
3015 | |
3016 f |= key->checkSideEffect(2); | |
3017 f |= value->checkSideEffect(2); | |
3018 } | |
3019 if (flag == 0 && f == 0) | |
3020 Expression::checkSideEffect(0); | |
3021 return f; | |
3022 } | |
3023 | |
3024 int AssocArrayLiteralExp::isBool(int result) | |
3025 { | |
3026 size_t dim = keys->dim; | |
3027 return result ? (dim != 0) : (dim == 0); | |
3028 } | |
3029 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3030 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3031 int AssocArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3032 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3033 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3034 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3035 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3036 |
159 | 3037 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3038 { | |
3039 buf->writeByte('['); | |
3040 for (size_t i = 0; i < keys->dim; i++) | |
3041 { Expression *key = (Expression *)keys->data[i]; | |
3042 Expression *value = (Expression *)values->data[i]; | |
3043 | |
3044 if (i) | |
3045 buf->writeByte(','); | |
3046 expToCBuffer(buf, hgs, key, PREC_assign); | |
3047 buf->writeByte(':'); | |
3048 expToCBuffer(buf, hgs, value, PREC_assign); | |
3049 } | |
3050 buf->writeByte(']'); | |
3051 } | |
3052 | |
3053 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3054 { | |
3055 size_t dim = keys->dim; | |
3056 buf->printf("A%u", dim); | |
3057 for (size_t i = 0; i < dim; i++) | |
3058 { Expression *key = (Expression *)keys->data[i]; | |
3059 Expression *value = (Expression *)values->data[i]; | |
3060 | |
3061 key->toMangleBuffer(buf); | |
3062 value->toMangleBuffer(buf); | |
3063 } | |
3064 } | |
3065 | |
3066 /************************ StructLiteralExp ************************************/ | |
3067 | |
3068 // sd( e1, e2, e3, ... ) | |
3069 | |
3070 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
3071 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
3072 { | |
3073 this->sd = sd; | |
3074 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
|
3075 #if IN_DMD |
159 | 3076 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
|
3077 #endif |
159 | 3078 this->soffset = 0; |
3079 this->fillHoles = 1; | |
3080 } | |
3081 | |
3082 Expression *StructLiteralExp::syntaxCopy() | |
3083 { | |
3084 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
3085 } | |
3086 | |
3087 Expression *StructLiteralExp::semantic(Scope *sc) | |
3088 { Expression *e; | |
3089 | |
3090 #if LOGSEMANTIC | |
3091 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
3092 #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
|
3093 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
|
3094 return this; |
159 | 3095 |
3096 // Run semantic() on each element | |
3097 for (size_t i = 0; i < elements->dim; i++) | |
3098 { e = (Expression *)elements->data[i]; | |
3099 if (!e) | |
3100 continue; | |
3101 e = e->semantic(sc); | |
3102 elements->data[i] = (void *)e; | |
3103 } | |
3104 expandTuples(elements); | |
3105 size_t offset = 0; | |
3106 for (size_t i = 0; i < elements->dim; i++) | |
3107 { e = (Expression *)elements->data[i]; | |
3108 if (!e) | |
3109 continue; | |
3110 | |
3111 if (!e->type) | |
3112 error("%s has no value", e->toChars()); | |
3113 e = resolveProperties(sc, e); | |
3114 if (i >= sd->fields.dim) | |
3115 { error("more initializers than fields of %s", sd->toChars()); | |
3116 break; | |
3117 } | |
3118 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3119 VarDeclaration *v = s->isVarDeclaration(); | |
3120 assert(v); | |
3121 if (v->offset < offset) | |
3122 error("overlapping initialization for %s", v->toChars()); | |
3123 offset = v->offset + v->type->size(); | |
3124 | |
3125 Type *telem = v->type; | |
3126 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
3127 { /* Static array initialization, as in: | |
3128 * T[3][5] = e; | |
3129 */ | |
3130 telem = telem->toBasetype()->nextOf(); | |
3131 } | |
3132 | |
3133 e = e->implicitCastTo(sc, telem); | |
3134 | |
3135 elements->data[i] = (void *)e; | |
3136 } | |
3137 | |
3138 /* Fill out remainder of elements[] with default initializers for fields[] | |
3139 */ | |
3140 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
3141 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3142 VarDeclaration *v = s->isVarDeclaration(); | |
3143 assert(v); | |
3144 | |
3145 if (v->offset < offset) | |
3146 { e = NULL; | |
3147 sd->hasUnions = 1; | |
3148 } | |
3149 else | |
3150 { | |
3151 if (v->init) | |
3152 { e = v->init->toExpression(); | |
3153 if (!e) | |
3154 error("cannot make expression out of initializer for %s", v->toChars()); | |
3155 } | |
3156 else | |
3157 { e = v->type->defaultInit(); | |
3158 e->loc = loc; | |
3159 } | |
3160 offset = v->offset + v->type->size(); | |
3161 } | |
3162 elements->push(e); | |
3163 } | |
3164 | |
3165 type = sd->type; | |
3166 return this; | |
3167 } | |
3168 | |
3169 /************************************** | |
3170 * Gets expression at offset of type. | |
3171 * Returns NULL if not found. | |
3172 */ | |
3173 | |
3174 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
|
3175 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3176 //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
|
3177 // /*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
|
3178 Expression *e = NULL; |
159 | 3179 int i = getFieldIndex(type, offset); |
3180 | |
3181 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
|
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("\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
|
3184 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
|
3185 e = (Expression *)elements->data[i]; |
159 | 3186 if (e) |
3187 { | |
3188 e = e->copy(); | |
3189 e->type = type; | |
3190 } | |
3191 } | |
3192 return e; | |
3193 } | |
3194 | |
3195 /************************************ | |
3196 * Get index of field. | |
3197 * Returns -1 if not found. | |
3198 */ | |
3199 | |
3200 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
3201 { | |
3202 /* Find which field offset is by looking at the field offsets | |
3203 */ | |
3204 for (size_t i = 0; i < sd->fields.dim; i++) | |
3205 { | |
3206 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3207 VarDeclaration *v = s->isVarDeclaration(); | |
3208 assert(v); | |
3209 | |
3210 if (offset == v->offset && | |
3211 type->size() == v->type->size()) | |
3212 { Expression *e = (Expression *)elements->data[i]; | |
3213 if (e) | |
3214 { | |
3215 return i; | |
3216 } | |
3217 break; | |
3218 } | |
3219 } | |
3220 return -1; | |
3221 } | |
3222 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3223 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3224 int StructLiteralExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3225 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3226 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3227 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3228 #endif |
159 | 3229 |
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
|
3230 /* |
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
|
3231 Removed in LDC. See declaration. |
159 | 3232 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) |
3233 { | |
3234 return this; | |
3235 } | |
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
|
3236 */ |
159 | 3237 |
3238 | |
3239 int StructLiteralExp::checkSideEffect(int flag) | |
3240 { int f = 0; | |
3241 | |
3242 for (size_t i = 0; i < elements->dim; i++) | |
3243 { Expression *e = (Expression *)elements->data[i]; | |
3244 if (!e) | |
3245 continue; | |
3246 | |
3247 f |= e->checkSideEffect(2); | |
3248 } | |
3249 if (flag == 0 && f == 0) | |
3250 Expression::checkSideEffect(0); | |
3251 return f; | |
3252 } | |
3253 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3254 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3255 int StructLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3256 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3257 return arrayExpressionCanThrow(elements); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3258 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3259 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3260 |
159 | 3261 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3262 { | |
3263 buf->writestring(sd->toChars()); | |
3264 buf->writeByte('('); | |
3265 argsToCBuffer(buf, elements, hgs); | |
3266 buf->writeByte(')'); | |
3267 } | |
3268 | |
3269 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3270 { | |
3271 size_t dim = elements ? elements->dim : 0; | |
3272 buf->printf("S%u", dim); | |
3273 for (size_t i = 0; i < dim; i++) | |
3274 { Expression *e = (Expression *)elements->data[i]; | |
3275 if (e) | |
3276 e->toMangleBuffer(buf); | |
3277 else | |
3278 buf->writeByte('v'); // 'v' for void | |
3279 } | |
3280 } | |
3281 | |
3282 /************************ TypeDotIdExp ************************************/ | |
3283 | |
3284 /* Things like: | |
3285 * int.size | |
3286 * foo.size | |
3287 * (foo).size | |
3288 * cast(foo).size | |
3289 */ | |
3290 | |
3291 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident) | |
3292 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp)) | |
3293 { | |
3294 this->type = type; | |
3295 this->ident = ident; | |
3296 } | |
3297 | |
3298 Expression *TypeDotIdExp::syntaxCopy() | |
3299 { | |
3300 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident); | |
3301 return te; | |
3302 } | |
3303 | |
3304 Expression *TypeDotIdExp::semantic(Scope *sc) | |
3305 { Expression *e; | |
3306 | |
3307 #if LOGSEMANTIC | |
3308 printf("TypeDotIdExp::semantic()\n"); | |
3309 #endif | |
3310 e = new DotIdExp(loc, new TypeExp(loc, type), ident); | |
3311 e = e->semantic(sc); | |
3312 return e; | |
3313 } | |
3314 | |
3315 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3316 { | |
3317 buf->writeByte('('); | |
3318 type->toCBuffer(buf, NULL, hgs); | |
3319 buf->writeByte(')'); | |
3320 buf->writeByte('.'); | |
3321 buf->writestring(ident->toChars()); | |
3322 } | |
3323 | |
3324 /************************************************************/ | |
3325 | |
3326 // Mainly just a placeholder | |
3327 | |
3328 TypeExp::TypeExp(Loc loc, Type *type) | |
3329 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
3330 { | |
3331 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
3332 this->type = type; | |
3333 } | |
3334 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3335 Expression *TypeExp::syntaxCopy() |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3336 { |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3337 //printf("TypeExp::syntaxCopy()\n"); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3338 return new TypeExp(loc, type->syntaxCopy()); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3339 } |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3340 |
159 | 3341 Expression *TypeExp::semantic(Scope *sc) |
3342 { | |
3343 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
3344 type = type->semantic(loc, sc); | |
3345 return this; | |
3346 } | |
3347 | |
3348 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3349 { | |
3350 type->toCBuffer(buf, NULL, hgs); | |
3351 } | |
3352 | |
3353 /************************************************************/ | |
3354 | |
3355 // Mainly just a placeholder | |
3356 | |
3357 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
3358 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
3359 { | |
3360 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
3361 //static int count; if (++count == 38) *(char*)0=0; | |
3362 this->sds = pkg; | |
3363 } | |
3364 | |
3365 Expression *ScopeExp::syntaxCopy() | |
3366 { | |
3367 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
3368 return se; | |
3369 } | |
3370 | |
3371 Expression *ScopeExp::semantic(Scope *sc) | |
3372 { | |
3373 TemplateInstance *ti; | |
3374 ScopeDsymbol *sds2; | |
3375 | |
3376 #if LOGSEMANTIC | |
3377 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
3378 #endif | |
3379 Lagain: | |
3380 ti = sds->isTemplateInstance(); | |
3381 if (ti && !global.errors) | |
3382 { Dsymbol *s; | |
3383 if (!ti->semanticdone) | |
3384 ti->semantic(sc); | |
3385 s = ti->inst->toAlias(); | |
3386 sds2 = s->isScopeDsymbol(); | |
3387 if (!sds2) | |
3388 { Expression *e; | |
3389 | |
3390 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
3391 if (ti->withsym) | |
3392 { | |
3393 // Same as wthis.s | |
3394 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3395 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3396 } | |
3397 else | |
3398 e = new DsymbolExp(loc, s); | |
3399 e = e->semantic(sc); | |
3400 //printf("-1ScopeExp::semantic()\n"); | |
3401 return e; | |
3402 } | |
3403 if (sds2 != sds) | |
3404 { | |
3405 sds = sds2; | |
3406 goto Lagain; | |
3407 } | |
3408 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3409 } | |
3410 else | |
3411 { | |
3412 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3413 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3414 sds->semantic(sc); | |
3415 } | |
3416 type = Type::tvoid; | |
3417 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3418 return this; | |
3419 } | |
3420 | |
3421 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3422 { | |
3423 if (sds->isTemplateInstance()) | |
3424 { | |
3425 sds->toCBuffer(buf, hgs); | |
3426 } | |
3427 else | |
3428 { | |
3429 buf->writestring(sds->kind()); | |
3430 buf->writestring(" "); | |
3431 buf->writestring(sds->toChars()); | |
3432 } | |
3433 } | |
3434 | |
3435 /********************** TemplateExp **************************************/ | |
3436 | |
3437 // Mainly just a placeholder | |
3438 | |
3439 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3440 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3441 { | |
3442 //printf("TemplateExp(): %s\n", td->toChars()); | |
3443 this->td = td; | |
3444 } | |
3445 | |
3446 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3447 { | |
3448 buf->writestring(td->toChars()); | |
3449 } | |
3450 | |
3451 void TemplateExp::rvalue() | |
3452 { | |
3453 error("template %s has no value", toChars()); | |
3454 } | |
3455 | |
3456 /********************** NewExp **************************************/ | |
3457 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3458 /* thisexp.new(newargs) newtype(arguments) */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3459 |
159 | 3460 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, |
3461 Type *newtype, Expressions *arguments) | |
3462 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3463 { | |
3464 this->thisexp = thisexp; | |
3465 this->newargs = newargs; | |
3466 this->newtype = newtype; | |
3467 this->arguments = arguments; | |
3468 member = NULL; | |
3469 allocator = NULL; | |
3470 onstack = 0; | |
3471 } | |
3472 | |
3473 Expression *NewExp::syntaxCopy() | |
3474 { | |
3475 return new NewExp(loc, | |
3476 thisexp ? thisexp->syntaxCopy() : NULL, | |
3477 arraySyntaxCopy(newargs), | |
3478 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3479 } | |
3480 | |
3481 | |
3482 Expression *NewExp::semantic(Scope *sc) | |
3483 { int i; | |
3484 Type *tb; | |
3485 ClassDeclaration *cdthis = NULL; | |
3486 | |
3487 #if LOGSEMANTIC | |
3488 printf("NewExp::semantic() %s\n", toChars()); | |
3489 if (thisexp) | |
3490 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3491 printf("\tnewtype: %s\n", newtype->toChars()); | |
3492 #endif | |
3493 if (type) // if semantic() already run | |
3494 return this; | |
3495 | |
3496 Lagain: | |
3497 if (thisexp) | |
3498 { thisexp = thisexp->semantic(sc); | |
3499 cdthis = thisexp->type->isClassHandle(); | |
3500 if (cdthis) | |
3501 { | |
3502 sc = sc->push(cdthis); | |
3503 type = newtype->semantic(loc, sc); | |
3504 sc = sc->pop(); | |
3505 } | |
3506 else | |
3507 { | |
3508 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3509 type = newtype->semantic(loc, sc); | |
3510 } | |
3511 } | |
3512 else | |
3513 type = newtype->semantic(loc, sc); | |
3514 newtype = type; // in case type gets cast to something else | |
3515 tb = type->toBasetype(); | |
3516 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3517 | |
3518 arrayExpressionSemantic(newargs, sc); | |
3519 preFunctionArguments(loc, sc, newargs); | |
3520 arrayExpressionSemantic(arguments, sc); | |
3521 preFunctionArguments(loc, sc, arguments); | |
3522 | |
3523 if (thisexp && tb->ty != Tclass) | |
3524 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3525 | |
3526 if (tb->ty == Tclass) | |
3527 { TypeFunction *tf; | |
3528 | |
3529 TypeClass *tc = (TypeClass *)(tb); | |
3530 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3531 if (cd->isInterfaceDeclaration()) | |
3532 error("cannot create instance of interface %s", cd->toChars()); | |
3533 else if (cd->isAbstract()) | |
3534 { error("cannot create instance of abstract class %s", cd->toChars()); | |
3535 for (int i = 0; i < cd->vtbl.dim; i++) | |
3536 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration(); | |
3537 if (fd && fd->isAbstract()) | |
3538 error("function %s is abstract", fd->toChars()); | |
3539 } | |
3540 } | |
3541 checkDeprecated(sc, cd); | |
3542 if (cd->isNested()) | |
3543 { /* We need a 'this' pointer for the nested class. | |
3544 * Ensure we have the right one. | |
3545 */ | |
3546 Dsymbol *s = cd->toParent2(); | |
3547 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
|
3548 FuncDeclaration *fdn = s->isFuncDeclaration(); |
159 | 3549 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3550 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3551 if (cdn) |
3552 { | |
3553 if (!cdthis) | |
3554 { | |
3555 // Supply an implicit 'this' and try again | |
3556 thisexp = new ThisExp(loc); | |
3557 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3558 { if (!sp) | |
3559 { | |
3560 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3561 break; | |
3562 } | |
3563 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3564 if (!cdp) | |
3565 continue; | |
3566 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3567 break; | |
3568 // Add a '.outer' and try again | |
3569 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3570 } | |
3571 if (!global.errors) | |
3572 goto Lagain; | |
3573 } | |
3574 if (cdthis) | |
3575 { | |
3576 //printf("cdthis = %s\n", cdthis->toChars()); | |
3577 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3578 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3579 } | |
3580 #if 0 | |
3581 else | |
3582 { | |
3583 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3584 { | |
3585 if (!sf) | |
3586 { | |
3587 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3588 break; | |
3589 } | |
3590 printf("sf = %s\n", sf->toChars()); | |
3591 AggregateDeclaration *ad = sf->isThis(); | |
3592 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3593 break; | |
3594 } | |
3595 } | |
3596 #endif | |
3597 } | |
3598 else if (thisexp) | |
3599 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
|
3600 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
|
3601 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3602 // 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
|
3603 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
|
3604 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3605 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
|
3606 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3607 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
|
3608 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
|
3609 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3610 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
|
3611 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3612 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3613 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3614 |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3615 } |
159 | 3616 } |
3617 else if (thisexp) | |
3618 error("e.new is only for allocating nested classes"); | |
3619 | |
3620 FuncDeclaration *f = cd->ctor; | |
3621 if (f) | |
3622 { | |
3623 assert(f); | |
3624 f = f->overloadResolve(loc, arguments); | |
3625 checkDeprecated(sc, f); | |
3626 member = f->isCtorDeclaration(); | |
3627 assert(member); | |
3628 | |
3629 cd->accessCheck(loc, sc, member); | |
3630 | |
3631 tf = (TypeFunction *)f->type; | |
3632 type = tf->next; | |
3633 | |
3634 if (!arguments) | |
3635 arguments = new Expressions(); | |
3636 functionArguments(loc, sc, tf, arguments); | |
3637 } | |
3638 else | |
3639 { | |
3640 if (arguments && arguments->dim) | |
3641 error("no constructor for %s", cd->toChars()); | |
3642 } | |
3643 | |
3644 if (cd->aggNew) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3645 { |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
3646 // Prepend the size argument to newargs[] |
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
|
3647 Expression *e = new IntegerExp(loc, cd->size(loc), Type::tsize_t); |
159 | 3648 if (!newargs) |
3649 newargs = new Expressions(); | |
3650 newargs->shift(e); | |
3651 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3652 f = cd->aggNew->overloadResolve(loc, newargs); |
159 | 3653 allocator = f->isNewDeclaration(); |
3654 assert(allocator); | |
3655 | |
3656 tf = (TypeFunction *)f->type; | |
3657 functionArguments(loc, sc, tf, newargs); | |
3658 } | |
3659 else | |
3660 { | |
3661 if (newargs && newargs->dim) | |
3662 error("no allocator for %s", cd->toChars()); | |
3663 } | |
3664 } | |
3665 else if (tb->ty == Tstruct) | |
3666 { | |
3667 TypeStruct *ts = (TypeStruct *)tb; | |
3668 StructDeclaration *sd = ts->sym; | |
3669 FuncDeclaration *f = sd->aggNew; | |
3670 TypeFunction *tf; | |
3671 | |
3672 if (arguments && arguments->dim) | |
3673 error("no constructor for %s", type->toChars()); | |
3674 | |
3675 if (f) | |
3676 { | |
3677 Expression *e; | |
3678 | |
3679 // Prepend the uint size argument to newargs[] | |
3680 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3681 if (!newargs) | |
3682 newargs = new Expressions(); | |
3683 newargs->shift(e); | |
3684 | |
3685 f = f->overloadResolve(loc, newargs); | |
3686 allocator = f->isNewDeclaration(); | |
3687 assert(allocator); | |
3688 | |
3689 tf = (TypeFunction *)f->type; | |
3690 functionArguments(loc, sc, tf, newargs); | |
3691 | |
3692 e = new VarExp(loc, f); | |
3693 e = new CallExp(loc, e, newargs); | |
3694 e = e->semantic(sc); | |
3695 e->type = type->pointerTo(); | |
3696 return e; | |
3697 } | |
3698 | |
3699 type = type->pointerTo(); | |
3700 } | |
3701 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3702 { | |
3703 for (size_t i = 0; i < arguments->dim; i++) | |
3704 { | |
3705 if (tb->ty != Tarray) | |
3706 { error("too many arguments for array"); | |
3707 arguments->dim = i; | |
3708 break; | |
3709 } | |
3710 | |
3711 Expression *arg = (Expression *)arguments->data[i]; | |
3712 arg = resolveProperties(sc, arg); | |
3713 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3714 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3715 error("negative array index %s", arg->toChars()); | |
3716 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
|
3717 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3718 } |
3719 } | |
3720 else if (tb->isscalar()) | |
3721 { | |
3722 if (arguments && arguments->dim) | |
3723 error("no constructor for %s", type->toChars()); | |
3724 | |
3725 type = type->pointerTo(); | |
3726 } | |
3727 else | |
3728 { | |
3729 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3730 type = type->pointerTo(); | |
3731 } | |
3732 | |
3733 //printf("NewExp: '%s'\n", toChars()); | |
3734 //printf("NewExp:type '%s'\n", type->toChars()); | |
3735 | |
3736 return this; | |
3737 } | |
3738 | |
3739 int NewExp::checkSideEffect(int flag) | |
3740 { | |
3741 return 1; | |
3742 } | |
3743 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3744 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3745 int NewExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3746 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3747 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3748 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3749 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3750 |
159 | 3751 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3752 { int i; | |
3753 | |
3754 if (thisexp) | |
3755 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3756 buf->writeByte('.'); | |
3757 } | |
3758 buf->writestring("new "); | |
3759 if (newargs && newargs->dim) | |
3760 { | |
3761 buf->writeByte('('); | |
3762 argsToCBuffer(buf, newargs, hgs); | |
3763 buf->writeByte(')'); | |
3764 } | |
3765 newtype->toCBuffer(buf, NULL, hgs); | |
3766 if (arguments && arguments->dim) | |
3767 { | |
3768 buf->writeByte('('); | |
3769 argsToCBuffer(buf, arguments, hgs); | |
3770 buf->writeByte(')'); | |
3771 } | |
3772 } | |
3773 | |
3774 /********************** NewAnonClassExp **************************************/ | |
3775 | |
3776 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3777 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3778 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3779 { | |
3780 this->thisexp = thisexp; | |
3781 this->newargs = newargs; | |
3782 this->cd = cd; | |
3783 this->arguments = arguments; | |
3784 } | |
3785 | |
3786 Expression *NewAnonClassExp::syntaxCopy() | |
3787 { | |
3788 return new NewAnonClassExp(loc, | |
3789 thisexp ? thisexp->syntaxCopy() : NULL, | |
3790 arraySyntaxCopy(newargs), | |
3791 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3792 arraySyntaxCopy(arguments)); | |
3793 } | |
3794 | |
3795 | |
3796 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3797 { | |
3798 #if LOGSEMANTIC | |
3799 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3800 //printf("thisexp = %p\n", thisexp); |
159 | 3801 //printf("type: %s\n", type->toChars()); |
3802 #endif | |
3803 | |
3804 Expression *d = new DeclarationExp(loc, cd); | |
3805 d = d->semantic(sc); | |
3806 | |
3807 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3808 | |
3809 Expression *c = new CommaExp(loc, d, n); | |
3810 return c->semantic(sc); | |
3811 } | |
3812 | |
3813 int NewAnonClassExp::checkSideEffect(int flag) | |
3814 { | |
3815 return 1; | |
3816 } | |
3817 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3818 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3819 int NewAnonClassExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3820 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3821 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3822 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3823 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3824 |
159 | 3825 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3826 { int i; | |
3827 | |
3828 if (thisexp) | |
3829 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3830 buf->writeByte('.'); | |
3831 } | |
3832 buf->writestring("new"); | |
3833 if (newargs && newargs->dim) | |
3834 { | |
3835 buf->writeByte('('); | |
3836 argsToCBuffer(buf, newargs, hgs); | |
3837 buf->writeByte(')'); | |
3838 } | |
3839 buf->writestring(" class "); | |
3840 if (arguments && arguments->dim) | |
3841 { | |
3842 buf->writeByte('('); | |
3843 argsToCBuffer(buf, arguments, hgs); | |
3844 buf->writeByte(')'); | |
3845 } | |
3846 //buf->writestring(" { }"); | |
3847 if (cd) | |
3848 { | |
3849 cd->toCBuffer(buf, hgs); | |
3850 } | |
3851 } | |
3852 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3853 /********************** SymbolExp **************************************/ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3854 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3855 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3856 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
|
3857 : Expression(loc, op, size) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3858 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3859 assert(var); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3860 this->var = var; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3861 this->hasOverloads = hasOverloads; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3862 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3863 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3864 |
159 | 3865 /********************** SymOffExp **************************************/ |
3866 | |
3867 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3868 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3869 { | |
3870 assert(var); | |
3871 this->var = var; | |
3872 this->offset = offset; | |
3873 VarDeclaration *v = var->isVarDeclaration(); | |
3874 if (v && v->needThis()) | |
3875 error("need 'this' for address of %s", v->toChars()); | |
3876 } | |
3877 | |
3878 Expression *SymOffExp::semantic(Scope *sc) | |
3879 { | |
3880 #if LOGSEMANTIC | |
3881 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3882 #endif | |
3883 //var->semantic(sc); | |
3884 if (!type) | |
3885 type = var->type->pointerTo(); | |
3886 VarDeclaration *v = var->isVarDeclaration(); | |
3887 if (v) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
3888 v->checkNestedReference(sc, loc); |
159 | 3889 return this; |
3890 } | |
3891 | |
3892 int SymOffExp::isBool(int result) | |
3893 { | |
3894 return result ? TRUE : FALSE; | |
3895 } | |
3896 | |
3897 void SymOffExp::checkEscape() | |
3898 { | |
3899 VarDeclaration *v = var->isVarDeclaration(); | |
3900 if (v) | |
3901 { | |
3902 if (!v->isDataseg()) | |
3903 error("escaping reference to local variable %s", v->toChars()); | |
3904 } | |
3905 } | |
3906 | |
3907 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3908 { | |
3909 if (offset) | |
3910 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3911 else | |
3912 buf->printf("& %s", var->toChars()); | |
3913 } | |
3914 | |
3915 /******************************** VarExp **************************/ | |
3916 | |
3917 VarExp::VarExp(Loc loc, Declaration *var) | |
3918 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3919 { | |
3920 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3921 this->var = var; | |
3922 this->type = var->type; | |
3923 } | |
3924 | |
3925 int VarExp::equals(Object *o) | |
3926 { VarExp *ne; | |
3927 | |
3928 if (this == o || | |
3929 (((Expression *)o)->op == TOKvar && | |
3930 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3931 var == ne->var)) | |
3932 return 1; | |
3933 return 0; | |
3934 } | |
3935 | |
3936 Expression *VarExp::semantic(Scope *sc) | |
3937 { FuncLiteralDeclaration *fd; | |
3938 | |
3939 #if LOGSEMANTIC | |
3940 printf("VarExp::semantic(%s)\n", toChars()); | |
3941 #endif | |
3942 if (!type) | |
3943 { type = var->type; | |
3944 #if 0 | |
3945 if (var->storage_class & STClazy) | |
3946 { | |
3947 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3948 type = new TypeDelegate(tf); | |
3949 type = type->semantic(loc, sc); | |
3950 } | |
3951 #endif | |
3952 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3953 /* 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
|
3954 * problems when instantiating imported templates passing private |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3955 * variables as alias template parameters. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3956 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3957 //accessCheck(loc, sc, NULL, var); |
159 | 3958 |
3959 VarDeclaration *v = var->isVarDeclaration(); | |
3960 if (v) | |
3961 { | |
3962 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3963 { | |
3964 ExpInitializer *ei = v->init->isExpInitializer(); | |
3965 if (ei) | |
3966 { | |
3967 //ei->exp->implicitCastTo(sc, type)->print(); | |
3968 return ei->exp->implicitCastTo(sc, type); | |
3969 } | |
3970 } | |
3971 v->checkNestedReference(sc, loc); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3972 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3973 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
|
3974 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3975 if (v->isDataseg() && !v->isInvariant()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3976 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
|
3977 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3978 #endif |
159 | 3979 } |
3980 #if 0 | |
3981 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3982 { Expression *e; | |
3983 e = new FuncExp(loc, fd); | |
3984 e->type = type; | |
3985 return e; | |
3986 } | |
3987 #endif | |
3988 return this; | |
3989 } | |
3990 | |
3991 char *VarExp::toChars() | |
3992 { | |
3993 return var->toChars(); | |
3994 } | |
3995 | |
3996 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3997 { | |
3998 buf->writestring(var->toChars()); | |
3999 } | |
4000 | |
4001 void VarExp::checkEscape() | |
4002 { | |
4003 VarDeclaration *v = var->isVarDeclaration(); | |
4004 if (v) | |
4005 { Type *tb = v->type->toBasetype(); | |
4006 // if reference type | |
4007 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
4008 { | |
4009 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
4010 error("escaping reference to auto local %s", v->toChars()); | |
4011 else if (v->storage_class & STCvariadic) | |
4012 error("escaping reference to variadic parameter %s", v->toChars()); | |
4013 } | |
4014 } | |
4015 } | |
4016 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4017 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4018 int VarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4019 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4020 if (var->storage_class & STClazy) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4021 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4022 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4023 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4024 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4025 |
159 | 4026 Expression *VarExp::toLvalue(Scope *sc, Expression *e) |
4027 { | |
4028 #if 0 | |
4029 tym = tybasic(e1->ET->Tty); | |
4030 if (!(tyscalar(tym) || | |
4031 tym == TYstruct || | |
4032 tym == TYarray && e->Eoper == TOKaddr)) | |
4033 synerr(EM_lvalue); // lvalue expected | |
4034 #endif | |
4035 if (var->storage_class & STClazy) | |
4036 error("lazy variables cannot be lvalues"); | |
4037 return this; | |
4038 } | |
4039 | |
4040 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
4041 { | |
4042 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
4043 if (sc->incontract && var->isParameter()) | |
4044 error("cannot modify parameter '%s' in contract", var->toChars()); | |
4045 | |
4046 if (type && type->toBasetype()->ty == Tsarray) | |
4047 error("cannot change reference to static array '%s'", var->toChars()); | |
4048 | |
4049 VarDeclaration *v = var->isVarDeclaration(); | |
4050 if (v && v->canassign == 0 && | |
4051 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
4052 error("cannot modify final variable '%s'", var->toChars()); | |
4053 | |
4054 if (var->isCtorinit()) | |
4055 { // It's only modifiable if inside the right constructor | |
4056 Dsymbol *s = sc->func; | |
4057 while (1) | |
4058 { | |
4059 FuncDeclaration *fd = NULL; | |
4060 if (s) | |
4061 fd = s->isFuncDeclaration(); | |
4062 if (fd && | |
4063 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
4064 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
4065 fd->toParent() == var->toParent() | |
4066 ) | |
4067 { | |
4068 VarDeclaration *v = var->isVarDeclaration(); | |
4069 assert(v); | |
4070 v->ctorinit = 1; | |
4071 //printf("setting ctorinit\n"); | |
4072 } | |
4073 else | |
4074 { | |
4075 if (s) | |
4076 { s = s->toParent2(); | |
4077 continue; | |
4078 } | |
4079 else | |
4080 { | |
4081 const char *p = var->isStatic() ? "static " : ""; | |
4082 error("can only initialize %sconst %s inside %sconstructor", | |
4083 p, var->toChars(), p); | |
4084 } | |
4085 } | |
4086 break; | |
4087 } | |
4088 } | |
4089 | |
4090 // See if this expression is a modifiable lvalue (i.e. not const) | |
4091 return toLvalue(sc, e); | |
4092 } | |
4093 | |
4094 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4095 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4096 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4097 #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
|
4098 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
|
4099 : 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
|
4100 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4101 //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
|
4102 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
|
4103 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
|
4104 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4105 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4106 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
|
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 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
|
4109 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4110 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4111 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
|
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 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
|
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 #endif |
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 |
159 | 4118 /******************************** TupleExp **************************/ |
4119 | |
4120 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
4121 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4122 { | |
4123 //printf("TupleExp(this = %p)\n", this); | |
4124 this->exps = exps; | |
4125 this->type = NULL; | |
4126 } | |
4127 | |
4128 | |
4129 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
4130 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4131 { | |
4132 exps = new Expressions(); | |
4133 type = NULL; | |
4134 | |
4135 exps->reserve(tup->objects->dim); | |
4136 for (size_t i = 0; i < tup->objects->dim; i++) | |
4137 { Object *o = (Object *)tup->objects->data[i]; | |
4138 if (o->dyncast() == DYNCAST_EXPRESSION) | |
4139 { | |
4140 Expression *e = (Expression *)o; | |
4141 e = e->syntaxCopy(); | |
4142 exps->push(e); | |
4143 } | |
4144 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
4145 { | |
4146 Dsymbol *s = (Dsymbol *)o; | |
4147 Expression *e = new DsymbolExp(loc, s); | |
4148 exps->push(e); | |
4149 } | |
4150 else if (o->dyncast() == DYNCAST_TYPE) | |
4151 { | |
4152 Type *t = (Type *)o; | |
4153 Expression *e = new TypeExp(loc, t); | |
4154 exps->push(e); | |
4155 } | |
4156 else | |
4157 { | |
4158 error("%s is not an expression", o->toChars()); | |
4159 } | |
4160 } | |
4161 } | |
4162 | |
4163 int TupleExp::equals(Object *o) | |
4164 { TupleExp *ne; | |
4165 | |
4166 if (this == o) | |
4167 return 1; | |
4168 if (((Expression *)o)->op == TOKtuple) | |
4169 { | |
4170 TupleExp *te = (TupleExp *)o; | |
4171 if (exps->dim != te->exps->dim) | |
4172 return 0; | |
4173 for (size_t i = 0; i < exps->dim; i++) | |
4174 { Expression *e1 = (Expression *)exps->data[i]; | |
4175 Expression *e2 = (Expression *)te->exps->data[i]; | |
4176 | |
4177 if (!e1->equals(e2)) | |
4178 return 0; | |
4179 } | |
4180 return 1; | |
4181 } | |
4182 return 0; | |
4183 } | |
4184 | |
4185 Expression *TupleExp::syntaxCopy() | |
4186 { | |
4187 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
4188 } | |
4189 | |
4190 Expression *TupleExp::semantic(Scope *sc) | |
4191 { | |
4192 #if LOGSEMANTIC | |
4193 printf("+TupleExp::semantic(%s)\n", toChars()); | |
4194 #endif | |
4195 if (type) | |
4196 return this; | |
4197 | |
4198 // Run semantic() on each argument | |
4199 for (size_t i = 0; i < exps->dim; i++) | |
4200 { Expression *e = (Expression *)exps->data[i]; | |
4201 | |
4202 e = e->semantic(sc); | |
4203 if (!e->type) | |
4204 { error("%s has no value", e->toChars()); | |
4205 e->type = Type::terror; | |
4206 } | |
4207 exps->data[i] = (void *)e; | |
4208 } | |
4209 | |
4210 expandTuples(exps); | |
4211 if (0 && exps->dim == 1) | |
4212 { | |
4213 return (Expression *)exps->data[0]; | |
4214 } | |
4215 type = new TypeTuple(exps); | |
4216 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
4217 return this; | |
4218 } | |
4219 | |
4220 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4221 { | |
4222 buf->writestring("tuple("); | |
4223 argsToCBuffer(buf, exps, hgs); | |
4224 buf->writeByte(')'); | |
4225 } | |
4226 | |
4227 int TupleExp::checkSideEffect(int flag) | |
4228 { int f = 0; | |
4229 | |
4230 for (int i = 0; i < exps->dim; i++) | |
4231 { Expression *e = (Expression *)exps->data[i]; | |
4232 | |
4233 f |= e->checkSideEffect(2); | |
4234 } | |
4235 if (flag == 0 && f == 0) | |
4236 Expression::checkSideEffect(0); | |
4237 return f; | |
4238 } | |
4239 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4240 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4241 int TupleExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4242 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4243 return arrayExpressionCanThrow(exps); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4244 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4245 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4246 |
159 | 4247 void TupleExp::checkEscape() |
4248 { | |
4249 for (size_t i = 0; i < exps->dim; i++) | |
4250 { Expression *e = (Expression *)exps->data[i]; | |
4251 e->checkEscape(); | |
4252 } | |
4253 } | |
4254 | |
4255 /******************************** FuncExp *********************************/ | |
4256 | |
4257 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4258 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4259 { | |
4260 this->fd = fd; | |
4261 } | |
4262 | |
4263 Expression *FuncExp::syntaxCopy() | |
4264 { | |
4265 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4266 } | |
4267 | |
4268 Expression *FuncExp::semantic(Scope *sc) | |
4269 { | |
4270 #if LOGSEMANTIC | |
4271 printf("FuncExp::semantic(%s)\n", toChars()); | |
4272 #endif | |
4273 if (!type) | |
4274 { | |
4275 fd->semantic(sc); | |
4276 fd->parent = sc->parent; | |
4277 if (global.errors) | |
4278 { | |
4279 if (!fd->type->next) | |
4280 fd->type->next = Type::terror; | |
4281 } | |
4282 else | |
4283 { | |
4284 fd->semantic2(sc); | |
4285 if (!global.errors) | |
4286 { | |
4287 fd->semantic3(sc); | |
4288 | |
4289 if (!global.errors && global.params.useInline) | |
4290 fd->inlineScan(); | |
4291 } | |
4292 } | |
4293 | |
4294 // Type is a "delegate to" or "pointer to" the function literal | |
4295 if (fd->isNested()) | |
4296 { | |
4297 type = new TypeDelegate(fd->type); | |
4298 type = type->semantic(loc, sc); | |
4299 } | |
4300 else | |
4301 { | |
4302 type = fd->type->pointerTo(); | |
4303 } | |
4304 } | |
4305 return this; | |
4306 } | |
4307 | |
4308 char *FuncExp::toChars() | |
4309 { | |
4310 return fd->toChars(); | |
4311 } | |
4312 | |
4313 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4314 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
4315 fd->toCBuffer(buf, hgs); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
4316 //buf->writestring(fd->toChars()); |
159 | 4317 } |
4318 | |
4319 | |
4320 /******************************** DeclarationExp **************************/ | |
4321 | |
4322 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4323 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4324 { | |
4325 this->declaration = declaration; | |
4326 } | |
4327 | |
4328 Expression *DeclarationExp::syntaxCopy() | |
4329 { | |
4330 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4331 } | |
4332 | |
4333 Expression *DeclarationExp::semantic(Scope *sc) | |
4334 { | |
4335 if (type) | |
4336 return this; | |
4337 | |
4338 #if LOGSEMANTIC | |
4339 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4340 #endif | |
4341 | |
4342 /* This is here to support extern(linkage) declaration, | |
4343 * where the extern(linkage) winds up being an AttribDeclaration | |
4344 * wrapper. | |
4345 */ | |
4346 Dsymbol *s = declaration; | |
4347 | |
4348 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4349 if (ad) | |
4350 { | |
4351 if (ad->decl && ad->decl->dim == 1) | |
4352 s = (Dsymbol *)ad->decl->data[0]; | |
4353 } | |
4354 | |
4355 if (s->isVarDeclaration()) | |
4356 { // Do semantic() on initializer first, so: | |
4357 // int a = a; | |
4358 // will be illegal. | |
4359 declaration->semantic(sc); | |
4360 s->parent = sc->parent; | |
4361 } | |
4362 | |
4363 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4364 // Insert into both local scope and function scope. | |
4365 // Must be unique in both. | |
4366 if (s->ident) | |
4367 { | |
4368 if (!sc->insert(s)) | |
4369 error("declaration %s is already defined", s->toPrettyChars()); | |
4370 else if (sc->func) | |
4371 { VarDeclaration *v = s->isVarDeclaration(); | |
4372 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4373 !sc->func->localsymtab->insert(s)) | |
4374 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4375 else if (!global.params.useDeprecated) | |
4376 { // Disallow shadowing | |
4377 | |
4378 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4379 { Dsymbol *s2; | |
4380 | |
4381 if (scx->scopesym && scx->scopesym->symtab && | |
4382 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4383 s != s2) | |
4384 { | |
4385 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4386 } | |
4387 } | |
4388 } | |
4389 } | |
4390 } | |
4391 if (!s->isVarDeclaration()) | |
4392 { | |
4393 declaration->semantic(sc); | |
4394 s->parent = sc->parent; | |
4395 } | |
4396 if (!global.errors) | |
4397 { | |
4398 declaration->semantic2(sc); | |
4399 if (!global.errors) | |
4400 { | |
4401 declaration->semantic3(sc); | |
4402 | |
4403 if (!global.errors && global.params.useInline) | |
4404 declaration->inlineScan(); | |
4405 } | |
4406 } | |
4407 | |
4408 type = Type::tvoid; | |
4409 return this; | |
4410 } | |
4411 | |
4412 int DeclarationExp::checkSideEffect(int flag) | |
4413 { | |
4414 return 1; | |
4415 } | |
4416 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4417 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4418 int DeclarationExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4419 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4420 VarDeclaration *v = declaration->isVarDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4421 if (v && v->init) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4422 { ExpInitializer *ie = v->init->isExpInitializer(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4423 return ie && ie->exp->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4424 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4425 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4426 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4427 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4428 |
159 | 4429 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4430 { | |
4431 declaration->toCBuffer(buf, hgs); | |
4432 } | |
4433 | |
4434 | |
4435 /************************ TypeidExp ************************************/ | |
4436 | |
4437 /* | |
4438 * typeid(int) | |
4439 */ | |
4440 | |
4441 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4442 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4443 { | |
4444 this->typeidType = typeidType; | |
4445 } | |
4446 | |
4447 | |
4448 Expression *TypeidExp::syntaxCopy() | |
4449 { | |
4450 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4451 } | |
4452 | |
4453 | |
4454 Expression *TypeidExp::semantic(Scope *sc) | |
4455 { Expression *e; | |
4456 | |
4457 #if LOGSEMANTIC | |
4458 printf("TypeidExp::semantic()\n"); | |
4459 #endif | |
4460 typeidType = typeidType->semantic(loc, sc); | |
4461 e = typeidType->getTypeInfo(sc); | |
336 | 4462 if (e->loc.linnum == 0) |
4463 e->loc = loc; // so there's at least some line number info | |
159 | 4464 return e; |
4465 } | |
4466 | |
4467 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4468 { | |
4469 buf->writestring("typeid("); | |
4470 typeidType->toCBuffer(buf, NULL, hgs); | |
4471 buf->writeByte(')'); | |
4472 } | |
4473 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4474 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4475 #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
|
4476 /* |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4477 * __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
|
4478 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4479 |
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::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
|
4481 : 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
|
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 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
|
4484 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4487 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4488 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
|
4489 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4490 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
|
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 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
|
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 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
|
4497 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
|
4498 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
|
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 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
|
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->writeByte(','); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4503 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
|
4504 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
|
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 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4507 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
|
4508 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4509 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4510 |
159 | 4511 /************************************************************/ |
4512 | |
4513 HaltExp::HaltExp(Loc loc) | |
4514 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4515 { | |
4516 } | |
4517 | |
4518 Expression *HaltExp::semantic(Scope *sc) | |
4519 { | |
4520 #if LOGSEMANTIC | |
4521 printf("HaltExp::semantic()\n"); | |
4522 #endif | |
4523 type = Type::tvoid; | |
4524 return this; | |
4525 } | |
4526 | |
4527 int HaltExp::checkSideEffect(int flag) | |
4528 { | |
4529 return 1; | |
4530 } | |
4531 | |
4532 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4533 { | |
4534 buf->writestring("halt"); | |
4535 } | |
4536 | |
4537 /************************************************************/ | |
4538 | |
4539 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4540 Type *tspec, enum TOK tok2) | |
4541 : Expression(loc, TOKis, sizeof(IsExp)) | |
4542 { | |
4543 this->targ = targ; | |
4544 this->id = id; | |
4545 this->tok = tok; | |
4546 this->tspec = tspec; | |
4547 this->tok2 = tok2; | |
4548 } | |
4549 | |
4550 Expression *IsExp::syntaxCopy() | |
4551 { | |
4552 return new IsExp(loc, | |
4553 targ->syntaxCopy(), | |
4554 id, | |
4555 tok, | |
4556 tspec ? tspec->syntaxCopy() : NULL, | |
4557 tok2); | |
4558 } | |
4559 | |
4560 Expression *IsExp::semantic(Scope *sc) | |
4561 { Type *tded; | |
4562 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4563 /* 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
|
4564 * 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
|
4565 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4566 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4567 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4568 if (id && !(sc->flags & SCOPEstaticif)) |
4569 error("can only declare type aliases within static if conditionals"); | |
4570 | |
4571 unsigned errors_save = global.errors; | |
4572 global.errors = 0; | |
4573 global.gag++; // suppress printing of error messages | |
4574 targ = targ->semantic(loc, sc); | |
4575 global.gag--; | |
4576 unsigned gerrors = global.errors; | |
4577 global.errors = errors_save; | |
4578 | |
4579 if (gerrors) // if any errors happened | |
4580 { // then condition is false | |
4581 goto Lno; | |
4582 } | |
4583 else if (tok2 != TOKreserved) | |
4584 { | |
4585 switch (tok2) | |
4586 { | |
4587 case TOKtypedef: | |
4588 if (targ->ty != Ttypedef) | |
4589 goto Lno; | |
4590 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4591 break; | |
4592 | |
4593 case TOKstruct: | |
4594 if (targ->ty != Tstruct) | |
4595 goto Lno; | |
4596 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4597 goto Lno; | |
4598 tded = targ; | |
4599 break; | |
4600 | |
4601 case TOKunion: | |
4602 if (targ->ty != Tstruct) | |
4603 goto Lno; | |
4604 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4605 goto Lno; | |
4606 tded = targ; | |
4607 break; | |
4608 | |
4609 case TOKclass: | |
4610 if (targ->ty != Tclass) | |
4611 goto Lno; | |
4612 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4613 goto Lno; | |
4614 tded = targ; | |
4615 break; | |
4616 | |
4617 case TOKinterface: | |
4618 if (targ->ty != Tclass) | |
4619 goto Lno; | |
4620 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4621 goto Lno; | |
4622 tded = targ; | |
4623 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
|
4624 #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
|
4625 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
|
4626 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
|
4627 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
|
4628 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
|
4629 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4630 |
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 TOKinvariant: |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4632 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
|
4633 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
|
4634 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
|
4635 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
|
4636 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4637 #endif |
159 | 4638 |
4639 case TOKsuper: | |
4640 // If class or interface, get the base class and interfaces | |
4641 if (targ->ty != Tclass) | |
4642 goto Lno; | |
4643 else | |
4644 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4645 Arguments *args = new Arguments; | |
4646 args->reserve(cd->baseclasses.dim); | |
4647 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4648 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4649 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4650 } | |
4651 tded = new TypeTuple(args); | |
4652 } | |
4653 break; | |
4654 | |
4655 case TOKenum: | |
4656 if (targ->ty != Tenum) | |
4657 goto Lno; | |
4658 tded = ((TypeEnum *)targ)->sym->memtype; | |
4659 break; | |
4660 | |
4661 case TOKdelegate: | |
4662 if (targ->ty != Tdelegate) | |
4663 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
|
4664 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4665 break; |
4666 | |
4667 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
|
4668 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4669 if (targ->ty != Tfunction) |
159 | 4670 goto Lno; |
4671 tded = targ; | |
4672 | |
4673 /* Generate tuple from function parameter types. | |
4674 */ | |
4675 assert(tded->ty == Tfunction); | |
4676 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4677 size_t dim = Argument::dim(params); | |
4678 Arguments *args = new Arguments; | |
4679 args->reserve(dim); | |
4680 for (size_t i = 0; i < dim; i++) | |
4681 { Argument *arg = Argument::getNth(params, i); | |
4682 assert(arg && arg->type); | |
4683 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4684 } | |
4685 tded = new TypeTuple(args); | |
4686 break; | |
4687 } | |
4688 case TOKreturn: | |
4689 /* Get the 'return type' for the function, | |
4690 * delegate, or pointer to function. | |
4691 */ | |
4692 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
|
4693 tded = ((TypeFunction *)targ)->next; |
159 | 4694 else if (targ->ty == Tdelegate) |
4695 tded = targ->next->next; | |
4696 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4697 tded = targ->next->next; | |
4698 else | |
4699 goto Lno; | |
4700 break; | |
4701 | |
4702 default: | |
4703 assert(0); | |
4704 } | |
4705 goto Lyes; | |
4706 } | |
4707 else if (id && tspec) | |
4708 { | |
4709 /* Evaluate to TRUE if targ matches tspec. | |
4710 * If TRUE, declare id as an alias for the specialized type. | |
4711 */ | |
4712 | |
4713 MATCH m; | |
4714 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4715 | |
4716 TemplateParameters parameters; | |
4717 parameters.setDim(1); | |
4718 parameters.data[0] = (void *)&tp; | |
4719 | |
4720 Objects dedtypes; | |
4721 dedtypes.setDim(1); | |
4722 dedtypes.data[0] = NULL; | |
4723 | |
4724 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4725 if (m == MATCHnomatch || | |
4726 (m != MATCHexact && tok == TOKequal)) | |
4727 goto Lno; | |
4728 else | |
4729 { | |
4730 assert(dedtypes.dim == 1); | |
4731 tded = (Type *)dedtypes.data[0]; | |
4732 if (!tded) | |
4733 tded = targ; | |
4734 goto Lyes; | |
4735 } | |
4736 } | |
4737 else if (id) | |
4738 { | |
4739 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4740 */ | |
4741 tded = targ; | |
4742 goto Lyes; | |
4743 } | |
4744 else if (tspec) | |
4745 { | |
4746 /* Evaluate to TRUE if targ matches tspec | |
4747 */ | |
4748 tspec = tspec->semantic(loc, sc); | |
4749 //printf("targ = %s\n", targ->toChars()); | |
4750 //printf("tspec = %s\n", tspec->toChars()); | |
4751 if (tok == TOKcolon) | |
4752 { if (targ->implicitConvTo(tspec)) | |
4753 goto Lyes; | |
4754 else | |
4755 goto Lno; | |
4756 } | |
4757 else /* == */ | |
4758 { if (targ->equals(tspec)) | |
4759 goto Lyes; | |
4760 else | |
4761 goto Lno; | |
4762 } | |
4763 } | |
4764 | |
4765 Lyes: | |
4766 if (id) | |
4767 { | |
4768 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4769 s->semantic(sc); | |
4770 sc->insert(s); | |
4771 if (sc->sd) | |
4772 s->addMember(sc, sc->sd, 1); | |
4773 } | |
4774 return new IntegerExp(1); | |
4775 | |
4776 Lno: | |
4777 return new IntegerExp(0); | |
4778 } | |
4779 | |
4780 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4781 { | |
4782 buf->writestring("is("); | |
4783 targ->toCBuffer(buf, id, hgs); | |
4784 if (tok2 != TOKreserved) | |
4785 { | |
4786 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4787 } | |
4788 else if (tspec) | |
4789 { | |
4790 if (tok == TOKcolon) | |
4791 buf->writestring(" : "); | |
4792 else | |
4793 buf->writestring(" == "); | |
4794 tspec->toCBuffer(buf, NULL, hgs); | |
4795 } | |
336 | 4796 #if DMDV2 |
159 | 4797 if (parameters) |
4798 { // First parameter is already output, so start with second | |
4799 for (int i = 1; i < parameters->dim; i++) | |
4800 { | |
4801 buf->writeByte(','); | |
4802 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4803 tp->toCBuffer(buf, hgs); | |
4804 } | |
4805 } | |
4806 #endif | |
4807 buf->writeByte(')'); | |
4808 } | |
4809 | |
4810 | |
4811 /************************************************************/ | |
4812 | |
4813 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4814 : Expression(loc, op, size) | |
4815 { | |
4816 this->e1 = e1; | |
4817 } | |
4818 | |
4819 Expression *UnaExp::syntaxCopy() | |
4820 { UnaExp *e; | |
4821 | |
4822 e = (UnaExp *)copy(); | |
4823 e->type = NULL; | |
4824 e->e1 = e->e1->syntaxCopy(); | |
4825 return e; | |
4826 } | |
4827 | |
4828 Expression *UnaExp::semantic(Scope *sc) | |
4829 { | |
4830 #if LOGSEMANTIC | |
4831 printf("UnaExp::semantic('%s')\n", toChars()); | |
4832 #endif | |
4833 e1 = e1->semantic(sc); | |
4834 // if (!e1->type) | |
4835 // error("%s has no value", e1->toChars()); | |
4836 return this; | |
4837 } | |
4838 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4839 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4840 int UnaExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4841 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4842 return e1->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4843 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4844 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4845 |
159 | 4846 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4847 { | |
4848 buf->writestring(Token::toChars(op)); | |
4849 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4850 } | |
4851 | |
4852 /************************************************************/ | |
4853 | |
4854 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4855 : Expression(loc, op, size) | |
4856 { | |
4857 this->e1 = e1; | |
4858 this->e2 = e2; | |
4859 } | |
4860 | |
4861 Expression *BinExp::syntaxCopy() | |
4862 { BinExp *e; | |
4863 | |
4864 e = (BinExp *)copy(); | |
4865 e->type = NULL; | |
4866 e->e1 = e->e1->syntaxCopy(); | |
4867 e->e2 = e->e2->syntaxCopy(); | |
4868 return e; | |
4869 } | |
4870 | |
4871 Expression *BinExp::semantic(Scope *sc) | |
4872 { | |
4873 #if LOGSEMANTIC | |
4874 printf("BinExp::semantic('%s')\n", toChars()); | |
4875 #endif | |
4876 e1 = e1->semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4877 if (!e1->type && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4878 !(op == TOKassign && e1->op == TOKdottd)) // a.template = e2 |
159 | 4879 { |
4880 error("%s has no value", e1->toChars()); | |
4881 e1->type = Type::terror; | |
4882 } | |
4883 e2 = e2->semantic(sc); | |
4884 if (!e2->type) | |
4885 { | |
4886 error("%s has no value", e2->toChars()); | |
4887 e2->type = Type::terror; | |
4888 } | |
4889 return this; | |
4890 } | |
4891 | |
4892 Expression *BinExp::semanticp(Scope *sc) | |
4893 { | |
4894 BinExp::semantic(sc); | |
4895 e1 = resolveProperties(sc, e1); | |
4896 e2 = resolveProperties(sc, e2); | |
4897 return this; | |
4898 } | |
4899 | |
4900 /*************************** | |
4901 * Common semantic routine for some xxxAssignExp's. | |
4902 */ | |
4903 | |
4904 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4905 { Expression *e; | |
4906 | |
4907 if (!type) | |
4908 { | |
4909 BinExp::semantic(sc); | |
4910 e2 = resolveProperties(sc, e2); | |
4911 | |
4912 e = op_overload(sc); | |
4913 if (e) | |
4914 return e; | |
4915 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4916 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
|
4917 { // 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
|
4918 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
|
4919 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
|
4920 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
|
4921 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4922 |
159 | 4923 e1 = e1->modifiableLvalue(sc, e1); |
4924 e1->checkScalar(); | |
4925 type = e1->type; | |
4926 if (type->toBasetype()->ty == Tbool) | |
4927 { | |
4928 error("operator not allowed on bool expression %s", toChars()); | |
4929 } | |
4930 typeCombine(sc); | |
4931 e1->checkArithmetic(); | |
4932 e2->checkArithmetic(); | |
4933 | |
4934 if (op == TOKmodass && e2->type->iscomplex()) | |
4935 { error("cannot perform modulo complex arithmetic"); | |
4936 return new IntegerExp(0); | |
4937 } | |
4938 } | |
4939 return this; | |
4940 } | |
4941 | |
4942 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4943 { Expression *e; | |
4944 | |
4945 if (!type) | |
4946 { | |
4947 BinExp::semantic(sc); | |
4948 e2 = resolveProperties(sc, e2); | |
4949 | |
4950 e = op_overload(sc); | |
4951 if (e) | |
4952 return e; | |
4953 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4954 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
|
4955 { // 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
|
4956 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
|
4957 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
|
4958 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
|
4959 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4960 |
159 | 4961 e1 = e1->modifiableLvalue(sc, e1); |
4962 e1->checkScalar(); | |
4963 type = e1->type; | |
4964 if (type->toBasetype()->ty == Tbool) | |
4965 { | |
4966 e2 = e2->implicitCastTo(sc, type); | |
4967 } | |
4968 | |
4969 typeCombine(sc); | |
4970 e1->checkIntegral(); | |
4971 e2->checkIntegral(); | |
4972 } | |
4973 return this; | |
4974 } | |
4975 | |
4976 int BinExp::checkSideEffect(int flag) | |
4977 { | |
4978 if (op == TOKplusplus || | |
4979 op == TOKminusminus || | |
4980 op == TOKassign || | |
4981 op == TOKconstruct || | |
4982 op == TOKblit || | |
4983 op == TOKaddass || | |
4984 op == TOKminass || | |
4985 op == TOKcatass || | |
4986 op == TOKmulass || | |
4987 op == TOKdivass || | |
4988 op == TOKmodass || | |
4989 op == TOKshlass || | |
4990 op == TOKshrass || | |
4991 op == TOKushrass || | |
4992 op == TOKandass || | |
4993 op == TOKorass || | |
4994 op == TOKxorass || | |
4995 op == TOKin || | |
4996 op == TOKremove) | |
4997 return 1; | |
4998 return Expression::checkSideEffect(flag); | |
4999 } | |
5000 | |
5001 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5002 { | |
5003 expToCBuffer(buf, hgs, e1, precedence[op]); | |
5004 buf->writeByte(' '); | |
5005 buf->writestring(Token::toChars(op)); | |
5006 buf->writeByte(' '); | |
5007 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
5008 } | |
5009 | |
5010 int BinExp::isunsigned() | |
5011 { | |
5012 return e1->type->isunsigned() || e2->type->isunsigned(); | |
5013 } | |
5014 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5015 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5016 int BinExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5017 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5018 return e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5019 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5020 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5021 |
159 | 5022 void BinExp::incompatibleTypes() |
5023 { | |
5024 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
5025 e1->toChars(), Token::toChars(op), e2->toChars(), | |
5026 e1->type->toChars(), e2->type->toChars()); | |
5027 } | |
5028 | |
5029 /************************************************************/ | |
5030 | |
5031 CompileExp::CompileExp(Loc loc, Expression *e) | |
5032 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
5033 { | |
5034 } | |
5035 | |
5036 Expression *CompileExp::semantic(Scope *sc) | |
5037 { | |
5038 #if LOGSEMANTIC | |
5039 printf("CompileExp::semantic('%s')\n", toChars()); | |
5040 #endif | |
5041 UnaExp::semantic(sc); | |
5042 e1 = resolveProperties(sc, e1); | |
5043 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
5044 if (e1->op != TOKstring) | |
5045 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
5046 type = Type::terror; | |
5047 return this; | |
5048 } | |
5049 StringExp *se = (StringExp *)e1; | |
5050 se = se->toUTF8(sc); | |
5051 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
5052 p.loc = loc; | |
5053 p.nextToken(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5054 //printf("p.loc.linnum = %d\n", p.loc.linnum); |
159 | 5055 Expression *e = p.parseExpression(); |
5056 if (p.token.value != TOKeof) | |
5057 error("incomplete mixin expression (%s)", se->toChars()); | |
5058 return e->semantic(sc); | |
5059 } | |
5060 | |
5061 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5062 { | |
5063 buf->writestring("mixin("); | |
5064 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5065 buf->writeByte(')'); | |
5066 } | |
5067 | |
5068 /************************************************************/ | |
5069 | |
5070 FileExp::FileExp(Loc loc, Expression *e) | |
5071 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
5072 { | |
5073 } | |
5074 | |
5075 Expression *FileExp::semantic(Scope *sc) | |
5076 { char *name; | |
5077 StringExp *se; | |
5078 | |
5079 #if LOGSEMANTIC | |
5080 printf("FileExp::semantic('%s')\n", toChars()); | |
5081 #endif | |
5082 UnaExp::semantic(sc); | |
5083 e1 = resolveProperties(sc, e1); | |
5084 e1 = e1->optimize(WANTvalue); | |
5085 if (e1->op != TOKstring) | |
5086 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
5087 goto Lerror; | |
5088 } | |
5089 se = (StringExp *)e1; | |
5090 se = se->toUTF8(sc); | |
5091 name = (char *)se->string; | |
5092 | |
5093 if (!global.params.fileImppath) | |
5094 { error("need -Jpath switch to import text file %s", name); | |
5095 goto Lerror; | |
5096 } | |
5097 | |
5098 if (name != FileName::name(name)) | |
5099 { error("use -Jpath switch to provide path for filename %s", name); | |
5100 goto Lerror; | |
5101 } | |
5102 | |
5103 name = FileName::searchPath(global.filePath, name, 0); | |
5104 if (!name) | |
5105 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
5106 goto Lerror; | |
5107 } | |
5108 | |
5109 if (global.params.verbose) | |
794
661384d6a936
Fix warnings on x86-64. By fvbommel.
Christian Kamm <kamm incasoftware de>
parents:
779
diff
changeset
|
5110 printf("file %s\t(%s)\n", (char*)se->string, name); |
159 | 5111 |
5112 { File f(name); | |
5113 if (f.read()) | |
5114 { error("cannot read file %s", f.toChars()); | |
5115 goto Lerror; | |
5116 } | |
5117 else | |
5118 { | |
5119 f.ref = 1; | |
5120 se = new StringExp(loc, f.buffer, f.len); | |
5121 } | |
5122 } | |
5123 Lret: | |
5124 return se->semantic(sc); | |
5125 | |
5126 Lerror: | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5127 se = new StringExp(loc, (char *)""); |
159 | 5128 goto Lret; |
5129 } | |
5130 | |
5131 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5132 { | |
5133 buf->writestring("import("); | |
5134 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5135 buf->writeByte(')'); | |
5136 } | |
5137 | |
5138 /************************************************************/ | |
5139 | |
5140 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
5141 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
5142 { | |
5143 this->msg = msg; | |
5144 } | |
5145 | |
5146 Expression *AssertExp::syntaxCopy() | |
5147 { | |
5148 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
5149 msg ? msg->syntaxCopy() : NULL); | |
5150 return ae; | |
5151 } | |
5152 | |
5153 Expression *AssertExp::semantic(Scope *sc) | |
5154 { | |
5155 #if LOGSEMANTIC | |
5156 printf("AssertExp::semantic('%s')\n", toChars()); | |
5157 #endif | |
5158 UnaExp::semantic(sc); | |
5159 e1 = resolveProperties(sc, e1); | |
5160 // BUG: see if we can do compile time elimination of the Assert | |
5161 e1 = e1->optimize(WANTvalue); | |
5162 e1 = e1->checkToBoolean(); | |
5163 if (msg) | |
5164 { | |
5165 msg = msg->semantic(sc); | |
5166 msg = resolveProperties(sc, msg); | |
5167 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
5168 msg = msg->optimize(WANTvalue); | |
5169 } | |
5170 if (e1->isBool(FALSE)) | |
5171 { | |
5172 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
5173 fd->hasReturnExp |= 4; | |
5174 | |
5175 if (!global.params.useAssert) | |
5176 { Expression *e = new HaltExp(loc); | |
5177 e = e->semantic(sc); | |
5178 return e; | |
5179 } | |
5180 } | |
5181 type = Type::tvoid; | |
5182 return this; | |
5183 } | |
5184 | |
5185 int AssertExp::checkSideEffect(int flag) | |
5186 { | |
5187 return 1; | |
5188 } | |
5189 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5190 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5191 int AssertExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5192 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5193 return (global.params.useAssert != 0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5194 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5195 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5196 |
159 | 5197 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
5198 { | |
5199 buf->writestring("assert("); | |
5200 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5201 if (msg) | |
5202 { | |
5203 buf->writeByte(','); | |
5204 expToCBuffer(buf, hgs, msg, PREC_assign); | |
5205 } | |
5206 buf->writeByte(')'); | |
5207 } | |
5208 | |
5209 /************************************************************/ | |
5210 | |
5211 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
5212 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
5213 { | |
5214 this->ident = ident; | |
5215 } | |
5216 | |
5217 Expression *DotIdExp::semantic(Scope *sc) | |
5218 { Expression *e; | |
5219 Expression *eleft; | |
5220 Expression *eright; | |
5221 | |
5222 #if LOGSEMANTIC | |
5223 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
5224 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
5225 #endif | |
5226 | |
5227 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
5228 | |
5229 #if 0 | |
5230 /* Don't do semantic analysis if we'll be converting | |
5231 * it to a string. | |
5232 */ | |
5233 if (ident == Id::stringof) | |
5234 { char *s = e1->toChars(); | |
5235 e = new StringExp(loc, s, strlen(s), 'c'); | |
5236 e = e->semantic(sc); | |
5237 return e; | |
5238 } | |
5239 #endif | |
5240 | |
5241 /* Special case: rewrite this.id and super.id | |
5242 * to be classtype.id and baseclasstype.id | |
5243 * if we have no this pointer. | |
5244 */ | |
5245 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
5246 { ClassDeclaration *cd; | |
5247 StructDeclaration *sd; | |
5248 AggregateDeclaration *ad; | |
5249 | |
5250 ad = sc->getStructClassScope(); | |
5251 if (ad) | |
5252 { | |
5253 cd = ad->isClassDeclaration(); | |
5254 if (cd) | |
5255 { | |
5256 if (e1->op == TOKthis) | |
5257 { | |
5258 e = new TypeDotIdExp(loc, cd->type, ident); | |
5259 return e->semantic(sc); | |
5260 } | |
5261 else if (cd->baseClass && e1->op == TOKsuper) | |
5262 { | |
5263 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
5264 return e->semantic(sc); | |
5265 } | |
5266 } | |
5267 else | |
5268 { | |
5269 sd = ad->isStructDeclaration(); | |
5270 if (sd) | |
5271 { | |
5272 if (e1->op == TOKthis) | |
5273 { | |
5274 e = new TypeDotIdExp(loc, sd->type, ident); | |
5275 return e->semantic(sc); | |
5276 } | |
5277 } | |
5278 } | |
5279 } | |
5280 } | |
5281 | |
5282 UnaExp::semantic(sc); | |
5283 | |
5284 if (e1->op == TOKdotexp) | |
5285 { | |
5286 DotExp *de = (DotExp *)e1; | |
5287 eleft = de->e1; | |
5288 eright = de->e2; | |
5289 } | |
5290 else | |
5291 { | |
5292 e1 = resolveProperties(sc, e1); | |
5293 eleft = NULL; | |
5294 eright = e1; | |
5295 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5296 #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
|
5297 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
|
5298 { /* '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
|
5299 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5300 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
|
5301 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
|
5302 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
|
5303 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
|
5304 { 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
|
5305 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
|
5306 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
|
5307 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
|
5308 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5309 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
|
5310 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
|
5311 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
|
5312 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5313 #endif |
159 | 5314 |
5315 if (e1->op == TOKtuple && ident == Id::length) | |
5316 { | |
5317 TupleExp *te = (TupleExp *)e1; | |
5318 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5319 return e; | |
5320 } | |
5321 | |
5322 if (eright->op == TOKimport) // also used for template alias's | |
5323 { | |
5324 ScopeExp *ie = (ScopeExp *)eright; | |
5325 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5326 /* 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
|
5327 * 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
|
5328 * 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
|
5329 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5330 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
|
5331 (ie->sds->isModule() && ie->sds != sc->module) ? 1 : 0); |
159 | 5332 if (s) |
5333 { | |
5334 s = s->toAlias(); | |
5335 checkDeprecated(sc, s); | |
5336 | |
5337 EnumMember *em = s->isEnumMember(); | |
5338 if (em) | |
5339 { | |
5340 e = em->value; | |
5341 e = e->semantic(sc); | |
5342 return e; | |
5343 } | |
5344 | |
5345 VarDeclaration *v = s->isVarDeclaration(); | |
5346 if (v) | |
5347 { | |
5348 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5349 if (v->inuse) | |
5350 { | |
5351 error("circular reference to '%s'", v->toChars()); | |
5352 type = Type::tint32; | |
5353 return this; | |
5354 } | |
5355 type = v->type; | |
5356 if (v->isConst()) | |
5357 { | |
5358 if (v->init) | |
5359 { | |
5360 ExpInitializer *ei = v->init->isExpInitializer(); | |
5361 if (ei) | |
5362 { | |
5363 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5364 //ei->exp = ei->exp->semantic(sc); | |
5365 if (ei->exp->type == type) | |
5366 { | |
5367 e = ei->exp->copy(); // make copy so we can change loc | |
5368 e->loc = loc; | |
5369 return e; | |
5370 } | |
5371 } | |
5372 } | |
5373 else if (type->isscalar()) | |
5374 { | |
5375 e = type->defaultInit(); | |
5376 e->loc = loc; | |
5377 return e; | |
5378 } | |
5379 } | |
5380 if (v->needThis()) | |
5381 { | |
5382 if (!eleft) | |
5383 eleft = new ThisExp(loc); | |
5384 e = new DotVarExp(loc, eleft, v); | |
5385 e = e->semantic(sc); | |
5386 } | |
5387 else | |
5388 { | |
5389 e = new VarExp(loc, v); | |
5390 if (eleft) | |
5391 { e = new CommaExp(loc, eleft, e); | |
5392 e->type = v->type; | |
5393 } | |
5394 } | |
5395 return e->deref(); | |
5396 } | |
5397 | |
5398 FuncDeclaration *f = s->isFuncDeclaration(); | |
5399 if (f) | |
5400 { | |
5401 //printf("it's a function\n"); | |
5402 if (f->needThis()) | |
5403 { | |
5404 if (!eleft) | |
5405 eleft = new ThisExp(loc); | |
5406 e = new DotVarExp(loc, eleft, f); | |
5407 e = e->semantic(sc); | |
5408 } | |
5409 else | |
5410 { | |
5411 e = new VarExp(loc, f); | |
5412 if (eleft) | |
5413 { e = new CommaExp(loc, eleft, e); | |
5414 e->type = f->type; | |
5415 } | |
5416 } | |
5417 return e; | |
5418 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5419 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5420 OverloadSet *o = s->isOverloadSet(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5421 if (o) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5422 { //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
|
5423 return new OverExp(o); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5424 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5425 #endif |
159 | 5426 |
5427 Type *t = s->getType(); | |
5428 if (t) | |
5429 { | |
5430 return new TypeExp(loc, t); | |
5431 } | |
5432 | |
336 | 5433 TupleDeclaration *tup = s->isTupleDeclaration(); |
5434 if (tup) | |
5435 { | |
5436 if (eleft) | |
5437 error("cannot have e.tuple"); | |
5438 e = new TupleExp(loc, tup); | |
5439 e = e->semantic(sc); | |
5440 return e; | |
5441 } | |
5442 | |
159 | 5443 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5444 if (sds) | |
5445 { | |
5446 //printf("it's a ScopeDsymbol\n"); | |
5447 e = new ScopeExp(loc, sds); | |
5448 e = e->semantic(sc); | |
5449 if (eleft) | |
5450 e = new DotExp(loc, eleft, e); | |
5451 return e; | |
5452 } | |
5453 | |
5454 Import *imp = s->isImport(); | |
5455 if (imp) | |
5456 { | |
5457 ScopeExp *ie; | |
5458 | |
5459 ie = new ScopeExp(loc, imp->pkg); | |
5460 return ie->semantic(sc); | |
5461 } | |
5462 | |
5463 // BUG: handle other cases like in IdentifierExp::semantic() | |
5464 #ifdef DEBUG | |
5465 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5466 #endif | |
5467 assert(0); | |
5468 } | |
5469 else if (ident == Id::stringof) | |
5470 { char *s = ie->toChars(); | |
5471 e = new StringExp(loc, s, strlen(s), 'c'); | |
5472 e = e->semantic(sc); | |
5473 return e; | |
5474 } | |
5475 error("undefined identifier %s", toChars()); | |
5476 type = Type::tvoid; | |
5477 return this; | |
5478 } | |
5479 else if (e1->type->ty == Tpointer && | |
5480 ident != Id::init && ident != Id::__sizeof && | |
5481 ident != Id::alignof && ident != Id::offsetof && | |
5482 ident != Id::mangleof && ident != Id::stringof) | |
5483 { | |
5484 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
|
5485 e->type = ((TypePointer *)e1->type)->next; |
159 | 5486 return e->type->dotExp(sc, e, ident); |
5487 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5488 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5489 else if (t1b->ty == Tarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5490 t1b->ty == Tsarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5491 t1b->ty == Taarray) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5492 { /* 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
|
5493 * e1.ident |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5494 * as: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5495 * .ident(e1) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5496 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5497 unsigned errors = global.errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5498 global.gag++; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5499 e = e1->type->dotExp(sc, e1, ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5500 global.gag--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5501 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
|
5502 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5503 global.errors = errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5504 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
|
5505 e = new CallExp(loc, e, e1); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5506 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5507 e = e->semantic(sc); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5508 return e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5509 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5510 #endif |
159 | 5511 else |
5512 { | |
5513 e = e1->type->dotExp(sc, e1, ident); | |
5514 e = e->semantic(sc); | |
5515 return e; | |
5516 } | |
5517 } | |
5518 | |
5519 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5520 { | |
5521 //printf("DotIdExp::toCBuffer()\n"); | |
5522 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5523 buf->writeByte('.'); | |
5524 buf->writestring(ident->toChars()); | |
5525 } | |
5526 | |
5527 /********************** DotTemplateExp ***********************************/ | |
5528 | |
5529 // Mainly just a placeholder | |
5530 | |
5531 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5532 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5533 | |
5534 { | |
5535 this->td = td; | |
5536 } | |
5537 | |
5538 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5539 { | |
5540 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5541 buf->writeByte('.'); | |
5542 buf->writestring(td->toChars()); | |
5543 } | |
5544 | |
5545 | |
5546 /************************************************************/ | |
5547 | |
5548 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5549 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5550 { | |
5551 //printf("DotVarExp()\n"); | |
5552 this->var = v; | |
5553 } | |
5554 | |
5555 Expression *DotVarExp::semantic(Scope *sc) | |
5556 { | |
5557 #if LOGSEMANTIC | |
5558 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5559 #endif | |
5560 if (!type) | |
5561 { | |
5562 var = var->toAlias()->isDeclaration(); | |
5563 | |
5564 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5565 if (tup) | |
5566 { /* Replace: | |
5567 * e1.tuple(a, b, c) | |
5568 * with: | |
5569 * tuple(e1.a, e1.b, e1.c) | |
5570 */ | |
5571 Expressions *exps = new Expressions; | |
5572 | |
5573 exps->reserve(tup->objects->dim); | |
5574 for (size_t i = 0; i < tup->objects->dim; i++) | |
5575 { Object *o = (Object *)tup->objects->data[i]; | |
5576 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5577 { | |
5578 error("%s is not an expression", o->toChars()); | |
5579 } | |
5580 else | |
5581 { | |
5582 Expression *e = (Expression *)o; | |
5583 if (e->op != TOKdsymbol) | |
5584 error("%s is not a member", e->toChars()); | |
5585 else | |
5586 { DsymbolExp *ve = (DsymbolExp *)e; | |
5587 | |
5588 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5589 exps->push(e); | |
5590 } | |
5591 } | |
5592 } | |
5593 Expression *e = new TupleExp(loc, exps); | |
5594 e = e->semantic(sc); | |
5595 return e; | |
5596 } | |
5597 | |
5598 e1 = e1->semantic(sc); | |
5599 type = var->type; | |
5600 if (!type && global.errors) | |
5601 { // var is goofed up, just return 0 | |
5602 return new IntegerExp(0); | |
5603 } | |
5604 assert(type); | |
5605 | |
5606 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5607 { | |
5608 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
336 | 5609 e1 = getRightThis(loc, sc, ad, e1, var); |
5610 if (!sc->noaccesscheck) | |
5611 accessCheck(loc, sc, e1, var); | |
5612 | |
5613 VarDeclaration *v = var->isVarDeclaration(); | |
5614 if (v && v->isConst()) | |
5615 { ExpInitializer *ei = v->getExpInitializer(); | |
5616 if (ei) | |
5617 { Expression *e = ei->exp->copy(); | |
5618 e = e->semantic(sc); | |
5619 return e; | |
159 | 5620 } |
5621 } | |
5622 } | |
5623 } | |
5624 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5625 return this; | |
5626 } | |
5627 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5628 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5629 int DotVarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5630 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5631 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5632 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5633 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5634 |
159 | 5635 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) |
5636 { | |
5637 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5638 return this; | |
5639 } | |
5640 | |
5641 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5642 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5643 #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
|
5644 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
|
5645 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
|
5646 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
|
5647 #endif |
159 | 5648 |
5649 if (var->isCtorinit()) | |
5650 { // It's only modifiable if inside the right constructor | |
5651 Dsymbol *s = sc->func; | |
5652 while (1) | |
5653 { | |
5654 FuncDeclaration *fd = NULL; | |
5655 if (s) | |
5656 fd = s->isFuncDeclaration(); | |
5657 if (fd && | |
5658 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5659 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5660 fd->toParent() == var->toParent() && | |
5661 e1->op == TOKthis | |
5662 ) | |
5663 { | |
5664 VarDeclaration *v = var->isVarDeclaration(); | |
5665 assert(v); | |
5666 v->ctorinit = 1; | |
5667 //printf("setting ctorinit\n"); | |
5668 } | |
5669 else | |
5670 { | |
5671 if (s) | |
5672 { s = s->toParent2(); | |
5673 continue; | |
5674 } | |
5675 else | |
5676 { | |
5677 const char *p = var->isStatic() ? "static " : ""; | |
5678 error("can only initialize %sconst member %s inside %sconstructor", | |
5679 p, var->toChars(), p); | |
5680 } | |
5681 } | |
5682 break; | |
5683 } | |
5684 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5685 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5686 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5687 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5688 Type *t1 = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5689 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5690 if (!t1->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5691 (t1->ty == Tpointer && !t1->nextOf()->isMutable()) || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5692 !var->type->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5693 !var->type->isAssignable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5694 var->storage_class & STCmanifest |
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 error("cannot modify const/invariant %s", toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5697 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5698 #endif |
159 | 5699 return this; |
5700 } | |
5701 | |
5702 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5703 { | |
5704 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5705 buf->writeByte('.'); | |
5706 buf->writestring(var->toChars()); | |
5707 } | |
5708 | |
5709 /************************************************************/ | |
5710 | |
5711 /* Things like: | |
5712 * foo.bar!(args) | |
5713 */ | |
5714 | |
5715 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5716 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5717 { | |
5718 //printf("DotTemplateInstanceExp()\n"); | |
5719 this->ti = ti; | |
5720 } | |
5721 | |
5722 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5723 { | |
5724 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5725 e1->syntaxCopy(), | |
5726 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5727 return de; | |
5728 } | |
5729 | |
5730 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5731 { Dsymbol *s; | |
5732 Dsymbol *s2; | |
5733 TemplateDeclaration *td; | |
5734 Expression *e; | |
5735 Identifier *id; | |
5736 Type *t1; | |
5737 Expression *eleft = NULL; | |
5738 Expression *eright; | |
5739 | |
5740 #if LOGSEMANTIC | |
5741 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5742 #endif | |
5743 //e1->print(); | |
5744 //print(); | |
5745 e1 = e1->semantic(sc); | |
5746 t1 = e1->type; | |
5747 if (t1) | |
5748 t1 = t1->toBasetype(); | |
5749 //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
|
5750 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5751 /* 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
|
5752 * 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
|
5753 * 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
|
5754 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5755 |
159 | 5756 if (e1->op == TOKdotexp) |
5757 { DotExp *de = (DotExp *)e1; | |
5758 eleft = de->e1; | |
5759 eright = de->e2; | |
5760 } | |
5761 else | |
5762 { eleft = NULL; | |
5763 eright = e1; | |
5764 } | |
5765 if (eright->op == TOKimport) | |
5766 { | |
5767 s = ((ScopeExp *)eright)->sds; | |
5768 } | |
5769 else if (e1->op == TOKtype) | |
5770 { | |
5771 s = t1->isClassHandle(); | |
5772 if (!s) | |
5773 { if (t1->ty == Tstruct) | |
5774 s = ((TypeStruct *)t1)->sym; | |
5775 else | |
5776 goto L1; | |
5777 } | |
5778 } | |
5779 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5780 { | |
5781 s = t1->toDsymbol(sc); | |
5782 eleft = e1; | |
5783 } | |
5784 else if (t1 && t1->ty == Tpointer) | |
5785 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5786 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 5787 if (t1->ty != Tstruct) |
5788 goto L1; | |
5789 s = t1->toDsymbol(sc); | |
5790 eleft = e1; | |
5791 } | |
5792 else | |
5793 { | |
5794 L1: | |
5795 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5796 goto Lerr; | |
5797 } | |
5798 | |
5799 assert(s); | |
5800 id = ti->name; | |
5801 s2 = s->search(loc, id, 0); | |
5802 if (!s2) | |
5803 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5804 goto Lerr; | |
5805 } | |
5806 s = s2; | |
5807 s->semantic(sc); | |
5808 s = s->toAlias(); | |
5809 td = s->isTemplateDeclaration(); | |
5810 if (!td) | |
5811 { | |
5812 error("%s is not a template", id->toChars()); | |
5813 goto Lerr; | |
5814 } | |
5815 if (global.errors) | |
5816 goto Lerr; | |
5817 | |
5818 ti->tempdecl = td; | |
5819 | |
5820 if (eleft) | |
5821 { Declaration *v; | |
5822 | |
5823 ti->semantic(sc); | |
5824 s = ti->inst->toAlias(); | |
5825 v = s->isDeclaration(); | |
5826 if (v) | |
5827 { e = new DotVarExp(loc, eleft, v); | |
5828 e = e->semantic(sc); | |
5829 return e; | |
5830 } | |
5831 } | |
5832 | |
5833 e = new ScopeExp(loc, ti); | |
5834 if (eleft) | |
5835 { | |
5836 e = new DotExp(loc, eleft, e); | |
5837 } | |
5838 e = e->semantic(sc); | |
5839 return e; | |
5840 | |
5841 Lerr: | |
336 | 5842 return new IntegerExp(loc, 0, Type::tint32); |
159 | 5843 } |
5844 | |
5845 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5846 { | |
5847 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5848 buf->writeByte('.'); | |
5849 ti->toCBuffer(buf, hgs); | |
5850 } | |
5851 | |
5852 /************************************************************/ | |
5853 | |
5854 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5855 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5856 { | |
5857 this->func = f; | |
5858 } | |
5859 | |
5860 Expression *DelegateExp::semantic(Scope *sc) | |
5861 { | |
5862 #if LOGSEMANTIC | |
5863 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5864 #endif | |
5865 if (!type) | |
5866 { | |
5867 e1 = e1->semantic(sc); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
5868 // 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
|
5869 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 5870 type = type->semantic(loc, sc); |
5871 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 5872 if (func->needThis()) |
5873 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 5874 } |
5875 return this; | |
5876 } | |
5877 | |
5878 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5879 { | |
5880 buf->writeByte('&'); | |
5881 if (!func->isNested()) | |
5882 { | |
5883 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5884 buf->writeByte('.'); | |
5885 } | |
5886 buf->writestring(func->toChars()); | |
5887 } | |
5888 | |
5889 /************************************************************/ | |
5890 | |
5891 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5892 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5893 { | |
5894 this->sym = s; | |
5895 this->type = s->getType(); | |
5896 } | |
5897 | |
5898 Expression *DotTypeExp::semantic(Scope *sc) | |
5899 { | |
5900 #if LOGSEMANTIC | |
5901 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5902 #endif | |
5903 UnaExp::semantic(sc); | |
5904 return this; | |
5905 } | |
5906 | |
5907 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5908 { | |
5909 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5910 buf->writeByte('.'); | |
5911 buf->writestring(sym->toChars()); | |
5912 } | |
5913 | |
5914 /************************************************************/ | |
5915 | |
5916 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5917 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5918 { | |
5919 this->arguments = exps; | |
5920 } | |
5921 | |
5922 CallExp::CallExp(Loc loc, Expression *e) | |
5923 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5924 { | |
5925 this->arguments = NULL; | |
5926 } | |
5927 | |
5928 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5929 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5930 { | |
5931 Expressions *arguments = new Expressions(); | |
5932 arguments->setDim(1); | |
5933 arguments->data[0] = (void *)earg1; | |
5934 | |
5935 this->arguments = arguments; | |
5936 } | |
5937 | |
5938 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5939 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5940 { | |
5941 Expressions *arguments = new Expressions(); | |
5942 arguments->setDim(2); | |
5943 arguments->data[0] = (void *)earg1; | |
5944 arguments->data[1] = (void *)earg2; | |
5945 | |
5946 this->arguments = arguments; | |
5947 } | |
5948 | |
5949 Expression *CallExp::syntaxCopy() | |
5950 { | |
5951 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5952 } | |
5953 | |
5954 | |
5955 Expression *CallExp::semantic(Scope *sc) | |
5956 { | |
5957 TypeFunction *tf; | |
5958 FuncDeclaration *f; | |
5959 int i; | |
5960 Type *t1; | |
5961 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
|
5962 Objects *targsi = NULL; // initial list of template arguments |
159 | 5963 |
5964 #if LOGSEMANTIC | |
5965 printf("CallExp::semantic() %s\n", toChars()); | |
5966 #endif | |
5967 if (type) | |
5968 return this; // semantic() already run | |
5969 #if 0 | |
5970 if (arguments && arguments->dim) | |
5971 { | |
5972 Expression *earg = (Expression *)arguments->data[0]; | |
5973 earg->print(); | |
5974 if (earg->type) earg->type->print(); | |
5975 } | |
5976 #endif | |
5977 | |
5978 if (e1->op == TOKdelegate) | |
5979 { DelegateExp *de = (DelegateExp *)e1; | |
5980 | |
5981 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5982 return semantic(sc); | |
5983 } | |
5984 | |
5985 /* Transform: | |
5986 * array.id(args) into id(array,args) | |
5987 * aa.remove(arg) into delete aa[arg] | |
5988 */ | |
5989 if (e1->op == TOKdot) | |
5990 { | |
5991 // BUG: we should handle array.a.b.c.e(args) too | |
5992 | |
5993 DotIdExp *dotid = (DotIdExp *)(e1); | |
5994 dotid->e1 = dotid->e1->semantic(sc); | |
5995 assert(dotid->e1); | |
5996 if (dotid->e1->type) | |
5997 { | |
5998 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
5999 if (e1ty == Taarray && dotid->ident == Id::remove) | |
6000 { | |
6001 if (!arguments || arguments->dim != 1) | |
6002 { error("expected key as argument to aa.remove()"); | |
6003 goto Lagain; | |
6004 } | |
6005 Expression *key = (Expression *)arguments->data[0]; | |
6006 key = key->semantic(sc); | |
6007 key = resolveProperties(sc, key); | |
6008 key->rvalue(); | |
6009 | |
6010 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
6011 key = key->implicitCastTo(sc, taa->index); | |
6012 key = key->implicitCastTo(sc, taa->key); | |
6013 | |
6014 return new RemoveExp(loc, dotid->e1, key); | |
6015 } | |
6016 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
6017 { | |
6018 if (!arguments) | |
6019 arguments = new Expressions(); | |
6020 arguments->shift(dotid->e1); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6021 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6022 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
|
6023 #else |
159 | 6024 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
|
6025 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6026 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6027 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6028 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6029 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6030 #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
|
6031 /* 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
|
6032 * 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
|
6033 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6034 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
|
6035 { 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
|
6036 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
|
6037 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
|
6038 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6039 /* 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
|
6040 * 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
|
6041 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6042 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
|
6043 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
|
6044 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
|
6045 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
|
6046 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
|
6047 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
|
6048 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6049 /* 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
|
6050 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6051 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
|
6052 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
|
6053 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
|
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 } |
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6058 /* 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
|
6059 * 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
|
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 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
|
6062 { 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
|
6063 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
|
6064 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
|
6065 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6066 /* 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
|
6067 * 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
|
6068 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6069 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
|
6070 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
|
6071 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
|
6072 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
|
6073 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
|
6074 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
|
6075 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
|
6076 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6077 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
|
6078 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
|
6079 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
|
6080 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6081 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6082 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
|
6083 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6084 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6085 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6086 |
159 | 6087 istemp = 0; |
6088 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
|
6089 //printf("Lagain: %s\n", toChars()); |
159 | 6090 f = NULL; |
6091 if (e1->op == TOKthis || e1->op == TOKsuper) | |
6092 { | |
6093 // semantic() run later for these | |
6094 } | |
6095 else | |
6096 { | |
6097 UnaExp::semantic(sc); | |
6098 | |
6099 /* Look for e1 being a lazy parameter | |
6100 */ | |
6101 if (e1->op == TOKvar) | |
6102 { VarExp *ve = (VarExp *)e1; | |
6103 | |
6104 if (ve->var->storage_class & STClazy) | |
6105 { | |
6106 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
6107 TypeDelegate *t = new TypeDelegate(tf); | |
6108 ve->type = t->semantic(loc, sc); | |
6109 } | |
6110 } | |
6111 | |
6112 if (e1->op == TOKimport) | |
6113 { // Perhaps this should be moved to ScopeExp::semantic() | |
6114 ScopeExp *se = (ScopeExp *)e1; | |
6115 e1 = new DsymbolExp(loc, se->sds); | |
6116 e1 = e1->semantic(sc); | |
6117 } | |
6118 #if 1 // patch for #540 by Oskar Linde | |
6119 else if (e1->op == TOKdotexp) | |
6120 { | |
6121 DotExp *de = (DotExp *) e1; | |
6122 | |
6123 if (de->e2->op == TOKimport) | |
6124 { // This should *really* be moved to ScopeExp::semantic() | |
6125 ScopeExp *se = (ScopeExp *)de->e2; | |
6126 de->e2 = new DsymbolExp(loc, se->sds); | |
6127 de->e2 = de->e2->semantic(sc); | |
6128 } | |
6129 | |
6130 if (de->e2->op == TOKtemplate) | |
6131 { TemplateExp *te = (TemplateExp *) de->e2; | |
6132 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
6133 } | |
6134 } | |
6135 #endif | |
6136 } | |
6137 | |
6138 if (e1->op == TOKcomma) | |
6139 { | |
6140 CommaExp *ce = (CommaExp *)e1; | |
6141 | |
6142 e1 = ce->e2; | |
6143 e1->type = ce->type; | |
6144 ce->e2 = this; | |
6145 ce->type = NULL; | |
6146 return ce->semantic(sc); | |
6147 } | |
6148 | |
6149 t1 = NULL; | |
6150 if (e1->type) | |
6151 t1 = e1->type->toBasetype(); | |
6152 | |
6153 // Check for call operator overload | |
6154 if (t1) | |
6155 { AggregateDeclaration *ad; | |
6156 | |
6157 if (t1->ty == Tstruct) | |
6158 { | |
6159 ad = ((TypeStruct *)t1)->sym; | |
6160 if (search_function(ad, Id::call)) | |
6161 goto L1; // overload of opCall, therefore it's a call | |
6162 | |
6163 if (e1->op != TOKtype) | |
6164 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
6165 /* It's a struct literal | |
6166 */ | |
6167 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
6168 e = e->semantic(sc); | |
6169 e->type = e1->type; // in case e1->type was a typedef | |
6170 return e; | |
6171 } | |
6172 else if (t1->ty == Tclass) | |
6173 { | |
6174 ad = ((TypeClass *)t1)->sym; | |
6175 goto L1; | |
6176 L1: | |
6177 // Rewrite as e1.call(arguments) | |
6178 Expression *e = new DotIdExp(loc, e1, Id::call); | |
6179 e = new CallExp(loc, e, arguments); | |
6180 e = e->semantic(sc); | |
6181 return e; | |
6182 } | |
6183 } | |
6184 | |
6185 arrayExpressionSemantic(arguments, sc); | |
6186 preFunctionArguments(loc, sc, arguments); | |
6187 | |
6188 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
6189 e1->op == TOKdottd) | |
6190 { | |
6191 DotVarExp *dve; | |
6192 DotTemplateExp *dte; | |
6193 AggregateDeclaration *ad; | |
6194 UnaExp *ue = (UnaExp *)(e1); | |
336 | 6195 |
159 | 6196 if (e1->op == TOKdotvar) |
6197 { // Do overload resolution | |
6198 dve = (DotVarExp *)(e1); | |
6199 | |
6200 f = dve->var->isFuncDeclaration(); | |
6201 assert(f); | |
6202 f = f->overloadResolve(loc, arguments); | |
6203 | |
6204 ad = f->toParent()->isAggregateDeclaration(); | |
6205 } | |
6206 else | |
6207 { dte = (DotTemplateExp *)(e1); | |
6208 TemplateDeclaration *td = dte->td; | |
6209 assert(td); | |
6210 if (!arguments) | |
6211 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
6212 arguments = new Expressions(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6213 f = td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6214 if (!f) |
6215 { type = Type::terror; | |
6216 return this; | |
6217 } | |
6218 ad = td->toParent()->isAggregateDeclaration(); | |
6219 } | |
336 | 6220 if (f->needThis()) |
6221 { | |
6222 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
6223 } | |
6224 | |
6225 /* Cannot call public functions from inside invariant | |
6226 * (because then the invariant would have infinite recursion) | |
159 | 6227 */ |
336 | 6228 if (sc->func && sc->func->isInvariantDeclaration() && |
6229 ue->e1->op == TOKthis && | |
6230 f->addPostInvariant() | |
159 | 6231 ) |
6232 { | |
336 | 6233 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 6234 } |
6235 | |
6236 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6237 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6238 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6239 #endif |
159 | 6240 accessCheck(loc, sc, ue->e1, f); |
6241 if (!f->needThis()) | |
6242 { | |
6243 VarExp *ve = new VarExp(loc, f); | |
6244 e1 = new CommaExp(loc, ue->e1, ve); | |
6245 e1->type = f->type; | |
6246 } | |
6247 else | |
6248 { | |
6249 if (e1->op == TOKdotvar) | |
6250 dve->var = f; | |
6251 else | |
6252 e1 = new DotVarExp(loc, dte->e1, f); | |
6253 e1->type = f->type; | |
6254 | |
6255 // See if we need to adjust the 'this' pointer | |
6256 AggregateDeclaration *ad = f->isThis(); | |
6257 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
6258 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
6259 ue->e1->op != TOKsuper) | |
6260 { | |
6261 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
6262 ue->e1 = ue->e1->semantic(sc); | |
6263 } | |
6264 } | |
6265 t1 = e1->type; | |
6266 } | |
6267 else if (e1->op == TOKsuper) | |
6268 { | |
6269 // Base class constructor call | |
6270 ClassDeclaration *cd = NULL; | |
6271 | |
6272 if (sc->func) | |
6273 cd = sc->func->toParent()->isClassDeclaration(); | |
6274 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
6275 { | |
6276 error("super class constructor call must be in a constructor"); | |
6277 type = Type::terror; | |
6278 return this; | |
6279 } | |
6280 else | |
6281 { | |
6282 f = cd->baseClass->ctor; | |
6283 if (!f) | |
6284 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
6285 type = Type::terror; | |
6286 return this; | |
6287 } | |
6288 else | |
6289 { | |
336 | 6290 if (!sc->intypeof) |
6291 { | |
159 | 6292 #if 0 |
336 | 6293 if (sc->callSuper & (CSXthis | CSXsuper)) |
6294 error("reference to this before super()"); | |
159 | 6295 #endif |
336 | 6296 if (sc->noctor || sc->callSuper & CSXlabel) |
6297 error("constructor calls not allowed in loops or after labels"); | |
6298 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6299 error("multiple constructor calls"); | |
6300 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
6301 } | |
159 | 6302 |
6303 f = f->overloadResolve(loc, arguments); | |
6304 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6305 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6306 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6307 #endif |
159 | 6308 e1 = new DotVarExp(e1->loc, e1, f); |
6309 e1 = e1->semantic(sc); | |
6310 t1 = e1->type; | |
6311 } | |
6312 } | |
6313 } | |
6314 else if (e1->op == TOKthis) | |
6315 { | |
6316 // same class constructor call | |
6317 ClassDeclaration *cd = NULL; | |
6318 | |
6319 if (sc->func) | |
6320 cd = sc->func->toParent()->isClassDeclaration(); | |
6321 if (!cd || !sc->func->isCtorDeclaration()) | |
6322 { | |
6323 error("class constructor call must be in a constructor"); | |
6324 type = Type::terror; | |
6325 return this; | |
6326 } | |
6327 else | |
6328 { | |
336 | 6329 if (!sc->intypeof) |
6330 { | |
159 | 6331 #if 0 |
336 | 6332 if (sc->callSuper & (CSXthis | CSXsuper)) |
6333 error("reference to this before super()"); | |
159 | 6334 #endif |
336 | 6335 if (sc->noctor || sc->callSuper & CSXlabel) |
6336 error("constructor calls not allowed in loops or after labels"); | |
6337 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6338 error("multiple constructor calls"); | |
6339 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6340 } | |
159 | 6341 |
6342 f = cd->ctor; | |
6343 f = f->overloadResolve(loc, arguments); | |
6344 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6345 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6346 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6347 #endif |
159 | 6348 e1 = new DotVarExp(e1->loc, e1, f); |
6349 e1 = e1->semantic(sc); | |
6350 t1 = e1->type; | |
6351 | |
6352 // BUG: this should really be done by checking the static | |
6353 // call graph | |
6354 if (f == sc->func) | |
6355 error("cyclic constructor call"); | |
6356 } | |
6357 } | |
6358 else if (!t1) | |
6359 { | |
6360 error("function expected before (), not '%s'", e1->toChars()); | |
6361 type = Type::terror; | |
6362 return this; | |
6363 } | |
6364 else if (t1->ty != Tfunction) | |
6365 { | |
6366 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
|
6367 { 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
|
6368 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
|
6369 tf = (TypeFunction *)(td->next); |
159 | 6370 goto Lcheckargs; |
6371 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6372 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6373 { Expression *e; |
6374 | |
6375 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
|
6376 t1 = ((TypePointer *)t1)->next; |
159 | 6377 e->type = t1; |
6378 e1 = e; | |
6379 } | |
6380 else if (e1->op == TOKtemplate) | |
6381 { | |
6382 TemplateExp *te = (TemplateExp *)e1; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6383 f = te->td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6384 if (!f) |
6385 { type = Type::terror; | |
6386 return this; | |
6387 } | |
6388 if (f->needThis() && hasThis(sc)) | |
6389 { | |
6390 // Supply an implicit 'this', as in | |
6391 // this.ident | |
6392 | |
6393 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6394 goto Lagain; | |
6395 } | |
6396 | |
6397 e1 = new VarExp(loc, f); | |
6398 goto Lagain; | |
6399 } | |
6400 else | |
6401 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6402 type = Type::terror; | |
6403 return this; | |
6404 } | |
6405 } | |
6406 else if (e1->op == TOKvar) | |
6407 { | |
6408 // Do overload resolution | |
6409 VarExp *ve = (VarExp *)e1; | |
6410 | |
6411 f = ve->var->isFuncDeclaration(); | |
6412 assert(f); | |
6413 | |
6414 // Look to see if f is really a function template | |
6415 if (0 && !istemp && f->parent) | |
6416 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6417 | |
6418 if (ti && | |
6419 (ti->name == f->ident || | |
6420 ti->toAlias()->ident == f->ident) | |
6421 && | |
6422 ti->tempdecl) | |
6423 { | |
6424 /* This is so that one can refer to the enclosing | |
6425 * template, even if it has the same name as a member | |
6426 * of the template, if it has a !(arguments) | |
6427 */ | |
6428 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6429 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6430 tempdecl = tempdecl->overroot; // then get the start | |
6431 e1 = new TemplateExp(loc, tempdecl); | |
6432 istemp = 1; | |
6433 goto Lagain; | |
6434 } | |
6435 } | |
6436 | |
6437 f = f->overloadResolve(loc, arguments); | |
6438 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6439 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6440 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6441 #endif |
159 | 6442 |
6443 if (f->needThis() && hasThis(sc)) | |
6444 { | |
6445 // Supply an implicit 'this', as in | |
6446 // this.ident | |
6447 | |
6448 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6449 goto Lagain; | |
6450 } | |
6451 | |
6452 accessCheck(loc, sc, NULL, f); | |
6453 | |
6454 ve->var = f; | |
6455 ve->type = f->type; | |
6456 t1 = f->type; | |
6457 } | |
6458 assert(t1->ty == Tfunction); | |
6459 tf = (TypeFunction *)(t1); | |
6460 | |
6461 Lcheckargs: | |
6462 assert(tf->ty == Tfunction); | |
6463 type = tf->next; | |
6464 | |
6465 if (!arguments) | |
6466 arguments = new Expressions(); | |
6467 functionArguments(loc, sc, tf, arguments); | |
6468 | |
6469 assert(type); | |
6470 | |
6471 if (f && f->tintro) | |
6472 { | |
6473 Type *t = type; | |
6474 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
|
6475 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
|
6476 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6477 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
|
6478 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6479 type = tf->next; |
159 | 6480 return castTo(sc, t); |
6481 } | |
6482 } | |
6483 | |
6484 return this; | |
6485 } | |
6486 | |
6487 int CallExp::checkSideEffect(int flag) | |
6488 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6489 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6490 if (flag != 2) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6491 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6492 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6493 if (e1->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6494 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6495 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6496 /* 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
|
6497 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6498 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
|
6499 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6500 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6501 if (e->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6502 return 1; |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6505 /* 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
|
6506 * then this expression has no side effects. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6507 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6508 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6509 if (t->ty == Tfunction && ((TypeFunction *)t)->ispure) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6510 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6511 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
|
6512 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6513 #endif |
159 | 6514 return 1; |
6515 } | |
6516 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6517 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6518 int CallExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6519 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6520 if (e1->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6521 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6522 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6523 /* 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
|
6524 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6525 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
|
6526 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6527 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6528 if (e->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6529 return 1; |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6532 /* 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
|
6533 * then this expression cannot throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6534 * Note that pure functions can throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6535 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6536 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6537 if (t->ty == Tfunction && ((TypeFunction *)t)->isnothrow) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6538 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6539 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
|
6540 return 0; |
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 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6543 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6544 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6545 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6546 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6547 int CallExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6548 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6549 if (type->toBasetype()->ty == Tstruct) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6550 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6551 Type *tb = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6552 if (tb->ty == Tfunction && ((TypeFunction *)tb)->isref) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6553 return 1; // function returns a reference |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6554 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6555 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6556 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6557 |
159 | 6558 Expression *CallExp::toLvalue(Scope *sc, Expression *e) |
6559 { | |
6560 if (type->toBasetype()->ty == Tstruct) | |
6561 return this; | |
6562 else | |
6563 return Expression::toLvalue(sc, e); | |
6564 } | |
6565 | |
6566 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6567 { int i; | |
6568 | |
6569 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6570 buf->writeByte('('); | |
6571 argsToCBuffer(buf, arguments, hgs); | |
6572 buf->writeByte(')'); | |
6573 } | |
6574 | |
6575 | |
6576 /************************************************************/ | |
6577 | |
6578 AddrExp::AddrExp(Loc loc, Expression *e) | |
6579 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6580 { | |
6581 } | |
6582 | |
6583 Expression *AddrExp::semantic(Scope *sc) | |
6584 { | |
6585 #if LOGSEMANTIC | |
6586 printf("AddrExp::semantic('%s')\n", toChars()); | |
6587 #endif | |
6588 if (!type) | |
6589 { | |
6590 UnaExp::semantic(sc); | |
6591 e1 = e1->toLvalue(sc, NULL); | |
6592 if (!e1->type) | |
6593 { | |
6594 error("cannot take address of %s", e1->toChars()); | |
6595 type = Type::tint32; | |
6596 return this; | |
6597 } | |
6598 type = e1->type->pointerTo(); | |
6599 | |
6600 // See if this should really be a delegate | |
6601 if (e1->op == TOKdotvar) | |
6602 { | |
6603 DotVarExp *dve = (DotVarExp *)e1; | |
6604 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6605 | |
6606 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
|
6607 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6608 e = e->semantic(sc); |
6609 return e; | |
6610 } | |
6611 } | |
6612 else if (e1->op == TOKvar) | |
6613 { | |
6614 VarExp *dve = (VarExp *)e1; | |
6615 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6616 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6617 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6618 // 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
|
6619 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
|
6620 { |
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
|
6621 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
|
6622 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
|
6623 } |
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
|
6624 |
159 | 6625 if (f && f->isNested()) |
6626 { Expression *e; | |
6627 | |
6628 e = new DelegateExp(loc, e1, f); | |
6629 e = e->semantic(sc); | |
6630 return e; | |
6631 } | |
6632 } | |
6633 else if (e1->op == TOKarray) | |
6634 { | |
6635 if (e1->type->toBasetype()->ty == Tbit) | |
6636 error("cannot take address of bit in array"); | |
6637 } | |
6638 return optimize(WANTvalue); | |
6639 } | |
6640 return this; | |
6641 } | |
6642 | |
6643 /************************************************************/ | |
6644 | |
6645 PtrExp::PtrExp(Loc loc, Expression *e) | |
6646 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6647 { | |
6648 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
|
6649 type = ((TypePointer *)e->type)->next; |
159 | 6650 } |
6651 | |
6652 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6653 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6654 { | |
6655 type = t; | |
6656 } | |
6657 | |
6658 Expression *PtrExp::semantic(Scope *sc) | |
6659 { Type *tb; | |
6660 | |
6661 #if LOGSEMANTIC | |
6662 printf("PtrExp::semantic('%s')\n", toChars()); | |
6663 #endif | |
6664 UnaExp::semantic(sc); | |
6665 e1 = resolveProperties(sc, e1); | |
6666 if (type) | |
6667 return this; | |
6668 if (!e1->type) | |
6669 printf("PtrExp::semantic('%s')\n", toChars()); | |
6670 tb = e1->type->toBasetype(); | |
6671 switch (tb->ty) | |
6672 { | |
6673 case Tpointer: | |
6674 type = tb->next; | |
6675 if (type->isbit()) | |
6676 { Expression *e; | |
6677 | |
6678 // Rewrite *p as p[0] | |
6679 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
6680 return e->semantic(sc); | |
6681 } | |
6682 break; | |
6683 | |
6684 case Tsarray: | |
6685 case Tarray: | |
6686 type = tb->next; | |
6687 e1 = e1->castTo(sc, type->pointerTo()); | |
6688 break; | |
6689 | |
6690 default: | |
6691 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
6692 type = Type::tint32; | |
6693 break; | |
6694 } | |
6695 rvalue(); | |
6696 return this; | |
6697 } | |
6698 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6699 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6700 int PtrExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6701 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6702 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6703 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6704 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6705 |
159 | 6706 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) |
6707 { | |
6708 #if 0 | |
6709 tym = tybasic(e1->ET->Tty); | |
6710 if (!(tyscalar(tym) || | |
6711 tym == TYstruct || | |
6712 tym == TYarray && e->Eoper == TOKaddr)) | |
6713 synerr(EM_lvalue); // lvalue expected | |
6714 #endif | |
6715 return this; | |
6716 } | |
6717 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6718 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6719 Expression *PtrExp::modifiableLvalue(Scope *sc, Expression *e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6720 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6721 //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
|
6722 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6723 if (e1->op == TOKsymoff) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6724 { SymOffExp *se = (SymOffExp *)e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6725 se->var->checkModify(loc, sc, type); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6726 //return toLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6727 } |
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 return Expression::modifiableLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6730 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6731 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6732 |
159 | 6733 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
6734 { | |
6735 buf->writeByte('*'); | |
6736 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6737 } | |
6738 | |
6739 /************************************************************/ | |
6740 | |
6741 NegExp::NegExp(Loc loc, Expression *e) | |
6742 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
6743 { | |
6744 } | |
6745 | |
6746 Expression *NegExp::semantic(Scope *sc) | |
6747 { Expression *e; | |
6748 | |
6749 #if LOGSEMANTIC | |
6750 printf("NegExp::semantic('%s')\n", toChars()); | |
6751 #endif | |
6752 if (!type) | |
6753 { | |
6754 UnaExp::semantic(sc); | |
6755 e1 = resolveProperties(sc, e1); | |
6756 e = op_overload(sc); | |
6757 if (e) | |
6758 return e; | |
6759 | |
6760 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
|
6761 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
|
6762 e1->checkArithmetic(); |
159 | 6763 type = e1->type; |
6764 } | |
6765 return this; | |
6766 } | |
6767 | |
6768 /************************************************************/ | |
6769 | |
6770 UAddExp::UAddExp(Loc loc, Expression *e) | |
6771 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
6772 { | |
6773 } | |
6774 | |
6775 Expression *UAddExp::semantic(Scope *sc) | |
6776 { Expression *e; | |
6777 | |
6778 #if LOGSEMANTIC | |
6779 printf("UAddExp::semantic('%s')\n", toChars()); | |
6780 #endif | |
6781 assert(!type); | |
6782 UnaExp::semantic(sc); | |
6783 e1 = resolveProperties(sc, e1); | |
6784 e = op_overload(sc); | |
6785 if (e) | |
6786 return e; | |
6787 e1->checkNoBool(); | |
6788 e1->checkArithmetic(); | |
6789 return e1; | |
6790 } | |
6791 | |
6792 /************************************************************/ | |
6793 | |
6794 ComExp::ComExp(Loc loc, Expression *e) | |
6795 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6796 { | |
6797 } | |
6798 | |
6799 Expression *ComExp::semantic(Scope *sc) | |
6800 { Expression *e; | |
6801 | |
6802 if (!type) | |
6803 { | |
6804 UnaExp::semantic(sc); | |
6805 e1 = resolveProperties(sc, e1); | |
6806 e = op_overload(sc); | |
6807 if (e) | |
6808 return e; | |
6809 | |
6810 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
|
6811 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
|
6812 e1 = e1->checkIntegral(); |
159 | 6813 type = e1->type; |
6814 } | |
6815 return this; | |
6816 } | |
6817 | |
6818 /************************************************************/ | |
6819 | |
6820 NotExp::NotExp(Loc loc, Expression *e) | |
6821 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6822 { | |
6823 } | |
6824 | |
6825 Expression *NotExp::semantic(Scope *sc) | |
6826 { | |
6827 UnaExp::semantic(sc); | |
6828 e1 = resolveProperties(sc, e1); | |
6829 e1 = e1->checkToBoolean(); | |
6830 type = Type::tboolean; | |
6831 return this; | |
6832 } | |
6833 | |
6834 int NotExp::isBit() | |
6835 { | |
6836 return TRUE; | |
6837 } | |
6838 | |
6839 | |
6840 | |
6841 /************************************************************/ | |
6842 | |
6843 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6844 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6845 { | |
6846 type = t; | |
6847 } | |
6848 | |
6849 Expression *BoolExp::semantic(Scope *sc) | |
6850 { | |
6851 UnaExp::semantic(sc); | |
6852 e1 = resolveProperties(sc, e1); | |
6853 e1 = e1->checkToBoolean(); | |
6854 type = Type::tboolean; | |
6855 return this; | |
6856 } | |
6857 | |
6858 int BoolExp::isBit() | |
6859 { | |
6860 return TRUE; | |
6861 } | |
6862 | |
6863 /************************************************************/ | |
6864 | |
6865 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6866 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6867 { | |
6868 } | |
6869 | |
6870 Expression *DeleteExp::semantic(Scope *sc) | |
6871 { | |
6872 Type *tb; | |
6873 | |
6874 UnaExp::semantic(sc); | |
6875 e1 = resolveProperties(sc, e1); | |
6876 e1 = e1->toLvalue(sc, NULL); | |
6877 type = Type::tvoid; | |
6878 | |
6879 tb = e1->type->toBasetype(); | |
6880 switch (tb->ty) | |
6881 { case Tclass: | |
6882 { TypeClass *tc = (TypeClass *)tb; | |
6883 ClassDeclaration *cd = tc->sym; | |
6884 | |
6885 if (cd->isCOMinterface()) | |
6886 { /* Because COM classes are deleted by IUnknown.Release() | |
6887 */ | |
6888 error("cannot delete instance of COM interface %s", cd->toChars()); | |
6889 } | |
6890 break; | |
6891 } | |
6892 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
|
6893 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 6894 if (tb->ty == Tstruct) |
6895 { | |
6896 TypeStruct *ts = (TypeStruct *)tb; | |
6897 StructDeclaration *sd = ts->sym; | |
6898 FuncDeclaration *f = sd->aggDelete; | |
6899 | |
6900 if (f) | |
6901 { | |
6902 Type *tpv = Type::tvoid->pointerTo(); | |
6903 | |
6904 Expression *e = e1->castTo(sc, tpv); | |
6905 Expression *ec = new VarExp(loc, f); | |
6906 e = new CallExp(loc, ec, e); | |
6907 return e->semantic(sc); | |
6908 } | |
6909 } | |
6910 break; | |
6911 | |
6912 case Tarray: | |
6913 break; | |
6914 | |
6915 default: | |
6916 if (e1->op == TOKindex) | |
6917 { | |
6918 IndexExp *ae = (IndexExp *)(e1); | |
6919 Type *tb1 = ae->e1->type->toBasetype(); | |
6920 if (tb1->ty == Taarray) | |
6921 break; | |
6922 } | |
6923 error("cannot delete type %s", e1->type->toChars()); | |
6924 break; | |
6925 } | |
6926 | |
6927 if (e1->op == TOKindex) | |
6928 { | |
6929 IndexExp *ae = (IndexExp *)(e1); | |
6930 Type *tb1 = ae->e1->type->toBasetype(); | |
6931 if (tb1->ty == Taarray) | |
6932 { if (!global.params.useDeprecated) | |
6933 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6934 } | |
6935 } | |
6936 | |
6937 return this; | |
6938 } | |
6939 | |
6940 int DeleteExp::checkSideEffect(int flag) | |
6941 { | |
6942 return 1; | |
6943 } | |
6944 | |
6945 Expression *DeleteExp::checkToBoolean() | |
6946 { | |
6947 error("delete does not give a boolean result"); | |
6948 return this; | |
6949 } | |
6950 | |
6951 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6952 { | |
6953 buf->writestring("delete "); | |
6954 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6955 } | |
6956 | |
6957 /************************************************************/ | |
6958 | |
6959 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6960 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6961 { | |
6962 to = t; | |
6963 } | |
6964 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6965 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6966 /* For cast(const) and cast(immutable) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6967 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6968 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
|
6969 : UnaExp(loc, TOKcast, sizeof(CastExp), e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6970 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6971 to = NULL; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6972 this->mod = mod; |
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 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6975 |
159 | 6976 Expression *CastExp::syntaxCopy() |
6977 { | |
6978 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6979 } | |
6980 | |
6981 | |
6982 Expression *CastExp::semantic(Scope *sc) | |
6983 { Expression *e; | |
6984 BinExp *b; | |
6985 UnaExp *u; | |
6986 | |
6987 #if LOGSEMANTIC | |
6988 printf("CastExp::semantic('%s')\n", toChars()); | |
6989 #endif | |
6990 | |
6991 //static int x; assert(++x < 10); | |
6992 | |
6993 if (type) | |
6994 return this; | |
6995 UnaExp::semantic(sc); | |
6996 if (e1->type) // if not a tuple | |
6997 { | |
6998 e1 = resolveProperties(sc, e1); | |
6999 to = to->semantic(loc, sc); | |
7000 | |
7001 e = op_overload(sc); | |
7002 if (e) | |
7003 { | |
7004 return e->implicitCastTo(sc, to); | |
7005 } | |
7006 | |
7007 Type *tob = to->toBasetype(); | |
7008 if (tob->ty == Tstruct && | |
7009 !tob->equals(e1->type->toBasetype()) && | |
7010 ((TypeStruct *)to)->sym->search(0, Id::call, 0) | |
7011 ) | |
7012 { | |
7013 /* Look to replace: | |
7014 * cast(S)t | |
7015 * with: | |
7016 * S(t) | |
7017 */ | |
7018 | |
7019 // Rewrite as to.call(e1) | |
7020 e = new TypeExp(loc, to); | |
7021 e = new DotIdExp(loc, e, Id::call); | |
7022 e = new CallExp(loc, e, e1); | |
7023 e = e->semantic(sc); | |
7024 return e; | |
7025 } | |
7026 } | |
7027 e = e1->castTo(sc, to); | |
7028 return e; | |
7029 } | |
7030 | |
7031 int CastExp::checkSideEffect(int flag) | |
7032 { | |
7033 /* if not: | |
7034 * cast(void) | |
7035 * cast(classtype)func() | |
7036 */ | |
7037 if (!to->equals(Type::tvoid) && | |
7038 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
7039 return Expression::checkSideEffect(flag); | |
7040 return 1; | |
7041 } | |
7042 | |
7043 void CastExp::checkEscape() | |
7044 { Type *tb = type->toBasetype(); | |
7045 if (tb->ty == Tarray && e1->op == TOKvar && | |
7046 e1->type->toBasetype()->ty == Tsarray) | |
7047 { VarExp *ve = (VarExp *)e1; | |
7048 VarDeclaration *v = ve->var->isVarDeclaration(); | |
7049 if (v) | |
7050 { | |
7051 if (!v->isDataseg() && !v->isParameter()) | |
7052 error("escaping reference to local %s", v->toChars()); | |
7053 } | |
7054 } | |
7055 } | |
7056 | |
7057 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7058 { | |
7059 buf->writestring("cast("); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7060 #if DMDV1 |
159 | 7061 to->toCBuffer(buf, NULL, hgs); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7062 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7063 if (to) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7064 to->toCBuffer(buf, NULL, hgs); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7065 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7066 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7067 switch (mod) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7068 { case 0: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7069 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7070 case MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7071 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7072 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7073 case MODinvariant: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7074 buf->writestring(Token::tochars[TOKimmutable]); |
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 MODshared: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7077 buf->writestring(Token::tochars[TOKshared]); |
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 MODshared | MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7080 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7081 buf->writeByte(' '); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7082 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7083 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7084 default: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7085 assert(0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7086 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7087 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7088 #endif |
159 | 7089 buf->writeByte(')'); |
7090 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7091 } | |
7092 | |
7093 | |
7094 /************************************************************/ | |
7095 | |
7096 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
7097 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
7098 { | |
7099 this->upr = upr; | |
7100 this->lwr = lwr; | |
7101 lengthVar = NULL; | |
7102 } | |
7103 | |
7104 Expression *SliceExp::syntaxCopy() | |
7105 { | |
7106 Expression *lwr = NULL; | |
7107 if (this->lwr) | |
7108 lwr = this->lwr->syntaxCopy(); | |
7109 | |
7110 Expression *upr = NULL; | |
7111 if (this->upr) | |
7112 upr = this->upr->syntaxCopy(); | |
7113 | |
7114 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
7115 } | |
7116 | |
7117 Expression *SliceExp::semantic(Scope *sc) | |
7118 { Expression *e; | |
7119 AggregateDeclaration *ad; | |
7120 //FuncDeclaration *fd; | |
7121 ScopeDsymbol *sym; | |
7122 | |
7123 #if LOGSEMANTIC | |
7124 printf("SliceExp::semantic('%s')\n", toChars()); | |
7125 #endif | |
7126 if (type) | |
7127 return this; | |
7128 | |
7129 UnaExp::semantic(sc); | |
7130 e1 = resolveProperties(sc, e1); | |
7131 | |
7132 e = this; | |
7133 | |
7134 Type *t = e1->type->toBasetype(); | |
7135 if (t->ty == Tpointer) | |
7136 { | |
7137 if (!lwr || !upr) | |
7138 error("need upper and lower bound to slice pointer"); | |
7139 } | |
7140 else if (t->ty == Tarray) | |
7141 { | |
7142 } | |
7143 else if (t->ty == Tsarray) | |
7144 { | |
7145 } | |
7146 else if (t->ty == Tclass) | |
7147 { | |
7148 ad = ((TypeClass *)t)->sym; | |
7149 goto L1; | |
7150 } | |
7151 else if (t->ty == Tstruct) | |
7152 { | |
7153 ad = ((TypeStruct *)t)->sym; | |
7154 | |
7155 L1: | |
7156 if (search_function(ad, Id::slice)) | |
7157 { | |
7158 // Rewrite as e1.slice(lwr, upr) | |
7159 e = new DotIdExp(loc, e1, Id::slice); | |
7160 | |
7161 if (lwr) | |
7162 { | |
7163 assert(upr); | |
7164 e = new CallExp(loc, e, lwr, upr); | |
7165 } | |
7166 else | |
7167 { assert(!upr); | |
7168 e = new CallExp(loc, e); | |
7169 } | |
7170 e = e->semantic(sc); | |
7171 return e; | |
7172 } | |
7173 goto Lerror; | |
7174 } | |
7175 else if (t->ty == Ttuple) | |
7176 { | |
7177 if (!lwr && !upr) | |
7178 return e1; | |
7179 if (!lwr || !upr) | |
7180 { error("need upper and lower bound to slice tuple"); | |
7181 goto Lerror; | |
7182 } | |
7183 } | |
7184 else | |
7185 goto Lerror; | |
7186 | |
7187 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7188 { | |
7189 sym = new ArrayScopeSymbol(this); | |
7190 sym->loc = loc; | |
7191 sym->parent = sc->scopesym; | |
7192 sc = sc->push(sym); | |
7193 } | |
7194 | |
7195 if (lwr) | |
7196 { lwr = lwr->semantic(sc); | |
7197 lwr = resolveProperties(sc, lwr); | |
7198 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
7199 } | |
7200 if (upr) | |
7201 { upr = upr->semantic(sc); | |
7202 upr = resolveProperties(sc, upr); | |
7203 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
7204 } | |
7205 | |
7206 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7207 sc->pop(); | |
7208 | |
7209 if (t->ty == Ttuple) | |
7210 { | |
7211 lwr = lwr->optimize(WANTvalue); | |
7212 upr = upr->optimize(WANTvalue); | |
7213 uinteger_t i1 = lwr->toUInteger(); | |
7214 uinteger_t i2 = upr->toUInteger(); | |
7215 | |
7216 size_t length; | |
7217 TupleExp *te; | |
7218 TypeTuple *tup; | |
7219 | |
7220 if (e1->op == TOKtuple) // slicing an expression tuple | |
7221 { te = (TupleExp *)e1; | |
7222 length = te->exps->dim; | |
7223 } | |
7224 else if (e1->op == TOKtype) // slicing a type tuple | |
7225 { tup = (TypeTuple *)t; | |
7226 length = Argument::dim(tup->arguments); | |
7227 } | |
7228 else | |
7229 assert(0); | |
7230 | |
7231 if (i1 <= i2 && i2 <= length) | |
7232 { size_t j1 = (size_t) i1; | |
7233 size_t j2 = (size_t) i2; | |
7234 | |
7235 if (e1->op == TOKtuple) | |
7236 { Expressions *exps = new Expressions; | |
7237 exps->setDim(j2 - j1); | |
7238 for (size_t i = 0; i < j2 - j1; i++) | |
7239 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
7240 exps->data[i] = (void *)e; | |
7241 } | |
7242 e = new TupleExp(loc, exps); | |
7243 } | |
7244 else | |
7245 { Arguments *args = new Arguments; | |
7246 args->reserve(j2 - j1); | |
7247 for (size_t i = j1; i < j2; i++) | |
7248 { Argument *arg = Argument::getNth(tup->arguments, i); | |
7249 args->push(arg); | |
7250 } | |
7251 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
7252 } | |
7253 e = e->semantic(sc); | |
7254 } | |
7255 else | |
7256 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7257 error("string slice [%ju .. %ju] is out of bounds", i1, i2); |
159 | 7258 e = e1; |
7259 } | |
7260 return e; | |
7261 } | |
7262 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7263 if (t->ty == Tarray) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7264 type = e1->type; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7265 else |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7266 type = t->nextOf()->arrayOf(); |
159 | 7267 return e; |
7268 | |
7269 Lerror: | |
7270 char *s; | |
7271 if (t->ty == Tvoid) | |
7272 s = e1->toChars(); | |
7273 else | |
7274 s = t->toChars(); | |
7275 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
|
7276 e = new IntegerExp(0); |
159 | 7277 return e; |
7278 } | |
7279 | |
7280 void SliceExp::checkEscape() | |
7281 { | |
7282 e1->checkEscape(); | |
7283 } | |
7284 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7285 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7286 int SliceExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7287 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7288 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7289 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7290 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7291 |
159 | 7292 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) |
7293 { | |
7294 return this; | |
7295 } | |
7296 | |
7297 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
7298 { | |
7299 error("slice expression %s is not a modifiable lvalue", toChars()); | |
7300 return this; | |
7301 } | |
7302 | |
7303 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7304 { | |
7305 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7306 buf->writeByte('['); | |
7307 if (upr || lwr) | |
7308 { | |
7309 if (lwr) | |
7310 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
7311 else | |
7312 buf->writeByte('0'); | |
7313 buf->writestring(".."); | |
7314 if (upr) | |
7315 expToCBuffer(buf, hgs, upr, PREC_assign); | |
7316 else | |
7317 buf->writestring("length"); // BUG: should be array.length | |
7318 } | |
7319 buf->writeByte(']'); | |
7320 } | |
7321 | |
7322 /********************** ArrayLength **************************************/ | |
7323 | |
7324 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
7325 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
7326 { | |
7327 } | |
7328 | |
7329 Expression *ArrayLengthExp::semantic(Scope *sc) | |
7330 { Expression *e; | |
7331 | |
7332 #if LOGSEMANTIC | |
7333 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
7334 #endif | |
7335 if (!type) | |
7336 { | |
7337 UnaExp::semantic(sc); | |
7338 e1 = resolveProperties(sc, e1); | |
7339 | |
7340 type = Type::tsize_t; | |
7341 } | |
7342 return this; | |
7343 } | |
7344 | |
7345 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7346 { | |
7347 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7348 buf->writestring(".length"); | |
7349 } | |
7350 | |
7351 /*********************** ArrayExp *************************************/ | |
7352 | |
7353 // e1 [ i1, i2, i3, ... ] | |
7354 | |
7355 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
7356 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
7357 { | |
7358 arguments = args; | |
7359 } | |
7360 | |
7361 Expression *ArrayExp::syntaxCopy() | |
7362 { | |
7363 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
7364 } | |
7365 | |
7366 Expression *ArrayExp::semantic(Scope *sc) | |
7367 { Expression *e; | |
7368 Type *t1; | |
7369 | |
7370 #if LOGSEMANTIC | |
7371 printf("ArrayExp::semantic('%s')\n", toChars()); | |
7372 #endif | |
7373 UnaExp::semantic(sc); | |
7374 e1 = resolveProperties(sc, e1); | |
7375 | |
7376 t1 = e1->type->toBasetype(); | |
7377 if (t1->ty != Tclass && t1->ty != Tstruct) | |
7378 { // Convert to IndexExp | |
7379 if (arguments->dim != 1) | |
7380 error("only one index allowed to index %s", t1->toChars()); | |
7381 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
7382 return e->semantic(sc); | |
7383 } | |
7384 | |
7385 // Run semantic() on each argument | |
7386 for (size_t i = 0; i < arguments->dim; i++) | |
7387 { e = (Expression *)arguments->data[i]; | |
7388 | |
7389 e = e->semantic(sc); | |
7390 if (!e->type) | |
7391 error("%s has no value", e->toChars()); | |
7392 arguments->data[i] = (void *)e; | |
7393 } | |
7394 | |
7395 expandTuples(arguments); | |
7396 assert(arguments && arguments->dim); | |
7397 | |
7398 e = op_overload(sc); | |
7399 if (!e) | |
7400 { error("no [] operator overload for type %s", e1->type->toChars()); | |
7401 e = e1; | |
7402 } | |
7403 return e; | |
7404 } | |
7405 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7406 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7407 int ArrayExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7408 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7409 if (type && type->toBasetype()->ty == Tvoid) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7410 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7411 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7412 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7413 #endif |
159 | 7414 |
7415 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
7416 { | |
7417 if (type && type->toBasetype()->ty == Tvoid) | |
7418 error("voids have no value"); | |
7419 return this; | |
7420 } | |
7421 | |
7422 | |
7423 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7424 { int i; | |
7425 | |
7426 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7427 buf->writeByte('['); | |
7428 argsToCBuffer(buf, arguments, hgs); | |
7429 buf->writeByte(']'); | |
7430 } | |
7431 | |
7432 /************************* DotExp ***********************************/ | |
7433 | |
7434 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
7435 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
7436 { | |
7437 } | |
7438 | |
7439 Expression *DotExp::semantic(Scope *sc) | |
7440 { | |
7441 #if LOGSEMANTIC | |
7442 printf("DotExp::semantic('%s')\n", toChars()); | |
7443 if (type) printf("\ttype = %s\n", type->toChars()); | |
7444 #endif | |
7445 e1 = e1->semantic(sc); | |
7446 e2 = e2->semantic(sc); | |
7447 if (e2->op == TOKimport) | |
7448 { | |
7449 ScopeExp *se = (ScopeExp *)e2; | |
7450 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
7451 if (td) | |
7452 { Expression *e = new DotTemplateExp(loc, e1, td); | |
7453 e = e->semantic(sc); | |
7454 return e; | |
7455 } | |
7456 } | |
7457 if (!type) | |
7458 type = e2->type; | |
7459 return this; | |
7460 } | |
7461 | |
7462 | |
7463 /************************* CommaExp ***********************************/ | |
7464 | |
7465 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
7466 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
7467 { | |
7468 } | |
7469 | |
7470 Expression *CommaExp::semantic(Scope *sc) | |
7471 { | |
7472 if (!type) | |
7473 { BinExp::semanticp(sc); | |
7474 type = e2->type; | |
7475 } | |
7476 return this; | |
7477 } | |
7478 | |
7479 void CommaExp::checkEscape() | |
7480 { | |
7481 e2->checkEscape(); | |
7482 } | |
7483 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7484 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7485 int CommaExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7486 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7487 return e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7488 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7489 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7490 |
159 | 7491 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) |
7492 { | |
7493 e2 = e2->toLvalue(sc, NULL); | |
7494 return this; | |
7495 } | |
7496 | |
7497 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7498 { | |
7499 e2 = e2->modifiableLvalue(sc, e); | |
7500 return this; | |
7501 } | |
7502 | |
7503 int CommaExp::isBool(int result) | |
7504 { | |
7505 return e2->isBool(result); | |
7506 } | |
7507 | |
7508 int CommaExp::checkSideEffect(int flag) | |
7509 { | |
7510 if (flag == 2) | |
7511 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7512 else | |
7513 { | |
7514 // Don't check e1 until we cast(void) the a,b code generation | |
7515 return e2->checkSideEffect(flag); | |
7516 } | |
7517 } | |
7518 | |
7519 /************************** IndexExp **********************************/ | |
7520 | |
7521 // e1 [ e2 ] | |
7522 | |
7523 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7524 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7525 { | |
7526 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7527 lengthVar = NULL; | |
7528 modifiable = 0; // assume it is an rvalue | |
7529 } | |
7530 | |
7531 Expression *IndexExp::semantic(Scope *sc) | |
7532 { Expression *e; | |
7533 BinExp *b; | |
7534 UnaExp *u; | |
7535 Type *t1; | |
7536 ScopeDsymbol *sym; | |
7537 | |
7538 #if LOGSEMANTIC | |
7539 printf("IndexExp::semantic('%s')\n", toChars()); | |
7540 #endif | |
7541 if (type) | |
7542 return this; | |
7543 if (!e1->type) | |
7544 e1 = e1->semantic(sc); | |
7545 assert(e1->type); // semantic() should already be run on it | |
7546 e = this; | |
7547 | |
7548 // Note that unlike C we do not implement the int[ptr] | |
7549 | |
7550 t1 = e1->type->toBasetype(); | |
7551 | |
7552 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7553 { // Create scope for 'length' variable | |
7554 sym = new ArrayScopeSymbol(this); | |
7555 sym->loc = loc; | |
7556 sym->parent = sc->scopesym; | |
7557 sc = sc->push(sym); | |
7558 } | |
7559 | |
7560 e2 = e2->semantic(sc); | |
7561 if (!e2->type) | |
7562 { | |
7563 error("%s has no value", e2->toChars()); | |
7564 e2->type = Type::terror; | |
7565 } | |
7566 e2 = resolveProperties(sc, e2); | |
7567 | |
7568 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7569 sc = sc->pop(); | |
7570 | |
7571 switch (t1->ty) | |
7572 { | |
7573 case Tpointer: | |
7574 case Tarray: | |
7575 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7576 e->type = t1->next; | |
7577 break; | |
7578 | |
7579 case Tsarray: | |
7580 { | |
7581 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7582 | |
7583 TypeSArray *tsa = (TypeSArray *)t1; | |
7584 | |
7585 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7586 // Do compile time array bounds checking if possible | |
7587 e2 = e2->optimize(WANTvalue); | |
7588 if (e2->op == TOKint64) | |
7589 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
7590 dinteger_t index = e2->toInteger(); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
7591 dinteger_t length = tsa->dim->toInteger(); |
159 | 7592 if (index < 0 || index >= length) |
7593 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7594 index, length); | |
7595 } | |
7596 #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
|
7597 e->type = t1->nextOf(); |
159 | 7598 break; |
7599 } | |
7600 | |
7601 case Taarray: | |
7602 { TypeAArray *taa = (TypeAArray *)t1; | |
7603 | |
7604 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7605 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7606 type = taa->next; | |
7607 break; | |
7608 } | |
7609 | |
7610 case Ttuple: | |
7611 { | |
7612 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7613 e2 = e2->optimize(WANTvalue); | |
7614 uinteger_t index = e2->toUInteger(); | |
7615 size_t length; | |
7616 TupleExp *te; | |
7617 TypeTuple *tup; | |
7618 | |
7619 if (e1->op == TOKtuple) | |
7620 { te = (TupleExp *)e1; | |
7621 length = te->exps->dim; | |
7622 } | |
7623 else if (e1->op == TOKtype) | |
7624 { | |
7625 tup = (TypeTuple *)t1; | |
7626 length = Argument::dim(tup->arguments); | |
7627 } | |
7628 else | |
7629 assert(0); | |
7630 | |
7631 if (index < length) | |
7632 { | |
7633 | |
7634 if (e1->op == TOKtuple) | |
7635 e = (Expression *)te->exps->data[(size_t)index]; | |
7636 else | |
7637 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
7638 } | |
7639 else | |
7640 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7641 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
|
7642 index, length); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7643 e = e1; |
159 | 7644 } |
7645 break; | |
7646 } | |
7647 | |
7648 default: | |
7649 error("%s must be an array or pointer type, not %s", | |
7650 e1->toChars(), e1->type->toChars()); | |
7651 type = Type::tint32; | |
7652 break; | |
7653 } | |
7654 return e; | |
7655 } | |
7656 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7657 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7658 int IndexExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7659 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7660 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7661 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7662 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7663 |
159 | 7664 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) |
7665 { | |
7666 // if (type && type->toBasetype()->ty == Tvoid) | |
7667 // error("voids have no value"); | |
7668 return this; | |
7669 } | |
7670 | |
7671 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
7672 { | |
7673 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
7674 modifiable = 1; | |
7675 if (e1->op == TOKstring) | |
7676 error("string literals are immutable"); | |
7677 if (e1->type->toBasetype()->ty == Taarray) | |
7678 e1 = e1->modifiableLvalue(sc, e1); | |
7679 return toLvalue(sc, e); | |
7680 } | |
7681 | |
7682 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7683 { | |
7684 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7685 buf->writeByte('['); | |
7686 expToCBuffer(buf, hgs, e2, PREC_assign); | |
7687 buf->writeByte(']'); | |
7688 } | |
7689 | |
7690 | |
7691 /************************* PostExp ***********************************/ | |
7692 | |
7693 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
7694 : BinExp(loc, op, sizeof(PostExp), e, | |
7695 new IntegerExp(loc, 1, Type::tint32)) | |
7696 { | |
7697 } | |
7698 | |
7699 Expression *PostExp::semantic(Scope *sc) | |
7700 { Expression *e = this; | |
7701 | |
7702 if (!type) | |
7703 { | |
7704 BinExp::semantic(sc); | |
7705 e2 = resolveProperties(sc, e2); | |
7706 | |
7707 e = op_overload(sc); | |
7708 if (e) | |
7709 return e; | |
7710 | |
7711 e = this; | |
7712 e1 = e1->modifiableLvalue(sc, e1); | |
7713 e1->checkScalar(); | |
7714 e1->checkNoBool(); | |
7715 if (e1->type->ty == Tpointer) | |
7716 e = scaleFactor(sc); | |
7717 else | |
7718 e2 = e2->castTo(sc, e1->type); | |
7719 e->type = e1->type; | |
7720 } | |
7721 return e; | |
7722 } | |
7723 | |
7724 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7725 { | |
7726 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7727 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
7728 } | |
7729 | |
7730 /************************************************************/ | |
7731 | |
7732 /* Can be TOKconstruct too */ | |
7733 | |
7734 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
7735 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
7736 { | |
7737 ismemset = 0; | |
7738 } | |
7739 | |
7740 Expression *AssignExp::semantic(Scope *sc) | |
7741 { Type *t1; | |
7742 Expression *e1old = e1; | |
7743 | |
7744 #if LOGSEMANTIC | |
7745 printf("AssignExp::semantic('%s')\n", toChars()); | |
7746 #endif | |
7747 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
7748 | |
7749 /* Look for operator overloading of a[i]=value. | |
7750 * Do it before semantic() otherwise the a[i] will have been | |
7751 * converted to a.opIndex() already. | |
7752 */ | |
7753 if (e1->op == TOKarray) | |
7754 { Type *t1; | |
7755 ArrayExp *ae = (ArrayExp *)e1; | |
7756 AggregateDeclaration *ad; | |
7757 Identifier *id = Id::index; | |
7758 | |
7759 ae->e1 = ae->e1->semantic(sc); | |
7760 t1 = ae->e1->type->toBasetype(); | |
7761 if (t1->ty == Tstruct) | |
7762 { | |
7763 ad = ((TypeStruct *)t1)->sym; | |
7764 goto L1; | |
7765 } | |
7766 else if (t1->ty == Tclass) | |
7767 { | |
7768 ad = ((TypeClass *)t1)->sym; | |
7769 L1: | |
7770 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
7771 if (search_function(ad, Id::indexass)) | |
7772 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
7773 Expressions *a = (Expressions *)ae->arguments->copy(); | |
7774 | |
7775 a->insert(0, e2); | |
7776 e = new CallExp(loc, e, a); | |
7777 e = e->semantic(sc); | |
7778 return e; | |
7779 } | |
7780 else | |
7781 { | |
7782 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
7783 if (search_function(ad, id)) | |
7784 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
7785 | |
7786 if (1 || !global.params.useDeprecated) | |
7787 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
7788 | |
7789 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
7790 e = e->semantic(sc); | |
7791 return e; | |
7792 } | |
7793 } | |
7794 } | |
7795 } | |
7796 /* Look for operator overloading of a[i..j]=value. | |
7797 * Do it before semantic() otherwise the a[i..j] will have been | |
7798 * converted to a.opSlice() already. | |
7799 */ | |
7800 if (e1->op == TOKslice) | |
7801 { Type *t1; | |
7802 SliceExp *ae = (SliceExp *)e1; | |
7803 AggregateDeclaration *ad; | |
7804 Identifier *id = Id::index; | |
7805 | |
7806 ae->e1 = ae->e1->semantic(sc); | |
7807 ae->e1 = resolveProperties(sc, ae->e1); | |
7808 t1 = ae->e1->type->toBasetype(); | |
7809 if (t1->ty == Tstruct) | |
7810 { | |
7811 ad = ((TypeStruct *)t1)->sym; | |
7812 goto L2; | |
7813 } | |
7814 else if (t1->ty == Tclass) | |
7815 { | |
7816 ad = ((TypeClass *)t1)->sym; | |
7817 L2: | |
7818 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
7819 if (search_function(ad, Id::sliceass)) | |
7820 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
7821 Expressions *a = new Expressions(); | |
7822 | |
7823 a->push(e2); | |
7824 if (ae->lwr) | |
7825 { a->push(ae->lwr); | |
7826 assert(ae->upr); | |
7827 a->push(ae->upr); | |
7828 } | |
7829 else | |
7830 assert(!ae->upr); | |
7831 e = new CallExp(loc, e, a); | |
7832 e = e->semantic(sc); | |
7833 return e; | |
7834 } | |
7835 } | |
7836 } | |
7837 | |
7838 BinExp::semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7839 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7840 if (e1->op == TOKdottd) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7841 { // 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
|
7842 Expression *e = new CallExp(loc, e1, e2); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7843 e = e->semantic(sc); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7844 return e; |
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 |
159 | 7847 e2 = resolveProperties(sc, e2); |
7848 assert(e1->type); | |
7849 | |
7850 /* Rewrite tuple assignment as a tuple of assignments. | |
7851 */ | |
7852 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
7853 { TupleExp *tup1 = (TupleExp *)e1; | |
7854 TupleExp *tup2 = (TupleExp *)e2; | |
7855 size_t dim = tup1->exps->dim; | |
7856 if (dim != tup2->exps->dim) | |
7857 { | |
7858 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
7859 } | |
7860 else | |
7861 { Expressions *exps = new Expressions; | |
7862 exps->setDim(dim); | |
7863 | |
7864 for (int i = 0; i < dim; i++) | |
7865 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
7866 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
7867 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
7868 } | |
7869 Expression *e = new TupleExp(loc, exps); | |
7870 e = e->semantic(sc); | |
7871 return e; | |
7872 } | |
7873 } | |
7874 | |
7875 t1 = e1->type->toBasetype(); | |
7876 | |
7877 if (t1->ty == Tfunction) | |
7878 { // Rewrite f=value to f(value) | |
7879 Expression *e; | |
7880 | |
7881 e = new CallExp(loc, e1, e2); | |
7882 e = e->semantic(sc); | |
7883 return e; | |
7884 } | |
7885 | |
7886 /* If it is an assignment from a 'foreign' type, | |
7887 * check for operator overloading. | |
7888 */ | |
7889 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7890 { | |
7891 if (!e2->type->implicitConvTo(e1->type)) | |
7892 { | |
7893 Expression *e = op_overload(sc); | |
7894 if (e) | |
7895 return e; | |
7896 } | |
7897 } | |
7898 | |
7899 e2->rvalue(); | |
7900 | |
7901 if (e1->op == TOKarraylength) | |
7902 { | |
7903 // e1 is not an lvalue, but we let code generator handle it | |
7904 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7905 | |
7906 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
7907 } | |
7908 else if (e1->op == TOKslice) | |
7909 ; | |
7910 else | |
7911 { // Try to do a decent error message with the expression | |
7912 // before it got constant folded | |
7913 e1 = e1->modifiableLvalue(sc, e1old); | |
7914 } | |
7915 | |
7916 if (e1->op == TOKslice && | |
7917 t1->nextOf() && | |
7918 e2->implicitConvTo(t1->nextOf()) | |
7919 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
7920 ) | |
7921 { // memset | |
7922 ismemset = 1; // make it easy for back end to tell what this is | |
7923 e2 = e2->implicitCastTo(sc, t1->next); | |
7924 } | |
7925 else if (t1->ty == Tsarray) | |
7926 { | |
7927 error("cannot assign to static array %s", e1->toChars()); | |
7928 } | |
7929 else | |
7930 { | |
7931 e2 = e2->implicitCastTo(sc, e1->type); | |
7932 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7933 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7934 /* 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
|
7935 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7936 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
|
7937 (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
|
7938 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
|
7939 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
|
7940 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
|
7941 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
|
7942 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7943 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
|
7944 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
|
7945 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7946 |
159 | 7947 type = e1->type; |
7948 assert(type); | |
7949 return this; | |
7950 } | |
7951 | |
7952 Expression *AssignExp::checkToBoolean() | |
7953 { | |
7954 // Things like: | |
7955 // if (a = b) ... | |
7956 // are usually mistakes. | |
7957 | |
7958 error("'=' does not give a boolean result"); | |
7959 return this; | |
7960 } | |
7961 | |
7962 /************************************************************/ | |
7963 | |
7964 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7965 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7966 { | |
7967 } | |
7968 | |
7969 Expression *AddAssignExp::semantic(Scope *sc) | |
7970 { Expression *e; | |
7971 | |
7972 if (type) | |
7973 return this; | |
7974 | |
7975 BinExp::semantic(sc); | |
7976 e2 = resolveProperties(sc, e2); | |
7977 | |
7978 e = op_overload(sc); | |
7979 if (e) | |
7980 return e; | |
7981 | |
7982 Type *tb1 = e1->type->toBasetype(); | |
7983 Type *tb2 = e2->type->toBasetype(); | |
7984 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7985 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
|
7986 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7987 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
|
7988 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
|
7989 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
|
7990 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7991 else |
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 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
|
7994 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7995 |
159 | 7996 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
7997 (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
|
7998 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 7999 ) |
8000 { | |
8001 type = e1->type; | |
8002 e = this; | |
8003 } | |
8004 else | |
8005 { | |
8006 e1->checkScalar(); | |
8007 e1->checkNoBool(); | |
8008 if (tb1->ty == Tpointer && tb2->isintegral()) | |
8009 e = scaleFactor(sc); | |
8010 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
8011 { | |
8012 #if 0 | |
8013 // Need to rethink this | |
8014 if (e1->op != TOKvar) | |
8015 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
8016 VarDeclaration *v; | |
8017 Expression *ea; | |
8018 Expression *ex; | |
8019 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8020 Identifier *id = Lexer::uniqueId("__name"); |
159 | 8021 |
8022 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
8023 v->semantic(sc); | |
8024 if (!sc->insert(v)) | |
8025 assert(0); | |
8026 v->parent = sc->func; | |
8027 | |
8028 ea = new AddrExp(loc, e1); | |
8029 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
8030 | |
8031 ex = new VarExp(loc, v); | |
8032 ex = new PtrExp(loc, ex); | |
8033 e = new AddExp(loc, ex, e2); | |
8034 e = new CastExp(loc, e, e1->type); | |
8035 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
8036 | |
8037 e = new CommaExp(loc, ea, e); | |
8038 } | |
8039 else | |
8040 #endif | |
8041 { // Rewrite e1+=e2 to e1=e1+e2 | |
8042 // BUG: doesn't account for side effects in e1 | |
8043 // BUG: other assignment operators for bits aren't handled at all | |
8044 e = new AddExp(loc, e1, e2); | |
8045 e = new CastExp(loc, e, e1->type); | |
8046 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
8047 } | |
8048 e = e->semantic(sc); | |
8049 } | |
8050 else | |
8051 { | |
8052 type = e1->type; | |
8053 typeCombine(sc); | |
8054 e1->checkArithmetic(); | |
8055 e2->checkArithmetic(); | |
8056 if (type->isreal() || type->isimaginary()) | |
8057 { | |
8058 assert(global.errors || e2->type->isfloating()); | |
8059 e2 = e2->castTo(sc, e1->type); | |
8060 } | |
8061 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8062 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8063 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
|
8064 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8065 } |
8066 } | |
8067 return e; | |
8068 } | |
8069 | |
8070 /************************************************************/ | |
8071 | |
8072 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8073 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
8074 { | |
8075 } | |
8076 | |
8077 Expression *MinAssignExp::semantic(Scope *sc) | |
8078 { Expression *e; | |
8079 | |
8080 if (type) | |
8081 return this; | |
8082 | |
8083 BinExp::semantic(sc); | |
8084 e2 = resolveProperties(sc, e2); | |
8085 | |
8086 e = op_overload(sc); | |
8087 if (e) | |
8088 return e; | |
8089 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8090 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
|
8091 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8092 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
|
8093 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
|
8094 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
|
8095 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8096 |
159 | 8097 e1 = e1->modifiableLvalue(sc, e1); |
8098 e1->checkScalar(); | |
8099 e1->checkNoBool(); | |
8100 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
8101 e = scaleFactor(sc); | |
8102 else | |
8103 { | |
8104 e1 = e1->checkArithmetic(); | |
8105 e2 = e2->checkArithmetic(); | |
8106 type = e1->type; | |
8107 typeCombine(sc); | |
8108 if (type->isreal() || type->isimaginary()) | |
8109 { | |
8110 assert(e2->type->isfloating()); | |
8111 e2 = e2->castTo(sc, e1->type); | |
8112 } | |
8113 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8114 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8115 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
|
8116 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8117 } |
8118 return e; | |
8119 } | |
8120 | |
8121 /************************************************************/ | |
8122 | |
8123 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8124 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
8125 { | |
8126 } | |
8127 | |
8128 Expression *CatAssignExp::semantic(Scope *sc) | |
8129 { Expression *e; | |
8130 | |
8131 BinExp::semantic(sc); | |
8132 e2 = resolveProperties(sc, e2); | |
8133 | |
8134 e = op_overload(sc); | |
8135 if (e) | |
8136 return e; | |
8137 | |
8138 if (e1->op == TOKslice) | |
8139 { SliceExp *se = (SliceExp *)e1; | |
8140 | |
8141 if (se->e1->type->toBasetype()->ty == Tsarray) | |
8142 error("cannot append to static array %s", se->e1->type->toChars()); | |
8143 } | |
8144 | |
8145 e1 = e1->modifiableLvalue(sc, e1); | |
8146 | |
8147 Type *tb1 = e1->type->toBasetype(); | |
8148 Type *tb2 = e2->type->toBasetype(); | |
8149 | |
8150 e2->rvalue(); | |
8151 | |
8152 if ((tb1->ty == Tarray) && | |
8153 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
8154 e2->implicitConvTo(e1->type) | |
8155 //e1->type->next->equals(e2->type->next) | |
8156 ) | |
8157 { // Append array | |
8158 e2 = e2->castTo(sc, e1->type); | |
8159 type = e1->type; | |
8160 e = this; | |
8161 } | |
8162 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
|
8163 e2->implicitConvTo(tb1->nextOf()) |
159 | 8164 ) |
8165 { // 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
|
8166 e2 = e2->castTo(sc, tb1->nextOf()); |
159 | 8167 type = e1->type; |
8168 e = this; | |
8169 } | |
8170 else | |
8171 { | |
8172 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
8173 type = Type::tint32; | |
8174 e = this; | |
8175 } | |
8176 return e; | |
8177 } | |
8178 | |
8179 /************************************************************/ | |
8180 | |
8181 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8182 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
8183 { | |
8184 } | |
8185 | |
8186 Expression *MulAssignExp::semantic(Scope *sc) | |
8187 { Expression *e; | |
8188 | |
8189 BinExp::semantic(sc); | |
8190 e2 = resolveProperties(sc, e2); | |
8191 | |
8192 e = op_overload(sc); | |
8193 if (e) | |
8194 return e; | |
8195 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8196 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
|
8197 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8198 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
|
8199 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
|
8200 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
|
8201 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8202 |
159 | 8203 e1 = e1->modifiableLvalue(sc, e1); |
8204 e1->checkScalar(); | |
8205 e1->checkNoBool(); | |
8206 type = e1->type; | |
8207 typeCombine(sc); | |
8208 e1->checkArithmetic(); | |
8209 e2->checkArithmetic(); | |
8210 if (e2->type->isfloating()) | |
8211 { Type *t1; | |
8212 Type *t2; | |
8213 | |
8214 t1 = e1->type; | |
8215 t2 = e2->type; | |
8216 if (t1->isreal()) | |
8217 { | |
8218 if (t2->isimaginary() || t2->iscomplex()) | |
8219 { | |
8220 e2 = e2->castTo(sc, t1); | |
8221 } | |
8222 } | |
8223 else if (t1->isimaginary()) | |
8224 { | |
8225 if (t2->isimaginary() || t2->iscomplex()) | |
8226 { | |
8227 switch (t1->ty) | |
8228 { | |
8229 case Timaginary32: t2 = Type::tfloat32; break; | |
8230 case Timaginary64: t2 = Type::tfloat64; break; | |
8231 case Timaginary80: t2 = Type::tfloat80; break; | |
8232 default: | |
8233 assert(0); | |
8234 } | |
8235 e2 = e2->castTo(sc, t2); | |
8236 } | |
8237 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8238 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8239 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
|
8240 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8241 } |
8242 return this; | |
8243 } | |
8244 | |
8245 /************************************************************/ | |
8246 | |
8247 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8248 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
8249 { | |
8250 } | |
8251 | |
8252 Expression *DivAssignExp::semantic(Scope *sc) | |
8253 { Expression *e; | |
8254 | |
8255 BinExp::semantic(sc); | |
8256 e2 = resolveProperties(sc, e2); | |
8257 | |
8258 e = op_overload(sc); | |
8259 if (e) | |
8260 return e; | |
8261 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8262 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
|
8263 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8264 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
|
8265 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
|
8266 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
|
8267 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8268 |
159 | 8269 e1 = e1->modifiableLvalue(sc, e1); |
8270 e1->checkScalar(); | |
8271 e1->checkNoBool(); | |
8272 type = e1->type; | |
8273 typeCombine(sc); | |
8274 e1->checkArithmetic(); | |
8275 e2->checkArithmetic(); | |
8276 if (e2->type->isimaginary()) | |
8277 { Type *t1; | |
8278 Type *t2; | |
8279 | |
8280 t1 = e1->type; | |
8281 if (t1->isreal()) | |
8282 { // x/iv = i(-x/v) | |
8283 // Therefore, the result is 0 | |
8284 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
8285 e2->type = t1; | |
8286 e = new AssignExp(loc, e1, e2); | |
8287 e->type = t1; | |
8288 return e; | |
8289 } | |
8290 else if (t1->isimaginary()) | |
8291 { Expression *e; | |
8292 | |
8293 switch (t1->ty) | |
8294 { | |
8295 case Timaginary32: t2 = Type::tfloat32; break; | |
8296 case Timaginary64: t2 = Type::tfloat64; break; | |
8297 case Timaginary80: t2 = Type::tfloat80; break; | |
8298 default: | |
8299 assert(0); | |
8300 } | |
8301 e2 = e2->castTo(sc, t2); | |
8302 e = new AssignExp(loc, e1, e2); | |
8303 e->type = t1; | |
8304 return e; | |
8305 } | |
8306 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8307 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8308 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
|
8309 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
|
8310 |
159 | 8311 return this; |
8312 } | |
8313 | |
8314 /************************************************************/ | |
8315 | |
8316 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8317 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
8318 { | |
8319 } | |
8320 | |
8321 Expression *ModAssignExp::semantic(Scope *sc) | |
8322 { | |
8323 return commonSemanticAssign(sc); | |
8324 } | |
8325 | |
8326 /************************************************************/ | |
8327 | |
8328 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8329 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
8330 { | |
8331 } | |
8332 | |
8333 Expression *ShlAssignExp::semantic(Scope *sc) | |
8334 { Expression *e; | |
8335 | |
8336 //printf("ShlAssignExp::semantic()\n"); | |
8337 BinExp::semantic(sc); | |
8338 e2 = resolveProperties(sc, e2); | |
8339 | |
8340 e = op_overload(sc); | |
8341 if (e) | |
8342 return e; | |
8343 | |
8344 e1 = e1->modifiableLvalue(sc, e1); | |
8345 e1->checkScalar(); | |
8346 e1->checkNoBool(); | |
8347 type = e1->type; | |
8348 typeCombine(sc); | |
8349 e1->checkIntegral(); | |
8350 e2 = e2->checkIntegral(); | |
8351 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8352 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8353 return this; |
8354 } | |
8355 | |
8356 /************************************************************/ | |
8357 | |
8358 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8359 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
8360 { | |
8361 } | |
8362 | |
8363 Expression *ShrAssignExp::semantic(Scope *sc) | |
8364 { Expression *e; | |
8365 | |
8366 BinExp::semantic(sc); | |
8367 e2 = resolveProperties(sc, e2); | |
8368 | |
8369 e = op_overload(sc); | |
8370 if (e) | |
8371 return e; | |
8372 | |
8373 e1 = e1->modifiableLvalue(sc, e1); | |
8374 e1->checkScalar(); | |
8375 e1->checkNoBool(); | |
8376 type = e1->type; | |
8377 typeCombine(sc); | |
8378 e1->checkIntegral(); | |
8379 e2 = e2->checkIntegral(); | |
8380 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8381 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8382 return this; |
8383 } | |
8384 | |
8385 /************************************************************/ | |
8386 | |
8387 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8388 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
8389 { | |
8390 } | |
8391 | |
8392 Expression *UshrAssignExp::semantic(Scope *sc) | |
8393 { Expression *e; | |
8394 | |
8395 BinExp::semantic(sc); | |
8396 e2 = resolveProperties(sc, e2); | |
8397 | |
8398 e = op_overload(sc); | |
8399 if (e) | |
8400 return e; | |
8401 | |
8402 e1 = e1->modifiableLvalue(sc, e1); | |
8403 e1->checkScalar(); | |
8404 e1->checkNoBool(); | |
8405 type = e1->type; | |
8406 typeCombine(sc); | |
8407 e1->checkIntegral(); | |
8408 e2 = e2->checkIntegral(); | |
8409 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8410 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8411 return this; |
8412 } | |
8413 | |
8414 /************************************************************/ | |
8415 | |
8416 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8417 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
8418 { | |
8419 } | |
8420 | |
8421 Expression *AndAssignExp::semantic(Scope *sc) | |
8422 { | |
8423 return commonSemanticAssignIntegral(sc); | |
8424 } | |
8425 | |
8426 /************************************************************/ | |
8427 | |
8428 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8429 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
8430 { | |
8431 } | |
8432 | |
8433 Expression *OrAssignExp::semantic(Scope *sc) | |
8434 { | |
8435 return commonSemanticAssignIntegral(sc); | |
8436 } | |
8437 | |
8438 /************************************************************/ | |
8439 | |
8440 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8441 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
8442 { | |
8443 } | |
8444 | |
8445 Expression *XorAssignExp::semantic(Scope *sc) | |
8446 { | |
8447 return commonSemanticAssignIntegral(sc); | |
8448 } | |
8449 | |
8450 /************************* AddExp *****************************/ | |
8451 | |
8452 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
8453 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
8454 { | |
8455 } | |
8456 | |
8457 Expression *AddExp::semantic(Scope *sc) | |
8458 { Expression *e; | |
8459 | |
8460 #if LOGSEMANTIC | |
8461 printf("AddExp::semantic('%s')\n", toChars()); | |
8462 #endif | |
8463 if (!type) | |
8464 { | |
8465 BinExp::semanticp(sc); | |
8466 | |
8467 e = op_overload(sc); | |
8468 if (e) | |
8469 return e; | |
8470 | |
8471 Type *tb1 = e1->type->toBasetype(); | |
8472 Type *tb2 = e2->type->toBasetype(); | |
8473 | |
8474 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
8475 (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
|
8476 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8477 ) |
8478 { | |
8479 type = e1->type; | |
8480 e = this; | |
8481 } | |
8482 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
8483 tb2->ty == Tpointer && e1->type->isintegral()) | |
8484 e = scaleFactor(sc); | |
8485 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
8486 { | |
8487 incompatibleTypes(); | |
8488 type = e1->type; | |
8489 e = this; | |
8490 } | |
8491 else | |
8492 { | |
8493 typeCombine(sc); | |
8494 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
8495 (e1->type->isimaginary() && e2->type->isreal())) | |
8496 { | |
8497 switch (type->toBasetype()->ty) | |
8498 { | |
8499 case Tfloat32: | |
8500 case Timaginary32: | |
8501 type = Type::tcomplex32; | |
8502 break; | |
8503 | |
8504 case Tfloat64: | |
8505 case Timaginary64: | |
8506 type = Type::tcomplex64; | |
8507 break; | |
8508 | |
8509 case Tfloat80: | |
8510 case Timaginary80: | |
8511 type = Type::tcomplex80; | |
8512 break; | |
8513 | |
8514 default: | |
8515 assert(0); | |
8516 } | |
8517 } | |
8518 e = this; | |
8519 } | |
8520 return e; | |
8521 } | |
8522 return this; | |
8523 } | |
8524 | |
8525 /************************************************************/ | |
8526 | |
8527 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8528 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8529 { | |
8530 } | |
8531 | |
8532 Expression *MinExp::semantic(Scope *sc) | |
8533 { Expression *e; | |
8534 Type *t1; | |
8535 Type *t2; | |
8536 | |
8537 #if LOGSEMANTIC | |
8538 printf("MinExp::semantic('%s')\n", toChars()); | |
8539 #endif | |
8540 if (type) | |
8541 return this; | |
8542 | |
8543 BinExp::semanticp(sc); | |
8544 | |
8545 e = op_overload(sc); | |
8546 if (e) | |
8547 return e; | |
8548 | |
8549 e = this; | |
8550 t1 = e1->type->toBasetype(); | |
8551 t2 = e2->type->toBasetype(); | |
8552 if (t1->ty == Tpointer) | |
8553 { | |
8554 if (t2->ty == Tpointer) | |
8555 { // Need to divide the result by the stride | |
8556 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8557 d_int64 stride; | |
8558 Expression *e; | |
8559 | |
8560 typeCombine(sc); // make sure pointer types are compatible | |
8561 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
|
8562 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
|
8563 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
|
8564 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8565 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
|
8566 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8567 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8568 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8569 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
|
8570 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
|
8571 } |
159 | 8572 return e; |
8573 } | |
8574 else if (t2->isintegral()) | |
8575 e = scaleFactor(sc); | |
8576 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
|
8577 { error("incompatible types for minus"); |
159 | 8578 return new IntegerExp(0); |
8579 } | |
8580 } | |
8581 else if (t2->ty == Tpointer) | |
8582 { | |
8583 type = e2->type; | |
8584 error("can't subtract pointer from %s", e1->type->toChars()); | |
8585 return new IntegerExp(0); | |
8586 } | |
8587 else | |
8588 { | |
8589 typeCombine(sc); | |
8590 t1 = e1->type->toBasetype(); | |
8591 t2 = e2->type->toBasetype(); | |
8592 if ((t1->isreal() && t2->isimaginary()) || | |
8593 (t1->isimaginary() && t2->isreal())) | |
8594 { | |
8595 switch (type->ty) | |
8596 { | |
8597 case Tfloat32: | |
8598 case Timaginary32: | |
8599 type = Type::tcomplex32; | |
8600 break; | |
8601 | |
8602 case Tfloat64: | |
8603 case Timaginary64: | |
8604 type = Type::tcomplex64; | |
8605 break; | |
8606 | |
8607 case Tfloat80: | |
8608 case Timaginary80: | |
8609 type = Type::tcomplex80; | |
8610 break; | |
8611 | |
8612 default: | |
8613 assert(0); | |
8614 } | |
8615 } | |
8616 } | |
8617 return e; | |
8618 } | |
8619 | |
8620 /************************* CatExp *****************************/ | |
8621 | |
8622 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
8623 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
8624 { | |
8625 } | |
8626 | |
8627 Expression *CatExp::semantic(Scope *sc) | |
8628 { Expression *e; | |
8629 | |
8630 //printf("CatExp::semantic() %s\n", toChars()); | |
8631 if (!type) | |
8632 { | |
8633 BinExp::semanticp(sc); | |
8634 e = op_overload(sc); | |
8635 if (e) | |
8636 return e; | |
8637 | |
8638 Type *tb1 = e1->type->toBasetype(); | |
8639 Type *tb2 = e2->type->toBasetype(); | |
8640 | |
8641 | |
8642 /* BUG: Should handle things like: | |
8643 * char c; | |
8644 * c ~ ' ' | |
8645 * ' ' ~ c; | |
8646 */ | |
8647 | |
8648 #if 0 | |
8649 e1->type->print(); | |
8650 e2->type->print(); | |
8651 #endif | |
8652 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
8653 e2->type->equals(tb1->next)) | |
8654 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8655 type = tb1->nextOf()->arrayOf(); |
159 | 8656 if (tb2->ty == Tarray) |
8657 { // Make e2 into [e2] | |
8658 e2 = new ArrayLiteralExp(e2->loc, e2); | |
8659 e2->type = type; | |
8660 } | |
8661 return this; | |
8662 } | |
8663 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
8664 e1->type->equals(tb2->next)) | |
8665 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8666 type = tb2->nextOf()->arrayOf(); |
159 | 8667 if (tb1->ty == Tarray) |
8668 { // Make e1 into [e1] | |
8669 e1 = new ArrayLiteralExp(e1->loc, e1); | |
8670 e1->type = type; | |
8671 } | |
8672 return this; | |
8673 } | |
8674 | |
8675 typeCombine(sc); | |
8676 | |
8677 if (type->toBasetype()->ty == Tsarray) | |
8678 type = type->toBasetype()->next->arrayOf(); | |
8679 #if 0 | |
8680 e1->type->print(); | |
8681 e2->type->print(); | |
8682 type->print(); | |
8683 print(); | |
8684 #endif | |
8685 if (e1->op == TOKstring && e2->op == TOKstring) | |
8686 e = optimize(WANTvalue); | |
8687 else if (e1->type->equals(e2->type) && | |
8688 (e1->type->toBasetype()->ty == Tarray || | |
8689 e1->type->toBasetype()->ty == Tsarray)) | |
8690 { | |
8691 e = this; | |
8692 } | |
8693 else | |
8694 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8695 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 8696 error("Can only concatenate arrays, not (%s ~ %s)", |
8697 e1->type->toChars(), e2->type->toChars()); | |
8698 type = Type::tint32; | |
8699 e = this; | |
8700 } | |
8701 e->type = e->type->semantic(loc, sc); | |
8702 return e; | |
8703 } | |
8704 return this; | |
8705 } | |
8706 | |
8707 /************************************************************/ | |
8708 | |
8709 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
8710 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
8711 { | |
8712 } | |
8713 | |
8714 Expression *MulExp::semantic(Scope *sc) | |
8715 { Expression *e; | |
8716 | |
8717 #if 0 | |
8718 printf("MulExp::semantic() %s\n", toChars()); | |
8719 #endif | |
8720 if (type) | |
8721 { | |
8722 return this; | |
8723 } | |
8724 | |
8725 BinExp::semanticp(sc); | |
8726 e = op_overload(sc); | |
8727 if (e) | |
8728 return e; | |
8729 | |
8730 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
|
8731 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
|
8732 { 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
|
8733 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
|
8734 } |
159 | 8735 if (type->isfloating()) |
8736 { Type *t1 = e1->type; | |
8737 Type *t2 = e2->type; | |
8738 | |
8739 if (t1->isreal()) | |
8740 { | |
8741 type = t2; | |
8742 } | |
8743 else if (t2->isreal()) | |
8744 { | |
8745 type = t1; | |
8746 } | |
8747 else if (t1->isimaginary()) | |
8748 { | |
8749 if (t2->isimaginary()) | |
8750 { Expression *e; | |
8751 | |
8752 switch (t1->ty) | |
8753 { | |
8754 case Timaginary32: type = Type::tfloat32; break; | |
8755 case Timaginary64: type = Type::tfloat64; break; | |
8756 case Timaginary80: type = Type::tfloat80; break; | |
8757 default: assert(0); | |
8758 } | |
8759 | |
8760 // iy * iv = -yv | |
8761 e1->type = type; | |
8762 e2->type = type; | |
8763 e = new NegExp(loc, this); | |
8764 e = e->semantic(sc); | |
8765 return e; | |
8766 } | |
8767 else | |
8768 type = t2; // t2 is complex | |
8769 } | |
8770 else if (t2->isimaginary()) | |
8771 { | |
8772 type = t1; // t1 is complex | |
8773 } | |
8774 } | |
8775 return this; | |
8776 } | |
8777 | |
8778 /************************************************************/ | |
8779 | |
8780 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
8781 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
8782 { | |
8783 } | |
8784 | |
8785 Expression *DivExp::semantic(Scope *sc) | |
8786 { Expression *e; | |
8787 | |
8788 if (type) | |
8789 return this; | |
8790 | |
8791 BinExp::semanticp(sc); | |
8792 e = op_overload(sc); | |
8793 if (e) | |
8794 return e; | |
8795 | |
8796 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
|
8797 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
|
8798 { 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
|
8799 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
|
8800 } |
159 | 8801 if (type->isfloating()) |
8802 { Type *t1 = e1->type; | |
8803 Type *t2 = e2->type; | |
8804 | |
8805 if (t1->isreal()) | |
8806 { | |
8807 type = t2; | |
8808 if (t2->isimaginary()) | |
8809 { Expression *e; | |
8810 | |
8811 // x/iv = i(-x/v) | |
8812 e2->type = t1; | |
8813 e = new NegExp(loc, this); | |
8814 e = e->semantic(sc); | |
8815 return e; | |
8816 } | |
8817 } | |
8818 else if (t2->isreal()) | |
8819 { | |
8820 type = t1; | |
8821 } | |
8822 else if (t1->isimaginary()) | |
8823 { | |
8824 if (t2->isimaginary()) | |
8825 { | |
8826 switch (t1->ty) | |
8827 { | |
8828 case Timaginary32: type = Type::tfloat32; break; | |
8829 case Timaginary64: type = Type::tfloat64; break; | |
8830 case Timaginary80: type = Type::tfloat80; break; | |
8831 default: assert(0); | |
8832 } | |
8833 } | |
8834 else | |
8835 type = t2; // t2 is complex | |
8836 } | |
8837 else if (t2->isimaginary()) | |
8838 { | |
8839 type = t1; // t1 is complex | |
8840 } | |
8841 } | |
8842 return this; | |
8843 } | |
8844 | |
8845 /************************************************************/ | |
8846 | |
8847 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
8848 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
8849 { | |
8850 } | |
8851 | |
8852 Expression *ModExp::semantic(Scope *sc) | |
8853 { Expression *e; | |
8854 | |
8855 if (type) | |
8856 return this; | |
8857 | |
8858 BinExp::semanticp(sc); | |
8859 e = op_overload(sc); | |
8860 if (e) | |
8861 return e; | |
8862 | |
8863 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
|
8864 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
|
8865 { 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
|
8866 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
|
8867 } |
159 | 8868 if (type->isfloating()) |
8869 { type = e1->type; | |
8870 if (e2->type->iscomplex()) | |
8871 { error("cannot perform modulo complex arithmetic"); | |
8872 return new IntegerExp(0); | |
8873 } | |
8874 } | |
8875 return this; | |
8876 } | |
8877 | |
8878 /************************************************************/ | |
8879 | |
8880 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
8881 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
8882 { | |
8883 } | |
8884 | |
8885 Expression *ShlExp::semantic(Scope *sc) | |
8886 { Expression *e; | |
8887 | |
8888 //printf("ShlExp::semantic(), type = %p\n", type); | |
8889 if (!type) | |
8890 { BinExp::semanticp(sc); | |
8891 e = op_overload(sc); | |
8892 if (e) | |
8893 return e; | |
8894 e1 = e1->checkIntegral(); | |
8895 e2 = e2->checkIntegral(); | |
8896 e1 = e1->integralPromotions(sc); | |
8897 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8898 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8899 type = e1->type; |
8900 } | |
8901 return this; | |
8902 } | |
8903 | |
8904 /************************************************************/ | |
8905 | |
8906 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
8907 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
8908 { | |
8909 } | |
8910 | |
8911 Expression *ShrExp::semantic(Scope *sc) | |
8912 { Expression *e; | |
8913 | |
8914 if (!type) | |
8915 { BinExp::semanticp(sc); | |
8916 e = op_overload(sc); | |
8917 if (e) | |
8918 return e; | |
8919 e1 = e1->checkIntegral(); | |
8920 e2 = e2->checkIntegral(); | |
8921 e1 = e1->integralPromotions(sc); | |
8922 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8923 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8924 type = e1->type; |
8925 } | |
8926 return this; | |
8927 } | |
8928 | |
8929 /************************************************************/ | |
8930 | |
8931 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
8932 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
8933 { | |
8934 } | |
8935 | |
8936 Expression *UshrExp::semantic(Scope *sc) | |
8937 { Expression *e; | |
8938 | |
8939 if (!type) | |
8940 { BinExp::semanticp(sc); | |
8941 e = op_overload(sc); | |
8942 if (e) | |
8943 return e; | |
8944 e1 = e1->checkIntegral(); | |
8945 e2 = e2->checkIntegral(); | |
8946 e1 = e1->integralPromotions(sc); | |
8947 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8948 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8949 type = e1->type; |
8950 } | |
8951 return this; | |
8952 } | |
8953 | |
8954 /************************************************************/ | |
8955 | |
8956 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8957 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8958 { | |
8959 } | |
8960 | |
8961 Expression *AndExp::semantic(Scope *sc) | |
8962 { Expression *e; | |
8963 | |
8964 if (!type) | |
8965 { BinExp::semanticp(sc); | |
8966 e = op_overload(sc); | |
8967 if (e) | |
8968 return e; | |
8969 if (e1->type->toBasetype()->ty == Tbool && | |
8970 e2->type->toBasetype()->ty == Tbool) | |
8971 { | |
8972 type = e1->type; | |
8973 e = this; | |
8974 } | |
8975 else | |
8976 { | |
8977 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
|
8978 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
|
8979 { 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
|
8980 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
|
8981 } |
159 | 8982 } |
8983 } | |
8984 return this; | |
8985 } | |
8986 | |
8987 /************************************************************/ | |
8988 | |
8989 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8990 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8991 { | |
8992 } | |
8993 | |
8994 Expression *OrExp::semantic(Scope *sc) | |
8995 { Expression *e; | |
8996 | |
8997 if (!type) | |
8998 { BinExp::semanticp(sc); | |
8999 e = op_overload(sc); | |
9000 if (e) | |
9001 return e; | |
9002 if (e1->type->toBasetype()->ty == Tbool && | |
9003 e2->type->toBasetype()->ty == Tbool) | |
9004 { | |
9005 type = e1->type; | |
9006 e = this; | |
9007 } | |
9008 else | |
9009 { | |
9010 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
|
9011 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
|
9012 { 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
|
9013 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
|
9014 } |
159 | 9015 } |
9016 } | |
9017 return this; | |
9018 } | |
9019 | |
9020 /************************************************************/ | |
9021 | |
9022 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
9023 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
9024 { | |
9025 } | |
9026 | |
9027 Expression *XorExp::semantic(Scope *sc) | |
9028 { Expression *e; | |
9029 | |
9030 if (!type) | |
9031 { BinExp::semanticp(sc); | |
9032 e = op_overload(sc); | |
9033 if (e) | |
9034 return e; | |
9035 if (e1->type->toBasetype()->ty == Tbool && | |
9036 e2->type->toBasetype()->ty == Tbool) | |
9037 { | |
9038 type = e1->type; | |
9039 e = this; | |
9040 } | |
9041 else | |
9042 { | |
9043 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
|
9044 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
|
9045 { 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
|
9046 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
|
9047 } |
159 | 9048 } |
9049 } | |
9050 return this; | |
9051 } | |
9052 | |
9053 | |
9054 /************************************************************/ | |
9055 | |
9056 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
9057 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
9058 { | |
9059 } | |
9060 | |
9061 Expression *OrOrExp::semantic(Scope *sc) | |
9062 { | |
9063 unsigned cs1; | |
9064 | |
9065 // same as for AndAnd | |
9066 e1 = e1->semantic(sc); | |
9067 e1 = resolveProperties(sc, e1); | |
9068 e1 = e1->checkToPointer(); | |
9069 e1 = e1->checkToBoolean(); | |
9070 cs1 = sc->callSuper; | |
9071 | |
9072 if (sc->flags & SCOPEstaticif) | |
9073 { | |
9074 /* If in static if, don't evaluate e2 if we don't have to. | |
9075 */ | |
9076 e1 = e1->optimize(WANTflags); | |
9077 if (e1->isBool(TRUE)) | |
9078 { | |
9079 return new IntegerExp(loc, 1, Type::tboolean); | |
9080 } | |
9081 } | |
9082 | |
9083 e2 = e2->semantic(sc); | |
9084 sc->mergeCallSuper(loc, cs1); | |
9085 e2 = resolveProperties(sc, e2); | |
9086 e2 = e2->checkToPointer(); | |
9087 | |
9088 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
|
9089 if (e2->type->ty == Tvoid) |
159 | 9090 type = Type::tvoid; |
9091 if (e2->op == TOKtype || e2->op == TOKimport) | |
9092 error("%s is not an expression", e2->toChars()); | |
9093 return this; | |
9094 } | |
9095 | |
9096 Expression *OrOrExp::checkToBoolean() | |
9097 { | |
9098 e2 = e2->checkToBoolean(); | |
9099 return this; | |
9100 } | |
9101 | |
9102 int OrOrExp::isBit() | |
9103 { | |
9104 return TRUE; | |
9105 } | |
9106 | |
9107 int OrOrExp::checkSideEffect(int flag) | |
9108 { | |
9109 if (flag == 2) | |
9110 { | |
9111 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9112 } | |
9113 else | |
9114 { e1->checkSideEffect(1); | |
9115 return e2->checkSideEffect(flag); | |
9116 } | |
9117 } | |
9118 | |
9119 /************************************************************/ | |
9120 | |
9121 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
9122 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
9123 { | |
9124 } | |
9125 | |
9126 Expression *AndAndExp::semantic(Scope *sc) | |
9127 { | |
9128 unsigned cs1; | |
9129 | |
9130 // same as for OrOr | |
9131 e1 = e1->semantic(sc); | |
9132 e1 = resolveProperties(sc, e1); | |
9133 e1 = e1->checkToPointer(); | |
9134 e1 = e1->checkToBoolean(); | |
9135 cs1 = sc->callSuper; | |
9136 | |
9137 if (sc->flags & SCOPEstaticif) | |
9138 { | |
9139 /* If in static if, don't evaluate e2 if we don't have to. | |
9140 */ | |
9141 e1 = e1->optimize(WANTflags); | |
9142 if (e1->isBool(FALSE)) | |
9143 { | |
9144 return new IntegerExp(loc, 0, Type::tboolean); | |
9145 } | |
9146 } | |
9147 | |
9148 e2 = e2->semantic(sc); | |
9149 sc->mergeCallSuper(loc, cs1); | |
9150 e2 = resolveProperties(sc, e2); | |
9151 e2 = e2->checkToPointer(); | |
9152 | |
9153 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
|
9154 if (e2->type->ty == Tvoid) |
159 | 9155 type = Type::tvoid; |
9156 if (e2->op == TOKtype || e2->op == TOKimport) | |
9157 error("%s is not an expression", e2->toChars()); | |
9158 return this; | |
9159 } | |
9160 | |
9161 Expression *AndAndExp::checkToBoolean() | |
9162 { | |
9163 e2 = e2->checkToBoolean(); | |
9164 return this; | |
9165 } | |
9166 | |
9167 int AndAndExp::isBit() | |
9168 { | |
9169 return TRUE; | |
9170 } | |
9171 | |
9172 int AndAndExp::checkSideEffect(int flag) | |
9173 { | |
9174 if (flag == 2) | |
9175 { | |
9176 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9177 } | |
9178 else | |
9179 { | |
9180 e1->checkSideEffect(1); | |
9181 return e2->checkSideEffect(flag); | |
9182 } | |
9183 } | |
9184 | |
9185 /************************************************************/ | |
9186 | |
9187 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
9188 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
9189 { | |
9190 } | |
9191 | |
9192 Expression *InExp::semantic(Scope *sc) | |
9193 { Expression *e; | |
9194 | |
9195 if (type) | |
9196 return this; | |
9197 | |
9198 BinExp::semanticp(sc); | |
9199 e = op_overload(sc); | |
9200 if (e) | |
9201 return e; | |
9202 | |
9203 //type = Type::tboolean; | |
9204 Type *t2b = e2->type->toBasetype(); | |
9205 if (t2b->ty != Taarray) | |
9206 { | |
9207 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
9208 type = Type::terror; | |
9209 } | |
9210 else | |
9211 { | |
9212 TypeAArray *ta = (TypeAArray *)t2b; | |
9213 | |
9214 // Convert key to type of key | |
9215 e1 = e1->implicitCastTo(sc, ta->index); | |
9216 | |
9217 // 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
|
9218 type = ta->nextOf()->pointerTo(); |
159 | 9219 } |
9220 return this; | |
9221 } | |
9222 | |
9223 int InExp::isBit() | |
9224 { | |
9225 return FALSE; | |
9226 } | |
9227 | |
9228 | |
9229 /************************************************************/ | |
9230 | |
9231 /* This deletes the key e1 from the associative array e2 | |
9232 */ | |
9233 | |
9234 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
9235 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
9236 { | |
9237 type = Type::tvoid; | |
9238 } | |
9239 | |
9240 /************************************************************/ | |
9241 | |
9242 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9243 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
9244 { | |
9245 } | |
9246 | |
9247 Expression *CmpExp::semantic(Scope *sc) | |
9248 { Expression *e; | |
9249 Type *t1; | |
9250 Type *t2; | |
9251 | |
9252 #if LOGSEMANTIC | |
9253 printf("CmpExp::semantic('%s')\n", toChars()); | |
9254 #endif | |
9255 if (type) | |
9256 return this; | |
9257 | |
9258 BinExp::semanticp(sc); | |
9259 | |
9260 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9261 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9262 { | |
9263 error("do not use null when comparing class types"); | |
9264 } | |
9265 | |
9266 e = op_overload(sc); | |
9267 if (e) | |
9268 { | |
9269 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
9270 e = e->semantic(sc); | |
9271 return e; | |
9272 } | |
9273 | |
9274 typeCombine(sc); | |
9275 type = Type::tboolean; | |
9276 | |
9277 // Special handling for array comparisons | |
9278 t1 = e1->type->toBasetype(); | |
9279 t2 = e2->type->toBasetype(); | |
9280 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9281 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9282 { | |
9283 if (!t1->next->equals(t2->next)) | |
9284 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9285 e = this; | |
9286 } | |
9287 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
9288 (t1->ty == Tclass && t2->ty == Tclass)) | |
9289 { | |
9290 if (t2->ty == Tstruct) | |
9291 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
9292 else | |
9293 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
9294 e = this; | |
9295 } | |
9296 #if 1 | |
9297 else if (t1->iscomplex() || t2->iscomplex()) | |
9298 { | |
9299 error("compare not defined for complex operands"); | |
9300 e = new IntegerExp(0); | |
9301 } | |
9302 #endif | |
9303 else | |
9304 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
|
9305 //printf("CmpExp: %s\n", e->toChars()); |
159 | 9306 return e; |
9307 } | |
9308 | |
9309 int CmpExp::isBit() | |
9310 { | |
9311 return TRUE; | |
9312 } | |
9313 | |
9314 | |
9315 /************************************************************/ | |
9316 | |
9317 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9318 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
9319 { | |
9320 assert(op == TOKequal || op == TOKnotequal); | |
9321 } | |
9322 | |
9323 Expression *EqualExp::semantic(Scope *sc) | |
9324 { Expression *e; | |
9325 Type *t1; | |
9326 Type *t2; | |
9327 | |
9328 //printf("EqualExp::semantic('%s')\n", toChars()); | |
9329 if (type) | |
9330 return this; | |
9331 | |
9332 BinExp::semanticp(sc); | |
9333 | |
9334 /* Before checking for operator overloading, check to see if we're | |
9335 * comparing the addresses of two statics. If so, we can just see | |
9336 * if they are the same symbol. | |
9337 */ | |
9338 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
9339 { AddrExp *ae1 = (AddrExp *)e1; | |
9340 AddrExp *ae2 = (AddrExp *)e2; | |
9341 | |
9342 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
9343 { VarExp *ve1 = (VarExp *)ae1->e1; | |
9344 VarExp *ve2 = (VarExp *)ae2->e1; | |
9345 | |
9346 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
9347 { | |
9348 // They are the same, result is 'true' for ==, 'false' for != | |
9349 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
9350 return e; | |
9351 } | |
9352 } | |
9353 } | |
9354 | |
9355 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9356 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9357 { | |
9358 error("use '%s' instead of '%s' when comparing with null", | |
9359 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
9360 Token::toChars(op)); | |
9361 } | |
9362 | |
9363 //if (e2->op != TOKnull) | |
9364 { | |
9365 e = op_overload(sc); | |
9366 if (e) | |
9367 { | |
9368 if (op == TOKnotequal) | |
9369 { | |
9370 e = new NotExp(e->loc, e); | |
9371 e = e->semantic(sc); | |
9372 } | |
9373 return e; | |
9374 } | |
9375 } | |
9376 | |
9377 e = typeCombine(sc); | |
9378 type = Type::tboolean; | |
9379 | |
9380 // Special handling for array comparisons | |
9381 t1 = e1->type->toBasetype(); | |
9382 t2 = e2->type->toBasetype(); | |
9383 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9384 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9385 { | |
9386 if (!t1->next->equals(t2->next)) | |
9387 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9388 } | |
9389 else | |
9390 { | |
9391 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9392 { | |
9393 // Cast both to complex | |
9394 e1 = e1->castTo(sc, Type::tcomplex80); | |
9395 e2 = e2->castTo(sc, Type::tcomplex80); | |
9396 } | |
9397 } | |
9398 return e; | |
9399 } | |
9400 | |
9401 int EqualExp::isBit() | |
9402 { | |
9403 return TRUE; | |
9404 } | |
9405 | |
9406 | |
9407 | |
9408 /************************************************************/ | |
9409 | |
9410 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9411 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
9412 { | |
9413 } | |
9414 | |
9415 Expression *IdentityExp::semantic(Scope *sc) | |
9416 { | |
9417 if (type) | |
9418 return this; | |
9419 | |
9420 BinExp::semanticp(sc); | |
9421 type = Type::tboolean; | |
9422 typeCombine(sc); | |
9423 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9424 { | |
9425 // Cast both to complex | |
9426 e1 = e1->castTo(sc, Type::tcomplex80); | |
9427 e2 = e2->castTo(sc, Type::tcomplex80); | |
9428 } | |
9429 return this; | |
9430 } | |
9431 | |
9432 int IdentityExp::isBit() | |
9433 { | |
9434 return TRUE; | |
9435 } | |
9436 | |
9437 | |
9438 /****************************************************************/ | |
9439 | |
9440 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
9441 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
9442 { | |
9443 this->econd = econd; | |
9444 } | |
9445 | |
9446 Expression *CondExp::syntaxCopy() | |
9447 { | |
9448 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
9449 } | |
9450 | |
9451 | |
9452 Expression *CondExp::semantic(Scope *sc) | |
9453 { Type *t1; | |
9454 Type *t2; | |
9455 unsigned cs0; | |
9456 unsigned cs1; | |
9457 | |
9458 #if LOGSEMANTIC | |
9459 printf("CondExp::semantic('%s')\n", toChars()); | |
9460 #endif | |
9461 if (type) | |
9462 return this; | |
9463 | |
9464 econd = econd->semantic(sc); | |
9465 econd = resolveProperties(sc, econd); | |
9466 econd = econd->checkToPointer(); | |
9467 econd = econd->checkToBoolean(); | |
9468 | |
9469 #if 0 /* this cannot work right because the types of e1 and e2 | |
9470 * both contribute to the type of the result. | |
9471 */ | |
9472 if (sc->flags & SCOPEstaticif) | |
9473 { | |
9474 /* If in static if, don't evaluate what we don't have to. | |
9475 */ | |
9476 econd = econd->optimize(WANTflags); | |
9477 if (econd->isBool(TRUE)) | |
9478 { | |
9479 e1 = e1->semantic(sc); | |
9480 e1 = resolveProperties(sc, e1); | |
9481 return e1; | |
9482 } | |
9483 else if (econd->isBool(FALSE)) | |
9484 { | |
9485 e2 = e2->semantic(sc); | |
9486 e2 = resolveProperties(sc, e2); | |
9487 return e2; | |
9488 } | |
9489 } | |
9490 #endif | |
9491 | |
9492 | |
9493 cs0 = sc->callSuper; | |
9494 e1 = e1->semantic(sc); | |
9495 e1 = resolveProperties(sc, e1); | |
9496 cs1 = sc->callSuper; | |
9497 sc->callSuper = cs0; | |
9498 e2 = e2->semantic(sc); | |
9499 e2 = resolveProperties(sc, e2); | |
9500 sc->mergeCallSuper(loc, cs1); | |
9501 | |
9502 | |
9503 // If either operand is void, the result is void | |
9504 t1 = e1->type; | |
9505 t2 = e2->type; | |
9506 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
9507 type = Type::tvoid; | |
9508 else if (t1 == t2) | |
9509 type = t1; | |
9510 else | |
9511 { | |
9512 typeCombine(sc); | |
9513 switch (e1->type->toBasetype()->ty) | |
9514 { | |
9515 case Tcomplex32: | |
9516 case Tcomplex64: | |
9517 case Tcomplex80: | |
9518 e2 = e2->castTo(sc, e1->type); | |
9519 break; | |
9520 } | |
9521 switch (e2->type->toBasetype()->ty) | |
9522 { | |
9523 case Tcomplex32: | |
9524 case Tcomplex64: | |
9525 case Tcomplex80: | |
9526 e1 = e1->castTo(sc, e2->type); | |
9527 break; | |
9528 } | |
9529 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9530 #if 0 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9531 printf("res: %s\n", type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9532 printf("e1 : %s\n", e1->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9533 printf("e2 : %s\n", e2->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9534 #endif |
159 | 9535 return this; |
9536 } | |
9537 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9538 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9539 int CondExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9540 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9541 return e1->isLvalue() && e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9542 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9543 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9544 |
159 | 9545 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) |
9546 { | |
9547 PtrExp *e; | |
9548 | |
9549 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
9550 e = new PtrExp(loc, this, type); | |
9551 | |
9552 e1 = e1->addressOf(sc); | |
9553 //e1 = e1->toLvalue(sc, NULL); | |
9554 | |
9555 e2 = e2->addressOf(sc); | |
9556 //e2 = e2->toLvalue(sc, NULL); | |
9557 | |
9558 typeCombine(sc); | |
9559 | |
9560 type = e2->type; | |
9561 return e; | |
9562 } | |
9563 | |
9564 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
9565 { | |
9566 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
9567 return this; | |
9568 } | |
9569 | |
9570 void CondExp::checkEscape() | |
9571 { | |
9572 e1->checkEscape(); | |
9573 e2->checkEscape(); | |
9574 } | |
9575 | |
9576 | |
9577 Expression *CondExp::checkToBoolean() | |
9578 { | |
9579 e1 = e1->checkToBoolean(); | |
9580 e2 = e2->checkToBoolean(); | |
9581 return this; | |
9582 } | |
9583 | |
9584 int CondExp::checkSideEffect(int flag) | |
9585 { | |
9586 if (flag == 2) | |
9587 { | |
9588 return econd->checkSideEffect(2) || | |
9589 e1->checkSideEffect(2) || | |
9590 e2->checkSideEffect(2); | |
9591 } | |
9592 else | |
9593 { | |
9594 econd->checkSideEffect(1); | |
9595 e1->checkSideEffect(flag); | |
9596 return e2->checkSideEffect(flag); | |
9597 } | |
9598 } | |
9599 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9600 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9601 int CondExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9602 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9603 return econd->canThrow() || e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9604 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9605 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9606 |
159 | 9607 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
9608 { | |
9609 expToCBuffer(buf, hgs, econd, PREC_oror); | |
9610 buf->writestring(" ? "); | |
9611 expToCBuffer(buf, hgs, e1, PREC_expr); | |
9612 buf->writestring(" : "); | |
9613 expToCBuffer(buf, hgs, e2, PREC_cond); | |
9614 } | |
9615 | |
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
|
9616 /************************************************************/ |
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
|
9617 |
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
|
9618 #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
|
9619 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9620 // 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
|
9621 |
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 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
|
9623 : 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
|
9624 { |
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 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
|
9626 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
|
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 |
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 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
|
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 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
|
9632 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
|
9633 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
|
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 |
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 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
|
9637 { |
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 // 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
|
9639 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
|
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 #endif |