Mercurial > projects > ldc
annotate dmd/expression.c @ 1203:d51b5fa41657
Implement static array -> static array cast. Fixes #223.
dynamic array -> static array is still misssing!
author | Christian Kamm <kamm incasoftware de> |
---|---|
date | Sun, 12 Apr 2009 16:18:02 +0200 |
parents | e961851fb8be |
children | 79758fd2f48a |
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 | |
58 Expression *createTypeInfoArray(Scope *sc, Expression *args[], int dim); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
59 Expression *expandVar(int result, VarDeclaration *v); |
159 | 60 |
61 #define LOGSEMANTIC 0 | |
62 | |
63 /********************************** | |
64 * Set operator precedence for each operator. | |
65 */ | |
66 | |
67 // Operator precedence - greater values are higher precedence | |
68 | |
69 enum PREC | |
70 { | |
71 PREC_zero, | |
72 PREC_expr, | |
73 PREC_assign, | |
74 PREC_cond, | |
75 PREC_oror, | |
76 PREC_andand, | |
77 PREC_or, | |
78 PREC_xor, | |
79 PREC_and, | |
80 PREC_equal, | |
81 PREC_rel, | |
82 PREC_shift, | |
83 PREC_add, | |
84 PREC_mul, | |
85 PREC_unary, | |
86 PREC_primary, | |
87 }; | |
88 | |
89 enum PREC precedence[TOKMAX]; | |
90 | |
91 void initPrecedence() | |
92 { | |
93 precedence[TOKdotvar] = PREC_primary; | |
94 precedence[TOKimport] = PREC_primary; | |
95 precedence[TOKidentifier] = PREC_primary; | |
96 precedence[TOKthis] = PREC_primary; | |
97 precedence[TOKsuper] = PREC_primary; | |
98 precedence[TOKint64] = PREC_primary; | |
99 precedence[TOKfloat64] = PREC_primary; | |
100 precedence[TOKnull] = PREC_primary; | |
101 precedence[TOKstring] = PREC_primary; | |
102 precedence[TOKarrayliteral] = PREC_primary; | |
103 precedence[TOKtypedot] = PREC_primary; | |
104 precedence[TOKtypeid] = PREC_primary; | |
105 precedence[TOKis] = PREC_primary; | |
106 precedence[TOKassert] = PREC_primary; | |
107 precedence[TOKfunction] = PREC_primary; | |
108 precedence[TOKvar] = PREC_primary; | |
336 | 109 #if DMDV2 |
110 precedence[TOKdefault] = PREC_primary; | |
111 #endif | |
159 | 112 |
113 // post | |
114 precedence[TOKdotti] = PREC_primary; | |
115 precedence[TOKdot] = PREC_primary; | |
116 // precedence[TOKarrow] = PREC_primary; | |
117 precedence[TOKplusplus] = PREC_primary; | |
118 precedence[TOKminusminus] = PREC_primary; | |
119 precedence[TOKcall] = PREC_primary; | |
120 precedence[TOKslice] = PREC_primary; | |
121 precedence[TOKarray] = PREC_primary; | |
122 | |
123 precedence[TOKaddress] = PREC_unary; | |
124 precedence[TOKstar] = PREC_unary; | |
125 precedence[TOKneg] = PREC_unary; | |
126 precedence[TOKuadd] = PREC_unary; | |
127 precedence[TOKnot] = PREC_unary; | |
128 precedence[TOKtobool] = PREC_add; | |
129 precedence[TOKtilde] = PREC_unary; | |
130 precedence[TOKdelete] = PREC_unary; | |
131 precedence[TOKnew] = PREC_unary; | |
132 precedence[TOKcast] = PREC_unary; | |
133 | |
134 precedence[TOKmul] = PREC_mul; | |
135 precedence[TOKdiv] = PREC_mul; | |
136 precedence[TOKmod] = PREC_mul; | |
137 | |
138 precedence[TOKadd] = PREC_add; | |
139 precedence[TOKmin] = PREC_add; | |
140 precedence[TOKcat] = PREC_add; | |
141 | |
142 precedence[TOKshl] = PREC_shift; | |
143 precedence[TOKshr] = PREC_shift; | |
144 precedence[TOKushr] = PREC_shift; | |
145 | |
146 precedence[TOKlt] = PREC_rel; | |
147 precedence[TOKle] = PREC_rel; | |
148 precedence[TOKgt] = PREC_rel; | |
149 precedence[TOKge] = PREC_rel; | |
150 precedence[TOKunord] = PREC_rel; | |
151 precedence[TOKlg] = PREC_rel; | |
152 precedence[TOKleg] = PREC_rel; | |
153 precedence[TOKule] = PREC_rel; | |
154 precedence[TOKul] = PREC_rel; | |
155 precedence[TOKuge] = PREC_rel; | |
156 precedence[TOKug] = PREC_rel; | |
157 precedence[TOKue] = PREC_rel; | |
158 precedence[TOKin] = PREC_rel; | |
159 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
160 #if 0 |
159 | 161 precedence[TOKequal] = PREC_equal; |
162 precedence[TOKnotequal] = PREC_equal; | |
163 precedence[TOKidentity] = PREC_equal; | |
164 precedence[TOKnotidentity] = PREC_equal; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
165 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
166 /* 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
|
167 * 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
|
168 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
169 precedence[TOKequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
170 precedence[TOKnotequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
171 precedence[TOKidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
172 precedence[TOKnotidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
173 #endif |
159 | 174 |
175 precedence[TOKand] = PREC_and; | |
176 | |
177 precedence[TOKxor] = PREC_xor; | |
178 | |
179 precedence[TOKor] = PREC_or; | |
180 | |
181 precedence[TOKandand] = PREC_andand; | |
182 | |
183 precedence[TOKoror] = PREC_oror; | |
184 | |
185 precedence[TOKquestion] = PREC_cond; | |
186 | |
187 precedence[TOKassign] = PREC_assign; | |
188 precedence[TOKconstruct] = PREC_assign; | |
189 precedence[TOKblit] = PREC_assign; | |
190 precedence[TOKaddass] = PREC_assign; | |
191 precedence[TOKminass] = PREC_assign; | |
192 precedence[TOKcatass] = PREC_assign; | |
193 precedence[TOKmulass] = PREC_assign; | |
194 precedence[TOKdivass] = PREC_assign; | |
195 precedence[TOKmodass] = PREC_assign; | |
196 precedence[TOKshlass] = PREC_assign; | |
197 precedence[TOKshrass] = PREC_assign; | |
198 precedence[TOKushrass] = PREC_assign; | |
199 precedence[TOKandass] = PREC_assign; | |
200 precedence[TOKorass] = PREC_assign; | |
201 precedence[TOKxorass] = PREC_assign; | |
202 | |
203 precedence[TOKcomma] = PREC_expr; | |
204 } | |
205 | |
336 | 206 /************************************************************* |
207 * Given var, we need to get the | |
208 * right 'this' pointer if var is in an outer class, but our | |
209 * existing 'this' pointer is in an inner class. | |
210 * Input: | |
211 * e1 existing 'this' | |
212 * ad struct or class we need the correct 'this' for | |
213 * var the specific member of ad we're accessing | |
214 */ | |
215 | |
216 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, | |
217 Expression *e1, Declaration *var) | |
218 { | |
219 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars()); | |
220 L1: | |
221 Type *t = e1->type->toBasetype(); | |
222 //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars()); | |
223 | |
224 /* If e1 is not the 'this' pointer for ad | |
225 */ | |
226 if (ad && | |
227 !(t->ty == Tpointer && t->nextOf()->ty == Tstruct && | |
228 ((TypeStruct *)t->nextOf())->sym == ad) | |
229 && | |
230 !(t->ty == Tstruct && | |
231 ((TypeStruct *)t)->sym == ad) | |
232 ) | |
233 { | |
234 ClassDeclaration *cd = ad->isClassDeclaration(); | |
235 ClassDeclaration *tcd = t->isClassHandle(); | |
236 | |
237 /* e1 is the right this if ad is a base class of e1 | |
238 */ | |
239 if (!cd || !tcd || | |
240 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
241 ) | |
242 { | |
243 /* Only classes can be inner classes with an 'outer' | |
244 * member pointing to the enclosing class instance | |
245 */ | |
246 if (tcd && tcd->isNested()) | |
247 { /* e1 is the 'this' pointer for an inner class: tcd. | |
248 * Rewrite it as the 'this' pointer for the outer class. | |
249 */ | |
250 | |
251 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
252 e1->type = tcd->vthis->type; | |
253 // Do not call checkNestedRef() | |
254 //e1 = e1->semantic(sc); | |
255 | |
256 // Skip up over nested functions, and get the enclosing | |
257 // class type. | |
258 int n = 0; | |
259 Dsymbol *s; | |
260 for (s = tcd->toParent(); | |
261 s && s->isFuncDeclaration(); | |
262 s = s->toParent()) | |
263 { FuncDeclaration *f = s->isFuncDeclaration(); | |
264 if (f->vthis) | |
265 { | |
266 //printf("rewriting e1 to %s's this\n", f->toChars()); | |
267 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
|
268 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
269 // 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
|
270 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
|
271 |
336 | 272 e1 = new VarExp(loc, f->vthis); |
273 } | |
274 } | |
275 if (s && s->isClassDeclaration()) | |
276 { e1->type = s->isClassDeclaration()->type; | |
277 if (n > 1) | |
278 e1 = e1->semantic(sc); | |
279 } | |
280 else | |
281 e1 = e1->semantic(sc); | |
282 goto L1; | |
283 } | |
284 /* Can't find a path from e1 to ad | |
285 */ | |
286 e1->error("this for %s needs to be type %s not type %s", | |
287 var->toChars(), ad->toChars(), t->toChars()); | |
288 } | |
289 } | |
290 return e1; | |
291 } | |
292 | |
159 | 293 /***************************************** |
294 * Determine if 'this' is available. | |
295 * If it is, return the FuncDeclaration that has it. | |
296 */ | |
297 | |
298 FuncDeclaration *hasThis(Scope *sc) | |
299 { FuncDeclaration *fd; | |
300 FuncDeclaration *fdthis; | |
301 | |
302 //printf("hasThis()\n"); | |
303 fdthis = sc->parent->isFuncDeclaration(); | |
304 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
305 | |
306 // Go upwards until we find the enclosing member function | |
307 fd = fdthis; | |
308 while (1) | |
309 { | |
310 if (!fd) | |
311 { | |
312 goto Lno; | |
313 } | |
314 if (!fd->isNested()) | |
315 break; | |
316 | |
317 Dsymbol *parent = fd->parent; | |
318 while (parent) | |
319 { | |
320 TemplateInstance *ti = parent->isTemplateInstance(); | |
321 if (ti) | |
322 parent = ti->parent; | |
323 else | |
324 break; | |
325 } | |
326 | |
327 fd = fd->parent->isFuncDeclaration(); | |
328 } | |
329 | |
330 if (!fd->isThis()) | |
331 { //printf("test '%s'\n", fd->toChars()); | |
332 goto Lno; | |
333 } | |
334 | |
335 assert(fd->vthis); | |
336 return fd; | |
337 | |
338 Lno: | |
339 return NULL; // don't have 'this' available | |
340 } | |
341 | |
342 | |
343 /*************************************** | |
344 * Pull out any properties. | |
345 */ | |
346 | |
347 Expression *resolveProperties(Scope *sc, Expression *e) | |
348 { | |
349 //printf("resolveProperties(%s)\n", e->toChars()); | |
350 if (e->type) | |
351 { | |
352 Type *t = e->type->toBasetype(); | |
353 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
354 if (t->ty == Tfunction /*|| e->op == TOKoverloadset*/) |
159 | 355 { |
356 e = new CallExp(e->loc, e); | |
357 e = e->semantic(sc); | |
358 } | |
359 | |
360 /* Look for e being a lazy parameter; rewrite as delegate call | |
361 */ | |
362 else if (e->op == TOKvar) | |
363 { VarExp *ve = (VarExp *)e; | |
364 | |
365 if (ve->var->storage_class & STClazy) | |
366 { | |
367 e = new CallExp(e->loc, e); | |
368 e = e->semantic(sc); | |
369 } | |
370 } | |
371 | |
372 else if (e->op == TOKdotexp) | |
373 { | |
374 e->error("expression has no value"); | |
375 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
376 |
159 | 377 } |
378 return e; | |
379 } | |
380 | |
381 /****************************** | |
382 * Perform semantic() on an array of Expressions. | |
383 */ | |
384 | |
385 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
386 { | |
387 if (exps) | |
388 { | |
389 for (size_t i = 0; i < exps->dim; i++) | |
390 { Expression *e = (Expression *)exps->data[i]; | |
391 | |
392 e = e->semantic(sc); | |
393 exps->data[i] = (void *)e; | |
394 } | |
395 } | |
396 } | |
397 | |
510
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 /****************************** |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
400 * 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
403 #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
|
404 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
|
405 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
406 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
|
407 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
408 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
|
409 { 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
|
410 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
|
411 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
|
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 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
414 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
|
415 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
416 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
417 |
159 | 418 /**************************************** |
419 * Expand tuples. | |
420 */ | |
421 | |
422 void expandTuples(Expressions *exps) | |
423 { | |
424 //printf("expandTuples()\n"); | |
425 if (exps) | |
426 { | |
427 for (size_t i = 0; i < exps->dim; i++) | |
428 { Expression *arg = (Expression *)exps->data[i]; | |
429 if (!arg) | |
430 continue; | |
431 | |
432 // Look for tuple with 0 members | |
433 if (arg->op == TOKtype) | |
434 { TypeExp *e = (TypeExp *)arg; | |
435 if (e->type->toBasetype()->ty == Ttuple) | |
436 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
437 | |
438 if (!tt->arguments || tt->arguments->dim == 0) | |
439 { | |
440 exps->remove(i); | |
441 if (i == exps->dim) | |
442 return; | |
443 i--; | |
444 continue; | |
445 } | |
446 } | |
447 } | |
448 | |
449 // Inline expand all the tuples | |
450 while (arg->op == TOKtuple) | |
451 { TupleExp *te = (TupleExp *)arg; | |
452 | |
453 exps->remove(i); // remove arg | |
454 exps->insert(i, te->exps); // replace with tuple contents | |
455 if (i == exps->dim) | |
456 return; // empty tuple, no more arguments | |
457 arg = (Expression *)exps->data[i]; | |
458 } | |
459 } | |
460 } | |
461 } | |
462 | |
463 /**************************************** | |
464 * Preprocess arguments to function. | |
465 */ | |
466 | |
467 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps) | |
468 { | |
469 if (exps) | |
470 { | |
471 expandTuples(exps); | |
472 | |
473 for (size_t i = 0; i < exps->dim; i++) | |
474 { Expression *arg = (Expression *)exps->data[i]; | |
475 | |
476 if (!arg->type) | |
477 { | |
478 #ifdef DEBUG | |
479 if (!global.gag) | |
480 printf("1: \n"); | |
481 #endif | |
482 arg->error("%s is not an expression", arg->toChars()); | |
483 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
484 } | |
485 | |
486 arg = resolveProperties(sc, arg); | |
487 exps->data[i] = (void *) arg; | |
488 | |
489 //arg->rvalue(); | |
490 #if 0 | |
491 if (arg->type->ty == Tfunction) | |
492 { | |
493 arg = new AddrExp(arg->loc, arg); | |
494 arg = arg->semantic(sc); | |
495 exps->data[i] = (void *) arg; | |
496 } | |
497 #endif | |
498 } | |
499 } | |
500 } | |
501 | |
336 | 502 /********************************************* |
503 * Call copy constructor for struct value argument. | |
504 */ | |
505 #if DMDV2 | |
506 Expression *callCpCtor(Loc loc, Scope *sc, Expression *e) | |
507 { | |
508 Type *tb = e->type->toBasetype(); | |
509 assert(tb->ty == Tstruct); | |
510 StructDeclaration *sd = ((TypeStruct *)tb)->sym; | |
511 if (sd->cpctor) | |
512 { | |
513 /* Create a variable tmp, and replace the argument e with: | |
514 * (tmp = e),tmp | |
515 * and let AssignExp() handle the construction. | |
516 * This is not the most efficent, ideally tmp would be constructed | |
517 * directly onto the stack. | |
518 */ | |
519 Identifier *idtmp = Lexer::uniqueId("__tmp"); | |
520 VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e)); | |
521 Expression *ae = new DeclarationExp(loc, tmp); | |
522 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
523 e = e->semantic(sc); | |
524 } | |
525 return e; | |
526 } | |
527 #endif | |
159 | 528 |
529 /**************************************** | |
530 * Now that we know the exact type of the function we're calling, | |
531 * 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
|
532 * 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
|
533 * 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
|
534 * 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
|
535 * 4. add hidden _arguments[] argument |
159 | 536 */ |
537 | |
538 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) | |
539 { | |
540 unsigned n; | |
541 | |
542 //printf("functionArguments()\n"); | |
543 assert(arguments); | |
544 size_t nargs = arguments ? arguments->dim : 0; | |
545 size_t nparams = Argument::dim(tf->parameters); | |
546 | |
547 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
|
548 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 549 |
550 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
551 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
552 int done = 0; |
159 | 553 for (size_t i = 0; i < n; i++) |
554 { | |
555 Expression *arg; | |
556 | |
557 if (i < nargs) | |
558 arg = (Expression *)arguments->data[i]; | |
559 else | |
560 arg = NULL; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
561 Type *tb; |
159 | 562 |
563 if (i < nparams) | |
564 { | |
565 Argument *p = Argument::getNth(tf->parameters, i); | |
566 | |
567 if (!arg) | |
568 { | |
569 if (!p->defaultArg) | |
570 { | |
571 if (tf->varargs == 2 && i + 1 == nparams) | |
572 goto L2; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
573 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 574 break; |
575 } | |
336 | 576 arg = p->defaultArg; |
577 #if DMDV2 | |
578 if (arg->op == TOKdefault) | |
579 { DefaultInitExp *de = (DefaultInitExp *)arg; | |
580 arg = de->resolve(loc, sc); | |
581 } | |
582 else | |
583 #endif | |
584 arg = arg->copy(); | |
159 | 585 arguments->push(arg); |
586 nargs++; | |
587 } | |
588 | |
589 if (tf->varargs == 2 && i + 1 == nparams) | |
590 { | |
591 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
592 if (arg->implicitConvTo(p->type)) | |
593 { | |
594 if (nargs != nparams) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
595 error(loc, "expected %zu arguments, not %zu", nparams, nargs); |
159 | 596 goto L1; |
597 } | |
598 L2: | |
599 Type *tb = p->type->toBasetype(); | |
600 Type *tret = p->isLazyArray(); | |
601 switch (tb->ty) | |
602 { | |
603 case Tsarray: | |
604 case Tarray: | |
605 { // Create a static array variable v of type arg->type | |
606 #ifdef IN_GCC | |
607 /* GCC 4.0 does not like zero length arrays used like | |
608 this; pass a null array value instead. Could also | |
609 just make a one-element array. */ | |
610 if (nargs - i == 0) | |
611 { | |
612 arg = new NullExp(loc); | |
613 break; | |
614 } | |
615 #endif | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
616 Identifier *id = Lexer::uniqueId("__arrayArg"); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
617 Type *t = new TypeSArray(((TypeArray *)tb)->next, new IntegerExp(nargs - i)); |
159 | 618 t = t->semantic(loc, sc); |
619 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
620 v->semantic(sc); | |
621 v->parent = sc->parent; | |
622 //sc->insert(v); | |
623 | |
624 Expression *c = new DeclarationExp(0, v); | |
625 c->type = v->type; | |
626 | |
627 for (size_t u = i; u < nargs; u++) | |
628 { Expression *a = (Expression *)arguments->data[u]; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
629 if (tret && !((TypeArray *)tb)->next->equals(a->type)) |
159 | 630 a = a->toDelegate(sc, tret); |
631 | |
632 Expression *e = new VarExp(loc, v); | |
633 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
|
634 AssignExp *ae = new AssignExp(loc, e, a); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
635 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
636 ae->op = TOKconstruct; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
637 #endif |
159 | 638 if (c) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
639 c = new CommaExp(loc, c, ae); |
159 | 640 else |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
641 c = ae; |
159 | 642 } |
643 arg = new VarExp(loc, v); | |
644 if (c) | |
645 arg = new CommaExp(loc, c, arg); | |
646 break; | |
647 } | |
648 case Tclass: | |
649 { /* Set arg to be: | |
650 * new Tclass(arg0, arg1, ..., argn) | |
651 */ | |
652 Expressions *args = new Expressions(); | |
653 args->setDim(nargs - i); | |
654 for (size_t u = i; u < nargs; u++) | |
655 args->data[u - i] = arguments->data[u]; | |
656 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
657 break; | |
658 } | |
659 default: | |
660 if (!arg) | |
661 { error(loc, "not enough arguments"); | |
662 return; | |
663 } | |
664 break; | |
665 } | |
666 arg = arg->semantic(sc); | |
667 //printf("\targ = '%s'\n", arg->toChars()); | |
668 arguments->setDim(i + 1); | |
669 done = 1; | |
670 } | |
671 | |
672 L1: | |
673 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
674 arg = arg->implicitCastTo(sc, p->type); | |
675 if (p->storageClass & (STCout | STCref)) | |
676 { | |
677 // BUG: should check that argument to ref is type 'invariant' | |
678 // BUG: assignments to ref should also be type 'invariant' | |
679 arg = arg->modifiableLvalue(sc, arg); | |
680 | |
681 //if (arg->op == TOKslice) | |
682 //arg->error("cannot modify slice %s", arg->toChars()); | |
683 } | |
684 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
685 // 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
|
686 #if !IN_LLVM |
159 | 687 // Convert static arrays to pointers |
688 tb = arg->type->toBasetype(); | |
689 if (tb->ty == Tsarray) | |
690 { | |
691 arg = arg->checkToPointer(); | |
692 } | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
693 #endif |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
694 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
695 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
|
696 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
697 arg = callCpCtor(loc, sc, arg); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
698 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
699 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
700 |
159 | 701 // Convert lazy argument to a delegate |
702 if (p->storageClass & STClazy) | |
703 { | |
704 arg = arg->toDelegate(sc, p->type); | |
705 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
706 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
707 /* 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
|
708 * function. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
709 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
710 if (!tf->parameterEscapes(p)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
711 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
712 /* 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
|
713 * 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
|
714 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
715 if (arg->op == TOKfunction) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
716 { FuncExp *fe = (FuncExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
717 fe->fd->tookAddressOf = 0; |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
720 /* 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
|
721 * 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
|
722 * 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
|
723 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
724 else if (arg->op == TOKdelegate) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
725 { DelegateExp *de = (DelegateExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
726 if (de->e1->op == TOKvar) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
727 { VarExp *ve = (VarExp *)de->e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
728 FuncDeclaration *f = ve->var->isFuncDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
729 if (f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
730 { f->tookAddressOf--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
731 //printf("tookAddressOf = %d\n", f->tookAddressOf); |
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 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
736 #endif |
159 | 737 } |
738 else | |
739 { | |
740 | |
741 // 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
|
742 // 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
|
743 if (tf->linkage != LINKd && tf->linkage != LINKintrinsic) |
159 | 744 { |
745 // Promote bytes, words, etc., to ints | |
746 arg = arg->integralPromotions(sc); | |
747 | |
748 // Promote floats to doubles | |
749 switch (arg->type->ty) | |
750 { | |
751 case Tfloat32: | |
752 arg = arg->castTo(sc, Type::tfloat64); | |
753 break; | |
754 | |
755 case Timaginary32: | |
756 arg = arg->castTo(sc, Type::timaginary64); | |
757 break; | |
758 } | |
759 } | |
760 | |
761 // Convert static arrays to dynamic arrays | |
762 tb = arg->type->toBasetype(); | |
763 if (tb->ty == Tsarray) | |
764 { TypeSArray *ts = (TypeSArray *)tb; | |
336 | 765 Type *ta = ts->next->arrayOf(); |
159 | 766 if (ts->size(arg->loc) == 0) |
767 { arg = new NullExp(arg->loc); | |
768 arg->type = ta; | |
769 } | |
770 else | |
771 arg = arg->castTo(sc, ta); | |
772 } | |
336 | 773 #if DMDV2 |
774 if (tb->ty == Tstruct) | |
775 { | |
776 arg = callCpCtor(loc, sc, arg); | |
777 } | |
778 | |
779 // Give error for overloaded function addresses | |
780 if (arg->op == TOKsymoff) | |
781 { SymOffExp *se = (SymOffExp *)arg; | |
782 if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique()) | |
783 arg->error("function %s is overloaded", arg->toChars()); | |
784 } | |
785 #endif | |
159 | 786 arg->rvalue(); |
787 } | |
788 arg = arg->optimize(WANTvalue); | |
789 arguments->data[i] = (void *) arg; | |
790 if (done) | |
791 break; | |
792 } | |
793 | |
794 #if !IN_LLVM | |
795 // If D linkage and variadic, add _arguments[] as first argument | |
796 if (tf->linkage == LINKd && tf->varargs == 1) | |
797 { | |
798 Expression *e; | |
799 | |
800 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], | |
801 arguments->dim - nparams); | |
802 arguments->insert(0, e); | |
803 } | |
804 #endif | |
805 } | |
806 | |
807 /************************************************** | |
808 * Write expression out to buf, but wrap it | |
809 * in ( ) if its precedence is less than pr. | |
810 */ | |
811 | |
812 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
813 { | |
336 | 814 //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
|
815 if (precedence[e->op] < pr || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
816 /* 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
|
817 * They must be parenthesized. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
818 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
819 (pr == PREC_rel && precedence[e->op] == pr)) |
159 | 820 { |
821 buf->writeByte('('); | |
822 e->toCBuffer(buf, hgs); | |
823 buf->writeByte(')'); | |
824 } | |
825 else | |
826 e->toCBuffer(buf, hgs); | |
827 } | |
828 | |
829 /************************************************** | |
830 * Write out argument list to buf. | |
831 */ | |
832 | |
833 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
834 { | |
835 if (arguments) | |
836 { | |
837 for (size_t i = 0; i < arguments->dim; i++) | |
838 { Expression *arg = (Expression *)arguments->data[i]; | |
839 | |
840 if (arg) | |
841 { if (i) | |
842 buf->writeByte(','); | |
843 expToCBuffer(buf, hgs, arg, PREC_assign); | |
844 } | |
845 } | |
846 } | |
847 } | |
848 | |
849 /************************************************** | |
850 * Write out argument types to buf. | |
851 */ | |
852 | |
853 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
854 { | |
855 if (arguments) | |
856 { OutBuffer argbuf; | |
857 | |
858 for (size_t i = 0; i < arguments->dim; i++) | |
859 { Expression *arg = (Expression *)arguments->data[i]; | |
860 | |
861 if (i) | |
862 buf->writeByte(','); | |
863 argbuf.reset(); | |
864 arg->type->toCBuffer2(&argbuf, hgs, 0); | |
865 buf->write(&argbuf); | |
866 } | |
867 } | |
868 } | |
869 | |
870 /******************************** Expression **************************/ | |
871 | |
872 Expression::Expression(Loc loc, enum TOK op, int size) | |
873 : loc(loc) | |
874 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
875 //printf("Expression::Expression(op = %d) this = %p\n", op, this); |
159 | 876 this->loc = loc; |
877 this->op = op; | |
878 this->size = size; | |
879 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
|
880 |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
881 #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
|
882 cachedLvalue = NULL; |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
883 #endif |
159 | 884 } |
885 | |
886 Expression *Expression::syntaxCopy() | |
887 { | |
888 //printf("Expression::syntaxCopy()\n"); | |
889 //dump(0); | |
890 return copy(); | |
891 } | |
892 | |
893 /********************************* | |
894 * Does *not* do a deep copy. | |
895 */ | |
896 | |
897 Expression *Expression::copy() | |
898 { | |
899 Expression *e; | |
900 if (!size) | |
901 { | |
902 #ifdef DEBUG | |
903 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
904 printf("op = %d\n", op); | |
905 dump(0); | |
906 #endif | |
907 assert(0); | |
908 } | |
909 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
|
910 //printf("Expression::copy(op = %d) e = %p\n", op, e); |
159 | 911 return (Expression *)memcpy(e, this, size); |
912 } | |
913 | |
914 /************************** | |
915 * Semantically analyze Expression. | |
916 * Determine types, fold constants, etc. | |
917 */ | |
918 | |
919 Expression *Expression::semantic(Scope *sc) | |
920 { | |
921 #if LOGSEMANTIC | |
336 | 922 printf("Expression::semantic() %s\n", toChars()); |
159 | 923 #endif |
924 if (type) | |
925 type = type->semantic(loc, sc); | |
926 else | |
927 type = Type::tvoid; | |
928 return this; | |
929 } | |
930 | |
931 void Expression::print() | |
932 { | |
933 fprintf(stdmsg, "%s\n", toChars()); | |
934 fflush(stdmsg); | |
935 } | |
936 | |
937 char *Expression::toChars() | |
938 { OutBuffer *buf; | |
939 HdrGenState hgs; | |
940 | |
941 memset(&hgs, 0, sizeof(hgs)); | |
942 buf = new OutBuffer(); | |
943 toCBuffer(buf, &hgs); | |
944 return buf->toChars(); | |
945 } | |
946 | |
947 void Expression::error(const char *format, ...) | |
948 { | |
949 va_list ap; | |
950 va_start(ap, format); | |
951 ::verror(loc, format, ap); | |
952 va_end( ap ); | |
953 } | |
954 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
955 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
|
956 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
957 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
|
958 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
959 va_list ap; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
960 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
|
961 ::vwarning(loc, format, ap); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
962 va_end( ap ); |
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 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
965 |
159 | 966 void Expression::rvalue() |
967 { | |
968 if (type && type->toBasetype()->ty == Tvoid) | |
969 { error("expression %s is void and has no value", toChars()); | |
970 #if 0 | |
971 dump(0); | |
972 halt(); | |
973 #endif | |
336 | 974 type = Type::tint32; |
159 | 975 } |
976 } | |
977 | |
978 Expression *Expression::combine(Expression *e1, Expression *e2) | |
979 { | |
980 if (e1) | |
981 { | |
982 if (e2) | |
983 { | |
984 e1 = new CommaExp(e1->loc, e1, e2); | |
985 e1->type = e2->type; | |
986 } | |
987 } | |
988 else | |
989 e1 = e2; | |
990 return e1; | |
991 } | |
992 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
993 dinteger_t Expression::toInteger() |
159 | 994 { |
995 //printf("Expression %s\n", Token::toChars(op)); | |
996 error("Integer constant expression expected instead of %s", toChars()); | |
997 return 0; | |
998 } | |
999 | |
1000 uinteger_t Expression::toUInteger() | |
1001 { | |
1002 //printf("Expression %s\n", Token::toChars(op)); | |
1003 return (uinteger_t)toInteger(); | |
1004 } | |
1005 | |
1006 real_t Expression::toReal() | |
1007 { | |
1008 error("Floating point constant expression expected instead of %s", toChars()); | |
1009 return 0; | |
1010 } | |
1011 | |
1012 real_t Expression::toImaginary() | |
1013 { | |
1014 error("Floating point constant expression expected instead of %s", toChars()); | |
1015 return 0; | |
1016 } | |
1017 | |
1018 complex_t Expression::toComplex() | |
1019 { | |
1020 error("Floating point constant expression expected instead of %s", toChars()); | |
1021 #ifdef IN_GCC | |
1022 return complex_t(real_t(0)); // %% nicer | |
1023 #else | |
1024 return 0; | |
1025 #endif | |
1026 } | |
1027 | |
1028 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1029 { | |
1030 buf->writestring(Token::toChars(op)); | |
1031 } | |
1032 | |
1033 void Expression::toMangleBuffer(OutBuffer *buf) | |
1034 { | |
1035 error("expression %s is not a valid template value argument", toChars()); | |
1036 } | |
1037 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1038 /*************************************** |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1039 * Return !=0 if expression is an lvalue. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1040 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1041 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1042 int Expression::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1043 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1044 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1045 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1046 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1047 |
159 | 1048 /******************************* |
1049 * Give error if we're not an lvalue. | |
1050 * If we can, convert expression to be an lvalue. | |
1051 */ | |
1052 | |
1053 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
1054 { | |
1055 if (!e) | |
1056 e = this; | |
1057 else if (!loc.filename) | |
1058 loc = e->loc; | |
1059 error("%s is not an lvalue", e->toChars()); | |
1060 return this; | |
1061 } | |
1062 | |
1063 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
1064 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1065 //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
|
1066 |
159 | 1067 // 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
|
1068 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1069 if (type && (!type->isMutable() || !type->isAssignable())) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1070 error("%s is not mutable", e->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1071 #endif |
159 | 1072 return toLvalue(sc, e); |
1073 } | |
1074 | |
1075 /************************************ | |
1076 * Detect cases where pointers to the stack can 'escape' the | |
1077 * lifetime of the stack frame. | |
1078 */ | |
1079 | |
1080 void Expression::checkEscape() | |
1081 { | |
1082 } | |
1083 | |
1084 void Expression::checkScalar() | |
1085 { | |
1086 if (!type->isscalar()) | |
1087 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
1088 } | |
1089 | |
1090 void Expression::checkNoBool() | |
1091 { | |
1092 if (type->toBasetype()->ty == Tbool) | |
1093 error("operation not allowed on bool '%s'", toChars()); | |
1094 } | |
1095 | |
1096 Expression *Expression::checkIntegral() | |
1097 { | |
1098 if (!type->isintegral()) | |
1099 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
1100 return new IntegerExp(0); | |
1101 } | |
1102 return this; | |
1103 } | |
1104 | |
1105 Expression *Expression::checkArithmetic() | |
1106 { | |
1107 if (!type->isintegral() && !type->isfloating()) | |
1108 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); | |
1109 return new IntegerExp(0); | |
1110 } | |
1111 return this; | |
1112 } | |
1113 | |
1114 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
1115 { | |
1116 s->checkDeprecated(loc, sc); | |
1117 } | |
1118 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1119 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1120 void Expression::checkPurity(Scope *sc, FuncDeclaration *f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1121 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1122 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
|
1123 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
|
1124 sc->func->toChars(), f->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1125 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1126 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1127 |
159 | 1128 /******************************** |
1129 * Check for expressions that have no use. | |
1130 * Input: | |
1131 * flag 0 not going to use the result, so issue error message if no | |
1132 * side effects | |
1133 * 1 the result of the expression is used, but still check | |
1134 * for useless subexpressions | |
1135 * 2 do not issue error messages, just return !=0 if expression | |
1136 * has side effects | |
1137 */ | |
1138 | |
1139 int Expression::checkSideEffect(int flag) | |
1140 { | |
1141 if (flag == 0) | |
1142 { if (op == TOKimport) | |
1143 { | |
1144 error("%s has no effect", toChars()); | |
1145 } | |
1146 else | |
1147 error("%s has no effect in expression (%s)", | |
1148 Token::toChars(op), toChars()); | |
1149 } | |
1150 return 0; | |
1151 } | |
1152 | |
1153 /***************************** | |
1154 * Check that expression can be tested for true or false. | |
1155 */ | |
1156 | |
1157 Expression *Expression::checkToBoolean() | |
1158 { | |
1159 // Default is 'yes' - do nothing | |
1160 | |
1161 #ifdef DEBUG | |
1162 if (!type) | |
1163 dump(0); | |
1164 #endif | |
1165 | |
1166 if (!type->checkBoolean()) | |
1167 { | |
1168 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
1169 } | |
1170 return this; | |
1171 } | |
1172 | |
1173 /**************************** | |
1174 */ | |
1175 | |
1176 Expression *Expression::checkToPointer() | |
1177 { | |
1178 Expression *e; | |
1179 Type *tb; | |
1180 | |
1181 //printf("Expression::checkToPointer()\n"); | |
1182 e = this; | |
1183 | |
1184 // If C static array, convert to pointer | |
1185 tb = type->toBasetype(); | |
1186 if (tb->ty == Tsarray) | |
1187 { TypeSArray *ts = (TypeSArray *)tb; | |
1188 if (ts->size(loc) == 0) | |
1189 e = new NullExp(loc); | |
1190 else | |
1191 e = new AddrExp(loc, this); | |
336 | 1192 e->type = ts->next->pointerTo(); |
159 | 1193 } |
1194 return e; | |
1195 } | |
1196 | |
1197 /****************************** | |
1198 * Take address of expression. | |
1199 */ | |
1200 | |
1201 Expression *Expression::addressOf(Scope *sc) | |
1202 { | |
1203 Expression *e; | |
1204 | |
1205 //printf("Expression::addressOf()\n"); | |
1206 e = toLvalue(sc, NULL); | |
1207 e = new AddrExp(loc, e); | |
1208 e->type = type->pointerTo(); | |
1209 return e; | |
1210 } | |
1211 | |
1212 /****************************** | |
1213 * If this is a reference, dereference it. | |
1214 */ | |
1215 | |
1216 Expression *Expression::deref() | |
1217 { | |
1218 //printf("Expression::deref()\n"); | |
1219 if (type->ty == Treference) | |
1220 { Expression *e; | |
1221 | |
1222 e = new PtrExp(loc, this); | |
336 | 1223 e->type = ((TypeReference *)type)->next; |
159 | 1224 return e; |
1225 } | |
1226 return this; | |
1227 } | |
1228 | |
1229 /******************************** | |
1230 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
1231 */ | |
1232 | |
1233 int Expression::isBool(int result) | |
1234 { | |
1235 return FALSE; | |
1236 } | |
1237 | |
1238 /******************************** | |
1239 * Does this expression result in either a 1 or a 0? | |
1240 */ | |
1241 | |
1242 int Expression::isBit() | |
1243 { | |
1244 return FALSE; | |
1245 } | |
1246 | |
336 | 1247 /******************************** |
1248 * Can this expression throw an exception? | |
1249 * Valid only after semantic() pass. | |
1250 */ | |
1251 | |
1252 int Expression::canThrow() | |
1253 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1254 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1255 return FALSE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1256 #else |
336 | 1257 return TRUE; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1258 #endif |
336 | 1259 } |
1260 | |
1261 | |
1262 | |
159 | 1263 Expressions *Expression::arraySyntaxCopy(Expressions *exps) |
1264 { Expressions *a = NULL; | |
1265 | |
1266 if (exps) | |
1267 { | |
1268 a = new Expressions(); | |
1269 a->setDim(exps->dim); | |
1270 for (int i = 0; i < a->dim; i++) | |
1271 { Expression *e = (Expression *)exps->data[i]; | |
1272 | |
1273 e = e->syntaxCopy(); | |
1274 a->data[i] = e; | |
1275 } | |
1276 } | |
1277 return a; | |
1278 } | |
1279 | |
1280 /******************************** IntegerExp **************************/ | |
1281 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1282 IntegerExp::IntegerExp(Loc loc, dinteger_t value, Type *type) |
159 | 1283 : Expression(loc, TOKint64, sizeof(IntegerExp)) |
1284 { | |
1285 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1286 if (type && !type->isscalar()) | |
1287 { | |
336 | 1288 //printf("%s, loc = %d\n", toChars(), loc.linnum); |
159 | 1289 error("integral constant must be scalar type, not %s", type->toChars()); |
1290 type = Type::terror; | |
1291 } | |
1292 this->type = type; | |
1293 this->value = value; | |
1294 } | |
1295 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1296 IntegerExp::IntegerExp(dinteger_t value) |
159 | 1297 : Expression(0, TOKint64, sizeof(IntegerExp)) |
1298 { | |
1299 this->type = Type::tint32; | |
1300 this->value = value; | |
1301 } | |
1302 | |
1303 int IntegerExp::equals(Object *o) | |
1304 { IntegerExp *ne; | |
1305 | |
1306 if (this == o || | |
1307 (((Expression *)o)->op == TOKint64 && | |
1308 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1309 value == ne->value)) | |
1310 return 1; | |
1311 return 0; | |
1312 } | |
1313 | |
1314 char *IntegerExp::toChars() | |
1315 { | |
1316 #if 1 | |
1317 return Expression::toChars(); | |
1318 #else | |
1319 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
|
1320 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1321 sprintf(buffer, "%jd", value); |
159 | 1322 return buffer; |
1323 #endif | |
1324 } | |
1325 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1326 dinteger_t IntegerExp::toInteger() |
159 | 1327 { Type *t; |
1328 | |
1329 t = type; | |
1330 while (t) | |
1331 { | |
1332 switch (t->ty) | |
1333 { | |
1334 case Tbit: | |
1335 case Tbool: value = (value != 0); break; | |
1336 case Tint8: value = (d_int8) value; break; | |
1337 case Tchar: | |
1338 case Tuns8: value = (d_uns8) value; break; | |
1339 case Tint16: value = (d_int16) value; break; | |
1340 case Twchar: | |
1341 case Tuns16: value = (d_uns16) value; break; | |
1342 case Tint32: value = (d_int32) value; break; | |
1343 case Tdchar: | |
1344 case Tuns32: value = (d_uns32) value; break; | |
1345 case Tint64: value = (d_int64) value; break; | |
1346 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
|
1347 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
|
1348 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
|
1349 value = (d_uns32) value; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1350 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
|
1351 value = (d_uns64) value; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1352 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1353 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
|
1354 break; |
159 | 1355 |
1356 case Tenum: | |
1357 { | |
1358 TypeEnum *te = (TypeEnum *)t; | |
1359 t = te->sym->memtype; | |
1360 continue; | |
1361 } | |
1362 | |
1363 case Ttypedef: | |
1364 { | |
1365 TypeTypedef *tt = (TypeTypedef *)t; | |
1366 t = tt->sym->basetype; | |
1367 continue; | |
1368 } | |
1369 | |
1370 default: | |
336 | 1371 /* This can happen if errors, such as |
1372 * the type is painted on like in fromConstInitializer(). | |
1373 */ | |
1374 if (!global.errors) | |
1375 { type->print(); | |
1376 assert(0); | |
1377 } | |
159 | 1378 break; |
1379 } | |
1380 break; | |
1381 } | |
1382 return value; | |
1383 } | |
1384 | |
1385 real_t IntegerExp::toReal() | |
1386 { | |
1387 Type *t; | |
1388 | |
1389 toInteger(); | |
1390 t = type->toBasetype(); | |
1391 if (t->ty == Tuns64) | |
1392 return (real_t)(d_uns64)value; | |
1393 else | |
1394 return (real_t)(d_int64)value; | |
1395 } | |
1396 | |
1397 real_t IntegerExp::toImaginary() | |
1398 { | |
1399 return (real_t) 0; | |
1400 } | |
1401 | |
1402 complex_t IntegerExp::toComplex() | |
1403 { | |
1404 return toReal(); | |
1405 } | |
1406 | |
1407 int IntegerExp::isBool(int result) | |
1408 { | |
1409 return result ? value != 0 : value == 0; | |
1410 } | |
1411 | |
1412 Expression *IntegerExp::semantic(Scope *sc) | |
1413 { | |
1414 if (!type) | |
1415 { | |
1416 // 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
|
1417 dinteger_t number = value; |
159 | 1418 |
1419 if (number & 0x8000000000000000LL) | |
1420 type = Type::tuns64; | |
1421 else if (number & 0xFFFFFFFF80000000LL) | |
1422 type = Type::tint64; | |
1423 else | |
1424 type = Type::tint32; | |
1425 } | |
1426 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
|
1427 { 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
|
1428 type = type->semantic(loc, sc); |
159 | 1429 } |
1430 return this; | |
1431 } | |
1432 | |
1433 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1434 { | |
1435 if (!e) | |
1436 e = this; | |
1437 else if (!loc.filename) | |
1438 loc = e->loc; | |
1439 e->error("constant %s is not an lvalue", e->toChars()); | |
1440 return this; | |
1441 } | |
1442 | |
1443 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1444 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1445 dinteger_t v = toInteger(); |
159 | 1446 |
1447 if (type) | |
1448 { Type *t = type; | |
1449 | |
1450 L1: | |
1451 switch (t->ty) | |
1452 { | |
1453 case Tenum: | |
1454 { TypeEnum *te = (TypeEnum *)t; | |
1455 buf->printf("cast(%s)", te->sym->toChars()); | |
1456 t = te->sym->memtype; | |
1457 goto L1; | |
1458 } | |
1459 | |
1460 case Ttypedef: | |
1461 { TypeTypedef *tt = (TypeTypedef *)t; | |
1462 buf->printf("cast(%s)", tt->sym->toChars()); | |
1463 t = tt->sym->basetype; | |
1464 goto L1; | |
1465 } | |
1466 | |
1467 case Twchar: // BUG: need to cast(wchar) | |
1468 case Tdchar: // BUG: need to cast(dchar) | |
1469 if ((uinteger_t)v > 0xFF) | |
1470 { | |
1471 buf->printf("'\\U%08x'", v); | |
1472 break; | |
1473 } | |
1474 case Tchar: | |
1475 if (v == '\'') | |
1476 buf->writestring("'\\''"); | |
1477 else if (isprint(v) && v != '\\') | |
1478 buf->printf("'%c'", (int)v); | |
1479 else | |
1480 buf->printf("'\\x%02x'", (int)v); | |
1481 break; | |
1482 | |
1483 case Tint8: | |
1484 buf->writestring("cast(byte)"); | |
1485 goto L2; | |
1486 | |
1487 case Tint16: | |
1488 buf->writestring("cast(short)"); | |
1489 goto L2; | |
1490 | |
1491 case Tint32: | |
1492 L2: | |
1493 buf->printf("%d", (int)v); | |
1494 break; | |
1495 | |
1496 case Tuns8: | |
1497 buf->writestring("cast(ubyte)"); | |
1498 goto L3; | |
1499 | |
1500 case Tuns16: | |
1501 buf->writestring("cast(ushort)"); | |
1502 goto L3; | |
1503 | |
1504 case Tuns32: | |
1505 L3: | |
1506 buf->printf("%du", (unsigned)v); | |
1507 break; | |
1508 | |
1509 case Tint64: | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1510 buf->printf("%jdL", v); |
159 | 1511 break; |
1512 | |
1513 case Tuns64: | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1514 L4: |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1515 buf->printf("%juLU", v); |
159 | 1516 break; |
1517 | |
1518 case Tbit: | |
1519 case Tbool: | |
1520 buf->writestring((char *)(v ? "true" : "false")); | |
1521 break; | |
1522 | |
1523 case Tpointer: | |
1524 buf->writestring("cast("); | |
1525 buf->writestring(t->toChars()); | |
1526 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
|
1527 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
|
1528 goto L3; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1529 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
|
1530 goto L4; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1531 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1532 assert(0); |
159 | 1533 |
1534 default: | |
336 | 1535 /* This can happen if errors, such as |
1536 * the type is painted on like in fromConstInitializer(). | |
1537 */ | |
1538 if (!global.errors) | |
1539 { | |
159 | 1540 #ifdef DEBUG |
336 | 1541 t->print(); |
159 | 1542 #endif |
336 | 1543 assert(0); |
1544 } | |
1545 break; | |
159 | 1546 } |
1547 } | |
1548 else if (v & 0x8000000000000000LL) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1549 buf->printf("0x%jx", v); |
159 | 1550 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1551 buf->printf("%jd", v); |
159 | 1552 } |
1553 | |
1554 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1555 { | |
1556 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
|
1557 buf->printf("N%jd", -value); |
159 | 1558 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1559 buf->printf("%jd", value); |
159 | 1560 } |
1561 | |
1562 /******************************** RealExp **************************/ | |
1563 | |
1564 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1565 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1566 { | |
1567 //printf("RealExp::RealExp(%Lg)\n", value); | |
1568 this->value = value; | |
1569 this->type = type; | |
1570 } | |
1571 | |
1572 char *RealExp::toChars() | |
1573 { | |
1574 char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1575 | |
1576 #ifdef IN_GCC | |
1577 value.format(buffer, sizeof(buffer)); | |
1578 if (type->isimaginary()) | |
1579 strcat(buffer, "i"); | |
1580 #else | |
1581 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1582 #endif | |
1583 assert(strlen(buffer) < sizeof(buffer)); | |
1584 return mem.strdup(buffer); | |
1585 } | |
1586 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1587 dinteger_t RealExp::toInteger() |
159 | 1588 { |
1589 #ifdef IN_GCC | |
1590 return toReal().toInt(); | |
1591 #else | |
1592 return (sinteger_t) toReal(); | |
1593 #endif | |
1594 } | |
1595 | |
1596 uinteger_t RealExp::toUInteger() | |
1597 { | |
1598 #ifdef IN_GCC | |
1599 return (uinteger_t) toReal().toInt(); | |
1600 #else | |
1601 return (uinteger_t) toReal(); | |
1602 #endif | |
1603 } | |
1604 | |
1605 real_t RealExp::toReal() | |
1606 { | |
1607 return type->isreal() ? value : 0; | |
1608 } | |
1609 | |
1610 real_t RealExp::toImaginary() | |
1611 { | |
1612 return type->isreal() ? 0 : value; | |
1613 } | |
1614 | |
1615 complex_t RealExp::toComplex() | |
1616 { | |
1617 #ifdef __DMC__ | |
1618 return toReal() + toImaginary() * I; | |
1619 #else | |
1620 return complex_t(toReal(), toImaginary()); | |
1621 #endif | |
1622 } | |
1623 | |
1624 /******************************** | |
1625 * Test to see if two reals are the same. | |
1626 * Regard NaN's as equivalent. | |
1627 * Regard +0 and -0 as different. | |
1628 */ | |
1629 | |
1630 int RealEquals(real_t x1, real_t x2) | |
1631 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1632 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1633 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
|
1634 #else |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1635 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
|
1636 (isnan(x1) && isnan(x2)) || |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1637 #endif |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1638 // 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
|
1639 (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
|
1640 // 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
|
1641 (x1 != 0. && x1 == x2); |
159 | 1642 } |
1643 | |
1644 int RealExp::equals(Object *o) | |
1645 { RealExp *ne; | |
1646 | |
1647 if (this == o || | |
1648 (((Expression *)o)->op == TOKfloat64 && | |
1649 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1650 RealEquals(value, ne->value) | |
1651 ) | |
1652 ) | |
1653 return 1; | |
1654 return 0; | |
1655 } | |
1656 | |
1657 Expression *RealExp::semantic(Scope *sc) | |
1658 { | |
1659 if (!type) | |
1660 type = Type::tfloat64; | |
1661 else | |
1662 type = type->semantic(loc, sc); | |
1663 return this; | |
1664 } | |
1665 | |
1666 int RealExp::isBool(int result) | |
1667 { | |
1668 #ifdef IN_GCC | |
1669 return result ? (! value.isZero()) : (value.isZero()); | |
1670 #else | |
1671 return result ? (value != 0) | |
1672 : (value == 0); | |
1673 #endif | |
1674 } | |
1675 | |
1676 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1677 { | |
1678 /* In order to get an exact representation, try converting it | |
1679 * to decimal then back again. If it matches, use it. | |
1680 * If it doesn't, fall back to hex, which is | |
1681 * always exact. | |
1682 */ | |
1683 char buffer[25]; | |
1684 sprintf(buffer, "%Lg", value); | |
1685 assert(strlen(buffer) < sizeof(buffer)); | |
1686 #if _WIN32 && __DMC__ | |
1687 char *save = __locale_decpoint; | |
1688 __locale_decpoint = "."; | |
1689 real_t r = strtold(buffer, NULL); | |
1690 __locale_decpoint = save; | |
1691 #else | |
1692 real_t r = strtold(buffer, NULL); | |
1693 #endif | |
1694 if (r == value) // if exact duplication | |
1695 buf->writestring(buffer); | |
1696 else | |
1697 buf->printf("%La", value); // ensure exact duplication | |
1698 | |
1699 if (type) | |
1700 { | |
1701 Type *t = type->toBasetype(); | |
1702 switch (t->ty) | |
1703 { | |
1704 case Tfloat32: | |
1705 case Timaginary32: | |
1706 case Tcomplex32: | |
1707 buf->writeByte('F'); | |
1708 break; | |
1709 | |
1710 case Tfloat80: | |
1711 case Timaginary80: | |
1712 case Tcomplex80: | |
1713 buf->writeByte('L'); | |
1714 break; | |
1715 | |
1716 default: | |
1717 break; | |
1718 } | |
1719 if (t->isimaginary()) | |
1720 buf->writeByte('i'); | |
1721 } | |
1722 } | |
1723 | |
1724 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1725 { | |
1726 floatToBuffer(buf, type, value); | |
1727 } | |
1728 | |
1729 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1730 { | |
1731 /* Rely on %A to get portable mangling. | |
1732 * Must munge result to get only identifier characters. | |
1733 * | |
1734 * Possible values from %A => mangled result | |
1735 * NAN => NAN | |
1736 * -INF => NINF | |
1737 * INF => INF | |
1738 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1739 * 0X1.9P+2 => 19P2 | |
1740 */ | |
1741 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1742 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1743 if (__inline_isnan(value)) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1744 #else |
159 | 1745 if (isnan(value)) |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1746 #endif |
159 | 1747 buf->writestring("NAN"); // no -NAN bugs |
1748 else | |
1749 { | |
1750 char buffer[32]; | |
1751 int n = sprintf(buffer, "%LA", value); | |
1752 assert(n > 0 && n < sizeof(buffer)); | |
1753 for (int i = 0; i < n; i++) | |
1754 { char c = buffer[i]; | |
1755 | |
1756 switch (c) | |
1757 { | |
1758 case '-': | |
1759 buf->writeByte('N'); | |
1760 break; | |
1761 | |
1762 case '+': | |
1763 case 'X': | |
1764 case '.': | |
1765 break; | |
1766 | |
1767 case '0': | |
1768 if (i < 2) | |
1769 break; // skip leading 0X | |
1770 default: | |
1771 buf->writeByte(c); | |
1772 break; | |
1773 } | |
1774 } | |
1775 } | |
1776 } | |
1777 | |
1778 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1779 { | |
1780 buf->writeByte('e'); | |
1781 realToMangleBuffer(buf, value); | |
1782 } | |
1783 | |
1784 | |
1785 /******************************** ComplexExp **************************/ | |
1786 | |
1787 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1788 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1789 { | |
1790 this->value = value; | |
1791 this->type = type; | |
1792 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1793 } | |
1794 | |
1795 char *ComplexExp::toChars() | |
1796 { | |
1797 char buffer[sizeof(value) * 3 + 8 + 1]; | |
1798 | |
1799 #ifdef IN_GCC | |
1800 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1801 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1802 creall(value).format(buf1, sizeof(buf1)); | |
1803 cimagl(value).format(buf2, sizeof(buf2)); | |
1804 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1805 #else | |
1806 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1807 assert(strlen(buffer) < sizeof(buffer)); | |
1808 #endif | |
1809 return mem.strdup(buffer); | |
1810 } | |
1811 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1812 dinteger_t ComplexExp::toInteger() |
159 | 1813 { |
1814 #ifdef IN_GCC | |
1815 return (sinteger_t) toReal().toInt(); | |
1816 #else | |
1817 return (sinteger_t) toReal(); | |
1818 #endif | |
1819 } | |
1820 | |
1821 uinteger_t ComplexExp::toUInteger() | |
1822 { | |
1823 #ifdef IN_GCC | |
1824 return (uinteger_t) toReal().toInt(); | |
1825 #else | |
1826 return (uinteger_t) toReal(); | |
1827 #endif | |
1828 } | |
1829 | |
1830 real_t ComplexExp::toReal() | |
1831 { | |
1832 return creall(value); | |
1833 } | |
1834 | |
1835 real_t ComplexExp::toImaginary() | |
1836 { | |
1837 return cimagl(value); | |
1838 } | |
1839 | |
1840 complex_t ComplexExp::toComplex() | |
1841 { | |
1842 return value; | |
1843 } | |
1844 | |
1845 int ComplexExp::equals(Object *o) | |
1846 { ComplexExp *ne; | |
1847 | |
1848 if (this == o || | |
1849 (((Expression *)o)->op == TOKcomplex80 && | |
1850 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1851 RealEquals(creall(value), creall(ne->value)) && | |
1852 RealEquals(cimagl(value), cimagl(ne->value)) | |
1853 ) | |
1854 ) | |
1855 return 1; | |
1856 return 0; | |
1857 } | |
1858 | |
1859 Expression *ComplexExp::semantic(Scope *sc) | |
1860 { | |
1861 if (!type) | |
1862 type = Type::tcomplex80; | |
1863 else | |
1864 type = type->semantic(loc, sc); | |
1865 return this; | |
1866 } | |
1867 | |
1868 int ComplexExp::isBool(int result) | |
1869 { | |
1870 if (result) | |
1871 return (bool)(value); | |
1872 else | |
1873 return !value; | |
1874 } | |
1875 | |
1876 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1877 { | |
1878 /* Print as: | |
1879 * (re+imi) | |
1880 */ | |
1881 #ifdef IN_GCC | |
1882 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1883 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1884 creall(value).format(buf1, sizeof(buf1)); | |
1885 cimagl(value).format(buf2, sizeof(buf2)); | |
1886 buf->printf("(%s+%si)", buf1, buf2); | |
1887 #else | |
1888 buf->writeByte('('); | |
1889 floatToBuffer(buf, type, creall(value)); | |
1890 buf->writeByte('+'); | |
1891 floatToBuffer(buf, type, cimagl(value)); | |
1892 buf->writestring("i)"); | |
1893 #endif | |
1894 } | |
1895 | |
1896 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
1897 { | |
1898 buf->writeByte('c'); | |
1899 real_t r = toReal(); | |
1900 realToMangleBuffer(buf, r); | |
1901 buf->writeByte('c'); // separate the two | |
1902 r = toImaginary(); | |
1903 realToMangleBuffer(buf, r); | |
1904 } | |
1905 | |
1906 /******************************** IdentifierExp **************************/ | |
1907 | |
1908 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
1909 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
1910 { | |
1911 this->ident = ident; | |
1912 } | |
1913 | |
1914 Expression *IdentifierExp::semantic(Scope *sc) | |
1915 { | |
1916 Dsymbol *s; | |
1917 Dsymbol *scopesym; | |
1918 | |
1919 #if LOGSEMANTIC | |
1920 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
1921 #endif | |
1922 s = sc->search(loc, ident, &scopesym); | |
1923 if (s) | |
1924 { Expression *e; | |
1925 WithScopeSymbol *withsym; | |
1926 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1927 /* 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
|
1928 */ |
159 | 1929 withsym = scopesym->isWithScopeSymbol(); |
1930 if (withsym) | |
1931 { | |
1932 s = s->toAlias(); | |
1933 | |
1934 // Same as wthis.ident | |
1935 if (s->needThis() || s->isTemplateDeclaration()) | |
1936 { | |
1937 e = new VarExp(loc, withsym->withstate->wthis); | |
1938 e = new DotIdExp(loc, e, ident); | |
1939 } | |
1940 else | |
1941 { Type *t = withsym->withstate->wthis->type; | |
1942 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
|
1943 t = ((TypePointer *)t)->next; |
159 | 1944 e = new TypeDotIdExp(loc, t, ident); |
1945 } | |
1946 } | |
1947 else | |
1948 { | |
1949 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
1950 { // Kludge to run semantic() here because | |
1951 // ArrayScopeSymbol::search() doesn't have access to sc. | |
1952 s->semantic(sc); | |
1953 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1954 /* If f is really a function template, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1955 * 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
|
1956 */ |
159 | 1957 FuncDeclaration *f = s->isFuncDeclaration(); |
1958 if (f && f->parent) | |
1959 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
1960 | |
1961 if (ti && | |
1962 !ti->isTemplateMixin() && | |
1963 (ti->name == f->ident || | |
1964 ti->toAlias()->ident == f->ident) | |
1965 && | |
1966 ti->tempdecl && ti->tempdecl->onemember) | |
1967 { | |
1968 TemplateDeclaration *tempdecl = ti->tempdecl; | |
1969 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
1970 tempdecl = tempdecl->overroot; // then get the start | |
1971 e = new TemplateExp(loc, tempdecl); | |
1972 e = e->semantic(sc); | |
1973 return e; | |
1974 } | |
1975 } | |
1976 e = new DsymbolExp(loc, s); | |
1977 } | |
1978 return e->semantic(sc); | |
1979 } | |
1980 error("undefined identifier %s", ident->toChars()); | |
1981 type = Type::terror; | |
1982 return this; | |
1983 } | |
1984 | |
1985 char *IdentifierExp::toChars() | |
1986 { | |
1987 return ident->toChars(); | |
1988 } | |
1989 | |
1990 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1991 { | |
1992 if (hgs->hdrgen) | |
1993 buf->writestring(ident->toHChars2()); | |
1994 else | |
1995 buf->writestring(ident->toChars()); | |
1996 } | |
1997 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1998 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1999 int IdentifierExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2000 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2001 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2002 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2003 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2004 |
159 | 2005 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) |
2006 { | |
2007 #if 0 | |
2008 tym = tybasic(e1->ET->Tty); | |
2009 if (!(tyscalar(tym) || | |
2010 tym == TYstruct || | |
2011 tym == TYarray && e->Eoper == TOKaddr)) | |
2012 synerr(EM_lvalue); // lvalue expected | |
2013 #endif | |
2014 return this; | |
2015 } | |
2016 | |
2017 /******************************** DollarExp **************************/ | |
2018 | |
2019 DollarExp::DollarExp(Loc loc) | |
2020 : IdentifierExp(loc, Id::dollar) | |
2021 { | |
2022 } | |
2023 | |
2024 /******************************** DsymbolExp **************************/ | |
2025 | |
2026 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
2027 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
2028 { | |
2029 this->s = s; | |
2030 } | |
2031 | |
2032 Expression *DsymbolExp::semantic(Scope *sc) | |
2033 { | |
2034 #if LOGSEMANTIC | |
2035 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
2036 #endif | |
2037 | |
2038 Lagain: | |
2039 EnumMember *em; | |
2040 Expression *e; | |
2041 VarDeclaration *v; | |
2042 FuncDeclaration *f; | |
2043 FuncLiteralDeclaration *fld; | |
2044 Declaration *d; | |
2045 ClassDeclaration *cd; | |
2046 ClassDeclaration *thiscd = NULL; | |
2047 Import *imp; | |
2048 Package *pkg; | |
2049 Type *t; | |
2050 | |
2051 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
2052 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
2053 if (type) | |
2054 return this; | |
2055 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
2056 checkDeprecated(sc, s); | |
2057 s = s->toAlias(); | |
2058 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
2059 if (!s->isFuncDeclaration()) | |
2060 checkDeprecated(sc, s); | |
2061 | |
2062 if (sc->func) | |
2063 thiscd = sc->func->parent->isClassDeclaration(); | |
2064 | |
2065 // BUG: This should happen after overload resolution for functions, not before | |
2066 if (s->needThis()) | |
2067 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2068 if (hasThis(sc) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2069 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2070 && !s->isFuncDeclaration() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2071 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2072 ) |
159 | 2073 { |
2074 // Supply an implicit 'this', as in | |
2075 // this.ident | |
2076 | |
2077 DotVarExp *de; | |
2078 | |
2079 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
2080 return de->semantic(sc); | |
2081 } | |
2082 } | |
2083 | |
2084 em = s->isEnumMember(); | |
2085 if (em) | |
2086 { | |
2087 e = em->value->copy(); | |
2088 e = e->semantic(sc); | |
2089 return e; | |
2090 } | |
2091 v = s->isVarDeclaration(); | |
2092 if (v) | |
2093 { | |
2094 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
2095 if (!type) | |
2096 { type = v->type; | |
2097 if (!v->type) | |
2098 { error("forward reference of %s", v->toChars()); | |
2099 type = Type::terror; | |
2100 } | |
2101 } | |
2102 if (v->isConst() && type->toBasetype()->ty != Tsarray) | |
2103 { | |
2104 if (v->init) | |
2105 { | |
2106 if (v->inuse) | |
2107 { | |
2108 error("circular reference to '%s'", v->toChars()); | |
2109 type = Type::tint32; | |
2110 return this; | |
2111 } | |
2112 ExpInitializer *ei = v->init->isExpInitializer(); | |
2113 if (ei) | |
2114 { | |
2115 e = ei->exp->copy(); // make copy so we can change loc | |
2116 if (e->op == TOKstring || !e->type) | |
2117 e = e->semantic(sc); | |
2118 e = e->implicitCastTo(sc, type); | |
2119 e->loc = loc; | |
2120 return e; | |
2121 } | |
2122 } | |
2123 else | |
2124 { | |
2125 e = type->defaultInit(); | |
2126 e->loc = loc; | |
2127 return e; | |
2128 } | |
2129 } | |
2130 e = new VarExp(loc, v); | |
2131 e->type = type; | |
2132 e = e->semantic(sc); | |
2133 return e->deref(); | |
2134 } | |
2135 fld = s->isFuncLiteralDeclaration(); | |
2136 if (fld) | |
2137 { //printf("'%s' is a function literal\n", fld->toChars()); | |
2138 e = new FuncExp(loc, fld); | |
2139 return e->semantic(sc); | |
2140 } | |
2141 f = s->isFuncDeclaration(); | |
2142 if (f) | |
2143 { //printf("'%s' is a function\n", f->toChars()); | |
2144 return new VarExp(loc, f); | |
2145 } | |
2146 cd = s->isClassDeclaration(); | |
2147 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
2148 { | |
2149 // We need to add an implicit 'this' if cd is this class or a base class. | |
2150 DotTypeExp *dte; | |
2151 | |
2152 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
2153 return dte->semantic(sc); | |
2154 } | |
2155 imp = s->isImport(); | |
2156 if (imp) | |
2157 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2158 if (!imp->pkg) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2159 { 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
|
2160 return this; |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2161 } |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2162 ScopeExp *ie = new ScopeExp(loc, imp->pkg); |
159 | 2163 return ie->semantic(sc); |
2164 } | |
2165 pkg = s->isPackage(); | |
2166 if (pkg) | |
2167 { | |
2168 ScopeExp *ie; | |
2169 | |
2170 ie = new ScopeExp(loc, pkg); | |
2171 return ie->semantic(sc); | |
2172 } | |
2173 Module *mod = s->isModule(); | |
2174 if (mod) | |
2175 { | |
2176 ScopeExp *ie; | |
2177 | |
2178 ie = new ScopeExp(loc, mod); | |
2179 return ie->semantic(sc); | |
2180 } | |
2181 | |
2182 t = s->getType(); | |
2183 if (t) | |
2184 { | |
2185 return new TypeExp(loc, t); | |
2186 } | |
2187 | |
2188 TupleDeclaration *tup = s->isTupleDeclaration(); | |
2189 if (tup) | |
2190 { | |
2191 e = new TupleExp(loc, tup); | |
2192 e = e->semantic(sc); | |
2193 return e; | |
2194 } | |
2195 | |
2196 TemplateInstance *ti = s->isTemplateInstance(); | |
2197 if (ti && !global.errors) | |
2198 { if (!ti->semanticdone) | |
2199 ti->semantic(sc); | |
2200 s = ti->inst->toAlias(); | |
2201 if (!s->isTemplateInstance()) | |
2202 goto Lagain; | |
2203 e = new ScopeExp(loc, ti); | |
2204 e = e->semantic(sc); | |
2205 return e; | |
2206 } | |
2207 | |
2208 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
2209 if (td) | |
2210 { | |
2211 e = new TemplateExp(loc, td); | |
2212 e = e->semantic(sc); | |
2213 return e; | |
2214 } | |
2215 | |
2216 Lerr: | |
2217 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
2218 type = Type::terror; | |
2219 return this; | |
2220 } | |
2221 | |
2222 char *DsymbolExp::toChars() | |
2223 { | |
2224 return s->toChars(); | |
2225 } | |
2226 | |
2227 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2228 { | |
2229 buf->writestring(s->toChars()); | |
2230 } | |
2231 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2232 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2233 int DsymbolExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2234 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2235 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2236 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2237 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2238 |
159 | 2239 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) |
2240 { | |
2241 #if 0 | |
2242 tym = tybasic(e1->ET->Tty); | |
2243 if (!(tyscalar(tym) || | |
2244 tym == TYstruct || | |
2245 tym == TYarray && e->Eoper == TOKaddr)) | |
2246 synerr(EM_lvalue); // lvalue expected | |
2247 #endif | |
2248 return this; | |
2249 } | |
2250 | |
2251 /******************************** ThisExp **************************/ | |
2252 | |
2253 ThisExp::ThisExp(Loc loc) | |
2254 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
2255 { | |
2256 var = NULL; | |
2257 } | |
2258 | |
2259 Expression *ThisExp::semantic(Scope *sc) | |
2260 { FuncDeclaration *fd; | |
2261 FuncDeclaration *fdthis; | |
2262 int nested = 0; | |
2263 | |
2264 #if LOGSEMANTIC | |
2265 printf("ThisExp::semantic()\n"); | |
2266 #endif | |
2267 if (type) | |
2268 { //assert(global.errors || var); | |
2269 return this; | |
2270 } | |
2271 | |
2272 /* Special case for typeof(this) and typeof(super) since both | |
2273 * should work even if they are not inside a non-static member function | |
2274 */ | |
2275 if (sc->intypeof) | |
2276 { | |
2277 // Find enclosing struct or class | |
2278 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2279 { | |
2280 ClassDeclaration *cd; | |
2281 StructDeclaration *sd; | |
2282 | |
2283 if (!s) | |
2284 { | |
2285 error("%s is not in a struct or class scope", toChars()); | |
2286 goto Lerr; | |
2287 } | |
2288 cd = s->isClassDeclaration(); | |
2289 if (cd) | |
2290 { | |
2291 type = cd->type; | |
2292 return this; | |
2293 } | |
2294 sd = s->isStructDeclaration(); | |
2295 if (sd) | |
2296 { | |
2297 type = sd->type->pointerTo(); | |
2298 return this; | |
2299 } | |
2300 } | |
2301 } | |
2302 | |
2303 fdthis = sc->parent->isFuncDeclaration(); | |
2304 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
2305 if (!fd) | |
2306 goto Lerr; | |
2307 | |
2308 assert(fd->vthis); | |
2309 var = fd->vthis; | |
2310 assert(var->parent); | |
2311 type = var->type; | |
2312 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2313 #if 0 | |
2314 if (fd != fdthis) // if nested | |
2315 { | |
2316 fdthis->getLevel(loc, fd); | |
2317 fd->vthis->nestedref = 1; | |
2318 fd->nestedFrameRef = 1; | |
2319 } | |
2320 #endif | |
336 | 2321 if (!sc->intypeof) |
2322 sc->callSuper |= CSXthis; | |
159 | 2323 return this; |
2324 | |
2325 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
|
2326 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
|
2327 type = Type::terror; |
159 | 2328 return this; |
2329 } | |
2330 | |
2331 int ThisExp::isBool(int result) | |
2332 { | |
2333 return result ? TRUE : FALSE; | |
2334 } | |
2335 | |
2336 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2337 { | |
2338 buf->writestring("this"); | |
2339 } | |
2340 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2341 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2342 int ThisExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2343 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2344 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2345 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2346 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2347 |
159 | 2348 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) |
2349 { | |
2350 return this; | |
2351 } | |
2352 | |
2353 /******************************** SuperExp **************************/ | |
2354 | |
2355 SuperExp::SuperExp(Loc loc) | |
2356 : ThisExp(loc) | |
2357 { | |
2358 op = TOKsuper; | |
2359 } | |
2360 | |
2361 Expression *SuperExp::semantic(Scope *sc) | |
2362 { FuncDeclaration *fd; | |
2363 FuncDeclaration *fdthis; | |
2364 ClassDeclaration *cd; | |
2365 Dsymbol *s; | |
2366 | |
2367 #if LOGSEMANTIC | |
2368 printf("SuperExp::semantic('%s')\n", toChars()); | |
2369 #endif | |
2370 if (type) | |
2371 return this; | |
2372 | |
2373 /* Special case for typeof(this) and typeof(super) since both | |
2374 * should work even if they are not inside a non-static member function | |
2375 */ | |
2376 if (sc->intypeof) | |
2377 { | |
2378 // Find enclosing class | |
2379 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2380 { | |
2381 ClassDeclaration *cd; | |
2382 | |
2383 if (!s) | |
2384 { | |
2385 error("%s is not in a class scope", toChars()); | |
2386 goto Lerr; | |
2387 } | |
2388 cd = s->isClassDeclaration(); | |
2389 if (cd) | |
2390 { | |
2391 cd = cd->baseClass; | |
2392 if (!cd) | |
2393 { error("class %s has no 'super'", s->toChars()); | |
2394 goto Lerr; | |
2395 } | |
2396 type = cd->type; | |
2397 return this; | |
2398 } | |
2399 } | |
2400 } | |
2401 | |
2402 fdthis = sc->parent->isFuncDeclaration(); | |
2403 fd = hasThis(sc); | |
2404 if (!fd) | |
2405 goto Lerr; | |
2406 assert(fd->vthis); | |
2407 var = fd->vthis; | |
2408 assert(var->parent); | |
2409 | |
2410 s = fd->toParent(); | |
2411 while (s && s->isTemplateInstance()) | |
2412 s = s->toParent(); | |
2413 assert(s); | |
2414 cd = s->isClassDeclaration(); | |
2415 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2416 if (!cd) | |
2417 goto Lerr; | |
2418 if (!cd->baseClass) | |
2419 { | |
2420 error("no base class for %s", cd->toChars()); | |
2421 type = fd->vthis->type; | |
2422 } | |
2423 else | |
2424 { | |
2425 type = cd->baseClass->type; | |
2426 } | |
2427 | |
2428 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2429 #if 0 | |
2430 if (fd != fdthis) | |
2431 { | |
2432 fdthis->getLevel(loc, fd); | |
2433 fd->vthis->nestedref = 1; | |
2434 fd->nestedFrameRef = 1; | |
2435 } | |
2436 #endif | |
2437 | |
336 | 2438 if (!sc->intypeof) |
2439 sc->callSuper |= CSXsuper; | |
159 | 2440 return this; |
2441 | |
2442 | |
2443 Lerr: | |
2444 error("'super' is only allowed in non-static class member functions"); | |
2445 type = Type::tint32; | |
2446 return this; | |
2447 } | |
2448 | |
2449 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2450 { | |
2451 buf->writestring("super"); | |
2452 } | |
2453 | |
2454 | |
2455 /******************************** NullExp **************************/ | |
2456 | |
2457 NullExp::NullExp(Loc loc) | |
2458 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2459 { | |
2460 committed = 0; | |
2461 } | |
2462 | |
2463 Expression *NullExp::semantic(Scope *sc) | |
2464 { | |
2465 #if LOGSEMANTIC | |
2466 printf("NullExp::semantic('%s')\n", toChars()); | |
2467 #endif | |
2468 // NULL is the same as (void *)0 | |
2469 if (!type) | |
2470 type = Type::tvoid->pointerTo(); | |
2471 return this; | |
2472 } | |
2473 | |
2474 int NullExp::isBool(int result) | |
2475 { | |
2476 return result ? FALSE : TRUE; | |
2477 } | |
2478 | |
2479 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2480 { | |
2481 buf->writestring("null"); | |
2482 } | |
2483 | |
2484 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2485 { | |
2486 buf->writeByte('n'); | |
2487 } | |
2488 | |
2489 /******************************** StringExp **************************/ | |
2490 | |
2491 StringExp::StringExp(Loc loc, char *string) | |
2492 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2493 { | |
2494 this->string = string; | |
2495 this->len = strlen(string); | |
2496 this->sz = 1; | |
2497 this->committed = 0; | |
2498 this->postfix = 0; | |
2499 } | |
2500 | |
2501 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2502 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2503 { | |
2504 this->string = string; | |
2505 this->len = len; | |
2506 this->sz = 1; | |
2507 this->committed = 0; | |
2508 this->postfix = 0; | |
2509 } | |
2510 | |
2511 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2512 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2513 { | |
2514 this->string = string; | |
2515 this->len = len; | |
2516 this->sz = 1; | |
2517 this->committed = 0; | |
2518 this->postfix = postfix; | |
2519 } | |
2520 | |
2521 #if 0 | |
2522 Expression *StringExp::syntaxCopy() | |
2523 { | |
2524 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2525 return copy(); | |
2526 } | |
2527 #endif | |
2528 | |
2529 int StringExp::equals(Object *o) | |
2530 { | |
2531 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2532 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2533 { Expression *e = (Expression *)o; | |
2534 | |
2535 if (e->op == TOKstring) | |
2536 { | |
2537 return compare(o) == 0; | |
2538 } | |
2539 } | |
2540 return FALSE; | |
2541 } | |
2542 | |
2543 char *StringExp::toChars() | |
2544 { | |
2545 OutBuffer buf; | |
2546 HdrGenState hgs; | |
2547 char *p; | |
2548 | |
2549 memset(&hgs, 0, sizeof(hgs)); | |
2550 toCBuffer(&buf, &hgs); | |
2551 buf.writeByte(0); | |
2552 p = (char *)buf.data; | |
2553 buf.data = NULL; | |
2554 return p; | |
2555 } | |
2556 | |
2557 Expression *StringExp::semantic(Scope *sc) | |
2558 { | |
2559 #if LOGSEMANTIC | |
2560 printf("StringExp::semantic() %s\n", toChars()); | |
2561 #endif | |
2562 if (!type) | |
2563 { OutBuffer buffer; | |
2564 size_t newlen = 0; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2565 const char *p; |
159 | 2566 size_t u; |
2567 unsigned c; | |
2568 | |
2569 switch (postfix) | |
2570 { | |
2571 case 'd': | |
2572 for (u = 0; u < len;) | |
2573 { | |
2574 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2575 if (p) | |
2576 { error("%s", p); | |
2577 break; | |
2578 } | |
2579 else | |
2580 { buffer.write4(c); | |
2581 newlen++; | |
2582 } | |
2583 } | |
2584 buffer.write4(0); | |
2585 string = buffer.extractData(); | |
2586 len = newlen; | |
2587 sz = 4; | |
2588 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2589 committed = 1; | |
2590 break; | |
2591 | |
2592 case 'w': | |
2593 for (u = 0; u < len;) | |
2594 { | |
2595 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2596 if (p) | |
2597 { error("%s", p); | |
2598 break; | |
2599 } | |
2600 else | |
2601 { buffer.writeUTF16(c); | |
2602 newlen++; | |
2603 if (c >= 0x10000) | |
2604 newlen++; | |
2605 } | |
2606 } | |
2607 buffer.writeUTF16(0); | |
2608 string = buffer.extractData(); | |
2609 len = newlen; | |
2610 sz = 2; | |
2611 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2612 committed = 1; | |
2613 break; | |
2614 | |
2615 case 'c': | |
2616 committed = 1; | |
2617 default: | |
2618 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2619 break; | |
2620 } | |
2621 type = type->semantic(loc, sc); | |
2622 } | |
2623 return this; | |
2624 } | |
2625 | |
2626 /**************************************** | |
2627 * Convert string to char[]. | |
2628 */ | |
2629 | |
2630 StringExp *StringExp::toUTF8(Scope *sc) | |
2631 { | |
2632 if (sz != 1) | |
2633 { // Convert to UTF-8 string | |
2634 committed = 0; | |
2635 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2636 e = e->optimize(WANTvalue); | |
2637 assert(e->op == TOKstring); | |
2638 StringExp *se = (StringExp *)e; | |
2639 assert(se->sz == 1); | |
2640 return se; | |
2641 } | |
2642 return this; | |
2643 } | |
2644 | |
2645 int StringExp::compare(Object *obj) | |
2646 { | |
2647 // Used to sort case statement expressions so we can do an efficient lookup | |
2648 StringExp *se2 = (StringExp *)(obj); | |
2649 | |
2650 // This is a kludge so isExpression() in template.c will return 5 | |
2651 // for StringExp's. | |
2652 if (!se2) | |
2653 return 5; | |
2654 | |
2655 assert(se2->op == TOKstring); | |
2656 | |
2657 int len1 = len; | |
2658 int len2 = se2->len; | |
2659 | |
2660 if (len1 == len2) | |
2661 { | |
2662 switch (sz) | |
2663 { | |
2664 case 1: | |
2665 return strcmp((char *)string, (char *)se2->string); | |
2666 | |
2667 case 2: | |
2668 { unsigned u; | |
2669 d_wchar *s1 = (d_wchar *)string; | |
2670 d_wchar *s2 = (d_wchar *)se2->string; | |
2671 | |
2672 for (u = 0; u < len; u++) | |
2673 { | |
2674 if (s1[u] != s2[u]) | |
2675 return s1[u] - s2[u]; | |
2676 } | |
2677 } | |
2678 | |
2679 case 4: | |
2680 { unsigned u; | |
2681 d_dchar *s1 = (d_dchar *)string; | |
2682 d_dchar *s2 = (d_dchar *)se2->string; | |
2683 | |
2684 for (u = 0; u < len; u++) | |
2685 { | |
2686 if (s1[u] != s2[u]) | |
2687 return s1[u] - s2[u]; | |
2688 } | |
2689 } | |
2690 break; | |
2691 | |
2692 default: | |
2693 assert(0); | |
2694 } | |
2695 } | |
2696 return len1 - len2; | |
2697 } | |
2698 | |
2699 int StringExp::isBool(int result) | |
2700 { | |
2701 return result ? TRUE : FALSE; | |
2702 } | |
2703 | |
2704 unsigned StringExp::charAt(size_t i) | |
2705 { unsigned value; | |
2706 | |
2707 switch (sz) | |
2708 { | |
2709 case 1: | |
2710 value = ((unsigned char *)string)[i]; | |
2711 break; | |
2712 | |
2713 case 2: | |
2714 value = ((unsigned short *)string)[i]; | |
2715 break; | |
2716 | |
2717 case 4: | |
2718 value = ((unsigned int *)string)[i]; | |
2719 break; | |
2720 | |
2721 default: | |
2722 assert(0); | |
2723 break; | |
2724 } | |
2725 return value; | |
2726 } | |
2727 | |
2728 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2729 { | |
2730 buf->writeByte('"'); | |
2731 for (size_t i = 0; i < len; i++) | |
2732 { unsigned c = charAt(i); | |
2733 | |
2734 switch (c) | |
2735 { | |
2736 case '"': | |
2737 case '\\': | |
2738 if (!hgs->console) | |
2739 buf->writeByte('\\'); | |
2740 default: | |
2741 if (c <= 0xFF) | |
2742 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2743 buf->writeByte(c); | |
2744 else | |
2745 buf->printf("\\x%02x", c); | |
2746 } | |
2747 else if (c <= 0xFFFF) | |
2748 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2749 else | |
2750 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2751 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2752 break; | |
2753 } | |
2754 } | |
2755 buf->writeByte('"'); | |
2756 if (postfix) | |
2757 buf->writeByte(postfix); | |
2758 } | |
2759 | |
2760 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2761 { char m; | |
2762 OutBuffer tmp; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2763 const char *p; |
159 | 2764 unsigned c; |
2765 size_t u; | |
2766 unsigned char *q; | |
2767 unsigned qlen; | |
2768 | |
2769 /* Write string in UTF-8 format | |
2770 */ | |
2771 switch (sz) | |
2772 { case 1: | |
2773 m = 'a'; | |
2774 q = (unsigned char *)string; | |
2775 qlen = len; | |
2776 break; | |
2777 case 2: | |
2778 m = 'w'; | |
2779 for (u = 0; u < len; ) | |
2780 { | |
2781 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2782 if (p) | |
2783 error("%s", p); | |
2784 else | |
2785 tmp.writeUTF8(c); | |
2786 } | |
2787 q = tmp.data; | |
2788 qlen = tmp.offset; | |
2789 break; | |
2790 case 4: | |
2791 m = 'd'; | |
2792 for (u = 0; u < len; u++) | |
2793 { | |
2794 c = ((unsigned *)string)[u]; | |
2795 if (!utf_isValidDchar(c)) | |
2796 error("invalid UCS-32 char \\U%08x", c); | |
2797 else | |
2798 tmp.writeUTF8(c); | |
2799 } | |
2800 q = tmp.data; | |
2801 qlen = tmp.offset; | |
2802 break; | |
2803 default: | |
2804 assert(0); | |
2805 } | |
2806 buf->writeByte(m); | |
2807 buf->printf("%d_", qlen); | |
2808 for (size_t i = 0; i < qlen; i++) | |
2809 buf->printf("%02x", q[i]); | |
2810 } | |
2811 | |
2812 /************************ ArrayLiteralExp ************************************/ | |
2813 | |
2814 // [ e1, e2, e3, ... ] | |
2815 | |
2816 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2817 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2818 { | |
2819 this->elements = elements; | |
2820 } | |
2821 | |
2822 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2823 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2824 { | |
2825 elements = new Expressions; | |
2826 elements->push(e); | |
2827 } | |
2828 | |
2829 Expression *ArrayLiteralExp::syntaxCopy() | |
2830 { | |
2831 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2832 } | |
2833 | |
2834 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2835 { Expression *e; | |
2836 Type *t0 = NULL; | |
2837 | |
2838 #if LOGSEMANTIC | |
2839 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2840 #endif | |
2841 if (type) | |
2842 return this; | |
2843 | |
2844 // Run semantic() on each element | |
2845 for (int i = 0; i < elements->dim; i++) | |
2846 { e = (Expression *)elements->data[i]; | |
2847 e = e->semantic(sc); | |
2848 elements->data[i] = (void *)e; | |
2849 } | |
2850 expandTuples(elements); | |
2851 for (int i = 0; i < elements->dim; i++) | |
2852 { e = (Expression *)elements->data[i]; | |
2853 | |
2854 if (!e->type) | |
2855 error("%s has no value", e->toChars()); | |
2856 e = resolveProperties(sc, e); | |
2857 | |
2858 unsigned char committed = 1; | |
2859 if (e->op == TOKstring) | |
2860 committed = ((StringExp *)e)->committed; | |
2861 | |
2862 if (!t0) | |
2863 { t0 = e->type; | |
2864 // Convert any static arrays to dynamic arrays | |
2865 if (t0->ty == Tsarray) | |
2866 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2867 t0 = ((TypeSArray *)t0)->next->arrayOf(); |
159 | 2868 e = e->implicitCastTo(sc, t0); |
2869 } | |
2870 } | |
2871 else | |
2872 e = e->implicitCastTo(sc, t0); | |
2873 if (!committed && e->op == TOKstring) | |
2874 { StringExp *se = (StringExp *)e; | |
2875 se->committed = 0; | |
2876 } | |
2877 elements->data[i] = (void *)e; | |
2878 } | |
2879 | |
2880 if (!t0) | |
2881 t0 = Type::tvoid; | |
2882 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
2883 type = type->semantic(loc, sc); | |
2884 return this; | |
2885 } | |
2886 | |
2887 int ArrayLiteralExp::checkSideEffect(int flag) | |
2888 { int f = 0; | |
2889 | |
2890 for (size_t i = 0; i < elements->dim; i++) | |
2891 { Expression *e = (Expression *)elements->data[i]; | |
2892 | |
2893 f |= e->checkSideEffect(2); | |
2894 } | |
2895 if (flag == 0 && f == 0) | |
2896 Expression::checkSideEffect(0); | |
2897 return f; | |
2898 } | |
2899 | |
2900 int ArrayLiteralExp::isBool(int result) | |
2901 { | |
2902 size_t dim = elements ? elements->dim : 0; | |
2903 return result ? (dim != 0) : (dim == 0); | |
2904 } | |
2905 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2906 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2907 int ArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2908 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2909 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
|
2910 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2911 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2912 |
159 | 2913 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2914 { | |
2915 buf->writeByte('['); | |
2916 argsToCBuffer(buf, elements, hgs); | |
2917 buf->writeByte(']'); | |
2918 } | |
2919 | |
2920 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2921 { | |
2922 size_t dim = elements ? elements->dim : 0; | |
2923 buf->printf("A%u", dim); | |
2924 for (size_t i = 0; i < dim; i++) | |
2925 { Expression *e = (Expression *)elements->data[i]; | |
2926 e->toMangleBuffer(buf); | |
2927 } | |
2928 } | |
2929 | |
2930 /************************ AssocArrayLiteralExp ************************************/ | |
2931 | |
2932 // [ key0 : value0, key1 : value1, ... ] | |
2933 | |
2934 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
2935 Expressions *keys, Expressions *values) | |
2936 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
2937 { | |
2938 assert(keys->dim == values->dim); | |
2939 this->keys = keys; | |
2940 this->values = values; | |
2941 } | |
2942 | |
2943 Expression *AssocArrayLiteralExp::syntaxCopy() | |
2944 { | |
2945 return new AssocArrayLiteralExp(loc, | |
2946 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
2947 } | |
2948 | |
2949 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
2950 { Expression *e; | |
2951 Type *tkey = NULL; | |
2952 Type *tvalue = NULL; | |
2953 | |
2954 #if LOGSEMANTIC | |
2955 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
2956 #endif | |
2957 | |
2958 // Run semantic() on each element | |
2959 for (size_t i = 0; i < keys->dim; i++) | |
2960 { Expression *key = (Expression *)keys->data[i]; | |
2961 Expression *value = (Expression *)values->data[i]; | |
2962 | |
2963 key = key->semantic(sc); | |
2964 value = value->semantic(sc); | |
2965 | |
2966 keys->data[i] = (void *)key; | |
2967 values->data[i] = (void *)value; | |
2968 } | |
2969 expandTuples(keys); | |
2970 expandTuples(values); | |
2971 if (keys->dim != values->dim) | |
2972 { | |
2973 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
2974 keys->setDim(0); | |
2975 values->setDim(0); | |
2976 } | |
2977 for (size_t i = 0; i < keys->dim; i++) | |
2978 { Expression *key = (Expression *)keys->data[i]; | |
2979 Expression *value = (Expression *)values->data[i]; | |
2980 | |
2981 if (!key->type) | |
2982 error("%s has no value", key->toChars()); | |
2983 if (!value->type) | |
2984 error("%s has no value", value->toChars()); | |
2985 key = resolveProperties(sc, key); | |
2986 value = resolveProperties(sc, value); | |
2987 | |
2988 if (!tkey) | |
2989 tkey = key->type; | |
2990 else | |
2991 key = key->implicitCastTo(sc, tkey); | |
2992 keys->data[i] = (void *)key; | |
2993 | |
2994 if (!tvalue) | |
2995 tvalue = value->type; | |
2996 else | |
2997 value = value->implicitCastTo(sc, tvalue); | |
2998 values->data[i] = (void *)value; | |
2999 } | |
3000 | |
3001 if (!tkey) | |
3002 tkey = Type::tvoid; | |
3003 if (!tvalue) | |
3004 tvalue = Type::tvoid; | |
3005 type = new TypeAArray(tvalue, tkey); | |
3006 type = type->semantic(loc, sc); | |
3007 return this; | |
3008 } | |
3009 | |
3010 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
3011 { int f = 0; | |
3012 | |
3013 for (size_t i = 0; i < keys->dim; i++) | |
3014 { Expression *key = (Expression *)keys->data[i]; | |
3015 Expression *value = (Expression *)values->data[i]; | |
3016 | |
3017 f |= key->checkSideEffect(2); | |
3018 f |= value->checkSideEffect(2); | |
3019 } | |
3020 if (flag == 0 && f == 0) | |
3021 Expression::checkSideEffect(0); | |
3022 return f; | |
3023 } | |
3024 | |
3025 int AssocArrayLiteralExp::isBool(int result) | |
3026 { | |
3027 size_t dim = keys->dim; | |
3028 return result ? (dim != 0) : (dim == 0); | |
3029 } | |
3030 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3031 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3032 int AssocArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3033 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3034 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3035 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3036 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3037 |
159 | 3038 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3039 { | |
3040 buf->writeByte('['); | |
3041 for (size_t i = 0; i < keys->dim; i++) | |
3042 { Expression *key = (Expression *)keys->data[i]; | |
3043 Expression *value = (Expression *)values->data[i]; | |
3044 | |
3045 if (i) | |
3046 buf->writeByte(','); | |
3047 expToCBuffer(buf, hgs, key, PREC_assign); | |
3048 buf->writeByte(':'); | |
3049 expToCBuffer(buf, hgs, value, PREC_assign); | |
3050 } | |
3051 buf->writeByte(']'); | |
3052 } | |
3053 | |
3054 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3055 { | |
3056 size_t dim = keys->dim; | |
3057 buf->printf("A%u", dim); | |
3058 for (size_t i = 0; i < dim; i++) | |
3059 { Expression *key = (Expression *)keys->data[i]; | |
3060 Expression *value = (Expression *)values->data[i]; | |
3061 | |
3062 key->toMangleBuffer(buf); | |
3063 value->toMangleBuffer(buf); | |
3064 } | |
3065 } | |
3066 | |
3067 /************************ StructLiteralExp ************************************/ | |
3068 | |
3069 // sd( e1, e2, e3, ... ) | |
3070 | |
3071 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
3072 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
3073 { | |
3074 this->sd = sd; | |
3075 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
|
3076 #if IN_DMD |
159 | 3077 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
|
3078 #endif |
159 | 3079 this->soffset = 0; |
3080 this->fillHoles = 1; | |
3081 } | |
3082 | |
3083 Expression *StructLiteralExp::syntaxCopy() | |
3084 { | |
3085 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
3086 } | |
3087 | |
3088 Expression *StructLiteralExp::semantic(Scope *sc) | |
3089 { Expression *e; | |
3090 | |
3091 #if LOGSEMANTIC | |
3092 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
3093 #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
|
3094 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
|
3095 return this; |
159 | 3096 |
3097 // Run semantic() on each element | |
3098 for (size_t i = 0; i < elements->dim; i++) | |
3099 { e = (Expression *)elements->data[i]; | |
3100 if (!e) | |
3101 continue; | |
3102 e = e->semantic(sc); | |
3103 elements->data[i] = (void *)e; | |
3104 } | |
3105 expandTuples(elements); | |
3106 size_t offset = 0; | |
3107 for (size_t i = 0; i < elements->dim; i++) | |
3108 { e = (Expression *)elements->data[i]; | |
3109 if (!e) | |
3110 continue; | |
3111 | |
3112 if (!e->type) | |
3113 error("%s has no value", e->toChars()); | |
3114 e = resolveProperties(sc, e); | |
3115 if (i >= sd->fields.dim) | |
3116 { error("more initializers than fields of %s", sd->toChars()); | |
3117 break; | |
3118 } | |
3119 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3120 VarDeclaration *v = s->isVarDeclaration(); | |
3121 assert(v); | |
3122 if (v->offset < offset) | |
3123 error("overlapping initialization for %s", v->toChars()); | |
3124 offset = v->offset + v->type->size(); | |
3125 | |
3126 Type *telem = v->type; | |
3127 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
3128 { /* Static array initialization, as in: | |
3129 * T[3][5] = e; | |
3130 */ | |
3131 telem = telem->toBasetype()->nextOf(); | |
3132 } | |
3133 | |
3134 e = e->implicitCastTo(sc, telem); | |
3135 | |
3136 elements->data[i] = (void *)e; | |
3137 } | |
3138 | |
3139 /* Fill out remainder of elements[] with default initializers for fields[] | |
3140 */ | |
3141 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
3142 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3143 VarDeclaration *v = s->isVarDeclaration(); | |
3144 assert(v); | |
3145 | |
3146 if (v->offset < offset) | |
3147 { e = NULL; | |
3148 sd->hasUnions = 1; | |
3149 } | |
3150 else | |
3151 { | |
3152 if (v->init) | |
3153 { e = v->init->toExpression(); | |
3154 if (!e) | |
3155 error("cannot make expression out of initializer for %s", v->toChars()); | |
3156 } | |
3157 else | |
3158 { e = v->type->defaultInit(); | |
3159 e->loc = loc; | |
3160 } | |
3161 offset = v->offset + v->type->size(); | |
3162 } | |
3163 elements->push(e); | |
3164 } | |
3165 | |
3166 type = sd->type; | |
3167 return this; | |
3168 } | |
3169 | |
3170 /************************************** | |
3171 * Gets expression at offset of type. | |
3172 * Returns NULL if not found. | |
3173 */ | |
3174 | |
3175 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
|
3176 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3177 //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
|
3178 // /*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
|
3179 Expression *e = NULL; |
159 | 3180 int i = getFieldIndex(type, offset); |
3181 | |
3182 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
|
3183 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3184 //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
|
3185 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
|
3186 e = (Expression *)elements->data[i]; |
159 | 3187 if (e) |
3188 { | |
3189 e = e->copy(); | |
3190 e->type = type; | |
3191 } | |
3192 } | |
3193 return e; | |
3194 } | |
3195 | |
3196 /************************************ | |
3197 * Get index of field. | |
3198 * Returns -1 if not found. | |
3199 */ | |
3200 | |
3201 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
3202 { | |
3203 /* Find which field offset is by looking at the field offsets | |
3204 */ | |
3205 for (size_t i = 0; i < sd->fields.dim; i++) | |
3206 { | |
3207 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3208 VarDeclaration *v = s->isVarDeclaration(); | |
3209 assert(v); | |
3210 | |
3211 if (offset == v->offset && | |
3212 type->size() == v->type->size()) | |
3213 { Expression *e = (Expression *)elements->data[i]; | |
3214 if (e) | |
3215 { | |
3216 return i; | |
3217 } | |
3218 break; | |
3219 } | |
3220 } | |
3221 return -1; | |
3222 } | |
3223 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3224 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3225 int StructLiteralExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3226 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3227 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3228 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3229 #endif |
159 | 3230 |
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
|
3231 /* |
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
|
3232 Removed in LDC. See declaration. |
159 | 3233 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) |
3234 { | |
3235 return this; | |
3236 } | |
1168
ab186e535e72
A different fix to #218 and DMD2682 that does not lead to constant folding regressions.
Christian Kamm <kamm incasoftware de>
parents:
1151
diff
changeset
|
3237 */ |
159 | 3238 |
3239 | |
3240 int StructLiteralExp::checkSideEffect(int flag) | |
3241 { int f = 0; | |
3242 | |
3243 for (size_t i = 0; i < elements->dim; i++) | |
3244 { Expression *e = (Expression *)elements->data[i]; | |
3245 if (!e) | |
3246 continue; | |
3247 | |
3248 f |= e->checkSideEffect(2); | |
3249 } | |
3250 if (flag == 0 && f == 0) | |
3251 Expression::checkSideEffect(0); | |
3252 return f; | |
3253 } | |
3254 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3255 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3256 int StructLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3257 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3258 return arrayExpressionCanThrow(elements); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3259 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3260 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3261 |
159 | 3262 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3263 { | |
3264 buf->writestring(sd->toChars()); | |
3265 buf->writeByte('('); | |
3266 argsToCBuffer(buf, elements, hgs); | |
3267 buf->writeByte(')'); | |
3268 } | |
3269 | |
3270 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3271 { | |
3272 size_t dim = elements ? elements->dim : 0; | |
3273 buf->printf("S%u", dim); | |
3274 for (size_t i = 0; i < dim; i++) | |
3275 { Expression *e = (Expression *)elements->data[i]; | |
3276 if (e) | |
3277 e->toMangleBuffer(buf); | |
3278 else | |
3279 buf->writeByte('v'); // 'v' for void | |
3280 } | |
3281 } | |
3282 | |
3283 /************************ TypeDotIdExp ************************************/ | |
3284 | |
3285 /* Things like: | |
3286 * int.size | |
3287 * foo.size | |
3288 * (foo).size | |
3289 * cast(foo).size | |
3290 */ | |
3291 | |
3292 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident) | |
3293 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp)) | |
3294 { | |
3295 this->type = type; | |
3296 this->ident = ident; | |
3297 } | |
3298 | |
3299 Expression *TypeDotIdExp::syntaxCopy() | |
3300 { | |
3301 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident); | |
3302 return te; | |
3303 } | |
3304 | |
3305 Expression *TypeDotIdExp::semantic(Scope *sc) | |
3306 { Expression *e; | |
3307 | |
3308 #if LOGSEMANTIC | |
3309 printf("TypeDotIdExp::semantic()\n"); | |
3310 #endif | |
3311 e = new DotIdExp(loc, new TypeExp(loc, type), ident); | |
3312 e = e->semantic(sc); | |
3313 return e; | |
3314 } | |
3315 | |
3316 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3317 { | |
3318 buf->writeByte('('); | |
3319 type->toCBuffer(buf, NULL, hgs); | |
3320 buf->writeByte(')'); | |
3321 buf->writeByte('.'); | |
3322 buf->writestring(ident->toChars()); | |
3323 } | |
3324 | |
3325 /************************************************************/ | |
3326 | |
3327 // Mainly just a placeholder | |
3328 | |
3329 TypeExp::TypeExp(Loc loc, Type *type) | |
3330 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
3331 { | |
3332 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
3333 this->type = type; | |
3334 } | |
3335 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3336 Expression *TypeExp::syntaxCopy() |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3337 { |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3338 //printf("TypeExp::syntaxCopy()\n"); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3339 return new TypeExp(loc, type->syntaxCopy()); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3340 } |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3341 |
159 | 3342 Expression *TypeExp::semantic(Scope *sc) |
3343 { | |
3344 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
3345 type = type->semantic(loc, sc); | |
3346 return this; | |
3347 } | |
3348 | |
3349 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3350 { | |
3351 type->toCBuffer(buf, NULL, hgs); | |
3352 } | |
3353 | |
3354 /************************************************************/ | |
3355 | |
3356 // Mainly just a placeholder | |
3357 | |
3358 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
3359 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
3360 { | |
3361 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
3362 //static int count; if (++count == 38) *(char*)0=0; | |
3363 this->sds = pkg; | |
3364 } | |
3365 | |
3366 Expression *ScopeExp::syntaxCopy() | |
3367 { | |
3368 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
3369 return se; | |
3370 } | |
3371 | |
3372 Expression *ScopeExp::semantic(Scope *sc) | |
3373 { | |
3374 TemplateInstance *ti; | |
3375 ScopeDsymbol *sds2; | |
3376 | |
3377 #if LOGSEMANTIC | |
3378 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
3379 #endif | |
3380 Lagain: | |
3381 ti = sds->isTemplateInstance(); | |
3382 if (ti && !global.errors) | |
3383 { Dsymbol *s; | |
3384 if (!ti->semanticdone) | |
3385 ti->semantic(sc); | |
3386 s = ti->inst->toAlias(); | |
3387 sds2 = s->isScopeDsymbol(); | |
3388 if (!sds2) | |
3389 { Expression *e; | |
3390 | |
3391 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
3392 if (ti->withsym) | |
3393 { | |
3394 // Same as wthis.s | |
3395 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3396 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3397 } | |
3398 else | |
3399 e = new DsymbolExp(loc, s); | |
3400 e = e->semantic(sc); | |
3401 //printf("-1ScopeExp::semantic()\n"); | |
3402 return e; | |
3403 } | |
3404 if (sds2 != sds) | |
3405 { | |
3406 sds = sds2; | |
3407 goto Lagain; | |
3408 } | |
3409 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3410 } | |
3411 else | |
3412 { | |
3413 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3414 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3415 sds->semantic(sc); | |
3416 } | |
3417 type = Type::tvoid; | |
3418 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3419 return this; | |
3420 } | |
3421 | |
3422 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3423 { | |
3424 if (sds->isTemplateInstance()) | |
3425 { | |
3426 sds->toCBuffer(buf, hgs); | |
3427 } | |
3428 else | |
3429 { | |
3430 buf->writestring(sds->kind()); | |
3431 buf->writestring(" "); | |
3432 buf->writestring(sds->toChars()); | |
3433 } | |
3434 } | |
3435 | |
3436 /********************** TemplateExp **************************************/ | |
3437 | |
3438 // Mainly just a placeholder | |
3439 | |
3440 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3441 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3442 { | |
3443 //printf("TemplateExp(): %s\n", td->toChars()); | |
3444 this->td = td; | |
3445 } | |
3446 | |
3447 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3448 { | |
3449 buf->writestring(td->toChars()); | |
3450 } | |
3451 | |
3452 void TemplateExp::rvalue() | |
3453 { | |
3454 error("template %s has no value", toChars()); | |
3455 } | |
3456 | |
3457 /********************** NewExp **************************************/ | |
3458 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3459 /* thisexp.new(newargs) newtype(arguments) */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3460 |
159 | 3461 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, |
3462 Type *newtype, Expressions *arguments) | |
3463 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3464 { | |
3465 this->thisexp = thisexp; | |
3466 this->newargs = newargs; | |
3467 this->newtype = newtype; | |
3468 this->arguments = arguments; | |
3469 member = NULL; | |
3470 allocator = NULL; | |
3471 onstack = 0; | |
3472 } | |
3473 | |
3474 Expression *NewExp::syntaxCopy() | |
3475 { | |
3476 return new NewExp(loc, | |
3477 thisexp ? thisexp->syntaxCopy() : NULL, | |
3478 arraySyntaxCopy(newargs), | |
3479 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3480 } | |
3481 | |
3482 | |
3483 Expression *NewExp::semantic(Scope *sc) | |
3484 { int i; | |
3485 Type *tb; | |
3486 ClassDeclaration *cdthis = NULL; | |
3487 | |
3488 #if LOGSEMANTIC | |
3489 printf("NewExp::semantic() %s\n", toChars()); | |
3490 if (thisexp) | |
3491 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3492 printf("\tnewtype: %s\n", newtype->toChars()); | |
3493 #endif | |
3494 if (type) // if semantic() already run | |
3495 return this; | |
3496 | |
3497 Lagain: | |
3498 if (thisexp) | |
3499 { thisexp = thisexp->semantic(sc); | |
3500 cdthis = thisexp->type->isClassHandle(); | |
3501 if (cdthis) | |
3502 { | |
3503 sc = sc->push(cdthis); | |
3504 type = newtype->semantic(loc, sc); | |
3505 sc = sc->pop(); | |
3506 } | |
3507 else | |
3508 { | |
3509 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3510 type = newtype->semantic(loc, sc); | |
3511 } | |
3512 } | |
3513 else | |
3514 type = newtype->semantic(loc, sc); | |
3515 newtype = type; // in case type gets cast to something else | |
3516 tb = type->toBasetype(); | |
3517 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3518 | |
3519 arrayExpressionSemantic(newargs, sc); | |
3520 preFunctionArguments(loc, sc, newargs); | |
3521 arrayExpressionSemantic(arguments, sc); | |
3522 preFunctionArguments(loc, sc, arguments); | |
3523 | |
3524 if (thisexp && tb->ty != Tclass) | |
3525 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3526 | |
3527 if (tb->ty == Tclass) | |
3528 { TypeFunction *tf; | |
3529 | |
3530 TypeClass *tc = (TypeClass *)(tb); | |
3531 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3532 if (cd->isInterfaceDeclaration()) | |
3533 error("cannot create instance of interface %s", cd->toChars()); | |
3534 else if (cd->isAbstract()) | |
3535 { error("cannot create instance of abstract class %s", cd->toChars()); | |
3536 for (int i = 0; i < cd->vtbl.dim; i++) | |
3537 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration(); | |
3538 if (fd && fd->isAbstract()) | |
3539 error("function %s is abstract", fd->toChars()); | |
3540 } | |
3541 } | |
3542 checkDeprecated(sc, cd); | |
3543 if (cd->isNested()) | |
3544 { /* We need a 'this' pointer for the nested class. | |
3545 * Ensure we have the right one. | |
3546 */ | |
3547 Dsymbol *s = cd->toParent2(); | |
3548 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
|
3549 FuncDeclaration *fdn = s->isFuncDeclaration(); |
159 | 3550 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3551 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3552 if (cdn) |
3553 { | |
3554 if (!cdthis) | |
3555 { | |
3556 // Supply an implicit 'this' and try again | |
3557 thisexp = new ThisExp(loc); | |
3558 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3559 { if (!sp) | |
3560 { | |
3561 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3562 break; | |
3563 } | |
3564 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3565 if (!cdp) | |
3566 continue; | |
3567 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3568 break; | |
3569 // Add a '.outer' and try again | |
3570 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3571 } | |
3572 if (!global.errors) | |
3573 goto Lagain; | |
3574 } | |
3575 if (cdthis) | |
3576 { | |
3577 //printf("cdthis = %s\n", cdthis->toChars()); | |
3578 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3579 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3580 } | |
3581 #if 0 | |
3582 else | |
3583 { | |
3584 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3585 { | |
3586 if (!sf) | |
3587 { | |
3588 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3589 break; | |
3590 } | |
3591 printf("sf = %s\n", sf->toChars()); | |
3592 AggregateDeclaration *ad = sf->isThis(); | |
3593 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3594 break; | |
3595 } | |
3596 } | |
3597 #endif | |
3598 } | |
3599 else if (thisexp) | |
3600 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
|
3601 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
|
3602 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3603 // 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
|
3604 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
|
3605 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3606 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
|
3607 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3608 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
|
3609 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
|
3610 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3611 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
|
3612 break; |
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 |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3616 } |
159 | 3617 } |
3618 else if (thisexp) | |
3619 error("e.new is only for allocating nested classes"); | |
3620 | |
3621 FuncDeclaration *f = cd->ctor; | |
3622 if (f) | |
3623 { | |
3624 assert(f); | |
3625 f = f->overloadResolve(loc, arguments); | |
3626 checkDeprecated(sc, f); | |
3627 member = f->isCtorDeclaration(); | |
3628 assert(member); | |
3629 | |
3630 cd->accessCheck(loc, sc, member); | |
3631 | |
3632 tf = (TypeFunction *)f->type; | |
3633 type = tf->next; | |
3634 | |
3635 if (!arguments) | |
3636 arguments = new Expressions(); | |
3637 functionArguments(loc, sc, tf, arguments); | |
3638 } | |
3639 else | |
3640 { | |
3641 if (arguments && arguments->dim) | |
3642 error("no constructor for %s", cd->toChars()); | |
3643 } | |
3644 | |
3645 if (cd->aggNew) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3646 { |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
3647 // 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
|
3648 Expression *e = new IntegerExp(loc, cd->size(loc), Type::tsize_t); |
159 | 3649 if (!newargs) |
3650 newargs = new Expressions(); | |
3651 newargs->shift(e); | |
3652 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3653 f = cd->aggNew->overloadResolve(loc, newargs); |
159 | 3654 allocator = f->isNewDeclaration(); |
3655 assert(allocator); | |
3656 | |
3657 tf = (TypeFunction *)f->type; | |
3658 functionArguments(loc, sc, tf, newargs); | |
3659 } | |
3660 else | |
3661 { | |
3662 if (newargs && newargs->dim) | |
3663 error("no allocator for %s", cd->toChars()); | |
3664 } | |
3665 } | |
3666 else if (tb->ty == Tstruct) | |
3667 { | |
3668 TypeStruct *ts = (TypeStruct *)tb; | |
3669 StructDeclaration *sd = ts->sym; | |
3670 FuncDeclaration *f = sd->aggNew; | |
3671 TypeFunction *tf; | |
3672 | |
3673 if (arguments && arguments->dim) | |
3674 error("no constructor for %s", type->toChars()); | |
3675 | |
3676 if (f) | |
3677 { | |
3678 Expression *e; | |
3679 | |
3680 // Prepend the uint size argument to newargs[] | |
3681 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3682 if (!newargs) | |
3683 newargs = new Expressions(); | |
3684 newargs->shift(e); | |
3685 | |
3686 f = f->overloadResolve(loc, newargs); | |
3687 allocator = f->isNewDeclaration(); | |
3688 assert(allocator); | |
3689 | |
3690 tf = (TypeFunction *)f->type; | |
3691 functionArguments(loc, sc, tf, newargs); | |
3692 | |
3693 e = new VarExp(loc, f); | |
3694 e = new CallExp(loc, e, newargs); | |
3695 e = e->semantic(sc); | |
3696 e->type = type->pointerTo(); | |
3697 return e; | |
3698 } | |
3699 | |
3700 type = type->pointerTo(); | |
3701 } | |
3702 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3703 { | |
3704 for (size_t i = 0; i < arguments->dim; i++) | |
3705 { | |
3706 if (tb->ty != Tarray) | |
3707 { error("too many arguments for array"); | |
3708 arguments->dim = i; | |
3709 break; | |
3710 } | |
3711 | |
3712 Expression *arg = (Expression *)arguments->data[i]; | |
3713 arg = resolveProperties(sc, arg); | |
3714 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3715 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3716 error("negative array index %s", arg->toChars()); | |
3717 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
|
3718 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3719 } |
3720 } | |
3721 else if (tb->isscalar()) | |
3722 { | |
3723 if (arguments && arguments->dim) | |
3724 error("no constructor for %s", type->toChars()); | |
3725 | |
3726 type = type->pointerTo(); | |
3727 } | |
3728 else | |
3729 { | |
3730 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3731 type = type->pointerTo(); | |
3732 } | |
3733 | |
3734 //printf("NewExp: '%s'\n", toChars()); | |
3735 //printf("NewExp:type '%s'\n", type->toChars()); | |
3736 | |
3737 return this; | |
3738 } | |
3739 | |
3740 int NewExp::checkSideEffect(int flag) | |
3741 { | |
3742 return 1; | |
3743 } | |
3744 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3745 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3746 int NewExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3747 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3748 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3749 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3750 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3751 |
159 | 3752 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3753 { int i; | |
3754 | |
3755 if (thisexp) | |
3756 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3757 buf->writeByte('.'); | |
3758 } | |
3759 buf->writestring("new "); | |
3760 if (newargs && newargs->dim) | |
3761 { | |
3762 buf->writeByte('('); | |
3763 argsToCBuffer(buf, newargs, hgs); | |
3764 buf->writeByte(')'); | |
3765 } | |
3766 newtype->toCBuffer(buf, NULL, hgs); | |
3767 if (arguments && arguments->dim) | |
3768 { | |
3769 buf->writeByte('('); | |
3770 argsToCBuffer(buf, arguments, hgs); | |
3771 buf->writeByte(')'); | |
3772 } | |
3773 } | |
3774 | |
3775 /********************** NewAnonClassExp **************************************/ | |
3776 | |
3777 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3778 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3779 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3780 { | |
3781 this->thisexp = thisexp; | |
3782 this->newargs = newargs; | |
3783 this->cd = cd; | |
3784 this->arguments = arguments; | |
3785 } | |
3786 | |
3787 Expression *NewAnonClassExp::syntaxCopy() | |
3788 { | |
3789 return new NewAnonClassExp(loc, | |
3790 thisexp ? thisexp->syntaxCopy() : NULL, | |
3791 arraySyntaxCopy(newargs), | |
3792 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3793 arraySyntaxCopy(arguments)); | |
3794 } | |
3795 | |
3796 | |
3797 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3798 { | |
3799 #if LOGSEMANTIC | |
3800 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3801 //printf("thisexp = %p\n", thisexp); |
159 | 3802 //printf("type: %s\n", type->toChars()); |
3803 #endif | |
3804 | |
3805 Expression *d = new DeclarationExp(loc, cd); | |
3806 d = d->semantic(sc); | |
3807 | |
3808 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3809 | |
3810 Expression *c = new CommaExp(loc, d, n); | |
3811 return c->semantic(sc); | |
3812 } | |
3813 | |
3814 int NewAnonClassExp::checkSideEffect(int flag) | |
3815 { | |
3816 return 1; | |
3817 } | |
3818 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3819 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3820 int NewAnonClassExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3821 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3822 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3823 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3824 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3825 |
159 | 3826 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3827 { int i; | |
3828 | |
3829 if (thisexp) | |
3830 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3831 buf->writeByte('.'); | |
3832 } | |
3833 buf->writestring("new"); | |
3834 if (newargs && newargs->dim) | |
3835 { | |
3836 buf->writeByte('('); | |
3837 argsToCBuffer(buf, newargs, hgs); | |
3838 buf->writeByte(')'); | |
3839 } | |
3840 buf->writestring(" class "); | |
3841 if (arguments && arguments->dim) | |
3842 { | |
3843 buf->writeByte('('); | |
3844 argsToCBuffer(buf, arguments, hgs); | |
3845 buf->writeByte(')'); | |
3846 } | |
3847 //buf->writestring(" { }"); | |
3848 if (cd) | |
3849 { | |
3850 cd->toCBuffer(buf, hgs); | |
3851 } | |
3852 } | |
3853 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3854 /********************** SymbolExp **************************************/ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3855 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3856 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3857 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
|
3858 : Expression(loc, op, size) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3859 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3860 assert(var); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3861 this->var = var; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3862 this->hasOverloads = hasOverloads; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3863 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3864 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3865 |
159 | 3866 /********************** SymOffExp **************************************/ |
3867 | |
3868 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3869 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3870 { | |
3871 assert(var); | |
3872 this->var = var; | |
3873 this->offset = offset; | |
3874 VarDeclaration *v = var->isVarDeclaration(); | |
3875 if (v && v->needThis()) | |
3876 error("need 'this' for address of %s", v->toChars()); | |
3877 } | |
3878 | |
3879 Expression *SymOffExp::semantic(Scope *sc) | |
3880 { | |
3881 #if LOGSEMANTIC | |
3882 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3883 #endif | |
3884 //var->semantic(sc); | |
3885 if (!type) | |
3886 type = var->type->pointerTo(); | |
3887 VarDeclaration *v = var->isVarDeclaration(); | |
3888 if (v) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
3889 v->checkNestedReference(sc, loc); |
159 | 3890 return this; |
3891 } | |
3892 | |
3893 int SymOffExp::isBool(int result) | |
3894 { | |
3895 return result ? TRUE : FALSE; | |
3896 } | |
3897 | |
3898 void SymOffExp::checkEscape() | |
3899 { | |
3900 VarDeclaration *v = var->isVarDeclaration(); | |
3901 if (v) | |
3902 { | |
3903 if (!v->isDataseg()) | |
3904 error("escaping reference to local variable %s", v->toChars()); | |
3905 } | |
3906 } | |
3907 | |
3908 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3909 { | |
3910 if (offset) | |
3911 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3912 else | |
3913 buf->printf("& %s", var->toChars()); | |
3914 } | |
3915 | |
3916 /******************************** VarExp **************************/ | |
3917 | |
3918 VarExp::VarExp(Loc loc, Declaration *var) | |
3919 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3920 { | |
3921 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3922 this->var = var; | |
3923 this->type = var->type; | |
3924 } | |
3925 | |
3926 int VarExp::equals(Object *o) | |
3927 { VarExp *ne; | |
3928 | |
3929 if (this == o || | |
3930 (((Expression *)o)->op == TOKvar && | |
3931 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3932 var == ne->var)) | |
3933 return 1; | |
3934 return 0; | |
3935 } | |
3936 | |
3937 Expression *VarExp::semantic(Scope *sc) | |
3938 { FuncLiteralDeclaration *fd; | |
3939 | |
3940 #if LOGSEMANTIC | |
3941 printf("VarExp::semantic(%s)\n", toChars()); | |
3942 #endif | |
3943 if (!type) | |
3944 { type = var->type; | |
3945 #if 0 | |
3946 if (var->storage_class & STClazy) | |
3947 { | |
3948 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3949 type = new TypeDelegate(tf); | |
3950 type = type->semantic(loc, sc); | |
3951 } | |
3952 #endif | |
3953 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3954 /* 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
|
3955 * problems when instantiating imported templates passing private |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3956 * variables as alias template parameters. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3957 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3958 //accessCheck(loc, sc, NULL, var); |
159 | 3959 |
3960 VarDeclaration *v = var->isVarDeclaration(); | |
3961 if (v) | |
3962 { | |
3963 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3964 { | |
3965 ExpInitializer *ei = v->init->isExpInitializer(); | |
3966 if (ei) | |
3967 { | |
3968 //ei->exp->implicitCastTo(sc, type)->print(); | |
3969 return ei->exp->implicitCastTo(sc, type); | |
3970 } | |
3971 } | |
3972 v->checkNestedReference(sc, loc); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3973 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3974 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
|
3975 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3976 if (v->isDataseg() && !v->isInvariant()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3977 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
|
3978 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3979 #endif |
159 | 3980 } |
3981 #if 0 | |
3982 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3983 { Expression *e; | |
3984 e = new FuncExp(loc, fd); | |
3985 e->type = type; | |
3986 return e; | |
3987 } | |
3988 #endif | |
3989 return this; | |
3990 } | |
3991 | |
3992 char *VarExp::toChars() | |
3993 { | |
3994 return var->toChars(); | |
3995 } | |
3996 | |
3997 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3998 { | |
3999 buf->writestring(var->toChars()); | |
4000 } | |
4001 | |
4002 void VarExp::checkEscape() | |
4003 { | |
4004 VarDeclaration *v = var->isVarDeclaration(); | |
4005 if (v) | |
4006 { Type *tb = v->type->toBasetype(); | |
4007 // if reference type | |
4008 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
4009 { | |
4010 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
4011 error("escaping reference to auto local %s", v->toChars()); | |
4012 else if (v->storage_class & STCvariadic) | |
4013 error("escaping reference to variadic parameter %s", v->toChars()); | |
4014 } | |
4015 } | |
4016 } | |
4017 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4018 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4019 int VarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4020 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4021 if (var->storage_class & STClazy) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4022 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4023 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4024 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4025 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4026 |
159 | 4027 Expression *VarExp::toLvalue(Scope *sc, Expression *e) |
4028 { | |
4029 #if 0 | |
4030 tym = tybasic(e1->ET->Tty); | |
4031 if (!(tyscalar(tym) || | |
4032 tym == TYstruct || | |
4033 tym == TYarray && e->Eoper == TOKaddr)) | |
4034 synerr(EM_lvalue); // lvalue expected | |
4035 #endif | |
4036 if (var->storage_class & STClazy) | |
4037 error("lazy variables cannot be lvalues"); | |
4038 return this; | |
4039 } | |
4040 | |
4041 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
4042 { | |
4043 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
4044 if (sc->incontract && var->isParameter()) | |
4045 error("cannot modify parameter '%s' in contract", var->toChars()); | |
4046 | |
4047 if (type && type->toBasetype()->ty == Tsarray) | |
4048 error("cannot change reference to static array '%s'", var->toChars()); | |
4049 | |
4050 VarDeclaration *v = var->isVarDeclaration(); | |
4051 if (v && v->canassign == 0 && | |
4052 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
4053 error("cannot modify final variable '%s'", var->toChars()); | |
4054 | |
4055 if (var->isCtorinit()) | |
4056 { // It's only modifiable if inside the right constructor | |
4057 Dsymbol *s = sc->func; | |
4058 while (1) | |
4059 { | |
4060 FuncDeclaration *fd = NULL; | |
4061 if (s) | |
4062 fd = s->isFuncDeclaration(); | |
4063 if (fd && | |
4064 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
4065 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
4066 fd->toParent() == var->toParent() | |
4067 ) | |
4068 { | |
4069 VarDeclaration *v = var->isVarDeclaration(); | |
4070 assert(v); | |
4071 v->ctorinit = 1; | |
4072 //printf("setting ctorinit\n"); | |
4073 } | |
4074 else | |
4075 { | |
4076 if (s) | |
4077 { s = s->toParent2(); | |
4078 continue; | |
4079 } | |
4080 else | |
4081 { | |
4082 const char *p = var->isStatic() ? "static " : ""; | |
4083 error("can only initialize %sconst %s inside %sconstructor", | |
4084 p, var->toChars(), p); | |
4085 } | |
4086 } | |
4087 break; | |
4088 } | |
4089 } | |
4090 | |
4091 // See if this expression is a modifiable lvalue (i.e. not const) | |
4092 return toLvalue(sc, e); | |
4093 } | |
4094 | |
4095 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4096 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4097 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4098 #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
|
4099 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
|
4100 : 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
|
4101 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4102 //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
|
4103 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
|
4104 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4107 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
|
4108 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4109 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4112 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
|
4113 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4114 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
|
4115 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4116 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4117 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4118 |
159 | 4119 /******************************** TupleExp **************************/ |
4120 | |
4121 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
4122 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4123 { | |
4124 //printf("TupleExp(this = %p)\n", this); | |
4125 this->exps = exps; | |
4126 this->type = NULL; | |
4127 } | |
4128 | |
4129 | |
4130 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
4131 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4132 { | |
4133 exps = new Expressions(); | |
4134 type = NULL; | |
4135 | |
4136 exps->reserve(tup->objects->dim); | |
4137 for (size_t i = 0; i < tup->objects->dim; i++) | |
4138 { Object *o = (Object *)tup->objects->data[i]; | |
4139 if (o->dyncast() == DYNCAST_EXPRESSION) | |
4140 { | |
4141 Expression *e = (Expression *)o; | |
4142 e = e->syntaxCopy(); | |
4143 exps->push(e); | |
4144 } | |
4145 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
4146 { | |
4147 Dsymbol *s = (Dsymbol *)o; | |
4148 Expression *e = new DsymbolExp(loc, s); | |
4149 exps->push(e); | |
4150 } | |
4151 else if (o->dyncast() == DYNCAST_TYPE) | |
4152 { | |
4153 Type *t = (Type *)o; | |
4154 Expression *e = new TypeExp(loc, t); | |
4155 exps->push(e); | |
4156 } | |
4157 else | |
4158 { | |
4159 error("%s is not an expression", o->toChars()); | |
4160 } | |
4161 } | |
4162 } | |
4163 | |
4164 int TupleExp::equals(Object *o) | |
4165 { TupleExp *ne; | |
4166 | |
4167 if (this == o) | |
4168 return 1; | |
4169 if (((Expression *)o)->op == TOKtuple) | |
4170 { | |
4171 TupleExp *te = (TupleExp *)o; | |
4172 if (exps->dim != te->exps->dim) | |
4173 return 0; | |
4174 for (size_t i = 0; i < exps->dim; i++) | |
4175 { Expression *e1 = (Expression *)exps->data[i]; | |
4176 Expression *e2 = (Expression *)te->exps->data[i]; | |
4177 | |
4178 if (!e1->equals(e2)) | |
4179 return 0; | |
4180 } | |
4181 return 1; | |
4182 } | |
4183 return 0; | |
4184 } | |
4185 | |
4186 Expression *TupleExp::syntaxCopy() | |
4187 { | |
4188 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
4189 } | |
4190 | |
4191 Expression *TupleExp::semantic(Scope *sc) | |
4192 { | |
4193 #if LOGSEMANTIC | |
4194 printf("+TupleExp::semantic(%s)\n", toChars()); | |
4195 #endif | |
4196 if (type) | |
4197 return this; | |
4198 | |
4199 // Run semantic() on each argument | |
4200 for (size_t i = 0; i < exps->dim; i++) | |
4201 { Expression *e = (Expression *)exps->data[i]; | |
4202 | |
4203 e = e->semantic(sc); | |
4204 if (!e->type) | |
4205 { error("%s has no value", e->toChars()); | |
4206 e->type = Type::terror; | |
4207 } | |
4208 exps->data[i] = (void *)e; | |
4209 } | |
4210 | |
4211 expandTuples(exps); | |
4212 if (0 && exps->dim == 1) | |
4213 { | |
4214 return (Expression *)exps->data[0]; | |
4215 } | |
4216 type = new TypeTuple(exps); | |
4217 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
4218 return this; | |
4219 } | |
4220 | |
4221 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4222 { | |
4223 buf->writestring("tuple("); | |
4224 argsToCBuffer(buf, exps, hgs); | |
4225 buf->writeByte(')'); | |
4226 } | |
4227 | |
4228 int TupleExp::checkSideEffect(int flag) | |
4229 { int f = 0; | |
4230 | |
4231 for (int i = 0; i < exps->dim; i++) | |
4232 { Expression *e = (Expression *)exps->data[i]; | |
4233 | |
4234 f |= e->checkSideEffect(2); | |
4235 } | |
4236 if (flag == 0 && f == 0) | |
4237 Expression::checkSideEffect(0); | |
4238 return f; | |
4239 } | |
4240 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4241 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4242 int TupleExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4243 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4244 return arrayExpressionCanThrow(exps); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4245 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4246 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4247 |
159 | 4248 void TupleExp::checkEscape() |
4249 { | |
4250 for (size_t i = 0; i < exps->dim; i++) | |
4251 { Expression *e = (Expression *)exps->data[i]; | |
4252 e->checkEscape(); | |
4253 } | |
4254 } | |
4255 | |
4256 /******************************** FuncExp *********************************/ | |
4257 | |
4258 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4259 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4260 { | |
4261 this->fd = fd; | |
4262 } | |
4263 | |
4264 Expression *FuncExp::syntaxCopy() | |
4265 { | |
4266 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4267 } | |
4268 | |
4269 Expression *FuncExp::semantic(Scope *sc) | |
4270 { | |
4271 #if LOGSEMANTIC | |
4272 printf("FuncExp::semantic(%s)\n", toChars()); | |
4273 #endif | |
4274 if (!type) | |
4275 { | |
4276 fd->semantic(sc); | |
4277 fd->parent = sc->parent; | |
4278 if (global.errors) | |
4279 { | |
4280 if (!fd->type->next) | |
4281 fd->type->next = Type::terror; | |
4282 } | |
4283 else | |
4284 { | |
4285 fd->semantic2(sc); | |
4286 if (!global.errors) | |
4287 { | |
4288 fd->semantic3(sc); | |
4289 | |
4290 if (!global.errors && global.params.useInline) | |
4291 fd->inlineScan(); | |
4292 } | |
4293 } | |
4294 | |
4295 // Type is a "delegate to" or "pointer to" the function literal | |
4296 if (fd->isNested()) | |
4297 { | |
4298 type = new TypeDelegate(fd->type); | |
4299 type = type->semantic(loc, sc); | |
4300 } | |
4301 else | |
4302 { | |
4303 type = fd->type->pointerTo(); | |
4304 } | |
4305 } | |
4306 return this; | |
4307 } | |
4308 | |
4309 char *FuncExp::toChars() | |
4310 { | |
4311 return fd->toChars(); | |
4312 } | |
4313 | |
4314 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4315 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
4316 fd->toCBuffer(buf, hgs); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
4317 //buf->writestring(fd->toChars()); |
159 | 4318 } |
4319 | |
4320 | |
4321 /******************************** DeclarationExp **************************/ | |
4322 | |
4323 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4324 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4325 { | |
4326 this->declaration = declaration; | |
4327 } | |
4328 | |
4329 Expression *DeclarationExp::syntaxCopy() | |
4330 { | |
4331 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4332 } | |
4333 | |
4334 Expression *DeclarationExp::semantic(Scope *sc) | |
4335 { | |
4336 if (type) | |
4337 return this; | |
4338 | |
4339 #if LOGSEMANTIC | |
4340 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4341 #endif | |
4342 | |
4343 /* This is here to support extern(linkage) declaration, | |
4344 * where the extern(linkage) winds up being an AttribDeclaration | |
4345 * wrapper. | |
4346 */ | |
4347 Dsymbol *s = declaration; | |
4348 | |
4349 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4350 if (ad) | |
4351 { | |
4352 if (ad->decl && ad->decl->dim == 1) | |
4353 s = (Dsymbol *)ad->decl->data[0]; | |
4354 } | |
4355 | |
4356 if (s->isVarDeclaration()) | |
4357 { // Do semantic() on initializer first, so: | |
4358 // int a = a; | |
4359 // will be illegal. | |
4360 declaration->semantic(sc); | |
4361 s->parent = sc->parent; | |
4362 } | |
4363 | |
4364 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4365 // Insert into both local scope and function scope. | |
4366 // Must be unique in both. | |
4367 if (s->ident) | |
4368 { | |
4369 if (!sc->insert(s)) | |
4370 error("declaration %s is already defined", s->toPrettyChars()); | |
4371 else if (sc->func) | |
4372 { VarDeclaration *v = s->isVarDeclaration(); | |
4373 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4374 !sc->func->localsymtab->insert(s)) | |
4375 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4376 else if (!global.params.useDeprecated) | |
4377 { // Disallow shadowing | |
4378 | |
4379 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4380 { Dsymbol *s2; | |
4381 | |
4382 if (scx->scopesym && scx->scopesym->symtab && | |
4383 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4384 s != s2) | |
4385 { | |
4386 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4387 } | |
4388 } | |
4389 } | |
4390 } | |
4391 } | |
4392 if (!s->isVarDeclaration()) | |
4393 { | |
4394 declaration->semantic(sc); | |
4395 s->parent = sc->parent; | |
4396 } | |
4397 if (!global.errors) | |
4398 { | |
4399 declaration->semantic2(sc); | |
4400 if (!global.errors) | |
4401 { | |
4402 declaration->semantic3(sc); | |
4403 | |
4404 if (!global.errors && global.params.useInline) | |
4405 declaration->inlineScan(); | |
4406 } | |
4407 } | |
4408 | |
4409 type = Type::tvoid; | |
4410 return this; | |
4411 } | |
4412 | |
4413 int DeclarationExp::checkSideEffect(int flag) | |
4414 { | |
4415 return 1; | |
4416 } | |
4417 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4418 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4419 int DeclarationExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4420 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4421 VarDeclaration *v = declaration->isVarDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4422 if (v && v->init) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4423 { ExpInitializer *ie = v->init->isExpInitializer(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4424 return ie && ie->exp->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4425 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4426 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4427 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4428 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4429 |
159 | 4430 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4431 { | |
4432 declaration->toCBuffer(buf, hgs); | |
4433 } | |
4434 | |
4435 | |
4436 /************************ TypeidExp ************************************/ | |
4437 | |
4438 /* | |
4439 * typeid(int) | |
4440 */ | |
4441 | |
4442 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4443 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4444 { | |
4445 this->typeidType = typeidType; | |
4446 } | |
4447 | |
4448 | |
4449 Expression *TypeidExp::syntaxCopy() | |
4450 { | |
4451 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4452 } | |
4453 | |
4454 | |
4455 Expression *TypeidExp::semantic(Scope *sc) | |
4456 { Expression *e; | |
4457 | |
4458 #if LOGSEMANTIC | |
4459 printf("TypeidExp::semantic()\n"); | |
4460 #endif | |
4461 typeidType = typeidType->semantic(loc, sc); | |
4462 e = typeidType->getTypeInfo(sc); | |
336 | 4463 if (e->loc.linnum == 0) |
4464 e->loc = loc; // so there's at least some line number info | |
159 | 4465 return e; |
4466 } | |
4467 | |
4468 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4469 { | |
4470 buf->writestring("typeid("); | |
4471 typeidType->toCBuffer(buf, NULL, hgs); | |
4472 buf->writeByte(')'); | |
4473 } | |
4474 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4475 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4476 #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
|
4477 /* |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4478 * __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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4481 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
|
4482 : 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
|
4483 { |
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->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
|
4485 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4489 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
|
4490 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4491 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4495 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
|
4496 { |
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("__traits("); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4498 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
|
4499 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
|
4500 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4501 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
|
4502 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4503 buf->writeByte(','); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4504 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
|
4505 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
|
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 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4508 buf->writeByte(')'); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4509 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4510 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4511 |
159 | 4512 /************************************************************/ |
4513 | |
4514 HaltExp::HaltExp(Loc loc) | |
4515 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4516 { | |
4517 } | |
4518 | |
4519 Expression *HaltExp::semantic(Scope *sc) | |
4520 { | |
4521 #if LOGSEMANTIC | |
4522 printf("HaltExp::semantic()\n"); | |
4523 #endif | |
4524 type = Type::tvoid; | |
4525 return this; | |
4526 } | |
4527 | |
4528 int HaltExp::checkSideEffect(int flag) | |
4529 { | |
4530 return 1; | |
4531 } | |
4532 | |
4533 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4534 { | |
4535 buf->writestring("halt"); | |
4536 } | |
4537 | |
4538 /************************************************************/ | |
4539 | |
4540 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4541 Type *tspec, enum TOK tok2) | |
4542 : Expression(loc, TOKis, sizeof(IsExp)) | |
4543 { | |
4544 this->targ = targ; | |
4545 this->id = id; | |
4546 this->tok = tok; | |
4547 this->tspec = tspec; | |
4548 this->tok2 = tok2; | |
4549 } | |
4550 | |
4551 Expression *IsExp::syntaxCopy() | |
4552 { | |
4553 return new IsExp(loc, | |
4554 targ->syntaxCopy(), | |
4555 id, | |
4556 tok, | |
4557 tspec ? tspec->syntaxCopy() : NULL, | |
4558 tok2); | |
4559 } | |
4560 | |
4561 Expression *IsExp::semantic(Scope *sc) | |
4562 { Type *tded; | |
4563 | |
510
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 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
|
4565 * 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4568 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4569 if (id && !(sc->flags & SCOPEstaticif)) |
4570 error("can only declare type aliases within static if conditionals"); | |
4571 | |
4572 unsigned errors_save = global.errors; | |
4573 global.errors = 0; | |
4574 global.gag++; // suppress printing of error messages | |
4575 targ = targ->semantic(loc, sc); | |
4576 global.gag--; | |
4577 unsigned gerrors = global.errors; | |
4578 global.errors = errors_save; | |
4579 | |
4580 if (gerrors) // if any errors happened | |
4581 { // then condition is false | |
4582 goto Lno; | |
4583 } | |
4584 else if (tok2 != TOKreserved) | |
4585 { | |
4586 switch (tok2) | |
4587 { | |
4588 case TOKtypedef: | |
4589 if (targ->ty != Ttypedef) | |
4590 goto Lno; | |
4591 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4592 break; | |
4593 | |
4594 case TOKstruct: | |
4595 if (targ->ty != Tstruct) | |
4596 goto Lno; | |
4597 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4598 goto Lno; | |
4599 tded = targ; | |
4600 break; | |
4601 | |
4602 case TOKunion: | |
4603 if (targ->ty != Tstruct) | |
4604 goto Lno; | |
4605 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4606 goto Lno; | |
4607 tded = targ; | |
4608 break; | |
4609 | |
4610 case TOKclass: | |
4611 if (targ->ty != Tclass) | |
4612 goto Lno; | |
4613 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4614 goto Lno; | |
4615 tded = targ; | |
4616 break; | |
4617 | |
4618 case TOKinterface: | |
4619 if (targ->ty != Tclass) | |
4620 goto Lno; | |
4621 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4622 goto Lno; | |
4623 tded = targ; | |
4624 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
|
4625 #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
|
4626 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
|
4627 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
|
4628 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
|
4629 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
|
4630 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4631 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4632 case TOKinvariant: |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4633 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
|
4634 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
|
4635 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
|
4636 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
|
4637 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4638 #endif |
159 | 4639 |
4640 case TOKsuper: | |
4641 // If class or interface, get the base class and interfaces | |
4642 if (targ->ty != Tclass) | |
4643 goto Lno; | |
4644 else | |
4645 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4646 Arguments *args = new Arguments; | |
4647 args->reserve(cd->baseclasses.dim); | |
4648 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4649 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4650 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4651 } | |
4652 tded = new TypeTuple(args); | |
4653 } | |
4654 break; | |
4655 | |
4656 case TOKenum: | |
4657 if (targ->ty != Tenum) | |
4658 goto Lno; | |
4659 tded = ((TypeEnum *)targ)->sym->memtype; | |
4660 break; | |
4661 | |
4662 case TOKdelegate: | |
4663 if (targ->ty != Tdelegate) | |
4664 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
|
4665 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4666 break; |
4667 | |
4668 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
|
4669 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4670 if (targ->ty != Tfunction) |
159 | 4671 goto Lno; |
4672 tded = targ; | |
4673 | |
4674 /* Generate tuple from function parameter types. | |
4675 */ | |
4676 assert(tded->ty == Tfunction); | |
4677 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4678 size_t dim = Argument::dim(params); | |
4679 Arguments *args = new Arguments; | |
4680 args->reserve(dim); | |
4681 for (size_t i = 0; i < dim; i++) | |
4682 { Argument *arg = Argument::getNth(params, i); | |
4683 assert(arg && arg->type); | |
4684 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4685 } | |
4686 tded = new TypeTuple(args); | |
4687 break; | |
4688 } | |
4689 case TOKreturn: | |
4690 /* Get the 'return type' for the function, | |
4691 * delegate, or pointer to function. | |
4692 */ | |
4693 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
|
4694 tded = ((TypeFunction *)targ)->next; |
159 | 4695 else if (targ->ty == Tdelegate) |
4696 tded = targ->next->next; | |
4697 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4698 tded = targ->next->next; | |
4699 else | |
4700 goto Lno; | |
4701 break; | |
4702 | |
4703 default: | |
4704 assert(0); | |
4705 } | |
4706 goto Lyes; | |
4707 } | |
4708 else if (id && tspec) | |
4709 { | |
4710 /* Evaluate to TRUE if targ matches tspec. | |
4711 * If TRUE, declare id as an alias for the specialized type. | |
4712 */ | |
4713 | |
4714 MATCH m; | |
4715 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4716 | |
4717 TemplateParameters parameters; | |
4718 parameters.setDim(1); | |
4719 parameters.data[0] = (void *)&tp; | |
4720 | |
4721 Objects dedtypes; | |
4722 dedtypes.setDim(1); | |
4723 dedtypes.data[0] = NULL; | |
4724 | |
4725 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4726 if (m == MATCHnomatch || | |
4727 (m != MATCHexact && tok == TOKequal)) | |
4728 goto Lno; | |
4729 else | |
4730 { | |
4731 assert(dedtypes.dim == 1); | |
4732 tded = (Type *)dedtypes.data[0]; | |
4733 if (!tded) | |
4734 tded = targ; | |
4735 goto Lyes; | |
4736 } | |
4737 } | |
4738 else if (id) | |
4739 { | |
4740 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4741 */ | |
4742 tded = targ; | |
4743 goto Lyes; | |
4744 } | |
4745 else if (tspec) | |
4746 { | |
4747 /* Evaluate to TRUE if targ matches tspec | |
4748 */ | |
4749 tspec = tspec->semantic(loc, sc); | |
4750 //printf("targ = %s\n", targ->toChars()); | |
4751 //printf("tspec = %s\n", tspec->toChars()); | |
4752 if (tok == TOKcolon) | |
4753 { if (targ->implicitConvTo(tspec)) | |
4754 goto Lyes; | |
4755 else | |
4756 goto Lno; | |
4757 } | |
4758 else /* == */ | |
4759 { if (targ->equals(tspec)) | |
4760 goto Lyes; | |
4761 else | |
4762 goto Lno; | |
4763 } | |
4764 } | |
4765 | |
4766 Lyes: | |
4767 if (id) | |
4768 { | |
4769 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4770 s->semantic(sc); | |
4771 sc->insert(s); | |
4772 if (sc->sd) | |
4773 s->addMember(sc, sc->sd, 1); | |
4774 } | |
4775 return new IntegerExp(1); | |
4776 | |
4777 Lno: | |
4778 return new IntegerExp(0); | |
4779 } | |
4780 | |
4781 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4782 { | |
4783 buf->writestring("is("); | |
4784 targ->toCBuffer(buf, id, hgs); | |
4785 if (tok2 != TOKreserved) | |
4786 { | |
4787 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4788 } | |
4789 else if (tspec) | |
4790 { | |
4791 if (tok == TOKcolon) | |
4792 buf->writestring(" : "); | |
4793 else | |
4794 buf->writestring(" == "); | |
4795 tspec->toCBuffer(buf, NULL, hgs); | |
4796 } | |
336 | 4797 #if DMDV2 |
159 | 4798 if (parameters) |
4799 { // First parameter is already output, so start with second | |
4800 for (int i = 1; i < parameters->dim; i++) | |
4801 { | |
4802 buf->writeByte(','); | |
4803 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4804 tp->toCBuffer(buf, hgs); | |
4805 } | |
4806 } | |
4807 #endif | |
4808 buf->writeByte(')'); | |
4809 } | |
4810 | |
4811 | |
4812 /************************************************************/ | |
4813 | |
4814 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4815 : Expression(loc, op, size) | |
4816 { | |
4817 this->e1 = e1; | |
4818 } | |
4819 | |
4820 Expression *UnaExp::syntaxCopy() | |
4821 { UnaExp *e; | |
4822 | |
4823 e = (UnaExp *)copy(); | |
4824 e->type = NULL; | |
4825 e->e1 = e->e1->syntaxCopy(); | |
4826 return e; | |
4827 } | |
4828 | |
4829 Expression *UnaExp::semantic(Scope *sc) | |
4830 { | |
4831 #if LOGSEMANTIC | |
4832 printf("UnaExp::semantic('%s')\n", toChars()); | |
4833 #endif | |
4834 e1 = e1->semantic(sc); | |
4835 // if (!e1->type) | |
4836 // error("%s has no value", e1->toChars()); | |
4837 return this; | |
4838 } | |
4839 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4840 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4841 int UnaExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4842 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4843 return e1->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4844 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4845 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4846 |
159 | 4847 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4848 { | |
4849 buf->writestring(Token::toChars(op)); | |
4850 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4851 } | |
4852 | |
4853 /************************************************************/ | |
4854 | |
4855 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4856 : Expression(loc, op, size) | |
4857 { | |
4858 this->e1 = e1; | |
4859 this->e2 = e2; | |
4860 } | |
4861 | |
4862 Expression *BinExp::syntaxCopy() | |
4863 { BinExp *e; | |
4864 | |
4865 e = (BinExp *)copy(); | |
4866 e->type = NULL; | |
4867 e->e1 = e->e1->syntaxCopy(); | |
4868 e->e2 = e->e2->syntaxCopy(); | |
4869 return e; | |
4870 } | |
4871 | |
4872 Expression *BinExp::semantic(Scope *sc) | |
4873 { | |
4874 #if LOGSEMANTIC | |
4875 printf("BinExp::semantic('%s')\n", toChars()); | |
4876 #endif | |
4877 e1 = e1->semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4878 if (!e1->type && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4879 !(op == TOKassign && e1->op == TOKdottd)) // a.template = e2 |
159 | 4880 { |
4881 error("%s has no value", e1->toChars()); | |
4882 e1->type = Type::terror; | |
4883 } | |
4884 e2 = e2->semantic(sc); | |
4885 if (!e2->type) | |
4886 { | |
4887 error("%s has no value", e2->toChars()); | |
4888 e2->type = Type::terror; | |
4889 } | |
4890 return this; | |
4891 } | |
4892 | |
4893 Expression *BinExp::semanticp(Scope *sc) | |
4894 { | |
4895 BinExp::semantic(sc); | |
4896 e1 = resolveProperties(sc, e1); | |
4897 e2 = resolveProperties(sc, e2); | |
4898 return this; | |
4899 } | |
4900 | |
4901 /*************************** | |
4902 * Common semantic routine for some xxxAssignExp's. | |
4903 */ | |
4904 | |
4905 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4906 { Expression *e; | |
4907 | |
4908 if (!type) | |
4909 { | |
4910 BinExp::semantic(sc); | |
4911 e2 = resolveProperties(sc, e2); | |
4912 | |
4913 e = op_overload(sc); | |
4914 if (e) | |
4915 return e; | |
4916 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4917 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
|
4918 { // 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
|
4919 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
|
4920 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
|
4921 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
|
4922 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4923 |
159 | 4924 e1 = e1->modifiableLvalue(sc, e1); |
4925 e1->checkScalar(); | |
4926 type = e1->type; | |
4927 if (type->toBasetype()->ty == Tbool) | |
4928 { | |
4929 error("operator not allowed on bool expression %s", toChars()); | |
4930 } | |
4931 typeCombine(sc); | |
4932 e1->checkArithmetic(); | |
4933 e2->checkArithmetic(); | |
4934 | |
4935 if (op == TOKmodass && e2->type->iscomplex()) | |
4936 { error("cannot perform modulo complex arithmetic"); | |
4937 return new IntegerExp(0); | |
4938 } | |
4939 } | |
4940 return this; | |
4941 } | |
4942 | |
4943 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4944 { Expression *e; | |
4945 | |
4946 if (!type) | |
4947 { | |
4948 BinExp::semantic(sc); | |
4949 e2 = resolveProperties(sc, e2); | |
4950 | |
4951 e = op_overload(sc); | |
4952 if (e) | |
4953 return e; | |
4954 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4955 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
|
4956 { // 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
|
4957 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
|
4958 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
|
4959 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
|
4960 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4961 |
159 | 4962 e1 = e1->modifiableLvalue(sc, e1); |
4963 e1->checkScalar(); | |
4964 type = e1->type; | |
4965 if (type->toBasetype()->ty == Tbool) | |
4966 { | |
4967 e2 = e2->implicitCastTo(sc, type); | |
4968 } | |
4969 | |
4970 typeCombine(sc); | |
4971 e1->checkIntegral(); | |
4972 e2->checkIntegral(); | |
4973 } | |
4974 return this; | |
4975 } | |
4976 | |
4977 int BinExp::checkSideEffect(int flag) | |
4978 { | |
4979 if (op == TOKplusplus || | |
4980 op == TOKminusminus || | |
4981 op == TOKassign || | |
4982 op == TOKconstruct || | |
4983 op == TOKblit || | |
4984 op == TOKaddass || | |
4985 op == TOKminass || | |
4986 op == TOKcatass || | |
4987 op == TOKmulass || | |
4988 op == TOKdivass || | |
4989 op == TOKmodass || | |
4990 op == TOKshlass || | |
4991 op == TOKshrass || | |
4992 op == TOKushrass || | |
4993 op == TOKandass || | |
4994 op == TOKorass || | |
4995 op == TOKxorass || | |
4996 op == TOKin || | |
4997 op == TOKremove) | |
4998 return 1; | |
4999 return Expression::checkSideEffect(flag); | |
5000 } | |
5001 | |
5002 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5003 { | |
5004 expToCBuffer(buf, hgs, e1, precedence[op]); | |
5005 buf->writeByte(' '); | |
5006 buf->writestring(Token::toChars(op)); | |
5007 buf->writeByte(' '); | |
5008 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
5009 } | |
5010 | |
5011 int BinExp::isunsigned() | |
5012 { | |
5013 return e1->type->isunsigned() || e2->type->isunsigned(); | |
5014 } | |
5015 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5016 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5017 int BinExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5018 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5019 return e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5020 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5021 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5022 |
159 | 5023 void BinExp::incompatibleTypes() |
5024 { | |
5025 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
5026 e1->toChars(), Token::toChars(op), e2->toChars(), | |
5027 e1->type->toChars(), e2->type->toChars()); | |
5028 } | |
5029 | |
5030 /************************************************************/ | |
5031 | |
5032 CompileExp::CompileExp(Loc loc, Expression *e) | |
5033 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
5034 { | |
5035 } | |
5036 | |
5037 Expression *CompileExp::semantic(Scope *sc) | |
5038 { | |
5039 #if LOGSEMANTIC | |
5040 printf("CompileExp::semantic('%s')\n", toChars()); | |
5041 #endif | |
5042 UnaExp::semantic(sc); | |
5043 e1 = resolveProperties(sc, e1); | |
5044 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
5045 if (e1->op != TOKstring) | |
5046 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
5047 type = Type::terror; | |
5048 return this; | |
5049 } | |
5050 StringExp *se = (StringExp *)e1; | |
5051 se = se->toUTF8(sc); | |
5052 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
5053 p.loc = loc; | |
5054 p.nextToken(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5055 //printf("p.loc.linnum = %d\n", p.loc.linnum); |
159 | 5056 Expression *e = p.parseExpression(); |
5057 if (p.token.value != TOKeof) | |
5058 error("incomplete mixin expression (%s)", se->toChars()); | |
5059 return e->semantic(sc); | |
5060 } | |
5061 | |
5062 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5063 { | |
5064 buf->writestring("mixin("); | |
5065 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5066 buf->writeByte(')'); | |
5067 } | |
5068 | |
5069 /************************************************************/ | |
5070 | |
5071 FileExp::FileExp(Loc loc, Expression *e) | |
5072 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
5073 { | |
5074 } | |
5075 | |
5076 Expression *FileExp::semantic(Scope *sc) | |
5077 { char *name; | |
5078 StringExp *se; | |
5079 | |
5080 #if LOGSEMANTIC | |
5081 printf("FileExp::semantic('%s')\n", toChars()); | |
5082 #endif | |
5083 UnaExp::semantic(sc); | |
5084 e1 = resolveProperties(sc, e1); | |
5085 e1 = e1->optimize(WANTvalue); | |
5086 if (e1->op != TOKstring) | |
5087 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
5088 goto Lerror; | |
5089 } | |
5090 se = (StringExp *)e1; | |
5091 se = se->toUTF8(sc); | |
5092 name = (char *)se->string; | |
5093 | |
5094 if (!global.params.fileImppath) | |
5095 { error("need -Jpath switch to import text file %s", name); | |
5096 goto Lerror; | |
5097 } | |
5098 | |
5099 if (name != FileName::name(name)) | |
5100 { error("use -Jpath switch to provide path for filename %s", name); | |
5101 goto Lerror; | |
5102 } | |
5103 | |
5104 name = FileName::searchPath(global.filePath, name, 0); | |
5105 if (!name) | |
5106 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
5107 goto Lerror; | |
5108 } | |
5109 | |
5110 if (global.params.verbose) | |
794
661384d6a936
Fix warnings on x86-64. By fvbommel.
Christian Kamm <kamm incasoftware de>
parents:
779
diff
changeset
|
5111 printf("file %s\t(%s)\n", (char*)se->string, name); |
159 | 5112 |
5113 { File f(name); | |
5114 if (f.read()) | |
5115 { error("cannot read file %s", f.toChars()); | |
5116 goto Lerror; | |
5117 } | |
5118 else | |
5119 { | |
5120 f.ref = 1; | |
5121 se = new StringExp(loc, f.buffer, f.len); | |
5122 } | |
5123 } | |
5124 Lret: | |
5125 return se->semantic(sc); | |
5126 | |
5127 Lerror: | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5128 se = new StringExp(loc, (char *)""); |
159 | 5129 goto Lret; |
5130 } | |
5131 | |
5132 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5133 { | |
5134 buf->writestring("import("); | |
5135 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5136 buf->writeByte(')'); | |
5137 } | |
5138 | |
5139 /************************************************************/ | |
5140 | |
5141 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
5142 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
5143 { | |
5144 this->msg = msg; | |
5145 } | |
5146 | |
5147 Expression *AssertExp::syntaxCopy() | |
5148 { | |
5149 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
5150 msg ? msg->syntaxCopy() : NULL); | |
5151 return ae; | |
5152 } | |
5153 | |
5154 Expression *AssertExp::semantic(Scope *sc) | |
5155 { | |
5156 #if LOGSEMANTIC | |
5157 printf("AssertExp::semantic('%s')\n", toChars()); | |
5158 #endif | |
5159 UnaExp::semantic(sc); | |
5160 e1 = resolveProperties(sc, e1); | |
5161 // BUG: see if we can do compile time elimination of the Assert | |
5162 e1 = e1->optimize(WANTvalue); | |
5163 e1 = e1->checkToBoolean(); | |
5164 if (msg) | |
5165 { | |
5166 msg = msg->semantic(sc); | |
5167 msg = resolveProperties(sc, msg); | |
5168 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
5169 msg = msg->optimize(WANTvalue); | |
5170 } | |
5171 if (e1->isBool(FALSE)) | |
5172 { | |
5173 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
5174 fd->hasReturnExp |= 4; | |
5175 | |
5176 if (!global.params.useAssert) | |
5177 { Expression *e = new HaltExp(loc); | |
5178 e = e->semantic(sc); | |
5179 return e; | |
5180 } | |
5181 } | |
5182 type = Type::tvoid; | |
5183 return this; | |
5184 } | |
5185 | |
5186 int AssertExp::checkSideEffect(int flag) | |
5187 { | |
5188 return 1; | |
5189 } | |
5190 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5191 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5192 int AssertExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5193 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5194 return (global.params.useAssert != 0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5195 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5196 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5197 |
159 | 5198 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
5199 { | |
5200 buf->writestring("assert("); | |
5201 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5202 if (msg) | |
5203 { | |
5204 buf->writeByte(','); | |
5205 expToCBuffer(buf, hgs, msg, PREC_assign); | |
5206 } | |
5207 buf->writeByte(')'); | |
5208 } | |
5209 | |
5210 /************************************************************/ | |
5211 | |
5212 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
5213 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
5214 { | |
5215 this->ident = ident; | |
5216 } | |
5217 | |
5218 Expression *DotIdExp::semantic(Scope *sc) | |
5219 { Expression *e; | |
5220 Expression *eleft; | |
5221 Expression *eright; | |
5222 | |
5223 #if LOGSEMANTIC | |
5224 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
5225 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
5226 #endif | |
5227 | |
5228 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
5229 | |
5230 #if 0 | |
5231 /* Don't do semantic analysis if we'll be converting | |
5232 * it to a string. | |
5233 */ | |
5234 if (ident == Id::stringof) | |
5235 { char *s = e1->toChars(); | |
5236 e = new StringExp(loc, s, strlen(s), 'c'); | |
5237 e = e->semantic(sc); | |
5238 return e; | |
5239 } | |
5240 #endif | |
5241 | |
5242 /* Special case: rewrite this.id and super.id | |
5243 * to be classtype.id and baseclasstype.id | |
5244 * if we have no this pointer. | |
5245 */ | |
5246 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
5247 { ClassDeclaration *cd; | |
5248 StructDeclaration *sd; | |
5249 AggregateDeclaration *ad; | |
5250 | |
5251 ad = sc->getStructClassScope(); | |
5252 if (ad) | |
5253 { | |
5254 cd = ad->isClassDeclaration(); | |
5255 if (cd) | |
5256 { | |
5257 if (e1->op == TOKthis) | |
5258 { | |
5259 e = new TypeDotIdExp(loc, cd->type, ident); | |
5260 return e->semantic(sc); | |
5261 } | |
5262 else if (cd->baseClass && e1->op == TOKsuper) | |
5263 { | |
5264 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
5265 return e->semantic(sc); | |
5266 } | |
5267 } | |
5268 else | |
5269 { | |
5270 sd = ad->isStructDeclaration(); | |
5271 if (sd) | |
5272 { | |
5273 if (e1->op == TOKthis) | |
5274 { | |
5275 e = new TypeDotIdExp(loc, sd->type, ident); | |
5276 return e->semantic(sc); | |
5277 } | |
5278 } | |
5279 } | |
5280 } | |
5281 } | |
5282 | |
5283 UnaExp::semantic(sc); | |
5284 | |
5285 if (e1->op == TOKdotexp) | |
5286 { | |
5287 DotExp *de = (DotExp *)e1; | |
5288 eleft = de->e1; | |
5289 eright = de->e2; | |
5290 } | |
5291 else | |
5292 { | |
5293 e1 = resolveProperties(sc, e1); | |
5294 eleft = NULL; | |
5295 eright = e1; | |
5296 } | |
510
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 DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5298 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
|
5299 { /* '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
|
5300 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5301 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
|
5302 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
|
5303 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
|
5304 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
|
5305 { 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
|
5306 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
|
5307 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
|
5308 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
|
5309 } |
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 = 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
|
5311 e = e->semantic(sc); |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5312 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
|
5313 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5314 #endif |
159 | 5315 |
5316 if (e1->op == TOKtuple && ident == Id::length) | |
5317 { | |
5318 TupleExp *te = (TupleExp *)e1; | |
5319 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5320 return e; | |
5321 } | |
5322 | |
5323 if (eright->op == TOKimport) // also used for template alias's | |
5324 { | |
5325 ScopeExp *ie = (ScopeExp *)eright; | |
5326 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5327 /* 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
|
5328 * 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
|
5329 * 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
|
5330 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5331 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
|
5332 (ie->sds->isModule() && ie->sds != sc->module) ? 1 : 0); |
159 | 5333 if (s) |
5334 { | |
5335 s = s->toAlias(); | |
5336 checkDeprecated(sc, s); | |
5337 | |
5338 EnumMember *em = s->isEnumMember(); | |
5339 if (em) | |
5340 { | |
5341 e = em->value; | |
5342 e = e->semantic(sc); | |
5343 return e; | |
5344 } | |
5345 | |
5346 VarDeclaration *v = s->isVarDeclaration(); | |
5347 if (v) | |
5348 { | |
5349 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5350 if (v->inuse) | |
5351 { | |
5352 error("circular reference to '%s'", v->toChars()); | |
5353 type = Type::tint32; | |
5354 return this; | |
5355 } | |
5356 type = v->type; | |
5357 if (v->isConst()) | |
5358 { | |
5359 if (v->init) | |
5360 { | |
5361 ExpInitializer *ei = v->init->isExpInitializer(); | |
5362 if (ei) | |
5363 { | |
5364 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5365 //ei->exp = ei->exp->semantic(sc); | |
5366 if (ei->exp->type == type) | |
5367 { | |
5368 e = ei->exp->copy(); // make copy so we can change loc | |
5369 e->loc = loc; | |
5370 return e; | |
5371 } | |
5372 } | |
5373 } | |
5374 else if (type->isscalar()) | |
5375 { | |
5376 e = type->defaultInit(); | |
5377 e->loc = loc; | |
5378 return e; | |
5379 } | |
5380 } | |
5381 if (v->needThis()) | |
5382 { | |
5383 if (!eleft) | |
5384 eleft = new ThisExp(loc); | |
5385 e = new DotVarExp(loc, eleft, v); | |
5386 e = e->semantic(sc); | |
5387 } | |
5388 else | |
5389 { | |
5390 e = new VarExp(loc, v); | |
5391 if (eleft) | |
5392 { e = new CommaExp(loc, eleft, e); | |
5393 e->type = v->type; | |
5394 } | |
5395 } | |
5396 return e->deref(); | |
5397 } | |
5398 | |
5399 FuncDeclaration *f = s->isFuncDeclaration(); | |
5400 if (f) | |
5401 { | |
5402 //printf("it's a function\n"); | |
5403 if (f->needThis()) | |
5404 { | |
5405 if (!eleft) | |
5406 eleft = new ThisExp(loc); | |
5407 e = new DotVarExp(loc, eleft, f); | |
5408 e = e->semantic(sc); | |
5409 } | |
5410 else | |
5411 { | |
5412 e = new VarExp(loc, f); | |
5413 if (eleft) | |
5414 { e = new CommaExp(loc, eleft, e); | |
5415 e->type = f->type; | |
5416 } | |
5417 } | |
5418 return e; | |
5419 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5420 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5421 OverloadSet *o = s->isOverloadSet(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5422 if (o) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5423 { //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
|
5424 return new OverExp(o); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5425 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5426 #endif |
159 | 5427 |
5428 Type *t = s->getType(); | |
5429 if (t) | |
5430 { | |
5431 return new TypeExp(loc, t); | |
5432 } | |
5433 | |
336 | 5434 TupleDeclaration *tup = s->isTupleDeclaration(); |
5435 if (tup) | |
5436 { | |
5437 if (eleft) | |
5438 error("cannot have e.tuple"); | |
5439 e = new TupleExp(loc, tup); | |
5440 e = e->semantic(sc); | |
5441 return e; | |
5442 } | |
5443 | |
159 | 5444 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5445 if (sds) | |
5446 { | |
5447 //printf("it's a ScopeDsymbol\n"); | |
5448 e = new ScopeExp(loc, sds); | |
5449 e = e->semantic(sc); | |
5450 if (eleft) | |
5451 e = new DotExp(loc, eleft, e); | |
5452 return e; | |
5453 } | |
5454 | |
5455 Import *imp = s->isImport(); | |
5456 if (imp) | |
5457 { | |
5458 ScopeExp *ie; | |
5459 | |
5460 ie = new ScopeExp(loc, imp->pkg); | |
5461 return ie->semantic(sc); | |
5462 } | |
5463 | |
5464 // BUG: handle other cases like in IdentifierExp::semantic() | |
5465 #ifdef DEBUG | |
5466 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5467 #endif | |
5468 assert(0); | |
5469 } | |
5470 else if (ident == Id::stringof) | |
5471 { char *s = ie->toChars(); | |
5472 e = new StringExp(loc, s, strlen(s), 'c'); | |
5473 e = e->semantic(sc); | |
5474 return e; | |
5475 } | |
5476 error("undefined identifier %s", toChars()); | |
5477 type = Type::tvoid; | |
5478 return this; | |
5479 } | |
5480 else if (e1->type->ty == Tpointer && | |
5481 ident != Id::init && ident != Id::__sizeof && | |
5482 ident != Id::alignof && ident != Id::offsetof && | |
5483 ident != Id::mangleof && ident != Id::stringof) | |
5484 { | |
5485 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
|
5486 e->type = ((TypePointer *)e1->type)->next; |
159 | 5487 return e->type->dotExp(sc, e, ident); |
5488 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5489 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5490 else if (t1b->ty == Tarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5491 t1b->ty == Tsarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5492 t1b->ty == Taarray) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5493 { /* 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
|
5494 * e1.ident |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5495 * as: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5496 * .ident(e1) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5497 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5498 unsigned errors = global.errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5499 global.gag++; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5500 e = e1->type->dotExp(sc, e1, ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5501 global.gag--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5502 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
|
5503 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5504 global.errors = errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5505 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
|
5506 e = new CallExp(loc, e, e1); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5507 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5508 e = e->semantic(sc); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5509 return e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5510 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5511 #endif |
159 | 5512 else |
5513 { | |
5514 e = e1->type->dotExp(sc, e1, ident); | |
5515 e = e->semantic(sc); | |
5516 return e; | |
5517 } | |
5518 } | |
5519 | |
5520 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5521 { | |
5522 //printf("DotIdExp::toCBuffer()\n"); | |
5523 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5524 buf->writeByte('.'); | |
5525 buf->writestring(ident->toChars()); | |
5526 } | |
5527 | |
5528 /********************** DotTemplateExp ***********************************/ | |
5529 | |
5530 // Mainly just a placeholder | |
5531 | |
5532 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5533 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5534 | |
5535 { | |
5536 this->td = td; | |
5537 } | |
5538 | |
5539 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5540 { | |
5541 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5542 buf->writeByte('.'); | |
5543 buf->writestring(td->toChars()); | |
5544 } | |
5545 | |
5546 | |
5547 /************************************************************/ | |
5548 | |
5549 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5550 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5551 { | |
5552 //printf("DotVarExp()\n"); | |
5553 this->var = v; | |
5554 } | |
5555 | |
5556 Expression *DotVarExp::semantic(Scope *sc) | |
5557 { | |
5558 #if LOGSEMANTIC | |
5559 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5560 #endif | |
5561 if (!type) | |
5562 { | |
5563 var = var->toAlias()->isDeclaration(); | |
5564 | |
5565 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5566 if (tup) | |
5567 { /* Replace: | |
5568 * e1.tuple(a, b, c) | |
5569 * with: | |
5570 * tuple(e1.a, e1.b, e1.c) | |
5571 */ | |
5572 Expressions *exps = new Expressions; | |
5573 | |
5574 exps->reserve(tup->objects->dim); | |
5575 for (size_t i = 0; i < tup->objects->dim; i++) | |
5576 { Object *o = (Object *)tup->objects->data[i]; | |
5577 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5578 { | |
5579 error("%s is not an expression", o->toChars()); | |
5580 } | |
5581 else | |
5582 { | |
5583 Expression *e = (Expression *)o; | |
5584 if (e->op != TOKdsymbol) | |
5585 error("%s is not a member", e->toChars()); | |
5586 else | |
5587 { DsymbolExp *ve = (DsymbolExp *)e; | |
5588 | |
5589 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5590 exps->push(e); | |
5591 } | |
5592 } | |
5593 } | |
5594 Expression *e = new TupleExp(loc, exps); | |
5595 e = e->semantic(sc); | |
5596 return e; | |
5597 } | |
5598 | |
5599 e1 = e1->semantic(sc); | |
5600 type = var->type; | |
5601 if (!type && global.errors) | |
5602 { // var is goofed up, just return 0 | |
5603 return new IntegerExp(0); | |
5604 } | |
5605 assert(type); | |
5606 | |
5607 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5608 { | |
5609 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
336 | 5610 e1 = getRightThis(loc, sc, ad, e1, var); |
5611 if (!sc->noaccesscheck) | |
5612 accessCheck(loc, sc, e1, var); | |
5613 | |
5614 VarDeclaration *v = var->isVarDeclaration(); | |
5615 if (v && v->isConst()) | |
5616 { ExpInitializer *ei = v->getExpInitializer(); | |
5617 if (ei) | |
5618 { Expression *e = ei->exp->copy(); | |
5619 e = e->semantic(sc); | |
5620 return e; | |
159 | 5621 } |
5622 } | |
5623 } | |
5624 } | |
5625 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5626 return this; | |
5627 } | |
5628 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5629 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5630 int DotVarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5631 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5632 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5633 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5634 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5635 |
159 | 5636 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) |
5637 { | |
5638 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5639 return this; | |
5640 } | |
5641 | |
5642 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5643 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5644 #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
|
5645 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
|
5646 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
|
5647 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
|
5648 #endif |
159 | 5649 |
5650 if (var->isCtorinit()) | |
5651 { // It's only modifiable if inside the right constructor | |
5652 Dsymbol *s = sc->func; | |
5653 while (1) | |
5654 { | |
5655 FuncDeclaration *fd = NULL; | |
5656 if (s) | |
5657 fd = s->isFuncDeclaration(); | |
5658 if (fd && | |
5659 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5660 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5661 fd->toParent() == var->toParent() && | |
5662 e1->op == TOKthis | |
5663 ) | |
5664 { | |
5665 VarDeclaration *v = var->isVarDeclaration(); | |
5666 assert(v); | |
5667 v->ctorinit = 1; | |
5668 //printf("setting ctorinit\n"); | |
5669 } | |
5670 else | |
5671 { | |
5672 if (s) | |
5673 { s = s->toParent2(); | |
5674 continue; | |
5675 } | |
5676 else | |
5677 { | |
5678 const char *p = var->isStatic() ? "static " : ""; | |
5679 error("can only initialize %sconst member %s inside %sconstructor", | |
5680 p, var->toChars(), p); | |
5681 } | |
5682 } | |
5683 break; | |
5684 } | |
5685 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5686 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5687 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5688 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5689 Type *t1 = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5690 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5691 if (!t1->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5692 (t1->ty == Tpointer && !t1->nextOf()->isMutable()) || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5693 !var->type->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5694 !var->type->isAssignable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5695 var->storage_class & STCmanifest |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5696 ) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5697 error("cannot modify const/invariant %s", toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5698 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5699 #endif |
159 | 5700 return this; |
5701 } | |
5702 | |
5703 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5704 { | |
5705 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5706 buf->writeByte('.'); | |
5707 buf->writestring(var->toChars()); | |
5708 } | |
5709 | |
5710 /************************************************************/ | |
5711 | |
5712 /* Things like: | |
5713 * foo.bar!(args) | |
5714 */ | |
5715 | |
5716 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5717 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5718 { | |
5719 //printf("DotTemplateInstanceExp()\n"); | |
5720 this->ti = ti; | |
5721 } | |
5722 | |
5723 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5724 { | |
5725 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5726 e1->syntaxCopy(), | |
5727 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5728 return de; | |
5729 } | |
5730 | |
5731 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5732 { Dsymbol *s; | |
5733 Dsymbol *s2; | |
5734 TemplateDeclaration *td; | |
5735 Expression *e; | |
5736 Identifier *id; | |
5737 Type *t1; | |
5738 Expression *eleft = NULL; | |
5739 Expression *eright; | |
5740 | |
5741 #if LOGSEMANTIC | |
5742 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5743 #endif | |
5744 //e1->print(); | |
5745 //print(); | |
5746 e1 = e1->semantic(sc); | |
5747 t1 = e1->type; | |
5748 if (t1) | |
5749 t1 = t1->toBasetype(); | |
5750 //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
|
5751 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5752 /* 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
|
5753 * 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
|
5754 * 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
|
5755 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5756 |
159 | 5757 if (e1->op == TOKdotexp) |
5758 { DotExp *de = (DotExp *)e1; | |
5759 eleft = de->e1; | |
5760 eright = de->e2; | |
5761 } | |
5762 else | |
5763 { eleft = NULL; | |
5764 eright = e1; | |
5765 } | |
5766 if (eright->op == TOKimport) | |
5767 { | |
5768 s = ((ScopeExp *)eright)->sds; | |
5769 } | |
5770 else if (e1->op == TOKtype) | |
5771 { | |
5772 s = t1->isClassHandle(); | |
5773 if (!s) | |
5774 { if (t1->ty == Tstruct) | |
5775 s = ((TypeStruct *)t1)->sym; | |
5776 else | |
5777 goto L1; | |
5778 } | |
5779 } | |
5780 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5781 { | |
5782 s = t1->toDsymbol(sc); | |
5783 eleft = e1; | |
5784 } | |
5785 else if (t1 && t1->ty == Tpointer) | |
5786 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5787 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 5788 if (t1->ty != Tstruct) |
5789 goto L1; | |
5790 s = t1->toDsymbol(sc); | |
5791 eleft = e1; | |
5792 } | |
5793 else | |
5794 { | |
5795 L1: | |
5796 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5797 goto Lerr; | |
5798 } | |
5799 | |
5800 assert(s); | |
5801 id = ti->name; | |
5802 s2 = s->search(loc, id, 0); | |
5803 if (!s2) | |
5804 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5805 goto Lerr; | |
5806 } | |
5807 s = s2; | |
5808 s->semantic(sc); | |
5809 s = s->toAlias(); | |
5810 td = s->isTemplateDeclaration(); | |
5811 if (!td) | |
5812 { | |
5813 error("%s is not a template", id->toChars()); | |
5814 goto Lerr; | |
5815 } | |
5816 if (global.errors) | |
5817 goto Lerr; | |
5818 | |
5819 ti->tempdecl = td; | |
5820 | |
5821 if (eleft) | |
5822 { Declaration *v; | |
5823 | |
5824 ti->semantic(sc); | |
5825 s = ti->inst->toAlias(); | |
5826 v = s->isDeclaration(); | |
5827 if (v) | |
5828 { e = new DotVarExp(loc, eleft, v); | |
5829 e = e->semantic(sc); | |
5830 return e; | |
5831 } | |
5832 } | |
5833 | |
5834 e = new ScopeExp(loc, ti); | |
5835 if (eleft) | |
5836 { | |
5837 e = new DotExp(loc, eleft, e); | |
5838 } | |
5839 e = e->semantic(sc); | |
5840 return e; | |
5841 | |
5842 Lerr: | |
336 | 5843 return new IntegerExp(loc, 0, Type::tint32); |
159 | 5844 } |
5845 | |
5846 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5847 { | |
5848 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5849 buf->writeByte('.'); | |
5850 ti->toCBuffer(buf, hgs); | |
5851 } | |
5852 | |
5853 /************************************************************/ | |
5854 | |
5855 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5856 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5857 { | |
5858 this->func = f; | |
5859 } | |
5860 | |
5861 Expression *DelegateExp::semantic(Scope *sc) | |
5862 { | |
5863 #if LOGSEMANTIC | |
5864 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5865 #endif | |
5866 if (!type) | |
5867 { | |
5868 e1 = e1->semantic(sc); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
5869 // 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
|
5870 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 5871 type = type->semantic(loc, sc); |
5872 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 5873 if (func->needThis()) |
5874 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 5875 } |
5876 return this; | |
5877 } | |
5878 | |
5879 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5880 { | |
5881 buf->writeByte('&'); | |
5882 if (!func->isNested()) | |
5883 { | |
5884 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5885 buf->writeByte('.'); | |
5886 } | |
5887 buf->writestring(func->toChars()); | |
5888 } | |
5889 | |
5890 /************************************************************/ | |
5891 | |
5892 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5893 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5894 { | |
5895 this->sym = s; | |
5896 this->type = s->getType(); | |
5897 } | |
5898 | |
5899 Expression *DotTypeExp::semantic(Scope *sc) | |
5900 { | |
5901 #if LOGSEMANTIC | |
5902 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5903 #endif | |
5904 UnaExp::semantic(sc); | |
5905 return this; | |
5906 } | |
5907 | |
5908 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5909 { | |
5910 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5911 buf->writeByte('.'); | |
5912 buf->writestring(sym->toChars()); | |
5913 } | |
5914 | |
5915 /************************************************************/ | |
5916 | |
5917 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5918 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5919 { | |
5920 this->arguments = exps; | |
5921 } | |
5922 | |
5923 CallExp::CallExp(Loc loc, Expression *e) | |
5924 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5925 { | |
5926 this->arguments = NULL; | |
5927 } | |
5928 | |
5929 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5930 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5931 { | |
5932 Expressions *arguments = new Expressions(); | |
5933 arguments->setDim(1); | |
5934 arguments->data[0] = (void *)earg1; | |
5935 | |
5936 this->arguments = arguments; | |
5937 } | |
5938 | |
5939 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5940 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5941 { | |
5942 Expressions *arguments = new Expressions(); | |
5943 arguments->setDim(2); | |
5944 arguments->data[0] = (void *)earg1; | |
5945 arguments->data[1] = (void *)earg2; | |
5946 | |
5947 this->arguments = arguments; | |
5948 } | |
5949 | |
5950 Expression *CallExp::syntaxCopy() | |
5951 { | |
5952 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5953 } | |
5954 | |
5955 | |
5956 Expression *CallExp::semantic(Scope *sc) | |
5957 { | |
5958 TypeFunction *tf; | |
5959 FuncDeclaration *f; | |
5960 int i; | |
5961 Type *t1; | |
5962 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
|
5963 Objects *targsi = NULL; // initial list of template arguments |
159 | 5964 |
5965 #if LOGSEMANTIC | |
5966 printf("CallExp::semantic() %s\n", toChars()); | |
5967 #endif | |
5968 if (type) | |
5969 return this; // semantic() already run | |
5970 #if 0 | |
5971 if (arguments && arguments->dim) | |
5972 { | |
5973 Expression *earg = (Expression *)arguments->data[0]; | |
5974 earg->print(); | |
5975 if (earg->type) earg->type->print(); | |
5976 } | |
5977 #endif | |
5978 | |
5979 if (e1->op == TOKdelegate) | |
5980 { DelegateExp *de = (DelegateExp *)e1; | |
5981 | |
5982 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5983 return semantic(sc); | |
5984 } | |
5985 | |
5986 /* Transform: | |
5987 * array.id(args) into id(array,args) | |
5988 * aa.remove(arg) into delete aa[arg] | |
5989 */ | |
5990 if (e1->op == TOKdot) | |
5991 { | |
5992 // BUG: we should handle array.a.b.c.e(args) too | |
5993 | |
5994 DotIdExp *dotid = (DotIdExp *)(e1); | |
5995 dotid->e1 = dotid->e1->semantic(sc); | |
5996 assert(dotid->e1); | |
5997 if (dotid->e1->type) | |
5998 { | |
5999 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
6000 if (e1ty == Taarray && dotid->ident == Id::remove) | |
6001 { | |
6002 if (!arguments || arguments->dim != 1) | |
6003 { error("expected key as argument to aa.remove()"); | |
6004 goto Lagain; | |
6005 } | |
6006 Expression *key = (Expression *)arguments->data[0]; | |
6007 key = key->semantic(sc); | |
6008 key = resolveProperties(sc, key); | |
6009 key->rvalue(); | |
6010 | |
6011 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
6012 key = key->implicitCastTo(sc, taa->index); | |
6013 key = key->implicitCastTo(sc, taa->key); | |
6014 | |
6015 return new RemoveExp(loc, dotid->e1, key); | |
6016 } | |
6017 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
6018 { | |
6019 if (!arguments) | |
6020 arguments = new Expressions(); | |
6021 arguments->shift(dotid->e1); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6022 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6023 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
|
6024 #else |
159 | 6025 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
|
6026 #endif |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6031 #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
|
6032 /* 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
|
6033 * 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
|
6034 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6035 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
|
6036 { 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
|
6037 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
|
6038 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
|
6039 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6040 /* 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
|
6041 * 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
|
6042 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6043 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
|
6044 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
|
6045 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
|
6046 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
|
6047 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
|
6048 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
|
6049 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6050 /* 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
|
6051 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6052 global.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
|
6053 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
|
6054 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
|
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 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6059 /* 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
|
6060 * 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
|
6061 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6062 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
|
6063 { 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
|
6064 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
|
6065 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
|
6066 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6067 /* 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
|
6068 * 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
|
6069 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6070 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
|
6071 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
|
6072 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
|
6073 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
|
6074 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
|
6075 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
|
6076 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
|
6077 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6078 global.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
|
6079 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
|
6080 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
|
6081 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6082 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6083 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
|
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 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6086 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6087 |
159 | 6088 istemp = 0; |
6089 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
|
6090 //printf("Lagain: %s\n", toChars()); |
159 | 6091 f = NULL; |
6092 if (e1->op == TOKthis || e1->op == TOKsuper) | |
6093 { | |
6094 // semantic() run later for these | |
6095 } | |
6096 else | |
6097 { | |
6098 UnaExp::semantic(sc); | |
6099 | |
6100 /* Look for e1 being a lazy parameter | |
6101 */ | |
6102 if (e1->op == TOKvar) | |
6103 { VarExp *ve = (VarExp *)e1; | |
6104 | |
6105 if (ve->var->storage_class & STClazy) | |
6106 { | |
6107 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
6108 TypeDelegate *t = new TypeDelegate(tf); | |
6109 ve->type = t->semantic(loc, sc); | |
6110 } | |
6111 } | |
6112 | |
6113 if (e1->op == TOKimport) | |
6114 { // Perhaps this should be moved to ScopeExp::semantic() | |
6115 ScopeExp *se = (ScopeExp *)e1; | |
6116 e1 = new DsymbolExp(loc, se->sds); | |
6117 e1 = e1->semantic(sc); | |
6118 } | |
6119 #if 1 // patch for #540 by Oskar Linde | |
6120 else if (e1->op == TOKdotexp) | |
6121 { | |
6122 DotExp *de = (DotExp *) e1; | |
6123 | |
6124 if (de->e2->op == TOKimport) | |
6125 { // This should *really* be moved to ScopeExp::semantic() | |
6126 ScopeExp *se = (ScopeExp *)de->e2; | |
6127 de->e2 = new DsymbolExp(loc, se->sds); | |
6128 de->e2 = de->e2->semantic(sc); | |
6129 } | |
6130 | |
6131 if (de->e2->op == TOKtemplate) | |
6132 { TemplateExp *te = (TemplateExp *) de->e2; | |
6133 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
6134 } | |
6135 } | |
6136 #endif | |
6137 } | |
6138 | |
6139 if (e1->op == TOKcomma) | |
6140 { | |
6141 CommaExp *ce = (CommaExp *)e1; | |
6142 | |
6143 e1 = ce->e2; | |
6144 e1->type = ce->type; | |
6145 ce->e2 = this; | |
6146 ce->type = NULL; | |
6147 return ce->semantic(sc); | |
6148 } | |
6149 | |
6150 t1 = NULL; | |
6151 if (e1->type) | |
6152 t1 = e1->type->toBasetype(); | |
6153 | |
6154 // Check for call operator overload | |
6155 if (t1) | |
6156 { AggregateDeclaration *ad; | |
6157 | |
6158 if (t1->ty == Tstruct) | |
6159 { | |
6160 ad = ((TypeStruct *)t1)->sym; | |
6161 if (search_function(ad, Id::call)) | |
6162 goto L1; // overload of opCall, therefore it's a call | |
6163 | |
6164 if (e1->op != TOKtype) | |
6165 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
6166 /* It's a struct literal | |
6167 */ | |
6168 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
6169 e = e->semantic(sc); | |
6170 e->type = e1->type; // in case e1->type was a typedef | |
6171 return e; | |
6172 } | |
6173 else if (t1->ty == Tclass) | |
6174 { | |
6175 ad = ((TypeClass *)t1)->sym; | |
6176 goto L1; | |
6177 L1: | |
6178 // Rewrite as e1.call(arguments) | |
6179 Expression *e = new DotIdExp(loc, e1, Id::call); | |
6180 e = new CallExp(loc, e, arguments); | |
6181 e = e->semantic(sc); | |
6182 return e; | |
6183 } | |
6184 } | |
6185 | |
6186 arrayExpressionSemantic(arguments, sc); | |
6187 preFunctionArguments(loc, sc, arguments); | |
6188 | |
6189 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
6190 e1->op == TOKdottd) | |
6191 { | |
6192 DotVarExp *dve; | |
6193 DotTemplateExp *dte; | |
6194 AggregateDeclaration *ad; | |
6195 UnaExp *ue = (UnaExp *)(e1); | |
336 | 6196 |
159 | 6197 if (e1->op == TOKdotvar) |
6198 { // Do overload resolution | |
6199 dve = (DotVarExp *)(e1); | |
6200 | |
6201 f = dve->var->isFuncDeclaration(); | |
6202 assert(f); | |
6203 f = f->overloadResolve(loc, arguments); | |
6204 | |
6205 ad = f->toParent()->isAggregateDeclaration(); | |
6206 } | |
6207 else | |
6208 { dte = (DotTemplateExp *)(e1); | |
6209 TemplateDeclaration *td = dte->td; | |
6210 assert(td); | |
6211 if (!arguments) | |
6212 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
6213 arguments = new Expressions(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6214 f = td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6215 if (!f) |
6216 { type = Type::terror; | |
6217 return this; | |
6218 } | |
6219 ad = td->toParent()->isAggregateDeclaration(); | |
6220 } | |
336 | 6221 if (f->needThis()) |
6222 { | |
6223 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
6224 } | |
6225 | |
6226 /* Cannot call public functions from inside invariant | |
6227 * (because then the invariant would have infinite recursion) | |
159 | 6228 */ |
336 | 6229 if (sc->func && sc->func->isInvariantDeclaration() && |
6230 ue->e1->op == TOKthis && | |
6231 f->addPostInvariant() | |
159 | 6232 ) |
6233 { | |
336 | 6234 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 6235 } |
6236 | |
6237 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6238 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6239 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6240 #endif |
159 | 6241 accessCheck(loc, sc, ue->e1, f); |
6242 if (!f->needThis()) | |
6243 { | |
6244 VarExp *ve = new VarExp(loc, f); | |
6245 e1 = new CommaExp(loc, ue->e1, ve); | |
6246 e1->type = f->type; | |
6247 } | |
6248 else | |
6249 { | |
6250 if (e1->op == TOKdotvar) | |
6251 dve->var = f; | |
6252 else | |
6253 e1 = new DotVarExp(loc, dte->e1, f); | |
6254 e1->type = f->type; | |
6255 | |
6256 // See if we need to adjust the 'this' pointer | |
6257 AggregateDeclaration *ad = f->isThis(); | |
6258 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
6259 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
6260 ue->e1->op != TOKsuper) | |
6261 { | |
6262 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
6263 ue->e1 = ue->e1->semantic(sc); | |
6264 } | |
6265 } | |
6266 t1 = e1->type; | |
6267 } | |
6268 else if (e1->op == TOKsuper) | |
6269 { | |
6270 // Base class constructor call | |
6271 ClassDeclaration *cd = NULL; | |
6272 | |
6273 if (sc->func) | |
6274 cd = sc->func->toParent()->isClassDeclaration(); | |
6275 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
6276 { | |
6277 error("super class constructor call must be in a constructor"); | |
6278 type = Type::terror; | |
6279 return this; | |
6280 } | |
6281 else | |
6282 { | |
6283 f = cd->baseClass->ctor; | |
6284 if (!f) | |
6285 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
6286 type = Type::terror; | |
6287 return this; | |
6288 } | |
6289 else | |
6290 { | |
336 | 6291 if (!sc->intypeof) |
6292 { | |
159 | 6293 #if 0 |
336 | 6294 if (sc->callSuper & (CSXthis | CSXsuper)) |
6295 error("reference to this before super()"); | |
159 | 6296 #endif |
336 | 6297 if (sc->noctor || sc->callSuper & CSXlabel) |
6298 error("constructor calls not allowed in loops or after labels"); | |
6299 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6300 error("multiple constructor calls"); | |
6301 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
6302 } | |
159 | 6303 |
6304 f = f->overloadResolve(loc, arguments); | |
6305 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6306 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6307 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6308 #endif |
159 | 6309 e1 = new DotVarExp(e1->loc, e1, f); |
6310 e1 = e1->semantic(sc); | |
6311 t1 = e1->type; | |
6312 } | |
6313 } | |
6314 } | |
6315 else if (e1->op == TOKthis) | |
6316 { | |
6317 // same class constructor call | |
6318 ClassDeclaration *cd = NULL; | |
6319 | |
6320 if (sc->func) | |
6321 cd = sc->func->toParent()->isClassDeclaration(); | |
6322 if (!cd || !sc->func->isCtorDeclaration()) | |
6323 { | |
6324 error("class constructor call must be in a constructor"); | |
6325 type = Type::terror; | |
6326 return this; | |
6327 } | |
6328 else | |
6329 { | |
336 | 6330 if (!sc->intypeof) |
6331 { | |
159 | 6332 #if 0 |
336 | 6333 if (sc->callSuper & (CSXthis | CSXsuper)) |
6334 error("reference to this before super()"); | |
159 | 6335 #endif |
336 | 6336 if (sc->noctor || sc->callSuper & CSXlabel) |
6337 error("constructor calls not allowed in loops or after labels"); | |
6338 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6339 error("multiple constructor calls"); | |
6340 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6341 } | |
159 | 6342 |
6343 f = cd->ctor; | |
6344 f = f->overloadResolve(loc, arguments); | |
6345 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6346 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6347 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6348 #endif |
159 | 6349 e1 = new DotVarExp(e1->loc, e1, f); |
6350 e1 = e1->semantic(sc); | |
6351 t1 = e1->type; | |
6352 | |
6353 // BUG: this should really be done by checking the static | |
6354 // call graph | |
6355 if (f == sc->func) | |
6356 error("cyclic constructor call"); | |
6357 } | |
6358 } | |
6359 else if (!t1) | |
6360 { | |
6361 error("function expected before (), not '%s'", e1->toChars()); | |
6362 type = Type::terror; | |
6363 return this; | |
6364 } | |
6365 else if (t1->ty != Tfunction) | |
6366 { | |
6367 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
|
6368 { 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
|
6369 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
|
6370 tf = (TypeFunction *)(td->next); |
159 | 6371 goto Lcheckargs; |
6372 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6373 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6374 { Expression *e; |
6375 | |
6376 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
|
6377 t1 = ((TypePointer *)t1)->next; |
159 | 6378 e->type = t1; |
6379 e1 = e; | |
6380 } | |
6381 else if (e1->op == TOKtemplate) | |
6382 { | |
6383 TemplateExp *te = (TemplateExp *)e1; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6384 f = te->td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6385 if (!f) |
6386 { type = Type::terror; | |
6387 return this; | |
6388 } | |
6389 if (f->needThis() && hasThis(sc)) | |
6390 { | |
6391 // Supply an implicit 'this', as in | |
6392 // this.ident | |
6393 | |
6394 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6395 goto Lagain; | |
6396 } | |
6397 | |
6398 e1 = new VarExp(loc, f); | |
6399 goto Lagain; | |
6400 } | |
6401 else | |
6402 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6403 type = Type::terror; | |
6404 return this; | |
6405 } | |
6406 } | |
6407 else if (e1->op == TOKvar) | |
6408 { | |
6409 // Do overload resolution | |
6410 VarExp *ve = (VarExp *)e1; | |
6411 | |
6412 f = ve->var->isFuncDeclaration(); | |
6413 assert(f); | |
6414 | |
6415 // Look to see if f is really a function template | |
6416 if (0 && !istemp && f->parent) | |
6417 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6418 | |
6419 if (ti && | |
6420 (ti->name == f->ident || | |
6421 ti->toAlias()->ident == f->ident) | |
6422 && | |
6423 ti->tempdecl) | |
6424 { | |
6425 /* This is so that one can refer to the enclosing | |
6426 * template, even if it has the same name as a member | |
6427 * of the template, if it has a !(arguments) | |
6428 */ | |
6429 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6430 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6431 tempdecl = tempdecl->overroot; // then get the start | |
6432 e1 = new TemplateExp(loc, tempdecl); | |
6433 istemp = 1; | |
6434 goto Lagain; | |
6435 } | |
6436 } | |
6437 | |
6438 f = f->overloadResolve(loc, arguments); | |
6439 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6440 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6441 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6442 #endif |
159 | 6443 |
6444 if (f->needThis() && hasThis(sc)) | |
6445 { | |
6446 // Supply an implicit 'this', as in | |
6447 // this.ident | |
6448 | |
6449 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6450 goto Lagain; | |
6451 } | |
6452 | |
6453 accessCheck(loc, sc, NULL, f); | |
6454 | |
6455 ve->var = f; | |
6456 ve->type = f->type; | |
6457 t1 = f->type; | |
6458 } | |
6459 assert(t1->ty == Tfunction); | |
6460 tf = (TypeFunction *)(t1); | |
6461 | |
6462 Lcheckargs: | |
6463 assert(tf->ty == Tfunction); | |
6464 type = tf->next; | |
6465 | |
6466 if (!arguments) | |
6467 arguments = new Expressions(); | |
6468 functionArguments(loc, sc, tf, arguments); | |
6469 | |
6470 assert(type); | |
6471 | |
6472 if (f && f->tintro) | |
6473 { | |
6474 Type *t = type; | |
6475 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
|
6476 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
|
6477 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6478 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
|
6479 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6480 type = tf->next; |
159 | 6481 return castTo(sc, t); |
6482 } | |
6483 } | |
6484 | |
6485 return this; | |
6486 } | |
6487 | |
6488 int CallExp::checkSideEffect(int flag) | |
6489 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6490 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6491 if (flag != 2) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6492 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6493 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6494 if (e1->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6495 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6496 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6497 /* 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
|
6498 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6499 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
|
6500 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6501 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6502 if (e->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6503 return 1; |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6506 /* 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
|
6507 * then this expression has no side effects. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6508 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6509 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6510 if (t->ty == Tfunction && ((TypeFunction *)t)->ispure) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6511 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6512 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
|
6513 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6514 #endif |
159 | 6515 return 1; |
6516 } | |
6517 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6518 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6519 int CallExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6520 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6521 if (e1->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6522 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6523 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6524 /* 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
|
6525 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6526 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
|
6527 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6528 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6529 if (e->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6530 return 1; |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6533 /* 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
|
6534 * then this expression cannot throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6535 * Note that pure functions can throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6536 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6537 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6538 if (t->ty == Tfunction && ((TypeFunction *)t)->isnothrow) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6539 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6540 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
|
6541 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6542 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6543 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6544 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6545 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6546 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6547 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6548 int CallExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6549 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6550 if (type->toBasetype()->ty == Tstruct) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6551 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6552 Type *tb = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6553 if (tb->ty == Tfunction && ((TypeFunction *)tb)->isref) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6554 return 1; // function returns a reference |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6555 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6556 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6557 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6558 |
159 | 6559 Expression *CallExp::toLvalue(Scope *sc, Expression *e) |
6560 { | |
6561 if (type->toBasetype()->ty == Tstruct) | |
6562 return this; | |
6563 else | |
6564 return Expression::toLvalue(sc, e); | |
6565 } | |
6566 | |
6567 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6568 { int i; | |
6569 | |
6570 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6571 buf->writeByte('('); | |
6572 argsToCBuffer(buf, arguments, hgs); | |
6573 buf->writeByte(')'); | |
6574 } | |
6575 | |
6576 | |
6577 /************************************************************/ | |
6578 | |
6579 AddrExp::AddrExp(Loc loc, Expression *e) | |
6580 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6581 { | |
6582 } | |
6583 | |
6584 Expression *AddrExp::semantic(Scope *sc) | |
6585 { | |
6586 #if LOGSEMANTIC | |
6587 printf("AddrExp::semantic('%s')\n", toChars()); | |
6588 #endif | |
6589 if (!type) | |
6590 { | |
6591 UnaExp::semantic(sc); | |
6592 e1 = e1->toLvalue(sc, NULL); | |
6593 if (!e1->type) | |
6594 { | |
6595 error("cannot take address of %s", e1->toChars()); | |
6596 type = Type::tint32; | |
6597 return this; | |
6598 } | |
6599 type = e1->type->pointerTo(); | |
6600 | |
6601 // See if this should really be a delegate | |
6602 if (e1->op == TOKdotvar) | |
6603 { | |
6604 DotVarExp *dve = (DotVarExp *)e1; | |
6605 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6606 | |
6607 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
|
6608 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6609 e = e->semantic(sc); |
6610 return e; | |
6611 } | |
6612 } | |
6613 else if (e1->op == TOKvar) | |
6614 { | |
6615 VarExp *dve = (VarExp *)e1; | |
6616 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6617 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6618 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6619 // 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
|
6620 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
|
6621 { |
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 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
|
6623 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
|
6624 } |
a34078905d01
Added pragma(llvmdc, "string") for misc per-module compiler configuration, currently "string" can only be "verbose" which forces -vv for module it appears in.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
471
diff
changeset
|
6625 |
159 | 6626 if (f && f->isNested()) |
6627 { Expression *e; | |
6628 | |
6629 e = new DelegateExp(loc, e1, f); | |
6630 e = e->semantic(sc); | |
6631 return e; | |
6632 } | |
6633 } | |
6634 else if (e1->op == TOKarray) | |
6635 { | |
6636 if (e1->type->toBasetype()->ty == Tbit) | |
6637 error("cannot take address of bit in array"); | |
6638 } | |
6639 return optimize(WANTvalue); | |
6640 } | |
6641 return this; | |
6642 } | |
6643 | |
6644 /************************************************************/ | |
6645 | |
6646 PtrExp::PtrExp(Loc loc, Expression *e) | |
6647 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6648 { | |
6649 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
|
6650 type = ((TypePointer *)e->type)->next; |
159 | 6651 } |
6652 | |
6653 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6654 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6655 { | |
6656 type = t; | |
6657 } | |
6658 | |
6659 Expression *PtrExp::semantic(Scope *sc) | |
6660 { Type *tb; | |
6661 | |
6662 #if LOGSEMANTIC | |
6663 printf("PtrExp::semantic('%s')\n", toChars()); | |
6664 #endif | |
6665 UnaExp::semantic(sc); | |
6666 e1 = resolveProperties(sc, e1); | |
6667 if (type) | |
6668 return this; | |
6669 if (!e1->type) | |
6670 printf("PtrExp::semantic('%s')\n", toChars()); | |
6671 tb = e1->type->toBasetype(); | |
6672 switch (tb->ty) | |
6673 { | |
6674 case Tpointer: | |
6675 type = tb->next; | |
6676 if (type->isbit()) | |
6677 { Expression *e; | |
6678 | |
6679 // Rewrite *p as p[0] | |
6680 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
6681 return e->semantic(sc); | |
6682 } | |
6683 break; | |
6684 | |
6685 case Tsarray: | |
6686 case Tarray: | |
6687 type = tb->next; | |
6688 e1 = e1->castTo(sc, type->pointerTo()); | |
6689 break; | |
6690 | |
6691 default: | |
6692 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
6693 type = Type::tint32; | |
6694 break; | |
6695 } | |
6696 rvalue(); | |
6697 return this; | |
6698 } | |
6699 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6700 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6701 int PtrExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6702 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6703 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6704 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6705 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6706 |
159 | 6707 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) |
6708 { | |
6709 #if 0 | |
6710 tym = tybasic(e1->ET->Tty); | |
6711 if (!(tyscalar(tym) || | |
6712 tym == TYstruct || | |
6713 tym == TYarray && e->Eoper == TOKaddr)) | |
6714 synerr(EM_lvalue); // lvalue expected | |
6715 #endif | |
6716 return this; | |
6717 } | |
6718 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6719 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6720 Expression *PtrExp::modifiableLvalue(Scope *sc, Expression *e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6721 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6722 //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
|
6723 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6724 if (e1->op == TOKsymoff) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6725 { SymOffExp *se = (SymOffExp *)e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6726 se->var->checkModify(loc, sc, type); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6727 //return toLvalue(sc, e); |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6730 return Expression::modifiableLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6731 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6732 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6733 |
159 | 6734 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
6735 { | |
6736 buf->writeByte('*'); | |
6737 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6738 } | |
6739 | |
6740 /************************************************************/ | |
6741 | |
6742 NegExp::NegExp(Loc loc, Expression *e) | |
6743 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
6744 { | |
6745 } | |
6746 | |
6747 Expression *NegExp::semantic(Scope *sc) | |
6748 { Expression *e; | |
6749 | |
6750 #if LOGSEMANTIC | |
6751 printf("NegExp::semantic('%s')\n", toChars()); | |
6752 #endif | |
6753 if (!type) | |
6754 { | |
6755 UnaExp::semantic(sc); | |
6756 e1 = resolveProperties(sc, e1); | |
6757 e = op_overload(sc); | |
6758 if (e) | |
6759 return e; | |
6760 | |
6761 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
|
6762 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
|
6763 e1->checkArithmetic(); |
159 | 6764 type = e1->type; |
6765 } | |
6766 return this; | |
6767 } | |
6768 | |
6769 /************************************************************/ | |
6770 | |
6771 UAddExp::UAddExp(Loc loc, Expression *e) | |
6772 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
6773 { | |
6774 } | |
6775 | |
6776 Expression *UAddExp::semantic(Scope *sc) | |
6777 { Expression *e; | |
6778 | |
6779 #if LOGSEMANTIC | |
6780 printf("UAddExp::semantic('%s')\n", toChars()); | |
6781 #endif | |
6782 assert(!type); | |
6783 UnaExp::semantic(sc); | |
6784 e1 = resolveProperties(sc, e1); | |
6785 e = op_overload(sc); | |
6786 if (e) | |
6787 return e; | |
6788 e1->checkNoBool(); | |
6789 e1->checkArithmetic(); | |
6790 return e1; | |
6791 } | |
6792 | |
6793 /************************************************************/ | |
6794 | |
6795 ComExp::ComExp(Loc loc, Expression *e) | |
6796 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6797 { | |
6798 } | |
6799 | |
6800 Expression *ComExp::semantic(Scope *sc) | |
6801 { Expression *e; | |
6802 | |
6803 if (!type) | |
6804 { | |
6805 UnaExp::semantic(sc); | |
6806 e1 = resolveProperties(sc, e1); | |
6807 e = op_overload(sc); | |
6808 if (e) | |
6809 return e; | |
6810 | |
6811 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
|
6812 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
|
6813 e1 = e1->checkIntegral(); |
159 | 6814 type = e1->type; |
6815 } | |
6816 return this; | |
6817 } | |
6818 | |
6819 /************************************************************/ | |
6820 | |
6821 NotExp::NotExp(Loc loc, Expression *e) | |
6822 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6823 { | |
6824 } | |
6825 | |
6826 Expression *NotExp::semantic(Scope *sc) | |
6827 { | |
6828 UnaExp::semantic(sc); | |
6829 e1 = resolveProperties(sc, e1); | |
6830 e1 = e1->checkToBoolean(); | |
6831 type = Type::tboolean; | |
6832 return this; | |
6833 } | |
6834 | |
6835 int NotExp::isBit() | |
6836 { | |
6837 return TRUE; | |
6838 } | |
6839 | |
6840 | |
6841 | |
6842 /************************************************************/ | |
6843 | |
6844 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6845 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6846 { | |
6847 type = t; | |
6848 } | |
6849 | |
6850 Expression *BoolExp::semantic(Scope *sc) | |
6851 { | |
6852 UnaExp::semantic(sc); | |
6853 e1 = resolveProperties(sc, e1); | |
6854 e1 = e1->checkToBoolean(); | |
6855 type = Type::tboolean; | |
6856 return this; | |
6857 } | |
6858 | |
6859 int BoolExp::isBit() | |
6860 { | |
6861 return TRUE; | |
6862 } | |
6863 | |
6864 /************************************************************/ | |
6865 | |
6866 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6867 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6868 { | |
6869 } | |
6870 | |
6871 Expression *DeleteExp::semantic(Scope *sc) | |
6872 { | |
6873 Type *tb; | |
6874 | |
6875 UnaExp::semantic(sc); | |
6876 e1 = resolveProperties(sc, e1); | |
6877 e1 = e1->toLvalue(sc, NULL); | |
6878 type = Type::tvoid; | |
6879 | |
6880 tb = e1->type->toBasetype(); | |
6881 switch (tb->ty) | |
6882 { case Tclass: | |
6883 { TypeClass *tc = (TypeClass *)tb; | |
6884 ClassDeclaration *cd = tc->sym; | |
6885 | |
6886 if (cd->isCOMinterface()) | |
6887 { /* Because COM classes are deleted by IUnknown.Release() | |
6888 */ | |
6889 error("cannot delete instance of COM interface %s", cd->toChars()); | |
6890 } | |
6891 break; | |
6892 } | |
6893 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
|
6894 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 6895 if (tb->ty == Tstruct) |
6896 { | |
6897 TypeStruct *ts = (TypeStruct *)tb; | |
6898 StructDeclaration *sd = ts->sym; | |
6899 FuncDeclaration *f = sd->aggDelete; | |
6900 | |
6901 if (f) | |
6902 { | |
6903 Type *tpv = Type::tvoid->pointerTo(); | |
6904 | |
6905 Expression *e = e1->castTo(sc, tpv); | |
6906 Expression *ec = new VarExp(loc, f); | |
6907 e = new CallExp(loc, ec, e); | |
6908 return e->semantic(sc); | |
6909 } | |
6910 } | |
6911 break; | |
6912 | |
6913 case Tarray: | |
6914 break; | |
6915 | |
6916 default: | |
6917 if (e1->op == TOKindex) | |
6918 { | |
6919 IndexExp *ae = (IndexExp *)(e1); | |
6920 Type *tb1 = ae->e1->type->toBasetype(); | |
6921 if (tb1->ty == Taarray) | |
6922 break; | |
6923 } | |
6924 error("cannot delete type %s", e1->type->toChars()); | |
6925 break; | |
6926 } | |
6927 | |
6928 if (e1->op == TOKindex) | |
6929 { | |
6930 IndexExp *ae = (IndexExp *)(e1); | |
6931 Type *tb1 = ae->e1->type->toBasetype(); | |
6932 if (tb1->ty == Taarray) | |
6933 { if (!global.params.useDeprecated) | |
6934 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6935 } | |
6936 } | |
6937 | |
6938 return this; | |
6939 } | |
6940 | |
6941 int DeleteExp::checkSideEffect(int flag) | |
6942 { | |
6943 return 1; | |
6944 } | |
6945 | |
6946 Expression *DeleteExp::checkToBoolean() | |
6947 { | |
6948 error("delete does not give a boolean result"); | |
6949 return this; | |
6950 } | |
6951 | |
6952 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6953 { | |
6954 buf->writestring("delete "); | |
6955 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6956 } | |
6957 | |
6958 /************************************************************/ | |
6959 | |
6960 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6961 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6962 { | |
6963 to = t; | |
6964 } | |
6965 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6966 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6967 /* For cast(const) and cast(immutable) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6968 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6969 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
|
6970 : UnaExp(loc, TOKcast, sizeof(CastExp), e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6971 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6972 to = NULL; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6973 this->mod = mod; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6974 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6975 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6976 |
159 | 6977 Expression *CastExp::syntaxCopy() |
6978 { | |
6979 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6980 } | |
6981 | |
6982 | |
6983 Expression *CastExp::semantic(Scope *sc) | |
6984 { Expression *e; | |
6985 BinExp *b; | |
6986 UnaExp *u; | |
6987 | |
6988 #if LOGSEMANTIC | |
6989 printf("CastExp::semantic('%s')\n", toChars()); | |
6990 #endif | |
6991 | |
6992 //static int x; assert(++x < 10); | |
6993 | |
6994 if (type) | |
6995 return this; | |
6996 UnaExp::semantic(sc); | |
6997 if (e1->type) // if not a tuple | |
6998 { | |
6999 e1 = resolveProperties(sc, e1); | |
7000 to = to->semantic(loc, sc); | |
7001 | |
7002 e = op_overload(sc); | |
7003 if (e) | |
7004 { | |
7005 return e->implicitCastTo(sc, to); | |
7006 } | |
7007 | |
7008 Type *tob = to->toBasetype(); | |
7009 if (tob->ty == Tstruct && | |
7010 !tob->equals(e1->type->toBasetype()) && | |
7011 ((TypeStruct *)to)->sym->search(0, Id::call, 0) | |
7012 ) | |
7013 { | |
7014 /* Look to replace: | |
7015 * cast(S)t | |
7016 * with: | |
7017 * S(t) | |
7018 */ | |
7019 | |
7020 // Rewrite as to.call(e1) | |
7021 e = new TypeExp(loc, to); | |
7022 e = new DotIdExp(loc, e, Id::call); | |
7023 e = new CallExp(loc, e, e1); | |
7024 e = e->semantic(sc); | |
7025 return e; | |
7026 } | |
7027 } | |
7028 e = e1->castTo(sc, to); | |
7029 return e; | |
7030 } | |
7031 | |
7032 int CastExp::checkSideEffect(int flag) | |
7033 { | |
7034 /* if not: | |
7035 * cast(void) | |
7036 * cast(classtype)func() | |
7037 */ | |
7038 if (!to->equals(Type::tvoid) && | |
7039 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
7040 return Expression::checkSideEffect(flag); | |
7041 return 1; | |
7042 } | |
7043 | |
7044 void CastExp::checkEscape() | |
7045 { Type *tb = type->toBasetype(); | |
7046 if (tb->ty == Tarray && e1->op == TOKvar && | |
7047 e1->type->toBasetype()->ty == Tsarray) | |
7048 { VarExp *ve = (VarExp *)e1; | |
7049 VarDeclaration *v = ve->var->isVarDeclaration(); | |
7050 if (v) | |
7051 { | |
7052 if (!v->isDataseg() && !v->isParameter()) | |
7053 error("escaping reference to local %s", v->toChars()); | |
7054 } | |
7055 } | |
7056 } | |
7057 | |
7058 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7059 { | |
7060 buf->writestring("cast("); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7061 #if DMDV1 |
159 | 7062 to->toCBuffer(buf, NULL, hgs); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7063 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7064 if (to) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7065 to->toCBuffer(buf, NULL, hgs); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7066 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7067 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7068 switch (mod) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7069 { case 0: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7070 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7071 case MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7072 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7073 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7074 case MODinvariant: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7075 buf->writestring(Token::tochars[TOKimmutable]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7076 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7077 case MODshared: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7078 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7079 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7080 case MODshared | MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7081 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7082 buf->writeByte(' '); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7083 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7084 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7085 default: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7086 assert(0); |
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 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7089 #endif |
159 | 7090 buf->writeByte(')'); |
7091 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7092 } | |
7093 | |
7094 | |
7095 /************************************************************/ | |
7096 | |
7097 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
7098 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
7099 { | |
7100 this->upr = upr; | |
7101 this->lwr = lwr; | |
7102 lengthVar = NULL; | |
7103 } | |
7104 | |
7105 Expression *SliceExp::syntaxCopy() | |
7106 { | |
7107 Expression *lwr = NULL; | |
7108 if (this->lwr) | |
7109 lwr = this->lwr->syntaxCopy(); | |
7110 | |
7111 Expression *upr = NULL; | |
7112 if (this->upr) | |
7113 upr = this->upr->syntaxCopy(); | |
7114 | |
7115 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
7116 } | |
7117 | |
7118 Expression *SliceExp::semantic(Scope *sc) | |
7119 { Expression *e; | |
7120 AggregateDeclaration *ad; | |
7121 //FuncDeclaration *fd; | |
7122 ScopeDsymbol *sym; | |
7123 | |
7124 #if LOGSEMANTIC | |
7125 printf("SliceExp::semantic('%s')\n", toChars()); | |
7126 #endif | |
7127 if (type) | |
7128 return this; | |
7129 | |
7130 UnaExp::semantic(sc); | |
7131 e1 = resolveProperties(sc, e1); | |
7132 | |
7133 e = this; | |
7134 | |
7135 Type *t = e1->type->toBasetype(); | |
7136 if (t->ty == Tpointer) | |
7137 { | |
7138 if (!lwr || !upr) | |
7139 error("need upper and lower bound to slice pointer"); | |
7140 } | |
7141 else if (t->ty == Tarray) | |
7142 { | |
7143 } | |
7144 else if (t->ty == Tsarray) | |
7145 { | |
7146 } | |
7147 else if (t->ty == Tclass) | |
7148 { | |
7149 ad = ((TypeClass *)t)->sym; | |
7150 goto L1; | |
7151 } | |
7152 else if (t->ty == Tstruct) | |
7153 { | |
7154 ad = ((TypeStruct *)t)->sym; | |
7155 | |
7156 L1: | |
7157 if (search_function(ad, Id::slice)) | |
7158 { | |
7159 // Rewrite as e1.slice(lwr, upr) | |
7160 e = new DotIdExp(loc, e1, Id::slice); | |
7161 | |
7162 if (lwr) | |
7163 { | |
7164 assert(upr); | |
7165 e = new CallExp(loc, e, lwr, upr); | |
7166 } | |
7167 else | |
7168 { assert(!upr); | |
7169 e = new CallExp(loc, e); | |
7170 } | |
7171 e = e->semantic(sc); | |
7172 return e; | |
7173 } | |
7174 goto Lerror; | |
7175 } | |
7176 else if (t->ty == Ttuple) | |
7177 { | |
7178 if (!lwr && !upr) | |
7179 return e1; | |
7180 if (!lwr || !upr) | |
7181 { error("need upper and lower bound to slice tuple"); | |
7182 goto Lerror; | |
7183 } | |
7184 } | |
7185 else | |
7186 goto Lerror; | |
7187 | |
7188 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7189 { | |
7190 sym = new ArrayScopeSymbol(this); | |
7191 sym->loc = loc; | |
7192 sym->parent = sc->scopesym; | |
7193 sc = sc->push(sym); | |
7194 } | |
7195 | |
7196 if (lwr) | |
7197 { lwr = lwr->semantic(sc); | |
7198 lwr = resolveProperties(sc, lwr); | |
7199 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
7200 } | |
7201 if (upr) | |
7202 { upr = upr->semantic(sc); | |
7203 upr = resolveProperties(sc, upr); | |
7204 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
7205 } | |
7206 | |
7207 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7208 sc->pop(); | |
7209 | |
7210 if (t->ty == Ttuple) | |
7211 { | |
7212 lwr = lwr->optimize(WANTvalue); | |
7213 upr = upr->optimize(WANTvalue); | |
7214 uinteger_t i1 = lwr->toUInteger(); | |
7215 uinteger_t i2 = upr->toUInteger(); | |
7216 | |
7217 size_t length; | |
7218 TupleExp *te; | |
7219 TypeTuple *tup; | |
7220 | |
7221 if (e1->op == TOKtuple) // slicing an expression tuple | |
7222 { te = (TupleExp *)e1; | |
7223 length = te->exps->dim; | |
7224 } | |
7225 else if (e1->op == TOKtype) // slicing a type tuple | |
7226 { tup = (TypeTuple *)t; | |
7227 length = Argument::dim(tup->arguments); | |
7228 } | |
7229 else | |
7230 assert(0); | |
7231 | |
7232 if (i1 <= i2 && i2 <= length) | |
7233 { size_t j1 = (size_t) i1; | |
7234 size_t j2 = (size_t) i2; | |
7235 | |
7236 if (e1->op == TOKtuple) | |
7237 { Expressions *exps = new Expressions; | |
7238 exps->setDim(j2 - j1); | |
7239 for (size_t i = 0; i < j2 - j1; i++) | |
7240 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
7241 exps->data[i] = (void *)e; | |
7242 } | |
7243 e = new TupleExp(loc, exps); | |
7244 } | |
7245 else | |
7246 { Arguments *args = new Arguments; | |
7247 args->reserve(j2 - j1); | |
7248 for (size_t i = j1; i < j2; i++) | |
7249 { Argument *arg = Argument::getNth(tup->arguments, i); | |
7250 args->push(arg); | |
7251 } | |
7252 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
7253 } | |
7254 e = e->semantic(sc); | |
7255 } | |
7256 else | |
7257 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7258 error("string slice [%ju .. %ju] is out of bounds", i1, i2); |
159 | 7259 e = e1; |
7260 } | |
7261 return e; | |
7262 } | |
7263 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7264 if (t->ty == Tarray) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7265 type = e1->type; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7266 else |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7267 type = t->nextOf()->arrayOf(); |
159 | 7268 return e; |
7269 | |
7270 Lerror: | |
7271 char *s; | |
7272 if (t->ty == Tvoid) | |
7273 s = e1->toChars(); | |
7274 else | |
7275 s = t->toChars(); | |
7276 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
|
7277 e = new IntegerExp(0); |
159 | 7278 return e; |
7279 } | |
7280 | |
7281 void SliceExp::checkEscape() | |
7282 { | |
7283 e1->checkEscape(); | |
7284 } | |
7285 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7286 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7287 int SliceExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7288 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7289 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7290 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7291 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7292 |
159 | 7293 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) |
7294 { | |
7295 return this; | |
7296 } | |
7297 | |
7298 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
7299 { | |
7300 error("slice expression %s is not a modifiable lvalue", toChars()); | |
7301 return this; | |
7302 } | |
7303 | |
7304 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7305 { | |
7306 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7307 buf->writeByte('['); | |
7308 if (upr || lwr) | |
7309 { | |
7310 if (lwr) | |
7311 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
7312 else | |
7313 buf->writeByte('0'); | |
7314 buf->writestring(".."); | |
7315 if (upr) | |
7316 expToCBuffer(buf, hgs, upr, PREC_assign); | |
7317 else | |
7318 buf->writestring("length"); // BUG: should be array.length | |
7319 } | |
7320 buf->writeByte(']'); | |
7321 } | |
7322 | |
7323 /********************** ArrayLength **************************************/ | |
7324 | |
7325 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
7326 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
7327 { | |
7328 } | |
7329 | |
7330 Expression *ArrayLengthExp::semantic(Scope *sc) | |
7331 { Expression *e; | |
7332 | |
7333 #if LOGSEMANTIC | |
7334 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
7335 #endif | |
7336 if (!type) | |
7337 { | |
7338 UnaExp::semantic(sc); | |
7339 e1 = resolveProperties(sc, e1); | |
7340 | |
7341 type = Type::tsize_t; | |
7342 } | |
7343 return this; | |
7344 } | |
7345 | |
7346 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7347 { | |
7348 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7349 buf->writestring(".length"); | |
7350 } | |
7351 | |
7352 /*********************** ArrayExp *************************************/ | |
7353 | |
7354 // e1 [ i1, i2, i3, ... ] | |
7355 | |
7356 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
7357 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
7358 { | |
7359 arguments = args; | |
7360 } | |
7361 | |
7362 Expression *ArrayExp::syntaxCopy() | |
7363 { | |
7364 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
7365 } | |
7366 | |
7367 Expression *ArrayExp::semantic(Scope *sc) | |
7368 { Expression *e; | |
7369 Type *t1; | |
7370 | |
7371 #if LOGSEMANTIC | |
7372 printf("ArrayExp::semantic('%s')\n", toChars()); | |
7373 #endif | |
7374 UnaExp::semantic(sc); | |
7375 e1 = resolveProperties(sc, e1); | |
7376 | |
7377 t1 = e1->type->toBasetype(); | |
7378 if (t1->ty != Tclass && t1->ty != Tstruct) | |
7379 { // Convert to IndexExp | |
7380 if (arguments->dim != 1) | |
7381 error("only one index allowed to index %s", t1->toChars()); | |
7382 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
7383 return e->semantic(sc); | |
7384 } | |
7385 | |
7386 // Run semantic() on each argument | |
7387 for (size_t i = 0; i < arguments->dim; i++) | |
7388 { e = (Expression *)arguments->data[i]; | |
7389 | |
7390 e = e->semantic(sc); | |
7391 if (!e->type) | |
7392 error("%s has no value", e->toChars()); | |
7393 arguments->data[i] = (void *)e; | |
7394 } | |
7395 | |
7396 expandTuples(arguments); | |
7397 assert(arguments && arguments->dim); | |
7398 | |
7399 e = op_overload(sc); | |
7400 if (!e) | |
7401 { error("no [] operator overload for type %s", e1->type->toChars()); | |
7402 e = e1; | |
7403 } | |
7404 return e; | |
7405 } | |
7406 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7407 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7408 int ArrayExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7409 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7410 if (type && type->toBasetype()->ty == Tvoid) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7411 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7412 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7413 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7414 #endif |
159 | 7415 |
7416 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
7417 { | |
7418 if (type && type->toBasetype()->ty == Tvoid) | |
7419 error("voids have no value"); | |
7420 return this; | |
7421 } | |
7422 | |
7423 | |
7424 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7425 { int i; | |
7426 | |
7427 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7428 buf->writeByte('['); | |
7429 argsToCBuffer(buf, arguments, hgs); | |
7430 buf->writeByte(']'); | |
7431 } | |
7432 | |
7433 /************************* DotExp ***********************************/ | |
7434 | |
7435 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
7436 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
7437 { | |
7438 } | |
7439 | |
7440 Expression *DotExp::semantic(Scope *sc) | |
7441 { | |
7442 #if LOGSEMANTIC | |
7443 printf("DotExp::semantic('%s')\n", toChars()); | |
7444 if (type) printf("\ttype = %s\n", type->toChars()); | |
7445 #endif | |
7446 e1 = e1->semantic(sc); | |
7447 e2 = e2->semantic(sc); | |
7448 if (e2->op == TOKimport) | |
7449 { | |
7450 ScopeExp *se = (ScopeExp *)e2; | |
7451 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
7452 if (td) | |
7453 { Expression *e = new DotTemplateExp(loc, e1, td); | |
7454 e = e->semantic(sc); | |
7455 return e; | |
7456 } | |
7457 } | |
7458 if (!type) | |
7459 type = e2->type; | |
7460 return this; | |
7461 } | |
7462 | |
7463 | |
7464 /************************* CommaExp ***********************************/ | |
7465 | |
7466 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
7467 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
7468 { | |
7469 } | |
7470 | |
7471 Expression *CommaExp::semantic(Scope *sc) | |
7472 { | |
7473 if (!type) | |
7474 { BinExp::semanticp(sc); | |
7475 type = e2->type; | |
7476 } | |
7477 return this; | |
7478 } | |
7479 | |
7480 void CommaExp::checkEscape() | |
7481 { | |
7482 e2->checkEscape(); | |
7483 } | |
7484 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7485 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7486 int CommaExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7487 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7488 return e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7489 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7490 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7491 |
159 | 7492 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) |
7493 { | |
7494 e2 = e2->toLvalue(sc, NULL); | |
7495 return this; | |
7496 } | |
7497 | |
7498 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7499 { | |
7500 e2 = e2->modifiableLvalue(sc, e); | |
7501 return this; | |
7502 } | |
7503 | |
7504 int CommaExp::isBool(int result) | |
7505 { | |
7506 return e2->isBool(result); | |
7507 } | |
7508 | |
7509 int CommaExp::checkSideEffect(int flag) | |
7510 { | |
7511 if (flag == 2) | |
7512 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7513 else | |
7514 { | |
7515 // Don't check e1 until we cast(void) the a,b code generation | |
7516 return e2->checkSideEffect(flag); | |
7517 } | |
7518 } | |
7519 | |
7520 /************************** IndexExp **********************************/ | |
7521 | |
7522 // e1 [ e2 ] | |
7523 | |
7524 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7525 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7526 { | |
7527 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7528 lengthVar = NULL; | |
7529 modifiable = 0; // assume it is an rvalue | |
7530 } | |
7531 | |
7532 Expression *IndexExp::semantic(Scope *sc) | |
7533 { Expression *e; | |
7534 BinExp *b; | |
7535 UnaExp *u; | |
7536 Type *t1; | |
7537 ScopeDsymbol *sym; | |
7538 | |
7539 #if LOGSEMANTIC | |
7540 printf("IndexExp::semantic('%s')\n", toChars()); | |
7541 #endif | |
7542 if (type) | |
7543 return this; | |
7544 if (!e1->type) | |
7545 e1 = e1->semantic(sc); | |
7546 assert(e1->type); // semantic() should already be run on it | |
7547 e = this; | |
7548 | |
7549 // Note that unlike C we do not implement the int[ptr] | |
7550 | |
7551 t1 = e1->type->toBasetype(); | |
7552 | |
7553 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7554 { // Create scope for 'length' variable | |
7555 sym = new ArrayScopeSymbol(this); | |
7556 sym->loc = loc; | |
7557 sym->parent = sc->scopesym; | |
7558 sc = sc->push(sym); | |
7559 } | |
7560 | |
7561 e2 = e2->semantic(sc); | |
7562 if (!e2->type) | |
7563 { | |
7564 error("%s has no value", e2->toChars()); | |
7565 e2->type = Type::terror; | |
7566 } | |
7567 e2 = resolveProperties(sc, e2); | |
7568 | |
7569 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7570 sc = sc->pop(); | |
7571 | |
7572 switch (t1->ty) | |
7573 { | |
7574 case Tpointer: | |
7575 case Tarray: | |
7576 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7577 e->type = t1->next; | |
7578 break; | |
7579 | |
7580 case Tsarray: | |
7581 { | |
7582 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7583 | |
7584 TypeSArray *tsa = (TypeSArray *)t1; | |
7585 | |
7586 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7587 // Do compile time array bounds checking if possible | |
7588 e2 = e2->optimize(WANTvalue); | |
7589 if (e2->op == TOKint64) | |
7590 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
7591 dinteger_t index = e2->toInteger(); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
7592 dinteger_t length = tsa->dim->toInteger(); |
159 | 7593 if (index < 0 || index >= length) |
7594 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7595 index, length); | |
7596 } | |
7597 #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
|
7598 e->type = t1->nextOf(); |
159 | 7599 break; |
7600 } | |
7601 | |
7602 case Taarray: | |
7603 { TypeAArray *taa = (TypeAArray *)t1; | |
7604 | |
7605 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7606 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7607 type = taa->next; | |
7608 break; | |
7609 } | |
7610 | |
7611 case Ttuple: | |
7612 { | |
7613 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7614 e2 = e2->optimize(WANTvalue); | |
7615 uinteger_t index = e2->toUInteger(); | |
7616 size_t length; | |
7617 TupleExp *te; | |
7618 TypeTuple *tup; | |
7619 | |
7620 if (e1->op == TOKtuple) | |
7621 { te = (TupleExp *)e1; | |
7622 length = te->exps->dim; | |
7623 } | |
7624 else if (e1->op == TOKtype) | |
7625 { | |
7626 tup = (TypeTuple *)t1; | |
7627 length = Argument::dim(tup->arguments); | |
7628 } | |
7629 else | |
7630 assert(0); | |
7631 | |
7632 if (index < length) | |
7633 { | |
7634 | |
7635 if (e1->op == TOKtuple) | |
7636 e = (Expression *)te->exps->data[(size_t)index]; | |
7637 else | |
7638 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
7639 } | |
7640 else | |
7641 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7642 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
|
7643 index, length); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7644 e = e1; |
159 | 7645 } |
7646 break; | |
7647 } | |
7648 | |
7649 default: | |
7650 error("%s must be an array or pointer type, not %s", | |
7651 e1->toChars(), e1->type->toChars()); | |
7652 type = Type::tint32; | |
7653 break; | |
7654 } | |
7655 return e; | |
7656 } | |
7657 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7658 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7659 int IndexExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7660 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7661 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7662 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7663 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7664 |
159 | 7665 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) |
7666 { | |
7667 // if (type && type->toBasetype()->ty == Tvoid) | |
7668 // error("voids have no value"); | |
7669 return this; | |
7670 } | |
7671 | |
7672 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
7673 { | |
7674 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
7675 modifiable = 1; | |
7676 if (e1->op == TOKstring) | |
7677 error("string literals are immutable"); | |
7678 if (e1->type->toBasetype()->ty == Taarray) | |
7679 e1 = e1->modifiableLvalue(sc, e1); | |
7680 return toLvalue(sc, e); | |
7681 } | |
7682 | |
7683 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7684 { | |
7685 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7686 buf->writeByte('['); | |
7687 expToCBuffer(buf, hgs, e2, PREC_assign); | |
7688 buf->writeByte(']'); | |
7689 } | |
7690 | |
7691 | |
7692 /************************* PostExp ***********************************/ | |
7693 | |
7694 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
7695 : BinExp(loc, op, sizeof(PostExp), e, | |
7696 new IntegerExp(loc, 1, Type::tint32)) | |
7697 { | |
7698 } | |
7699 | |
7700 Expression *PostExp::semantic(Scope *sc) | |
7701 { Expression *e = this; | |
7702 | |
7703 if (!type) | |
7704 { | |
7705 BinExp::semantic(sc); | |
7706 e2 = resolveProperties(sc, e2); | |
7707 | |
7708 e = op_overload(sc); | |
7709 if (e) | |
7710 return e; | |
7711 | |
7712 e = this; | |
7713 e1 = e1->modifiableLvalue(sc, e1); | |
7714 e1->checkScalar(); | |
7715 e1->checkNoBool(); | |
7716 if (e1->type->ty == Tpointer) | |
7717 e = scaleFactor(sc); | |
7718 else | |
7719 e2 = e2->castTo(sc, e1->type); | |
7720 e->type = e1->type; | |
7721 } | |
7722 return e; | |
7723 } | |
7724 | |
7725 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7726 { | |
7727 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7728 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
7729 } | |
7730 | |
7731 /************************************************************/ | |
7732 | |
7733 /* Can be TOKconstruct too */ | |
7734 | |
7735 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
7736 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
7737 { | |
7738 ismemset = 0; | |
7739 } | |
7740 | |
7741 Expression *AssignExp::semantic(Scope *sc) | |
7742 { Type *t1; | |
7743 Expression *e1old = e1; | |
7744 | |
7745 #if LOGSEMANTIC | |
7746 printf("AssignExp::semantic('%s')\n", toChars()); | |
7747 #endif | |
7748 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
7749 | |
7750 /* Look for operator overloading of a[i]=value. | |
7751 * Do it before semantic() otherwise the a[i] will have been | |
7752 * converted to a.opIndex() already. | |
7753 */ | |
7754 if (e1->op == TOKarray) | |
7755 { Type *t1; | |
7756 ArrayExp *ae = (ArrayExp *)e1; | |
7757 AggregateDeclaration *ad; | |
7758 Identifier *id = Id::index; | |
7759 | |
7760 ae->e1 = ae->e1->semantic(sc); | |
7761 t1 = ae->e1->type->toBasetype(); | |
7762 if (t1->ty == Tstruct) | |
7763 { | |
7764 ad = ((TypeStruct *)t1)->sym; | |
7765 goto L1; | |
7766 } | |
7767 else if (t1->ty == Tclass) | |
7768 { | |
7769 ad = ((TypeClass *)t1)->sym; | |
7770 L1: | |
7771 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
7772 if (search_function(ad, Id::indexass)) | |
7773 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
7774 Expressions *a = (Expressions *)ae->arguments->copy(); | |
7775 | |
7776 a->insert(0, e2); | |
7777 e = new CallExp(loc, e, a); | |
7778 e = e->semantic(sc); | |
7779 return e; | |
7780 } | |
7781 else | |
7782 { | |
7783 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
7784 if (search_function(ad, id)) | |
7785 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
7786 | |
7787 if (1 || !global.params.useDeprecated) | |
7788 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
7789 | |
7790 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
7791 e = e->semantic(sc); | |
7792 return e; | |
7793 } | |
7794 } | |
7795 } | |
7796 } | |
7797 /* Look for operator overloading of a[i..j]=value. | |
7798 * Do it before semantic() otherwise the a[i..j] will have been | |
7799 * converted to a.opSlice() already. | |
7800 */ | |
7801 if (e1->op == TOKslice) | |
7802 { Type *t1; | |
7803 SliceExp *ae = (SliceExp *)e1; | |
7804 AggregateDeclaration *ad; | |
7805 Identifier *id = Id::index; | |
7806 | |
7807 ae->e1 = ae->e1->semantic(sc); | |
7808 ae->e1 = resolveProperties(sc, ae->e1); | |
7809 t1 = ae->e1->type->toBasetype(); | |
7810 if (t1->ty == Tstruct) | |
7811 { | |
7812 ad = ((TypeStruct *)t1)->sym; | |
7813 goto L2; | |
7814 } | |
7815 else if (t1->ty == Tclass) | |
7816 { | |
7817 ad = ((TypeClass *)t1)->sym; | |
7818 L2: | |
7819 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
7820 if (search_function(ad, Id::sliceass)) | |
7821 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
7822 Expressions *a = new Expressions(); | |
7823 | |
7824 a->push(e2); | |
7825 if (ae->lwr) | |
7826 { a->push(ae->lwr); | |
7827 assert(ae->upr); | |
7828 a->push(ae->upr); | |
7829 } | |
7830 else | |
7831 assert(!ae->upr); | |
7832 e = new CallExp(loc, e, a); | |
7833 e = e->semantic(sc); | |
7834 return e; | |
7835 } | |
7836 } | |
7837 } | |
7838 | |
7839 BinExp::semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7840 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7841 if (e1->op == TOKdottd) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7842 { // 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
|
7843 Expression *e = new CallExp(loc, e1, e2); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7844 e = e->semantic(sc); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7845 return e; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7846 } |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7847 |
159 | 7848 e2 = resolveProperties(sc, e2); |
7849 assert(e1->type); | |
7850 | |
7851 /* Rewrite tuple assignment as a tuple of assignments. | |
7852 */ | |
7853 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
7854 { TupleExp *tup1 = (TupleExp *)e1; | |
7855 TupleExp *tup2 = (TupleExp *)e2; | |
7856 size_t dim = tup1->exps->dim; | |
7857 if (dim != tup2->exps->dim) | |
7858 { | |
7859 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
7860 } | |
7861 else | |
7862 { Expressions *exps = new Expressions; | |
7863 exps->setDim(dim); | |
7864 | |
7865 for (int i = 0; i < dim; i++) | |
7866 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
7867 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
7868 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
7869 } | |
7870 Expression *e = new TupleExp(loc, exps); | |
7871 e = e->semantic(sc); | |
7872 return e; | |
7873 } | |
7874 } | |
7875 | |
7876 t1 = e1->type->toBasetype(); | |
7877 | |
7878 if (t1->ty == Tfunction) | |
7879 { // Rewrite f=value to f(value) | |
7880 Expression *e; | |
7881 | |
7882 e = new CallExp(loc, e1, e2); | |
7883 e = e->semantic(sc); | |
7884 return e; | |
7885 } | |
7886 | |
7887 /* If it is an assignment from a 'foreign' type, | |
7888 * check for operator overloading. | |
7889 */ | |
7890 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7891 { | |
7892 if (!e2->type->implicitConvTo(e1->type)) | |
7893 { | |
7894 Expression *e = op_overload(sc); | |
7895 if (e) | |
7896 return e; | |
7897 } | |
7898 } | |
7899 | |
7900 e2->rvalue(); | |
7901 | |
7902 if (e1->op == TOKarraylength) | |
7903 { | |
7904 // e1 is not an lvalue, but we let code generator handle it | |
7905 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7906 | |
7907 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
7908 } | |
7909 else if (e1->op == TOKslice) | |
7910 ; | |
7911 else | |
7912 { // Try to do a decent error message with the expression | |
7913 // before it got constant folded | |
7914 e1 = e1->modifiableLvalue(sc, e1old); | |
7915 } | |
7916 | |
7917 if (e1->op == TOKslice && | |
7918 t1->nextOf() && | |
7919 e2->implicitConvTo(t1->nextOf()) | |
7920 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
7921 ) | |
7922 { // memset | |
7923 ismemset = 1; // make it easy for back end to tell what this is | |
7924 e2 = e2->implicitCastTo(sc, t1->next); | |
7925 } | |
7926 else if (t1->ty == Tsarray) | |
7927 { | |
7928 error("cannot assign to static array %s", e1->toChars()); | |
7929 } | |
7930 else | |
7931 { | |
7932 e2 = e2->implicitCastTo(sc, e1->type); | |
7933 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7934 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7935 /* 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
|
7936 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7937 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
|
7938 (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
|
7939 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
|
7940 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
|
7941 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
|
7942 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
|
7943 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7944 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
|
7945 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
|
7946 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7947 |
159 | 7948 type = e1->type; |
7949 assert(type); | |
7950 return this; | |
7951 } | |
7952 | |
7953 Expression *AssignExp::checkToBoolean() | |
7954 { | |
7955 // Things like: | |
7956 // if (a = b) ... | |
7957 // are usually mistakes. | |
7958 | |
7959 error("'=' does not give a boolean result"); | |
7960 return this; | |
7961 } | |
7962 | |
7963 /************************************************************/ | |
7964 | |
7965 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7966 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7967 { | |
7968 } | |
7969 | |
7970 Expression *AddAssignExp::semantic(Scope *sc) | |
7971 { Expression *e; | |
7972 | |
7973 if (type) | |
7974 return this; | |
7975 | |
7976 BinExp::semantic(sc); | |
7977 e2 = resolveProperties(sc, e2); | |
7978 | |
7979 e = op_overload(sc); | |
7980 if (e) | |
7981 return e; | |
7982 | |
7983 Type *tb1 = e1->type->toBasetype(); | |
7984 Type *tb2 = e2->type->toBasetype(); | |
7985 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7986 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
|
7987 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7988 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
|
7989 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
|
7990 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
|
7991 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7992 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7993 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7994 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
|
7995 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7996 |
159 | 7997 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
7998 (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
|
7999 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8000 ) |
8001 { | |
8002 type = e1->type; | |
8003 e = this; | |
8004 } | |
8005 else | |
8006 { | |
8007 e1->checkScalar(); | |
8008 e1->checkNoBool(); | |
8009 if (tb1->ty == Tpointer && tb2->isintegral()) | |
8010 e = scaleFactor(sc); | |
8011 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
8012 { | |
8013 #if 0 | |
8014 // Need to rethink this | |
8015 if (e1->op != TOKvar) | |
8016 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
8017 VarDeclaration *v; | |
8018 Expression *ea; | |
8019 Expression *ex; | |
8020 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8021 Identifier *id = Lexer::uniqueId("__name"); |
159 | 8022 |
8023 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
8024 v->semantic(sc); | |
8025 if (!sc->insert(v)) | |
8026 assert(0); | |
8027 v->parent = sc->func; | |
8028 | |
8029 ea = new AddrExp(loc, e1); | |
8030 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
8031 | |
8032 ex = new VarExp(loc, v); | |
8033 ex = new PtrExp(loc, ex); | |
8034 e = new AddExp(loc, ex, e2); | |
8035 e = new CastExp(loc, e, e1->type); | |
8036 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
8037 | |
8038 e = new CommaExp(loc, ea, e); | |
8039 } | |
8040 else | |
8041 #endif | |
8042 { // Rewrite e1+=e2 to e1=e1+e2 | |
8043 // BUG: doesn't account for side effects in e1 | |
8044 // BUG: other assignment operators for bits aren't handled at all | |
8045 e = new AddExp(loc, e1, e2); | |
8046 e = new CastExp(loc, e, e1->type); | |
8047 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
8048 } | |
8049 e = e->semantic(sc); | |
8050 } | |
8051 else | |
8052 { | |
8053 type = e1->type; | |
8054 typeCombine(sc); | |
8055 e1->checkArithmetic(); | |
8056 e2->checkArithmetic(); | |
8057 if (type->isreal() || type->isimaginary()) | |
8058 { | |
8059 assert(global.errors || e2->type->isfloating()); | |
8060 e2 = e2->castTo(sc, e1->type); | |
8061 } | |
8062 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8063 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8064 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
|
8065 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8066 } |
8067 } | |
8068 return e; | |
8069 } | |
8070 | |
8071 /************************************************************/ | |
8072 | |
8073 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8074 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
8075 { | |
8076 } | |
8077 | |
8078 Expression *MinAssignExp::semantic(Scope *sc) | |
8079 { Expression *e; | |
8080 | |
8081 if (type) | |
8082 return this; | |
8083 | |
8084 BinExp::semantic(sc); | |
8085 e2 = resolveProperties(sc, e2); | |
8086 | |
8087 e = op_overload(sc); | |
8088 if (e) | |
8089 return e; | |
8090 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8091 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
|
8092 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8093 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
|
8094 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
|
8095 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
|
8096 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8097 |
159 | 8098 e1 = e1->modifiableLvalue(sc, e1); |
8099 e1->checkScalar(); | |
8100 e1->checkNoBool(); | |
8101 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
8102 e = scaleFactor(sc); | |
8103 else | |
8104 { | |
8105 e1 = e1->checkArithmetic(); | |
8106 e2 = e2->checkArithmetic(); | |
8107 type = e1->type; | |
8108 typeCombine(sc); | |
8109 if (type->isreal() || type->isimaginary()) | |
8110 { | |
8111 assert(e2->type->isfloating()); | |
8112 e2 = e2->castTo(sc, e1->type); | |
8113 } | |
8114 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8115 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8116 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
|
8117 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8118 } |
8119 return e; | |
8120 } | |
8121 | |
8122 /************************************************************/ | |
8123 | |
8124 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8125 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
8126 { | |
8127 } | |
8128 | |
8129 Expression *CatAssignExp::semantic(Scope *sc) | |
8130 { Expression *e; | |
8131 | |
8132 BinExp::semantic(sc); | |
8133 e2 = resolveProperties(sc, e2); | |
8134 | |
8135 e = op_overload(sc); | |
8136 if (e) | |
8137 return e; | |
8138 | |
8139 if (e1->op == TOKslice) | |
8140 { SliceExp *se = (SliceExp *)e1; | |
8141 | |
8142 if (se->e1->type->toBasetype()->ty == Tsarray) | |
8143 error("cannot append to static array %s", se->e1->type->toChars()); | |
8144 } | |
8145 | |
8146 e1 = e1->modifiableLvalue(sc, e1); | |
8147 | |
8148 Type *tb1 = e1->type->toBasetype(); | |
8149 Type *tb2 = e2->type->toBasetype(); | |
8150 | |
8151 e2->rvalue(); | |
8152 | |
8153 if ((tb1->ty == Tarray) && | |
8154 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
8155 e2->implicitConvTo(e1->type) | |
8156 //e1->type->next->equals(e2->type->next) | |
8157 ) | |
8158 { // Append array | |
8159 e2 = e2->castTo(sc, e1->type); | |
8160 type = e1->type; | |
8161 e = this; | |
8162 } | |
8163 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
|
8164 e2->implicitConvTo(tb1->nextOf()) |
159 | 8165 ) |
8166 { // 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
|
8167 e2 = e2->castTo(sc, tb1->nextOf()); |
159 | 8168 type = e1->type; |
8169 e = this; | |
8170 } | |
8171 else | |
8172 { | |
8173 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
8174 type = Type::tint32; | |
8175 e = this; | |
8176 } | |
8177 return e; | |
8178 } | |
8179 | |
8180 /************************************************************/ | |
8181 | |
8182 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8183 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
8184 { | |
8185 } | |
8186 | |
8187 Expression *MulAssignExp::semantic(Scope *sc) | |
8188 { Expression *e; | |
8189 | |
8190 BinExp::semantic(sc); | |
8191 e2 = resolveProperties(sc, e2); | |
8192 | |
8193 e = op_overload(sc); | |
8194 if (e) | |
8195 return e; | |
8196 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8197 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
|
8198 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8199 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
|
8200 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
|
8201 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
|
8202 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8203 |
159 | 8204 e1 = e1->modifiableLvalue(sc, e1); |
8205 e1->checkScalar(); | |
8206 e1->checkNoBool(); | |
8207 type = e1->type; | |
8208 typeCombine(sc); | |
8209 e1->checkArithmetic(); | |
8210 e2->checkArithmetic(); | |
8211 if (e2->type->isfloating()) | |
8212 { Type *t1; | |
8213 Type *t2; | |
8214 | |
8215 t1 = e1->type; | |
8216 t2 = e2->type; | |
8217 if (t1->isreal()) | |
8218 { | |
8219 if (t2->isimaginary() || t2->iscomplex()) | |
8220 { | |
8221 e2 = e2->castTo(sc, t1); | |
8222 } | |
8223 } | |
8224 else if (t1->isimaginary()) | |
8225 { | |
8226 if (t2->isimaginary() || t2->iscomplex()) | |
8227 { | |
8228 switch (t1->ty) | |
8229 { | |
8230 case Timaginary32: t2 = Type::tfloat32; break; | |
8231 case Timaginary64: t2 = Type::tfloat64; break; | |
8232 case Timaginary80: t2 = Type::tfloat80; break; | |
8233 default: | |
8234 assert(0); | |
8235 } | |
8236 e2 = e2->castTo(sc, t2); | |
8237 } | |
8238 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8239 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8240 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
|
8241 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8242 } |
8243 return this; | |
8244 } | |
8245 | |
8246 /************************************************************/ | |
8247 | |
8248 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8249 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
8250 { | |
8251 } | |
8252 | |
8253 Expression *DivAssignExp::semantic(Scope *sc) | |
8254 { Expression *e; | |
8255 | |
8256 BinExp::semantic(sc); | |
8257 e2 = resolveProperties(sc, e2); | |
8258 | |
8259 e = op_overload(sc); | |
8260 if (e) | |
8261 return e; | |
8262 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8263 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
|
8264 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8265 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
|
8266 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
|
8267 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
|
8268 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8269 |
159 | 8270 e1 = e1->modifiableLvalue(sc, e1); |
8271 e1->checkScalar(); | |
8272 e1->checkNoBool(); | |
8273 type = e1->type; | |
8274 typeCombine(sc); | |
8275 e1->checkArithmetic(); | |
8276 e2->checkArithmetic(); | |
8277 if (e2->type->isimaginary()) | |
8278 { Type *t1; | |
8279 Type *t2; | |
8280 | |
8281 t1 = e1->type; | |
8282 if (t1->isreal()) | |
8283 { // x/iv = i(-x/v) | |
8284 // Therefore, the result is 0 | |
8285 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
8286 e2->type = t1; | |
8287 e = new AssignExp(loc, e1, e2); | |
8288 e->type = t1; | |
8289 return e; | |
8290 } | |
8291 else if (t1->isimaginary()) | |
8292 { Expression *e; | |
8293 | |
8294 switch (t1->ty) | |
8295 { | |
8296 case Timaginary32: t2 = Type::tfloat32; break; | |
8297 case Timaginary64: t2 = Type::tfloat64; break; | |
8298 case Timaginary80: t2 = Type::tfloat80; break; | |
8299 default: | |
8300 assert(0); | |
8301 } | |
8302 e2 = e2->castTo(sc, t2); | |
8303 e = new AssignExp(loc, e1, e2); | |
8304 e->type = t1; | |
8305 return e; | |
8306 } | |
8307 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8308 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8309 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
|
8310 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
|
8311 |
159 | 8312 return this; |
8313 } | |
8314 | |
8315 /************************************************************/ | |
8316 | |
8317 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8318 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
8319 { | |
8320 } | |
8321 | |
8322 Expression *ModAssignExp::semantic(Scope *sc) | |
8323 { | |
8324 return commonSemanticAssign(sc); | |
8325 } | |
8326 | |
8327 /************************************************************/ | |
8328 | |
8329 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8330 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
8331 { | |
8332 } | |
8333 | |
8334 Expression *ShlAssignExp::semantic(Scope *sc) | |
8335 { Expression *e; | |
8336 | |
8337 //printf("ShlAssignExp::semantic()\n"); | |
8338 BinExp::semantic(sc); | |
8339 e2 = resolveProperties(sc, e2); | |
8340 | |
8341 e = op_overload(sc); | |
8342 if (e) | |
8343 return e; | |
8344 | |
8345 e1 = e1->modifiableLvalue(sc, e1); | |
8346 e1->checkScalar(); | |
8347 e1->checkNoBool(); | |
8348 type = e1->type; | |
8349 typeCombine(sc); | |
8350 e1->checkIntegral(); | |
8351 e2 = e2->checkIntegral(); | |
8352 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8353 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8354 return this; |
8355 } | |
8356 | |
8357 /************************************************************/ | |
8358 | |
8359 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8360 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
8361 { | |
8362 } | |
8363 | |
8364 Expression *ShrAssignExp::semantic(Scope *sc) | |
8365 { Expression *e; | |
8366 | |
8367 BinExp::semantic(sc); | |
8368 e2 = resolveProperties(sc, e2); | |
8369 | |
8370 e = op_overload(sc); | |
8371 if (e) | |
8372 return e; | |
8373 | |
8374 e1 = e1->modifiableLvalue(sc, e1); | |
8375 e1->checkScalar(); | |
8376 e1->checkNoBool(); | |
8377 type = e1->type; | |
8378 typeCombine(sc); | |
8379 e1->checkIntegral(); | |
8380 e2 = e2->checkIntegral(); | |
8381 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8382 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8383 return this; |
8384 } | |
8385 | |
8386 /************************************************************/ | |
8387 | |
8388 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8389 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
8390 { | |
8391 } | |
8392 | |
8393 Expression *UshrAssignExp::semantic(Scope *sc) | |
8394 { Expression *e; | |
8395 | |
8396 BinExp::semantic(sc); | |
8397 e2 = resolveProperties(sc, e2); | |
8398 | |
8399 e = op_overload(sc); | |
8400 if (e) | |
8401 return e; | |
8402 | |
8403 e1 = e1->modifiableLvalue(sc, e1); | |
8404 e1->checkScalar(); | |
8405 e1->checkNoBool(); | |
8406 type = e1->type; | |
8407 typeCombine(sc); | |
8408 e1->checkIntegral(); | |
8409 e2 = e2->checkIntegral(); | |
8410 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8411 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8412 return this; |
8413 } | |
8414 | |
8415 /************************************************************/ | |
8416 | |
8417 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8418 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
8419 { | |
8420 } | |
8421 | |
8422 Expression *AndAssignExp::semantic(Scope *sc) | |
8423 { | |
8424 return commonSemanticAssignIntegral(sc); | |
8425 } | |
8426 | |
8427 /************************************************************/ | |
8428 | |
8429 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8430 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
8431 { | |
8432 } | |
8433 | |
8434 Expression *OrAssignExp::semantic(Scope *sc) | |
8435 { | |
8436 return commonSemanticAssignIntegral(sc); | |
8437 } | |
8438 | |
8439 /************************************************************/ | |
8440 | |
8441 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8442 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
8443 { | |
8444 } | |
8445 | |
8446 Expression *XorAssignExp::semantic(Scope *sc) | |
8447 { | |
8448 return commonSemanticAssignIntegral(sc); | |
8449 } | |
8450 | |
8451 /************************* AddExp *****************************/ | |
8452 | |
8453 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
8454 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
8455 { | |
8456 } | |
8457 | |
8458 Expression *AddExp::semantic(Scope *sc) | |
8459 { Expression *e; | |
8460 | |
8461 #if LOGSEMANTIC | |
8462 printf("AddExp::semantic('%s')\n", toChars()); | |
8463 #endif | |
8464 if (!type) | |
8465 { | |
8466 BinExp::semanticp(sc); | |
8467 | |
8468 e = op_overload(sc); | |
8469 if (e) | |
8470 return e; | |
8471 | |
8472 Type *tb1 = e1->type->toBasetype(); | |
8473 Type *tb2 = e2->type->toBasetype(); | |
8474 | |
8475 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
8476 (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
|
8477 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8478 ) |
8479 { | |
8480 type = e1->type; | |
8481 e = this; | |
8482 } | |
8483 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
8484 tb2->ty == Tpointer && e1->type->isintegral()) | |
8485 e = scaleFactor(sc); | |
8486 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
8487 { | |
8488 incompatibleTypes(); | |
8489 type = e1->type; | |
8490 e = this; | |
8491 } | |
8492 else | |
8493 { | |
8494 typeCombine(sc); | |
8495 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
8496 (e1->type->isimaginary() && e2->type->isreal())) | |
8497 { | |
8498 switch (type->toBasetype()->ty) | |
8499 { | |
8500 case Tfloat32: | |
8501 case Timaginary32: | |
8502 type = Type::tcomplex32; | |
8503 break; | |
8504 | |
8505 case Tfloat64: | |
8506 case Timaginary64: | |
8507 type = Type::tcomplex64; | |
8508 break; | |
8509 | |
8510 case Tfloat80: | |
8511 case Timaginary80: | |
8512 type = Type::tcomplex80; | |
8513 break; | |
8514 | |
8515 default: | |
8516 assert(0); | |
8517 } | |
8518 } | |
8519 e = this; | |
8520 } | |
8521 return e; | |
8522 } | |
8523 return this; | |
8524 } | |
8525 | |
8526 /************************************************************/ | |
8527 | |
8528 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8529 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8530 { | |
8531 } | |
8532 | |
8533 Expression *MinExp::semantic(Scope *sc) | |
8534 { Expression *e; | |
8535 Type *t1; | |
8536 Type *t2; | |
8537 | |
8538 #if LOGSEMANTIC | |
8539 printf("MinExp::semantic('%s')\n", toChars()); | |
8540 #endif | |
8541 if (type) | |
8542 return this; | |
8543 | |
8544 BinExp::semanticp(sc); | |
8545 | |
8546 e = op_overload(sc); | |
8547 if (e) | |
8548 return e; | |
8549 | |
8550 e = this; | |
8551 t1 = e1->type->toBasetype(); | |
8552 t2 = e2->type->toBasetype(); | |
8553 if (t1->ty == Tpointer) | |
8554 { | |
8555 if (t2->ty == Tpointer) | |
8556 { // Need to divide the result by the stride | |
8557 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8558 d_int64 stride; | |
8559 Expression *e; | |
8560 | |
8561 typeCombine(sc); // make sure pointer types are compatible | |
8562 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
|
8563 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
|
8564 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
|
8565 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8566 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
|
8567 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8568 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8569 { |
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 = 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
|
8571 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
|
8572 } |
159 | 8573 return e; |
8574 } | |
8575 else if (t2->isintegral()) | |
8576 e = scaleFactor(sc); | |
8577 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
|
8578 { error("incompatible types for minus"); |
159 | 8579 return new IntegerExp(0); |
8580 } | |
8581 } | |
8582 else if (t2->ty == Tpointer) | |
8583 { | |
8584 type = e2->type; | |
8585 error("can't subtract pointer from %s", e1->type->toChars()); | |
8586 return new IntegerExp(0); | |
8587 } | |
8588 else | |
8589 { | |
8590 typeCombine(sc); | |
8591 t1 = e1->type->toBasetype(); | |
8592 t2 = e2->type->toBasetype(); | |
8593 if ((t1->isreal() && t2->isimaginary()) || | |
8594 (t1->isimaginary() && t2->isreal())) | |
8595 { | |
8596 switch (type->ty) | |
8597 { | |
8598 case Tfloat32: | |
8599 case Timaginary32: | |
8600 type = Type::tcomplex32; | |
8601 break; | |
8602 | |
8603 case Tfloat64: | |
8604 case Timaginary64: | |
8605 type = Type::tcomplex64; | |
8606 break; | |
8607 | |
8608 case Tfloat80: | |
8609 case Timaginary80: | |
8610 type = Type::tcomplex80; | |
8611 break; | |
8612 | |
8613 default: | |
8614 assert(0); | |
8615 } | |
8616 } | |
8617 } | |
8618 return e; | |
8619 } | |
8620 | |
8621 /************************* CatExp *****************************/ | |
8622 | |
8623 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
8624 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
8625 { | |
8626 } | |
8627 | |
8628 Expression *CatExp::semantic(Scope *sc) | |
8629 { Expression *e; | |
8630 | |
8631 //printf("CatExp::semantic() %s\n", toChars()); | |
8632 if (!type) | |
8633 { | |
8634 BinExp::semanticp(sc); | |
8635 e = op_overload(sc); | |
8636 if (e) | |
8637 return e; | |
8638 | |
8639 Type *tb1 = e1->type->toBasetype(); | |
8640 Type *tb2 = e2->type->toBasetype(); | |
8641 | |
8642 | |
8643 /* BUG: Should handle things like: | |
8644 * char c; | |
8645 * c ~ ' ' | |
8646 * ' ' ~ c; | |
8647 */ | |
8648 | |
8649 #if 0 | |
8650 e1->type->print(); | |
8651 e2->type->print(); | |
8652 #endif | |
8653 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
8654 e2->type->equals(tb1->next)) | |
8655 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8656 type = tb1->nextOf()->arrayOf(); |
159 | 8657 if (tb2->ty == Tarray) |
8658 { // Make e2 into [e2] | |
8659 e2 = new ArrayLiteralExp(e2->loc, e2); | |
8660 e2->type = type; | |
8661 } | |
8662 return this; | |
8663 } | |
8664 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
8665 e1->type->equals(tb2->next)) | |
8666 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8667 type = tb2->nextOf()->arrayOf(); |
159 | 8668 if (tb1->ty == Tarray) |
8669 { // Make e1 into [e1] | |
8670 e1 = new ArrayLiteralExp(e1->loc, e1); | |
8671 e1->type = type; | |
8672 } | |
8673 return this; | |
8674 } | |
8675 | |
8676 typeCombine(sc); | |
8677 | |
8678 if (type->toBasetype()->ty == Tsarray) | |
8679 type = type->toBasetype()->next->arrayOf(); | |
8680 #if 0 | |
8681 e1->type->print(); | |
8682 e2->type->print(); | |
8683 type->print(); | |
8684 print(); | |
8685 #endif | |
8686 if (e1->op == TOKstring && e2->op == TOKstring) | |
8687 e = optimize(WANTvalue); | |
8688 else if (e1->type->equals(e2->type) && | |
8689 (e1->type->toBasetype()->ty == Tarray || | |
8690 e1->type->toBasetype()->ty == Tsarray)) | |
8691 { | |
8692 e = this; | |
8693 } | |
8694 else | |
8695 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8696 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 8697 error("Can only concatenate arrays, not (%s ~ %s)", |
8698 e1->type->toChars(), e2->type->toChars()); | |
8699 type = Type::tint32; | |
8700 e = this; | |
8701 } | |
8702 e->type = e->type->semantic(loc, sc); | |
8703 return e; | |
8704 } | |
8705 return this; | |
8706 } | |
8707 | |
8708 /************************************************************/ | |
8709 | |
8710 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
8711 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
8712 { | |
8713 } | |
8714 | |
8715 Expression *MulExp::semantic(Scope *sc) | |
8716 { Expression *e; | |
8717 | |
8718 #if 0 | |
8719 printf("MulExp::semantic() %s\n", toChars()); | |
8720 #endif | |
8721 if (type) | |
8722 { | |
8723 return this; | |
8724 } | |
8725 | |
8726 BinExp::semanticp(sc); | |
8727 e = op_overload(sc); | |
8728 if (e) | |
8729 return e; | |
8730 | |
8731 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
|
8732 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
|
8733 { 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
|
8734 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
|
8735 } |
159 | 8736 if (type->isfloating()) |
8737 { Type *t1 = e1->type; | |
8738 Type *t2 = e2->type; | |
8739 | |
8740 if (t1->isreal()) | |
8741 { | |
8742 type = t2; | |
8743 } | |
8744 else if (t2->isreal()) | |
8745 { | |
8746 type = t1; | |
8747 } | |
8748 else if (t1->isimaginary()) | |
8749 { | |
8750 if (t2->isimaginary()) | |
8751 { Expression *e; | |
8752 | |
8753 switch (t1->ty) | |
8754 { | |
8755 case Timaginary32: type = Type::tfloat32; break; | |
8756 case Timaginary64: type = Type::tfloat64; break; | |
8757 case Timaginary80: type = Type::tfloat80; break; | |
8758 default: assert(0); | |
8759 } | |
8760 | |
8761 // iy * iv = -yv | |
8762 e1->type = type; | |
8763 e2->type = type; | |
8764 e = new NegExp(loc, this); | |
8765 e = e->semantic(sc); | |
8766 return e; | |
8767 } | |
8768 else | |
8769 type = t2; // t2 is complex | |
8770 } | |
8771 else if (t2->isimaginary()) | |
8772 { | |
8773 type = t1; // t1 is complex | |
8774 } | |
8775 } | |
8776 return this; | |
8777 } | |
8778 | |
8779 /************************************************************/ | |
8780 | |
8781 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
8782 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
8783 { | |
8784 } | |
8785 | |
8786 Expression *DivExp::semantic(Scope *sc) | |
8787 { Expression *e; | |
8788 | |
8789 if (type) | |
8790 return this; | |
8791 | |
8792 BinExp::semanticp(sc); | |
8793 e = op_overload(sc); | |
8794 if (e) | |
8795 return e; | |
8796 | |
8797 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
|
8798 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
|
8799 { 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
|
8800 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
|
8801 } |
159 | 8802 if (type->isfloating()) |
8803 { Type *t1 = e1->type; | |
8804 Type *t2 = e2->type; | |
8805 | |
8806 if (t1->isreal()) | |
8807 { | |
8808 type = t2; | |
8809 if (t2->isimaginary()) | |
8810 { Expression *e; | |
8811 | |
8812 // x/iv = i(-x/v) | |
8813 e2->type = t1; | |
8814 e = new NegExp(loc, this); | |
8815 e = e->semantic(sc); | |
8816 return e; | |
8817 } | |
8818 } | |
8819 else if (t2->isreal()) | |
8820 { | |
8821 type = t1; | |
8822 } | |
8823 else if (t1->isimaginary()) | |
8824 { | |
8825 if (t2->isimaginary()) | |
8826 { | |
8827 switch (t1->ty) | |
8828 { | |
8829 case Timaginary32: type = Type::tfloat32; break; | |
8830 case Timaginary64: type = Type::tfloat64; break; | |
8831 case Timaginary80: type = Type::tfloat80; break; | |
8832 default: assert(0); | |
8833 } | |
8834 } | |
8835 else | |
8836 type = t2; // t2 is complex | |
8837 } | |
8838 else if (t2->isimaginary()) | |
8839 { | |
8840 type = t1; // t1 is complex | |
8841 } | |
8842 } | |
8843 return this; | |
8844 } | |
8845 | |
8846 /************************************************************/ | |
8847 | |
8848 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
8849 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
8850 { | |
8851 } | |
8852 | |
8853 Expression *ModExp::semantic(Scope *sc) | |
8854 { Expression *e; | |
8855 | |
8856 if (type) | |
8857 return this; | |
8858 | |
8859 BinExp::semanticp(sc); | |
8860 e = op_overload(sc); | |
8861 if (e) | |
8862 return e; | |
8863 | |
8864 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
|
8865 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
|
8866 { 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
|
8867 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
|
8868 } |
159 | 8869 if (type->isfloating()) |
8870 { type = e1->type; | |
8871 if (e2->type->iscomplex()) | |
8872 { error("cannot perform modulo complex arithmetic"); | |
8873 return new IntegerExp(0); | |
8874 } | |
8875 } | |
8876 return this; | |
8877 } | |
8878 | |
8879 /************************************************************/ | |
8880 | |
8881 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
8882 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
8883 { | |
8884 } | |
8885 | |
8886 Expression *ShlExp::semantic(Scope *sc) | |
8887 { Expression *e; | |
8888 | |
8889 //printf("ShlExp::semantic(), type = %p\n", type); | |
8890 if (!type) | |
8891 { BinExp::semanticp(sc); | |
8892 e = op_overload(sc); | |
8893 if (e) | |
8894 return e; | |
8895 e1 = e1->checkIntegral(); | |
8896 e2 = e2->checkIntegral(); | |
8897 e1 = e1->integralPromotions(sc); | |
8898 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8899 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8900 type = e1->type; |
8901 } | |
8902 return this; | |
8903 } | |
8904 | |
8905 /************************************************************/ | |
8906 | |
8907 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
8908 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
8909 { | |
8910 } | |
8911 | |
8912 Expression *ShrExp::semantic(Scope *sc) | |
8913 { Expression *e; | |
8914 | |
8915 if (!type) | |
8916 { BinExp::semanticp(sc); | |
8917 e = op_overload(sc); | |
8918 if (e) | |
8919 return e; | |
8920 e1 = e1->checkIntegral(); | |
8921 e2 = e2->checkIntegral(); | |
8922 e1 = e1->integralPromotions(sc); | |
8923 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8924 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8925 type = e1->type; |
8926 } | |
8927 return this; | |
8928 } | |
8929 | |
8930 /************************************************************/ | |
8931 | |
8932 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
8933 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
8934 { | |
8935 } | |
8936 | |
8937 Expression *UshrExp::semantic(Scope *sc) | |
8938 { Expression *e; | |
8939 | |
8940 if (!type) | |
8941 { BinExp::semanticp(sc); | |
8942 e = op_overload(sc); | |
8943 if (e) | |
8944 return e; | |
8945 e1 = e1->checkIntegral(); | |
8946 e2 = e2->checkIntegral(); | |
8947 e1 = e1->integralPromotions(sc); | |
8948 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8949 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8950 type = e1->type; |
8951 } | |
8952 return this; | |
8953 } | |
8954 | |
8955 /************************************************************/ | |
8956 | |
8957 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8958 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8959 { | |
8960 } | |
8961 | |
8962 Expression *AndExp::semantic(Scope *sc) | |
8963 { Expression *e; | |
8964 | |
8965 if (!type) | |
8966 { BinExp::semanticp(sc); | |
8967 e = op_overload(sc); | |
8968 if (e) | |
8969 return e; | |
8970 if (e1->type->toBasetype()->ty == Tbool && | |
8971 e2->type->toBasetype()->ty == Tbool) | |
8972 { | |
8973 type = e1->type; | |
8974 e = this; | |
8975 } | |
8976 else | |
8977 { | |
8978 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
|
8979 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
|
8980 { 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
|
8981 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
|
8982 } |
159 | 8983 } |
8984 } | |
8985 return this; | |
8986 } | |
8987 | |
8988 /************************************************************/ | |
8989 | |
8990 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8991 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8992 { | |
8993 } | |
8994 | |
8995 Expression *OrExp::semantic(Scope *sc) | |
8996 { Expression *e; | |
8997 | |
8998 if (!type) | |
8999 { BinExp::semanticp(sc); | |
9000 e = op_overload(sc); | |
9001 if (e) | |
9002 return e; | |
9003 if (e1->type->toBasetype()->ty == Tbool && | |
9004 e2->type->toBasetype()->ty == Tbool) | |
9005 { | |
9006 type = e1->type; | |
9007 e = this; | |
9008 } | |
9009 else | |
9010 { | |
9011 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
|
9012 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
|
9013 { 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
|
9014 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
|
9015 } |
159 | 9016 } |
9017 } | |
9018 return this; | |
9019 } | |
9020 | |
9021 /************************************************************/ | |
9022 | |
9023 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
9024 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
9025 { | |
9026 } | |
9027 | |
9028 Expression *XorExp::semantic(Scope *sc) | |
9029 { Expression *e; | |
9030 | |
9031 if (!type) | |
9032 { BinExp::semanticp(sc); | |
9033 e = op_overload(sc); | |
9034 if (e) | |
9035 return e; | |
9036 if (e1->type->toBasetype()->ty == Tbool && | |
9037 e2->type->toBasetype()->ty == Tbool) | |
9038 { | |
9039 type = e1->type; | |
9040 e = this; | |
9041 } | |
9042 else | |
9043 { | |
9044 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
|
9045 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
|
9046 { 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
|
9047 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
|
9048 } |
159 | 9049 } |
9050 } | |
9051 return this; | |
9052 } | |
9053 | |
9054 | |
9055 /************************************************************/ | |
9056 | |
9057 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
9058 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
9059 { | |
9060 } | |
9061 | |
9062 Expression *OrOrExp::semantic(Scope *sc) | |
9063 { | |
9064 unsigned cs1; | |
9065 | |
9066 // same as for AndAnd | |
9067 e1 = e1->semantic(sc); | |
9068 e1 = resolveProperties(sc, e1); | |
9069 e1 = e1->checkToPointer(); | |
9070 e1 = e1->checkToBoolean(); | |
9071 cs1 = sc->callSuper; | |
9072 | |
9073 if (sc->flags & SCOPEstaticif) | |
9074 { | |
9075 /* If in static if, don't evaluate e2 if we don't have to. | |
9076 */ | |
9077 e1 = e1->optimize(WANTflags); | |
9078 if (e1->isBool(TRUE)) | |
9079 { | |
9080 return new IntegerExp(loc, 1, Type::tboolean); | |
9081 } | |
9082 } | |
9083 | |
9084 e2 = e2->semantic(sc); | |
9085 sc->mergeCallSuper(loc, cs1); | |
9086 e2 = resolveProperties(sc, e2); | |
9087 e2 = e2->checkToPointer(); | |
9088 | |
9089 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
|
9090 if (e2->type->ty == Tvoid) |
159 | 9091 type = Type::tvoid; |
9092 if (e2->op == TOKtype || e2->op == TOKimport) | |
9093 error("%s is not an expression", e2->toChars()); | |
9094 return this; | |
9095 } | |
9096 | |
9097 Expression *OrOrExp::checkToBoolean() | |
9098 { | |
9099 e2 = e2->checkToBoolean(); | |
9100 return this; | |
9101 } | |
9102 | |
9103 int OrOrExp::isBit() | |
9104 { | |
9105 return TRUE; | |
9106 } | |
9107 | |
9108 int OrOrExp::checkSideEffect(int flag) | |
9109 { | |
9110 if (flag == 2) | |
9111 { | |
9112 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9113 } | |
9114 else | |
9115 { e1->checkSideEffect(1); | |
9116 return e2->checkSideEffect(flag); | |
9117 } | |
9118 } | |
9119 | |
9120 /************************************************************/ | |
9121 | |
9122 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
9123 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
9124 { | |
9125 } | |
9126 | |
9127 Expression *AndAndExp::semantic(Scope *sc) | |
9128 { | |
9129 unsigned cs1; | |
9130 | |
9131 // same as for OrOr | |
9132 e1 = e1->semantic(sc); | |
9133 e1 = resolveProperties(sc, e1); | |
9134 e1 = e1->checkToPointer(); | |
9135 e1 = e1->checkToBoolean(); | |
9136 cs1 = sc->callSuper; | |
9137 | |
9138 if (sc->flags & SCOPEstaticif) | |
9139 { | |
9140 /* If in static if, don't evaluate e2 if we don't have to. | |
9141 */ | |
9142 e1 = e1->optimize(WANTflags); | |
9143 if (e1->isBool(FALSE)) | |
9144 { | |
9145 return new IntegerExp(loc, 0, Type::tboolean); | |
9146 } | |
9147 } | |
9148 | |
9149 e2 = e2->semantic(sc); | |
9150 sc->mergeCallSuper(loc, cs1); | |
9151 e2 = resolveProperties(sc, e2); | |
9152 e2 = e2->checkToPointer(); | |
9153 | |
9154 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
|
9155 if (e2->type->ty == Tvoid) |
159 | 9156 type = Type::tvoid; |
9157 if (e2->op == TOKtype || e2->op == TOKimport) | |
9158 error("%s is not an expression", e2->toChars()); | |
9159 return this; | |
9160 } | |
9161 | |
9162 Expression *AndAndExp::checkToBoolean() | |
9163 { | |
9164 e2 = e2->checkToBoolean(); | |
9165 return this; | |
9166 } | |
9167 | |
9168 int AndAndExp::isBit() | |
9169 { | |
9170 return TRUE; | |
9171 } | |
9172 | |
9173 int AndAndExp::checkSideEffect(int flag) | |
9174 { | |
9175 if (flag == 2) | |
9176 { | |
9177 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9178 } | |
9179 else | |
9180 { | |
9181 e1->checkSideEffect(1); | |
9182 return e2->checkSideEffect(flag); | |
9183 } | |
9184 } | |
9185 | |
9186 /************************************************************/ | |
9187 | |
9188 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
9189 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
9190 { | |
9191 } | |
9192 | |
9193 Expression *InExp::semantic(Scope *sc) | |
9194 { Expression *e; | |
9195 | |
9196 if (type) | |
9197 return this; | |
9198 | |
9199 BinExp::semanticp(sc); | |
9200 e = op_overload(sc); | |
9201 if (e) | |
9202 return e; | |
9203 | |
9204 //type = Type::tboolean; | |
9205 Type *t2b = e2->type->toBasetype(); | |
9206 if (t2b->ty != Taarray) | |
9207 { | |
9208 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
9209 type = Type::terror; | |
9210 } | |
9211 else | |
9212 { | |
9213 TypeAArray *ta = (TypeAArray *)t2b; | |
9214 | |
9215 // Convert key to type of key | |
9216 e1 = e1->implicitCastTo(sc, ta->index); | |
9217 | |
9218 // 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
|
9219 type = ta->nextOf()->pointerTo(); |
159 | 9220 } |
9221 return this; | |
9222 } | |
9223 | |
9224 int InExp::isBit() | |
9225 { | |
9226 return FALSE; | |
9227 } | |
9228 | |
9229 | |
9230 /************************************************************/ | |
9231 | |
9232 /* This deletes the key e1 from the associative array e2 | |
9233 */ | |
9234 | |
9235 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
9236 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
9237 { | |
9238 type = Type::tvoid; | |
9239 } | |
9240 | |
9241 /************************************************************/ | |
9242 | |
9243 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9244 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
9245 { | |
9246 } | |
9247 | |
9248 Expression *CmpExp::semantic(Scope *sc) | |
9249 { Expression *e; | |
9250 Type *t1; | |
9251 Type *t2; | |
9252 | |
9253 #if LOGSEMANTIC | |
9254 printf("CmpExp::semantic('%s')\n", toChars()); | |
9255 #endif | |
9256 if (type) | |
9257 return this; | |
9258 | |
9259 BinExp::semanticp(sc); | |
9260 | |
9261 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9262 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9263 { | |
9264 error("do not use null when comparing class types"); | |
9265 } | |
9266 | |
9267 e = op_overload(sc); | |
9268 if (e) | |
9269 { | |
9270 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
9271 e = e->semantic(sc); | |
9272 return e; | |
9273 } | |
9274 | |
9275 typeCombine(sc); | |
9276 type = Type::tboolean; | |
9277 | |
9278 // Special handling for array comparisons | |
9279 t1 = e1->type->toBasetype(); | |
9280 t2 = e2->type->toBasetype(); | |
9281 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9282 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9283 { | |
9284 if (!t1->next->equals(t2->next)) | |
9285 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9286 e = this; | |
9287 } | |
9288 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
9289 (t1->ty == Tclass && t2->ty == Tclass)) | |
9290 { | |
9291 if (t2->ty == Tstruct) | |
9292 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
9293 else | |
9294 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
9295 e = this; | |
9296 } | |
9297 #if 1 | |
9298 else if (t1->iscomplex() || t2->iscomplex()) | |
9299 { | |
9300 error("compare not defined for complex operands"); | |
9301 e = new IntegerExp(0); | |
9302 } | |
9303 #endif | |
9304 else | |
9305 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
|
9306 //printf("CmpExp: %s\n", e->toChars()); |
159 | 9307 return e; |
9308 } | |
9309 | |
9310 int CmpExp::isBit() | |
9311 { | |
9312 return TRUE; | |
9313 } | |
9314 | |
9315 | |
9316 /************************************************************/ | |
9317 | |
9318 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9319 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
9320 { | |
9321 assert(op == TOKequal || op == TOKnotequal); | |
9322 } | |
9323 | |
9324 Expression *EqualExp::semantic(Scope *sc) | |
9325 { Expression *e; | |
9326 Type *t1; | |
9327 Type *t2; | |
9328 | |
9329 //printf("EqualExp::semantic('%s')\n", toChars()); | |
9330 if (type) | |
9331 return this; | |
9332 | |
9333 BinExp::semanticp(sc); | |
9334 | |
9335 /* Before checking for operator overloading, check to see if we're | |
9336 * comparing the addresses of two statics. If so, we can just see | |
9337 * if they are the same symbol. | |
9338 */ | |
9339 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
9340 { AddrExp *ae1 = (AddrExp *)e1; | |
9341 AddrExp *ae2 = (AddrExp *)e2; | |
9342 | |
9343 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
9344 { VarExp *ve1 = (VarExp *)ae1->e1; | |
9345 VarExp *ve2 = (VarExp *)ae2->e1; | |
9346 | |
9347 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
9348 { | |
9349 // They are the same, result is 'true' for ==, 'false' for != | |
9350 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
9351 return e; | |
9352 } | |
9353 } | |
9354 } | |
9355 | |
9356 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9357 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9358 { | |
9359 error("use '%s' instead of '%s' when comparing with null", | |
9360 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
9361 Token::toChars(op)); | |
9362 } | |
9363 | |
9364 //if (e2->op != TOKnull) | |
9365 { | |
9366 e = op_overload(sc); | |
9367 if (e) | |
9368 { | |
9369 if (op == TOKnotequal) | |
9370 { | |
9371 e = new NotExp(e->loc, e); | |
9372 e = e->semantic(sc); | |
9373 } | |
9374 return e; | |
9375 } | |
9376 } | |
9377 | |
9378 e = typeCombine(sc); | |
9379 type = Type::tboolean; | |
9380 | |
9381 // Special handling for array comparisons | |
9382 t1 = e1->type->toBasetype(); | |
9383 t2 = e2->type->toBasetype(); | |
9384 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9385 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9386 { | |
9387 if (!t1->next->equals(t2->next)) | |
9388 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9389 } | |
9390 else | |
9391 { | |
9392 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9393 { | |
9394 // Cast both to complex | |
9395 e1 = e1->castTo(sc, Type::tcomplex80); | |
9396 e2 = e2->castTo(sc, Type::tcomplex80); | |
9397 } | |
9398 } | |
9399 return e; | |
9400 } | |
9401 | |
9402 int EqualExp::isBit() | |
9403 { | |
9404 return TRUE; | |
9405 } | |
9406 | |
9407 | |
9408 | |
9409 /************************************************************/ | |
9410 | |
9411 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9412 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
9413 { | |
9414 } | |
9415 | |
9416 Expression *IdentityExp::semantic(Scope *sc) | |
9417 { | |
9418 if (type) | |
9419 return this; | |
9420 | |
9421 BinExp::semanticp(sc); | |
9422 type = Type::tboolean; | |
9423 typeCombine(sc); | |
9424 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9425 { | |
9426 // Cast both to complex | |
9427 e1 = e1->castTo(sc, Type::tcomplex80); | |
9428 e2 = e2->castTo(sc, Type::tcomplex80); | |
9429 } | |
9430 return this; | |
9431 } | |
9432 | |
9433 int IdentityExp::isBit() | |
9434 { | |
9435 return TRUE; | |
9436 } | |
9437 | |
9438 | |
9439 /****************************************************************/ | |
9440 | |
9441 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
9442 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
9443 { | |
9444 this->econd = econd; | |
9445 } | |
9446 | |
9447 Expression *CondExp::syntaxCopy() | |
9448 { | |
9449 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
9450 } | |
9451 | |
9452 | |
9453 Expression *CondExp::semantic(Scope *sc) | |
9454 { Type *t1; | |
9455 Type *t2; | |
9456 unsigned cs0; | |
9457 unsigned cs1; | |
9458 | |
9459 #if LOGSEMANTIC | |
9460 printf("CondExp::semantic('%s')\n", toChars()); | |
9461 #endif | |
9462 if (type) | |
9463 return this; | |
9464 | |
9465 econd = econd->semantic(sc); | |
9466 econd = resolveProperties(sc, econd); | |
9467 econd = econd->checkToPointer(); | |
9468 econd = econd->checkToBoolean(); | |
9469 | |
9470 #if 0 /* this cannot work right because the types of e1 and e2 | |
9471 * both contribute to the type of the result. | |
9472 */ | |
9473 if (sc->flags & SCOPEstaticif) | |
9474 { | |
9475 /* If in static if, don't evaluate what we don't have to. | |
9476 */ | |
9477 econd = econd->optimize(WANTflags); | |
9478 if (econd->isBool(TRUE)) | |
9479 { | |
9480 e1 = e1->semantic(sc); | |
9481 e1 = resolveProperties(sc, e1); | |
9482 return e1; | |
9483 } | |
9484 else if (econd->isBool(FALSE)) | |
9485 { | |
9486 e2 = e2->semantic(sc); | |
9487 e2 = resolveProperties(sc, e2); | |
9488 return e2; | |
9489 } | |
9490 } | |
9491 #endif | |
9492 | |
9493 | |
9494 cs0 = sc->callSuper; | |
9495 e1 = e1->semantic(sc); | |
9496 e1 = resolveProperties(sc, e1); | |
9497 cs1 = sc->callSuper; | |
9498 sc->callSuper = cs0; | |
9499 e2 = e2->semantic(sc); | |
9500 e2 = resolveProperties(sc, e2); | |
9501 sc->mergeCallSuper(loc, cs1); | |
9502 | |
9503 | |
9504 // If either operand is void, the result is void | |
9505 t1 = e1->type; | |
9506 t2 = e2->type; | |
9507 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
9508 type = Type::tvoid; | |
9509 else if (t1 == t2) | |
9510 type = t1; | |
9511 else | |
9512 { | |
9513 typeCombine(sc); | |
9514 switch (e1->type->toBasetype()->ty) | |
9515 { | |
9516 case Tcomplex32: | |
9517 case Tcomplex64: | |
9518 case Tcomplex80: | |
9519 e2 = e2->castTo(sc, e1->type); | |
9520 break; | |
9521 } | |
9522 switch (e2->type->toBasetype()->ty) | |
9523 { | |
9524 case Tcomplex32: | |
9525 case Tcomplex64: | |
9526 case Tcomplex80: | |
9527 e1 = e1->castTo(sc, e2->type); | |
9528 break; | |
9529 } | |
9530 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9531 #if 0 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9532 printf("res: %s\n", type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9533 printf("e1 : %s\n", e1->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9534 printf("e2 : %s\n", e2->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9535 #endif |
159 | 9536 return this; |
9537 } | |
9538 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9539 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9540 int CondExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9541 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9542 return e1->isLvalue() && e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9543 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9544 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9545 |
159 | 9546 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) |
9547 { | |
9548 PtrExp *e; | |
9549 | |
9550 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
9551 e = new PtrExp(loc, this, type); | |
9552 | |
9553 e1 = e1->addressOf(sc); | |
9554 //e1 = e1->toLvalue(sc, NULL); | |
9555 | |
9556 e2 = e2->addressOf(sc); | |
9557 //e2 = e2->toLvalue(sc, NULL); | |
9558 | |
9559 typeCombine(sc); | |
9560 | |
9561 type = e2->type; | |
9562 return e; | |
9563 } | |
9564 | |
9565 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
9566 { | |
9567 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
9568 return this; | |
9569 } | |
9570 | |
9571 void CondExp::checkEscape() | |
9572 { | |
9573 e1->checkEscape(); | |
9574 e2->checkEscape(); | |
9575 } | |
9576 | |
9577 | |
9578 Expression *CondExp::checkToBoolean() | |
9579 { | |
9580 e1 = e1->checkToBoolean(); | |
9581 e2 = e2->checkToBoolean(); | |
9582 return this; | |
9583 } | |
9584 | |
9585 int CondExp::checkSideEffect(int flag) | |
9586 { | |
9587 if (flag == 2) | |
9588 { | |
9589 return econd->checkSideEffect(2) || | |
9590 e1->checkSideEffect(2) || | |
9591 e2->checkSideEffect(2); | |
9592 } | |
9593 else | |
9594 { | |
9595 econd->checkSideEffect(1); | |
9596 e1->checkSideEffect(flag); | |
9597 return e2->checkSideEffect(flag); | |
9598 } | |
9599 } | |
9600 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9601 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9602 int CondExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9603 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9604 return econd->canThrow() || e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9605 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9606 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9607 |
159 | 9608 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
9609 { | |
9610 expToCBuffer(buf, hgs, econd, PREC_oror); | |
9611 buf->writestring(" ? "); | |
9612 expToCBuffer(buf, hgs, e1, PREC_expr); | |
9613 buf->writestring(" : "); | |
9614 expToCBuffer(buf, hgs, e2, PREC_cond); | |
9615 } | |
9616 | |
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
|
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 |
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 #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
|
9620 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9621 // 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
|
9622 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9623 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
|
9624 : 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
|
9625 { |
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 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
|
9627 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
|
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 |
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 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
|
9631 { |
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 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
|
9633 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
|
9634 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
|
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 |
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 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
|
9638 { |
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 // 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
|
9640 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
|
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 |
4435f57956e7
Fixed .funcptr property of delegates, no longer uses the infamous DMD rewrites to pointer arithmetic, instead a GEPExp has been introduced.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
585
diff
changeset
|
9643 #endif |