Mercurial > projects > ldc
annotate dmd/expression.c @ 875:330f999ade44
Merged DMD 1.038
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Tue, 06 Jan 2009 16:33:51 +0100 |
parents | bc982f1ad106 |
children | 27a379f288bf |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
3 // Copyright (c) 1999-2008 by Digital Mars | |
4 // All Rights Reserved | |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <ctype.h> | |
14 #include <assert.h> | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
15 #if _MSC_VER |
159 | 16 #include <complex> |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
17 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
18 #endif |
159 | 19 #include <math.h> |
20 | |
21 #if _WIN32 && __DMC__ | |
22 extern "C" char * __cdecl __locale_decpoint; | |
23 #endif | |
24 | |
285
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
25 #if __MINGW32__ |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
26 #ifndef isnan |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
27 #define isnan _isnan |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
28 #endif |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
29 #endif |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
30 |
536
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
31 #ifdef __APPLE__ |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
32 #ifndef isnan |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
33 int isnan(double); |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
34 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
35 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
36 |
159 | 37 #if IN_GCC |
38 // Issues with using -include total.h (defines integer_t) and then complex.h fails... | |
39 #undef integer_t | |
40 #endif | |
41 | |
42 #ifdef __APPLE__ | |
43 #define integer_t dmd_integer_t | |
44 #endif | |
45 | |
46 #if IN_GCC || IN_LLVM | |
47 #include "mem.h" | |
48 #elif _WIN32 | |
49 #include "..\root\mem.h" | |
571
cbd6c8073a32
Changed all '#if linux || __APPLE__' to '#if POSIX' so we can support other platforms too, thanx for the suggestion anders.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
536
diff
changeset
|
50 #elif POSIX |
159 | 51 #include "../root/mem.h" |
52 #endif | |
53 | |
54 //#include "port.h" | |
55 #include "mtype.h" | |
56 #include "init.h" | |
57 #include "expression.h" | |
58 #include "template.h" | |
59 #include "utf.h" | |
60 #include "enum.h" | |
61 #include "scope.h" | |
62 #include "statement.h" | |
63 #include "declaration.h" | |
64 #include "aggregate.h" | |
65 #include "import.h" | |
66 #include "id.h" | |
67 #include "dsymbol.h" | |
68 #include "module.h" | |
69 #include "attrib.h" | |
70 #include "hdrgen.h" | |
71 #include "parse.h" | |
72 | |
73 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
|
74 Expression *expandVar(int result, VarDeclaration *v); |
159 | 75 |
76 #define LOGSEMANTIC 0 | |
77 | |
78 /********************************** | |
79 * Set operator precedence for each operator. | |
80 */ | |
81 | |
82 // Operator precedence - greater values are higher precedence | |
83 | |
84 enum PREC | |
85 { | |
86 PREC_zero, | |
87 PREC_expr, | |
88 PREC_assign, | |
89 PREC_cond, | |
90 PREC_oror, | |
91 PREC_andand, | |
92 PREC_or, | |
93 PREC_xor, | |
94 PREC_and, | |
95 PREC_equal, | |
96 PREC_rel, | |
97 PREC_shift, | |
98 PREC_add, | |
99 PREC_mul, | |
100 PREC_unary, | |
101 PREC_primary, | |
102 }; | |
103 | |
104 enum PREC precedence[TOKMAX]; | |
105 | |
106 void initPrecedence() | |
107 { | |
108 precedence[TOKdotvar] = PREC_primary; | |
109 precedence[TOKimport] = PREC_primary; | |
110 precedence[TOKidentifier] = PREC_primary; | |
111 precedence[TOKthis] = PREC_primary; | |
112 precedence[TOKsuper] = PREC_primary; | |
113 precedence[TOKint64] = PREC_primary; | |
114 precedence[TOKfloat64] = PREC_primary; | |
115 precedence[TOKnull] = PREC_primary; | |
116 precedence[TOKstring] = PREC_primary; | |
117 precedence[TOKarrayliteral] = PREC_primary; | |
118 precedence[TOKtypedot] = PREC_primary; | |
119 precedence[TOKtypeid] = PREC_primary; | |
120 precedence[TOKis] = PREC_primary; | |
121 precedence[TOKassert] = PREC_primary; | |
122 precedence[TOKfunction] = PREC_primary; | |
123 precedence[TOKvar] = PREC_primary; | |
336 | 124 #if DMDV2 |
125 precedence[TOKdefault] = PREC_primary; | |
126 #endif | |
159 | 127 |
128 // post | |
129 precedence[TOKdotti] = PREC_primary; | |
130 precedence[TOKdot] = PREC_primary; | |
131 // precedence[TOKarrow] = PREC_primary; | |
132 precedence[TOKplusplus] = PREC_primary; | |
133 precedence[TOKminusminus] = PREC_primary; | |
134 precedence[TOKcall] = PREC_primary; | |
135 precedence[TOKslice] = PREC_primary; | |
136 precedence[TOKarray] = PREC_primary; | |
137 | |
138 precedence[TOKaddress] = PREC_unary; | |
139 precedence[TOKstar] = PREC_unary; | |
140 precedence[TOKneg] = PREC_unary; | |
141 precedence[TOKuadd] = PREC_unary; | |
142 precedence[TOKnot] = PREC_unary; | |
143 precedence[TOKtobool] = PREC_add; | |
144 precedence[TOKtilde] = PREC_unary; | |
145 precedence[TOKdelete] = PREC_unary; | |
146 precedence[TOKnew] = PREC_unary; | |
147 precedence[TOKcast] = PREC_unary; | |
148 | |
149 precedence[TOKmul] = PREC_mul; | |
150 precedence[TOKdiv] = PREC_mul; | |
151 precedence[TOKmod] = PREC_mul; | |
152 | |
153 precedence[TOKadd] = PREC_add; | |
154 precedence[TOKmin] = PREC_add; | |
155 precedence[TOKcat] = PREC_add; | |
156 | |
157 precedence[TOKshl] = PREC_shift; | |
158 precedence[TOKshr] = PREC_shift; | |
159 precedence[TOKushr] = PREC_shift; | |
160 | |
161 precedence[TOKlt] = PREC_rel; | |
162 precedence[TOKle] = PREC_rel; | |
163 precedence[TOKgt] = PREC_rel; | |
164 precedence[TOKge] = PREC_rel; | |
165 precedence[TOKunord] = PREC_rel; | |
166 precedence[TOKlg] = PREC_rel; | |
167 precedence[TOKleg] = PREC_rel; | |
168 precedence[TOKule] = PREC_rel; | |
169 precedence[TOKul] = PREC_rel; | |
170 precedence[TOKuge] = PREC_rel; | |
171 precedence[TOKug] = PREC_rel; | |
172 precedence[TOKue] = PREC_rel; | |
173 precedence[TOKin] = PREC_rel; | |
174 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
175 #if 0 |
159 | 176 precedence[TOKequal] = PREC_equal; |
177 precedence[TOKnotequal] = PREC_equal; | |
178 precedence[TOKidentity] = PREC_equal; | |
179 precedence[TOKnotidentity] = PREC_equal; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
180 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
181 /* 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
|
182 * 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
|
183 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
184 precedence[TOKequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
185 precedence[TOKnotequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
186 precedence[TOKidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
187 precedence[TOKnotidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
188 #endif |
159 | 189 |
190 precedence[TOKand] = PREC_and; | |
191 | |
192 precedence[TOKxor] = PREC_xor; | |
193 | |
194 precedence[TOKor] = PREC_or; | |
195 | |
196 precedence[TOKandand] = PREC_andand; | |
197 | |
198 precedence[TOKoror] = PREC_oror; | |
199 | |
200 precedence[TOKquestion] = PREC_cond; | |
201 | |
202 precedence[TOKassign] = PREC_assign; | |
203 precedence[TOKconstruct] = PREC_assign; | |
204 precedence[TOKblit] = PREC_assign; | |
205 precedence[TOKaddass] = PREC_assign; | |
206 precedence[TOKminass] = PREC_assign; | |
207 precedence[TOKcatass] = PREC_assign; | |
208 precedence[TOKmulass] = PREC_assign; | |
209 precedence[TOKdivass] = PREC_assign; | |
210 precedence[TOKmodass] = PREC_assign; | |
211 precedence[TOKshlass] = PREC_assign; | |
212 precedence[TOKshrass] = PREC_assign; | |
213 precedence[TOKushrass] = PREC_assign; | |
214 precedence[TOKandass] = PREC_assign; | |
215 precedence[TOKorass] = PREC_assign; | |
216 precedence[TOKxorass] = PREC_assign; | |
217 | |
218 precedence[TOKcomma] = PREC_expr; | |
219 } | |
220 | |
336 | 221 /************************************************************* |
222 * Given var, we need to get the | |
223 * right 'this' pointer if var is in an outer class, but our | |
224 * existing 'this' pointer is in an inner class. | |
225 * Input: | |
226 * e1 existing 'this' | |
227 * ad struct or class we need the correct 'this' for | |
228 * var the specific member of ad we're accessing | |
229 */ | |
230 | |
231 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, | |
232 Expression *e1, Declaration *var) | |
233 { | |
234 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars()); | |
235 L1: | |
236 Type *t = e1->type->toBasetype(); | |
237 //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars()); | |
238 | |
239 /* If e1 is not the 'this' pointer for ad | |
240 */ | |
241 if (ad && | |
242 !(t->ty == Tpointer && t->nextOf()->ty == Tstruct && | |
243 ((TypeStruct *)t->nextOf())->sym == ad) | |
244 && | |
245 !(t->ty == Tstruct && | |
246 ((TypeStruct *)t)->sym == ad) | |
247 ) | |
248 { | |
249 ClassDeclaration *cd = ad->isClassDeclaration(); | |
250 ClassDeclaration *tcd = t->isClassHandle(); | |
251 | |
252 /* e1 is the right this if ad is a base class of e1 | |
253 */ | |
254 if (!cd || !tcd || | |
255 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
256 ) | |
257 { | |
258 /* Only classes can be inner classes with an 'outer' | |
259 * member pointing to the enclosing class instance | |
260 */ | |
261 if (tcd && tcd->isNested()) | |
262 { /* e1 is the 'this' pointer for an inner class: tcd. | |
263 * Rewrite it as the 'this' pointer for the outer class. | |
264 */ | |
265 | |
266 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
267 e1->type = tcd->vthis->type; | |
268 // Do not call checkNestedRef() | |
269 //e1 = e1->semantic(sc); | |
270 | |
271 // Skip up over nested functions, and get the enclosing | |
272 // class type. | |
273 int n = 0; | |
274 Dsymbol *s; | |
275 for (s = tcd->toParent(); | |
276 s && s->isFuncDeclaration(); | |
277 s = s->toParent()) | |
278 { FuncDeclaration *f = s->isFuncDeclaration(); | |
279 if (f->vthis) | |
280 { | |
281 //printf("rewriting e1 to %s's this\n", f->toChars()); | |
282 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
|
283 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
284 // 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
|
285 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
|
286 |
336 | 287 e1 = new VarExp(loc, f->vthis); |
288 } | |
289 } | |
290 if (s && s->isClassDeclaration()) | |
291 { e1->type = s->isClassDeclaration()->type; | |
292 if (n > 1) | |
293 e1 = e1->semantic(sc); | |
294 } | |
295 else | |
296 e1 = e1->semantic(sc); | |
297 goto L1; | |
298 } | |
299 /* Can't find a path from e1 to ad | |
300 */ | |
301 e1->error("this for %s needs to be type %s not type %s", | |
302 var->toChars(), ad->toChars(), t->toChars()); | |
303 } | |
304 } | |
305 return e1; | |
306 } | |
307 | |
159 | 308 /***************************************** |
309 * Determine if 'this' is available. | |
310 * If it is, return the FuncDeclaration that has it. | |
311 */ | |
312 | |
313 FuncDeclaration *hasThis(Scope *sc) | |
314 { FuncDeclaration *fd; | |
315 FuncDeclaration *fdthis; | |
316 | |
317 //printf("hasThis()\n"); | |
318 fdthis = sc->parent->isFuncDeclaration(); | |
319 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
320 | |
321 // Go upwards until we find the enclosing member function | |
322 fd = fdthis; | |
323 while (1) | |
324 { | |
325 if (!fd) | |
326 { | |
327 goto Lno; | |
328 } | |
329 if (!fd->isNested()) | |
330 break; | |
331 | |
332 Dsymbol *parent = fd->parent; | |
333 while (parent) | |
334 { | |
335 TemplateInstance *ti = parent->isTemplateInstance(); | |
336 if (ti) | |
337 parent = ti->parent; | |
338 else | |
339 break; | |
340 } | |
341 | |
342 fd = fd->parent->isFuncDeclaration(); | |
343 } | |
344 | |
345 if (!fd->isThis()) | |
346 { //printf("test '%s'\n", fd->toChars()); | |
347 goto Lno; | |
348 } | |
349 | |
350 assert(fd->vthis); | |
351 return fd; | |
352 | |
353 Lno: | |
354 return NULL; // don't have 'this' available | |
355 } | |
356 | |
357 | |
358 /*************************************** | |
359 * Pull out any properties. | |
360 */ | |
361 | |
362 Expression *resolveProperties(Scope *sc, Expression *e) | |
363 { | |
364 //printf("resolveProperties(%s)\n", e->toChars()); | |
365 if (e->type) | |
366 { | |
367 Type *t = e->type->toBasetype(); | |
368 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
369 if (t->ty == Tfunction /*|| e->op == TOKoverloadset*/) |
159 | 370 { |
371 e = new CallExp(e->loc, e); | |
372 e = e->semantic(sc); | |
373 } | |
374 | |
375 /* Look for e being a lazy parameter; rewrite as delegate call | |
376 */ | |
377 else if (e->op == TOKvar) | |
378 { VarExp *ve = (VarExp *)e; | |
379 | |
380 if (ve->var->storage_class & STClazy) | |
381 { | |
382 e = new CallExp(e->loc, e); | |
383 e = e->semantic(sc); | |
384 } | |
385 } | |
386 | |
387 else if (e->op == TOKdotexp) | |
388 { | |
389 e->error("expression has no value"); | |
390 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
391 |
159 | 392 } |
393 return e; | |
394 } | |
395 | |
396 /****************************** | |
397 * Perform semantic() on an array of Expressions. | |
398 */ | |
399 | |
400 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
401 { | |
402 if (exps) | |
403 { | |
404 for (size_t i = 0; i < exps->dim; i++) | |
405 { Expression *e = (Expression *)exps->data[i]; | |
406 | |
407 e = e->semantic(sc); | |
408 exps->data[i] = (void *)e; | |
409 } | |
410 } | |
411 } | |
412 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
413 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
414 /****************************** |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
415 * 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
|
416 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
417 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
418 #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
|
419 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
|
420 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
421 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
|
422 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
423 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
|
424 { 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
|
425 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
|
426 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
|
427 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
428 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
429 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
|
430 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
431 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
432 |
159 | 433 /**************************************** |
434 * Expand tuples. | |
435 */ | |
436 | |
437 void expandTuples(Expressions *exps) | |
438 { | |
439 //printf("expandTuples()\n"); | |
440 if (exps) | |
441 { | |
442 for (size_t i = 0; i < exps->dim; i++) | |
443 { Expression *arg = (Expression *)exps->data[i]; | |
444 if (!arg) | |
445 continue; | |
446 | |
447 // Look for tuple with 0 members | |
448 if (arg->op == TOKtype) | |
449 { TypeExp *e = (TypeExp *)arg; | |
450 if (e->type->toBasetype()->ty == Ttuple) | |
451 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
452 | |
453 if (!tt->arguments || tt->arguments->dim == 0) | |
454 { | |
455 exps->remove(i); | |
456 if (i == exps->dim) | |
457 return; | |
458 i--; | |
459 continue; | |
460 } | |
461 } | |
462 } | |
463 | |
464 // Inline expand all the tuples | |
465 while (arg->op == TOKtuple) | |
466 { TupleExp *te = (TupleExp *)arg; | |
467 | |
468 exps->remove(i); // remove arg | |
469 exps->insert(i, te->exps); // replace with tuple contents | |
470 if (i == exps->dim) | |
471 return; // empty tuple, no more arguments | |
472 arg = (Expression *)exps->data[i]; | |
473 } | |
474 } | |
475 } | |
476 } | |
477 | |
478 /**************************************** | |
479 * Preprocess arguments to function. | |
480 */ | |
481 | |
482 void preFunctionArguments(Loc loc, Scope *sc, Expressions *exps) | |
483 { | |
484 if (exps) | |
485 { | |
486 expandTuples(exps); | |
487 | |
488 for (size_t i = 0; i < exps->dim; i++) | |
489 { Expression *arg = (Expression *)exps->data[i]; | |
490 | |
491 if (!arg->type) | |
492 { | |
493 #ifdef DEBUG | |
494 if (!global.gag) | |
495 printf("1: \n"); | |
496 #endif | |
497 arg->error("%s is not an expression", arg->toChars()); | |
498 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
499 } | |
500 | |
501 arg = resolveProperties(sc, arg); | |
502 exps->data[i] = (void *) arg; | |
503 | |
504 //arg->rvalue(); | |
505 #if 0 | |
506 if (arg->type->ty == Tfunction) | |
507 { | |
508 arg = new AddrExp(arg->loc, arg); | |
509 arg = arg->semantic(sc); | |
510 exps->data[i] = (void *) arg; | |
511 } | |
512 #endif | |
513 } | |
514 } | |
515 } | |
516 | |
336 | 517 /********************************************* |
518 * Call copy constructor for struct value argument. | |
519 */ | |
520 #if DMDV2 | |
521 Expression *callCpCtor(Loc loc, Scope *sc, Expression *e) | |
522 { | |
523 Type *tb = e->type->toBasetype(); | |
524 assert(tb->ty == Tstruct); | |
525 StructDeclaration *sd = ((TypeStruct *)tb)->sym; | |
526 if (sd->cpctor) | |
527 { | |
528 /* Create a variable tmp, and replace the argument e with: | |
529 * (tmp = e),tmp | |
530 * and let AssignExp() handle the construction. | |
531 * This is not the most efficent, ideally tmp would be constructed | |
532 * directly onto the stack. | |
533 */ | |
534 Identifier *idtmp = Lexer::uniqueId("__tmp"); | |
535 VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e)); | |
536 Expression *ae = new DeclarationExp(loc, tmp); | |
537 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
538 e = e->semantic(sc); | |
539 } | |
540 return e; | |
541 } | |
542 #endif | |
159 | 543 |
544 /**************************************** | |
545 * Now that we know the exact type of the function we're calling, | |
546 * 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
|
547 * 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
|
548 * 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
|
549 * 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
|
550 * 4. add hidden _arguments[] argument |
159 | 551 */ |
552 | |
553 void functionArguments(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) | |
554 { | |
555 unsigned n; | |
556 | |
557 //printf("functionArguments()\n"); | |
558 assert(arguments); | |
559 size_t nargs = arguments ? arguments->dim : 0; | |
560 size_t nparams = Argument::dim(tf->parameters); | |
561 | |
562 if (nargs > nparams && tf->varargs == 0) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
563 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs); |
159 | 564 |
565 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
566 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
567 int done = 0; |
159 | 568 for (size_t i = 0; i < n; i++) |
569 { | |
570 Expression *arg; | |
571 | |
572 if (i < nargs) | |
573 arg = (Expression *)arguments->data[i]; | |
574 else | |
575 arg = NULL; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
576 Type *tb; |
159 | 577 |
578 if (i < nparams) | |
579 { | |
580 Argument *p = Argument::getNth(tf->parameters, i); | |
581 | |
582 if (!arg) | |
583 { | |
584 if (!p->defaultArg) | |
585 { | |
586 if (tf->varargs == 2 && i + 1 == nparams) | |
587 goto L2; | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
588 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs); |
159 | 589 break; |
590 } | |
336 | 591 arg = p->defaultArg; |
592 #if DMDV2 | |
593 if (arg->op == TOKdefault) | |
594 { DefaultInitExp *de = (DefaultInitExp *)arg; | |
595 arg = de->resolve(loc, sc); | |
596 } | |
597 else | |
598 #endif | |
599 arg = arg->copy(); | |
159 | 600 arguments->push(arg); |
601 nargs++; | |
602 } | |
603 | |
604 if (tf->varargs == 2 && i + 1 == nparams) | |
605 { | |
606 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
607 if (arg->implicitConvTo(p->type)) | |
608 { | |
609 if (nargs != nparams) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
610 error(loc, "expected %"PRIuSIZE" arguments, not %"PRIuSIZE, nparams, nargs); |
159 | 611 goto L1; |
612 } | |
613 L2: | |
614 Type *tb = p->type->toBasetype(); | |
615 Type *tret = p->isLazyArray(); | |
616 switch (tb->ty) | |
617 { | |
618 case Tsarray: | |
619 case Tarray: | |
620 { // Create a static array variable v of type arg->type | |
621 #ifdef IN_GCC | |
622 /* GCC 4.0 does not like zero length arrays used like | |
623 this; pass a null array value instead. Could also | |
624 just make a one-element array. */ | |
625 if (nargs - i == 0) | |
626 { | |
627 arg = new NullExp(loc); | |
628 break; | |
629 } | |
630 #endif | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
631 Identifier *id = Lexer::uniqueId("__arrayArg"); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
632 Type *t = new TypeSArray(((TypeArray *)tb)->next, new IntegerExp(nargs - i)); |
159 | 633 t = t->semantic(loc, sc); |
634 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
635 v->semantic(sc); | |
636 v->parent = sc->parent; | |
637 //sc->insert(v); | |
638 | |
639 Expression *c = new DeclarationExp(0, v); | |
640 c->type = v->type; | |
641 | |
642 for (size_t u = i; u < nargs; u++) | |
643 { Expression *a = (Expression *)arguments->data[u]; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
644 if (tret && !((TypeArray *)tb)->next->equals(a->type)) |
159 | 645 a = a->toDelegate(sc, tret); |
646 | |
647 Expression *e = new VarExp(loc, v); | |
648 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
|
649 AssignExp *ae = new AssignExp(loc, e, a); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
650 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
651 ae->op = TOKconstruct; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
652 #endif |
159 | 653 if (c) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
654 c = new CommaExp(loc, c, ae); |
159 | 655 else |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
656 c = ae; |
159 | 657 } |
658 arg = new VarExp(loc, v); | |
659 if (c) | |
660 arg = new CommaExp(loc, c, arg); | |
661 break; | |
662 } | |
663 case Tclass: | |
664 { /* Set arg to be: | |
665 * new Tclass(arg0, arg1, ..., argn) | |
666 */ | |
667 Expressions *args = new Expressions(); | |
668 args->setDim(nargs - i); | |
669 for (size_t u = i; u < nargs; u++) | |
670 args->data[u - i] = arguments->data[u]; | |
671 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
672 break; | |
673 } | |
674 default: | |
675 if (!arg) | |
676 { error(loc, "not enough arguments"); | |
677 return; | |
678 } | |
679 break; | |
680 } | |
681 arg = arg->semantic(sc); | |
682 //printf("\targ = '%s'\n", arg->toChars()); | |
683 arguments->setDim(i + 1); | |
684 done = 1; | |
685 } | |
686 | |
687 L1: | |
688 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
689 arg = arg->implicitCastTo(sc, p->type); | |
690 if (p->storageClass & (STCout | STCref)) | |
691 { | |
692 // BUG: should check that argument to ref is type 'invariant' | |
693 // BUG: assignments to ref should also be type 'invariant' | |
694 arg = arg->modifiableLvalue(sc, arg); | |
695 | |
696 //if (arg->op == TOKslice) | |
697 //arg->error("cannot modify slice %s", arg->toChars()); | |
698 } | |
699 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
700 // 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
|
701 #if !IN_LLVM |
159 | 702 // Convert static arrays to pointers |
703 tb = arg->type->toBasetype(); | |
704 if (tb->ty == Tsarray) | |
705 { | |
706 arg = arg->checkToPointer(); | |
707 } | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
708 #endif |
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
709 |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
710 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
711 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
|
712 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
713 arg = callCpCtor(loc, sc, arg); |
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 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
716 |
159 | 717 |
718 // Convert lazy argument to a delegate | |
719 if (p->storageClass & STClazy) | |
720 { | |
721 arg = arg->toDelegate(sc, p->type); | |
722 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
723 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
724 /* 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
|
725 * function. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
726 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
727 if (!tf->parameterEscapes(p)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
728 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
729 /* 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
|
730 * 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
|
731 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
732 if (arg->op == TOKfunction) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
733 { FuncExp *fe = (FuncExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
734 fe->fd->tookAddressOf = 0; |
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 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
737 /* 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
|
738 * 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
|
739 * 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
|
740 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
741 else if (arg->op == TOKdelegate) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
742 { DelegateExp *de = (DelegateExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
743 if (de->e1->op == TOKvar) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
744 { VarExp *ve = (VarExp *)de->e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
745 FuncDeclaration *f = ve->var->isFuncDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
746 if (f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
747 { f->tookAddressOf--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
748 //printf("tookAddressOf = %d\n", f->tookAddressOf); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
749 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
750 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
751 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
752 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
753 #endif |
159 | 754 } |
755 else | |
756 { | |
757 | |
758 // 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
|
759 // 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
|
760 if (tf->linkage != LINKd && tf->linkage != LINKintrinsic) |
159 | 761 { |
762 // Promote bytes, words, etc., to ints | |
763 arg = arg->integralPromotions(sc); | |
764 | |
765 // Promote floats to doubles | |
766 switch (arg->type->ty) | |
767 { | |
768 case Tfloat32: | |
769 arg = arg->castTo(sc, Type::tfloat64); | |
770 break; | |
771 | |
772 case Timaginary32: | |
773 arg = arg->castTo(sc, Type::timaginary64); | |
774 break; | |
775 } | |
776 } | |
777 | |
778 // Convert static arrays to dynamic arrays | |
779 tb = arg->type->toBasetype(); | |
780 if (tb->ty == Tsarray) | |
781 { TypeSArray *ts = (TypeSArray *)tb; | |
336 | 782 Type *ta = ts->next->arrayOf(); |
159 | 783 if (ts->size(arg->loc) == 0) |
784 { arg = new NullExp(arg->loc); | |
785 arg->type = ta; | |
786 } | |
787 else | |
788 arg = arg->castTo(sc, ta); | |
789 } | |
336 | 790 #if DMDV2 |
791 if (tb->ty == Tstruct) | |
792 { | |
793 arg = callCpCtor(loc, sc, arg); | |
794 } | |
795 | |
796 // Give error for overloaded function addresses | |
797 if (arg->op == TOKsymoff) | |
798 { SymOffExp *se = (SymOffExp *)arg; | |
799 if (se->hasOverloads && !se->var->isFuncDeclaration()->isUnique()) | |
800 arg->error("function %s is overloaded", arg->toChars()); | |
801 } | |
802 #endif | |
159 | 803 arg->rvalue(); |
804 } | |
805 arg = arg->optimize(WANTvalue); | |
806 arguments->data[i] = (void *) arg; | |
807 if (done) | |
808 break; | |
809 } | |
810 | |
811 #if !IN_LLVM | |
812 // If D linkage and variadic, add _arguments[] as first argument | |
813 if (tf->linkage == LINKd && tf->varargs == 1) | |
814 { | |
815 Expression *e; | |
816 | |
817 e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], | |
818 arguments->dim - nparams); | |
819 arguments->insert(0, e); | |
820 } | |
821 #endif | |
822 } | |
823 | |
824 /************************************************** | |
825 * Write expression out to buf, but wrap it | |
826 * in ( ) if its precedence is less than pr. | |
827 */ | |
828 | |
829 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
830 { | |
336 | 831 //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
|
832 if (precedence[e->op] < pr || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
833 /* 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
|
834 * They must be parenthesized. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
835 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
836 (pr == PREC_rel && precedence[e->op] == pr)) |
159 | 837 { |
838 buf->writeByte('('); | |
839 e->toCBuffer(buf, hgs); | |
840 buf->writeByte(')'); | |
841 } | |
842 else | |
843 e->toCBuffer(buf, hgs); | |
844 } | |
845 | |
846 /************************************************** | |
847 * Write out argument list to buf. | |
848 */ | |
849 | |
850 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
851 { | |
852 if (arguments) | |
853 { | |
854 for (size_t i = 0; i < arguments->dim; i++) | |
855 { Expression *arg = (Expression *)arguments->data[i]; | |
856 | |
857 if (arg) | |
858 { if (i) | |
859 buf->writeByte(','); | |
860 expToCBuffer(buf, hgs, arg, PREC_assign); | |
861 } | |
862 } | |
863 } | |
864 } | |
865 | |
866 /************************************************** | |
867 * Write out argument types to buf. | |
868 */ | |
869 | |
870 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
871 { | |
872 if (arguments) | |
873 { OutBuffer argbuf; | |
874 | |
875 for (size_t i = 0; i < arguments->dim; i++) | |
876 { Expression *arg = (Expression *)arguments->data[i]; | |
877 | |
878 if (i) | |
879 buf->writeByte(','); | |
880 argbuf.reset(); | |
881 arg->type->toCBuffer2(&argbuf, hgs, 0); | |
882 buf->write(&argbuf); | |
883 } | |
884 } | |
885 } | |
886 | |
887 /******************************** Expression **************************/ | |
888 | |
889 Expression::Expression(Loc loc, enum TOK op, int size) | |
890 : loc(loc) | |
891 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
892 //printf("Expression::Expression(op = %d) this = %p\n", op, this); |
159 | 893 this->loc = loc; |
894 this->op = op; | |
895 this->size = size; | |
896 type = NULL; | |
897 } | |
898 | |
899 Expression *Expression::syntaxCopy() | |
900 { | |
901 //printf("Expression::syntaxCopy()\n"); | |
902 //dump(0); | |
903 return copy(); | |
904 } | |
905 | |
906 /********************************* | |
907 * Does *not* do a deep copy. | |
908 */ | |
909 | |
910 Expression *Expression::copy() | |
911 { | |
912 Expression *e; | |
913 if (!size) | |
914 { | |
915 #ifdef DEBUG | |
916 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
917 printf("op = %d\n", op); | |
918 dump(0); | |
919 #endif | |
920 assert(0); | |
921 } | |
922 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
|
923 //printf("Expression::copy(op = %d) e = %p\n", op, e); |
159 | 924 return (Expression *)memcpy(e, this, size); |
925 } | |
926 | |
927 /************************** | |
928 * Semantically analyze Expression. | |
929 * Determine types, fold constants, etc. | |
930 */ | |
931 | |
932 Expression *Expression::semantic(Scope *sc) | |
933 { | |
934 #if LOGSEMANTIC | |
336 | 935 printf("Expression::semantic() %s\n", toChars()); |
159 | 936 #endif |
937 if (type) | |
938 type = type->semantic(loc, sc); | |
939 else | |
940 type = Type::tvoid; | |
941 return this; | |
942 } | |
943 | |
944 void Expression::print() | |
945 { | |
946 fprintf(stdmsg, "%s\n", toChars()); | |
947 fflush(stdmsg); | |
948 } | |
949 | |
950 char *Expression::toChars() | |
951 { OutBuffer *buf; | |
952 HdrGenState hgs; | |
953 | |
954 memset(&hgs, 0, sizeof(hgs)); | |
955 buf = new OutBuffer(); | |
956 toCBuffer(buf, &hgs); | |
957 return buf->toChars(); | |
958 } | |
959 | |
960 void Expression::error(const char *format, ...) | |
961 { | |
962 va_list ap; | |
963 va_start(ap, format); | |
964 ::verror(loc, format, ap); | |
965 va_end( ap ); | |
966 } | |
967 | |
968 void Expression::rvalue() | |
969 { | |
970 if (type && type->toBasetype()->ty == Tvoid) | |
971 { error("expression %s is void and has no value", toChars()); | |
972 #if 0 | |
973 dump(0); | |
974 halt(); | |
975 #endif | |
336 | 976 type = Type::tint32; |
159 | 977 } |
978 } | |
979 | |
980 Expression *Expression::combine(Expression *e1, Expression *e2) | |
981 { | |
982 if (e1) | |
983 { | |
984 if (e2) | |
985 { | |
986 e1 = new CommaExp(e1->loc, e1, e2); | |
987 e1->type = e2->type; | |
988 } | |
989 } | |
990 else | |
991 e1 = e2; | |
992 return e1; | |
993 } | |
994 | |
995 integer_t Expression::toInteger() | |
996 { | |
997 //printf("Expression %s\n", Token::toChars(op)); | |
998 error("Integer constant expression expected instead of %s", toChars()); | |
999 return 0; | |
1000 } | |
1001 | |
1002 uinteger_t Expression::toUInteger() | |
1003 { | |
1004 //printf("Expression %s\n", Token::toChars(op)); | |
1005 return (uinteger_t)toInteger(); | |
1006 } | |
1007 | |
1008 real_t Expression::toReal() | |
1009 { | |
1010 error("Floating point constant expression expected instead of %s", toChars()); | |
1011 return 0; | |
1012 } | |
1013 | |
1014 real_t Expression::toImaginary() | |
1015 { | |
1016 error("Floating point constant expression expected instead of %s", toChars()); | |
1017 return 0; | |
1018 } | |
1019 | |
1020 complex_t Expression::toComplex() | |
1021 { | |
1022 error("Floating point constant expression expected instead of %s", toChars()); | |
1023 #ifdef IN_GCC | |
1024 return complex_t(real_t(0)); // %% nicer | |
1025 #else | |
1026 return 0; | |
1027 #endif | |
1028 } | |
1029 | |
1030 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1031 { | |
1032 buf->writestring(Token::toChars(op)); | |
1033 } | |
1034 | |
1035 void Expression::toMangleBuffer(OutBuffer *buf) | |
1036 { | |
1037 error("expression %s is not a valid template value argument", toChars()); | |
1038 } | |
1039 | |
875
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 * Return !=0 if expression is an lvalue. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1042 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1043 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1044 int Expression::isLvalue() |
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 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1047 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1048 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1049 |
159 | 1050 /******************************* |
1051 * Give error if we're not an lvalue. | |
1052 * If we can, convert expression to be an lvalue. | |
1053 */ | |
1054 | |
1055 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
1056 { | |
1057 if (!e) | |
1058 e = this; | |
1059 else if (!loc.filename) | |
1060 loc = e->loc; | |
1061 error("%s is not an lvalue", e->toChars()); | |
1062 return this; | |
1063 } | |
1064 | |
1065 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
1066 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1067 //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
|
1068 |
159 | 1069 // 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
|
1070 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1071 if (type && (!type->isMutable() || !type->isAssignable())) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1072 error("%s is not mutable", e->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1073 #endif |
159 | 1074 return toLvalue(sc, e); |
1075 } | |
1076 | |
1077 /************************************ | |
1078 * Detect cases where pointers to the stack can 'escape' the | |
1079 * lifetime of the stack frame. | |
1080 */ | |
1081 | |
1082 void Expression::checkEscape() | |
1083 { | |
1084 } | |
1085 | |
1086 void Expression::checkScalar() | |
1087 { | |
1088 if (!type->isscalar()) | |
1089 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
1090 } | |
1091 | |
1092 void Expression::checkNoBool() | |
1093 { | |
1094 if (type->toBasetype()->ty == Tbool) | |
1095 error("operation not allowed on bool '%s'", toChars()); | |
1096 } | |
1097 | |
1098 Expression *Expression::checkIntegral() | |
1099 { | |
1100 if (!type->isintegral()) | |
1101 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
1102 return new IntegerExp(0); | |
1103 } | |
1104 return this; | |
1105 } | |
1106 | |
1107 Expression *Expression::checkArithmetic() | |
1108 { | |
1109 if (!type->isintegral() && !type->isfloating()) | |
1110 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); | |
1111 return new IntegerExp(0); | |
1112 } | |
1113 return this; | |
1114 } | |
1115 | |
1116 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
1117 { | |
1118 s->checkDeprecated(loc, sc); | |
1119 } | |
1120 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1121 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1122 void Expression::checkPurity(Scope *sc, FuncDeclaration *f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1123 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1124 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
|
1125 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
|
1126 sc->func->toChars(), f->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1127 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1128 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1129 |
159 | 1130 /******************************** |
1131 * Check for expressions that have no use. | |
1132 * Input: | |
1133 * flag 0 not going to use the result, so issue error message if no | |
1134 * side effects | |
1135 * 1 the result of the expression is used, but still check | |
1136 * for useless subexpressions | |
1137 * 2 do not issue error messages, just return !=0 if expression | |
1138 * has side effects | |
1139 */ | |
1140 | |
1141 int Expression::checkSideEffect(int flag) | |
1142 { | |
1143 if (flag == 0) | |
1144 { if (op == TOKimport) | |
1145 { | |
1146 error("%s has no effect", toChars()); | |
1147 } | |
1148 else | |
1149 error("%s has no effect in expression (%s)", | |
1150 Token::toChars(op), toChars()); | |
1151 } | |
1152 return 0; | |
1153 } | |
1154 | |
1155 /***************************** | |
1156 * Check that expression can be tested for true or false. | |
1157 */ | |
1158 | |
1159 Expression *Expression::checkToBoolean() | |
1160 { | |
1161 // Default is 'yes' - do nothing | |
1162 | |
1163 #ifdef DEBUG | |
1164 if (!type) | |
1165 dump(0); | |
1166 #endif | |
1167 | |
1168 if (!type->checkBoolean()) | |
1169 { | |
1170 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
1171 } | |
1172 return this; | |
1173 } | |
1174 | |
1175 /**************************** | |
1176 */ | |
1177 | |
1178 Expression *Expression::checkToPointer() | |
1179 { | |
1180 Expression *e; | |
1181 Type *tb; | |
1182 | |
1183 //printf("Expression::checkToPointer()\n"); | |
1184 e = this; | |
1185 | |
1186 // If C static array, convert to pointer | |
1187 tb = type->toBasetype(); | |
1188 if (tb->ty == Tsarray) | |
1189 { TypeSArray *ts = (TypeSArray *)tb; | |
1190 if (ts->size(loc) == 0) | |
1191 e = new NullExp(loc); | |
1192 else | |
1193 e = new AddrExp(loc, this); | |
336 | 1194 e->type = ts->next->pointerTo(); |
159 | 1195 } |
1196 return e; | |
1197 } | |
1198 | |
1199 /****************************** | |
1200 * Take address of expression. | |
1201 */ | |
1202 | |
1203 Expression *Expression::addressOf(Scope *sc) | |
1204 { | |
1205 Expression *e; | |
1206 | |
1207 //printf("Expression::addressOf()\n"); | |
1208 e = toLvalue(sc, NULL); | |
1209 e = new AddrExp(loc, e); | |
1210 e->type = type->pointerTo(); | |
1211 return e; | |
1212 } | |
1213 | |
1214 /****************************** | |
1215 * If this is a reference, dereference it. | |
1216 */ | |
1217 | |
1218 Expression *Expression::deref() | |
1219 { | |
1220 //printf("Expression::deref()\n"); | |
1221 if (type->ty == Treference) | |
1222 { Expression *e; | |
1223 | |
1224 e = new PtrExp(loc, this); | |
336 | 1225 e->type = ((TypeReference *)type)->next; |
159 | 1226 return e; |
1227 } | |
1228 return this; | |
1229 } | |
1230 | |
1231 /******************************** | |
1232 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
1233 */ | |
1234 | |
1235 int Expression::isBool(int result) | |
1236 { | |
1237 return FALSE; | |
1238 } | |
1239 | |
1240 /******************************** | |
1241 * Does this expression result in either a 1 or a 0? | |
1242 */ | |
1243 | |
1244 int Expression::isBit() | |
1245 { | |
1246 return FALSE; | |
1247 } | |
1248 | |
336 | 1249 /******************************** |
1250 * Can this expression throw an exception? | |
1251 * Valid only after semantic() pass. | |
1252 */ | |
1253 | |
1254 int Expression::canThrow() | |
1255 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1256 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1257 return FALSE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1258 #else |
336 | 1259 return TRUE; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1260 #endif |
336 | 1261 } |
1262 | |
1263 | |
1264 | |
159 | 1265 Expressions *Expression::arraySyntaxCopy(Expressions *exps) |
1266 { Expressions *a = NULL; | |
1267 | |
1268 if (exps) | |
1269 { | |
1270 a = new Expressions(); | |
1271 a->setDim(exps->dim); | |
1272 for (int i = 0; i < a->dim; i++) | |
1273 { Expression *e = (Expression *)exps->data[i]; | |
1274 | |
1275 e = e->syntaxCopy(); | |
1276 a->data[i] = e; | |
1277 } | |
1278 } | |
1279 return a; | |
1280 } | |
1281 | |
1282 /******************************** IntegerExp **************************/ | |
1283 | |
1284 IntegerExp::IntegerExp(Loc loc, integer_t value, Type *type) | |
1285 : Expression(loc, TOKint64, sizeof(IntegerExp)) | |
1286 { | |
1287 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1288 if (type && !type->isscalar()) | |
1289 { | |
336 | 1290 //printf("%s, loc = %d\n", toChars(), loc.linnum); |
159 | 1291 error("integral constant must be scalar type, not %s", type->toChars()); |
1292 type = Type::terror; | |
1293 } | |
1294 this->type = type; | |
1295 this->value = value; | |
1296 } | |
1297 | |
1298 IntegerExp::IntegerExp(integer_t value) | |
1299 : Expression(0, TOKint64, sizeof(IntegerExp)) | |
1300 { | |
1301 this->type = Type::tint32; | |
1302 this->value = value; | |
1303 } | |
1304 | |
1305 int IntegerExp::equals(Object *o) | |
1306 { IntegerExp *ne; | |
1307 | |
1308 if (this == o || | |
1309 (((Expression *)o)->op == TOKint64 && | |
1310 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1311 value == ne->value)) | |
1312 return 1; | |
1313 return 0; | |
1314 } | |
1315 | |
1316 char *IntegerExp::toChars() | |
1317 { | |
1318 #if 1 | |
1319 return Expression::toChars(); | |
1320 #else | |
1321 static char buffer[sizeof(value) * 3 + 1]; | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1322 sprintf(buffer, "%lld", value); |
159 | 1323 return buffer; |
1324 #endif | |
1325 } | |
1326 | |
1327 integer_t IntegerExp::toInteger() | |
1328 { Type *t; | |
1329 | |
1330 t = type; | |
1331 while (t) | |
1332 { | |
1333 switch (t->ty) | |
1334 { | |
1335 case Tbit: | |
1336 case Tbool: value = (value != 0); break; | |
1337 case Tint8: value = (d_int8) value; break; | |
1338 case Tchar: | |
1339 case Tuns8: value = (d_uns8) value; break; | |
1340 case Tint16: value = (d_int16) value; break; | |
1341 case Twchar: | |
1342 case Tuns16: value = (d_uns16) value; break; | |
1343 case Tint32: value = (d_int32) value; break; | |
1344 case Tpointer: | |
1345 case Tdchar: | |
1346 case Tuns32: value = (d_uns32) value; break; | |
1347 case Tint64: value = (d_int64) value; break; | |
1348 case Tuns64: value = (d_uns64) value; break; | |
1349 | |
1350 case Tenum: | |
1351 { | |
1352 TypeEnum *te = (TypeEnum *)t; | |
1353 t = te->sym->memtype; | |
1354 continue; | |
1355 } | |
1356 | |
1357 case Ttypedef: | |
1358 { | |
1359 TypeTypedef *tt = (TypeTypedef *)t; | |
1360 t = tt->sym->basetype; | |
1361 continue; | |
1362 } | |
1363 | |
1364 default: | |
336 | 1365 /* This can happen if errors, such as |
1366 * the type is painted on like in fromConstInitializer(). | |
1367 */ | |
1368 if (!global.errors) | |
1369 { type->print(); | |
1370 assert(0); | |
1371 } | |
159 | 1372 break; |
1373 } | |
1374 break; | |
1375 } | |
1376 return value; | |
1377 } | |
1378 | |
1379 real_t IntegerExp::toReal() | |
1380 { | |
1381 Type *t; | |
1382 | |
1383 toInteger(); | |
1384 t = type->toBasetype(); | |
1385 if (t->ty == Tuns64) | |
1386 return (real_t)(d_uns64)value; | |
1387 else | |
1388 return (real_t)(d_int64)value; | |
1389 } | |
1390 | |
1391 real_t IntegerExp::toImaginary() | |
1392 { | |
1393 return (real_t) 0; | |
1394 } | |
1395 | |
1396 complex_t IntegerExp::toComplex() | |
1397 { | |
1398 return toReal(); | |
1399 } | |
1400 | |
1401 int IntegerExp::isBool(int result) | |
1402 { | |
1403 return result ? value != 0 : value == 0; | |
1404 } | |
1405 | |
1406 Expression *IntegerExp::semantic(Scope *sc) | |
1407 { | |
1408 if (!type) | |
1409 { | |
1410 // Determine what the type of this number is | |
1411 integer_t number = value; | |
1412 | |
1413 if (number & 0x8000000000000000LL) | |
1414 type = Type::tuns64; | |
1415 else if (number & 0xFFFFFFFF80000000LL) | |
1416 type = Type::tint64; | |
1417 else | |
1418 type = Type::tint32; | |
1419 } | |
1420 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
|
1421 { 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
|
1422 type = type->semantic(loc, sc); |
159 | 1423 } |
1424 return this; | |
1425 } | |
1426 | |
1427 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1428 { | |
1429 if (!e) | |
1430 e = this; | |
1431 else if (!loc.filename) | |
1432 loc = e->loc; | |
1433 e->error("constant %s is not an lvalue", e->toChars()); | |
1434 return this; | |
1435 } | |
1436 | |
1437 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1438 { | |
1439 integer_t v = toInteger(); | |
1440 | |
1441 if (type) | |
1442 { Type *t = type; | |
1443 | |
1444 L1: | |
1445 switch (t->ty) | |
1446 { | |
1447 case Tenum: | |
1448 { TypeEnum *te = (TypeEnum *)t; | |
1449 buf->printf("cast(%s)", te->sym->toChars()); | |
1450 t = te->sym->memtype; | |
1451 goto L1; | |
1452 } | |
1453 | |
1454 case Ttypedef: | |
1455 { TypeTypedef *tt = (TypeTypedef *)t; | |
1456 buf->printf("cast(%s)", tt->sym->toChars()); | |
1457 t = tt->sym->basetype; | |
1458 goto L1; | |
1459 } | |
1460 | |
1461 case Twchar: // BUG: need to cast(wchar) | |
1462 case Tdchar: // BUG: need to cast(dchar) | |
1463 if ((uinteger_t)v > 0xFF) | |
1464 { | |
1465 buf->printf("'\\U%08x'", v); | |
1466 break; | |
1467 } | |
1468 case Tchar: | |
1469 if (v == '\'') | |
1470 buf->writestring("'\\''"); | |
1471 else if (isprint(v) && v != '\\') | |
1472 buf->printf("'%c'", (int)v); | |
1473 else | |
1474 buf->printf("'\\x%02x'", (int)v); | |
1475 break; | |
1476 | |
1477 case Tint8: | |
1478 buf->writestring("cast(byte)"); | |
1479 goto L2; | |
1480 | |
1481 case Tint16: | |
1482 buf->writestring("cast(short)"); | |
1483 goto L2; | |
1484 | |
1485 case Tint32: | |
1486 L2: | |
1487 buf->printf("%d", (int)v); | |
1488 break; | |
1489 | |
1490 case Tuns8: | |
1491 buf->writestring("cast(ubyte)"); | |
1492 goto L3; | |
1493 | |
1494 case Tuns16: | |
1495 buf->writestring("cast(ushort)"); | |
1496 goto L3; | |
1497 | |
1498 case Tuns32: | |
1499 L3: | |
1500 buf->printf("%du", (unsigned)v); | |
1501 break; | |
1502 | |
1503 case Tint64: | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1504 buf->printf("%lldL", v); |
159 | 1505 break; |
1506 | |
1507 case Tuns64: | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1508 buf->printf("%lluLU", v); |
159 | 1509 break; |
1510 | |
1511 case Tbit: | |
1512 case Tbool: | |
1513 buf->writestring((char *)(v ? "true" : "false")); | |
1514 break; | |
1515 | |
1516 case Tpointer: | |
1517 buf->writestring("cast("); | |
1518 buf->writestring(t->toChars()); | |
1519 buf->writeByte(')'); | |
1520 goto L3; | |
1521 | |
1522 default: | |
336 | 1523 /* This can happen if errors, such as |
1524 * the type is painted on like in fromConstInitializer(). | |
1525 */ | |
1526 if (!global.errors) | |
1527 { | |
159 | 1528 #ifdef DEBUG |
336 | 1529 t->print(); |
159 | 1530 #endif |
336 | 1531 assert(0); |
1532 } | |
1533 break; | |
159 | 1534 } |
1535 } | |
1536 else if (v & 0x8000000000000000LL) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1537 buf->printf("0x%llx", v); |
159 | 1538 else |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1539 buf->printf("%lld", v); |
159 | 1540 } |
1541 | |
1542 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1543 { | |
1544 if ((sinteger_t)value < 0) | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1545 buf->printf("N%lld", -value); |
159 | 1546 else |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
1547 buf->printf("%lld", value); |
159 | 1548 } |
1549 | |
1550 /******************************** RealExp **************************/ | |
1551 | |
1552 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1553 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1554 { | |
1555 //printf("RealExp::RealExp(%Lg)\n", value); | |
1556 this->value = value; | |
1557 this->type = type; | |
1558 } | |
1559 | |
1560 char *RealExp::toChars() | |
1561 { | |
1562 char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1563 | |
1564 #ifdef IN_GCC | |
1565 value.format(buffer, sizeof(buffer)); | |
1566 if (type->isimaginary()) | |
1567 strcat(buffer, "i"); | |
1568 #else | |
1569 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1570 #endif | |
1571 assert(strlen(buffer) < sizeof(buffer)); | |
1572 return mem.strdup(buffer); | |
1573 } | |
1574 | |
1575 integer_t RealExp::toInteger() | |
1576 { | |
1577 #ifdef IN_GCC | |
1578 return toReal().toInt(); | |
1579 #else | |
1580 return (sinteger_t) toReal(); | |
1581 #endif | |
1582 } | |
1583 | |
1584 uinteger_t RealExp::toUInteger() | |
1585 { | |
1586 #ifdef IN_GCC | |
1587 return (uinteger_t) toReal().toInt(); | |
1588 #else | |
1589 return (uinteger_t) toReal(); | |
1590 #endif | |
1591 } | |
1592 | |
1593 real_t RealExp::toReal() | |
1594 { | |
1595 return type->isreal() ? value : 0; | |
1596 } | |
1597 | |
1598 real_t RealExp::toImaginary() | |
1599 { | |
1600 return type->isreal() ? 0 : value; | |
1601 } | |
1602 | |
1603 complex_t RealExp::toComplex() | |
1604 { | |
1605 #ifdef __DMC__ | |
1606 return toReal() + toImaginary() * I; | |
1607 #else | |
1608 return complex_t(toReal(), toImaginary()); | |
1609 #endif | |
1610 } | |
1611 | |
1612 /******************************** | |
1613 * Test to see if two reals are the same. | |
1614 * Regard NaN's as equivalent. | |
1615 * Regard +0 and -0 as different. | |
1616 */ | |
1617 | |
1618 int RealEquals(real_t x1, real_t x2) | |
1619 { | |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1620 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
|
1621 (isnan(x1) && isnan(x2)) || |
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1622 // 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
|
1623 (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
|
1624 // 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
|
1625 (x1 != 0. && x1 == x2); |
159 | 1626 } |
1627 | |
1628 int RealExp::equals(Object *o) | |
1629 { RealExp *ne; | |
1630 | |
1631 if (this == o || | |
1632 (((Expression *)o)->op == TOKfloat64 && | |
1633 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1634 RealEquals(value, ne->value) | |
1635 ) | |
1636 ) | |
1637 return 1; | |
1638 return 0; | |
1639 } | |
1640 | |
1641 Expression *RealExp::semantic(Scope *sc) | |
1642 { | |
1643 if (!type) | |
1644 type = Type::tfloat64; | |
1645 else | |
1646 type = type->semantic(loc, sc); | |
1647 return this; | |
1648 } | |
1649 | |
1650 int RealExp::isBool(int result) | |
1651 { | |
1652 #ifdef IN_GCC | |
1653 return result ? (! value.isZero()) : (value.isZero()); | |
1654 #else | |
1655 return result ? (value != 0) | |
1656 : (value == 0); | |
1657 #endif | |
1658 } | |
1659 | |
1660 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1661 { | |
1662 /* In order to get an exact representation, try converting it | |
1663 * to decimal then back again. If it matches, use it. | |
1664 * If it doesn't, fall back to hex, which is | |
1665 * always exact. | |
1666 */ | |
1667 char buffer[25]; | |
1668 sprintf(buffer, "%Lg", value); | |
1669 assert(strlen(buffer) < sizeof(buffer)); | |
1670 #if _WIN32 && __DMC__ | |
1671 char *save = __locale_decpoint; | |
1672 __locale_decpoint = "."; | |
1673 real_t r = strtold(buffer, NULL); | |
1674 __locale_decpoint = save; | |
1675 #else | |
1676 real_t r = strtold(buffer, NULL); | |
1677 #endif | |
1678 if (r == value) // if exact duplication | |
1679 buf->writestring(buffer); | |
1680 else | |
1681 buf->printf("%La", value); // ensure exact duplication | |
1682 | |
1683 if (type) | |
1684 { | |
1685 Type *t = type->toBasetype(); | |
1686 switch (t->ty) | |
1687 { | |
1688 case Tfloat32: | |
1689 case Timaginary32: | |
1690 case Tcomplex32: | |
1691 buf->writeByte('F'); | |
1692 break; | |
1693 | |
1694 case Tfloat80: | |
1695 case Timaginary80: | |
1696 case Tcomplex80: | |
1697 buf->writeByte('L'); | |
1698 break; | |
1699 | |
1700 default: | |
1701 break; | |
1702 } | |
1703 if (t->isimaginary()) | |
1704 buf->writeByte('i'); | |
1705 } | |
1706 } | |
1707 | |
1708 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1709 { | |
1710 floatToBuffer(buf, type, value); | |
1711 } | |
1712 | |
1713 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1714 { | |
1715 /* Rely on %A to get portable mangling. | |
1716 * Must munge result to get only identifier characters. | |
1717 * | |
1718 * Possible values from %A => mangled result | |
1719 * NAN => NAN | |
1720 * -INF => NINF | |
1721 * INF => INF | |
1722 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1723 * 0X1.9P+2 => 19P2 | |
1724 */ | |
1725 | |
1726 if (isnan(value)) | |
1727 buf->writestring("NAN"); // no -NAN bugs | |
1728 else | |
1729 { | |
1730 char buffer[32]; | |
1731 int n = sprintf(buffer, "%LA", value); | |
1732 assert(n > 0 && n < sizeof(buffer)); | |
1733 for (int i = 0; i < n; i++) | |
1734 { char c = buffer[i]; | |
1735 | |
1736 switch (c) | |
1737 { | |
1738 case '-': | |
1739 buf->writeByte('N'); | |
1740 break; | |
1741 | |
1742 case '+': | |
1743 case 'X': | |
1744 case '.': | |
1745 break; | |
1746 | |
1747 case '0': | |
1748 if (i < 2) | |
1749 break; // skip leading 0X | |
1750 default: | |
1751 buf->writeByte(c); | |
1752 break; | |
1753 } | |
1754 } | |
1755 } | |
1756 } | |
1757 | |
1758 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1759 { | |
1760 buf->writeByte('e'); | |
1761 realToMangleBuffer(buf, value); | |
1762 } | |
1763 | |
1764 | |
1765 /******************************** ComplexExp **************************/ | |
1766 | |
1767 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1768 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1769 { | |
1770 this->value = value; | |
1771 this->type = type; | |
1772 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1773 } | |
1774 | |
1775 char *ComplexExp::toChars() | |
1776 { | |
1777 char buffer[sizeof(value) * 3 + 8 + 1]; | |
1778 | |
1779 #ifdef IN_GCC | |
1780 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1781 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1782 creall(value).format(buf1, sizeof(buf1)); | |
1783 cimagl(value).format(buf2, sizeof(buf2)); | |
1784 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1785 #else | |
1786 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1787 assert(strlen(buffer) < sizeof(buffer)); | |
1788 #endif | |
1789 return mem.strdup(buffer); | |
1790 } | |
1791 | |
1792 integer_t ComplexExp::toInteger() | |
1793 { | |
1794 #ifdef IN_GCC | |
1795 return (sinteger_t) toReal().toInt(); | |
1796 #else | |
1797 return (sinteger_t) toReal(); | |
1798 #endif | |
1799 } | |
1800 | |
1801 uinteger_t ComplexExp::toUInteger() | |
1802 { | |
1803 #ifdef IN_GCC | |
1804 return (uinteger_t) toReal().toInt(); | |
1805 #else | |
1806 return (uinteger_t) toReal(); | |
1807 #endif | |
1808 } | |
1809 | |
1810 real_t ComplexExp::toReal() | |
1811 { | |
1812 return creall(value); | |
1813 } | |
1814 | |
1815 real_t ComplexExp::toImaginary() | |
1816 { | |
1817 return cimagl(value); | |
1818 } | |
1819 | |
1820 complex_t ComplexExp::toComplex() | |
1821 { | |
1822 return value; | |
1823 } | |
1824 | |
1825 int ComplexExp::equals(Object *o) | |
1826 { ComplexExp *ne; | |
1827 | |
1828 if (this == o || | |
1829 (((Expression *)o)->op == TOKcomplex80 && | |
1830 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1831 RealEquals(creall(value), creall(ne->value)) && | |
1832 RealEquals(cimagl(value), cimagl(ne->value)) | |
1833 ) | |
1834 ) | |
1835 return 1; | |
1836 return 0; | |
1837 } | |
1838 | |
1839 Expression *ComplexExp::semantic(Scope *sc) | |
1840 { | |
1841 if (!type) | |
1842 type = Type::tcomplex80; | |
1843 else | |
1844 type = type->semantic(loc, sc); | |
1845 return this; | |
1846 } | |
1847 | |
1848 int ComplexExp::isBool(int result) | |
1849 { | |
1850 if (result) | |
1851 return (bool)(value); | |
1852 else | |
1853 return !value; | |
1854 } | |
1855 | |
1856 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1857 { | |
1858 /* Print as: | |
1859 * (re+imi) | |
1860 */ | |
1861 #ifdef IN_GCC | |
1862 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1863 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1864 creall(value).format(buf1, sizeof(buf1)); | |
1865 cimagl(value).format(buf2, sizeof(buf2)); | |
1866 buf->printf("(%s+%si)", buf1, buf2); | |
1867 #else | |
1868 buf->writeByte('('); | |
1869 floatToBuffer(buf, type, creall(value)); | |
1870 buf->writeByte('+'); | |
1871 floatToBuffer(buf, type, cimagl(value)); | |
1872 buf->writestring("i)"); | |
1873 #endif | |
1874 } | |
1875 | |
1876 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
1877 { | |
1878 buf->writeByte('c'); | |
1879 real_t r = toReal(); | |
1880 realToMangleBuffer(buf, r); | |
1881 buf->writeByte('c'); // separate the two | |
1882 r = toImaginary(); | |
1883 realToMangleBuffer(buf, r); | |
1884 } | |
1885 | |
1886 /******************************** IdentifierExp **************************/ | |
1887 | |
1888 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
1889 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
1890 { | |
1891 this->ident = ident; | |
1892 } | |
1893 | |
1894 Expression *IdentifierExp::semantic(Scope *sc) | |
1895 { | |
1896 Dsymbol *s; | |
1897 Dsymbol *scopesym; | |
1898 | |
1899 #if LOGSEMANTIC | |
1900 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
1901 #endif | |
1902 s = sc->search(loc, ident, &scopesym); | |
1903 if (s) | |
1904 { Expression *e; | |
1905 WithScopeSymbol *withsym; | |
1906 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1907 /* 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
|
1908 */ |
159 | 1909 withsym = scopesym->isWithScopeSymbol(); |
1910 if (withsym) | |
1911 { | |
1912 s = s->toAlias(); | |
1913 | |
1914 // Same as wthis.ident | |
1915 if (s->needThis() || s->isTemplateDeclaration()) | |
1916 { | |
1917 e = new VarExp(loc, withsym->withstate->wthis); | |
1918 e = new DotIdExp(loc, e, ident); | |
1919 } | |
1920 else | |
1921 { Type *t = withsym->withstate->wthis->type; | |
1922 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
|
1923 t = ((TypePointer *)t)->next; |
159 | 1924 e = new TypeDotIdExp(loc, t, ident); |
1925 } | |
1926 } | |
1927 else | |
1928 { | |
1929 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
1930 { // Kludge to run semantic() here because | |
1931 // ArrayScopeSymbol::search() doesn't have access to sc. | |
1932 s->semantic(sc); | |
1933 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1934 /* If f is really a function template, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1935 * 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
|
1936 */ |
159 | 1937 FuncDeclaration *f = s->isFuncDeclaration(); |
1938 if (f && f->parent) | |
1939 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
1940 | |
1941 if (ti && | |
1942 !ti->isTemplateMixin() && | |
1943 (ti->name == f->ident || | |
1944 ti->toAlias()->ident == f->ident) | |
1945 && | |
1946 ti->tempdecl && ti->tempdecl->onemember) | |
1947 { | |
1948 TemplateDeclaration *tempdecl = ti->tempdecl; | |
1949 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
1950 tempdecl = tempdecl->overroot; // then get the start | |
1951 e = new TemplateExp(loc, tempdecl); | |
1952 e = e->semantic(sc); | |
1953 return e; | |
1954 } | |
1955 } | |
1956 e = new DsymbolExp(loc, s); | |
1957 } | |
1958 return e->semantic(sc); | |
1959 } | |
1960 error("undefined identifier %s", ident->toChars()); | |
1961 type = Type::terror; | |
1962 return this; | |
1963 } | |
1964 | |
1965 char *IdentifierExp::toChars() | |
1966 { | |
1967 return ident->toChars(); | |
1968 } | |
1969 | |
1970 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1971 { | |
1972 if (hgs->hdrgen) | |
1973 buf->writestring(ident->toHChars2()); | |
1974 else | |
1975 buf->writestring(ident->toChars()); | |
1976 } | |
1977 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1978 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1979 int IdentifierExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1980 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1981 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1982 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1983 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1984 |
159 | 1985 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) |
1986 { | |
1987 #if 0 | |
1988 tym = tybasic(e1->ET->Tty); | |
1989 if (!(tyscalar(tym) || | |
1990 tym == TYstruct || | |
1991 tym == TYarray && e->Eoper == TOKaddr)) | |
1992 synerr(EM_lvalue); // lvalue expected | |
1993 #endif | |
1994 return this; | |
1995 } | |
1996 | |
1997 /******************************** DollarExp **************************/ | |
1998 | |
1999 DollarExp::DollarExp(Loc loc) | |
2000 : IdentifierExp(loc, Id::dollar) | |
2001 { | |
2002 } | |
2003 | |
2004 /******************************** DsymbolExp **************************/ | |
2005 | |
2006 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
2007 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
2008 { | |
2009 this->s = s; | |
2010 } | |
2011 | |
2012 Expression *DsymbolExp::semantic(Scope *sc) | |
2013 { | |
2014 #if LOGSEMANTIC | |
2015 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
2016 #endif | |
2017 | |
2018 Lagain: | |
2019 EnumMember *em; | |
2020 Expression *e; | |
2021 VarDeclaration *v; | |
2022 FuncDeclaration *f; | |
2023 FuncLiteralDeclaration *fld; | |
2024 Declaration *d; | |
2025 ClassDeclaration *cd; | |
2026 ClassDeclaration *thiscd = NULL; | |
2027 Import *imp; | |
2028 Package *pkg; | |
2029 Type *t; | |
2030 | |
2031 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
2032 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
2033 if (type) | |
2034 return this; | |
2035 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
2036 checkDeprecated(sc, s); | |
2037 s = s->toAlias(); | |
2038 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
2039 if (!s->isFuncDeclaration()) | |
2040 checkDeprecated(sc, s); | |
2041 | |
2042 if (sc->func) | |
2043 thiscd = sc->func->parent->isClassDeclaration(); | |
2044 | |
2045 // BUG: This should happen after overload resolution for functions, not before | |
2046 if (s->needThis()) | |
2047 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2048 if (hasThis(sc) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2049 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2050 && !s->isFuncDeclaration() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2051 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2052 ) |
159 | 2053 { |
2054 // Supply an implicit 'this', as in | |
2055 // this.ident | |
2056 | |
2057 DotVarExp *de; | |
2058 | |
2059 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
2060 return de->semantic(sc); | |
2061 } | |
2062 } | |
2063 | |
2064 em = s->isEnumMember(); | |
2065 if (em) | |
2066 { | |
2067 e = em->value->copy(); | |
2068 e = e->semantic(sc); | |
2069 return e; | |
2070 } | |
2071 v = s->isVarDeclaration(); | |
2072 if (v) | |
2073 { | |
2074 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
2075 if (!type) | |
2076 { type = v->type; | |
2077 if (!v->type) | |
2078 { error("forward reference of %s", v->toChars()); | |
2079 type = Type::terror; | |
2080 } | |
2081 } | |
2082 if (v->isConst() && type->toBasetype()->ty != Tsarray) | |
2083 { | |
2084 if (v->init) | |
2085 { | |
2086 if (v->inuse) | |
2087 { | |
2088 error("circular reference to '%s'", v->toChars()); | |
2089 type = Type::tint32; | |
2090 return this; | |
2091 } | |
2092 ExpInitializer *ei = v->init->isExpInitializer(); | |
2093 if (ei) | |
2094 { | |
2095 e = ei->exp->copy(); // make copy so we can change loc | |
2096 if (e->op == TOKstring || !e->type) | |
2097 e = e->semantic(sc); | |
2098 e = e->implicitCastTo(sc, type); | |
2099 e->loc = loc; | |
2100 return e; | |
2101 } | |
2102 } | |
2103 else | |
2104 { | |
2105 e = type->defaultInit(); | |
2106 e->loc = loc; | |
2107 return e; | |
2108 } | |
2109 } | |
2110 e = new VarExp(loc, v); | |
2111 e->type = type; | |
2112 e = e->semantic(sc); | |
2113 return e->deref(); | |
2114 } | |
2115 fld = s->isFuncLiteralDeclaration(); | |
2116 if (fld) | |
2117 { //printf("'%s' is a function literal\n", fld->toChars()); | |
2118 e = new FuncExp(loc, fld); | |
2119 return e->semantic(sc); | |
2120 } | |
2121 f = s->isFuncDeclaration(); | |
2122 if (f) | |
2123 { //printf("'%s' is a function\n", f->toChars()); | |
2124 return new VarExp(loc, f); | |
2125 } | |
2126 cd = s->isClassDeclaration(); | |
2127 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
2128 { | |
2129 // We need to add an implicit 'this' if cd is this class or a base class. | |
2130 DotTypeExp *dte; | |
2131 | |
2132 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
2133 return dte->semantic(sc); | |
2134 } | |
2135 imp = s->isImport(); | |
2136 if (imp) | |
2137 { | |
2138 ScopeExp *ie; | |
2139 | |
2140 ie = new ScopeExp(loc, imp->pkg); | |
2141 return ie->semantic(sc); | |
2142 } | |
2143 pkg = s->isPackage(); | |
2144 if (pkg) | |
2145 { | |
2146 ScopeExp *ie; | |
2147 | |
2148 ie = new ScopeExp(loc, pkg); | |
2149 return ie->semantic(sc); | |
2150 } | |
2151 Module *mod = s->isModule(); | |
2152 if (mod) | |
2153 { | |
2154 ScopeExp *ie; | |
2155 | |
2156 ie = new ScopeExp(loc, mod); | |
2157 return ie->semantic(sc); | |
2158 } | |
2159 | |
2160 t = s->getType(); | |
2161 if (t) | |
2162 { | |
2163 return new TypeExp(loc, t); | |
2164 } | |
2165 | |
2166 TupleDeclaration *tup = s->isTupleDeclaration(); | |
2167 if (tup) | |
2168 { | |
2169 e = new TupleExp(loc, tup); | |
2170 e = e->semantic(sc); | |
2171 return e; | |
2172 } | |
2173 | |
2174 TemplateInstance *ti = s->isTemplateInstance(); | |
2175 if (ti && !global.errors) | |
2176 { if (!ti->semanticdone) | |
2177 ti->semantic(sc); | |
2178 s = ti->inst->toAlias(); | |
2179 if (!s->isTemplateInstance()) | |
2180 goto Lagain; | |
2181 e = new ScopeExp(loc, ti); | |
2182 e = e->semantic(sc); | |
2183 return e; | |
2184 } | |
2185 | |
2186 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
2187 if (td) | |
2188 { | |
2189 e = new TemplateExp(loc, td); | |
2190 e = e->semantic(sc); | |
2191 return e; | |
2192 } | |
2193 | |
2194 Lerr: | |
2195 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
2196 type = Type::terror; | |
2197 return this; | |
2198 } | |
2199 | |
2200 char *DsymbolExp::toChars() | |
2201 { | |
2202 return s->toChars(); | |
2203 } | |
2204 | |
2205 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2206 { | |
2207 buf->writestring(s->toChars()); | |
2208 } | |
2209 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2210 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2211 int DsymbolExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2212 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2213 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2214 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2215 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2216 |
159 | 2217 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) |
2218 { | |
2219 #if 0 | |
2220 tym = tybasic(e1->ET->Tty); | |
2221 if (!(tyscalar(tym) || | |
2222 tym == TYstruct || | |
2223 tym == TYarray && e->Eoper == TOKaddr)) | |
2224 synerr(EM_lvalue); // lvalue expected | |
2225 #endif | |
2226 return this; | |
2227 } | |
2228 | |
2229 /******************************** ThisExp **************************/ | |
2230 | |
2231 ThisExp::ThisExp(Loc loc) | |
2232 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
2233 { | |
2234 var = NULL; | |
2235 } | |
2236 | |
2237 Expression *ThisExp::semantic(Scope *sc) | |
2238 { FuncDeclaration *fd; | |
2239 FuncDeclaration *fdthis; | |
2240 int nested = 0; | |
2241 | |
2242 #if LOGSEMANTIC | |
2243 printf("ThisExp::semantic()\n"); | |
2244 #endif | |
2245 if (type) | |
2246 { //assert(global.errors || var); | |
2247 return this; | |
2248 } | |
2249 | |
2250 /* Special case for typeof(this) and typeof(super) since both | |
2251 * should work even if they are not inside a non-static member function | |
2252 */ | |
2253 if (sc->intypeof) | |
2254 { | |
2255 // Find enclosing struct or class | |
2256 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2257 { | |
2258 ClassDeclaration *cd; | |
2259 StructDeclaration *sd; | |
2260 | |
2261 if (!s) | |
2262 { | |
2263 error("%s is not in a struct or class scope", toChars()); | |
2264 goto Lerr; | |
2265 } | |
2266 cd = s->isClassDeclaration(); | |
2267 if (cd) | |
2268 { | |
2269 type = cd->type; | |
2270 return this; | |
2271 } | |
2272 sd = s->isStructDeclaration(); | |
2273 if (sd) | |
2274 { | |
2275 type = sd->type->pointerTo(); | |
2276 return this; | |
2277 } | |
2278 } | |
2279 } | |
2280 | |
2281 fdthis = sc->parent->isFuncDeclaration(); | |
2282 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
2283 if (!fd) | |
2284 goto Lerr; | |
2285 | |
2286 assert(fd->vthis); | |
2287 var = fd->vthis; | |
2288 assert(var->parent); | |
2289 type = var->type; | |
2290 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2291 #if 0 | |
2292 if (fd != fdthis) // if nested | |
2293 { | |
2294 fdthis->getLevel(loc, fd); | |
2295 fd->vthis->nestedref = 1; | |
2296 fd->nestedFrameRef = 1; | |
2297 } | |
2298 #endif | |
336 | 2299 if (!sc->intypeof) |
2300 sc->callSuper |= CSXthis; | |
159 | 2301 return this; |
2302 | |
2303 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
|
2304 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
|
2305 type = Type::terror; |
159 | 2306 return this; |
2307 } | |
2308 | |
2309 int ThisExp::isBool(int result) | |
2310 { | |
2311 return result ? TRUE : FALSE; | |
2312 } | |
2313 | |
2314 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2315 { | |
2316 buf->writestring("this"); | |
2317 } | |
2318 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2319 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2320 int ThisExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2321 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2322 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2323 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2324 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2325 |
159 | 2326 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) |
2327 { | |
2328 return this; | |
2329 } | |
2330 | |
2331 /******************************** SuperExp **************************/ | |
2332 | |
2333 SuperExp::SuperExp(Loc loc) | |
2334 : ThisExp(loc) | |
2335 { | |
2336 op = TOKsuper; | |
2337 } | |
2338 | |
2339 Expression *SuperExp::semantic(Scope *sc) | |
2340 { FuncDeclaration *fd; | |
2341 FuncDeclaration *fdthis; | |
2342 ClassDeclaration *cd; | |
2343 Dsymbol *s; | |
2344 | |
2345 #if LOGSEMANTIC | |
2346 printf("SuperExp::semantic('%s')\n", toChars()); | |
2347 #endif | |
2348 if (type) | |
2349 return this; | |
2350 | |
2351 /* Special case for typeof(this) and typeof(super) since both | |
2352 * should work even if they are not inside a non-static member function | |
2353 */ | |
2354 if (sc->intypeof) | |
2355 { | |
2356 // Find enclosing class | |
2357 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2358 { | |
2359 ClassDeclaration *cd; | |
2360 | |
2361 if (!s) | |
2362 { | |
2363 error("%s is not in a class scope", toChars()); | |
2364 goto Lerr; | |
2365 } | |
2366 cd = s->isClassDeclaration(); | |
2367 if (cd) | |
2368 { | |
2369 cd = cd->baseClass; | |
2370 if (!cd) | |
2371 { error("class %s has no 'super'", s->toChars()); | |
2372 goto Lerr; | |
2373 } | |
2374 type = cd->type; | |
2375 return this; | |
2376 } | |
2377 } | |
2378 } | |
2379 | |
2380 fdthis = sc->parent->isFuncDeclaration(); | |
2381 fd = hasThis(sc); | |
2382 if (!fd) | |
2383 goto Lerr; | |
2384 assert(fd->vthis); | |
2385 var = fd->vthis; | |
2386 assert(var->parent); | |
2387 | |
2388 s = fd->toParent(); | |
2389 while (s && s->isTemplateInstance()) | |
2390 s = s->toParent(); | |
2391 assert(s); | |
2392 cd = s->isClassDeclaration(); | |
2393 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2394 if (!cd) | |
2395 goto Lerr; | |
2396 if (!cd->baseClass) | |
2397 { | |
2398 error("no base class for %s", cd->toChars()); | |
2399 type = fd->vthis->type; | |
2400 } | |
2401 else | |
2402 { | |
2403 type = cd->baseClass->type; | |
2404 } | |
2405 | |
2406 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2407 #if 0 | |
2408 if (fd != fdthis) | |
2409 { | |
2410 fdthis->getLevel(loc, fd); | |
2411 fd->vthis->nestedref = 1; | |
2412 fd->nestedFrameRef = 1; | |
2413 } | |
2414 #endif | |
2415 | |
336 | 2416 if (!sc->intypeof) |
2417 sc->callSuper |= CSXsuper; | |
159 | 2418 return this; |
2419 | |
2420 | |
2421 Lerr: | |
2422 error("'super' is only allowed in non-static class member functions"); | |
2423 type = Type::tint32; | |
2424 return this; | |
2425 } | |
2426 | |
2427 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2428 { | |
2429 buf->writestring("super"); | |
2430 } | |
2431 | |
2432 | |
2433 /******************************** NullExp **************************/ | |
2434 | |
2435 NullExp::NullExp(Loc loc) | |
2436 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2437 { | |
2438 committed = 0; | |
2439 } | |
2440 | |
2441 Expression *NullExp::semantic(Scope *sc) | |
2442 { | |
2443 #if LOGSEMANTIC | |
2444 printf("NullExp::semantic('%s')\n", toChars()); | |
2445 #endif | |
2446 // NULL is the same as (void *)0 | |
2447 if (!type) | |
2448 type = Type::tvoid->pointerTo(); | |
2449 return this; | |
2450 } | |
2451 | |
2452 int NullExp::isBool(int result) | |
2453 { | |
2454 return result ? FALSE : TRUE; | |
2455 } | |
2456 | |
2457 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2458 { | |
2459 buf->writestring("null"); | |
2460 } | |
2461 | |
2462 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2463 { | |
2464 buf->writeByte('n'); | |
2465 } | |
2466 | |
2467 /******************************** StringExp **************************/ | |
2468 | |
2469 StringExp::StringExp(Loc loc, char *string) | |
2470 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2471 { | |
2472 this->string = string; | |
2473 this->len = strlen(string); | |
2474 this->sz = 1; | |
2475 this->committed = 0; | |
2476 this->postfix = 0; | |
2477 } | |
2478 | |
2479 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2480 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2481 { | |
2482 this->string = string; | |
2483 this->len = len; | |
2484 this->sz = 1; | |
2485 this->committed = 0; | |
2486 this->postfix = 0; | |
2487 } | |
2488 | |
2489 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2490 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2491 { | |
2492 this->string = string; | |
2493 this->len = len; | |
2494 this->sz = 1; | |
2495 this->committed = 0; | |
2496 this->postfix = postfix; | |
2497 } | |
2498 | |
2499 #if 0 | |
2500 Expression *StringExp::syntaxCopy() | |
2501 { | |
2502 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2503 return copy(); | |
2504 } | |
2505 #endif | |
2506 | |
2507 int StringExp::equals(Object *o) | |
2508 { | |
2509 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2510 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2511 { Expression *e = (Expression *)o; | |
2512 | |
2513 if (e->op == TOKstring) | |
2514 { | |
2515 return compare(o) == 0; | |
2516 } | |
2517 } | |
2518 return FALSE; | |
2519 } | |
2520 | |
2521 char *StringExp::toChars() | |
2522 { | |
2523 OutBuffer buf; | |
2524 HdrGenState hgs; | |
2525 char *p; | |
2526 | |
2527 memset(&hgs, 0, sizeof(hgs)); | |
2528 toCBuffer(&buf, &hgs); | |
2529 buf.writeByte(0); | |
2530 p = (char *)buf.data; | |
2531 buf.data = NULL; | |
2532 return p; | |
2533 } | |
2534 | |
2535 Expression *StringExp::semantic(Scope *sc) | |
2536 { | |
2537 #if LOGSEMANTIC | |
2538 printf("StringExp::semantic() %s\n", toChars()); | |
2539 #endif | |
2540 if (!type) | |
2541 { OutBuffer buffer; | |
2542 size_t newlen = 0; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2543 const char *p; |
159 | 2544 size_t u; |
2545 unsigned c; | |
2546 | |
2547 switch (postfix) | |
2548 { | |
2549 case 'd': | |
2550 for (u = 0; u < len;) | |
2551 { | |
2552 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2553 if (p) | |
2554 { error("%s", p); | |
2555 break; | |
2556 } | |
2557 else | |
2558 { buffer.write4(c); | |
2559 newlen++; | |
2560 } | |
2561 } | |
2562 buffer.write4(0); | |
2563 string = buffer.extractData(); | |
2564 len = newlen; | |
2565 sz = 4; | |
2566 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2567 committed = 1; | |
2568 break; | |
2569 | |
2570 case 'w': | |
2571 for (u = 0; u < len;) | |
2572 { | |
2573 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2574 if (p) | |
2575 { error("%s", p); | |
2576 break; | |
2577 } | |
2578 else | |
2579 { buffer.writeUTF16(c); | |
2580 newlen++; | |
2581 if (c >= 0x10000) | |
2582 newlen++; | |
2583 } | |
2584 } | |
2585 buffer.writeUTF16(0); | |
2586 string = buffer.extractData(); | |
2587 len = newlen; | |
2588 sz = 2; | |
2589 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2590 committed = 1; | |
2591 break; | |
2592 | |
2593 case 'c': | |
2594 committed = 1; | |
2595 default: | |
2596 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2597 break; | |
2598 } | |
2599 type = type->semantic(loc, sc); | |
2600 } | |
2601 return this; | |
2602 } | |
2603 | |
2604 /**************************************** | |
2605 * Convert string to char[]. | |
2606 */ | |
2607 | |
2608 StringExp *StringExp::toUTF8(Scope *sc) | |
2609 { | |
2610 if (sz != 1) | |
2611 { // Convert to UTF-8 string | |
2612 committed = 0; | |
2613 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2614 e = e->optimize(WANTvalue); | |
2615 assert(e->op == TOKstring); | |
2616 StringExp *se = (StringExp *)e; | |
2617 assert(se->sz == 1); | |
2618 return se; | |
2619 } | |
2620 return this; | |
2621 } | |
2622 | |
2623 int StringExp::compare(Object *obj) | |
2624 { | |
2625 // Used to sort case statement expressions so we can do an efficient lookup | |
2626 StringExp *se2 = (StringExp *)(obj); | |
2627 | |
2628 // This is a kludge so isExpression() in template.c will return 5 | |
2629 // for StringExp's. | |
2630 if (!se2) | |
2631 return 5; | |
2632 | |
2633 assert(se2->op == TOKstring); | |
2634 | |
2635 int len1 = len; | |
2636 int len2 = se2->len; | |
2637 | |
2638 if (len1 == len2) | |
2639 { | |
2640 switch (sz) | |
2641 { | |
2642 case 1: | |
2643 return strcmp((char *)string, (char *)se2->string); | |
2644 | |
2645 case 2: | |
2646 { unsigned u; | |
2647 d_wchar *s1 = (d_wchar *)string; | |
2648 d_wchar *s2 = (d_wchar *)se2->string; | |
2649 | |
2650 for (u = 0; u < len; u++) | |
2651 { | |
2652 if (s1[u] != s2[u]) | |
2653 return s1[u] - s2[u]; | |
2654 } | |
2655 } | |
2656 | |
2657 case 4: | |
2658 { unsigned u; | |
2659 d_dchar *s1 = (d_dchar *)string; | |
2660 d_dchar *s2 = (d_dchar *)se2->string; | |
2661 | |
2662 for (u = 0; u < len; u++) | |
2663 { | |
2664 if (s1[u] != s2[u]) | |
2665 return s1[u] - s2[u]; | |
2666 } | |
2667 } | |
2668 break; | |
2669 | |
2670 default: | |
2671 assert(0); | |
2672 } | |
2673 } | |
2674 return len1 - len2; | |
2675 } | |
2676 | |
2677 int StringExp::isBool(int result) | |
2678 { | |
2679 return result ? TRUE : FALSE; | |
2680 } | |
2681 | |
2682 unsigned StringExp::charAt(size_t i) | |
2683 { unsigned value; | |
2684 | |
2685 switch (sz) | |
2686 { | |
2687 case 1: | |
2688 value = ((unsigned char *)string)[i]; | |
2689 break; | |
2690 | |
2691 case 2: | |
2692 value = ((unsigned short *)string)[i]; | |
2693 break; | |
2694 | |
2695 case 4: | |
2696 value = ((unsigned int *)string)[i]; | |
2697 break; | |
2698 | |
2699 default: | |
2700 assert(0); | |
2701 break; | |
2702 } | |
2703 return value; | |
2704 } | |
2705 | |
2706 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2707 { | |
2708 buf->writeByte('"'); | |
2709 for (size_t i = 0; i < len; i++) | |
2710 { unsigned c = charAt(i); | |
2711 | |
2712 switch (c) | |
2713 { | |
2714 case '"': | |
2715 case '\\': | |
2716 if (!hgs->console) | |
2717 buf->writeByte('\\'); | |
2718 default: | |
2719 if (c <= 0xFF) | |
2720 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2721 buf->writeByte(c); | |
2722 else | |
2723 buf->printf("\\x%02x", c); | |
2724 } | |
2725 else if (c <= 0xFFFF) | |
2726 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2727 else | |
2728 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2729 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2730 break; | |
2731 } | |
2732 } | |
2733 buf->writeByte('"'); | |
2734 if (postfix) | |
2735 buf->writeByte(postfix); | |
2736 } | |
2737 | |
2738 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2739 { char m; | |
2740 OutBuffer tmp; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2741 const char *p; |
159 | 2742 unsigned c; |
2743 size_t u; | |
2744 unsigned char *q; | |
2745 unsigned qlen; | |
2746 | |
2747 /* Write string in UTF-8 format | |
2748 */ | |
2749 switch (sz) | |
2750 { case 1: | |
2751 m = 'a'; | |
2752 q = (unsigned char *)string; | |
2753 qlen = len; | |
2754 break; | |
2755 case 2: | |
2756 m = 'w'; | |
2757 for (u = 0; u < len; ) | |
2758 { | |
2759 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2760 if (p) | |
2761 error("%s", p); | |
2762 else | |
2763 tmp.writeUTF8(c); | |
2764 } | |
2765 q = tmp.data; | |
2766 qlen = tmp.offset; | |
2767 break; | |
2768 case 4: | |
2769 m = 'd'; | |
2770 for (u = 0; u < len; u++) | |
2771 { | |
2772 c = ((unsigned *)string)[u]; | |
2773 if (!utf_isValidDchar(c)) | |
2774 error("invalid UCS-32 char \\U%08x", c); | |
2775 else | |
2776 tmp.writeUTF8(c); | |
2777 } | |
2778 q = tmp.data; | |
2779 qlen = tmp.offset; | |
2780 break; | |
2781 default: | |
2782 assert(0); | |
2783 } | |
2784 buf->writeByte(m); | |
2785 buf->printf("%d_", qlen); | |
2786 for (size_t i = 0; i < qlen; i++) | |
2787 buf->printf("%02x", q[i]); | |
2788 } | |
2789 | |
2790 /************************ ArrayLiteralExp ************************************/ | |
2791 | |
2792 // [ e1, e2, e3, ... ] | |
2793 | |
2794 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2795 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2796 { | |
2797 this->elements = elements; | |
2798 } | |
2799 | |
2800 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2801 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2802 { | |
2803 elements = new Expressions; | |
2804 elements->push(e); | |
2805 } | |
2806 | |
2807 Expression *ArrayLiteralExp::syntaxCopy() | |
2808 { | |
2809 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2810 } | |
2811 | |
2812 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2813 { Expression *e; | |
2814 Type *t0 = NULL; | |
2815 | |
2816 #if LOGSEMANTIC | |
2817 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2818 #endif | |
2819 if (type) | |
2820 return this; | |
2821 | |
2822 // Run semantic() on each element | |
2823 for (int i = 0; i < elements->dim; i++) | |
2824 { e = (Expression *)elements->data[i]; | |
2825 e = e->semantic(sc); | |
2826 elements->data[i] = (void *)e; | |
2827 } | |
2828 expandTuples(elements); | |
2829 for (int i = 0; i < elements->dim; i++) | |
2830 { e = (Expression *)elements->data[i]; | |
2831 | |
2832 if (!e->type) | |
2833 error("%s has no value", e->toChars()); | |
2834 e = resolveProperties(sc, e); | |
2835 | |
2836 unsigned char committed = 1; | |
2837 if (e->op == TOKstring) | |
2838 committed = ((StringExp *)e)->committed; | |
2839 | |
2840 if (!t0) | |
2841 { t0 = e->type; | |
2842 // Convert any static arrays to dynamic arrays | |
2843 if (t0->ty == Tsarray) | |
2844 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2845 t0 = ((TypeSArray *)t0)->next->arrayOf(); |
159 | 2846 e = e->implicitCastTo(sc, t0); |
2847 } | |
2848 } | |
2849 else | |
2850 e = e->implicitCastTo(sc, t0); | |
2851 if (!committed && e->op == TOKstring) | |
2852 { StringExp *se = (StringExp *)e; | |
2853 se->committed = 0; | |
2854 } | |
2855 elements->data[i] = (void *)e; | |
2856 } | |
2857 | |
2858 if (!t0) | |
2859 t0 = Type::tvoid; | |
2860 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
2861 type = type->semantic(loc, sc); | |
2862 return this; | |
2863 } | |
2864 | |
2865 int ArrayLiteralExp::checkSideEffect(int flag) | |
2866 { int f = 0; | |
2867 | |
2868 for (size_t i = 0; i < elements->dim; i++) | |
2869 { Expression *e = (Expression *)elements->data[i]; | |
2870 | |
2871 f |= e->checkSideEffect(2); | |
2872 } | |
2873 if (flag == 0 && f == 0) | |
2874 Expression::checkSideEffect(0); | |
2875 return f; | |
2876 } | |
2877 | |
2878 int ArrayLiteralExp::isBool(int result) | |
2879 { | |
2880 size_t dim = elements ? elements->dim : 0; | |
2881 return result ? (dim != 0) : (dim == 0); | |
2882 } | |
2883 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2884 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2885 int ArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2886 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2887 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
|
2888 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2889 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2890 |
159 | 2891 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
2892 { | |
2893 buf->writeByte('['); | |
2894 argsToCBuffer(buf, elements, hgs); | |
2895 buf->writeByte(']'); | |
2896 } | |
2897 | |
2898 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
2899 { | |
2900 size_t dim = elements ? elements->dim : 0; | |
2901 buf->printf("A%u", dim); | |
2902 for (size_t i = 0; i < dim; i++) | |
2903 { Expression *e = (Expression *)elements->data[i]; | |
2904 e->toMangleBuffer(buf); | |
2905 } | |
2906 } | |
2907 | |
2908 /************************ AssocArrayLiteralExp ************************************/ | |
2909 | |
2910 // [ key0 : value0, key1 : value1, ... ] | |
2911 | |
2912 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
2913 Expressions *keys, Expressions *values) | |
2914 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
2915 { | |
2916 assert(keys->dim == values->dim); | |
2917 this->keys = keys; | |
2918 this->values = values; | |
2919 } | |
2920 | |
2921 Expression *AssocArrayLiteralExp::syntaxCopy() | |
2922 { | |
2923 return new AssocArrayLiteralExp(loc, | |
2924 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
2925 } | |
2926 | |
2927 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
2928 { Expression *e; | |
2929 Type *tkey = NULL; | |
2930 Type *tvalue = NULL; | |
2931 | |
2932 #if LOGSEMANTIC | |
2933 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
2934 #endif | |
2935 | |
2936 // Run semantic() on each element | |
2937 for (size_t i = 0; i < keys->dim; i++) | |
2938 { Expression *key = (Expression *)keys->data[i]; | |
2939 Expression *value = (Expression *)values->data[i]; | |
2940 | |
2941 key = key->semantic(sc); | |
2942 value = value->semantic(sc); | |
2943 | |
2944 keys->data[i] = (void *)key; | |
2945 values->data[i] = (void *)value; | |
2946 } | |
2947 expandTuples(keys); | |
2948 expandTuples(values); | |
2949 if (keys->dim != values->dim) | |
2950 { | |
2951 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
2952 keys->setDim(0); | |
2953 values->setDim(0); | |
2954 } | |
2955 for (size_t i = 0; i < keys->dim; i++) | |
2956 { Expression *key = (Expression *)keys->data[i]; | |
2957 Expression *value = (Expression *)values->data[i]; | |
2958 | |
2959 if (!key->type) | |
2960 error("%s has no value", key->toChars()); | |
2961 if (!value->type) | |
2962 error("%s has no value", value->toChars()); | |
2963 key = resolveProperties(sc, key); | |
2964 value = resolveProperties(sc, value); | |
2965 | |
2966 if (!tkey) | |
2967 tkey = key->type; | |
2968 else | |
2969 key = key->implicitCastTo(sc, tkey); | |
2970 keys->data[i] = (void *)key; | |
2971 | |
2972 if (!tvalue) | |
2973 tvalue = value->type; | |
2974 else | |
2975 value = value->implicitCastTo(sc, tvalue); | |
2976 values->data[i] = (void *)value; | |
2977 } | |
2978 | |
2979 if (!tkey) | |
2980 tkey = Type::tvoid; | |
2981 if (!tvalue) | |
2982 tvalue = Type::tvoid; | |
2983 type = new TypeAArray(tvalue, tkey); | |
2984 type = type->semantic(loc, sc); | |
2985 return this; | |
2986 } | |
2987 | |
2988 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
2989 { int f = 0; | |
2990 | |
2991 for (size_t i = 0; i < keys->dim; i++) | |
2992 { Expression *key = (Expression *)keys->data[i]; | |
2993 Expression *value = (Expression *)values->data[i]; | |
2994 | |
2995 f |= key->checkSideEffect(2); | |
2996 f |= value->checkSideEffect(2); | |
2997 } | |
2998 if (flag == 0 && f == 0) | |
2999 Expression::checkSideEffect(0); | |
3000 return f; | |
3001 } | |
3002 | |
3003 int AssocArrayLiteralExp::isBool(int result) | |
3004 { | |
3005 size_t dim = keys->dim; | |
3006 return result ? (dim != 0) : (dim == 0); | |
3007 } | |
3008 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3009 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3010 int AssocArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3011 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3012 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3013 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3014 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3015 |
159 | 3016 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3017 { | |
3018 buf->writeByte('['); | |
3019 for (size_t i = 0; i < keys->dim; i++) | |
3020 { Expression *key = (Expression *)keys->data[i]; | |
3021 Expression *value = (Expression *)values->data[i]; | |
3022 | |
3023 if (i) | |
3024 buf->writeByte(','); | |
3025 expToCBuffer(buf, hgs, key, PREC_assign); | |
3026 buf->writeByte(':'); | |
3027 expToCBuffer(buf, hgs, value, PREC_assign); | |
3028 } | |
3029 buf->writeByte(']'); | |
3030 } | |
3031 | |
3032 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3033 { | |
3034 size_t dim = keys->dim; | |
3035 buf->printf("A%u", dim); | |
3036 for (size_t i = 0; i < dim; i++) | |
3037 { Expression *key = (Expression *)keys->data[i]; | |
3038 Expression *value = (Expression *)values->data[i]; | |
3039 | |
3040 key->toMangleBuffer(buf); | |
3041 value->toMangleBuffer(buf); | |
3042 } | |
3043 } | |
3044 | |
3045 /************************ StructLiteralExp ************************************/ | |
3046 | |
3047 // sd( e1, e2, e3, ... ) | |
3048 | |
3049 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
3050 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
3051 { | |
3052 this->sd = sd; | |
3053 this->elements = elements; | |
3054 this->sym = NULL; | |
3055 this->soffset = 0; | |
3056 this->fillHoles = 1; | |
3057 } | |
3058 | |
3059 Expression *StructLiteralExp::syntaxCopy() | |
3060 { | |
3061 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
3062 } | |
3063 | |
3064 Expression *StructLiteralExp::semantic(Scope *sc) | |
3065 { Expression *e; | |
3066 | |
3067 #if LOGSEMANTIC | |
3068 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
3069 #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
|
3070 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
|
3071 return this; |
159 | 3072 |
3073 // Run semantic() on each element | |
3074 for (size_t i = 0; i < elements->dim; i++) | |
3075 { e = (Expression *)elements->data[i]; | |
3076 if (!e) | |
3077 continue; | |
3078 e = e->semantic(sc); | |
3079 elements->data[i] = (void *)e; | |
3080 } | |
3081 expandTuples(elements); | |
3082 size_t offset = 0; | |
3083 for (size_t i = 0; i < elements->dim; i++) | |
3084 { e = (Expression *)elements->data[i]; | |
3085 if (!e) | |
3086 continue; | |
3087 | |
3088 if (!e->type) | |
3089 error("%s has no value", e->toChars()); | |
3090 e = resolveProperties(sc, e); | |
3091 if (i >= sd->fields.dim) | |
3092 { error("more initializers than fields of %s", sd->toChars()); | |
3093 break; | |
3094 } | |
3095 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3096 VarDeclaration *v = s->isVarDeclaration(); | |
3097 assert(v); | |
3098 if (v->offset < offset) | |
3099 error("overlapping initialization for %s", v->toChars()); | |
3100 offset = v->offset + v->type->size(); | |
3101 | |
3102 Type *telem = v->type; | |
3103 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
3104 { /* Static array initialization, as in: | |
3105 * T[3][5] = e; | |
3106 */ | |
3107 telem = telem->toBasetype()->nextOf(); | |
3108 } | |
3109 | |
3110 e = e->implicitCastTo(sc, telem); | |
3111 | |
3112 elements->data[i] = (void *)e; | |
3113 } | |
3114 | |
3115 /* Fill out remainder of elements[] with default initializers for fields[] | |
3116 */ | |
3117 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
3118 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3119 VarDeclaration *v = s->isVarDeclaration(); | |
3120 assert(v); | |
3121 | |
3122 if (v->offset < offset) | |
3123 { e = NULL; | |
3124 sd->hasUnions = 1; | |
3125 } | |
3126 else | |
3127 { | |
3128 if (v->init) | |
3129 { e = v->init->toExpression(); | |
3130 if (!e) | |
3131 error("cannot make expression out of initializer for %s", v->toChars()); | |
3132 } | |
3133 else | |
3134 { e = v->type->defaultInit(); | |
3135 e->loc = loc; | |
3136 } | |
3137 offset = v->offset + v->type->size(); | |
3138 } | |
3139 elements->push(e); | |
3140 } | |
3141 | |
3142 type = sd->type; | |
3143 return this; | |
3144 } | |
3145 | |
3146 /************************************** | |
3147 * Gets expression at offset of type. | |
3148 * Returns NULL if not found. | |
3149 */ | |
3150 | |
3151 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
|
3152 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3153 //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
|
3154 // /*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
|
3155 Expression *e = NULL; |
159 | 3156 int i = getFieldIndex(type, offset); |
3157 | |
3158 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
|
3159 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3160 //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
|
3161 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
|
3162 e = (Expression *)elements->data[i]; |
159 | 3163 if (e) |
3164 { | |
3165 e = e->copy(); | |
3166 e->type = type; | |
3167 } | |
3168 } | |
3169 return e; | |
3170 } | |
3171 | |
3172 /************************************ | |
3173 * Get index of field. | |
3174 * Returns -1 if not found. | |
3175 */ | |
3176 | |
3177 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
3178 { | |
3179 /* Find which field offset is by looking at the field offsets | |
3180 */ | |
3181 for (size_t i = 0; i < sd->fields.dim; i++) | |
3182 { | |
3183 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3184 VarDeclaration *v = s->isVarDeclaration(); | |
3185 assert(v); | |
3186 | |
3187 if (offset == v->offset && | |
3188 type->size() == v->type->size()) | |
3189 { Expression *e = (Expression *)elements->data[i]; | |
3190 if (e) | |
3191 { | |
3192 return i; | |
3193 } | |
3194 break; | |
3195 } | |
3196 } | |
3197 return -1; | |
3198 } | |
3199 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3200 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3201 int StructLiteralExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3202 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3203 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3204 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3205 #endif |
159 | 3206 |
3207 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) | |
3208 { | |
3209 return this; | |
3210 } | |
3211 | |
3212 | |
3213 int StructLiteralExp::checkSideEffect(int flag) | |
3214 { int f = 0; | |
3215 | |
3216 for (size_t i = 0; i < elements->dim; i++) | |
3217 { Expression *e = (Expression *)elements->data[i]; | |
3218 if (!e) | |
3219 continue; | |
3220 | |
3221 f |= e->checkSideEffect(2); | |
3222 } | |
3223 if (flag == 0 && f == 0) | |
3224 Expression::checkSideEffect(0); | |
3225 return f; | |
3226 } | |
3227 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3228 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3229 int StructLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3230 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3231 return arrayExpressionCanThrow(elements); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3232 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3233 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3234 |
159 | 3235 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3236 { | |
3237 buf->writestring(sd->toChars()); | |
3238 buf->writeByte('('); | |
3239 argsToCBuffer(buf, elements, hgs); | |
3240 buf->writeByte(')'); | |
3241 } | |
3242 | |
3243 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3244 { | |
3245 size_t dim = elements ? elements->dim : 0; | |
3246 buf->printf("S%u", dim); | |
3247 for (size_t i = 0; i < dim; i++) | |
3248 { Expression *e = (Expression *)elements->data[i]; | |
3249 if (e) | |
3250 e->toMangleBuffer(buf); | |
3251 else | |
3252 buf->writeByte('v'); // 'v' for void | |
3253 } | |
3254 } | |
3255 | |
3256 /************************ TypeDotIdExp ************************************/ | |
3257 | |
3258 /* Things like: | |
3259 * int.size | |
3260 * foo.size | |
3261 * (foo).size | |
3262 * cast(foo).size | |
3263 */ | |
3264 | |
3265 TypeDotIdExp::TypeDotIdExp(Loc loc, Type *type, Identifier *ident) | |
3266 : Expression(loc, TOKtypedot, sizeof(TypeDotIdExp)) | |
3267 { | |
3268 this->type = type; | |
3269 this->ident = ident; | |
3270 } | |
3271 | |
3272 Expression *TypeDotIdExp::syntaxCopy() | |
3273 { | |
3274 TypeDotIdExp *te = new TypeDotIdExp(loc, type->syntaxCopy(), ident); | |
3275 return te; | |
3276 } | |
3277 | |
3278 Expression *TypeDotIdExp::semantic(Scope *sc) | |
3279 { Expression *e; | |
3280 | |
3281 #if LOGSEMANTIC | |
3282 printf("TypeDotIdExp::semantic()\n"); | |
3283 #endif | |
3284 e = new DotIdExp(loc, new TypeExp(loc, type), ident); | |
3285 e = e->semantic(sc); | |
3286 return e; | |
3287 } | |
3288 | |
3289 void TypeDotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3290 { | |
3291 buf->writeByte('('); | |
3292 type->toCBuffer(buf, NULL, hgs); | |
3293 buf->writeByte(')'); | |
3294 buf->writeByte('.'); | |
3295 buf->writestring(ident->toChars()); | |
3296 } | |
3297 | |
3298 /************************************************************/ | |
3299 | |
3300 // Mainly just a placeholder | |
3301 | |
3302 TypeExp::TypeExp(Loc loc, Type *type) | |
3303 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
3304 { | |
3305 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
3306 this->type = type; | |
3307 } | |
3308 | |
3309 Expression *TypeExp::semantic(Scope *sc) | |
3310 { | |
3311 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
3312 type = type->semantic(loc, sc); | |
3313 return this; | |
3314 } | |
3315 | |
3316 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3317 { | |
3318 type->toCBuffer(buf, NULL, hgs); | |
3319 } | |
3320 | |
3321 /************************************************************/ | |
3322 | |
3323 // Mainly just a placeholder | |
3324 | |
3325 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
3326 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
3327 { | |
3328 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
3329 //static int count; if (++count == 38) *(char*)0=0; | |
3330 this->sds = pkg; | |
3331 } | |
3332 | |
3333 Expression *ScopeExp::syntaxCopy() | |
3334 { | |
3335 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
3336 return se; | |
3337 } | |
3338 | |
3339 Expression *ScopeExp::semantic(Scope *sc) | |
3340 { | |
3341 TemplateInstance *ti; | |
3342 ScopeDsymbol *sds2; | |
3343 | |
3344 #if LOGSEMANTIC | |
3345 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
3346 #endif | |
3347 Lagain: | |
3348 ti = sds->isTemplateInstance(); | |
3349 if (ti && !global.errors) | |
3350 { Dsymbol *s; | |
3351 if (!ti->semanticdone) | |
3352 ti->semantic(sc); | |
3353 s = ti->inst->toAlias(); | |
3354 sds2 = s->isScopeDsymbol(); | |
3355 if (!sds2) | |
3356 { Expression *e; | |
3357 | |
3358 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
3359 if (ti->withsym) | |
3360 { | |
3361 // Same as wthis.s | |
3362 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3363 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3364 } | |
3365 else | |
3366 e = new DsymbolExp(loc, s); | |
3367 e = e->semantic(sc); | |
3368 //printf("-1ScopeExp::semantic()\n"); | |
3369 return e; | |
3370 } | |
3371 if (sds2 != sds) | |
3372 { | |
3373 sds = sds2; | |
3374 goto Lagain; | |
3375 } | |
3376 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3377 } | |
3378 else | |
3379 { | |
3380 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3381 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3382 sds->semantic(sc); | |
3383 } | |
3384 type = Type::tvoid; | |
3385 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3386 return this; | |
3387 } | |
3388 | |
3389 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3390 { | |
3391 if (sds->isTemplateInstance()) | |
3392 { | |
3393 sds->toCBuffer(buf, hgs); | |
3394 } | |
3395 else | |
3396 { | |
3397 buf->writestring(sds->kind()); | |
3398 buf->writestring(" "); | |
3399 buf->writestring(sds->toChars()); | |
3400 } | |
3401 } | |
3402 | |
3403 /********************** TemplateExp **************************************/ | |
3404 | |
3405 // Mainly just a placeholder | |
3406 | |
3407 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3408 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3409 { | |
3410 //printf("TemplateExp(): %s\n", td->toChars()); | |
3411 this->td = td; | |
3412 } | |
3413 | |
3414 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3415 { | |
3416 buf->writestring(td->toChars()); | |
3417 } | |
3418 | |
3419 void TemplateExp::rvalue() | |
3420 { | |
3421 error("template %s has no value", toChars()); | |
3422 } | |
3423 | |
3424 /********************** NewExp **************************************/ | |
3425 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3426 /* thisexp.new(newargs) newtype(arguments) */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3427 |
159 | 3428 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, |
3429 Type *newtype, Expressions *arguments) | |
3430 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3431 { | |
3432 this->thisexp = thisexp; | |
3433 this->newargs = newargs; | |
3434 this->newtype = newtype; | |
3435 this->arguments = arguments; | |
3436 member = NULL; | |
3437 allocator = NULL; | |
3438 onstack = 0; | |
3439 } | |
3440 | |
3441 Expression *NewExp::syntaxCopy() | |
3442 { | |
3443 return new NewExp(loc, | |
3444 thisexp ? thisexp->syntaxCopy() : NULL, | |
3445 arraySyntaxCopy(newargs), | |
3446 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3447 } | |
3448 | |
3449 | |
3450 Expression *NewExp::semantic(Scope *sc) | |
3451 { int i; | |
3452 Type *tb; | |
3453 ClassDeclaration *cdthis = NULL; | |
3454 | |
3455 #if LOGSEMANTIC | |
3456 printf("NewExp::semantic() %s\n", toChars()); | |
3457 if (thisexp) | |
3458 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3459 printf("\tnewtype: %s\n", newtype->toChars()); | |
3460 #endif | |
3461 if (type) // if semantic() already run | |
3462 return this; | |
3463 | |
3464 Lagain: | |
3465 if (thisexp) | |
3466 { thisexp = thisexp->semantic(sc); | |
3467 cdthis = thisexp->type->isClassHandle(); | |
3468 if (cdthis) | |
3469 { | |
3470 sc = sc->push(cdthis); | |
3471 type = newtype->semantic(loc, sc); | |
3472 sc = sc->pop(); | |
3473 } | |
3474 else | |
3475 { | |
3476 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3477 type = newtype->semantic(loc, sc); | |
3478 } | |
3479 } | |
3480 else | |
3481 type = newtype->semantic(loc, sc); | |
3482 newtype = type; // in case type gets cast to something else | |
3483 tb = type->toBasetype(); | |
3484 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3485 | |
3486 arrayExpressionSemantic(newargs, sc); | |
3487 preFunctionArguments(loc, sc, newargs); | |
3488 arrayExpressionSemantic(arguments, sc); | |
3489 preFunctionArguments(loc, sc, arguments); | |
3490 | |
3491 if (thisexp && tb->ty != Tclass) | |
3492 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3493 | |
3494 if (tb->ty == Tclass) | |
3495 { TypeFunction *tf; | |
3496 | |
3497 TypeClass *tc = (TypeClass *)(tb); | |
3498 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3499 if (cd->isInterfaceDeclaration()) | |
3500 error("cannot create instance of interface %s", cd->toChars()); | |
3501 else if (cd->isAbstract()) | |
3502 { error("cannot create instance of abstract class %s", cd->toChars()); | |
3503 for (int i = 0; i < cd->vtbl.dim; i++) | |
3504 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration(); | |
3505 if (fd && fd->isAbstract()) | |
3506 error("function %s is abstract", fd->toChars()); | |
3507 } | |
3508 } | |
3509 checkDeprecated(sc, cd); | |
3510 if (cd->isNested()) | |
3511 { /* We need a 'this' pointer for the nested class. | |
3512 * Ensure we have the right one. | |
3513 */ | |
3514 Dsymbol *s = cd->toParent2(); | |
3515 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
|
3516 FuncDeclaration *fdn = s->isFuncDeclaration(); |
159 | 3517 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3518 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3519 if (cdn) |
3520 { | |
3521 if (!cdthis) | |
3522 { | |
3523 // Supply an implicit 'this' and try again | |
3524 thisexp = new ThisExp(loc); | |
3525 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3526 { if (!sp) | |
3527 { | |
3528 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3529 break; | |
3530 } | |
3531 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3532 if (!cdp) | |
3533 continue; | |
3534 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3535 break; | |
3536 // Add a '.outer' and try again | |
3537 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3538 } | |
3539 if (!global.errors) | |
3540 goto Lagain; | |
3541 } | |
3542 if (cdthis) | |
3543 { | |
3544 //printf("cdthis = %s\n", cdthis->toChars()); | |
3545 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3546 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3547 } | |
3548 #if 0 | |
3549 else | |
3550 { | |
3551 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3552 { | |
3553 if (!sf) | |
3554 { | |
3555 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3556 break; | |
3557 } | |
3558 printf("sf = %s\n", sf->toChars()); | |
3559 AggregateDeclaration *ad = sf->isThis(); | |
3560 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3561 break; | |
3562 } | |
3563 } | |
3564 #endif | |
3565 } | |
3566 else if (thisexp) | |
3567 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
|
3568 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
|
3569 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3570 // 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
|
3571 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
|
3572 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3573 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
|
3574 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3575 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
|
3576 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
|
3577 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3578 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
|
3579 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3580 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3581 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3582 |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3583 } |
159 | 3584 } |
3585 else if (thisexp) | |
3586 error("e.new is only for allocating nested classes"); | |
3587 | |
3588 FuncDeclaration *f = cd->ctor; | |
3589 if (f) | |
3590 { | |
3591 assert(f); | |
3592 f = f->overloadResolve(loc, arguments); | |
3593 checkDeprecated(sc, f); | |
3594 member = f->isCtorDeclaration(); | |
3595 assert(member); | |
3596 | |
3597 cd->accessCheck(loc, sc, member); | |
3598 | |
3599 tf = (TypeFunction *)f->type; | |
3600 type = tf->next; | |
3601 | |
3602 if (!arguments) | |
3603 arguments = new Expressions(); | |
3604 functionArguments(loc, sc, tf, arguments); | |
3605 } | |
3606 else | |
3607 { | |
3608 if (arguments && arguments->dim) | |
3609 error("no constructor for %s", cd->toChars()); | |
3610 } | |
3611 | |
3612 if (cd->aggNew) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3613 { |
159 | 3614 // Prepend the uint size argument to newargs[] |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3615 Expression *e = new IntegerExp(loc, cd->size(loc), Type::tuns32); |
159 | 3616 if (!newargs) |
3617 newargs = new Expressions(); | |
3618 newargs->shift(e); | |
3619 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3620 f = cd->aggNew->overloadResolve(loc, newargs); |
159 | 3621 allocator = f->isNewDeclaration(); |
3622 assert(allocator); | |
3623 | |
3624 tf = (TypeFunction *)f->type; | |
3625 functionArguments(loc, sc, tf, newargs); | |
3626 } | |
3627 else | |
3628 { | |
3629 if (newargs && newargs->dim) | |
3630 error("no allocator for %s", cd->toChars()); | |
3631 } | |
3632 } | |
3633 else if (tb->ty == Tstruct) | |
3634 { | |
3635 TypeStruct *ts = (TypeStruct *)tb; | |
3636 StructDeclaration *sd = ts->sym; | |
3637 FuncDeclaration *f = sd->aggNew; | |
3638 TypeFunction *tf; | |
3639 | |
3640 if (arguments && arguments->dim) | |
3641 error("no constructor for %s", type->toChars()); | |
3642 | |
3643 if (f) | |
3644 { | |
3645 Expression *e; | |
3646 | |
3647 // Prepend the uint size argument to newargs[] | |
3648 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3649 if (!newargs) | |
3650 newargs = new Expressions(); | |
3651 newargs->shift(e); | |
3652 | |
3653 f = f->overloadResolve(loc, newargs); | |
3654 allocator = f->isNewDeclaration(); | |
3655 assert(allocator); | |
3656 | |
3657 tf = (TypeFunction *)f->type; | |
3658 functionArguments(loc, sc, tf, newargs); | |
3659 | |
3660 e = new VarExp(loc, f); | |
3661 e = new CallExp(loc, e, newargs); | |
3662 e = e->semantic(sc); | |
3663 e->type = type->pointerTo(); | |
3664 return e; | |
3665 } | |
3666 | |
3667 type = type->pointerTo(); | |
3668 } | |
3669 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3670 { | |
3671 for (size_t i = 0; i < arguments->dim; i++) | |
3672 { | |
3673 if (tb->ty != Tarray) | |
3674 { error("too many arguments for array"); | |
3675 arguments->dim = i; | |
3676 break; | |
3677 } | |
3678 | |
3679 Expression *arg = (Expression *)arguments->data[i]; | |
3680 arg = resolveProperties(sc, arg); | |
3681 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3682 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3683 error("negative array index %s", arg->toChars()); | |
3684 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
|
3685 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3686 } |
3687 } | |
3688 else if (tb->isscalar()) | |
3689 { | |
3690 if (arguments && arguments->dim) | |
3691 error("no constructor for %s", type->toChars()); | |
3692 | |
3693 type = type->pointerTo(); | |
3694 } | |
3695 else | |
3696 { | |
3697 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3698 type = type->pointerTo(); | |
3699 } | |
3700 | |
3701 //printf("NewExp: '%s'\n", toChars()); | |
3702 //printf("NewExp:type '%s'\n", type->toChars()); | |
3703 | |
3704 return this; | |
3705 } | |
3706 | |
3707 int NewExp::checkSideEffect(int flag) | |
3708 { | |
3709 return 1; | |
3710 } | |
3711 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3712 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3713 int NewExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3714 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3715 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3716 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3717 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3718 |
159 | 3719 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3720 { int i; | |
3721 | |
3722 if (thisexp) | |
3723 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3724 buf->writeByte('.'); | |
3725 } | |
3726 buf->writestring("new "); | |
3727 if (newargs && newargs->dim) | |
3728 { | |
3729 buf->writeByte('('); | |
3730 argsToCBuffer(buf, newargs, hgs); | |
3731 buf->writeByte(')'); | |
3732 } | |
3733 newtype->toCBuffer(buf, NULL, hgs); | |
3734 if (arguments && arguments->dim) | |
3735 { | |
3736 buf->writeByte('('); | |
3737 argsToCBuffer(buf, arguments, hgs); | |
3738 buf->writeByte(')'); | |
3739 } | |
3740 } | |
3741 | |
3742 /********************** NewAnonClassExp **************************************/ | |
3743 | |
3744 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3745 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3746 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3747 { | |
3748 this->thisexp = thisexp; | |
3749 this->newargs = newargs; | |
3750 this->cd = cd; | |
3751 this->arguments = arguments; | |
3752 } | |
3753 | |
3754 Expression *NewAnonClassExp::syntaxCopy() | |
3755 { | |
3756 return new NewAnonClassExp(loc, | |
3757 thisexp ? thisexp->syntaxCopy() : NULL, | |
3758 arraySyntaxCopy(newargs), | |
3759 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3760 arraySyntaxCopy(arguments)); | |
3761 } | |
3762 | |
3763 | |
3764 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3765 { | |
3766 #if LOGSEMANTIC | |
3767 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3768 //printf("thisexp = %p\n", thisexp); |
159 | 3769 //printf("type: %s\n", type->toChars()); |
3770 #endif | |
3771 | |
3772 Expression *d = new DeclarationExp(loc, cd); | |
3773 d = d->semantic(sc); | |
3774 | |
3775 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3776 | |
3777 Expression *c = new CommaExp(loc, d, n); | |
3778 return c->semantic(sc); | |
3779 } | |
3780 | |
3781 int NewAnonClassExp::checkSideEffect(int flag) | |
3782 { | |
3783 return 1; | |
3784 } | |
3785 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3786 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3787 int NewAnonClassExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3788 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3789 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3790 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3791 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3792 |
159 | 3793 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3794 { int i; | |
3795 | |
3796 if (thisexp) | |
3797 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3798 buf->writeByte('.'); | |
3799 } | |
3800 buf->writestring("new"); | |
3801 if (newargs && newargs->dim) | |
3802 { | |
3803 buf->writeByte('('); | |
3804 argsToCBuffer(buf, newargs, hgs); | |
3805 buf->writeByte(')'); | |
3806 } | |
3807 buf->writestring(" class "); | |
3808 if (arguments && arguments->dim) | |
3809 { | |
3810 buf->writeByte('('); | |
3811 argsToCBuffer(buf, arguments, hgs); | |
3812 buf->writeByte(')'); | |
3813 } | |
3814 //buf->writestring(" { }"); | |
3815 if (cd) | |
3816 { | |
3817 cd->toCBuffer(buf, hgs); | |
3818 } | |
3819 } | |
3820 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3821 /********************** SymbolExp **************************************/ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3822 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3823 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3824 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
|
3825 : Expression(loc, op, size) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3826 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3827 assert(var); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3828 this->var = var; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3829 this->hasOverloads = hasOverloads; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3830 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3831 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3832 |
159 | 3833 /********************** SymOffExp **************************************/ |
3834 | |
3835 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3836 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3837 { | |
3838 assert(var); | |
3839 this->var = var; | |
3840 this->offset = offset; | |
3841 VarDeclaration *v = var->isVarDeclaration(); | |
3842 if (v && v->needThis()) | |
3843 error("need 'this' for address of %s", v->toChars()); | |
3844 } | |
3845 | |
3846 Expression *SymOffExp::semantic(Scope *sc) | |
3847 { | |
3848 #if LOGSEMANTIC | |
3849 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3850 #endif | |
3851 //var->semantic(sc); | |
3852 if (!type) | |
3853 type = var->type->pointerTo(); | |
3854 VarDeclaration *v = var->isVarDeclaration(); | |
3855 if (v) | |
3856 { | |
3857 v->checkNestedReference(sc, loc); | |
3858 } | |
3859 return this; | |
3860 } | |
3861 | |
3862 int SymOffExp::isBool(int result) | |
3863 { | |
3864 return result ? TRUE : FALSE; | |
3865 } | |
3866 | |
3867 void SymOffExp::checkEscape() | |
3868 { | |
3869 VarDeclaration *v = var->isVarDeclaration(); | |
3870 if (v) | |
3871 { | |
3872 if (!v->isDataseg()) | |
3873 error("escaping reference to local variable %s", v->toChars()); | |
3874 } | |
3875 } | |
3876 | |
3877 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3878 { | |
3879 if (offset) | |
3880 buf->printf("(& %s+%u)", var->toChars(), offset); | |
3881 else | |
3882 buf->printf("& %s", var->toChars()); | |
3883 } | |
3884 | |
3885 /******************************** VarExp **************************/ | |
3886 | |
3887 VarExp::VarExp(Loc loc, Declaration *var) | |
3888 : Expression(loc, TOKvar, sizeof(VarExp)) | |
3889 { | |
3890 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
3891 this->var = var; | |
3892 this->type = var->type; | |
3893 } | |
3894 | |
3895 int VarExp::equals(Object *o) | |
3896 { VarExp *ne; | |
3897 | |
3898 if (this == o || | |
3899 (((Expression *)o)->op == TOKvar && | |
3900 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
3901 var == ne->var)) | |
3902 return 1; | |
3903 return 0; | |
3904 } | |
3905 | |
3906 Expression *VarExp::semantic(Scope *sc) | |
3907 { FuncLiteralDeclaration *fd; | |
3908 | |
3909 #if LOGSEMANTIC | |
3910 printf("VarExp::semantic(%s)\n", toChars()); | |
3911 #endif | |
3912 if (!type) | |
3913 { type = var->type; | |
3914 #if 0 | |
3915 if (var->storage_class & STClazy) | |
3916 { | |
3917 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
3918 type = new TypeDelegate(tf); | |
3919 type = type->semantic(loc, sc); | |
3920 } | |
3921 #endif | |
3922 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3923 /* 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
|
3924 * problems when instantiating imported templates passing private |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3925 * variables as alias template parameters. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3926 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3927 //accessCheck(loc, sc, NULL, var); |
159 | 3928 |
3929 VarDeclaration *v = var->isVarDeclaration(); | |
3930 if (v) | |
3931 { | |
3932 if (v->isConst() && type->toBasetype()->ty != Tsarray && v->init) | |
3933 { | |
3934 ExpInitializer *ei = v->init->isExpInitializer(); | |
3935 if (ei) | |
3936 { | |
3937 //ei->exp->implicitCastTo(sc, type)->print(); | |
3938 return ei->exp->implicitCastTo(sc, type); | |
3939 } | |
3940 } | |
3941 v->checkNestedReference(sc, loc); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3942 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3943 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
|
3944 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3945 if (v->isDataseg() && !v->isInvariant()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3946 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
|
3947 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3948 #endif |
159 | 3949 } |
3950 #if 0 | |
3951 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
3952 { Expression *e; | |
3953 e = new FuncExp(loc, fd); | |
3954 e->type = type; | |
3955 return e; | |
3956 } | |
3957 #endif | |
3958 return this; | |
3959 } | |
3960 | |
3961 char *VarExp::toChars() | |
3962 { | |
3963 return var->toChars(); | |
3964 } | |
3965 | |
3966 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3967 { | |
3968 buf->writestring(var->toChars()); | |
3969 } | |
3970 | |
3971 void VarExp::checkEscape() | |
3972 { | |
3973 VarDeclaration *v = var->isVarDeclaration(); | |
3974 if (v) | |
3975 { Type *tb = v->type->toBasetype(); | |
3976 // if reference type | |
3977 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
3978 { | |
3979 if ((v->isAuto() || v->isScope()) && !v->noauto) | |
3980 error("escaping reference to auto local %s", v->toChars()); | |
3981 else if (v->storage_class & STCvariadic) | |
3982 error("escaping reference to variadic parameter %s", v->toChars()); | |
3983 } | |
3984 } | |
3985 } | |
3986 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3987 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3988 int VarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3989 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3990 if (var->storage_class & STClazy) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3991 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3992 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3993 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3994 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3995 |
159 | 3996 Expression *VarExp::toLvalue(Scope *sc, Expression *e) |
3997 { | |
3998 #if 0 | |
3999 tym = tybasic(e1->ET->Tty); | |
4000 if (!(tyscalar(tym) || | |
4001 tym == TYstruct || | |
4002 tym == TYarray && e->Eoper == TOKaddr)) | |
4003 synerr(EM_lvalue); // lvalue expected | |
4004 #endif | |
4005 if (var->storage_class & STClazy) | |
4006 error("lazy variables cannot be lvalues"); | |
4007 return this; | |
4008 } | |
4009 | |
4010 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
4011 { | |
4012 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
4013 if (sc->incontract && var->isParameter()) | |
4014 error("cannot modify parameter '%s' in contract", var->toChars()); | |
4015 | |
4016 if (type && type->toBasetype()->ty == Tsarray) | |
4017 error("cannot change reference to static array '%s'", var->toChars()); | |
4018 | |
4019 VarDeclaration *v = var->isVarDeclaration(); | |
4020 if (v && v->canassign == 0 && | |
4021 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
4022 error("cannot modify final variable '%s'", var->toChars()); | |
4023 | |
4024 if (var->isCtorinit()) | |
4025 { // It's only modifiable if inside the right constructor | |
4026 Dsymbol *s = sc->func; | |
4027 while (1) | |
4028 { | |
4029 FuncDeclaration *fd = NULL; | |
4030 if (s) | |
4031 fd = s->isFuncDeclaration(); | |
4032 if (fd && | |
4033 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
4034 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
4035 fd->toParent() == var->toParent() | |
4036 ) | |
4037 { | |
4038 VarDeclaration *v = var->isVarDeclaration(); | |
4039 assert(v); | |
4040 v->ctorinit = 1; | |
4041 //printf("setting ctorinit\n"); | |
4042 } | |
4043 else | |
4044 { | |
4045 if (s) | |
4046 { s = s->toParent2(); | |
4047 continue; | |
4048 } | |
4049 else | |
4050 { | |
4051 const char *p = var->isStatic() ? "static " : ""; | |
4052 error("can only initialize %sconst %s inside %sconstructor", | |
4053 p, var->toChars(), p); | |
4054 } | |
4055 } | |
4056 break; | |
4057 } | |
4058 } | |
4059 | |
4060 // See if this expression is a modifiable lvalue (i.e. not const) | |
4061 return toLvalue(sc, e); | |
4062 } | |
4063 | |
4064 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4065 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4066 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4067 #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
|
4068 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
|
4069 : 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
|
4070 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4071 //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
|
4072 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
|
4073 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
|
4074 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4075 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4076 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
|
4077 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4078 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
|
4079 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4080 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4081 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
|
4082 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4083 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
|
4084 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4085 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4086 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4087 |
159 | 4088 /******************************** TupleExp **************************/ |
4089 | |
4090 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
4091 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4092 { | |
4093 //printf("TupleExp(this = %p)\n", this); | |
4094 this->exps = exps; | |
4095 this->type = NULL; | |
4096 } | |
4097 | |
4098 | |
4099 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
4100 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4101 { | |
4102 exps = new Expressions(); | |
4103 type = NULL; | |
4104 | |
4105 exps->reserve(tup->objects->dim); | |
4106 for (size_t i = 0; i < tup->objects->dim; i++) | |
4107 { Object *o = (Object *)tup->objects->data[i]; | |
4108 if (o->dyncast() == DYNCAST_EXPRESSION) | |
4109 { | |
4110 Expression *e = (Expression *)o; | |
4111 e = e->syntaxCopy(); | |
4112 exps->push(e); | |
4113 } | |
4114 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
4115 { | |
4116 Dsymbol *s = (Dsymbol *)o; | |
4117 Expression *e = new DsymbolExp(loc, s); | |
4118 exps->push(e); | |
4119 } | |
4120 else if (o->dyncast() == DYNCAST_TYPE) | |
4121 { | |
4122 Type *t = (Type *)o; | |
4123 Expression *e = new TypeExp(loc, t); | |
4124 exps->push(e); | |
4125 } | |
4126 else | |
4127 { | |
4128 error("%s is not an expression", o->toChars()); | |
4129 } | |
4130 } | |
4131 } | |
4132 | |
4133 int TupleExp::equals(Object *o) | |
4134 { TupleExp *ne; | |
4135 | |
4136 if (this == o) | |
4137 return 1; | |
4138 if (((Expression *)o)->op == TOKtuple) | |
4139 { | |
4140 TupleExp *te = (TupleExp *)o; | |
4141 if (exps->dim != te->exps->dim) | |
4142 return 0; | |
4143 for (size_t i = 0; i < exps->dim; i++) | |
4144 { Expression *e1 = (Expression *)exps->data[i]; | |
4145 Expression *e2 = (Expression *)te->exps->data[i]; | |
4146 | |
4147 if (!e1->equals(e2)) | |
4148 return 0; | |
4149 } | |
4150 return 1; | |
4151 } | |
4152 return 0; | |
4153 } | |
4154 | |
4155 Expression *TupleExp::syntaxCopy() | |
4156 { | |
4157 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
4158 } | |
4159 | |
4160 Expression *TupleExp::semantic(Scope *sc) | |
4161 { | |
4162 #if LOGSEMANTIC | |
4163 printf("+TupleExp::semantic(%s)\n", toChars()); | |
4164 #endif | |
4165 if (type) | |
4166 return this; | |
4167 | |
4168 // Run semantic() on each argument | |
4169 for (size_t i = 0; i < exps->dim; i++) | |
4170 { Expression *e = (Expression *)exps->data[i]; | |
4171 | |
4172 e = e->semantic(sc); | |
4173 if (!e->type) | |
4174 { error("%s has no value", e->toChars()); | |
4175 e->type = Type::terror; | |
4176 } | |
4177 exps->data[i] = (void *)e; | |
4178 } | |
4179 | |
4180 expandTuples(exps); | |
4181 if (0 && exps->dim == 1) | |
4182 { | |
4183 return (Expression *)exps->data[0]; | |
4184 } | |
4185 type = new TypeTuple(exps); | |
4186 //printf("-TupleExp::semantic(%s)\n", toChars()); | |
4187 return this; | |
4188 } | |
4189 | |
4190 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4191 { | |
4192 buf->writestring("tuple("); | |
4193 argsToCBuffer(buf, exps, hgs); | |
4194 buf->writeByte(')'); | |
4195 } | |
4196 | |
4197 int TupleExp::checkSideEffect(int flag) | |
4198 { int f = 0; | |
4199 | |
4200 for (int i = 0; i < exps->dim; i++) | |
4201 { Expression *e = (Expression *)exps->data[i]; | |
4202 | |
4203 f |= e->checkSideEffect(2); | |
4204 } | |
4205 if (flag == 0 && f == 0) | |
4206 Expression::checkSideEffect(0); | |
4207 return f; | |
4208 } | |
4209 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4210 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4211 int TupleExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4212 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4213 return arrayExpressionCanThrow(exps); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4214 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4215 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4216 |
159 | 4217 void TupleExp::checkEscape() |
4218 { | |
4219 for (size_t i = 0; i < exps->dim; i++) | |
4220 { Expression *e = (Expression *)exps->data[i]; | |
4221 e->checkEscape(); | |
4222 } | |
4223 } | |
4224 | |
4225 /******************************** FuncExp *********************************/ | |
4226 | |
4227 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4228 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4229 { | |
4230 this->fd = fd; | |
4231 } | |
4232 | |
4233 Expression *FuncExp::syntaxCopy() | |
4234 { | |
4235 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4236 } | |
4237 | |
4238 Expression *FuncExp::semantic(Scope *sc) | |
4239 { | |
4240 #if LOGSEMANTIC | |
4241 printf("FuncExp::semantic(%s)\n", toChars()); | |
4242 #endif | |
4243 if (!type) | |
4244 { | |
4245 fd->semantic(sc); | |
4246 fd->parent = sc->parent; | |
4247 if (global.errors) | |
4248 { | |
4249 if (!fd->type->next) | |
4250 fd->type->next = Type::terror; | |
4251 } | |
4252 else | |
4253 { | |
4254 fd->semantic2(sc); | |
4255 if (!global.errors) | |
4256 { | |
4257 fd->semantic3(sc); | |
4258 | |
4259 if (!global.errors && global.params.useInline) | |
4260 fd->inlineScan(); | |
4261 } | |
4262 } | |
4263 | |
4264 // Type is a "delegate to" or "pointer to" the function literal | |
4265 if (fd->isNested()) | |
4266 { | |
4267 type = new TypeDelegate(fd->type); | |
4268 type = type->semantic(loc, sc); | |
4269 } | |
4270 else | |
4271 { | |
4272 type = fd->type->pointerTo(); | |
4273 } | |
4274 } | |
4275 return this; | |
4276 } | |
4277 | |
4278 char *FuncExp::toChars() | |
4279 { | |
4280 return fd->toChars(); | |
4281 } | |
4282 | |
4283 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4284 { | |
4285 buf->writestring(fd->toChars()); | |
4286 } | |
4287 | |
4288 | |
4289 /******************************** DeclarationExp **************************/ | |
4290 | |
4291 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4292 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4293 { | |
4294 this->declaration = declaration; | |
4295 } | |
4296 | |
4297 Expression *DeclarationExp::syntaxCopy() | |
4298 { | |
4299 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4300 } | |
4301 | |
4302 Expression *DeclarationExp::semantic(Scope *sc) | |
4303 { | |
4304 if (type) | |
4305 return this; | |
4306 | |
4307 #if LOGSEMANTIC | |
4308 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4309 #endif | |
4310 | |
4311 /* This is here to support extern(linkage) declaration, | |
4312 * where the extern(linkage) winds up being an AttribDeclaration | |
4313 * wrapper. | |
4314 */ | |
4315 Dsymbol *s = declaration; | |
4316 | |
4317 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4318 if (ad) | |
4319 { | |
4320 if (ad->decl && ad->decl->dim == 1) | |
4321 s = (Dsymbol *)ad->decl->data[0]; | |
4322 } | |
4323 | |
4324 if (s->isVarDeclaration()) | |
4325 { // Do semantic() on initializer first, so: | |
4326 // int a = a; | |
4327 // will be illegal. | |
4328 declaration->semantic(sc); | |
4329 s->parent = sc->parent; | |
4330 } | |
4331 | |
4332 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4333 // Insert into both local scope and function scope. | |
4334 // Must be unique in both. | |
4335 if (s->ident) | |
4336 { | |
4337 if (!sc->insert(s)) | |
4338 error("declaration %s is already defined", s->toPrettyChars()); | |
4339 else if (sc->func) | |
4340 { VarDeclaration *v = s->isVarDeclaration(); | |
4341 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4342 !sc->func->localsymtab->insert(s)) | |
4343 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4344 else if (!global.params.useDeprecated) | |
4345 { // Disallow shadowing | |
4346 | |
4347 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4348 { Dsymbol *s2; | |
4349 | |
4350 if (scx->scopesym && scx->scopesym->symtab && | |
4351 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4352 s != s2) | |
4353 { | |
4354 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4355 } | |
4356 } | |
4357 } | |
4358 } | |
4359 } | |
4360 if (!s->isVarDeclaration()) | |
4361 { | |
4362 declaration->semantic(sc); | |
4363 s->parent = sc->parent; | |
4364 } | |
4365 if (!global.errors) | |
4366 { | |
4367 declaration->semantic2(sc); | |
4368 if (!global.errors) | |
4369 { | |
4370 declaration->semantic3(sc); | |
4371 | |
4372 if (!global.errors && global.params.useInline) | |
4373 declaration->inlineScan(); | |
4374 } | |
4375 } | |
4376 | |
4377 type = Type::tvoid; | |
4378 return this; | |
4379 } | |
4380 | |
4381 int DeclarationExp::checkSideEffect(int flag) | |
4382 { | |
4383 return 1; | |
4384 } | |
4385 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4386 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4387 int DeclarationExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4388 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4389 VarDeclaration *v = declaration->isVarDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4390 if (v && v->init) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4391 { ExpInitializer *ie = v->init->isExpInitializer(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4392 return ie && ie->exp->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4393 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4394 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4395 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4396 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4397 |
159 | 4398 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4399 { | |
4400 declaration->toCBuffer(buf, hgs); | |
4401 } | |
4402 | |
4403 | |
4404 /************************ TypeidExp ************************************/ | |
4405 | |
4406 /* | |
4407 * typeid(int) | |
4408 */ | |
4409 | |
4410 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4411 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4412 { | |
4413 this->typeidType = typeidType; | |
4414 } | |
4415 | |
4416 | |
4417 Expression *TypeidExp::syntaxCopy() | |
4418 { | |
4419 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4420 } | |
4421 | |
4422 | |
4423 Expression *TypeidExp::semantic(Scope *sc) | |
4424 { Expression *e; | |
4425 | |
4426 #if LOGSEMANTIC | |
4427 printf("TypeidExp::semantic()\n"); | |
4428 #endif | |
4429 typeidType = typeidType->semantic(loc, sc); | |
4430 e = typeidType->getTypeInfo(sc); | |
336 | 4431 if (e->loc.linnum == 0) |
4432 e->loc = loc; // so there's at least some line number info | |
159 | 4433 return e; |
4434 } | |
4435 | |
4436 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4437 { | |
4438 buf->writestring("typeid("); | |
4439 typeidType->toCBuffer(buf, NULL, hgs); | |
4440 buf->writeByte(')'); | |
4441 } | |
4442 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4443 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4444 #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
|
4445 /* |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4446 * __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
|
4447 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4448 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4449 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
|
4450 : 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
|
4451 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4452 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
|
4453 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
|
4454 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4455 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4456 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4457 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
|
4458 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4459 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
|
4460 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4461 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4462 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4463 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
|
4464 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4465 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
|
4466 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
|
4467 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
|
4468 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4469 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
|
4470 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4471 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
|
4472 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
|
4473 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
|
4474 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4475 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4476 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
|
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 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4479 |
159 | 4480 /************************************************************/ |
4481 | |
4482 HaltExp::HaltExp(Loc loc) | |
4483 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4484 { | |
4485 } | |
4486 | |
4487 Expression *HaltExp::semantic(Scope *sc) | |
4488 { | |
4489 #if LOGSEMANTIC | |
4490 printf("HaltExp::semantic()\n"); | |
4491 #endif | |
4492 type = Type::tvoid; | |
4493 return this; | |
4494 } | |
4495 | |
4496 int HaltExp::checkSideEffect(int flag) | |
4497 { | |
4498 return 1; | |
4499 } | |
4500 | |
4501 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4502 { | |
4503 buf->writestring("halt"); | |
4504 } | |
4505 | |
4506 /************************************************************/ | |
4507 | |
4508 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4509 Type *tspec, enum TOK tok2) | |
4510 : Expression(loc, TOKis, sizeof(IsExp)) | |
4511 { | |
4512 this->targ = targ; | |
4513 this->id = id; | |
4514 this->tok = tok; | |
4515 this->tspec = tspec; | |
4516 this->tok2 = tok2; | |
4517 } | |
4518 | |
4519 Expression *IsExp::syntaxCopy() | |
4520 { | |
4521 return new IsExp(loc, | |
4522 targ->syntaxCopy(), | |
4523 id, | |
4524 tok, | |
4525 tspec ? tspec->syntaxCopy() : NULL, | |
4526 tok2); | |
4527 } | |
4528 | |
4529 Expression *IsExp::semantic(Scope *sc) | |
4530 { Type *tded; | |
4531 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4532 /* 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
|
4533 * 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
|
4534 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4535 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4536 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4537 if (id && !(sc->flags & SCOPEstaticif)) |
4538 error("can only declare type aliases within static if conditionals"); | |
4539 | |
4540 unsigned errors_save = global.errors; | |
4541 global.errors = 0; | |
4542 global.gag++; // suppress printing of error messages | |
4543 targ = targ->semantic(loc, sc); | |
4544 global.gag--; | |
4545 unsigned gerrors = global.errors; | |
4546 global.errors = errors_save; | |
4547 | |
4548 if (gerrors) // if any errors happened | |
4549 { // then condition is false | |
4550 goto Lno; | |
4551 } | |
4552 else if (tok2 != TOKreserved) | |
4553 { | |
4554 switch (tok2) | |
4555 { | |
4556 case TOKtypedef: | |
4557 if (targ->ty != Ttypedef) | |
4558 goto Lno; | |
4559 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4560 break; | |
4561 | |
4562 case TOKstruct: | |
4563 if (targ->ty != Tstruct) | |
4564 goto Lno; | |
4565 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4566 goto Lno; | |
4567 tded = targ; | |
4568 break; | |
4569 | |
4570 case TOKunion: | |
4571 if (targ->ty != Tstruct) | |
4572 goto Lno; | |
4573 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4574 goto Lno; | |
4575 tded = targ; | |
4576 break; | |
4577 | |
4578 case TOKclass: | |
4579 if (targ->ty != Tclass) | |
4580 goto Lno; | |
4581 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4582 goto Lno; | |
4583 tded = targ; | |
4584 break; | |
4585 | |
4586 case TOKinterface: | |
4587 if (targ->ty != Tclass) | |
4588 goto Lno; | |
4589 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4590 goto Lno; | |
4591 tded = targ; | |
4592 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
|
4593 #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
|
4594 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
|
4595 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
|
4596 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
|
4597 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
|
4598 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4599 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4600 case TOKinvariant: |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4601 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
|
4602 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
|
4603 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
|
4604 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
|
4605 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4606 #endif |
159 | 4607 |
4608 case TOKsuper: | |
4609 // If class or interface, get the base class and interfaces | |
4610 if (targ->ty != Tclass) | |
4611 goto Lno; | |
4612 else | |
4613 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
4614 Arguments *args = new Arguments; | |
4615 args->reserve(cd->baseclasses.dim); | |
4616 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4617 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
4618 args->push(new Argument(STCin, b->type, NULL, NULL)); | |
4619 } | |
4620 tded = new TypeTuple(args); | |
4621 } | |
4622 break; | |
4623 | |
4624 case TOKenum: | |
4625 if (targ->ty != Tenum) | |
4626 goto Lno; | |
4627 tded = ((TypeEnum *)targ)->sym->memtype; | |
4628 break; | |
4629 | |
4630 case TOKdelegate: | |
4631 if (targ->ty != Tdelegate) | |
4632 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
|
4633 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4634 break; |
4635 | |
4636 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
|
4637 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4638 if (targ->ty != Tfunction) |
159 | 4639 goto Lno; |
4640 tded = targ; | |
4641 | |
4642 /* Generate tuple from function parameter types. | |
4643 */ | |
4644 assert(tded->ty == Tfunction); | |
4645 Arguments *params = ((TypeFunction *)tded)->parameters; | |
4646 size_t dim = Argument::dim(params); | |
4647 Arguments *args = new Arguments; | |
4648 args->reserve(dim); | |
4649 for (size_t i = 0; i < dim; i++) | |
4650 { Argument *arg = Argument::getNth(params, i); | |
4651 assert(arg && arg->type); | |
4652 args->push(new Argument(arg->storageClass, arg->type, NULL, NULL)); | |
4653 } | |
4654 tded = new TypeTuple(args); | |
4655 break; | |
4656 } | |
4657 case TOKreturn: | |
4658 /* Get the 'return type' for the function, | |
4659 * delegate, or pointer to function. | |
4660 */ | |
4661 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
|
4662 tded = ((TypeFunction *)targ)->next; |
159 | 4663 else if (targ->ty == Tdelegate) |
4664 tded = targ->next->next; | |
4665 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4666 tded = targ->next->next; | |
4667 else | |
4668 goto Lno; | |
4669 break; | |
4670 | |
4671 default: | |
4672 assert(0); | |
4673 } | |
4674 goto Lyes; | |
4675 } | |
4676 else if (id && tspec) | |
4677 { | |
4678 /* Evaluate to TRUE if targ matches tspec. | |
4679 * If TRUE, declare id as an alias for the specialized type. | |
4680 */ | |
4681 | |
4682 MATCH m; | |
4683 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4684 | |
4685 TemplateParameters parameters; | |
4686 parameters.setDim(1); | |
4687 parameters.data[0] = (void *)&tp; | |
4688 | |
4689 Objects dedtypes; | |
4690 dedtypes.setDim(1); | |
4691 dedtypes.data[0] = NULL; | |
4692 | |
4693 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4694 if (m == MATCHnomatch || | |
4695 (m != MATCHexact && tok == TOKequal)) | |
4696 goto Lno; | |
4697 else | |
4698 { | |
4699 assert(dedtypes.dim == 1); | |
4700 tded = (Type *)dedtypes.data[0]; | |
4701 if (!tded) | |
4702 tded = targ; | |
4703 goto Lyes; | |
4704 } | |
4705 } | |
4706 else if (id) | |
4707 { | |
4708 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4709 */ | |
4710 tded = targ; | |
4711 goto Lyes; | |
4712 } | |
4713 else if (tspec) | |
4714 { | |
4715 /* Evaluate to TRUE if targ matches tspec | |
4716 */ | |
4717 tspec = tspec->semantic(loc, sc); | |
4718 //printf("targ = %s\n", targ->toChars()); | |
4719 //printf("tspec = %s\n", tspec->toChars()); | |
4720 if (tok == TOKcolon) | |
4721 { if (targ->implicitConvTo(tspec)) | |
4722 goto Lyes; | |
4723 else | |
4724 goto Lno; | |
4725 } | |
4726 else /* == */ | |
4727 { if (targ->equals(tspec)) | |
4728 goto Lyes; | |
4729 else | |
4730 goto Lno; | |
4731 } | |
4732 } | |
4733 | |
4734 Lyes: | |
4735 if (id) | |
4736 { | |
4737 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4738 s->semantic(sc); | |
4739 sc->insert(s); | |
4740 if (sc->sd) | |
4741 s->addMember(sc, sc->sd, 1); | |
4742 } | |
4743 return new IntegerExp(1); | |
4744 | |
4745 Lno: | |
4746 return new IntegerExp(0); | |
4747 } | |
4748 | |
4749 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4750 { | |
4751 buf->writestring("is("); | |
4752 targ->toCBuffer(buf, id, hgs); | |
4753 if (tok2 != TOKreserved) | |
4754 { | |
4755 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4756 } | |
4757 else if (tspec) | |
4758 { | |
4759 if (tok == TOKcolon) | |
4760 buf->writestring(" : "); | |
4761 else | |
4762 buf->writestring(" == "); | |
4763 tspec->toCBuffer(buf, NULL, hgs); | |
4764 } | |
336 | 4765 #if DMDV2 |
159 | 4766 if (parameters) |
4767 { // First parameter is already output, so start with second | |
4768 for (int i = 1; i < parameters->dim; i++) | |
4769 { | |
4770 buf->writeByte(','); | |
4771 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4772 tp->toCBuffer(buf, hgs); | |
4773 } | |
4774 } | |
4775 #endif | |
4776 buf->writeByte(')'); | |
4777 } | |
4778 | |
4779 | |
4780 /************************************************************/ | |
4781 | |
4782 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4783 : Expression(loc, op, size) | |
4784 { | |
4785 this->e1 = e1; | |
4786 } | |
4787 | |
4788 Expression *UnaExp::syntaxCopy() | |
4789 { UnaExp *e; | |
4790 | |
4791 e = (UnaExp *)copy(); | |
4792 e->type = NULL; | |
4793 e->e1 = e->e1->syntaxCopy(); | |
4794 return e; | |
4795 } | |
4796 | |
4797 Expression *UnaExp::semantic(Scope *sc) | |
4798 { | |
4799 #if LOGSEMANTIC | |
4800 printf("UnaExp::semantic('%s')\n", toChars()); | |
4801 #endif | |
4802 e1 = e1->semantic(sc); | |
4803 // if (!e1->type) | |
4804 // error("%s has no value", e1->toChars()); | |
4805 return this; | |
4806 } | |
4807 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4808 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4809 int UnaExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4810 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4811 return e1->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4812 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4813 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4814 |
159 | 4815 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4816 { | |
4817 buf->writestring(Token::toChars(op)); | |
4818 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4819 } | |
4820 | |
4821 /************************************************************/ | |
4822 | |
4823 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4824 : Expression(loc, op, size) | |
4825 { | |
4826 this->e1 = e1; | |
4827 this->e2 = e2; | |
4828 } | |
4829 | |
4830 Expression *BinExp::syntaxCopy() | |
4831 { BinExp *e; | |
4832 | |
4833 e = (BinExp *)copy(); | |
4834 e->type = NULL; | |
4835 e->e1 = e->e1->syntaxCopy(); | |
4836 e->e2 = e->e2->syntaxCopy(); | |
4837 return e; | |
4838 } | |
4839 | |
4840 Expression *BinExp::semantic(Scope *sc) | |
4841 { | |
4842 #if LOGSEMANTIC | |
4843 printf("BinExp::semantic('%s')\n", toChars()); | |
4844 #endif | |
4845 e1 = e1->semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4846 if (!e1->type && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
4847 !(op == TOKassign && e1->op == TOKdottd)) // a.template = e2 |
159 | 4848 { |
4849 error("%s has no value", e1->toChars()); | |
4850 e1->type = Type::terror; | |
4851 } | |
4852 e2 = e2->semantic(sc); | |
4853 if (!e2->type) | |
4854 { | |
4855 error("%s has no value", e2->toChars()); | |
4856 e2->type = Type::terror; | |
4857 } | |
4858 return this; | |
4859 } | |
4860 | |
4861 Expression *BinExp::semanticp(Scope *sc) | |
4862 { | |
4863 BinExp::semantic(sc); | |
4864 e1 = resolveProperties(sc, e1); | |
4865 e2 = resolveProperties(sc, e2); | |
4866 return this; | |
4867 } | |
4868 | |
4869 /*************************** | |
4870 * Common semantic routine for some xxxAssignExp's. | |
4871 */ | |
4872 | |
4873 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
4874 { Expression *e; | |
4875 | |
4876 if (!type) | |
4877 { | |
4878 BinExp::semantic(sc); | |
4879 e2 = resolveProperties(sc, e2); | |
4880 | |
4881 e = op_overload(sc); | |
4882 if (e) | |
4883 return e; | |
4884 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4885 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
|
4886 { // 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
|
4887 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
|
4888 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
|
4889 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
|
4890 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4891 |
159 | 4892 e1 = e1->modifiableLvalue(sc, e1); |
4893 e1->checkScalar(); | |
4894 type = e1->type; | |
4895 if (type->toBasetype()->ty == Tbool) | |
4896 { | |
4897 error("operator not allowed on bool expression %s", toChars()); | |
4898 } | |
4899 typeCombine(sc); | |
4900 e1->checkArithmetic(); | |
4901 e2->checkArithmetic(); | |
4902 | |
4903 if (op == TOKmodass && e2->type->iscomplex()) | |
4904 { error("cannot perform modulo complex arithmetic"); | |
4905 return new IntegerExp(0); | |
4906 } | |
4907 } | |
4908 return this; | |
4909 } | |
4910 | |
4911 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
4912 { Expression *e; | |
4913 | |
4914 if (!type) | |
4915 { | |
4916 BinExp::semantic(sc); | |
4917 e2 = resolveProperties(sc, e2); | |
4918 | |
4919 e = op_overload(sc); | |
4920 if (e) | |
4921 return e; | |
4922 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4923 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
|
4924 { // 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
|
4925 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
|
4926 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
|
4927 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
|
4928 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4929 |
159 | 4930 e1 = e1->modifiableLvalue(sc, e1); |
4931 e1->checkScalar(); | |
4932 type = e1->type; | |
4933 if (type->toBasetype()->ty == Tbool) | |
4934 { | |
4935 e2 = e2->implicitCastTo(sc, type); | |
4936 } | |
4937 | |
4938 typeCombine(sc); | |
4939 e1->checkIntegral(); | |
4940 e2->checkIntegral(); | |
4941 } | |
4942 return this; | |
4943 } | |
4944 | |
4945 int BinExp::checkSideEffect(int flag) | |
4946 { | |
4947 if (op == TOKplusplus || | |
4948 op == TOKminusminus || | |
4949 op == TOKassign || | |
4950 op == TOKconstruct || | |
4951 op == TOKblit || | |
4952 op == TOKaddass || | |
4953 op == TOKminass || | |
4954 op == TOKcatass || | |
4955 op == TOKmulass || | |
4956 op == TOKdivass || | |
4957 op == TOKmodass || | |
4958 op == TOKshlass || | |
4959 op == TOKshrass || | |
4960 op == TOKushrass || | |
4961 op == TOKandass || | |
4962 op == TOKorass || | |
4963 op == TOKxorass || | |
4964 op == TOKin || | |
4965 op == TOKremove) | |
4966 return 1; | |
4967 return Expression::checkSideEffect(flag); | |
4968 } | |
4969 | |
4970 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4971 { | |
4972 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4973 buf->writeByte(' '); | |
4974 buf->writestring(Token::toChars(op)); | |
4975 buf->writeByte(' '); | |
4976 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
4977 } | |
4978 | |
4979 int BinExp::isunsigned() | |
4980 { | |
4981 return e1->type->isunsigned() || e2->type->isunsigned(); | |
4982 } | |
4983 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4984 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4985 int BinExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4986 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4987 return e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4988 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4989 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4990 |
159 | 4991 void BinExp::incompatibleTypes() |
4992 { | |
4993 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
4994 e1->toChars(), Token::toChars(op), e2->toChars(), | |
4995 e1->type->toChars(), e2->type->toChars()); | |
4996 } | |
4997 | |
4998 /************************************************************/ | |
4999 | |
5000 CompileExp::CompileExp(Loc loc, Expression *e) | |
5001 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
5002 { | |
5003 } | |
5004 | |
5005 Expression *CompileExp::semantic(Scope *sc) | |
5006 { | |
5007 #if LOGSEMANTIC | |
5008 printf("CompileExp::semantic('%s')\n", toChars()); | |
5009 #endif | |
5010 UnaExp::semantic(sc); | |
5011 e1 = resolveProperties(sc, e1); | |
5012 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
5013 if (e1->op != TOKstring) | |
5014 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
5015 type = Type::terror; | |
5016 return this; | |
5017 } | |
5018 StringExp *se = (StringExp *)e1; | |
5019 se = se->toUTF8(sc); | |
5020 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
5021 p.loc = loc; | |
5022 p.nextToken(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5023 //printf("p.loc.linnum = %d\n", p.loc.linnum); |
159 | 5024 Expression *e = p.parseExpression(); |
5025 if (p.token.value != TOKeof) | |
5026 error("incomplete mixin expression (%s)", se->toChars()); | |
5027 return e->semantic(sc); | |
5028 } | |
5029 | |
5030 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5031 { | |
5032 buf->writestring("mixin("); | |
5033 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5034 buf->writeByte(')'); | |
5035 } | |
5036 | |
5037 /************************************************************/ | |
5038 | |
5039 FileExp::FileExp(Loc loc, Expression *e) | |
5040 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
5041 { | |
5042 } | |
5043 | |
5044 Expression *FileExp::semantic(Scope *sc) | |
5045 { char *name; | |
5046 StringExp *se; | |
5047 | |
5048 #if LOGSEMANTIC | |
5049 printf("FileExp::semantic('%s')\n", toChars()); | |
5050 #endif | |
5051 UnaExp::semantic(sc); | |
5052 e1 = resolveProperties(sc, e1); | |
5053 e1 = e1->optimize(WANTvalue); | |
5054 if (e1->op != TOKstring) | |
5055 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
5056 goto Lerror; | |
5057 } | |
5058 se = (StringExp *)e1; | |
5059 se = se->toUTF8(sc); | |
5060 name = (char *)se->string; | |
5061 | |
5062 if (!global.params.fileImppath) | |
5063 { error("need -Jpath switch to import text file %s", name); | |
5064 goto Lerror; | |
5065 } | |
5066 | |
5067 if (name != FileName::name(name)) | |
5068 { error("use -Jpath switch to provide path for filename %s", name); | |
5069 goto Lerror; | |
5070 } | |
5071 | |
5072 name = FileName::searchPath(global.filePath, name, 0); | |
5073 if (!name) | |
5074 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
5075 goto Lerror; | |
5076 } | |
5077 | |
5078 if (global.params.verbose) | |
794
661384d6a936
Fix warnings on x86-64. By fvbommel.
Christian Kamm <kamm incasoftware de>
parents:
779
diff
changeset
|
5079 printf("file %s\t(%s)\n", (char*)se->string, name); |
159 | 5080 |
5081 { File f(name); | |
5082 if (f.read()) | |
5083 { error("cannot read file %s", f.toChars()); | |
5084 goto Lerror; | |
5085 } | |
5086 else | |
5087 { | |
5088 f.ref = 1; | |
5089 se = new StringExp(loc, f.buffer, f.len); | |
5090 } | |
5091 } | |
5092 Lret: | |
5093 return se->semantic(sc); | |
5094 | |
5095 Lerror: | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5096 se = new StringExp(loc, (char *)""); |
159 | 5097 goto Lret; |
5098 } | |
5099 | |
5100 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5101 { | |
5102 buf->writestring("import("); | |
5103 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5104 buf->writeByte(')'); | |
5105 } | |
5106 | |
5107 /************************************************************/ | |
5108 | |
5109 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
5110 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
5111 { | |
5112 this->msg = msg; | |
5113 } | |
5114 | |
5115 Expression *AssertExp::syntaxCopy() | |
5116 { | |
5117 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
5118 msg ? msg->syntaxCopy() : NULL); | |
5119 return ae; | |
5120 } | |
5121 | |
5122 Expression *AssertExp::semantic(Scope *sc) | |
5123 { | |
5124 #if LOGSEMANTIC | |
5125 printf("AssertExp::semantic('%s')\n", toChars()); | |
5126 #endif | |
5127 UnaExp::semantic(sc); | |
5128 e1 = resolveProperties(sc, e1); | |
5129 // BUG: see if we can do compile time elimination of the Assert | |
5130 e1 = e1->optimize(WANTvalue); | |
5131 e1 = e1->checkToBoolean(); | |
5132 if (msg) | |
5133 { | |
5134 msg = msg->semantic(sc); | |
5135 msg = resolveProperties(sc, msg); | |
5136 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
5137 msg = msg->optimize(WANTvalue); | |
5138 } | |
5139 if (e1->isBool(FALSE)) | |
5140 { | |
5141 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
5142 fd->hasReturnExp |= 4; | |
5143 | |
5144 if (!global.params.useAssert) | |
5145 { Expression *e = new HaltExp(loc); | |
5146 e = e->semantic(sc); | |
5147 return e; | |
5148 } | |
5149 } | |
5150 type = Type::tvoid; | |
5151 return this; | |
5152 } | |
5153 | |
5154 int AssertExp::checkSideEffect(int flag) | |
5155 { | |
5156 return 1; | |
5157 } | |
5158 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5159 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5160 int AssertExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5161 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5162 return (global.params.useAssert != 0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5163 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5164 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5165 |
159 | 5166 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
5167 { | |
5168 buf->writestring("assert("); | |
5169 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5170 if (msg) | |
5171 { | |
5172 buf->writeByte(','); | |
5173 expToCBuffer(buf, hgs, msg, PREC_assign); | |
5174 } | |
5175 buf->writeByte(')'); | |
5176 } | |
5177 | |
5178 /************************************************************/ | |
5179 | |
5180 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
5181 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
5182 { | |
5183 this->ident = ident; | |
5184 } | |
5185 | |
5186 Expression *DotIdExp::semantic(Scope *sc) | |
5187 { Expression *e; | |
5188 Expression *eleft; | |
5189 Expression *eright; | |
5190 | |
5191 #if LOGSEMANTIC | |
5192 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
5193 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
5194 #endif | |
5195 | |
5196 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
5197 | |
5198 #if 0 | |
5199 /* Don't do semantic analysis if we'll be converting | |
5200 * it to a string. | |
5201 */ | |
5202 if (ident == Id::stringof) | |
5203 { char *s = e1->toChars(); | |
5204 e = new StringExp(loc, s, strlen(s), 'c'); | |
5205 e = e->semantic(sc); | |
5206 return e; | |
5207 } | |
5208 #endif | |
5209 | |
5210 /* Special case: rewrite this.id and super.id | |
5211 * to be classtype.id and baseclasstype.id | |
5212 * if we have no this pointer. | |
5213 */ | |
5214 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
5215 { ClassDeclaration *cd; | |
5216 StructDeclaration *sd; | |
5217 AggregateDeclaration *ad; | |
5218 | |
5219 ad = sc->getStructClassScope(); | |
5220 if (ad) | |
5221 { | |
5222 cd = ad->isClassDeclaration(); | |
5223 if (cd) | |
5224 { | |
5225 if (e1->op == TOKthis) | |
5226 { | |
5227 e = new TypeDotIdExp(loc, cd->type, ident); | |
5228 return e->semantic(sc); | |
5229 } | |
5230 else if (cd->baseClass && e1->op == TOKsuper) | |
5231 { | |
5232 e = new TypeDotIdExp(loc, cd->baseClass->type, ident); | |
5233 return e->semantic(sc); | |
5234 } | |
5235 } | |
5236 else | |
5237 { | |
5238 sd = ad->isStructDeclaration(); | |
5239 if (sd) | |
5240 { | |
5241 if (e1->op == TOKthis) | |
5242 { | |
5243 e = new TypeDotIdExp(loc, sd->type, ident); | |
5244 return e->semantic(sc); | |
5245 } | |
5246 } | |
5247 } | |
5248 } | |
5249 } | |
5250 | |
5251 UnaExp::semantic(sc); | |
5252 | |
5253 if (e1->op == TOKdotexp) | |
5254 { | |
5255 DotExp *de = (DotExp *)e1; | |
5256 eleft = de->e1; | |
5257 eright = de->e2; | |
5258 } | |
5259 else | |
5260 { | |
5261 e1 = resolveProperties(sc, e1); | |
5262 eleft = NULL; | |
5263 eright = e1; | |
5264 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5265 #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
|
5266 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
|
5267 { /* '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
|
5268 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5269 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
|
5270 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
|
5271 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
|
5272 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
|
5273 { 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
|
5274 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
|
5275 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
|
5276 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
|
5277 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5278 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
|
5279 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
|
5280 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
|
5281 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5282 #endif |
159 | 5283 |
5284 if (e1->op == TOKtuple && ident == Id::length) | |
5285 { | |
5286 TupleExp *te = (TupleExp *)e1; | |
5287 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5288 return e; | |
5289 } | |
5290 | |
5291 if (eright->op == TOKimport) // also used for template alias's | |
5292 { | |
5293 ScopeExp *ie = (ScopeExp *)eright; | |
5294 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5295 /* 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
|
5296 * 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
|
5297 * 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
|
5298 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5299 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
|
5300 (ie->sds->isModule() && ie->sds != sc->module) ? 1 : 0); |
159 | 5301 if (s) |
5302 { | |
5303 s = s->toAlias(); | |
5304 checkDeprecated(sc, s); | |
5305 | |
5306 EnumMember *em = s->isEnumMember(); | |
5307 if (em) | |
5308 { | |
5309 e = em->value; | |
5310 e = e->semantic(sc); | |
5311 return e; | |
5312 } | |
5313 | |
5314 VarDeclaration *v = s->isVarDeclaration(); | |
5315 if (v) | |
5316 { | |
5317 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5318 if (v->inuse) | |
5319 { | |
5320 error("circular reference to '%s'", v->toChars()); | |
5321 type = Type::tint32; | |
5322 return this; | |
5323 } | |
5324 type = v->type; | |
5325 if (v->isConst()) | |
5326 { | |
5327 if (v->init) | |
5328 { | |
5329 ExpInitializer *ei = v->init->isExpInitializer(); | |
5330 if (ei) | |
5331 { | |
5332 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5333 //ei->exp = ei->exp->semantic(sc); | |
5334 if (ei->exp->type == type) | |
5335 { | |
5336 e = ei->exp->copy(); // make copy so we can change loc | |
5337 e->loc = loc; | |
5338 return e; | |
5339 } | |
5340 } | |
5341 } | |
5342 else if (type->isscalar()) | |
5343 { | |
5344 e = type->defaultInit(); | |
5345 e->loc = loc; | |
5346 return e; | |
5347 } | |
5348 } | |
5349 if (v->needThis()) | |
5350 { | |
5351 if (!eleft) | |
5352 eleft = new ThisExp(loc); | |
5353 e = new DotVarExp(loc, eleft, v); | |
5354 e = e->semantic(sc); | |
5355 } | |
5356 else | |
5357 { | |
5358 e = new VarExp(loc, v); | |
5359 if (eleft) | |
5360 { e = new CommaExp(loc, eleft, e); | |
5361 e->type = v->type; | |
5362 } | |
5363 } | |
5364 return e->deref(); | |
5365 } | |
5366 | |
5367 FuncDeclaration *f = s->isFuncDeclaration(); | |
5368 if (f) | |
5369 { | |
5370 //printf("it's a function\n"); | |
5371 if (f->needThis()) | |
5372 { | |
5373 if (!eleft) | |
5374 eleft = new ThisExp(loc); | |
5375 e = new DotVarExp(loc, eleft, f); | |
5376 e = e->semantic(sc); | |
5377 } | |
5378 else | |
5379 { | |
5380 e = new VarExp(loc, f); | |
5381 if (eleft) | |
5382 { e = new CommaExp(loc, eleft, e); | |
5383 e->type = f->type; | |
5384 } | |
5385 } | |
5386 return e; | |
5387 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5388 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5389 OverloadSet *o = s->isOverloadSet(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5390 if (o) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5391 { //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
|
5392 return new OverExp(o); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5393 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5394 #endif |
159 | 5395 |
5396 Type *t = s->getType(); | |
5397 if (t) | |
5398 { | |
5399 return new TypeExp(loc, t); | |
5400 } | |
5401 | |
336 | 5402 TupleDeclaration *tup = s->isTupleDeclaration(); |
5403 if (tup) | |
5404 { | |
5405 if (eleft) | |
5406 error("cannot have e.tuple"); | |
5407 e = new TupleExp(loc, tup); | |
5408 e = e->semantic(sc); | |
5409 return e; | |
5410 } | |
5411 | |
159 | 5412 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5413 if (sds) | |
5414 { | |
5415 //printf("it's a ScopeDsymbol\n"); | |
5416 e = new ScopeExp(loc, sds); | |
5417 e = e->semantic(sc); | |
5418 if (eleft) | |
5419 e = new DotExp(loc, eleft, e); | |
5420 return e; | |
5421 } | |
5422 | |
5423 Import *imp = s->isImport(); | |
5424 if (imp) | |
5425 { | |
5426 ScopeExp *ie; | |
5427 | |
5428 ie = new ScopeExp(loc, imp->pkg); | |
5429 return ie->semantic(sc); | |
5430 } | |
5431 | |
5432 // BUG: handle other cases like in IdentifierExp::semantic() | |
5433 #ifdef DEBUG | |
5434 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5435 #endif | |
5436 assert(0); | |
5437 } | |
5438 else if (ident == Id::stringof) | |
5439 { char *s = ie->toChars(); | |
5440 e = new StringExp(loc, s, strlen(s), 'c'); | |
5441 e = e->semantic(sc); | |
5442 return e; | |
5443 } | |
5444 error("undefined identifier %s", toChars()); | |
5445 type = Type::tvoid; | |
5446 return this; | |
5447 } | |
5448 else if (e1->type->ty == Tpointer && | |
5449 ident != Id::init && ident != Id::__sizeof && | |
5450 ident != Id::alignof && ident != Id::offsetof && | |
5451 ident != Id::mangleof && ident != Id::stringof) | |
5452 { | |
5453 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
|
5454 e->type = ((TypePointer *)e1->type)->next; |
159 | 5455 return e->type->dotExp(sc, e, ident); |
5456 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5457 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5458 else if (t1b->ty == Tarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5459 t1b->ty == Tsarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5460 t1b->ty == Taarray) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5461 { /* 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
|
5462 * e1.ident |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5463 * as: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5464 * .ident(e1) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5465 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5466 unsigned errors = global.errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5467 global.gag++; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5468 e = e1->type->dotExp(sc, e1, ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5469 global.gag--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5470 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
|
5471 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5472 global.errors = errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5473 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
|
5474 e = new CallExp(loc, e, e1); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5475 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5476 e = e->semantic(sc); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5477 return e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5478 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5479 #endif |
159 | 5480 else |
5481 { | |
5482 e = e1->type->dotExp(sc, e1, ident); | |
5483 e = e->semantic(sc); | |
5484 return e; | |
5485 } | |
5486 } | |
5487 | |
5488 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5489 { | |
5490 //printf("DotIdExp::toCBuffer()\n"); | |
5491 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5492 buf->writeByte('.'); | |
5493 buf->writestring(ident->toChars()); | |
5494 } | |
5495 | |
5496 /********************** DotTemplateExp ***********************************/ | |
5497 | |
5498 // Mainly just a placeholder | |
5499 | |
5500 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5501 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5502 | |
5503 { | |
5504 this->td = td; | |
5505 } | |
5506 | |
5507 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5508 { | |
5509 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5510 buf->writeByte('.'); | |
5511 buf->writestring(td->toChars()); | |
5512 } | |
5513 | |
5514 | |
5515 /************************************************************/ | |
5516 | |
5517 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5518 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5519 { | |
5520 //printf("DotVarExp()\n"); | |
5521 this->var = v; | |
5522 } | |
5523 | |
5524 Expression *DotVarExp::semantic(Scope *sc) | |
5525 { | |
5526 #if LOGSEMANTIC | |
5527 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5528 #endif | |
5529 if (!type) | |
5530 { | |
5531 var = var->toAlias()->isDeclaration(); | |
5532 | |
5533 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5534 if (tup) | |
5535 { /* Replace: | |
5536 * e1.tuple(a, b, c) | |
5537 * with: | |
5538 * tuple(e1.a, e1.b, e1.c) | |
5539 */ | |
5540 Expressions *exps = new Expressions; | |
5541 | |
5542 exps->reserve(tup->objects->dim); | |
5543 for (size_t i = 0; i < tup->objects->dim; i++) | |
5544 { Object *o = (Object *)tup->objects->data[i]; | |
5545 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5546 { | |
5547 error("%s is not an expression", o->toChars()); | |
5548 } | |
5549 else | |
5550 { | |
5551 Expression *e = (Expression *)o; | |
5552 if (e->op != TOKdsymbol) | |
5553 error("%s is not a member", e->toChars()); | |
5554 else | |
5555 { DsymbolExp *ve = (DsymbolExp *)e; | |
5556 | |
5557 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5558 exps->push(e); | |
5559 } | |
5560 } | |
5561 } | |
5562 Expression *e = new TupleExp(loc, exps); | |
5563 e = e->semantic(sc); | |
5564 return e; | |
5565 } | |
5566 | |
5567 e1 = e1->semantic(sc); | |
5568 type = var->type; | |
5569 if (!type && global.errors) | |
5570 { // var is goofed up, just return 0 | |
5571 return new IntegerExp(0); | |
5572 } | |
5573 assert(type); | |
5574 | |
5575 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5576 { | |
5577 AggregateDeclaration *ad = var->toParent()->isAggregateDeclaration(); | |
336 | 5578 e1 = getRightThis(loc, sc, ad, e1, var); |
5579 if (!sc->noaccesscheck) | |
5580 accessCheck(loc, sc, e1, var); | |
5581 | |
5582 VarDeclaration *v = var->isVarDeclaration(); | |
5583 if (v && v->isConst()) | |
5584 { ExpInitializer *ei = v->getExpInitializer(); | |
5585 if (ei) | |
5586 { Expression *e = ei->exp->copy(); | |
5587 e = e->semantic(sc); | |
5588 return e; | |
159 | 5589 } |
5590 } | |
5591 } | |
5592 } | |
5593 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5594 return this; | |
5595 } | |
5596 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5597 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5598 int DotVarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5599 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5600 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5601 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5602 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5603 |
159 | 5604 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) |
5605 { | |
5606 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5607 return this; | |
5608 } | |
5609 | |
5610 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5611 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5612 #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
|
5613 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
|
5614 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
|
5615 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
|
5616 #endif |
159 | 5617 |
5618 if (var->isCtorinit()) | |
5619 { // It's only modifiable if inside the right constructor | |
5620 Dsymbol *s = sc->func; | |
5621 while (1) | |
5622 { | |
5623 FuncDeclaration *fd = NULL; | |
5624 if (s) | |
5625 fd = s->isFuncDeclaration(); | |
5626 if (fd && | |
5627 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5628 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5629 fd->toParent() == var->toParent() && | |
5630 e1->op == TOKthis | |
5631 ) | |
5632 { | |
5633 VarDeclaration *v = var->isVarDeclaration(); | |
5634 assert(v); | |
5635 v->ctorinit = 1; | |
5636 //printf("setting ctorinit\n"); | |
5637 } | |
5638 else | |
5639 { | |
5640 if (s) | |
5641 { s = s->toParent2(); | |
5642 continue; | |
5643 } | |
5644 else | |
5645 { | |
5646 const char *p = var->isStatic() ? "static " : ""; | |
5647 error("can only initialize %sconst member %s inside %sconstructor", | |
5648 p, var->toChars(), p); | |
5649 } | |
5650 } | |
5651 break; | |
5652 } | |
5653 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5654 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5655 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5656 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5657 Type *t1 = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5658 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5659 if (!t1->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5660 (t1->ty == Tpointer && !t1->nextOf()->isMutable()) || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5661 !var->type->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5662 !var->type->isAssignable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5663 var->storage_class & STCmanifest |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5664 ) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5665 error("cannot modify const/invariant %s", toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5666 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5667 #endif |
159 | 5668 return this; |
5669 } | |
5670 | |
5671 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5672 { | |
5673 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5674 buf->writeByte('.'); | |
5675 buf->writestring(var->toChars()); | |
5676 } | |
5677 | |
5678 /************************************************************/ | |
5679 | |
5680 /* Things like: | |
5681 * foo.bar!(args) | |
5682 */ | |
5683 | |
5684 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5685 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5686 { | |
5687 //printf("DotTemplateInstanceExp()\n"); | |
5688 this->ti = ti; | |
5689 } | |
5690 | |
5691 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5692 { | |
5693 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5694 e1->syntaxCopy(), | |
5695 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5696 return de; | |
5697 } | |
5698 | |
5699 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
5700 { Dsymbol *s; | |
5701 Dsymbol *s2; | |
5702 TemplateDeclaration *td; | |
5703 Expression *e; | |
5704 Identifier *id; | |
5705 Type *t1; | |
5706 Expression *eleft = NULL; | |
5707 Expression *eright; | |
5708 | |
5709 #if LOGSEMANTIC | |
5710 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
5711 #endif | |
5712 //e1->print(); | |
5713 //print(); | |
5714 e1 = e1->semantic(sc); | |
5715 t1 = e1->type; | |
5716 if (t1) | |
5717 t1 = t1->toBasetype(); | |
5718 //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
|
5719 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5720 /* 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
|
5721 * 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
|
5722 * 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
|
5723 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5724 |
159 | 5725 if (e1->op == TOKdotexp) |
5726 { DotExp *de = (DotExp *)e1; | |
5727 eleft = de->e1; | |
5728 eright = de->e2; | |
5729 } | |
5730 else | |
5731 { eleft = NULL; | |
5732 eright = e1; | |
5733 } | |
5734 if (eright->op == TOKimport) | |
5735 { | |
5736 s = ((ScopeExp *)eright)->sds; | |
5737 } | |
5738 else if (e1->op == TOKtype) | |
5739 { | |
5740 s = t1->isClassHandle(); | |
5741 if (!s) | |
5742 { if (t1->ty == Tstruct) | |
5743 s = ((TypeStruct *)t1)->sym; | |
5744 else | |
5745 goto L1; | |
5746 } | |
5747 } | |
5748 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
5749 { | |
5750 s = t1->toDsymbol(sc); | |
5751 eleft = e1; | |
5752 } | |
5753 else if (t1 && t1->ty == Tpointer) | |
5754 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5755 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 5756 if (t1->ty != Tstruct) |
5757 goto L1; | |
5758 s = t1->toDsymbol(sc); | |
5759 eleft = e1; | |
5760 } | |
5761 else | |
5762 { | |
5763 L1: | |
5764 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
5765 goto Lerr; | |
5766 } | |
5767 | |
5768 assert(s); | |
5769 id = ti->name; | |
5770 s2 = s->search(loc, id, 0); | |
5771 if (!s2) | |
5772 { error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); | |
5773 goto Lerr; | |
5774 } | |
5775 s = s2; | |
5776 s->semantic(sc); | |
5777 s = s->toAlias(); | |
5778 td = s->isTemplateDeclaration(); | |
5779 if (!td) | |
5780 { | |
5781 error("%s is not a template", id->toChars()); | |
5782 goto Lerr; | |
5783 } | |
5784 if (global.errors) | |
5785 goto Lerr; | |
5786 | |
5787 ti->tempdecl = td; | |
5788 | |
5789 if (eleft) | |
5790 { Declaration *v; | |
5791 | |
5792 ti->semantic(sc); | |
5793 s = ti->inst->toAlias(); | |
5794 v = s->isDeclaration(); | |
5795 if (v) | |
5796 { e = new DotVarExp(loc, eleft, v); | |
5797 e = e->semantic(sc); | |
5798 return e; | |
5799 } | |
5800 } | |
5801 | |
5802 e = new ScopeExp(loc, ti); | |
5803 if (eleft) | |
5804 { | |
5805 e = new DotExp(loc, eleft, e); | |
5806 } | |
5807 e = e->semantic(sc); | |
5808 return e; | |
5809 | |
5810 Lerr: | |
336 | 5811 return new IntegerExp(loc, 0, Type::tint32); |
159 | 5812 } |
5813 | |
5814 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5815 { | |
5816 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5817 buf->writeByte('.'); | |
5818 ti->toCBuffer(buf, hgs); | |
5819 } | |
5820 | |
5821 /************************************************************/ | |
5822 | |
5823 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
5824 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
5825 { | |
5826 this->func = f; | |
5827 } | |
5828 | |
5829 Expression *DelegateExp::semantic(Scope *sc) | |
5830 { | |
5831 #if LOGSEMANTIC | |
5832 printf("DelegateExp::semantic('%s')\n", toChars()); | |
5833 #endif | |
5834 if (!type) | |
5835 { | |
5836 e1 = e1->semantic(sc); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
5837 // 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
|
5838 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 5839 type = type->semantic(loc, sc); |
5840 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 5841 if (func->needThis()) |
5842 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 5843 } |
5844 return this; | |
5845 } | |
5846 | |
5847 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5848 { | |
5849 buf->writeByte('&'); | |
5850 if (!func->isNested()) | |
5851 { | |
5852 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5853 buf->writeByte('.'); | |
5854 } | |
5855 buf->writestring(func->toChars()); | |
5856 } | |
5857 | |
5858 /************************************************************/ | |
5859 | |
5860 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
5861 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
5862 { | |
5863 this->sym = s; | |
5864 this->type = s->getType(); | |
5865 } | |
5866 | |
5867 Expression *DotTypeExp::semantic(Scope *sc) | |
5868 { | |
5869 #if LOGSEMANTIC | |
5870 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
5871 #endif | |
5872 UnaExp::semantic(sc); | |
5873 return this; | |
5874 } | |
5875 | |
5876 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5877 { | |
5878 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5879 buf->writeByte('.'); | |
5880 buf->writestring(sym->toChars()); | |
5881 } | |
5882 | |
5883 /************************************************************/ | |
5884 | |
5885 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
5886 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5887 { | |
5888 this->arguments = exps; | |
5889 } | |
5890 | |
5891 CallExp::CallExp(Loc loc, Expression *e) | |
5892 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5893 { | |
5894 this->arguments = NULL; | |
5895 } | |
5896 | |
5897 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
5898 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5899 { | |
5900 Expressions *arguments = new Expressions(); | |
5901 arguments->setDim(1); | |
5902 arguments->data[0] = (void *)earg1; | |
5903 | |
5904 this->arguments = arguments; | |
5905 } | |
5906 | |
5907 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
5908 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
5909 { | |
5910 Expressions *arguments = new Expressions(); | |
5911 arguments->setDim(2); | |
5912 arguments->data[0] = (void *)earg1; | |
5913 arguments->data[1] = (void *)earg2; | |
5914 | |
5915 this->arguments = arguments; | |
5916 } | |
5917 | |
5918 Expression *CallExp::syntaxCopy() | |
5919 { | |
5920 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
5921 } | |
5922 | |
5923 | |
5924 Expression *CallExp::semantic(Scope *sc) | |
5925 { | |
5926 TypeFunction *tf; | |
5927 FuncDeclaration *f; | |
5928 int i; | |
5929 Type *t1; | |
5930 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
|
5931 Objects *targsi = NULL; // initial list of template arguments |
159 | 5932 |
5933 #if LOGSEMANTIC | |
5934 printf("CallExp::semantic() %s\n", toChars()); | |
5935 #endif | |
5936 if (type) | |
5937 return this; // semantic() already run | |
5938 #if 0 | |
5939 if (arguments && arguments->dim) | |
5940 { | |
5941 Expression *earg = (Expression *)arguments->data[0]; | |
5942 earg->print(); | |
5943 if (earg->type) earg->type->print(); | |
5944 } | |
5945 #endif | |
5946 | |
5947 if (e1->op == TOKdelegate) | |
5948 { DelegateExp *de = (DelegateExp *)e1; | |
5949 | |
5950 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
5951 return semantic(sc); | |
5952 } | |
5953 | |
5954 /* Transform: | |
5955 * array.id(args) into id(array,args) | |
5956 * aa.remove(arg) into delete aa[arg] | |
5957 */ | |
5958 if (e1->op == TOKdot) | |
5959 { | |
5960 // BUG: we should handle array.a.b.c.e(args) too | |
5961 | |
5962 DotIdExp *dotid = (DotIdExp *)(e1); | |
5963 dotid->e1 = dotid->e1->semantic(sc); | |
5964 assert(dotid->e1); | |
5965 if (dotid->e1->type) | |
5966 { | |
5967 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
5968 if (e1ty == Taarray && dotid->ident == Id::remove) | |
5969 { | |
5970 if (!arguments || arguments->dim != 1) | |
5971 { error("expected key as argument to aa.remove()"); | |
5972 goto Lagain; | |
5973 } | |
5974 Expression *key = (Expression *)arguments->data[0]; | |
5975 key = key->semantic(sc); | |
5976 key = resolveProperties(sc, key); | |
5977 key->rvalue(); | |
5978 | |
5979 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
5980 key = key->implicitCastTo(sc, taa->index); | |
5981 key = key->implicitCastTo(sc, taa->key); | |
5982 | |
5983 return new RemoveExp(loc, dotid->e1, key); | |
5984 } | |
5985 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
5986 { | |
5987 if (!arguments) | |
5988 arguments = new Expressions(); | |
5989 arguments->shift(dotid->e1); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5990 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5991 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
|
5992 #else |
159 | 5993 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
|
5994 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5995 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5996 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5997 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5998 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5999 #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
|
6000 /* 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
|
6001 * 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
|
6002 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6003 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
|
6004 { 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
|
6005 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
|
6006 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
|
6007 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6008 /* 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
|
6009 * 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
|
6010 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6011 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
|
6012 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
|
6013 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
|
6014 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
|
6015 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
|
6016 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
|
6017 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6018 /* 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
|
6019 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6020 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
|
6021 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
|
6022 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
|
6023 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6024 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6025 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6026 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6027 /* 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
|
6028 * 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
|
6029 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6030 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
|
6031 { 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
|
6032 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
|
6033 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
|
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 /* 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
|
6036 * 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
|
6037 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6038 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
|
6039 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
|
6040 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
|
6041 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
|
6042 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
|
6043 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
|
6044 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
|
6045 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6046 global.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
|
6047 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
|
6048 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
|
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 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6051 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
|
6052 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6053 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6054 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6055 |
159 | 6056 istemp = 0; |
6057 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
|
6058 //printf("Lagain: %s\n", toChars()); |
159 | 6059 f = NULL; |
6060 if (e1->op == TOKthis || e1->op == TOKsuper) | |
6061 { | |
6062 // semantic() run later for these | |
6063 } | |
6064 else | |
6065 { | |
6066 UnaExp::semantic(sc); | |
6067 | |
6068 /* Look for e1 being a lazy parameter | |
6069 */ | |
6070 if (e1->op == TOKvar) | |
6071 { VarExp *ve = (VarExp *)e1; | |
6072 | |
6073 if (ve->var->storage_class & STClazy) | |
6074 { | |
6075 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
6076 TypeDelegate *t = new TypeDelegate(tf); | |
6077 ve->type = t->semantic(loc, sc); | |
6078 } | |
6079 } | |
6080 | |
6081 if (e1->op == TOKimport) | |
6082 { // Perhaps this should be moved to ScopeExp::semantic() | |
6083 ScopeExp *se = (ScopeExp *)e1; | |
6084 e1 = new DsymbolExp(loc, se->sds); | |
6085 e1 = e1->semantic(sc); | |
6086 } | |
6087 #if 1 // patch for #540 by Oskar Linde | |
6088 else if (e1->op == TOKdotexp) | |
6089 { | |
6090 DotExp *de = (DotExp *) e1; | |
6091 | |
6092 if (de->e2->op == TOKimport) | |
6093 { // This should *really* be moved to ScopeExp::semantic() | |
6094 ScopeExp *se = (ScopeExp *)de->e2; | |
6095 de->e2 = new DsymbolExp(loc, se->sds); | |
6096 de->e2 = de->e2->semantic(sc); | |
6097 } | |
6098 | |
6099 if (de->e2->op == TOKtemplate) | |
6100 { TemplateExp *te = (TemplateExp *) de->e2; | |
6101 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
6102 } | |
6103 } | |
6104 #endif | |
6105 } | |
6106 | |
6107 if (e1->op == TOKcomma) | |
6108 { | |
6109 CommaExp *ce = (CommaExp *)e1; | |
6110 | |
6111 e1 = ce->e2; | |
6112 e1->type = ce->type; | |
6113 ce->e2 = this; | |
6114 ce->type = NULL; | |
6115 return ce->semantic(sc); | |
6116 } | |
6117 | |
6118 t1 = NULL; | |
6119 if (e1->type) | |
6120 t1 = e1->type->toBasetype(); | |
6121 | |
6122 // Check for call operator overload | |
6123 if (t1) | |
6124 { AggregateDeclaration *ad; | |
6125 | |
6126 if (t1->ty == Tstruct) | |
6127 { | |
6128 ad = ((TypeStruct *)t1)->sym; | |
6129 if (search_function(ad, Id::call)) | |
6130 goto L1; // overload of opCall, therefore it's a call | |
6131 | |
6132 if (e1->op != TOKtype) | |
6133 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
6134 /* It's a struct literal | |
6135 */ | |
6136 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
6137 e = e->semantic(sc); | |
6138 e->type = e1->type; // in case e1->type was a typedef | |
6139 return e; | |
6140 } | |
6141 else if (t1->ty == Tclass) | |
6142 { | |
6143 ad = ((TypeClass *)t1)->sym; | |
6144 goto L1; | |
6145 L1: | |
6146 // Rewrite as e1.call(arguments) | |
6147 Expression *e = new DotIdExp(loc, e1, Id::call); | |
6148 e = new CallExp(loc, e, arguments); | |
6149 e = e->semantic(sc); | |
6150 return e; | |
6151 } | |
6152 } | |
6153 | |
6154 arrayExpressionSemantic(arguments, sc); | |
6155 preFunctionArguments(loc, sc, arguments); | |
6156 | |
6157 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
6158 e1->op == TOKdottd) | |
6159 { | |
6160 DotVarExp *dve; | |
6161 DotTemplateExp *dte; | |
6162 AggregateDeclaration *ad; | |
6163 UnaExp *ue = (UnaExp *)(e1); | |
336 | 6164 |
159 | 6165 if (e1->op == TOKdotvar) |
6166 { // Do overload resolution | |
6167 dve = (DotVarExp *)(e1); | |
6168 | |
6169 f = dve->var->isFuncDeclaration(); | |
6170 assert(f); | |
6171 f = f->overloadResolve(loc, arguments); | |
6172 | |
6173 ad = f->toParent()->isAggregateDeclaration(); | |
6174 } | |
6175 else | |
6176 { dte = (DotTemplateExp *)(e1); | |
6177 TemplateDeclaration *td = dte->td; | |
6178 assert(td); | |
6179 if (!arguments) | |
6180 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
6181 arguments = new Expressions(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6182 f = td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6183 if (!f) |
6184 { type = Type::terror; | |
6185 return this; | |
6186 } | |
6187 ad = td->toParent()->isAggregateDeclaration(); | |
6188 } | |
336 | 6189 if (f->needThis()) |
6190 { | |
6191 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
6192 } | |
6193 | |
6194 /* Cannot call public functions from inside invariant | |
6195 * (because then the invariant would have infinite recursion) | |
159 | 6196 */ |
336 | 6197 if (sc->func && sc->func->isInvariantDeclaration() && |
6198 ue->e1->op == TOKthis && | |
6199 f->addPostInvariant() | |
159 | 6200 ) |
6201 { | |
336 | 6202 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 6203 } |
6204 | |
6205 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6206 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6207 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6208 #endif |
159 | 6209 accessCheck(loc, sc, ue->e1, f); |
6210 if (!f->needThis()) | |
6211 { | |
6212 VarExp *ve = new VarExp(loc, f); | |
6213 e1 = new CommaExp(loc, ue->e1, ve); | |
6214 e1->type = f->type; | |
6215 } | |
6216 else | |
6217 { | |
6218 if (e1->op == TOKdotvar) | |
6219 dve->var = f; | |
6220 else | |
6221 e1 = new DotVarExp(loc, dte->e1, f); | |
6222 e1->type = f->type; | |
6223 | |
6224 // See if we need to adjust the 'this' pointer | |
6225 AggregateDeclaration *ad = f->isThis(); | |
6226 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
6227 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
6228 ue->e1->op != TOKsuper) | |
6229 { | |
6230 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
6231 ue->e1 = ue->e1->semantic(sc); | |
6232 } | |
6233 } | |
6234 t1 = e1->type; | |
6235 } | |
6236 else if (e1->op == TOKsuper) | |
6237 { | |
6238 // Base class constructor call | |
6239 ClassDeclaration *cd = NULL; | |
6240 | |
6241 if (sc->func) | |
6242 cd = sc->func->toParent()->isClassDeclaration(); | |
6243 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
6244 { | |
6245 error("super class constructor call must be in a constructor"); | |
6246 type = Type::terror; | |
6247 return this; | |
6248 } | |
6249 else | |
6250 { | |
6251 f = cd->baseClass->ctor; | |
6252 if (!f) | |
6253 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
6254 type = Type::terror; | |
6255 return this; | |
6256 } | |
6257 else | |
6258 { | |
336 | 6259 if (!sc->intypeof) |
6260 { | |
159 | 6261 #if 0 |
336 | 6262 if (sc->callSuper & (CSXthis | CSXsuper)) |
6263 error("reference to this before super()"); | |
159 | 6264 #endif |
336 | 6265 if (sc->noctor || sc->callSuper & CSXlabel) |
6266 error("constructor calls not allowed in loops or after labels"); | |
6267 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6268 error("multiple constructor calls"); | |
6269 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
6270 } | |
159 | 6271 |
6272 f = f->overloadResolve(loc, arguments); | |
6273 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6274 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6275 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6276 #endif |
159 | 6277 e1 = new DotVarExp(e1->loc, e1, f); |
6278 e1 = e1->semantic(sc); | |
6279 t1 = e1->type; | |
6280 } | |
6281 } | |
6282 } | |
6283 else if (e1->op == TOKthis) | |
6284 { | |
6285 // same class constructor call | |
6286 ClassDeclaration *cd = NULL; | |
6287 | |
6288 if (sc->func) | |
6289 cd = sc->func->toParent()->isClassDeclaration(); | |
6290 if (!cd || !sc->func->isCtorDeclaration()) | |
6291 { | |
6292 error("class constructor call must be in a constructor"); | |
6293 type = Type::terror; | |
6294 return this; | |
6295 } | |
6296 else | |
6297 { | |
336 | 6298 if (!sc->intypeof) |
6299 { | |
159 | 6300 #if 0 |
336 | 6301 if (sc->callSuper & (CSXthis | CSXsuper)) |
6302 error("reference to this before super()"); | |
159 | 6303 #endif |
336 | 6304 if (sc->noctor || sc->callSuper & CSXlabel) |
6305 error("constructor calls not allowed in loops or after labels"); | |
6306 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6307 error("multiple constructor calls"); | |
6308 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6309 } | |
159 | 6310 |
6311 f = cd->ctor; | |
6312 f = f->overloadResolve(loc, arguments); | |
6313 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6314 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6315 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6316 #endif |
159 | 6317 e1 = new DotVarExp(e1->loc, e1, f); |
6318 e1 = e1->semantic(sc); | |
6319 t1 = e1->type; | |
6320 | |
6321 // BUG: this should really be done by checking the static | |
6322 // call graph | |
6323 if (f == sc->func) | |
6324 error("cyclic constructor call"); | |
6325 } | |
6326 } | |
6327 else if (!t1) | |
6328 { | |
6329 error("function expected before (), not '%s'", e1->toChars()); | |
6330 type = Type::terror; | |
6331 return this; | |
6332 } | |
6333 else if (t1->ty != Tfunction) | |
6334 { | |
6335 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
|
6336 { 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
|
6337 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
|
6338 tf = (TypeFunction *)(td->next); |
159 | 6339 goto Lcheckargs; |
6340 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6341 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6342 { Expression *e; |
6343 | |
6344 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
|
6345 t1 = ((TypePointer *)t1)->next; |
159 | 6346 e->type = t1; |
6347 e1 = e; | |
6348 } | |
6349 else if (e1->op == TOKtemplate) | |
6350 { | |
6351 TemplateExp *te = (TemplateExp *)e1; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6352 f = te->td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6353 if (!f) |
6354 { type = Type::terror; | |
6355 return this; | |
6356 } | |
6357 if (f->needThis() && hasThis(sc)) | |
6358 { | |
6359 // Supply an implicit 'this', as in | |
6360 // this.ident | |
6361 | |
6362 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6363 goto Lagain; | |
6364 } | |
6365 | |
6366 e1 = new VarExp(loc, f); | |
6367 goto Lagain; | |
6368 } | |
6369 else | |
6370 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6371 type = Type::terror; | |
6372 return this; | |
6373 } | |
6374 } | |
6375 else if (e1->op == TOKvar) | |
6376 { | |
6377 // Do overload resolution | |
6378 VarExp *ve = (VarExp *)e1; | |
6379 | |
6380 f = ve->var->isFuncDeclaration(); | |
6381 assert(f); | |
6382 | |
6383 // Look to see if f is really a function template | |
6384 if (0 && !istemp && f->parent) | |
6385 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6386 | |
6387 if (ti && | |
6388 (ti->name == f->ident || | |
6389 ti->toAlias()->ident == f->ident) | |
6390 && | |
6391 ti->tempdecl) | |
6392 { | |
6393 /* This is so that one can refer to the enclosing | |
6394 * template, even if it has the same name as a member | |
6395 * of the template, if it has a !(arguments) | |
6396 */ | |
6397 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6398 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6399 tempdecl = tempdecl->overroot; // then get the start | |
6400 e1 = new TemplateExp(loc, tempdecl); | |
6401 istemp = 1; | |
6402 goto Lagain; | |
6403 } | |
6404 } | |
6405 | |
6406 f = f->overloadResolve(loc, arguments); | |
6407 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6408 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6409 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6410 #endif |
159 | 6411 |
6412 if (f->needThis() && hasThis(sc)) | |
6413 { | |
6414 // Supply an implicit 'this', as in | |
6415 // this.ident | |
6416 | |
6417 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6418 goto Lagain; | |
6419 } | |
6420 | |
6421 accessCheck(loc, sc, NULL, f); | |
6422 | |
6423 ve->var = f; | |
6424 ve->type = f->type; | |
6425 t1 = f->type; | |
6426 } | |
6427 assert(t1->ty == Tfunction); | |
6428 tf = (TypeFunction *)(t1); | |
6429 | |
6430 Lcheckargs: | |
6431 assert(tf->ty == Tfunction); | |
6432 type = tf->next; | |
6433 | |
6434 if (!arguments) | |
6435 arguments = new Expressions(); | |
6436 functionArguments(loc, sc, tf, arguments); | |
6437 | |
6438 assert(type); | |
6439 | |
6440 if (f && f->tintro) | |
6441 { | |
6442 Type *t = type; | |
6443 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
|
6444 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
|
6445 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6446 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
|
6447 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6448 type = tf->next; |
159 | 6449 return castTo(sc, t); |
6450 } | |
6451 } | |
6452 | |
6453 return this; | |
6454 } | |
6455 | |
6456 int CallExp::checkSideEffect(int flag) | |
6457 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6458 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6459 if (flag != 2) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6460 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6461 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6462 if (e1->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6463 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6464 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6465 /* 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
|
6466 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6467 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
|
6468 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6469 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6470 if (e->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6471 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6472 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6473 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6474 /* 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
|
6475 * then this expression has no side effects. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6476 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6477 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6478 if (t->ty == Tfunction && ((TypeFunction *)t)->ispure) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6479 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6480 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
|
6481 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6482 #endif |
159 | 6483 return 1; |
6484 } | |
6485 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6486 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6487 int CallExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6488 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6489 if (e1->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6490 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6491 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6492 /* 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
|
6493 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6494 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
|
6495 { Expression *e = (Expression *)arguments->data[i]; |
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 (e->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6498 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6499 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6500 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6501 /* If 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
|
6502 * then this expression cannot throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6503 * Note that pure functions can throw. |
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 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6506 if (t->ty == Tfunction && ((TypeFunction *)t)->isnothrow) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6507 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6508 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
|
6509 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6510 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6511 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6512 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6513 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6514 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6515 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6516 int CallExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6517 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6518 if (type->toBasetype()->ty == Tstruct) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6519 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6520 Type *tb = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6521 if (tb->ty == Tfunction && ((TypeFunction *)tb)->isref) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6522 return 1; // function returns a reference |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6523 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6524 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6525 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6526 |
159 | 6527 Expression *CallExp::toLvalue(Scope *sc, Expression *e) |
6528 { | |
6529 if (type->toBasetype()->ty == Tstruct) | |
6530 return this; | |
6531 else | |
6532 return Expression::toLvalue(sc, e); | |
6533 } | |
6534 | |
6535 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6536 { int i; | |
6537 | |
6538 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6539 buf->writeByte('('); | |
6540 argsToCBuffer(buf, arguments, hgs); | |
6541 buf->writeByte(')'); | |
6542 } | |
6543 | |
6544 | |
6545 /************************************************************/ | |
6546 | |
6547 AddrExp::AddrExp(Loc loc, Expression *e) | |
6548 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6549 { | |
6550 } | |
6551 | |
6552 Expression *AddrExp::semantic(Scope *sc) | |
6553 { | |
6554 #if LOGSEMANTIC | |
6555 printf("AddrExp::semantic('%s')\n", toChars()); | |
6556 #endif | |
6557 if (!type) | |
6558 { | |
6559 UnaExp::semantic(sc); | |
6560 e1 = e1->toLvalue(sc, NULL); | |
6561 if (!e1->type) | |
6562 { | |
6563 error("cannot take address of %s", e1->toChars()); | |
6564 type = Type::tint32; | |
6565 return this; | |
6566 } | |
6567 type = e1->type->pointerTo(); | |
6568 | |
6569 // See if this should really be a delegate | |
6570 if (e1->op == TOKdotvar) | |
6571 { | |
6572 DotVarExp *dve = (DotVarExp *)e1; | |
6573 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6574 | |
6575 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
|
6576 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6577 e = e->semantic(sc); |
6578 return e; | |
6579 } | |
6580 } | |
6581 else if (e1->op == TOKvar) | |
6582 { | |
6583 VarExp *dve = (VarExp *)e1; | |
6584 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6585 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6586 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6587 // 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
|
6588 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
|
6589 { |
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
|
6590 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
|
6591 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
|
6592 } |
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
|
6593 |
159 | 6594 if (f && f->isNested()) |
6595 { Expression *e; | |
6596 | |
6597 e = new DelegateExp(loc, e1, f); | |
6598 e = e->semantic(sc); | |
6599 return e; | |
6600 } | |
6601 } | |
6602 else if (e1->op == TOKarray) | |
6603 { | |
6604 if (e1->type->toBasetype()->ty == Tbit) | |
6605 error("cannot take address of bit in array"); | |
6606 } | |
6607 return optimize(WANTvalue); | |
6608 } | |
6609 return this; | |
6610 } | |
6611 | |
6612 /************************************************************/ | |
6613 | |
6614 PtrExp::PtrExp(Loc loc, Expression *e) | |
6615 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6616 { | |
6617 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
|
6618 type = ((TypePointer *)e->type)->next; |
159 | 6619 } |
6620 | |
6621 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6622 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6623 { | |
6624 type = t; | |
6625 } | |
6626 | |
6627 Expression *PtrExp::semantic(Scope *sc) | |
6628 { Type *tb; | |
6629 | |
6630 #if LOGSEMANTIC | |
6631 printf("PtrExp::semantic('%s')\n", toChars()); | |
6632 #endif | |
6633 UnaExp::semantic(sc); | |
6634 e1 = resolveProperties(sc, e1); | |
6635 if (type) | |
6636 return this; | |
6637 if (!e1->type) | |
6638 printf("PtrExp::semantic('%s')\n", toChars()); | |
6639 tb = e1->type->toBasetype(); | |
6640 switch (tb->ty) | |
6641 { | |
6642 case Tpointer: | |
6643 type = tb->next; | |
6644 if (type->isbit()) | |
6645 { Expression *e; | |
6646 | |
6647 // Rewrite *p as p[0] | |
6648 e = new IndexExp(loc, e1, new IntegerExp(0)); | |
6649 return e->semantic(sc); | |
6650 } | |
6651 break; | |
6652 | |
6653 case Tsarray: | |
6654 case Tarray: | |
6655 type = tb->next; | |
6656 e1 = e1->castTo(sc, type->pointerTo()); | |
6657 break; | |
6658 | |
6659 default: | |
6660 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
6661 type = Type::tint32; | |
6662 break; | |
6663 } | |
6664 rvalue(); | |
6665 return this; | |
6666 } | |
6667 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6668 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6669 int PtrExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6670 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6671 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6672 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6673 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6674 |
159 | 6675 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) |
6676 { | |
6677 #if 0 | |
6678 tym = tybasic(e1->ET->Tty); | |
6679 if (!(tyscalar(tym) || | |
6680 tym == TYstruct || | |
6681 tym == TYarray && e->Eoper == TOKaddr)) | |
6682 synerr(EM_lvalue); // lvalue expected | |
6683 #endif | |
6684 return this; | |
6685 } | |
6686 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6687 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6688 Expression *PtrExp::modifiableLvalue(Scope *sc, Expression *e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6689 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6690 //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
|
6691 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6692 if (e1->op == TOKsymoff) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6693 { SymOffExp *se = (SymOffExp *)e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6694 se->var->checkModify(loc, sc, type); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6695 //return toLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6696 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6697 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6698 return Expression::modifiableLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6699 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6700 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6701 |
159 | 6702 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
6703 { | |
6704 buf->writeByte('*'); | |
6705 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6706 } | |
6707 | |
6708 /************************************************************/ | |
6709 | |
6710 NegExp::NegExp(Loc loc, Expression *e) | |
6711 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
6712 { | |
6713 } | |
6714 | |
6715 Expression *NegExp::semantic(Scope *sc) | |
6716 { Expression *e; | |
6717 | |
6718 #if LOGSEMANTIC | |
6719 printf("NegExp::semantic('%s')\n", toChars()); | |
6720 #endif | |
6721 if (!type) | |
6722 { | |
6723 UnaExp::semantic(sc); | |
6724 e1 = resolveProperties(sc, e1); | |
6725 e = op_overload(sc); | |
6726 if (e) | |
6727 return e; | |
6728 | |
6729 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
|
6730 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
|
6731 e1->checkArithmetic(); |
159 | 6732 type = e1->type; |
6733 } | |
6734 return this; | |
6735 } | |
6736 | |
6737 /************************************************************/ | |
6738 | |
6739 UAddExp::UAddExp(Loc loc, Expression *e) | |
6740 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
6741 { | |
6742 } | |
6743 | |
6744 Expression *UAddExp::semantic(Scope *sc) | |
6745 { Expression *e; | |
6746 | |
6747 #if LOGSEMANTIC | |
6748 printf("UAddExp::semantic('%s')\n", toChars()); | |
6749 #endif | |
6750 assert(!type); | |
6751 UnaExp::semantic(sc); | |
6752 e1 = resolveProperties(sc, e1); | |
6753 e = op_overload(sc); | |
6754 if (e) | |
6755 return e; | |
6756 e1->checkNoBool(); | |
6757 e1->checkArithmetic(); | |
6758 return e1; | |
6759 } | |
6760 | |
6761 /************************************************************/ | |
6762 | |
6763 ComExp::ComExp(Loc loc, Expression *e) | |
6764 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
6765 { | |
6766 } | |
6767 | |
6768 Expression *ComExp::semantic(Scope *sc) | |
6769 { Expression *e; | |
6770 | |
6771 if (!type) | |
6772 { | |
6773 UnaExp::semantic(sc); | |
6774 e1 = resolveProperties(sc, e1); | |
6775 e = op_overload(sc); | |
6776 if (e) | |
6777 return e; | |
6778 | |
6779 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
|
6780 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
|
6781 e1 = e1->checkIntegral(); |
159 | 6782 type = e1->type; |
6783 } | |
6784 return this; | |
6785 } | |
6786 | |
6787 /************************************************************/ | |
6788 | |
6789 NotExp::NotExp(Loc loc, Expression *e) | |
6790 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
6791 { | |
6792 } | |
6793 | |
6794 Expression *NotExp::semantic(Scope *sc) | |
6795 { | |
6796 UnaExp::semantic(sc); | |
6797 e1 = resolveProperties(sc, e1); | |
6798 e1 = e1->checkToBoolean(); | |
6799 type = Type::tboolean; | |
6800 return this; | |
6801 } | |
6802 | |
6803 int NotExp::isBit() | |
6804 { | |
6805 return TRUE; | |
6806 } | |
6807 | |
6808 | |
6809 | |
6810 /************************************************************/ | |
6811 | |
6812 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
6813 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
6814 { | |
6815 type = t; | |
6816 } | |
6817 | |
6818 Expression *BoolExp::semantic(Scope *sc) | |
6819 { | |
6820 UnaExp::semantic(sc); | |
6821 e1 = resolveProperties(sc, e1); | |
6822 e1 = e1->checkToBoolean(); | |
6823 type = Type::tboolean; | |
6824 return this; | |
6825 } | |
6826 | |
6827 int BoolExp::isBit() | |
6828 { | |
6829 return TRUE; | |
6830 } | |
6831 | |
6832 /************************************************************/ | |
6833 | |
6834 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
6835 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
6836 { | |
6837 } | |
6838 | |
6839 Expression *DeleteExp::semantic(Scope *sc) | |
6840 { | |
6841 Type *tb; | |
6842 | |
6843 UnaExp::semantic(sc); | |
6844 e1 = resolveProperties(sc, e1); | |
6845 e1 = e1->toLvalue(sc, NULL); | |
6846 type = Type::tvoid; | |
6847 | |
6848 tb = e1->type->toBasetype(); | |
6849 switch (tb->ty) | |
6850 { case Tclass: | |
6851 { TypeClass *tc = (TypeClass *)tb; | |
6852 ClassDeclaration *cd = tc->sym; | |
6853 | |
6854 if (cd->isCOMinterface()) | |
6855 { /* Because COM classes are deleted by IUnknown.Release() | |
6856 */ | |
6857 error("cannot delete instance of COM interface %s", cd->toChars()); | |
6858 } | |
6859 break; | |
6860 } | |
6861 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
|
6862 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 6863 if (tb->ty == Tstruct) |
6864 { | |
6865 TypeStruct *ts = (TypeStruct *)tb; | |
6866 StructDeclaration *sd = ts->sym; | |
6867 FuncDeclaration *f = sd->aggDelete; | |
6868 | |
6869 if (f) | |
6870 { | |
6871 Type *tpv = Type::tvoid->pointerTo(); | |
6872 | |
6873 Expression *e = e1->castTo(sc, tpv); | |
6874 Expression *ec = new VarExp(loc, f); | |
6875 e = new CallExp(loc, ec, e); | |
6876 return e->semantic(sc); | |
6877 } | |
6878 } | |
6879 break; | |
6880 | |
6881 case Tarray: | |
6882 break; | |
6883 | |
6884 default: | |
6885 if (e1->op == TOKindex) | |
6886 { | |
6887 IndexExp *ae = (IndexExp *)(e1); | |
6888 Type *tb1 = ae->e1->type->toBasetype(); | |
6889 if (tb1->ty == Taarray) | |
6890 break; | |
6891 } | |
6892 error("cannot delete type %s", e1->type->toChars()); | |
6893 break; | |
6894 } | |
6895 | |
6896 if (e1->op == TOKindex) | |
6897 { | |
6898 IndexExp *ae = (IndexExp *)(e1); | |
6899 Type *tb1 = ae->e1->type->toBasetype(); | |
6900 if (tb1->ty == Taarray) | |
6901 { if (!global.params.useDeprecated) | |
6902 error("delete aa[key] deprecated, use aa.remove(key)"); | |
6903 } | |
6904 } | |
6905 | |
6906 return this; | |
6907 } | |
6908 | |
6909 int DeleteExp::checkSideEffect(int flag) | |
6910 { | |
6911 return 1; | |
6912 } | |
6913 | |
6914 Expression *DeleteExp::checkToBoolean() | |
6915 { | |
6916 error("delete does not give a boolean result"); | |
6917 return this; | |
6918 } | |
6919 | |
6920 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6921 { | |
6922 buf->writestring("delete "); | |
6923 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6924 } | |
6925 | |
6926 /************************************************************/ | |
6927 | |
6928 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
6929 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
6930 { | |
6931 to = t; | |
6932 } | |
6933 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6934 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6935 /* For cast(const) and cast(immutable) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6936 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6937 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
|
6938 : UnaExp(loc, TOKcast, sizeof(CastExp), e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6939 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6940 to = NULL; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6941 this->mod = mod; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6942 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6943 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6944 |
159 | 6945 Expression *CastExp::syntaxCopy() |
6946 { | |
6947 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
6948 } | |
6949 | |
6950 | |
6951 Expression *CastExp::semantic(Scope *sc) | |
6952 { Expression *e; | |
6953 BinExp *b; | |
6954 UnaExp *u; | |
6955 | |
6956 #if LOGSEMANTIC | |
6957 printf("CastExp::semantic('%s')\n", toChars()); | |
6958 #endif | |
6959 | |
6960 //static int x; assert(++x < 10); | |
6961 | |
6962 if (type) | |
6963 return this; | |
6964 UnaExp::semantic(sc); | |
6965 if (e1->type) // if not a tuple | |
6966 { | |
6967 e1 = resolveProperties(sc, e1); | |
6968 to = to->semantic(loc, sc); | |
6969 | |
6970 e = op_overload(sc); | |
6971 if (e) | |
6972 { | |
6973 return e->implicitCastTo(sc, to); | |
6974 } | |
6975 | |
6976 Type *tob = to->toBasetype(); | |
6977 if (tob->ty == Tstruct && | |
6978 !tob->equals(e1->type->toBasetype()) && | |
6979 ((TypeStruct *)to)->sym->search(0, Id::call, 0) | |
6980 ) | |
6981 { | |
6982 /* Look to replace: | |
6983 * cast(S)t | |
6984 * with: | |
6985 * S(t) | |
6986 */ | |
6987 | |
6988 // Rewrite as to.call(e1) | |
6989 e = new TypeExp(loc, to); | |
6990 e = new DotIdExp(loc, e, Id::call); | |
6991 e = new CallExp(loc, e, e1); | |
6992 e = e->semantic(sc); | |
6993 return e; | |
6994 } | |
6995 } | |
6996 e = e1->castTo(sc, to); | |
6997 return e; | |
6998 } | |
6999 | |
7000 int CastExp::checkSideEffect(int flag) | |
7001 { | |
7002 /* if not: | |
7003 * cast(void) | |
7004 * cast(classtype)func() | |
7005 */ | |
7006 if (!to->equals(Type::tvoid) && | |
7007 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
7008 return Expression::checkSideEffect(flag); | |
7009 return 1; | |
7010 } | |
7011 | |
7012 void CastExp::checkEscape() | |
7013 { Type *tb = type->toBasetype(); | |
7014 if (tb->ty == Tarray && e1->op == TOKvar && | |
7015 e1->type->toBasetype()->ty == Tsarray) | |
7016 { VarExp *ve = (VarExp *)e1; | |
7017 VarDeclaration *v = ve->var->isVarDeclaration(); | |
7018 if (v) | |
7019 { | |
7020 if (!v->isDataseg() && !v->isParameter()) | |
7021 error("escaping reference to local %s", v->toChars()); | |
7022 } | |
7023 } | |
7024 } | |
7025 | |
7026 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7027 { | |
7028 buf->writestring("cast("); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7029 #if DMDV1 |
159 | 7030 to->toCBuffer(buf, NULL, hgs); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7031 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7032 if (to) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7033 to->toCBuffer(buf, NULL, hgs); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7034 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7035 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7036 switch (mod) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7037 { case 0: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7038 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7039 case MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7040 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7041 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7042 case MODinvariant: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7043 buf->writestring(Token::tochars[TOKimmutable]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7044 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7045 case MODshared: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7046 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7047 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7048 case MODshared | MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7049 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7050 buf->writeByte(' '); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7051 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7052 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7053 default: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7054 assert(0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7055 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7056 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7057 #endif |
159 | 7058 buf->writeByte(')'); |
7059 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7060 } | |
7061 | |
7062 | |
7063 /************************************************************/ | |
7064 | |
7065 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
7066 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
7067 { | |
7068 this->upr = upr; | |
7069 this->lwr = lwr; | |
7070 lengthVar = NULL; | |
7071 } | |
7072 | |
7073 Expression *SliceExp::syntaxCopy() | |
7074 { | |
7075 Expression *lwr = NULL; | |
7076 if (this->lwr) | |
7077 lwr = this->lwr->syntaxCopy(); | |
7078 | |
7079 Expression *upr = NULL; | |
7080 if (this->upr) | |
7081 upr = this->upr->syntaxCopy(); | |
7082 | |
7083 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
7084 } | |
7085 | |
7086 Expression *SliceExp::semantic(Scope *sc) | |
7087 { Expression *e; | |
7088 AggregateDeclaration *ad; | |
7089 //FuncDeclaration *fd; | |
7090 ScopeDsymbol *sym; | |
7091 | |
7092 #if LOGSEMANTIC | |
7093 printf("SliceExp::semantic('%s')\n", toChars()); | |
7094 #endif | |
7095 if (type) | |
7096 return this; | |
7097 | |
7098 UnaExp::semantic(sc); | |
7099 e1 = resolveProperties(sc, e1); | |
7100 | |
7101 e = this; | |
7102 | |
7103 Type *t = e1->type->toBasetype(); | |
7104 if (t->ty == Tpointer) | |
7105 { | |
7106 if (!lwr || !upr) | |
7107 error("need upper and lower bound to slice pointer"); | |
7108 } | |
7109 else if (t->ty == Tarray) | |
7110 { | |
7111 } | |
7112 else if (t->ty == Tsarray) | |
7113 { | |
7114 } | |
7115 else if (t->ty == Tclass) | |
7116 { | |
7117 ad = ((TypeClass *)t)->sym; | |
7118 goto L1; | |
7119 } | |
7120 else if (t->ty == Tstruct) | |
7121 { | |
7122 ad = ((TypeStruct *)t)->sym; | |
7123 | |
7124 L1: | |
7125 if (search_function(ad, Id::slice)) | |
7126 { | |
7127 // Rewrite as e1.slice(lwr, upr) | |
7128 e = new DotIdExp(loc, e1, Id::slice); | |
7129 | |
7130 if (lwr) | |
7131 { | |
7132 assert(upr); | |
7133 e = new CallExp(loc, e, lwr, upr); | |
7134 } | |
7135 else | |
7136 { assert(!upr); | |
7137 e = new CallExp(loc, e); | |
7138 } | |
7139 e = e->semantic(sc); | |
7140 return e; | |
7141 } | |
7142 goto Lerror; | |
7143 } | |
7144 else if (t->ty == Ttuple) | |
7145 { | |
7146 if (!lwr && !upr) | |
7147 return e1; | |
7148 if (!lwr || !upr) | |
7149 { error("need upper and lower bound to slice tuple"); | |
7150 goto Lerror; | |
7151 } | |
7152 } | |
7153 else | |
7154 goto Lerror; | |
7155 | |
7156 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7157 { | |
7158 sym = new ArrayScopeSymbol(this); | |
7159 sym->loc = loc; | |
7160 sym->parent = sc->scopesym; | |
7161 sc = sc->push(sym); | |
7162 } | |
7163 | |
7164 if (lwr) | |
7165 { lwr = lwr->semantic(sc); | |
7166 lwr = resolveProperties(sc, lwr); | |
7167 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
7168 } | |
7169 if (upr) | |
7170 { upr = upr->semantic(sc); | |
7171 upr = resolveProperties(sc, upr); | |
7172 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
7173 } | |
7174 | |
7175 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7176 sc->pop(); | |
7177 | |
7178 if (t->ty == Ttuple) | |
7179 { | |
7180 lwr = lwr->optimize(WANTvalue); | |
7181 upr = upr->optimize(WANTvalue); | |
7182 uinteger_t i1 = lwr->toUInteger(); | |
7183 uinteger_t i2 = upr->toUInteger(); | |
7184 | |
7185 size_t length; | |
7186 TupleExp *te; | |
7187 TypeTuple *tup; | |
7188 | |
7189 if (e1->op == TOKtuple) // slicing an expression tuple | |
7190 { te = (TupleExp *)e1; | |
7191 length = te->exps->dim; | |
7192 } | |
7193 else if (e1->op == TOKtype) // slicing a type tuple | |
7194 { tup = (TypeTuple *)t; | |
7195 length = Argument::dim(tup->arguments); | |
7196 } | |
7197 else | |
7198 assert(0); | |
7199 | |
7200 if (i1 <= i2 && i2 <= length) | |
7201 { size_t j1 = (size_t) i1; | |
7202 size_t j2 = (size_t) i2; | |
7203 | |
7204 if (e1->op == TOKtuple) | |
7205 { Expressions *exps = new Expressions; | |
7206 exps->setDim(j2 - j1); | |
7207 for (size_t i = 0; i < j2 - j1; i++) | |
7208 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
7209 exps->data[i] = (void *)e; | |
7210 } | |
7211 e = new TupleExp(loc, exps); | |
7212 } | |
7213 else | |
7214 { Arguments *args = new Arguments; | |
7215 args->reserve(j2 - j1); | |
7216 for (size_t i = j1; i < j2; i++) | |
7217 { Argument *arg = Argument::getNth(tup->arguments, i); | |
7218 args->push(arg); | |
7219 } | |
7220 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
7221 } | |
7222 e = e->semantic(sc); | |
7223 } | |
7224 else | |
7225 { | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7226 error("string slice [%llu .. %llu] is out of bounds", i1, i2); |
159 | 7227 e = e1; |
7228 } | |
7229 return e; | |
7230 } | |
7231 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7232 type = t->nextOf()->arrayOf(); |
159 | 7233 return e; |
7234 | |
7235 Lerror: | |
7236 char *s; | |
7237 if (t->ty == Tvoid) | |
7238 s = e1->toChars(); | |
7239 else | |
7240 s = t->toChars(); | |
7241 error("%s cannot be sliced with []", s); | |
7242 type = Type::terror; | |
7243 return e; | |
7244 } | |
7245 | |
7246 void SliceExp::checkEscape() | |
7247 { | |
7248 e1->checkEscape(); | |
7249 } | |
7250 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7251 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7252 int SliceExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7253 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7254 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7255 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7256 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7257 |
159 | 7258 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) |
7259 { | |
7260 return this; | |
7261 } | |
7262 | |
7263 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
7264 { | |
7265 error("slice expression %s is not a modifiable lvalue", toChars()); | |
7266 return this; | |
7267 } | |
7268 | |
7269 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7270 { | |
7271 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7272 buf->writeByte('['); | |
7273 if (upr || lwr) | |
7274 { | |
7275 if (lwr) | |
7276 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
7277 else | |
7278 buf->writeByte('0'); | |
7279 buf->writestring(".."); | |
7280 if (upr) | |
7281 expToCBuffer(buf, hgs, upr, PREC_assign); | |
7282 else | |
7283 buf->writestring("length"); // BUG: should be array.length | |
7284 } | |
7285 buf->writeByte(']'); | |
7286 } | |
7287 | |
7288 /********************** ArrayLength **************************************/ | |
7289 | |
7290 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
7291 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
7292 { | |
7293 } | |
7294 | |
7295 Expression *ArrayLengthExp::semantic(Scope *sc) | |
7296 { Expression *e; | |
7297 | |
7298 #if LOGSEMANTIC | |
7299 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
7300 #endif | |
7301 if (!type) | |
7302 { | |
7303 UnaExp::semantic(sc); | |
7304 e1 = resolveProperties(sc, e1); | |
7305 | |
7306 type = Type::tsize_t; | |
7307 } | |
7308 return this; | |
7309 } | |
7310 | |
7311 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7312 { | |
7313 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7314 buf->writestring(".length"); | |
7315 } | |
7316 | |
7317 /*********************** ArrayExp *************************************/ | |
7318 | |
7319 // e1 [ i1, i2, i3, ... ] | |
7320 | |
7321 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
7322 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
7323 { | |
7324 arguments = args; | |
7325 } | |
7326 | |
7327 Expression *ArrayExp::syntaxCopy() | |
7328 { | |
7329 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
7330 } | |
7331 | |
7332 Expression *ArrayExp::semantic(Scope *sc) | |
7333 { Expression *e; | |
7334 Type *t1; | |
7335 | |
7336 #if LOGSEMANTIC | |
7337 printf("ArrayExp::semantic('%s')\n", toChars()); | |
7338 #endif | |
7339 UnaExp::semantic(sc); | |
7340 e1 = resolveProperties(sc, e1); | |
7341 | |
7342 t1 = e1->type->toBasetype(); | |
7343 if (t1->ty != Tclass && t1->ty != Tstruct) | |
7344 { // Convert to IndexExp | |
7345 if (arguments->dim != 1) | |
7346 error("only one index allowed to index %s", t1->toChars()); | |
7347 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
7348 return e->semantic(sc); | |
7349 } | |
7350 | |
7351 // Run semantic() on each argument | |
7352 for (size_t i = 0; i < arguments->dim; i++) | |
7353 { e = (Expression *)arguments->data[i]; | |
7354 | |
7355 e = e->semantic(sc); | |
7356 if (!e->type) | |
7357 error("%s has no value", e->toChars()); | |
7358 arguments->data[i] = (void *)e; | |
7359 } | |
7360 | |
7361 expandTuples(arguments); | |
7362 assert(arguments && arguments->dim); | |
7363 | |
7364 e = op_overload(sc); | |
7365 if (!e) | |
7366 { error("no [] operator overload for type %s", e1->type->toChars()); | |
7367 e = e1; | |
7368 } | |
7369 return e; | |
7370 } | |
7371 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7372 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7373 int ArrayExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7374 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7375 if (type && type->toBasetype()->ty == Tvoid) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7376 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7377 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7378 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7379 #endif |
159 | 7380 |
7381 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
7382 { | |
7383 if (type && type->toBasetype()->ty == Tvoid) | |
7384 error("voids have no value"); | |
7385 return this; | |
7386 } | |
7387 | |
7388 | |
7389 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7390 { int i; | |
7391 | |
7392 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7393 buf->writeByte('['); | |
7394 argsToCBuffer(buf, arguments, hgs); | |
7395 buf->writeByte(']'); | |
7396 } | |
7397 | |
7398 /************************* DotExp ***********************************/ | |
7399 | |
7400 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
7401 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
7402 { | |
7403 } | |
7404 | |
7405 Expression *DotExp::semantic(Scope *sc) | |
7406 { | |
7407 #if LOGSEMANTIC | |
7408 printf("DotExp::semantic('%s')\n", toChars()); | |
7409 if (type) printf("\ttype = %s\n", type->toChars()); | |
7410 #endif | |
7411 e1 = e1->semantic(sc); | |
7412 e2 = e2->semantic(sc); | |
7413 if (e2->op == TOKimport) | |
7414 { | |
7415 ScopeExp *se = (ScopeExp *)e2; | |
7416 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
7417 if (td) | |
7418 { Expression *e = new DotTemplateExp(loc, e1, td); | |
7419 e = e->semantic(sc); | |
7420 return e; | |
7421 } | |
7422 } | |
7423 if (!type) | |
7424 type = e2->type; | |
7425 return this; | |
7426 } | |
7427 | |
7428 | |
7429 /************************* CommaExp ***********************************/ | |
7430 | |
7431 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
7432 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
7433 { | |
7434 } | |
7435 | |
7436 Expression *CommaExp::semantic(Scope *sc) | |
7437 { | |
7438 if (!type) | |
7439 { BinExp::semanticp(sc); | |
7440 type = e2->type; | |
7441 } | |
7442 return this; | |
7443 } | |
7444 | |
7445 void CommaExp::checkEscape() | |
7446 { | |
7447 e2->checkEscape(); | |
7448 } | |
7449 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7450 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7451 int CommaExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7452 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7453 return e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7454 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7455 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7456 |
159 | 7457 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) |
7458 { | |
7459 e2 = e2->toLvalue(sc, NULL); | |
7460 return this; | |
7461 } | |
7462 | |
7463 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7464 { | |
7465 e2 = e2->modifiableLvalue(sc, e); | |
7466 return this; | |
7467 } | |
7468 | |
7469 int CommaExp::isBool(int result) | |
7470 { | |
7471 return e2->isBool(result); | |
7472 } | |
7473 | |
7474 int CommaExp::checkSideEffect(int flag) | |
7475 { | |
7476 if (flag == 2) | |
7477 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7478 else | |
7479 { | |
7480 // Don't check e1 until we cast(void) the a,b code generation | |
7481 return e2->checkSideEffect(flag); | |
7482 } | |
7483 } | |
7484 | |
7485 /************************** IndexExp **********************************/ | |
7486 | |
7487 // e1 [ e2 ] | |
7488 | |
7489 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7490 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7491 { | |
7492 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7493 lengthVar = NULL; | |
7494 modifiable = 0; // assume it is an rvalue | |
7495 } | |
7496 | |
7497 Expression *IndexExp::semantic(Scope *sc) | |
7498 { Expression *e; | |
7499 BinExp *b; | |
7500 UnaExp *u; | |
7501 Type *t1; | |
7502 ScopeDsymbol *sym; | |
7503 | |
7504 #if LOGSEMANTIC | |
7505 printf("IndexExp::semantic('%s')\n", toChars()); | |
7506 #endif | |
7507 if (type) | |
7508 return this; | |
7509 if (!e1->type) | |
7510 e1 = e1->semantic(sc); | |
7511 assert(e1->type); // semantic() should already be run on it | |
7512 e = this; | |
7513 | |
7514 // Note that unlike C we do not implement the int[ptr] | |
7515 | |
7516 t1 = e1->type->toBasetype(); | |
7517 | |
7518 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7519 { // Create scope for 'length' variable | |
7520 sym = new ArrayScopeSymbol(this); | |
7521 sym->loc = loc; | |
7522 sym->parent = sc->scopesym; | |
7523 sc = sc->push(sym); | |
7524 } | |
7525 | |
7526 e2 = e2->semantic(sc); | |
7527 if (!e2->type) | |
7528 { | |
7529 error("%s has no value", e2->toChars()); | |
7530 e2->type = Type::terror; | |
7531 } | |
7532 e2 = resolveProperties(sc, e2); | |
7533 | |
7534 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7535 sc = sc->pop(); | |
7536 | |
7537 switch (t1->ty) | |
7538 { | |
7539 case Tpointer: | |
7540 case Tarray: | |
7541 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7542 e->type = t1->next; | |
7543 break; | |
7544 | |
7545 case Tsarray: | |
7546 { | |
7547 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7548 | |
7549 TypeSArray *tsa = (TypeSArray *)t1; | |
7550 | |
7551 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7552 // Do compile time array bounds checking if possible | |
7553 e2 = e2->optimize(WANTvalue); | |
7554 if (e2->op == TOKint64) | |
7555 { | |
7556 integer_t index = e2->toInteger(); | |
7557 integer_t length = tsa->dim->toInteger(); | |
7558 if (index < 0 || index >= length) | |
7559 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7560 index, length); | |
7561 } | |
7562 #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
|
7563 e->type = t1->nextOf(); |
159 | 7564 break; |
7565 } | |
7566 | |
7567 case Taarray: | |
7568 { TypeAArray *taa = (TypeAArray *)t1; | |
7569 | |
7570 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7571 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7572 type = taa->next; | |
7573 break; | |
7574 } | |
7575 | |
7576 case Ttuple: | |
7577 { | |
7578 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7579 e2 = e2->optimize(WANTvalue); | |
7580 uinteger_t index = e2->toUInteger(); | |
7581 size_t length; | |
7582 TupleExp *te; | |
7583 TypeTuple *tup; | |
7584 | |
7585 if (e1->op == TOKtuple) | |
7586 { te = (TupleExp *)e1; | |
7587 length = te->exps->dim; | |
7588 } | |
7589 else if (e1->op == TOKtype) | |
7590 { | |
7591 tup = (TypeTuple *)t1; | |
7592 length = Argument::dim(tup->arguments); | |
7593 } | |
7594 else | |
7595 assert(0); | |
7596 | |
7597 if (index < length) | |
7598 { | |
7599 | |
7600 if (e1->op == TOKtuple) | |
7601 e = (Expression *)te->exps->data[(size_t)index]; | |
7602 else | |
7603 e = new TypeExp(e1->loc, Argument::getNth(tup->arguments, (size_t)index)->type); | |
7604 } | |
7605 else | |
7606 { | |
305
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7607 error("array index [%llu] is outside array bounds [0 .. %"PRIuSIZE"]", |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7608 index, length); |
2b72433d5c8c
[svn r326] Fixed a bunch of issues with printf's that MinGW32 did not support.
lindquist
parents:
285
diff
changeset
|
7609 e = e1; |
159 | 7610 } |
7611 break; | |
7612 } | |
7613 | |
7614 default: | |
7615 error("%s must be an array or pointer type, not %s", | |
7616 e1->toChars(), e1->type->toChars()); | |
7617 type = Type::tint32; | |
7618 break; | |
7619 } | |
7620 return e; | |
7621 } | |
7622 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7623 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7624 int IndexExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7625 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7626 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7627 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7628 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7629 |
159 | 7630 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) |
7631 { | |
7632 // if (type && type->toBasetype()->ty == Tvoid) | |
7633 // error("voids have no value"); | |
7634 return this; | |
7635 } | |
7636 | |
7637 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
7638 { | |
7639 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
7640 modifiable = 1; | |
7641 if (e1->op == TOKstring) | |
7642 error("string literals are immutable"); | |
7643 if (e1->type->toBasetype()->ty == Taarray) | |
7644 e1 = e1->modifiableLvalue(sc, e1); | |
7645 return toLvalue(sc, e); | |
7646 } | |
7647 | |
7648 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7649 { | |
7650 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7651 buf->writeByte('['); | |
7652 expToCBuffer(buf, hgs, e2, PREC_assign); | |
7653 buf->writeByte(']'); | |
7654 } | |
7655 | |
7656 | |
7657 /************************* PostExp ***********************************/ | |
7658 | |
7659 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
7660 : BinExp(loc, op, sizeof(PostExp), e, | |
7661 new IntegerExp(loc, 1, Type::tint32)) | |
7662 { | |
7663 } | |
7664 | |
7665 Expression *PostExp::semantic(Scope *sc) | |
7666 { Expression *e = this; | |
7667 | |
7668 if (!type) | |
7669 { | |
7670 BinExp::semantic(sc); | |
7671 e2 = resolveProperties(sc, e2); | |
7672 | |
7673 e = op_overload(sc); | |
7674 if (e) | |
7675 return e; | |
7676 | |
7677 e = this; | |
7678 e1 = e1->modifiableLvalue(sc, e1); | |
7679 e1->checkScalar(); | |
7680 e1->checkNoBool(); | |
7681 if (e1->type->ty == Tpointer) | |
7682 e = scaleFactor(sc); | |
7683 else | |
7684 e2 = e2->castTo(sc, e1->type); | |
7685 e->type = e1->type; | |
7686 } | |
7687 return e; | |
7688 } | |
7689 | |
7690 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7691 { | |
7692 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7693 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
7694 } | |
7695 | |
7696 /************************************************************/ | |
7697 | |
7698 /* Can be TOKconstruct too */ | |
7699 | |
7700 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
7701 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
7702 { | |
7703 ismemset = 0; | |
7704 } | |
7705 | |
7706 Expression *AssignExp::semantic(Scope *sc) | |
7707 { Type *t1; | |
7708 Expression *e1old = e1; | |
7709 | |
7710 #if LOGSEMANTIC | |
7711 printf("AssignExp::semantic('%s')\n", toChars()); | |
7712 #endif | |
7713 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
7714 | |
7715 /* Look for operator overloading of a[i]=value. | |
7716 * Do it before semantic() otherwise the a[i] will have been | |
7717 * converted to a.opIndex() already. | |
7718 */ | |
7719 if (e1->op == TOKarray) | |
7720 { Type *t1; | |
7721 ArrayExp *ae = (ArrayExp *)e1; | |
7722 AggregateDeclaration *ad; | |
7723 Identifier *id = Id::index; | |
7724 | |
7725 ae->e1 = ae->e1->semantic(sc); | |
7726 t1 = ae->e1->type->toBasetype(); | |
7727 if (t1->ty == Tstruct) | |
7728 { | |
7729 ad = ((TypeStruct *)t1)->sym; | |
7730 goto L1; | |
7731 } | |
7732 else if (t1->ty == Tclass) | |
7733 { | |
7734 ad = ((TypeClass *)t1)->sym; | |
7735 L1: | |
7736 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
7737 if (search_function(ad, Id::indexass)) | |
7738 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
7739 Expressions *a = (Expressions *)ae->arguments->copy(); | |
7740 | |
7741 a->insert(0, e2); | |
7742 e = new CallExp(loc, e, a); | |
7743 e = e->semantic(sc); | |
7744 return e; | |
7745 } | |
7746 else | |
7747 { | |
7748 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
7749 if (search_function(ad, id)) | |
7750 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
7751 | |
7752 if (1 || !global.params.useDeprecated) | |
7753 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
7754 | |
7755 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
7756 e = e->semantic(sc); | |
7757 return e; | |
7758 } | |
7759 } | |
7760 } | |
7761 } | |
7762 /* Look for operator overloading of a[i..j]=value. | |
7763 * Do it before semantic() otherwise the a[i..j] will have been | |
7764 * converted to a.opSlice() already. | |
7765 */ | |
7766 if (e1->op == TOKslice) | |
7767 { Type *t1; | |
7768 SliceExp *ae = (SliceExp *)e1; | |
7769 AggregateDeclaration *ad; | |
7770 Identifier *id = Id::index; | |
7771 | |
7772 ae->e1 = ae->e1->semantic(sc); | |
7773 ae->e1 = resolveProperties(sc, ae->e1); | |
7774 t1 = ae->e1->type->toBasetype(); | |
7775 if (t1->ty == Tstruct) | |
7776 { | |
7777 ad = ((TypeStruct *)t1)->sym; | |
7778 goto L2; | |
7779 } | |
7780 else if (t1->ty == Tclass) | |
7781 { | |
7782 ad = ((TypeClass *)t1)->sym; | |
7783 L2: | |
7784 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
7785 if (search_function(ad, Id::sliceass)) | |
7786 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
7787 Expressions *a = new Expressions(); | |
7788 | |
7789 a->push(e2); | |
7790 if (ae->lwr) | |
7791 { a->push(ae->lwr); | |
7792 assert(ae->upr); | |
7793 a->push(ae->upr); | |
7794 } | |
7795 else | |
7796 assert(!ae->upr); | |
7797 e = new CallExp(loc, e, a); | |
7798 e = e->semantic(sc); | |
7799 return e; | |
7800 } | |
7801 } | |
7802 } | |
7803 | |
7804 BinExp::semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7805 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7806 if (e1->op == TOKdottd) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7807 { // 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
|
7808 Expression *e = new CallExp(loc, e1, e2); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7809 e = e->semantic(sc); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7810 return e; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7811 } |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
7812 |
159 | 7813 e2 = resolveProperties(sc, e2); |
7814 assert(e1->type); | |
7815 | |
7816 /* Rewrite tuple assignment as a tuple of assignments. | |
7817 */ | |
7818 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
7819 { TupleExp *tup1 = (TupleExp *)e1; | |
7820 TupleExp *tup2 = (TupleExp *)e2; | |
7821 size_t dim = tup1->exps->dim; | |
7822 if (dim != tup2->exps->dim) | |
7823 { | |
7824 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
7825 } | |
7826 else | |
7827 { Expressions *exps = new Expressions; | |
7828 exps->setDim(dim); | |
7829 | |
7830 for (int i = 0; i < dim; i++) | |
7831 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
7832 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
7833 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
7834 } | |
7835 Expression *e = new TupleExp(loc, exps); | |
7836 e = e->semantic(sc); | |
7837 return e; | |
7838 } | |
7839 } | |
7840 | |
7841 t1 = e1->type->toBasetype(); | |
7842 | |
7843 if (t1->ty == Tfunction) | |
7844 { // Rewrite f=value to f(value) | |
7845 Expression *e; | |
7846 | |
7847 e = new CallExp(loc, e1, e2); | |
7848 e = e->semantic(sc); | |
7849 return e; | |
7850 } | |
7851 | |
7852 /* If it is an assignment from a 'foreign' type, | |
7853 * check for operator overloading. | |
7854 */ | |
7855 if (t1->ty == Tclass || t1->ty == Tstruct) | |
7856 { | |
7857 if (!e2->type->implicitConvTo(e1->type)) | |
7858 { | |
7859 Expression *e = op_overload(sc); | |
7860 if (e) | |
7861 return e; | |
7862 } | |
7863 } | |
7864 | |
7865 e2->rvalue(); | |
7866 | |
7867 if (e1->op == TOKarraylength) | |
7868 { | |
7869 // e1 is not an lvalue, but we let code generator handle it | |
7870 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
7871 | |
7872 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
7873 } | |
7874 else if (e1->op == TOKslice) | |
7875 ; | |
7876 else | |
7877 { // Try to do a decent error message with the expression | |
7878 // before it got constant folded | |
7879 e1 = e1->modifiableLvalue(sc, e1old); | |
7880 } | |
7881 | |
7882 if (e1->op == TOKslice && | |
7883 t1->nextOf() && | |
7884 e2->implicitConvTo(t1->nextOf()) | |
7885 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
7886 ) | |
7887 { // memset | |
7888 ismemset = 1; // make it easy for back end to tell what this is | |
7889 e2 = e2->implicitCastTo(sc, t1->next); | |
7890 } | |
7891 else if (t1->ty == Tsarray) | |
7892 { | |
7893 error("cannot assign to static array %s", e1->toChars()); | |
7894 } | |
7895 else | |
7896 { | |
7897 e2 = e2->implicitCastTo(sc, e1->type); | |
7898 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7899 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7900 /* 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
|
7901 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7902 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
|
7903 (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
|
7904 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
|
7905 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
|
7906 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
|
7907 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
|
7908 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7909 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
|
7910 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
|
7911 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7912 |
159 | 7913 type = e1->type; |
7914 assert(type); | |
7915 return this; | |
7916 } | |
7917 | |
7918 Expression *AssignExp::checkToBoolean() | |
7919 { | |
7920 // Things like: | |
7921 // if (a = b) ... | |
7922 // are usually mistakes. | |
7923 | |
7924 error("'=' does not give a boolean result"); | |
7925 return this; | |
7926 } | |
7927 | |
7928 /************************************************************/ | |
7929 | |
7930 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
7931 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
7932 { | |
7933 } | |
7934 | |
7935 Expression *AddAssignExp::semantic(Scope *sc) | |
7936 { Expression *e; | |
7937 | |
7938 if (type) | |
7939 return this; | |
7940 | |
7941 BinExp::semantic(sc); | |
7942 e2 = resolveProperties(sc, e2); | |
7943 | |
7944 e = op_overload(sc); | |
7945 if (e) | |
7946 return e; | |
7947 | |
7948 Type *tb1 = e1->type->toBasetype(); | |
7949 Type *tb2 = e2->type->toBasetype(); | |
7950 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7951 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
|
7952 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7953 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
|
7954 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
|
7955 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
|
7956 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7957 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7958 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7959 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
|
7960 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7961 |
159 | 7962 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
7963 (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
|
7964 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 7965 ) |
7966 { | |
7967 type = e1->type; | |
7968 e = this; | |
7969 } | |
7970 else | |
7971 { | |
7972 e1->checkScalar(); | |
7973 e1->checkNoBool(); | |
7974 if (tb1->ty == Tpointer && tb2->isintegral()) | |
7975 e = scaleFactor(sc); | |
7976 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
7977 { | |
7978 #if 0 | |
7979 // Need to rethink this | |
7980 if (e1->op != TOKvar) | |
7981 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
7982 VarDeclaration *v; | |
7983 Expression *ea; | |
7984 Expression *ex; | |
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 Identifier *id = Lexer::uniqueId("__name"); |
159 | 7987 |
7988 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
7989 v->semantic(sc); | |
7990 if (!sc->insert(v)) | |
7991 assert(0); | |
7992 v->parent = sc->func; | |
7993 | |
7994 ea = new AddrExp(loc, e1); | |
7995 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
7996 | |
7997 ex = new VarExp(loc, v); | |
7998 ex = new PtrExp(loc, ex); | |
7999 e = new AddExp(loc, ex, e2); | |
8000 e = new CastExp(loc, e, e1->type); | |
8001 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
8002 | |
8003 e = new CommaExp(loc, ea, e); | |
8004 } | |
8005 else | |
8006 #endif | |
8007 { // Rewrite e1+=e2 to e1=e1+e2 | |
8008 // BUG: doesn't account for side effects in e1 | |
8009 // BUG: other assignment operators for bits aren't handled at all | |
8010 e = new AddExp(loc, e1, e2); | |
8011 e = new CastExp(loc, e, e1->type); | |
8012 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
8013 } | |
8014 e = e->semantic(sc); | |
8015 } | |
8016 else | |
8017 { | |
8018 type = e1->type; | |
8019 typeCombine(sc); | |
8020 e1->checkArithmetic(); | |
8021 e2->checkArithmetic(); | |
8022 if (type->isreal() || type->isimaginary()) | |
8023 { | |
8024 assert(global.errors || e2->type->isfloating()); | |
8025 e2 = e2->castTo(sc, e1->type); | |
8026 } | |
8027 e = this; | |
8028 } | |
8029 } | |
8030 return e; | |
8031 } | |
8032 | |
8033 /************************************************************/ | |
8034 | |
8035 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8036 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
8037 { | |
8038 } | |
8039 | |
8040 Expression *MinAssignExp::semantic(Scope *sc) | |
8041 { Expression *e; | |
8042 | |
8043 if (type) | |
8044 return this; | |
8045 | |
8046 BinExp::semantic(sc); | |
8047 e2 = resolveProperties(sc, e2); | |
8048 | |
8049 e = op_overload(sc); | |
8050 if (e) | |
8051 return e; | |
8052 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8053 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
|
8054 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8055 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
|
8056 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
|
8057 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
|
8058 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8059 |
159 | 8060 e1 = e1->modifiableLvalue(sc, e1); |
8061 e1->checkScalar(); | |
8062 e1->checkNoBool(); | |
8063 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
8064 e = scaleFactor(sc); | |
8065 else | |
8066 { | |
8067 e1 = e1->checkArithmetic(); | |
8068 e2 = e2->checkArithmetic(); | |
8069 type = e1->type; | |
8070 typeCombine(sc); | |
8071 if (type->isreal() || type->isimaginary()) | |
8072 { | |
8073 assert(e2->type->isfloating()); | |
8074 e2 = e2->castTo(sc, e1->type); | |
8075 } | |
8076 e = this; | |
8077 } | |
8078 return e; | |
8079 } | |
8080 | |
8081 /************************************************************/ | |
8082 | |
8083 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8084 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
8085 { | |
8086 } | |
8087 | |
8088 Expression *CatAssignExp::semantic(Scope *sc) | |
8089 { Expression *e; | |
8090 | |
8091 BinExp::semantic(sc); | |
8092 e2 = resolveProperties(sc, e2); | |
8093 | |
8094 e = op_overload(sc); | |
8095 if (e) | |
8096 return e; | |
8097 | |
8098 if (e1->op == TOKslice) | |
8099 { SliceExp *se = (SliceExp *)e1; | |
8100 | |
8101 if (se->e1->type->toBasetype()->ty == Tsarray) | |
8102 error("cannot append to static array %s", se->e1->type->toChars()); | |
8103 } | |
8104 | |
8105 e1 = e1->modifiableLvalue(sc, e1); | |
8106 | |
8107 Type *tb1 = e1->type->toBasetype(); | |
8108 Type *tb2 = e2->type->toBasetype(); | |
8109 | |
8110 e2->rvalue(); | |
8111 | |
8112 if ((tb1->ty == Tarray) && | |
8113 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
8114 e2->implicitConvTo(e1->type) | |
8115 //e1->type->next->equals(e2->type->next) | |
8116 ) | |
8117 { // Append array | |
8118 e2 = e2->castTo(sc, e1->type); | |
8119 type = e1->type; | |
8120 e = this; | |
8121 } | |
8122 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
|
8123 e2->implicitConvTo(tb1->nextOf()) |
159 | 8124 ) |
8125 { // 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
|
8126 e2 = e2->castTo(sc, tb1->nextOf()); |
159 | 8127 type = e1->type; |
8128 e = this; | |
8129 } | |
8130 else | |
8131 { | |
8132 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
8133 type = Type::tint32; | |
8134 e = this; | |
8135 } | |
8136 return e; | |
8137 } | |
8138 | |
8139 /************************************************************/ | |
8140 | |
8141 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8142 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
8143 { | |
8144 } | |
8145 | |
8146 Expression *MulAssignExp::semantic(Scope *sc) | |
8147 { Expression *e; | |
8148 | |
8149 BinExp::semantic(sc); | |
8150 e2 = resolveProperties(sc, e2); | |
8151 | |
8152 e = op_overload(sc); | |
8153 if (e) | |
8154 return e; | |
8155 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8156 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
|
8157 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8158 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
|
8159 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
|
8160 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
|
8161 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8162 |
159 | 8163 e1 = e1->modifiableLvalue(sc, e1); |
8164 e1->checkScalar(); | |
8165 e1->checkNoBool(); | |
8166 type = e1->type; | |
8167 typeCombine(sc); | |
8168 e1->checkArithmetic(); | |
8169 e2->checkArithmetic(); | |
8170 if (e2->type->isfloating()) | |
8171 { Type *t1; | |
8172 Type *t2; | |
8173 | |
8174 t1 = e1->type; | |
8175 t2 = e2->type; | |
8176 if (t1->isreal()) | |
8177 { | |
8178 if (t2->isimaginary() || t2->iscomplex()) | |
8179 { | |
8180 e2 = e2->castTo(sc, t1); | |
8181 } | |
8182 } | |
8183 else if (t1->isimaginary()) | |
8184 { | |
8185 if (t2->isimaginary() || t2->iscomplex()) | |
8186 { | |
8187 switch (t1->ty) | |
8188 { | |
8189 case Timaginary32: t2 = Type::tfloat32; break; | |
8190 case Timaginary64: t2 = Type::tfloat64; break; | |
8191 case Timaginary80: t2 = Type::tfloat80; break; | |
8192 default: | |
8193 assert(0); | |
8194 } | |
8195 e2 = e2->castTo(sc, t2); | |
8196 } | |
8197 } | |
8198 } | |
8199 return this; | |
8200 } | |
8201 | |
8202 /************************************************************/ | |
8203 | |
8204 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8205 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
8206 { | |
8207 } | |
8208 | |
8209 Expression *DivAssignExp::semantic(Scope *sc) | |
8210 { Expression *e; | |
8211 | |
8212 BinExp::semantic(sc); | |
8213 e2 = resolveProperties(sc, e2); | |
8214 | |
8215 e = op_overload(sc); | |
8216 if (e) | |
8217 return e; | |
8218 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8219 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
|
8220 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8221 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
|
8222 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
|
8223 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
|
8224 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8225 |
159 | 8226 e1 = e1->modifiableLvalue(sc, e1); |
8227 e1->checkScalar(); | |
8228 e1->checkNoBool(); | |
8229 type = e1->type; | |
8230 typeCombine(sc); | |
8231 e1->checkArithmetic(); | |
8232 e2->checkArithmetic(); | |
8233 if (e2->type->isimaginary()) | |
8234 { Type *t1; | |
8235 Type *t2; | |
8236 | |
8237 t1 = e1->type; | |
8238 if (t1->isreal()) | |
8239 { // x/iv = i(-x/v) | |
8240 // Therefore, the result is 0 | |
8241 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
8242 e2->type = t1; | |
8243 e = new AssignExp(loc, e1, e2); | |
8244 e->type = t1; | |
8245 return e; | |
8246 } | |
8247 else if (t1->isimaginary()) | |
8248 { Expression *e; | |
8249 | |
8250 switch (t1->ty) | |
8251 { | |
8252 case Timaginary32: t2 = Type::tfloat32; break; | |
8253 case Timaginary64: t2 = Type::tfloat64; break; | |
8254 case Timaginary80: t2 = Type::tfloat80; break; | |
8255 default: | |
8256 assert(0); | |
8257 } | |
8258 e2 = e2->castTo(sc, t2); | |
8259 e = new AssignExp(loc, e1, e2); | |
8260 e->type = t1; | |
8261 return e; | |
8262 } | |
8263 } | |
8264 return this; | |
8265 } | |
8266 | |
8267 /************************************************************/ | |
8268 | |
8269 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8270 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
8271 { | |
8272 } | |
8273 | |
8274 Expression *ModAssignExp::semantic(Scope *sc) | |
8275 { | |
8276 return commonSemanticAssign(sc); | |
8277 } | |
8278 | |
8279 /************************************************************/ | |
8280 | |
8281 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8282 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
8283 { | |
8284 } | |
8285 | |
8286 Expression *ShlAssignExp::semantic(Scope *sc) | |
8287 { Expression *e; | |
8288 | |
8289 //printf("ShlAssignExp::semantic()\n"); | |
8290 BinExp::semantic(sc); | |
8291 e2 = resolveProperties(sc, e2); | |
8292 | |
8293 e = op_overload(sc); | |
8294 if (e) | |
8295 return e; | |
8296 | |
8297 e1 = e1->modifiableLvalue(sc, e1); | |
8298 e1->checkScalar(); | |
8299 e1->checkNoBool(); | |
8300 type = e1->type; | |
8301 typeCombine(sc); | |
8302 e1->checkIntegral(); | |
8303 e2 = e2->checkIntegral(); | |
8304 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8305 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8306 return this; |
8307 } | |
8308 | |
8309 /************************************************************/ | |
8310 | |
8311 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8312 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
8313 { | |
8314 } | |
8315 | |
8316 Expression *ShrAssignExp::semantic(Scope *sc) | |
8317 { Expression *e; | |
8318 | |
8319 BinExp::semantic(sc); | |
8320 e2 = resolveProperties(sc, e2); | |
8321 | |
8322 e = op_overload(sc); | |
8323 if (e) | |
8324 return e; | |
8325 | |
8326 e1 = e1->modifiableLvalue(sc, e1); | |
8327 e1->checkScalar(); | |
8328 e1->checkNoBool(); | |
8329 type = e1->type; | |
8330 typeCombine(sc); | |
8331 e1->checkIntegral(); | |
8332 e2 = e2->checkIntegral(); | |
8333 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8334 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8335 return this; |
8336 } | |
8337 | |
8338 /************************************************************/ | |
8339 | |
8340 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8341 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
8342 { | |
8343 } | |
8344 | |
8345 Expression *UshrAssignExp::semantic(Scope *sc) | |
8346 { Expression *e; | |
8347 | |
8348 BinExp::semantic(sc); | |
8349 e2 = resolveProperties(sc, e2); | |
8350 | |
8351 e = op_overload(sc); | |
8352 if (e) | |
8353 return e; | |
8354 | |
8355 e1 = e1->modifiableLvalue(sc, e1); | |
8356 e1->checkScalar(); | |
8357 e1->checkNoBool(); | |
8358 type = e1->type; | |
8359 typeCombine(sc); | |
8360 e1->checkIntegral(); | |
8361 e2 = e2->checkIntegral(); | |
8362 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8363 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8364 return this; |
8365 } | |
8366 | |
8367 /************************************************************/ | |
8368 | |
8369 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8370 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
8371 { | |
8372 } | |
8373 | |
8374 Expression *AndAssignExp::semantic(Scope *sc) | |
8375 { | |
8376 return commonSemanticAssignIntegral(sc); | |
8377 } | |
8378 | |
8379 /************************************************************/ | |
8380 | |
8381 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8382 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
8383 { | |
8384 } | |
8385 | |
8386 Expression *OrAssignExp::semantic(Scope *sc) | |
8387 { | |
8388 return commonSemanticAssignIntegral(sc); | |
8389 } | |
8390 | |
8391 /************************************************************/ | |
8392 | |
8393 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8394 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
8395 { | |
8396 } | |
8397 | |
8398 Expression *XorAssignExp::semantic(Scope *sc) | |
8399 { | |
8400 return commonSemanticAssignIntegral(sc); | |
8401 } | |
8402 | |
8403 /************************* AddExp *****************************/ | |
8404 | |
8405 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
8406 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
8407 { | |
8408 } | |
8409 | |
8410 Expression *AddExp::semantic(Scope *sc) | |
8411 { Expression *e; | |
8412 | |
8413 #if LOGSEMANTIC | |
8414 printf("AddExp::semantic('%s')\n", toChars()); | |
8415 #endif | |
8416 if (!type) | |
8417 { | |
8418 BinExp::semanticp(sc); | |
8419 | |
8420 e = op_overload(sc); | |
8421 if (e) | |
8422 return e; | |
8423 | |
8424 Type *tb1 = e1->type->toBasetype(); | |
8425 Type *tb2 = e2->type->toBasetype(); | |
8426 | |
8427 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
8428 (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
|
8429 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8430 ) |
8431 { | |
8432 type = e1->type; | |
8433 e = this; | |
8434 } | |
8435 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
8436 tb2->ty == Tpointer && e1->type->isintegral()) | |
8437 e = scaleFactor(sc); | |
8438 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
8439 { | |
8440 incompatibleTypes(); | |
8441 type = e1->type; | |
8442 e = this; | |
8443 } | |
8444 else | |
8445 { | |
8446 typeCombine(sc); | |
8447 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
8448 (e1->type->isimaginary() && e2->type->isreal())) | |
8449 { | |
8450 switch (type->toBasetype()->ty) | |
8451 { | |
8452 case Tfloat32: | |
8453 case Timaginary32: | |
8454 type = Type::tcomplex32; | |
8455 break; | |
8456 | |
8457 case Tfloat64: | |
8458 case Timaginary64: | |
8459 type = Type::tcomplex64; | |
8460 break; | |
8461 | |
8462 case Tfloat80: | |
8463 case Timaginary80: | |
8464 type = Type::tcomplex80; | |
8465 break; | |
8466 | |
8467 default: | |
8468 assert(0); | |
8469 } | |
8470 } | |
8471 e = this; | |
8472 } | |
8473 return e; | |
8474 } | |
8475 return this; | |
8476 } | |
8477 | |
8478 /************************************************************/ | |
8479 | |
8480 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8481 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8482 { | |
8483 } | |
8484 | |
8485 Expression *MinExp::semantic(Scope *sc) | |
8486 { Expression *e; | |
8487 Type *t1; | |
8488 Type *t2; | |
8489 | |
8490 #if LOGSEMANTIC | |
8491 printf("MinExp::semantic('%s')\n", toChars()); | |
8492 #endif | |
8493 if (type) | |
8494 return this; | |
8495 | |
8496 BinExp::semanticp(sc); | |
8497 | |
8498 e = op_overload(sc); | |
8499 if (e) | |
8500 return e; | |
8501 | |
8502 e = this; | |
8503 t1 = e1->type->toBasetype(); | |
8504 t2 = e2->type->toBasetype(); | |
8505 if (t1->ty == Tpointer) | |
8506 { | |
8507 if (t2->ty == Tpointer) | |
8508 { // Need to divide the result by the stride | |
8509 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8510 d_int64 stride; | |
8511 Expression *e; | |
8512 | |
8513 typeCombine(sc); // make sure pointer types are compatible | |
8514 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
|
8515 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
|
8516 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
|
8517 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8518 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
|
8519 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8520 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8521 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8522 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
|
8523 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
|
8524 } |
159 | 8525 return e; |
8526 } | |
8527 else if (t2->isintegral()) | |
8528 e = scaleFactor(sc); | |
8529 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
|
8530 { error("incompatible types for minus"); |
159 | 8531 return new IntegerExp(0); |
8532 } | |
8533 } | |
8534 else if (t2->ty == Tpointer) | |
8535 { | |
8536 type = e2->type; | |
8537 error("can't subtract pointer from %s", e1->type->toChars()); | |
8538 return new IntegerExp(0); | |
8539 } | |
8540 else | |
8541 { | |
8542 typeCombine(sc); | |
8543 t1 = e1->type->toBasetype(); | |
8544 t2 = e2->type->toBasetype(); | |
8545 if ((t1->isreal() && t2->isimaginary()) || | |
8546 (t1->isimaginary() && t2->isreal())) | |
8547 { | |
8548 switch (type->ty) | |
8549 { | |
8550 case Tfloat32: | |
8551 case Timaginary32: | |
8552 type = Type::tcomplex32; | |
8553 break; | |
8554 | |
8555 case Tfloat64: | |
8556 case Timaginary64: | |
8557 type = Type::tcomplex64; | |
8558 break; | |
8559 | |
8560 case Tfloat80: | |
8561 case Timaginary80: | |
8562 type = Type::tcomplex80; | |
8563 break; | |
8564 | |
8565 default: | |
8566 assert(0); | |
8567 } | |
8568 } | |
8569 } | |
8570 return e; | |
8571 } | |
8572 | |
8573 /************************* CatExp *****************************/ | |
8574 | |
8575 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
8576 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
8577 { | |
8578 } | |
8579 | |
8580 Expression *CatExp::semantic(Scope *sc) | |
8581 { Expression *e; | |
8582 | |
8583 //printf("CatExp::semantic() %s\n", toChars()); | |
8584 if (!type) | |
8585 { | |
8586 BinExp::semanticp(sc); | |
8587 e = op_overload(sc); | |
8588 if (e) | |
8589 return e; | |
8590 | |
8591 Type *tb1 = e1->type->toBasetype(); | |
8592 Type *tb2 = e2->type->toBasetype(); | |
8593 | |
8594 | |
8595 /* BUG: Should handle things like: | |
8596 * char c; | |
8597 * c ~ ' ' | |
8598 * ' ' ~ c; | |
8599 */ | |
8600 | |
8601 #if 0 | |
8602 e1->type->print(); | |
8603 e2->type->print(); | |
8604 #endif | |
8605 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
8606 e2->type->equals(tb1->next)) | |
8607 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8608 type = tb1->nextOf()->arrayOf(); |
159 | 8609 if (tb2->ty == Tarray) |
8610 { // Make e2 into [e2] | |
8611 e2 = new ArrayLiteralExp(e2->loc, e2); | |
8612 e2->type = type; | |
8613 } | |
8614 return this; | |
8615 } | |
8616 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
8617 e1->type->equals(tb2->next)) | |
8618 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8619 type = tb2->nextOf()->arrayOf(); |
159 | 8620 if (tb1->ty == Tarray) |
8621 { // Make e1 into [e1] | |
8622 e1 = new ArrayLiteralExp(e1->loc, e1); | |
8623 e1->type = type; | |
8624 } | |
8625 return this; | |
8626 } | |
8627 | |
8628 typeCombine(sc); | |
8629 | |
8630 if (type->toBasetype()->ty == Tsarray) | |
8631 type = type->toBasetype()->next->arrayOf(); | |
8632 #if 0 | |
8633 e1->type->print(); | |
8634 e2->type->print(); | |
8635 type->print(); | |
8636 print(); | |
8637 #endif | |
8638 if (e1->op == TOKstring && e2->op == TOKstring) | |
8639 e = optimize(WANTvalue); | |
8640 else if (e1->type->equals(e2->type) && | |
8641 (e1->type->toBasetype()->ty == Tarray || | |
8642 e1->type->toBasetype()->ty == Tsarray)) | |
8643 { | |
8644 e = this; | |
8645 } | |
8646 else | |
8647 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8648 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 8649 error("Can only concatenate arrays, not (%s ~ %s)", |
8650 e1->type->toChars(), e2->type->toChars()); | |
8651 type = Type::tint32; | |
8652 e = this; | |
8653 } | |
8654 e->type = e->type->semantic(loc, sc); | |
8655 return e; | |
8656 } | |
8657 return this; | |
8658 } | |
8659 | |
8660 /************************************************************/ | |
8661 | |
8662 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
8663 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
8664 { | |
8665 } | |
8666 | |
8667 Expression *MulExp::semantic(Scope *sc) | |
8668 { Expression *e; | |
8669 | |
8670 #if 0 | |
8671 printf("MulExp::semantic() %s\n", toChars()); | |
8672 #endif | |
8673 if (type) | |
8674 { | |
8675 return this; | |
8676 } | |
8677 | |
8678 BinExp::semanticp(sc); | |
8679 e = op_overload(sc); | |
8680 if (e) | |
8681 return e; | |
8682 | |
8683 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
|
8684 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
|
8685 { 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
|
8686 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
|
8687 } |
159 | 8688 if (type->isfloating()) |
8689 { Type *t1 = e1->type; | |
8690 Type *t2 = e2->type; | |
8691 | |
8692 if (t1->isreal()) | |
8693 { | |
8694 type = t2; | |
8695 } | |
8696 else if (t2->isreal()) | |
8697 { | |
8698 type = t1; | |
8699 } | |
8700 else if (t1->isimaginary()) | |
8701 { | |
8702 if (t2->isimaginary()) | |
8703 { Expression *e; | |
8704 | |
8705 switch (t1->ty) | |
8706 { | |
8707 case Timaginary32: type = Type::tfloat32; break; | |
8708 case Timaginary64: type = Type::tfloat64; break; | |
8709 case Timaginary80: type = Type::tfloat80; break; | |
8710 default: assert(0); | |
8711 } | |
8712 | |
8713 // iy * iv = -yv | |
8714 e1->type = type; | |
8715 e2->type = type; | |
8716 e = new NegExp(loc, this); | |
8717 e = e->semantic(sc); | |
8718 return e; | |
8719 } | |
8720 else | |
8721 type = t2; // t2 is complex | |
8722 } | |
8723 else if (t2->isimaginary()) | |
8724 { | |
8725 type = t1; // t1 is complex | |
8726 } | |
8727 } | |
8728 return this; | |
8729 } | |
8730 | |
8731 /************************************************************/ | |
8732 | |
8733 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
8734 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
8735 { | |
8736 } | |
8737 | |
8738 Expression *DivExp::semantic(Scope *sc) | |
8739 { Expression *e; | |
8740 | |
8741 if (type) | |
8742 return this; | |
8743 | |
8744 BinExp::semanticp(sc); | |
8745 e = op_overload(sc); | |
8746 if (e) | |
8747 return e; | |
8748 | |
8749 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
|
8750 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
|
8751 { 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
|
8752 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
|
8753 } |
159 | 8754 if (type->isfloating()) |
8755 { Type *t1 = e1->type; | |
8756 Type *t2 = e2->type; | |
8757 | |
8758 if (t1->isreal()) | |
8759 { | |
8760 type = t2; | |
8761 if (t2->isimaginary()) | |
8762 { Expression *e; | |
8763 | |
8764 // x/iv = i(-x/v) | |
8765 e2->type = t1; | |
8766 e = new NegExp(loc, this); | |
8767 e = e->semantic(sc); | |
8768 return e; | |
8769 } | |
8770 } | |
8771 else if (t2->isreal()) | |
8772 { | |
8773 type = t1; | |
8774 } | |
8775 else if (t1->isimaginary()) | |
8776 { | |
8777 if (t2->isimaginary()) | |
8778 { | |
8779 switch (t1->ty) | |
8780 { | |
8781 case Timaginary32: type = Type::tfloat32; break; | |
8782 case Timaginary64: type = Type::tfloat64; break; | |
8783 case Timaginary80: type = Type::tfloat80; break; | |
8784 default: assert(0); | |
8785 } | |
8786 } | |
8787 else | |
8788 type = t2; // t2 is complex | |
8789 } | |
8790 else if (t2->isimaginary()) | |
8791 { | |
8792 type = t1; // t1 is complex | |
8793 } | |
8794 } | |
8795 return this; | |
8796 } | |
8797 | |
8798 /************************************************************/ | |
8799 | |
8800 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
8801 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
8802 { | |
8803 } | |
8804 | |
8805 Expression *ModExp::semantic(Scope *sc) | |
8806 { Expression *e; | |
8807 | |
8808 if (type) | |
8809 return this; | |
8810 | |
8811 BinExp::semanticp(sc); | |
8812 e = op_overload(sc); | |
8813 if (e) | |
8814 return e; | |
8815 | |
8816 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
|
8817 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
|
8818 { 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
|
8819 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
|
8820 } |
159 | 8821 if (type->isfloating()) |
8822 { type = e1->type; | |
8823 if (e2->type->iscomplex()) | |
8824 { error("cannot perform modulo complex arithmetic"); | |
8825 return new IntegerExp(0); | |
8826 } | |
8827 } | |
8828 return this; | |
8829 } | |
8830 | |
8831 /************************************************************/ | |
8832 | |
8833 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
8834 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
8835 { | |
8836 } | |
8837 | |
8838 Expression *ShlExp::semantic(Scope *sc) | |
8839 { Expression *e; | |
8840 | |
8841 //printf("ShlExp::semantic(), type = %p\n", type); | |
8842 if (!type) | |
8843 { BinExp::semanticp(sc); | |
8844 e = op_overload(sc); | |
8845 if (e) | |
8846 return e; | |
8847 e1 = e1->checkIntegral(); | |
8848 e2 = e2->checkIntegral(); | |
8849 e1 = e1->integralPromotions(sc); | |
8850 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8851 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8852 type = e1->type; |
8853 } | |
8854 return this; | |
8855 } | |
8856 | |
8857 /************************************************************/ | |
8858 | |
8859 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
8860 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
8861 { | |
8862 } | |
8863 | |
8864 Expression *ShrExp::semantic(Scope *sc) | |
8865 { Expression *e; | |
8866 | |
8867 if (!type) | |
8868 { BinExp::semanticp(sc); | |
8869 e = op_overload(sc); | |
8870 if (e) | |
8871 return e; | |
8872 e1 = e1->checkIntegral(); | |
8873 e2 = e2->checkIntegral(); | |
8874 e1 = e1->integralPromotions(sc); | |
8875 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8876 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8877 type = e1->type; |
8878 } | |
8879 return this; | |
8880 } | |
8881 | |
8882 /************************************************************/ | |
8883 | |
8884 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
8885 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
8886 { | |
8887 } | |
8888 | |
8889 Expression *UshrExp::semantic(Scope *sc) | |
8890 { Expression *e; | |
8891 | |
8892 if (!type) | |
8893 { BinExp::semanticp(sc); | |
8894 e = op_overload(sc); | |
8895 if (e) | |
8896 return e; | |
8897 e1 = e1->checkIntegral(); | |
8898 e2 = e2->checkIntegral(); | |
8899 e1 = e1->integralPromotions(sc); | |
8900 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8901 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8902 type = e1->type; |
8903 } | |
8904 return this; | |
8905 } | |
8906 | |
8907 /************************************************************/ | |
8908 | |
8909 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
8910 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
8911 { | |
8912 } | |
8913 | |
8914 Expression *AndExp::semantic(Scope *sc) | |
8915 { Expression *e; | |
8916 | |
8917 if (!type) | |
8918 { BinExp::semanticp(sc); | |
8919 e = op_overload(sc); | |
8920 if (e) | |
8921 return e; | |
8922 if (e1->type->toBasetype()->ty == Tbool && | |
8923 e2->type->toBasetype()->ty == Tbool) | |
8924 { | |
8925 type = e1->type; | |
8926 e = this; | |
8927 } | |
8928 else | |
8929 { | |
8930 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
|
8931 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
|
8932 { 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
|
8933 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
|
8934 } |
159 | 8935 } |
8936 } | |
8937 return this; | |
8938 } | |
8939 | |
8940 /************************************************************/ | |
8941 | |
8942 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
8943 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
8944 { | |
8945 } | |
8946 | |
8947 Expression *OrExp::semantic(Scope *sc) | |
8948 { Expression *e; | |
8949 | |
8950 if (!type) | |
8951 { BinExp::semanticp(sc); | |
8952 e = op_overload(sc); | |
8953 if (e) | |
8954 return e; | |
8955 if (e1->type->toBasetype()->ty == Tbool && | |
8956 e2->type->toBasetype()->ty == Tbool) | |
8957 { | |
8958 type = e1->type; | |
8959 e = this; | |
8960 } | |
8961 else | |
8962 { | |
8963 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
|
8964 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
|
8965 { 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
|
8966 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
|
8967 } |
159 | 8968 } |
8969 } | |
8970 return this; | |
8971 } | |
8972 | |
8973 /************************************************************/ | |
8974 | |
8975 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
8976 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
8977 { | |
8978 } | |
8979 | |
8980 Expression *XorExp::semantic(Scope *sc) | |
8981 { Expression *e; | |
8982 | |
8983 if (!type) | |
8984 { BinExp::semanticp(sc); | |
8985 e = op_overload(sc); | |
8986 if (e) | |
8987 return e; | |
8988 if (e1->type->toBasetype()->ty == Tbool && | |
8989 e2->type->toBasetype()->ty == Tbool) | |
8990 { | |
8991 type = e1->type; | |
8992 e = this; | |
8993 } | |
8994 else | |
8995 { | |
8996 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
|
8997 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
|
8998 { 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
|
8999 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
|
9000 } |
159 | 9001 } |
9002 } | |
9003 return this; | |
9004 } | |
9005 | |
9006 | |
9007 /************************************************************/ | |
9008 | |
9009 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
9010 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
9011 { | |
9012 } | |
9013 | |
9014 Expression *OrOrExp::semantic(Scope *sc) | |
9015 { | |
9016 unsigned cs1; | |
9017 | |
9018 // same as for AndAnd | |
9019 e1 = e1->semantic(sc); | |
9020 e1 = resolveProperties(sc, e1); | |
9021 e1 = e1->checkToPointer(); | |
9022 e1 = e1->checkToBoolean(); | |
9023 cs1 = sc->callSuper; | |
9024 | |
9025 if (sc->flags & SCOPEstaticif) | |
9026 { | |
9027 /* If in static if, don't evaluate e2 if we don't have to. | |
9028 */ | |
9029 e1 = e1->optimize(WANTflags); | |
9030 if (e1->isBool(TRUE)) | |
9031 { | |
9032 return new IntegerExp(loc, 1, Type::tboolean); | |
9033 } | |
9034 } | |
9035 | |
9036 e2 = e2->semantic(sc); | |
9037 sc->mergeCallSuper(loc, cs1); | |
9038 e2 = resolveProperties(sc, e2); | |
9039 e2 = e2->checkToPointer(); | |
9040 | |
9041 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
|
9042 if (e2->type->ty == Tvoid) |
159 | 9043 type = Type::tvoid; |
9044 if (e2->op == TOKtype || e2->op == TOKimport) | |
9045 error("%s is not an expression", e2->toChars()); | |
9046 return this; | |
9047 } | |
9048 | |
9049 Expression *OrOrExp::checkToBoolean() | |
9050 { | |
9051 e2 = e2->checkToBoolean(); | |
9052 return this; | |
9053 } | |
9054 | |
9055 int OrOrExp::isBit() | |
9056 { | |
9057 return TRUE; | |
9058 } | |
9059 | |
9060 int OrOrExp::checkSideEffect(int flag) | |
9061 { | |
9062 if (flag == 2) | |
9063 { | |
9064 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9065 } | |
9066 else | |
9067 { e1->checkSideEffect(1); | |
9068 return e2->checkSideEffect(flag); | |
9069 } | |
9070 } | |
9071 | |
9072 /************************************************************/ | |
9073 | |
9074 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
9075 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
9076 { | |
9077 } | |
9078 | |
9079 Expression *AndAndExp::semantic(Scope *sc) | |
9080 { | |
9081 unsigned cs1; | |
9082 | |
9083 // same as for OrOr | |
9084 e1 = e1->semantic(sc); | |
9085 e1 = resolveProperties(sc, e1); | |
9086 e1 = e1->checkToPointer(); | |
9087 e1 = e1->checkToBoolean(); | |
9088 cs1 = sc->callSuper; | |
9089 | |
9090 if (sc->flags & SCOPEstaticif) | |
9091 { | |
9092 /* If in static if, don't evaluate e2 if we don't have to. | |
9093 */ | |
9094 e1 = e1->optimize(WANTflags); | |
9095 if (e1->isBool(FALSE)) | |
9096 { | |
9097 return new IntegerExp(loc, 0, Type::tboolean); | |
9098 } | |
9099 } | |
9100 | |
9101 e2 = e2->semantic(sc); | |
9102 sc->mergeCallSuper(loc, cs1); | |
9103 e2 = resolveProperties(sc, e2); | |
9104 e2 = e2->checkToPointer(); | |
9105 | |
9106 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
|
9107 if (e2->type->ty == Tvoid) |
159 | 9108 type = Type::tvoid; |
9109 if (e2->op == TOKtype || e2->op == TOKimport) | |
9110 error("%s is not an expression", e2->toChars()); | |
9111 return this; | |
9112 } | |
9113 | |
9114 Expression *AndAndExp::checkToBoolean() | |
9115 { | |
9116 e2 = e2->checkToBoolean(); | |
9117 return this; | |
9118 } | |
9119 | |
9120 int AndAndExp::isBit() | |
9121 { | |
9122 return TRUE; | |
9123 } | |
9124 | |
9125 int AndAndExp::checkSideEffect(int flag) | |
9126 { | |
9127 if (flag == 2) | |
9128 { | |
9129 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9130 } | |
9131 else | |
9132 { | |
9133 e1->checkSideEffect(1); | |
9134 return e2->checkSideEffect(flag); | |
9135 } | |
9136 } | |
9137 | |
9138 /************************************************************/ | |
9139 | |
9140 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
9141 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
9142 { | |
9143 } | |
9144 | |
9145 Expression *InExp::semantic(Scope *sc) | |
9146 { Expression *e; | |
9147 | |
9148 if (type) | |
9149 return this; | |
9150 | |
9151 BinExp::semanticp(sc); | |
9152 e = op_overload(sc); | |
9153 if (e) | |
9154 return e; | |
9155 | |
9156 //type = Type::tboolean; | |
9157 Type *t2b = e2->type->toBasetype(); | |
9158 if (t2b->ty != Taarray) | |
9159 { | |
9160 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
9161 type = Type::terror; | |
9162 } | |
9163 else | |
9164 { | |
9165 TypeAArray *ta = (TypeAArray *)t2b; | |
9166 | |
9167 // Convert key to type of key | |
9168 e1 = e1->implicitCastTo(sc, ta->index); | |
9169 | |
9170 // 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
|
9171 type = ta->nextOf()->pointerTo(); |
159 | 9172 } |
9173 return this; | |
9174 } | |
9175 | |
9176 int InExp::isBit() | |
9177 { | |
9178 return FALSE; | |
9179 } | |
9180 | |
9181 | |
9182 /************************************************************/ | |
9183 | |
9184 /* This deletes the key e1 from the associative array e2 | |
9185 */ | |
9186 | |
9187 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
9188 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
9189 { | |
9190 type = Type::tvoid; | |
9191 } | |
9192 | |
9193 /************************************************************/ | |
9194 | |
9195 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9196 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
9197 { | |
9198 } | |
9199 | |
9200 Expression *CmpExp::semantic(Scope *sc) | |
9201 { Expression *e; | |
9202 Type *t1; | |
9203 Type *t2; | |
9204 | |
9205 #if LOGSEMANTIC | |
9206 printf("CmpExp::semantic('%s')\n", toChars()); | |
9207 #endif | |
9208 if (type) | |
9209 return this; | |
9210 | |
9211 BinExp::semanticp(sc); | |
9212 | |
9213 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9214 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9215 { | |
9216 error("do not use null when comparing class types"); | |
9217 } | |
9218 | |
9219 e = op_overload(sc); | |
9220 if (e) | |
9221 { | |
9222 e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); | |
9223 e = e->semantic(sc); | |
9224 return e; | |
9225 } | |
9226 | |
9227 typeCombine(sc); | |
9228 type = Type::tboolean; | |
9229 | |
9230 // Special handling for array comparisons | |
9231 t1 = e1->type->toBasetype(); | |
9232 t2 = e2->type->toBasetype(); | |
9233 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9234 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9235 { | |
9236 if (!t1->next->equals(t2->next)) | |
9237 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9238 e = this; | |
9239 } | |
9240 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
9241 (t1->ty == Tclass && t2->ty == Tclass)) | |
9242 { | |
9243 if (t2->ty == Tstruct) | |
9244 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
9245 else | |
9246 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
9247 e = this; | |
9248 } | |
9249 #if 1 | |
9250 else if (t1->iscomplex() || t2->iscomplex()) | |
9251 { | |
9252 error("compare not defined for complex operands"); | |
9253 e = new IntegerExp(0); | |
9254 } | |
9255 #endif | |
9256 else | |
9257 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
|
9258 //printf("CmpExp: %s\n", e->toChars()); |
159 | 9259 return e; |
9260 } | |
9261 | |
9262 int CmpExp::isBit() | |
9263 { | |
9264 return TRUE; | |
9265 } | |
9266 | |
9267 | |
9268 /************************************************************/ | |
9269 | |
9270 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9271 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
9272 { | |
9273 assert(op == TOKequal || op == TOKnotequal); | |
9274 } | |
9275 | |
9276 Expression *EqualExp::semantic(Scope *sc) | |
9277 { Expression *e; | |
9278 Type *t1; | |
9279 Type *t2; | |
9280 | |
9281 //printf("EqualExp::semantic('%s')\n", toChars()); | |
9282 if (type) | |
9283 return this; | |
9284 | |
9285 BinExp::semanticp(sc); | |
9286 | |
9287 /* Before checking for operator overloading, check to see if we're | |
9288 * comparing the addresses of two statics. If so, we can just see | |
9289 * if they are the same symbol. | |
9290 */ | |
9291 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
9292 { AddrExp *ae1 = (AddrExp *)e1; | |
9293 AddrExp *ae2 = (AddrExp *)e2; | |
9294 | |
9295 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
9296 { VarExp *ve1 = (VarExp *)ae1->e1; | |
9297 VarExp *ve2 = (VarExp *)ae2->e1; | |
9298 | |
9299 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
9300 { | |
9301 // They are the same, result is 'true' for ==, 'false' for != | |
9302 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
9303 return e; | |
9304 } | |
9305 } | |
9306 } | |
9307 | |
9308 if (e1->type->toBasetype()->ty == Tclass && e2->op == TOKnull || | |
9309 e2->type->toBasetype()->ty == Tclass && e1->op == TOKnull) | |
9310 { | |
9311 error("use '%s' instead of '%s' when comparing with null", | |
9312 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
9313 Token::toChars(op)); | |
9314 } | |
9315 | |
9316 //if (e2->op != TOKnull) | |
9317 { | |
9318 e = op_overload(sc); | |
9319 if (e) | |
9320 { | |
9321 if (op == TOKnotequal) | |
9322 { | |
9323 e = new NotExp(e->loc, e); | |
9324 e = e->semantic(sc); | |
9325 } | |
9326 return e; | |
9327 } | |
9328 } | |
9329 | |
9330 e = typeCombine(sc); | |
9331 type = Type::tboolean; | |
9332 | |
9333 // Special handling for array comparisons | |
9334 t1 = e1->type->toBasetype(); | |
9335 t2 = e2->type->toBasetype(); | |
9336 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9337 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9338 { | |
9339 if (!t1->next->equals(t2->next)) | |
9340 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9341 } | |
9342 else | |
9343 { | |
9344 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9345 { | |
9346 // Cast both to complex | |
9347 e1 = e1->castTo(sc, Type::tcomplex80); | |
9348 e2 = e2->castTo(sc, Type::tcomplex80); | |
9349 } | |
9350 } | |
9351 return e; | |
9352 } | |
9353 | |
9354 int EqualExp::isBit() | |
9355 { | |
9356 return TRUE; | |
9357 } | |
9358 | |
9359 | |
9360 | |
9361 /************************************************************/ | |
9362 | |
9363 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9364 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
9365 { | |
9366 } | |
9367 | |
9368 Expression *IdentityExp::semantic(Scope *sc) | |
9369 { | |
9370 if (type) | |
9371 return this; | |
9372 | |
9373 BinExp::semanticp(sc); | |
9374 type = Type::tboolean; | |
9375 typeCombine(sc); | |
9376 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9377 { | |
9378 // Cast both to complex | |
9379 e1 = e1->castTo(sc, Type::tcomplex80); | |
9380 e2 = e2->castTo(sc, Type::tcomplex80); | |
9381 } | |
9382 return this; | |
9383 } | |
9384 | |
9385 int IdentityExp::isBit() | |
9386 { | |
9387 return TRUE; | |
9388 } | |
9389 | |
9390 | |
9391 /****************************************************************/ | |
9392 | |
9393 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
9394 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
9395 { | |
9396 this->econd = econd; | |
9397 } | |
9398 | |
9399 Expression *CondExp::syntaxCopy() | |
9400 { | |
9401 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
9402 } | |
9403 | |
9404 | |
9405 Expression *CondExp::semantic(Scope *sc) | |
9406 { Type *t1; | |
9407 Type *t2; | |
9408 unsigned cs0; | |
9409 unsigned cs1; | |
9410 | |
9411 #if LOGSEMANTIC | |
9412 printf("CondExp::semantic('%s')\n", toChars()); | |
9413 #endif | |
9414 if (type) | |
9415 return this; | |
9416 | |
9417 econd = econd->semantic(sc); | |
9418 econd = resolveProperties(sc, econd); | |
9419 econd = econd->checkToPointer(); | |
9420 econd = econd->checkToBoolean(); | |
9421 | |
9422 #if 0 /* this cannot work right because the types of e1 and e2 | |
9423 * both contribute to the type of the result. | |
9424 */ | |
9425 if (sc->flags & SCOPEstaticif) | |
9426 { | |
9427 /* If in static if, don't evaluate what we don't have to. | |
9428 */ | |
9429 econd = econd->optimize(WANTflags); | |
9430 if (econd->isBool(TRUE)) | |
9431 { | |
9432 e1 = e1->semantic(sc); | |
9433 e1 = resolveProperties(sc, e1); | |
9434 return e1; | |
9435 } | |
9436 else if (econd->isBool(FALSE)) | |
9437 { | |
9438 e2 = e2->semantic(sc); | |
9439 e2 = resolveProperties(sc, e2); | |
9440 return e2; | |
9441 } | |
9442 } | |
9443 #endif | |
9444 | |
9445 | |
9446 cs0 = sc->callSuper; | |
9447 e1 = e1->semantic(sc); | |
9448 e1 = resolveProperties(sc, e1); | |
9449 cs1 = sc->callSuper; | |
9450 sc->callSuper = cs0; | |
9451 e2 = e2->semantic(sc); | |
9452 e2 = resolveProperties(sc, e2); | |
9453 sc->mergeCallSuper(loc, cs1); | |
9454 | |
9455 | |
9456 // If either operand is void, the result is void | |
9457 t1 = e1->type; | |
9458 t2 = e2->type; | |
9459 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
9460 type = Type::tvoid; | |
9461 else if (t1 == t2) | |
9462 type = t1; | |
9463 else | |
9464 { | |
9465 typeCombine(sc); | |
9466 switch (e1->type->toBasetype()->ty) | |
9467 { | |
9468 case Tcomplex32: | |
9469 case Tcomplex64: | |
9470 case Tcomplex80: | |
9471 e2 = e2->castTo(sc, e1->type); | |
9472 break; | |
9473 } | |
9474 switch (e2->type->toBasetype()->ty) | |
9475 { | |
9476 case Tcomplex32: | |
9477 case Tcomplex64: | |
9478 case Tcomplex80: | |
9479 e1 = e1->castTo(sc, e2->type); | |
9480 break; | |
9481 } | |
9482 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9483 #if 0 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9484 printf("res: %s\n", type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9485 printf("e1 : %s\n", e1->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9486 printf("e2 : %s\n", e2->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9487 #endif |
159 | 9488 return this; |
9489 } | |
9490 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9491 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9492 int CondExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9493 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9494 return e1->isLvalue() && e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9495 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9496 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9497 |
159 | 9498 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) |
9499 { | |
9500 PtrExp *e; | |
9501 | |
9502 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
9503 e = new PtrExp(loc, this, type); | |
9504 | |
9505 e1 = e1->addressOf(sc); | |
9506 //e1 = e1->toLvalue(sc, NULL); | |
9507 | |
9508 e2 = e2->addressOf(sc); | |
9509 //e2 = e2->toLvalue(sc, NULL); | |
9510 | |
9511 typeCombine(sc); | |
9512 | |
9513 type = e2->type; | |
9514 return e; | |
9515 } | |
9516 | |
9517 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
9518 { | |
9519 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
9520 return this; | |
9521 } | |
9522 | |
9523 void CondExp::checkEscape() | |
9524 { | |
9525 e1->checkEscape(); | |
9526 e2->checkEscape(); | |
9527 } | |
9528 | |
9529 | |
9530 Expression *CondExp::checkToBoolean() | |
9531 { | |
9532 e1 = e1->checkToBoolean(); | |
9533 e2 = e2->checkToBoolean(); | |
9534 return this; | |
9535 } | |
9536 | |
9537 int CondExp::checkSideEffect(int flag) | |
9538 { | |
9539 if (flag == 2) | |
9540 { | |
9541 return econd->checkSideEffect(2) || | |
9542 e1->checkSideEffect(2) || | |
9543 e2->checkSideEffect(2); | |
9544 } | |
9545 else | |
9546 { | |
9547 econd->checkSideEffect(1); | |
9548 e1->checkSideEffect(flag); | |
9549 return e2->checkSideEffect(flag); | |
9550 } | |
9551 } | |
9552 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9553 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9554 int CondExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9555 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9556 return econd->canThrow() || e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9557 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9558 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9559 |
159 | 9560 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
9561 { | |
9562 expToCBuffer(buf, hgs, econd, PREC_oror); | |
9563 buf->writestring(" ? "); | |
9564 expToCBuffer(buf, hgs, e1, PREC_expr); | |
9565 buf->writestring(" : "); | |
9566 expToCBuffer(buf, hgs, e2, PREC_cond); | |
9567 } | |
9568 | |
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
|
9569 /************************************************************/ |
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
|
9570 |
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
|
9571 #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
|
9572 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9573 // 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
|
9574 |
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
|
9575 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
|
9576 : 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
|
9577 { |
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
|
9578 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
|
9579 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
|
9580 } |
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
|
9581 |
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
|
9582 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
|
9583 { |
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
|
9584 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
|
9585 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
|
9586 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
|
9587 } |
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
|
9588 |
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
|
9589 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
|
9590 { |
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
|
9591 // 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
|
9592 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
|
9593 } |
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
|
9594 |
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
|
9595 #endif |