Mercurial > projects > ldc
annotate dmd/expression.c @ 1621:fb2e6707ad17
Merge DMD r314+r315: bugzilla 2029 Typesafe variadic functions don't...
Both DMD revisions are for fixing bugzilla 2029 (Typesafe variadic
functions don't work in CTFE).
The DMD r314 commit message is:
bugzilla 2029 (Typesafe variadic functions don't work in CTFE
The DMD r315 commit message is:
bugzilla 2029 - try again
---
dmd/constfold.c | 11 ++++-
dmd/declaration.c | 21 +++++++++-
dmd/declaration.h | 10 ++++-
dmd/expression.c | 1 +
dmd/interpret.c | 111 +++++++++++++++++++++++++++++++++++++++++++++--------
dmd/mars.h | 2 +-
dmd/mtype.c | 2 +-
7 files changed, 135 insertions(+), 23 deletions(-)
author | Leandro Lucarella <llucax@gmail.com> |
---|---|
date | Wed, 06 Jan 2010 15:18:22 -0300 |
parents | c61782a76dff |
children | 8fa4ab3dcc88 |
rev | line source |
---|---|
159 | 1 |
2 // Compiler implementation of the D programming language | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
3 // Copyright (c) 1999-2009 by Digital Mars |
159 | 4 // All Rights Reserved |
5 // written by Walter Bright | |
6 // http://www.digitalmars.com | |
7 // License for redistribution is by either the Artistic License | |
8 // in artistic.txt, or the GNU General Public License in gnu.txt. | |
9 // See the included readme.txt for details. | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <ctype.h> | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
14 #include <math.h> |
159 | 15 #include <assert.h> |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
16 #if _MSC_VER |
159 | 17 #include <complex> |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
18 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
19 #endif |
159 | 20 |
21 #if _WIN32 && __DMC__ | |
22 extern "C" char * __cdecl __locale_decpoint; | |
23 #endif | |
24 | |
285
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
25 #if __MINGW32__ |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
26 #ifndef isnan |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
27 #define isnan _isnan |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
28 #endif |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
29 #endif |
297690b5d4a5
[svn r306] Fixed: it's now possible to compile and link llvmdc with MinGW32 and msys on Win32 :D I tried it myself ;) Building the runtime still needs some work, but it's a step in the right direction.
lindquist
parents:
163
diff
changeset
|
30 |
536
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
31 #ifdef __APPLE__ |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
32 #ifndef isnan |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
33 int isnan(double); |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
34 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
35 #endif |
0d2db7efa105
Add isnan for __APPLE__ and darwin version ident for OSX.
Christian Kamm <kamm incasoftware de>
parents:
535
diff
changeset
|
36 |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
37 #include "rmem.h" |
1587 | 38 #include "port.h" |
39 | |
159 | 40 #include "mtype.h" |
41 #include "init.h" | |
42 #include "expression.h" | |
43 #include "template.h" | |
44 #include "utf.h" | |
45 #include "enum.h" | |
46 #include "scope.h" | |
47 #include "statement.h" | |
48 #include "declaration.h" | |
49 #include "aggregate.h" | |
50 #include "import.h" | |
51 #include "id.h" | |
52 #include "dsymbol.h" | |
53 #include "module.h" | |
54 #include "attrib.h" | |
55 #include "hdrgen.h" | |
56 #include "parse.h" | |
57 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
58 Expression *expandVar(int result, VarDeclaration *v); |
159 | 59 |
60 #define LOGSEMANTIC 0 | |
61 | |
62 /********************************** | |
63 * Set operator precedence for each operator. | |
64 */ | |
65 | |
66 // Operator precedence - greater values are higher precedence | |
67 | |
68 enum PREC | |
69 { | |
70 PREC_zero, | |
71 PREC_expr, | |
72 PREC_assign, | |
73 PREC_cond, | |
74 PREC_oror, | |
75 PREC_andand, | |
76 PREC_or, | |
77 PREC_xor, | |
78 PREC_and, | |
79 PREC_equal, | |
80 PREC_rel, | |
81 PREC_shift, | |
82 PREC_add, | |
83 PREC_mul, | |
84 PREC_unary, | |
85 PREC_primary, | |
86 }; | |
87 | |
88 enum PREC precedence[TOKMAX]; | |
89 | |
90 void initPrecedence() | |
91 { | |
92 precedence[TOKdotvar] = PREC_primary; | |
93 precedence[TOKimport] = PREC_primary; | |
94 precedence[TOKidentifier] = PREC_primary; | |
95 precedence[TOKthis] = PREC_primary; | |
96 precedence[TOKsuper] = PREC_primary; | |
97 precedence[TOKint64] = PREC_primary; | |
98 precedence[TOKfloat64] = PREC_primary; | |
99 precedence[TOKnull] = PREC_primary; | |
100 precedence[TOKstring] = PREC_primary; | |
101 precedence[TOKarrayliteral] = PREC_primary; | |
102 precedence[TOKtypeid] = PREC_primary; | |
103 precedence[TOKis] = PREC_primary; | |
104 precedence[TOKassert] = PREC_primary; | |
105 precedence[TOKfunction] = PREC_primary; | |
106 precedence[TOKvar] = PREC_primary; | |
336 | 107 #if DMDV2 |
108 precedence[TOKdefault] = PREC_primary; | |
109 #endif | |
159 | 110 |
111 // post | |
112 precedence[TOKdotti] = PREC_primary; | |
113 precedence[TOKdot] = PREC_primary; | |
114 // precedence[TOKarrow] = PREC_primary; | |
115 precedence[TOKplusplus] = PREC_primary; | |
116 precedence[TOKminusminus] = PREC_primary; | |
117 precedence[TOKcall] = PREC_primary; | |
118 precedence[TOKslice] = PREC_primary; | |
119 precedence[TOKarray] = PREC_primary; | |
120 | |
121 precedence[TOKaddress] = PREC_unary; | |
122 precedence[TOKstar] = PREC_unary; | |
123 precedence[TOKneg] = PREC_unary; | |
124 precedence[TOKuadd] = PREC_unary; | |
125 precedence[TOKnot] = PREC_unary; | |
126 precedence[TOKtobool] = PREC_add; | |
127 precedence[TOKtilde] = PREC_unary; | |
128 precedence[TOKdelete] = PREC_unary; | |
129 precedence[TOKnew] = PREC_unary; | |
130 precedence[TOKcast] = PREC_unary; | |
131 | |
132 precedence[TOKmul] = PREC_mul; | |
133 precedence[TOKdiv] = PREC_mul; | |
134 precedence[TOKmod] = PREC_mul; | |
135 | |
136 precedence[TOKadd] = PREC_add; | |
137 precedence[TOKmin] = PREC_add; | |
138 precedence[TOKcat] = PREC_add; | |
139 | |
140 precedence[TOKshl] = PREC_shift; | |
141 precedence[TOKshr] = PREC_shift; | |
142 precedence[TOKushr] = PREC_shift; | |
143 | |
144 precedence[TOKlt] = PREC_rel; | |
145 precedence[TOKle] = PREC_rel; | |
146 precedence[TOKgt] = PREC_rel; | |
147 precedence[TOKge] = PREC_rel; | |
148 precedence[TOKunord] = PREC_rel; | |
149 precedence[TOKlg] = PREC_rel; | |
150 precedence[TOKleg] = PREC_rel; | |
151 precedence[TOKule] = PREC_rel; | |
152 precedence[TOKul] = PREC_rel; | |
153 precedence[TOKuge] = PREC_rel; | |
154 precedence[TOKug] = PREC_rel; | |
155 precedence[TOKue] = PREC_rel; | |
156 precedence[TOKin] = PREC_rel; | |
157 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
158 #if 0 |
159 | 159 precedence[TOKequal] = PREC_equal; |
160 precedence[TOKnotequal] = PREC_equal; | |
161 precedence[TOKidentity] = PREC_equal; | |
162 precedence[TOKnotidentity] = PREC_equal; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
163 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
164 /* 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
|
165 * 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
|
166 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
167 precedence[TOKequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
168 precedence[TOKnotequal] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
169 precedence[TOKidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
170 precedence[TOKnotidentity] = PREC_rel; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
171 #endif |
159 | 172 |
173 precedence[TOKand] = PREC_and; | |
174 | |
175 precedence[TOKxor] = PREC_xor; | |
176 | |
177 precedence[TOKor] = PREC_or; | |
178 | |
179 precedence[TOKandand] = PREC_andand; | |
180 | |
181 precedence[TOKoror] = PREC_oror; | |
182 | |
183 precedence[TOKquestion] = PREC_cond; | |
184 | |
185 precedence[TOKassign] = PREC_assign; | |
186 precedence[TOKconstruct] = PREC_assign; | |
187 precedence[TOKblit] = PREC_assign; | |
188 precedence[TOKaddass] = PREC_assign; | |
189 precedence[TOKminass] = PREC_assign; | |
190 precedence[TOKcatass] = PREC_assign; | |
191 precedence[TOKmulass] = PREC_assign; | |
192 precedence[TOKdivass] = PREC_assign; | |
193 precedence[TOKmodass] = PREC_assign; | |
194 precedence[TOKshlass] = PREC_assign; | |
195 precedence[TOKshrass] = PREC_assign; | |
196 precedence[TOKushrass] = PREC_assign; | |
197 precedence[TOKandass] = PREC_assign; | |
198 precedence[TOKorass] = PREC_assign; | |
199 precedence[TOKxorass] = PREC_assign; | |
200 | |
201 precedence[TOKcomma] = PREC_expr; | |
202 } | |
203 | |
336 | 204 /************************************************************* |
205 * Given var, we need to get the | |
206 * right 'this' pointer if var is in an outer class, but our | |
207 * existing 'this' pointer is in an inner class. | |
208 * Input: | |
209 * e1 existing 'this' | |
210 * ad struct or class we need the correct 'this' for | |
211 * var the specific member of ad we're accessing | |
212 */ | |
213 | |
214 Expression *getRightThis(Loc loc, Scope *sc, AggregateDeclaration *ad, | |
215 Expression *e1, Declaration *var) | |
216 { | |
217 //printf("\ngetRightThis(e1 = %s, ad = %s, var = %s)\n", e1->toChars(), ad->toChars(), var->toChars()); | |
218 L1: | |
219 Type *t = e1->type->toBasetype(); | |
220 //printf("e1->type = %s, var->type = %s\n", e1->type->toChars(), var->type->toChars()); | |
221 | |
222 /* If e1 is not the 'this' pointer for ad | |
223 */ | |
224 if (ad && | |
225 !(t->ty == Tpointer && t->nextOf()->ty == Tstruct && | |
226 ((TypeStruct *)t->nextOf())->sym == ad) | |
227 && | |
228 !(t->ty == Tstruct && | |
229 ((TypeStruct *)t)->sym == ad) | |
230 ) | |
231 { | |
232 ClassDeclaration *cd = ad->isClassDeclaration(); | |
233 ClassDeclaration *tcd = t->isClassHandle(); | |
234 | |
235 /* e1 is the right this if ad is a base class of e1 | |
236 */ | |
237 if (!cd || !tcd || | |
238 !(tcd == cd || cd->isBaseOf(tcd, NULL)) | |
239 ) | |
240 { | |
241 /* Only classes can be inner classes with an 'outer' | |
242 * member pointing to the enclosing class instance | |
243 */ | |
244 if (tcd && tcd->isNested()) | |
245 { /* e1 is the 'this' pointer for an inner class: tcd. | |
246 * Rewrite it as the 'this' pointer for the outer class. | |
247 */ | |
248 | |
249 e1 = new DotVarExp(loc, e1, tcd->vthis); | |
250 e1->type = tcd->vthis->type; | |
251 // Do not call checkNestedRef() | |
252 //e1 = e1->semantic(sc); | |
253 | |
254 // Skip up over nested functions, and get the enclosing | |
255 // class type. | |
256 int n = 0; | |
257 Dsymbol *s; | |
258 for (s = tcd->toParent(); | |
259 s && s->isFuncDeclaration(); | |
260 s = s->toParent()) | |
261 { FuncDeclaration *f = s->isFuncDeclaration(); | |
262 if (f->vthis) | |
263 { | |
264 //printf("rewriting e1 to %s's this\n", f->toChars()); | |
265 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
|
266 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
267 // 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
|
268 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
|
269 |
336 | 270 e1 = new VarExp(loc, f->vthis); |
271 } | |
272 } | |
273 if (s && s->isClassDeclaration()) | |
274 { e1->type = s->isClassDeclaration()->type; | |
275 if (n > 1) | |
276 e1 = e1->semantic(sc); | |
277 } | |
278 else | |
279 e1 = e1->semantic(sc); | |
280 goto L1; | |
281 } | |
282 /* Can't find a path from e1 to ad | |
283 */ | |
284 e1->error("this for %s needs to be type %s not type %s", | |
285 var->toChars(), ad->toChars(), t->toChars()); | |
286 } | |
287 } | |
288 return e1; | |
289 } | |
290 | |
159 | 291 /***************************************** |
292 * Determine if 'this' is available. | |
293 * If it is, return the FuncDeclaration that has it. | |
294 */ | |
295 | |
296 FuncDeclaration *hasThis(Scope *sc) | |
297 { FuncDeclaration *fd; | |
298 FuncDeclaration *fdthis; | |
299 | |
300 //printf("hasThis()\n"); | |
301 fdthis = sc->parent->isFuncDeclaration(); | |
302 //printf("fdthis = %p, '%s'\n", fdthis, fdthis ? fdthis->toChars() : ""); | |
303 | |
304 // Go upwards until we find the enclosing member function | |
305 fd = fdthis; | |
306 while (1) | |
307 { | |
308 if (!fd) | |
309 { | |
310 goto Lno; | |
311 } | |
312 if (!fd->isNested()) | |
313 break; | |
314 | |
315 Dsymbol *parent = fd->parent; | |
316 while (parent) | |
317 { | |
318 TemplateInstance *ti = parent->isTemplateInstance(); | |
319 if (ti) | |
320 parent = ti->parent; | |
321 else | |
322 break; | |
323 } | |
324 | |
325 fd = fd->parent->isFuncDeclaration(); | |
326 } | |
327 | |
328 if (!fd->isThis()) | |
329 { //printf("test '%s'\n", fd->toChars()); | |
330 goto Lno; | |
331 } | |
332 | |
333 assert(fd->vthis); | |
334 return fd; | |
335 | |
336 Lno: | |
337 return NULL; // don't have 'this' available | |
338 } | |
339 | |
340 | |
341 /*************************************** | |
342 * Pull out any properties. | |
343 */ | |
344 | |
345 Expression *resolveProperties(Scope *sc, Expression *e) | |
346 { | |
347 //printf("resolveProperties(%s)\n", e->toChars()); | |
348 if (e->type) | |
349 { | |
350 Type *t = e->type->toBasetype(); | |
351 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
352 if (t->ty == Tfunction /*|| e->op == TOKoverloadset*/) |
159 | 353 { |
354 e = new CallExp(e->loc, e); | |
355 e = e->semantic(sc); | |
356 } | |
357 | |
358 /* Look for e being a lazy parameter; rewrite as delegate call | |
359 */ | |
360 else if (e->op == TOKvar) | |
361 { VarExp *ve = (VarExp *)e; | |
362 | |
363 if (ve->var->storage_class & STClazy) | |
364 { | |
365 e = new CallExp(e->loc, e); | |
366 e = e->semantic(sc); | |
367 } | |
368 } | |
369 | |
370 else if (e->op == TOKdotexp) | |
371 { | |
372 e->error("expression has no value"); | |
373 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
374 |
159 | 375 } |
1587 | 376 else if (e->op == TOKdottd) |
377 { | |
378 e = new CallExp(e->loc, e); | |
379 e = e->semantic(sc); | |
380 } | |
159 | 381 return e; |
382 } | |
383 | |
384 /****************************** | |
385 * Perform semantic() on an array of Expressions. | |
386 */ | |
387 | |
388 void arrayExpressionSemantic(Expressions *exps, Scope *sc) | |
389 { | |
390 if (exps) | |
391 { | |
392 for (size_t i = 0; i < exps->dim; i++) | |
393 { Expression *e = (Expression *)exps->data[i]; | |
394 | |
395 e = e->semantic(sc); | |
396 exps->data[i] = (void *)e; | |
397 } | |
398 } | |
399 } | |
400 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
401 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
402 /****************************** |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
403 * 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
|
404 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
405 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
406 #if DMDV2 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
407 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
|
408 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
409 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
|
410 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
411 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
|
412 { 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
|
413 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
|
414 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
|
415 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
416 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
417 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
|
418 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
419 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
420 |
159 | 421 /**************************************** |
422 * Expand tuples. | |
423 */ | |
424 | |
425 void expandTuples(Expressions *exps) | |
426 { | |
427 //printf("expandTuples()\n"); | |
428 if (exps) | |
429 { | |
430 for (size_t i = 0; i < exps->dim; i++) | |
431 { Expression *arg = (Expression *)exps->data[i]; | |
432 if (!arg) | |
433 continue; | |
434 | |
435 // Look for tuple with 0 members | |
436 if (arg->op == TOKtype) | |
437 { TypeExp *e = (TypeExp *)arg; | |
438 if (e->type->toBasetype()->ty == Ttuple) | |
439 { TypeTuple *tt = (TypeTuple *)e->type->toBasetype(); | |
440 | |
441 if (!tt->arguments || tt->arguments->dim == 0) | |
442 { | |
443 exps->remove(i); | |
444 if (i == exps->dim) | |
445 return; | |
446 i--; | |
447 continue; | |
448 } | |
449 } | |
450 } | |
451 | |
452 // Inline expand all the tuples | |
453 while (arg->op == TOKtuple) | |
454 { TupleExp *te = (TupleExp *)arg; | |
455 | |
456 exps->remove(i); // remove arg | |
457 exps->insert(i, te->exps); // replace with tuple contents | |
458 if (i == exps->dim) | |
459 return; // empty tuple, no more arguments | |
460 arg = (Expression *)exps->data[i]; | |
461 } | |
462 } | |
463 } | |
464 } | |
465 | |
1604
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
466 Expressions *arrayExpressionToCommonType(Scope *sc, Expressions *exps, Type **pt) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
467 { |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
468 #if DMDV1 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
469 /* The first element sets the type |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
470 */ |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
471 Type *t0 = NULL; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
472 for (size_t i = 0; i < exps->dim; i++) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
473 { Expression *e = (Expression *)exps->data[i]; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
474 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
475 if (!e->type) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
476 { error("%s has no value", e->toChars()); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
477 e = new ErrorExp(); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
478 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
479 e = resolveProperties(sc, e); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
480 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
481 if (!t0) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
482 t0 = e->type; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
483 else |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
484 e = e->implicitCastTo(sc, t0); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
485 exps->data[i] = (void *)e; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
486 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
487 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
488 if (!t0) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
489 t0 = Type::tvoid; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
490 if (pt) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
491 *pt = t0; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
492 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
493 // Eventually, we want to make this copy-on-write |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
494 return exps; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
495 #endif |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
496 #if DMDV2 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
497 /* The type is determined by applying ?: to each pair. |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
498 */ |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
499 IntegerExp integerexp(0); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
500 CondExp condexp(0, &integerexp, NULL, NULL); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
501 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
502 Type *t0 = NULL; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
503 Expression *e0; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
504 int j0; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
505 for (size_t i = 0; i < exps->dim; i++) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
506 { Expression *e = (Expression *)exps->data[i]; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
507 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
508 e = resolveProperties(sc, e); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
509 if (!e->type) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
510 { error("%s has no value", e->toChars()); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
511 e = new ErrorExp(); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
512 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
513 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
514 if (t0) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
515 { if (t0 != e->type) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
516 { |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
517 /* This applies ?: to merge the types. It's backwards; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
518 * ?: should call this function to merge types. |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
519 */ |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
520 condexp.type = NULL; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
521 condexp.e1 = e0; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
522 condexp.e2 = e; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
523 condexp.semantic(sc); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
524 exps->data[j0] = (void *)condexp.e1; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
525 e = condexp.e2; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
526 t0 = e->type; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
527 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
528 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
529 else |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
530 { j0 = i; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
531 e0 = e; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
532 t0 = e->type; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
533 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
534 exps->data[i] = (void *)e; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
535 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
536 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
537 if (t0) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
538 { |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
539 for (size_t i = 0; i < exps->dim; i++) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
540 { Expression *e = (Expression *)exps->data[i]; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
541 e = e->implicitCastTo(sc, t0); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
542 exps->data[i] = (void *)e; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
543 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
544 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
545 else |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
546 t0 = Type::tvoid; // [] is typed as void[] |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
547 if (pt) |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
548 *pt = t0; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
549 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
550 // Eventually, we want to make this copy-on-write |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
551 return exps; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
552 #endif |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
553 } |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
554 |
159 | 555 /**************************************** |
556 * Preprocess arguments to function. | |
557 */ | |
558 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
559 void preFunctionParameters(Loc loc, Scope *sc, Expressions *exps) |
159 | 560 { |
561 if (exps) | |
562 { | |
563 expandTuples(exps); | |
564 | |
565 for (size_t i = 0; i < exps->dim; i++) | |
566 { Expression *arg = (Expression *)exps->data[i]; | |
567 | |
568 if (!arg->type) | |
569 { | |
570 #ifdef DEBUG | |
571 if (!global.gag) | |
572 printf("1: \n"); | |
573 #endif | |
574 arg->error("%s is not an expression", arg->toChars()); | |
575 arg = new IntegerExp(arg->loc, 0, Type::tint32); | |
576 } | |
577 | |
578 arg = resolveProperties(sc, arg); | |
579 exps->data[i] = (void *) arg; | |
580 | |
581 //arg->rvalue(); | |
582 #if 0 | |
583 if (arg->type->ty == Tfunction) | |
584 { | |
585 arg = new AddrExp(arg->loc, arg); | |
586 arg = arg->semantic(sc); | |
587 exps->data[i] = (void *) arg; | |
588 } | |
589 #endif | |
590 } | |
591 } | |
592 } | |
593 | |
336 | 594 /********************************************* |
595 * Call copy constructor for struct value argument. | |
596 */ | |
597 #if DMDV2 | |
598 Expression *callCpCtor(Loc loc, Scope *sc, Expression *e) | |
599 { | |
600 Type *tb = e->type->toBasetype(); | |
601 assert(tb->ty == Tstruct); | |
602 StructDeclaration *sd = ((TypeStruct *)tb)->sym; | |
603 if (sd->cpctor) | |
604 { | |
605 /* Create a variable tmp, and replace the argument e with: | |
606 * (tmp = e),tmp | |
607 * and let AssignExp() handle the construction. | |
608 * This is not the most efficent, ideally tmp would be constructed | |
609 * directly onto the stack. | |
610 */ | |
611 Identifier *idtmp = Lexer::uniqueId("__tmp"); | |
612 VarDeclaration *tmp = new VarDeclaration(loc, tb, idtmp, new ExpInitializer(0, e)); | |
613 Expression *ae = new DeclarationExp(loc, tmp); | |
614 e = new CommaExp(loc, ae, new VarExp(loc, tmp)); | |
615 e = e->semantic(sc); | |
616 } | |
617 return e; | |
618 } | |
619 #endif | |
159 | 620 |
621 /**************************************** | |
622 * Now that we know the exact type of the function we're calling, | |
623 * 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
|
624 * 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
|
625 * 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
|
626 * 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
|
627 * 4. add hidden _arguments[] argument |
159 | 628 */ |
629 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
630 void functionParameters(Loc loc, Scope *sc, TypeFunction *tf, Expressions *arguments) |
159 | 631 { |
632 unsigned n; | |
633 | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
634 //printf("functionParameters()\n"); |
159 | 635 assert(arguments); |
636 size_t nargs = arguments ? arguments->dim : 0; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
637 size_t nparams = Parameter::dim(tf->parameters); |
159 | 638 |
639 if (nargs > nparams && tf->varargs == 0) | |
1587 | 640 error(loc, "expected %zu arguments, not %zu for non-variadic function type %s", nparams, nargs, tf->toChars()); |
159 | 641 |
642 n = (nargs > nparams) ? nargs : nparams; // n = max(nargs, nparams) | |
643 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
644 int done = 0; |
159 | 645 for (size_t i = 0; i < n; i++) |
646 { | |
647 Expression *arg; | |
648 | |
649 if (i < nargs) | |
650 arg = (Expression *)arguments->data[i]; | |
651 else | |
652 arg = NULL; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
653 Type *tb; |
159 | 654 |
655 if (i < nparams) | |
656 { | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
657 Parameter *p = Parameter::getNth(tf->parameters, i); |
159 | 658 |
659 if (!arg) | |
660 { | |
661 if (!p->defaultArg) | |
662 { | |
663 if (tf->varargs == 2 && i + 1 == nparams) | |
664 goto L2; | |
1587 | 665 error(loc, "expected %zu function arguments, not %zu", nparams, nargs); |
1611
3f728445fc42
Merge DMD r277: bugzilla 3495 Segfault(typinf.c) instantiating D variadic...
Leandro Lucarella <llucax@gmail.com>
parents:
1609
diff
changeset
|
666 return; |
159 | 667 } |
336 | 668 arg = p->defaultArg; |
669 #if DMDV2 | |
670 if (arg->op == TOKdefault) | |
671 { DefaultInitExp *de = (DefaultInitExp *)arg; | |
672 arg = de->resolve(loc, sc); | |
673 } | |
674 else | |
675 #endif | |
676 arg = arg->copy(); | |
159 | 677 arguments->push(arg); |
678 nargs++; | |
679 } | |
680 | |
681 if (tf->varargs == 2 && i + 1 == nparams) | |
682 { | |
683 //printf("\t\tvarargs == 2, p->type = '%s'\n", p->type->toChars()); | |
684 if (arg->implicitConvTo(p->type)) | |
685 { | |
686 if (nargs != nparams) | |
1611
3f728445fc42
Merge DMD r277: bugzilla 3495 Segfault(typinf.c) instantiating D variadic...
Leandro Lucarella <llucax@gmail.com>
parents:
1609
diff
changeset
|
687 { error(loc, "expected %zu function arguments, not %zu", nparams, nargs); |
3f728445fc42
Merge DMD r277: bugzilla 3495 Segfault(typinf.c) instantiating D variadic...
Leandro Lucarella <llucax@gmail.com>
parents:
1609
diff
changeset
|
688 return; |
3f728445fc42
Merge DMD r277: bugzilla 3495 Segfault(typinf.c) instantiating D variadic...
Leandro Lucarella <llucax@gmail.com>
parents:
1609
diff
changeset
|
689 } |
159 | 690 goto L1; |
691 } | |
692 L2: | |
693 Type *tb = p->type->toBasetype(); | |
694 Type *tret = p->isLazyArray(); | |
695 switch (tb->ty) | |
696 { | |
697 case Tsarray: | |
698 case Tarray: | |
699 { // Create a static array variable v of type arg->type | |
700 #ifdef IN_GCC | |
701 /* GCC 4.0 does not like zero length arrays used like | |
702 this; pass a null array value instead. Could also | |
703 just make a one-element array. */ | |
704 if (nargs - i == 0) | |
705 { | |
706 arg = new NullExp(loc); | |
707 break; | |
708 } | |
709 #endif | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
710 Identifier *id = Lexer::uniqueId("__arrayArg"); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
711 Type *t = new TypeSArray(((TypeArray *)tb)->next, new IntegerExp(nargs - i)); |
159 | 712 t = t->semantic(loc, sc); |
713 VarDeclaration *v = new VarDeclaration(loc, t, id, new VoidInitializer(loc)); | |
1621
fb2e6707ad17
Merge DMD r314+r315: bugzilla 2029 Typesafe variadic functions don't...
Leandro Lucarella <llucax@gmail.com>
parents:
1619
diff
changeset
|
714 v->storage_class |= STCctfe; |
159 | 715 v->semantic(sc); |
716 v->parent = sc->parent; | |
717 //sc->insert(v); | |
718 | |
719 Expression *c = new DeclarationExp(0, v); | |
720 c->type = v->type; | |
721 | |
722 for (size_t u = i; u < nargs; u++) | |
723 { Expression *a = (Expression *)arguments->data[u]; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
724 if (tret && !((TypeArray *)tb)->next->equals(a->type)) |
159 | 725 a = a->toDelegate(sc, tret); |
726 | |
727 Expression *e = new VarExp(loc, v); | |
728 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
|
729 AssignExp *ae = new AssignExp(loc, e, a); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
730 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
731 ae->op = TOKconstruct; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
732 #endif |
159 | 733 if (c) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
734 c = new CommaExp(loc, c, ae); |
159 | 735 else |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
736 c = ae; |
159 | 737 } |
738 arg = new VarExp(loc, v); | |
739 if (c) | |
740 arg = new CommaExp(loc, c, arg); | |
741 break; | |
742 } | |
743 case Tclass: | |
744 { /* Set arg to be: | |
745 * new Tclass(arg0, arg1, ..., argn) | |
746 */ | |
747 Expressions *args = new Expressions(); | |
748 args->setDim(nargs - i); | |
749 for (size_t u = i; u < nargs; u++) | |
750 args->data[u - i] = arguments->data[u]; | |
751 arg = new NewExp(loc, NULL, NULL, p->type, args); | |
752 break; | |
753 } | |
754 default: | |
755 if (!arg) | |
756 { error(loc, "not enough arguments"); | |
757 return; | |
758 } | |
759 break; | |
760 } | |
761 arg = arg->semantic(sc); | |
762 //printf("\targ = '%s'\n", arg->toChars()); | |
763 arguments->setDim(i + 1); | |
764 done = 1; | |
765 } | |
766 | |
767 L1: | |
768 if (!(p->storageClass & STClazy && p->type->ty == Tvoid)) | |
1587 | 769 { |
770 if (p->type != arg->type) | |
771 { | |
772 //printf("arg->type = %s, p->type = %s\n", arg->type->toChars(), p->type->toChars()); | |
773 if (arg->op == TOKtype) | |
774 arg->error("cannot pass type %s as function argument", arg->toChars()); | |
775 arg = arg->implicitCastTo(sc, p->type); | |
776 arg = arg->optimize(WANTvalue); | |
777 } | |
778 } | |
159 | 779 if (p->storageClass & (STCout | STCref)) |
780 { | |
781 // BUG: should check that argument to ref is type 'invariant' | |
782 // BUG: assignments to ref should also be type 'invariant' | |
783 arg = arg->modifiableLvalue(sc, arg); | |
784 | |
785 //if (arg->op == TOKslice) | |
786 //arg->error("cannot modify slice %s", arg->toChars()); | |
787 } | |
788 | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
789 // 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
|
790 #if !IN_LLVM |
159 | 791 // Convert static arrays to pointers |
792 tb = arg->type->toBasetype(); | |
793 if (tb->ty == Tsarray) | |
794 { | |
795 arg = arg->checkToPointer(); | |
796 } | |
723
55f6c2e454d7
Implemented correct parameter order according to x86-32 ABI documentation.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
717
diff
changeset
|
797 #endif |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
798 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
799 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
|
800 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
801 arg = callCpCtor(loc, sc, arg); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
802 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
803 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
804 |
159 | 805 // Convert lazy argument to a delegate |
806 if (p->storageClass & STClazy) | |
807 { | |
808 arg = arg->toDelegate(sc, p->type); | |
809 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
810 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
811 /* 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
|
812 * function. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
813 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
814 if (!tf->parameterEscapes(p)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
815 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
816 /* 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
|
817 * 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
|
818 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
819 if (arg->op == TOKfunction) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
820 { FuncExp *fe = (FuncExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
821 fe->fd->tookAddressOf = 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
822 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
823 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
824 /* 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
|
825 * 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
|
826 * 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
|
827 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
828 else if (arg->op == TOKdelegate) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
829 { DelegateExp *de = (DelegateExp *)arg; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
830 if (de->e1->op == TOKvar) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
831 { VarExp *ve = (VarExp *)de->e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
832 FuncDeclaration *f = ve->var->isFuncDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
833 if (f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
834 { f->tookAddressOf--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
835 //printf("tookAddressOf = %d\n", f->tookAddressOf); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
836 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
837 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
838 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
839 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
840 #endif |
159 | 841 } |
842 else | |
843 { | |
844 | |
845 // 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
|
846 // 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
|
847 if (tf->linkage != LINKd && tf->linkage != LINKintrinsic) |
159 | 848 { |
849 // Promote bytes, words, etc., to ints | |
850 arg = arg->integralPromotions(sc); | |
851 | |
852 // Promote floats to doubles | |
853 switch (arg->type->ty) | |
854 { | |
855 case Tfloat32: | |
856 arg = arg->castTo(sc, Type::tfloat64); | |
857 break; | |
858 | |
859 case Timaginary32: | |
860 arg = arg->castTo(sc, Type::timaginary64); | |
861 break; | |
862 } | |
863 } | |
864 | |
865 // Convert static arrays to dynamic arrays | |
866 tb = arg->type->toBasetype(); | |
867 if (tb->ty == Tsarray) | |
868 { TypeSArray *ts = (TypeSArray *)tb; | |
336 | 869 Type *ta = ts->next->arrayOf(); |
159 | 870 if (ts->size(arg->loc) == 0) |
871 { arg = new NullExp(arg->loc); | |
872 arg->type = ta; | |
873 } | |
874 else | |
875 arg = arg->castTo(sc, ta); | |
876 } | |
336 | 877 #if DMDV2 |
878 if (tb->ty == Tstruct) | |
879 { | |
880 arg = callCpCtor(loc, sc, arg); | |
881 } | |
1587 | 882 #endif |
336 | 883 |
884 // Give error for overloaded function addresses | |
1587 | 885 #if DMDV2 |
336 | 886 if (arg->op == TOKsymoff) |
887 { SymOffExp *se = (SymOffExp *)arg; | |
1587 | 888 if ( |
889 se->hasOverloads && | |
890 !se->var->isFuncDeclaration()->isUnique()) | |
336 | 891 arg->error("function %s is overloaded", arg->toChars()); |
892 } | |
893 #endif | |
159 | 894 arg->rvalue(); |
895 } | |
896 arg = arg->optimize(WANTvalue); | |
897 arguments->data[i] = (void *) arg; | |
898 if (done) | |
899 break; | |
900 } | |
901 | |
902 #if !IN_LLVM | |
903 // If D linkage and variadic, add _arguments[] as first argument | |
904 if (tf->linkage == LINKd && tf->varargs == 1) | |
905 { | |
1611
3f728445fc42
Merge DMD r277: bugzilla 3495 Segfault(typinf.c) instantiating D variadic...
Leandro Lucarella <llucax@gmail.com>
parents:
1609
diff
changeset
|
906 assert(arguments->dim >= nparams); |
3f728445fc42
Merge DMD r277: bugzilla 3495 Segfault(typinf.c) instantiating D variadic...
Leandro Lucarella <llucax@gmail.com>
parents:
1609
diff
changeset
|
907 Expression *e = createTypeInfoArray(sc, (Expression **)&arguments->data[nparams], |
159 | 908 arguments->dim - nparams); |
909 arguments->insert(0, e); | |
910 } | |
911 #endif | |
912 } | |
913 | |
914 /************************************************** | |
915 * Write expression out to buf, but wrap it | |
916 * in ( ) if its precedence is less than pr. | |
917 */ | |
918 | |
919 void expToCBuffer(OutBuffer *buf, HdrGenState *hgs, Expression *e, enum PREC pr) | |
920 { | |
336 | 921 //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
|
922 if (precedence[e->op] < pr || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
923 /* 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
|
924 * They must be parenthesized. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
925 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
926 (pr == PREC_rel && precedence[e->op] == pr)) |
159 | 927 { |
928 buf->writeByte('('); | |
929 e->toCBuffer(buf, hgs); | |
930 buf->writeByte(')'); | |
931 } | |
932 else | |
933 e->toCBuffer(buf, hgs); | |
934 } | |
935 | |
936 /************************************************** | |
937 * Write out argument list to buf. | |
938 */ | |
939 | |
940 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
941 { | |
942 if (arguments) | |
943 { | |
944 for (size_t i = 0; i < arguments->dim; i++) | |
945 { Expression *arg = (Expression *)arguments->data[i]; | |
946 | |
947 if (arg) | |
948 { if (i) | |
949 buf->writeByte(','); | |
950 expToCBuffer(buf, hgs, arg, PREC_assign); | |
951 } | |
952 } | |
953 } | |
954 } | |
955 | |
956 /************************************************** | |
957 * Write out argument types to buf. | |
958 */ | |
959 | |
960 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs) | |
961 { | |
962 if (arguments) | |
963 { OutBuffer argbuf; | |
964 | |
965 for (size_t i = 0; i < arguments->dim; i++) | |
966 { Expression *arg = (Expression *)arguments->data[i]; | |
967 | |
968 if (i) | |
969 buf->writeByte(','); | |
970 argbuf.reset(); | |
971 arg->type->toCBuffer2(&argbuf, hgs, 0); | |
972 buf->write(&argbuf); | |
973 } | |
974 } | |
975 } | |
976 | |
977 /******************************** Expression **************************/ | |
978 | |
979 Expression::Expression(Loc loc, enum TOK op, int size) | |
980 : loc(loc) | |
981 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
982 //printf("Expression::Expression(op = %d) this = %p\n", op, this); |
159 | 983 this->loc = loc; |
984 this->op = op; | |
985 this->size = size; | |
986 type = NULL; | |
1151
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
987 |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
988 #if IN_LLVM |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
989 cachedLvalue = NULL; |
3cf0066e6faf
- Versioned Expresssion::toElem with #if IN_DMD/IN_LLVM.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1146
diff
changeset
|
990 #endif |
159 | 991 } |
992 | |
993 Expression *Expression::syntaxCopy() | |
994 { | |
995 //printf("Expression::syntaxCopy()\n"); | |
996 //dump(0); | |
997 return copy(); | |
998 } | |
999 | |
1000 /********************************* | |
1001 * Does *not* do a deep copy. | |
1002 */ | |
1003 | |
1004 Expression *Expression::copy() | |
1005 { | |
1006 Expression *e; | |
1007 if (!size) | |
1008 { | |
1009 #ifdef DEBUG | |
1010 fprintf(stdmsg, "No expression copy for: %s\n", toChars()); | |
1011 printf("op = %d\n", op); | |
1012 dump(0); | |
1013 #endif | |
1014 assert(0); | |
1015 } | |
1016 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
|
1017 //printf("Expression::copy(op = %d) e = %p\n", op, e); |
159 | 1018 return (Expression *)memcpy(e, this, size); |
1019 } | |
1020 | |
1021 /************************** | |
1022 * Semantically analyze Expression. | |
1023 * Determine types, fold constants, etc. | |
1024 */ | |
1025 | |
1026 Expression *Expression::semantic(Scope *sc) | |
1027 { | |
1028 #if LOGSEMANTIC | |
336 | 1029 printf("Expression::semantic() %s\n", toChars()); |
159 | 1030 #endif |
1031 if (type) | |
1032 type = type->semantic(loc, sc); | |
1033 else | |
1034 type = Type::tvoid; | |
1035 return this; | |
1036 } | |
1037 | |
1038 void Expression::print() | |
1039 { | |
1040 fprintf(stdmsg, "%s\n", toChars()); | |
1041 fflush(stdmsg); | |
1042 } | |
1043 | |
1044 char *Expression::toChars() | |
1045 { OutBuffer *buf; | |
1046 HdrGenState hgs; | |
1047 | |
1048 memset(&hgs, 0, sizeof(hgs)); | |
1049 buf = new OutBuffer(); | |
1050 toCBuffer(buf, &hgs); | |
1051 return buf->toChars(); | |
1052 } | |
1053 | |
1054 void Expression::error(const char *format, ...) | |
1055 { | |
1056 va_list ap; | |
1057 va_start(ap, format); | |
1058 ::verror(loc, format, ap); | |
1059 va_end( ap ); | |
1060 } | |
1061 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1062 void Expression::warning(const char *format, ...) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1063 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1064 if (global.params.warnings && !global.gag) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1065 { |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1066 va_list ap; |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1067 va_start(ap, format); |
1124
e7f0c2b48047
Fix a bug where ::warning() was called with a va_list argument instead of an
Frits van Bommel <fvbommel wxs.nl>
parents:
1120
diff
changeset
|
1068 ::vwarning(loc, format, ap); |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1069 va_end( ap ); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1070 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1071 } |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1072 |
159 | 1073 void Expression::rvalue() |
1074 { | |
1075 if (type && type->toBasetype()->ty == Tvoid) | |
1076 { error("expression %s is void and has no value", toChars()); | |
1077 #if 0 | |
1078 dump(0); | |
1079 halt(); | |
1080 #endif | |
336 | 1081 type = Type::tint32; |
159 | 1082 } |
1083 } | |
1084 | |
1085 Expression *Expression::combine(Expression *e1, Expression *e2) | |
1086 { | |
1087 if (e1) | |
1088 { | |
1089 if (e2) | |
1090 { | |
1091 e1 = new CommaExp(e1->loc, e1, e2); | |
1092 e1->type = e2->type; | |
1093 } | |
1094 } | |
1095 else | |
1096 e1 = e2; | |
1097 return e1; | |
1098 } | |
1099 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1100 dinteger_t Expression::toInteger() |
159 | 1101 { |
1102 //printf("Expression %s\n", Token::toChars(op)); | |
1103 error("Integer constant expression expected instead of %s", toChars()); | |
1104 return 0; | |
1105 } | |
1106 | |
1107 uinteger_t Expression::toUInteger() | |
1108 { | |
1109 //printf("Expression %s\n", Token::toChars(op)); | |
1110 return (uinteger_t)toInteger(); | |
1111 } | |
1112 | |
1113 real_t Expression::toReal() | |
1114 { | |
1115 error("Floating point constant expression expected instead of %s", toChars()); | |
1116 return 0; | |
1117 } | |
1118 | |
1119 real_t Expression::toImaginary() | |
1120 { | |
1121 error("Floating point constant expression expected instead of %s", toChars()); | |
1122 return 0; | |
1123 } | |
1124 | |
1125 complex_t Expression::toComplex() | |
1126 { | |
1127 error("Floating point constant expression expected instead of %s", toChars()); | |
1128 #ifdef IN_GCC | |
1129 return complex_t(real_t(0)); // %% nicer | |
1130 #else | |
1131 return 0; | |
1132 #endif | |
1133 } | |
1134 | |
1135 void Expression::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1136 { | |
1137 buf->writestring(Token::toChars(op)); | |
1138 } | |
1139 | |
1140 void Expression::toMangleBuffer(OutBuffer *buf) | |
1141 { | |
1142 error("expression %s is not a valid template value argument", toChars()); | |
1143 } | |
1144 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1145 /*************************************** |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1146 * Return !=0 if expression is an lvalue. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1147 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1148 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1149 int Expression::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1150 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1151 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1152 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1153 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1154 |
159 | 1155 /******************************* |
1156 * Give error if we're not an lvalue. | |
1157 * If we can, convert expression to be an lvalue. | |
1158 */ | |
1159 | |
1160 Expression *Expression::toLvalue(Scope *sc, Expression *e) | |
1161 { | |
1162 if (!e) | |
1163 e = this; | |
1164 else if (!loc.filename) | |
1165 loc = e->loc; | |
1166 error("%s is not an lvalue", e->toChars()); | |
1167 return this; | |
1168 } | |
1169 | |
1170 Expression *Expression::modifiableLvalue(Scope *sc, Expression *e) | |
1171 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
1172 //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
|
1173 |
159 | 1174 // 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
|
1175 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1176 if (type && (!type->isMutable() || !type->isAssignable())) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1177 error("%s is not mutable", e->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1178 #endif |
159 | 1179 return toLvalue(sc, e); |
1180 } | |
1181 | |
1182 /************************************ | |
1183 * Detect cases where pointers to the stack can 'escape' the | |
1184 * lifetime of the stack frame. | |
1185 */ | |
1186 | |
1187 void Expression::checkEscape() | |
1188 { | |
1189 } | |
1190 | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
1191 void Expression::checkEscapeRef() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
1192 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
1193 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
1194 |
159 | 1195 void Expression::checkScalar() |
1196 { | |
1197 if (!type->isscalar()) | |
1198 error("'%s' is not a scalar, it is a %s", toChars(), type->toChars()); | |
1199 } | |
1200 | |
1201 void Expression::checkNoBool() | |
1202 { | |
1203 if (type->toBasetype()->ty == Tbool) | |
1204 error("operation not allowed on bool '%s'", toChars()); | |
1205 } | |
1206 | |
1207 Expression *Expression::checkIntegral() | |
1208 { | |
1209 if (!type->isintegral()) | |
1210 { error("'%s' is not of integral type, it is a %s", toChars(), type->toChars()); | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
1211 return new ErrorExp(); |
159 | 1212 } |
1213 return this; | |
1214 } | |
1215 | |
1216 Expression *Expression::checkArithmetic() | |
1217 { | |
1218 if (!type->isintegral() && !type->isfloating()) | |
1219 { error("'%s' is not of arithmetic type, it is a %s", toChars(), type->toChars()); | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
1220 return new ErrorExp(); |
159 | 1221 } |
1222 return this; | |
1223 } | |
1224 | |
1225 void Expression::checkDeprecated(Scope *sc, Dsymbol *s) | |
1226 { | |
1227 s->checkDeprecated(loc, sc); | |
1228 } | |
1229 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1230 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1231 void Expression::checkPurity(Scope *sc, FuncDeclaration *f) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1232 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1233 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
|
1234 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
|
1235 sc->func->toChars(), f->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1236 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1237 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1238 |
159 | 1239 /******************************** |
1240 * Check for expressions that have no use. | |
1241 * Input: | |
1242 * flag 0 not going to use the result, so issue error message if no | |
1243 * side effects | |
1244 * 1 the result of the expression is used, but still check | |
1245 * for useless subexpressions | |
1246 * 2 do not issue error messages, just return !=0 if expression | |
1247 * has side effects | |
1248 */ | |
1249 | |
1250 int Expression::checkSideEffect(int flag) | |
1251 { | |
1252 if (flag == 0) | |
1253 { if (op == TOKimport) | |
1254 { | |
1255 error("%s has no effect", toChars()); | |
1256 } | |
1257 else | |
1258 error("%s has no effect in expression (%s)", | |
1259 Token::toChars(op), toChars()); | |
1260 } | |
1261 return 0; | |
1262 } | |
1263 | |
1264 /***************************** | |
1265 * Check that expression can be tested for true or false. | |
1266 */ | |
1267 | |
1268 Expression *Expression::checkToBoolean() | |
1269 { | |
1270 // Default is 'yes' - do nothing | |
1271 | |
1272 #ifdef DEBUG | |
1273 if (!type) | |
1274 dump(0); | |
1275 #endif | |
1276 | |
1277 if (!type->checkBoolean()) | |
1278 { | |
1279 error("expression %s of type %s does not have a boolean value", toChars(), type->toChars()); | |
1280 } | |
1281 return this; | |
1282 } | |
1283 | |
1284 /**************************** | |
1285 */ | |
1286 | |
1287 Expression *Expression::checkToPointer() | |
1288 { | |
1289 Expression *e; | |
1290 Type *tb; | |
1291 | |
1292 //printf("Expression::checkToPointer()\n"); | |
1293 e = this; | |
1294 | |
1295 // If C static array, convert to pointer | |
1296 tb = type->toBasetype(); | |
1297 if (tb->ty == Tsarray) | |
1298 { TypeSArray *ts = (TypeSArray *)tb; | |
1299 if (ts->size(loc) == 0) | |
1300 e = new NullExp(loc); | |
1301 else | |
1302 e = new AddrExp(loc, this); | |
336 | 1303 e->type = ts->next->pointerTo(); |
159 | 1304 } |
1305 return e; | |
1306 } | |
1307 | |
1308 /****************************** | |
1309 * Take address of expression. | |
1310 */ | |
1311 | |
1312 Expression *Expression::addressOf(Scope *sc) | |
1313 { | |
1314 Expression *e; | |
1315 | |
1316 //printf("Expression::addressOf()\n"); | |
1317 e = toLvalue(sc, NULL); | |
1318 e = new AddrExp(loc, e); | |
1319 e->type = type->pointerTo(); | |
1320 return e; | |
1321 } | |
1322 | |
1323 /****************************** | |
1324 * If this is a reference, dereference it. | |
1325 */ | |
1326 | |
1327 Expression *Expression::deref() | |
1328 { | |
1329 //printf("Expression::deref()\n"); | |
1587 | 1330 // type could be null if forward referencing an 'auto' variable |
1331 if (type && type->ty == Treference) | |
1332 { | |
1333 Expression *e = new PtrExp(loc, this); | |
336 | 1334 e->type = ((TypeReference *)type)->next; |
159 | 1335 return e; |
1336 } | |
1337 return this; | |
1338 } | |
1339 | |
1340 /******************************** | |
1341 * Does this expression statically evaluate to a boolean TRUE or FALSE? | |
1342 */ | |
1343 | |
1344 int Expression::isBool(int result) | |
1345 { | |
1346 return FALSE; | |
1347 } | |
1348 | |
1349 /******************************** | |
1350 * Does this expression result in either a 1 or a 0? | |
1351 */ | |
1352 | |
1353 int Expression::isBit() | |
1354 { | |
1355 return FALSE; | |
1356 } | |
1357 | |
336 | 1358 /******************************** |
1359 * Can this expression throw an exception? | |
1360 * Valid only after semantic() pass. | |
1361 */ | |
1362 | |
1363 int Expression::canThrow() | |
1364 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1365 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1366 return FALSE; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1367 #else |
336 | 1368 return TRUE; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
1369 #endif |
336 | 1370 } |
1371 | |
1372 | |
1373 | |
159 | 1374 Expressions *Expression::arraySyntaxCopy(Expressions *exps) |
1375 { Expressions *a = NULL; | |
1376 | |
1377 if (exps) | |
1378 { | |
1379 a = new Expressions(); | |
1380 a->setDim(exps->dim); | |
1381 for (int i = 0; i < a->dim; i++) | |
1382 { Expression *e = (Expression *)exps->data[i]; | |
1383 | |
1384 e = e->syntaxCopy(); | |
1385 a->data[i] = e; | |
1386 } | |
1387 } | |
1388 return a; | |
1389 } | |
1390 | |
1391 /******************************** IntegerExp **************************/ | |
1392 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1393 IntegerExp::IntegerExp(Loc loc, dinteger_t value, Type *type) |
159 | 1394 : Expression(loc, TOKint64, sizeof(IntegerExp)) |
1395 { | |
1396 //printf("IntegerExp(value = %lld, type = '%s')\n", value, type ? type->toChars() : ""); | |
1397 if (type && !type->isscalar()) | |
1398 { | |
336 | 1399 //printf("%s, loc = %d\n", toChars(), loc.linnum); |
159 | 1400 error("integral constant must be scalar type, not %s", type->toChars()); |
1401 type = Type::terror; | |
1402 } | |
1403 this->type = type; | |
1404 this->value = value; | |
1405 } | |
1406 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1407 IntegerExp::IntegerExp(dinteger_t value) |
159 | 1408 : Expression(0, TOKint64, sizeof(IntegerExp)) |
1409 { | |
1410 this->type = Type::tint32; | |
1411 this->value = value; | |
1412 } | |
1413 | |
1414 int IntegerExp::equals(Object *o) | |
1415 { IntegerExp *ne; | |
1416 | |
1417 if (this == o || | |
1418 (((Expression *)o)->op == TOKint64 && | |
1419 ((ne = (IntegerExp *)o), type->equals(ne->type)) && | |
1420 value == ne->value)) | |
1421 return 1; | |
1422 return 0; | |
1423 } | |
1424 | |
1425 char *IntegerExp::toChars() | |
1426 { | |
1427 #if 1 | |
1428 return Expression::toChars(); | |
1429 #else | |
1430 static char buffer[sizeof(value) * 3 + 1]; | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1431 |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1432 sprintf(buffer, "%jd", value); |
159 | 1433 return buffer; |
1434 #endif | |
1435 } | |
1436 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1437 dinteger_t IntegerExp::toInteger() |
159 | 1438 { Type *t; |
1439 | |
1440 t = type; | |
1441 while (t) | |
1442 { | |
1443 switch (t->ty) | |
1444 { | |
1445 case Tbit: | |
1446 case Tbool: value = (value != 0); break; | |
1447 case Tint8: value = (d_int8) value; break; | |
1448 case Tchar: | |
1449 case Tuns8: value = (d_uns8) value; break; | |
1450 case Tint16: value = (d_int16) value; break; | |
1451 case Twchar: | |
1452 case Tuns16: value = (d_uns16) value; break; | |
1453 case Tint32: value = (d_int32) value; break; | |
1454 case Tdchar: | |
1455 case Tuns32: value = (d_uns32) value; break; | |
1456 case Tint64: value = (d_int64) value; break; | |
1457 case Tuns64: value = (d_uns64) value; break; | |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1458 case Tpointer: |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1459 if (PTRSIZE == 4) |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1460 value = (d_uns32) value; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1461 else if (PTRSIZE == 8) |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1462 value = (d_uns64) value; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1463 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1464 assert(0); |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1465 break; |
159 | 1466 |
1467 case Tenum: | |
1468 { | |
1469 TypeEnum *te = (TypeEnum *)t; | |
1470 t = te->sym->memtype; | |
1471 continue; | |
1472 } | |
1473 | |
1474 case Ttypedef: | |
1475 { | |
1476 TypeTypedef *tt = (TypeTypedef *)t; | |
1477 t = tt->sym->basetype; | |
1478 continue; | |
1479 } | |
1480 | |
1481 default: | |
336 | 1482 /* This can happen if errors, such as |
1483 * the type is painted on like in fromConstInitializer(). | |
1484 */ | |
1485 if (!global.errors) | |
1486 { type->print(); | |
1487 assert(0); | |
1488 } | |
159 | 1489 break; |
1490 } | |
1491 break; | |
1492 } | |
1493 return value; | |
1494 } | |
1495 | |
1496 real_t IntegerExp::toReal() | |
1497 { | |
1498 Type *t; | |
1499 | |
1500 toInteger(); | |
1501 t = type->toBasetype(); | |
1502 if (t->ty == Tuns64) | |
1503 return (real_t)(d_uns64)value; | |
1504 else | |
1505 return (real_t)(d_int64)value; | |
1506 } | |
1507 | |
1508 real_t IntegerExp::toImaginary() | |
1509 { | |
1510 return (real_t) 0; | |
1511 } | |
1512 | |
1513 complex_t IntegerExp::toComplex() | |
1514 { | |
1515 return toReal(); | |
1516 } | |
1517 | |
1518 int IntegerExp::isBool(int result) | |
1519 { | |
1616
c94049033c20
Merge DMD r296: bugzilla 3617 CTFE: wrong code for if(x) where x is int...
Leandro Lucarella <llucax@gmail.com>
parents:
1612
diff
changeset
|
1520 int r = toInteger() != 0; |
c94049033c20
Merge DMD r296: bugzilla 3617 CTFE: wrong code for if(x) where x is int...
Leandro Lucarella <llucax@gmail.com>
parents:
1612
diff
changeset
|
1521 return result ? r : !r; |
159 | 1522 } |
1523 | |
1524 Expression *IntegerExp::semantic(Scope *sc) | |
1525 { | |
1526 if (!type) | |
1527 { | |
1528 // Determine what the type of this number is | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1529 dinteger_t number = value; |
159 | 1530 |
1531 if (number & 0x8000000000000000LL) | |
1532 type = Type::tuns64; | |
1533 else if (number & 0xFFFFFFFF80000000LL) | |
1534 type = Type::tint64; | |
1535 else | |
1536 type = Type::tint32; | |
1537 } | |
1538 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
|
1539 { 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
|
1540 type = type->semantic(loc, sc); |
159 | 1541 } |
1542 return this; | |
1543 } | |
1544 | |
1545 Expression *IntegerExp::toLvalue(Scope *sc, Expression *e) | |
1546 { | |
1547 if (!e) | |
1548 e = this; | |
1549 else if (!loc.filename) | |
1550 loc = e->loc; | |
1551 e->error("constant %s is not an lvalue", e->toChars()); | |
1552 return this; | |
1553 } | |
1554 | |
1555 void IntegerExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1556 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1557 dinteger_t v = toInteger(); |
159 | 1558 |
1559 if (type) | |
1560 { Type *t = type; | |
1561 | |
1562 L1: | |
1563 switch (t->ty) | |
1564 { | |
1565 case Tenum: | |
1566 { TypeEnum *te = (TypeEnum *)t; | |
1567 buf->printf("cast(%s)", te->sym->toChars()); | |
1568 t = te->sym->memtype; | |
1569 goto L1; | |
1570 } | |
1571 | |
1572 case Ttypedef: | |
1573 { TypeTypedef *tt = (TypeTypedef *)t; | |
1574 buf->printf("cast(%s)", tt->sym->toChars()); | |
1575 t = tt->sym->basetype; | |
1576 goto L1; | |
1577 } | |
1578 | |
1579 case Twchar: // BUG: need to cast(wchar) | |
1580 case Tdchar: // BUG: need to cast(dchar) | |
1581 if ((uinteger_t)v > 0xFF) | |
1582 { | |
1372
229e02867307
Fix format-string bugs by adding __attribute__((__format__)) in all applicable
Frits van Bommel <fvbommel wxs.nl>
parents:
1367
diff
changeset
|
1583 buf->printf("'\\U%08x'", (unsigned)v); |
159 | 1584 break; |
1585 } | |
1586 case Tchar: | |
1587 if (v == '\'') | |
1588 buf->writestring("'\\''"); | |
1589 else if (isprint(v) && v != '\\') | |
1590 buf->printf("'%c'", (int)v); | |
1591 else | |
1592 buf->printf("'\\x%02x'", (int)v); | |
1593 break; | |
1594 | |
1595 case Tint8: | |
1596 buf->writestring("cast(byte)"); | |
1597 goto L2; | |
1598 | |
1599 case Tint16: | |
1600 buf->writestring("cast(short)"); | |
1601 goto L2; | |
1602 | |
1603 case Tint32: | |
1604 L2: | |
1605 buf->printf("%d", (int)v); | |
1606 break; | |
1607 | |
1608 case Tuns8: | |
1609 buf->writestring("cast(ubyte)"); | |
1610 goto L3; | |
1611 | |
1612 case Tuns16: | |
1613 buf->writestring("cast(ushort)"); | |
1614 goto L3; | |
1615 | |
1616 case Tuns32: | |
1617 L3: | |
1618 buf->printf("%du", (unsigned)v); | |
1619 break; | |
1620 | |
1621 case Tint64: | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1622 buf->printf("%jdL", v); |
159 | 1623 break; |
1624 | |
1625 case Tuns64: | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1626 L4: |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1627 buf->printf("%juLU", v); |
159 | 1628 break; |
1629 | |
1630 case Tbit: | |
1631 case Tbool: | |
1632 buf->writestring((char *)(v ? "true" : "false")); | |
1633 break; | |
1634 | |
1635 case Tpointer: | |
1636 buf->writestring("cast("); | |
1637 buf->writestring(t->toChars()); | |
1638 buf->writeByte(')'); | |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1639 if (PTRSIZE == 4) |
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1640 goto L3; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1641 else if (PTRSIZE == 8) |
1120
0d38fa5a0b46
Another "pointers are 32 bits" issue in the frontend. long/ulong constants cast
Frits van Bommel <fvbommel wxs.nl>
parents:
1103
diff
changeset
|
1642 goto L4; |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1643 else |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1644 assert(0); |
159 | 1645 |
1646 default: | |
336 | 1647 /* This can happen if errors, such as |
1648 * the type is painted on like in fromConstInitializer(). | |
1649 */ | |
1650 if (!global.errors) | |
1651 { | |
159 | 1652 #ifdef DEBUG |
336 | 1653 t->print(); |
159 | 1654 #endif |
336 | 1655 assert(0); |
1656 } | |
1657 break; | |
159 | 1658 } |
1659 } | |
1660 else if (v & 0x8000000000000000LL) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1661 buf->printf("0x%jx", v); |
159 | 1662 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1663 buf->printf("%jd", v); |
159 | 1664 } |
1665 | |
1666 void IntegerExp::toMangleBuffer(OutBuffer *buf) | |
1667 { | |
1668 if ((sinteger_t)value < 0) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1669 buf->printf("N%jd", -value); |
159 | 1670 else |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1671 buf->printf("%jd", value); |
159 | 1672 } |
1673 | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1674 /******************************** ErrorExp **************************/ |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1675 |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1676 /* Use this expression for error recovery. |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1677 * It should behave as a 'sink' to prevent further cascaded error messages. |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1678 */ |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1679 |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1680 ErrorExp::ErrorExp() |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1681 : IntegerExp(0, 0, Type::terror) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1682 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1683 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1684 |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1685 void ErrorExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1686 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1687 buf->writestring("__error"); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1688 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
1689 |
159 | 1690 /******************************** RealExp **************************/ |
1691 | |
1692 RealExp::RealExp(Loc loc, real_t value, Type *type) | |
1693 : Expression(loc, TOKfloat64, sizeof(RealExp)) | |
1694 { | |
1695 //printf("RealExp::RealExp(%Lg)\n", value); | |
1696 this->value = value; | |
1697 this->type = type; | |
1698 } | |
1699 | |
1700 char *RealExp::toChars() | |
1701 { | |
1702 char buffer[sizeof(value) * 3 + 8 + 1 + 1]; | |
1703 | |
1704 #ifdef IN_GCC | |
1705 value.format(buffer, sizeof(buffer)); | |
1706 if (type->isimaginary()) | |
1707 strcat(buffer, "i"); | |
1708 #else | |
1709 sprintf(buffer, type->isimaginary() ? "%Lgi" : "%Lg", value); | |
1710 #endif | |
1711 assert(strlen(buffer) < sizeof(buffer)); | |
1712 return mem.strdup(buffer); | |
1713 } | |
1714 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1715 dinteger_t RealExp::toInteger() |
159 | 1716 { |
1717 #ifdef IN_GCC | |
1718 return toReal().toInt(); | |
1719 #else | |
1720 return (sinteger_t) toReal(); | |
1721 #endif | |
1722 } | |
1723 | |
1724 uinteger_t RealExp::toUInteger() | |
1725 { | |
1726 #ifdef IN_GCC | |
1727 return (uinteger_t) toReal().toInt(); | |
1728 #else | |
1729 return (uinteger_t) toReal(); | |
1730 #endif | |
1731 } | |
1732 | |
1733 real_t RealExp::toReal() | |
1734 { | |
1735 return type->isreal() ? value : 0; | |
1736 } | |
1737 | |
1738 real_t RealExp::toImaginary() | |
1739 { | |
1740 return type->isreal() ? 0 : value; | |
1741 } | |
1742 | |
1743 complex_t RealExp::toComplex() | |
1744 { | |
1745 #ifdef __DMC__ | |
1746 return toReal() + toImaginary() * I; | |
1747 #else | |
1748 return complex_t(toReal(), toImaginary()); | |
1749 #endif | |
1750 } | |
1751 | |
1752 /******************************** | |
1753 * Test to see if two reals are the same. | |
1754 * Regard NaN's as equivalent. | |
1755 * Regard +0 and -0 as different. | |
1756 */ | |
1757 | |
1758 int RealEquals(real_t x1, real_t x2) | |
1759 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1760 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1761 return (__inline_isnan(x1) && __inline_isnan(x2)) || |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1762 #else |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1763 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
|
1764 (isnan(x1) && isnan(x2)) || |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1765 #endif |
421
1c65b5477eaa
Fix real comparison for real: allow +0 to be distinguished from -0.
Christian Kamm <kamm incasoftware de>
parents:
420
diff
changeset
|
1766 // 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
|
1767 (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
|
1768 // 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
|
1769 (x1 != 0. && x1 == x2); |
159 | 1770 } |
1771 | |
1772 int RealExp::equals(Object *o) | |
1773 { RealExp *ne; | |
1774 | |
1775 if (this == o || | |
1776 (((Expression *)o)->op == TOKfloat64 && | |
1777 ((ne = (RealExp *)o), type->equals(ne->type)) && | |
1778 RealEquals(value, ne->value) | |
1779 ) | |
1780 ) | |
1781 return 1; | |
1782 return 0; | |
1783 } | |
1784 | |
1785 Expression *RealExp::semantic(Scope *sc) | |
1786 { | |
1787 if (!type) | |
1788 type = Type::tfloat64; | |
1789 else | |
1790 type = type->semantic(loc, sc); | |
1791 return this; | |
1792 } | |
1793 | |
1794 int RealExp::isBool(int result) | |
1795 { | |
1796 #ifdef IN_GCC | |
1797 return result ? (! value.isZero()) : (value.isZero()); | |
1798 #else | |
1799 return result ? (value != 0) | |
1800 : (value == 0); | |
1801 #endif | |
1802 } | |
1803 | |
1804 void floatToBuffer(OutBuffer *buf, Type *type, real_t value) | |
1805 { | |
1806 /* In order to get an exact representation, try converting it | |
1807 * to decimal then back again. If it matches, use it. | |
1808 * If it doesn't, fall back to hex, which is | |
1809 * always exact. | |
1810 */ | |
1811 char buffer[25]; | |
1812 sprintf(buffer, "%Lg", value); | |
1813 assert(strlen(buffer) < sizeof(buffer)); | |
1814 #if _WIN32 && __DMC__ | |
1815 char *save = __locale_decpoint; | |
1816 __locale_decpoint = "."; | |
1817 real_t r = strtold(buffer, NULL); | |
1818 __locale_decpoint = save; | |
1819 #else | |
1820 real_t r = strtold(buffer, NULL); | |
1821 #endif | |
1822 if (r == value) // if exact duplication | |
1823 buf->writestring(buffer); | |
1824 else | |
1825 buf->printf("%La", value); // ensure exact duplication | |
1826 | |
1827 if (type) | |
1828 { | |
1829 Type *t = type->toBasetype(); | |
1830 switch (t->ty) | |
1831 { | |
1832 case Tfloat32: | |
1833 case Timaginary32: | |
1834 case Tcomplex32: | |
1835 buf->writeByte('F'); | |
1836 break; | |
1837 | |
1838 case Tfloat80: | |
1839 case Timaginary80: | |
1840 case Tcomplex80: | |
1841 buf->writeByte('L'); | |
1842 break; | |
1843 | |
1844 default: | |
1845 break; | |
1846 } | |
1847 if (t->isimaginary()) | |
1848 buf->writeByte('i'); | |
1849 } | |
1850 } | |
1851 | |
1852 void RealExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
1853 { | |
1854 floatToBuffer(buf, type, value); | |
1855 } | |
1856 | |
1857 void realToMangleBuffer(OutBuffer *buf, real_t value) | |
1858 { | |
1859 /* Rely on %A to get portable mangling. | |
1860 * Must munge result to get only identifier characters. | |
1861 * | |
1862 * Possible values from %A => mangled result | |
1863 * NAN => NAN | |
1864 * -INF => NINF | |
1865 * INF => INF | |
1866 * -0X1.1BC18BA997B95P+79 => N11BC18BA997B95P79 | |
1867 * 0X1.9P+2 => 19P2 | |
1868 */ | |
1869 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1870 #if __APPLE__ |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1871 if (__inline_isnan(value)) |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1872 #else |
159 | 1873 if (isnan(value)) |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
1874 #endif |
159 | 1875 buf->writestring("NAN"); // no -NAN bugs |
1876 else | |
1877 { | |
1878 char buffer[32]; | |
1879 int n = sprintf(buffer, "%LA", value); | |
1880 assert(n > 0 && n < sizeof(buffer)); | |
1881 for (int i = 0; i < n; i++) | |
1882 { char c = buffer[i]; | |
1883 | |
1884 switch (c) | |
1885 { | |
1886 case '-': | |
1887 buf->writeByte('N'); | |
1888 break; | |
1889 | |
1890 case '+': | |
1891 case 'X': | |
1892 case '.': | |
1893 break; | |
1894 | |
1895 case '0': | |
1896 if (i < 2) | |
1897 break; // skip leading 0X | |
1898 default: | |
1899 buf->writeByte(c); | |
1900 break; | |
1901 } | |
1902 } | |
1903 } | |
1904 } | |
1905 | |
1906 void RealExp::toMangleBuffer(OutBuffer *buf) | |
1907 { | |
1908 buf->writeByte('e'); | |
1909 realToMangleBuffer(buf, value); | |
1910 } | |
1911 | |
1912 | |
1913 /******************************** ComplexExp **************************/ | |
1914 | |
1915 ComplexExp::ComplexExp(Loc loc, complex_t value, Type *type) | |
1916 : Expression(loc, TOKcomplex80, sizeof(ComplexExp)) | |
1917 { | |
1918 this->value = value; | |
1919 this->type = type; | |
1920 //printf("ComplexExp::ComplexExp(%s)\n", toChars()); | |
1921 } | |
1922 | |
1923 char *ComplexExp::toChars() | |
1924 { | |
1925 char buffer[sizeof(value) * 3 + 8 + 1]; | |
1926 | |
1927 #ifdef IN_GCC | |
1928 char buf1[sizeof(value) * 3 + 8 + 1]; | |
1929 char buf2[sizeof(value) * 3 + 8 + 1]; | |
1930 creall(value).format(buf1, sizeof(buf1)); | |
1931 cimagl(value).format(buf2, sizeof(buf2)); | |
1932 sprintf(buffer, "(%s+%si)", buf1, buf2); | |
1933 #else | |
1934 sprintf(buffer, "(%Lg+%Lgi)", creall(value), cimagl(value)); | |
1935 assert(strlen(buffer) < sizeof(buffer)); | |
1936 #endif | |
1937 return mem.strdup(buffer); | |
1938 } | |
1939 | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
1940 dinteger_t ComplexExp::toInteger() |
159 | 1941 { |
1942 #ifdef IN_GCC | |
1943 return (sinteger_t) toReal().toInt(); | |
1944 #else | |
1945 return (sinteger_t) toReal(); | |
1946 #endif | |
1947 } | |
1948 | |
1949 uinteger_t ComplexExp::toUInteger() | |
1950 { | |
1951 #ifdef IN_GCC | |
1952 return (uinteger_t) toReal().toInt(); | |
1953 #else | |
1954 return (uinteger_t) toReal(); | |
1955 #endif | |
1956 } | |
1957 | |
1958 real_t ComplexExp::toReal() | |
1959 { | |
1960 return creall(value); | |
1961 } | |
1962 | |
1963 real_t ComplexExp::toImaginary() | |
1964 { | |
1965 return cimagl(value); | |
1966 } | |
1967 | |
1968 complex_t ComplexExp::toComplex() | |
1969 { | |
1970 return value; | |
1971 } | |
1972 | |
1973 int ComplexExp::equals(Object *o) | |
1974 { ComplexExp *ne; | |
1975 | |
1976 if (this == o || | |
1977 (((Expression *)o)->op == TOKcomplex80 && | |
1978 ((ne = (ComplexExp *)o), type->equals(ne->type)) && | |
1979 RealEquals(creall(value), creall(ne->value)) && | |
1980 RealEquals(cimagl(value), cimagl(ne->value)) | |
1981 ) | |
1982 ) | |
1983 return 1; | |
1984 return 0; | |
1985 } | |
1986 | |
1987 Expression *ComplexExp::semantic(Scope *sc) | |
1988 { | |
1989 if (!type) | |
1990 type = Type::tcomplex80; | |
1991 else | |
1992 type = type->semantic(loc, sc); | |
1993 return this; | |
1994 } | |
1995 | |
1996 int ComplexExp::isBool(int result) | |
1997 { | |
1998 if (result) | |
1999 return (bool)(value); | |
2000 else | |
2001 return !value; | |
2002 } | |
2003 | |
2004 void ComplexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2005 { | |
2006 /* Print as: | |
2007 * (re+imi) | |
2008 */ | |
2009 #ifdef IN_GCC | |
2010 char buf1[sizeof(value) * 3 + 8 + 1]; | |
2011 char buf2[sizeof(value) * 3 + 8 + 1]; | |
2012 creall(value).format(buf1, sizeof(buf1)); | |
2013 cimagl(value).format(buf2, sizeof(buf2)); | |
2014 buf->printf("(%s+%si)", buf1, buf2); | |
2015 #else | |
2016 buf->writeByte('('); | |
2017 floatToBuffer(buf, type, creall(value)); | |
2018 buf->writeByte('+'); | |
2019 floatToBuffer(buf, type, cimagl(value)); | |
2020 buf->writestring("i)"); | |
2021 #endif | |
2022 } | |
2023 | |
2024 void ComplexExp::toMangleBuffer(OutBuffer *buf) | |
2025 { | |
2026 buf->writeByte('c'); | |
2027 real_t r = toReal(); | |
2028 realToMangleBuffer(buf, r); | |
2029 buf->writeByte('c'); // separate the two | |
2030 r = toImaginary(); | |
2031 realToMangleBuffer(buf, r); | |
2032 } | |
2033 | |
2034 /******************************** IdentifierExp **************************/ | |
2035 | |
2036 IdentifierExp::IdentifierExp(Loc loc, Identifier *ident) | |
2037 : Expression(loc, TOKidentifier, sizeof(IdentifierExp)) | |
2038 { | |
2039 this->ident = ident; | |
2040 } | |
2041 | |
2042 Expression *IdentifierExp::semantic(Scope *sc) | |
2043 { | |
2044 Dsymbol *s; | |
2045 Dsymbol *scopesym; | |
2046 | |
2047 #if LOGSEMANTIC | |
2048 printf("IdentifierExp::semantic('%s')\n", ident->toChars()); | |
2049 #endif | |
2050 s = sc->search(loc, ident, &scopesym); | |
2051 if (s) | |
2052 { Expression *e; | |
2053 WithScopeSymbol *withsym; | |
2054 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
2055 /* 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
|
2056 */ |
159 | 2057 withsym = scopesym->isWithScopeSymbol(); |
2058 if (withsym) | |
2059 { | |
2060 s = s->toAlias(); | |
2061 | |
2062 // Same as wthis.ident | |
2063 if (s->needThis() || s->isTemplateDeclaration()) | |
2064 { | |
2065 e = new VarExp(loc, withsym->withstate->wthis); | |
2066 e = new DotIdExp(loc, e, ident); | |
2067 } | |
2068 else | |
2069 { Type *t = withsym->withstate->wthis->type; | |
2070 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
|
2071 t = ((TypePointer *)t)->next; |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
2072 e = typeDotIdExp(loc, t, ident); |
159 | 2073 } |
2074 } | |
2075 else | |
2076 { | |
2077 if (!s->parent && scopesym->isArrayScopeSymbol()) | |
2078 { // Kludge to run semantic() here because | |
2079 // ArrayScopeSymbol::search() doesn't have access to sc. | |
2080 s->semantic(sc); | |
2081 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2082 /* If f is really a function template, |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2083 * 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
|
2084 */ |
159 | 2085 FuncDeclaration *f = s->isFuncDeclaration(); |
2086 if (f && f->parent) | |
2087 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
2088 | |
2089 if (ti && | |
2090 !ti->isTemplateMixin() && | |
2091 (ti->name == f->ident || | |
2092 ti->toAlias()->ident == f->ident) | |
2093 && | |
2094 ti->tempdecl && ti->tempdecl->onemember) | |
2095 { | |
2096 TemplateDeclaration *tempdecl = ti->tempdecl; | |
2097 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
2098 tempdecl = tempdecl->overroot; // then get the start | |
2099 e = new TemplateExp(loc, tempdecl); | |
2100 e = e->semantic(sc); | |
2101 return e; | |
2102 } | |
2103 } | |
2104 e = new DsymbolExp(loc, s); | |
2105 } | |
2106 return e->semantic(sc); | |
2107 } | |
2108 error("undefined identifier %s", ident->toChars()); | |
2109 type = Type::terror; | |
2110 return this; | |
2111 } | |
2112 | |
2113 char *IdentifierExp::toChars() | |
2114 { | |
2115 return ident->toChars(); | |
2116 } | |
2117 | |
2118 void IdentifierExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2119 { | |
2120 if (hgs->hdrgen) | |
2121 buf->writestring(ident->toHChars2()); | |
2122 else | |
2123 buf->writestring(ident->toChars()); | |
2124 } | |
2125 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2126 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2127 int IdentifierExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2128 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2129 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2130 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2131 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2132 |
159 | 2133 Expression *IdentifierExp::toLvalue(Scope *sc, Expression *e) |
2134 { | |
2135 #if 0 | |
2136 tym = tybasic(e1->ET->Tty); | |
2137 if (!(tyscalar(tym) || | |
2138 tym == TYstruct || | |
2139 tym == TYarray && e->Eoper == TOKaddr)) | |
2140 synerr(EM_lvalue); // lvalue expected | |
2141 #endif | |
2142 return this; | |
2143 } | |
2144 | |
2145 /******************************** DollarExp **************************/ | |
2146 | |
2147 DollarExp::DollarExp(Loc loc) | |
2148 : IdentifierExp(loc, Id::dollar) | |
2149 { | |
2150 } | |
2151 | |
2152 /******************************** DsymbolExp **************************/ | |
2153 | |
2154 DsymbolExp::DsymbolExp(Loc loc, Dsymbol *s) | |
2155 : Expression(loc, TOKdsymbol, sizeof(DsymbolExp)) | |
2156 { | |
2157 this->s = s; | |
2158 } | |
2159 | |
2160 Expression *DsymbolExp::semantic(Scope *sc) | |
2161 { | |
2162 #if LOGSEMANTIC | |
2163 printf("DsymbolExp::semantic('%s')\n", s->toChars()); | |
2164 #endif | |
2165 | |
2166 Lagain: | |
2167 EnumMember *em; | |
2168 Expression *e; | |
2169 VarDeclaration *v; | |
2170 FuncDeclaration *f; | |
2171 FuncLiteralDeclaration *fld; | |
2172 Declaration *d; | |
2173 ClassDeclaration *cd; | |
2174 ClassDeclaration *thiscd = NULL; | |
2175 Import *imp; | |
2176 Package *pkg; | |
2177 Type *t; | |
2178 | |
2179 //printf("DsymbolExp:: %p '%s' is a symbol\n", this, toChars()); | |
2180 //printf("s = '%s', s->kind = '%s'\n", s->toChars(), s->kind()); | |
2181 if (type) | |
2182 return this; | |
2183 if (!s->isFuncDeclaration()) // functions are checked after overloading | |
2184 checkDeprecated(sc, s); | |
2185 s = s->toAlias(); | |
2186 //printf("s = '%s', s->kind = '%s', s->needThis() = %p\n", s->toChars(), s->kind(), s->needThis()); | |
2187 if (!s->isFuncDeclaration()) | |
2188 checkDeprecated(sc, s); | |
2189 | |
2190 if (sc->func) | |
2191 thiscd = sc->func->parent->isClassDeclaration(); | |
2192 | |
2193 // BUG: This should happen after overload resolution for functions, not before | |
2194 if (s->needThis()) | |
2195 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2196 if (hasThis(sc) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2197 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2198 && !s->isFuncDeclaration() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2199 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2200 ) |
159 | 2201 { |
2202 // Supply an implicit 'this', as in | |
2203 // this.ident | |
2204 | |
2205 DotVarExp *de; | |
2206 | |
2207 de = new DotVarExp(loc, new ThisExp(loc), s->isDeclaration()); | |
2208 return de->semantic(sc); | |
2209 } | |
2210 } | |
2211 | |
2212 em = s->isEnumMember(); | |
2213 if (em) | |
2214 { | |
2215 e = em->value->copy(); | |
2216 e = e->semantic(sc); | |
2217 return e; | |
2218 } | |
2219 v = s->isVarDeclaration(); | |
2220 if (v) | |
2221 { | |
2222 //printf("Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
2223 if (!type) | |
2224 { type = v->type; | |
2225 if (!v->type) | |
2226 { error("forward reference of %s", v->toChars()); | |
2227 type = Type::terror; | |
2228 } | |
2229 } | |
1499
df11cdec45a2
Another shot at fixing the issues with (constant) struct literals and their addresses. See DMD2682, #218, #324.
Christian Kamm <kamm incasoftware de>
parents:
1372
diff
changeset
|
2230 if (v->isSameAsInitializer() && type->toBasetype()->ty != Tsarray) |
159 | 2231 { |
2232 if (v->init) | |
2233 { | |
2234 if (v->inuse) | |
2235 { | |
2236 error("circular reference to '%s'", v->toChars()); | |
2237 type = Type::tint32; | |
2238 return this; | |
2239 } | |
2240 ExpInitializer *ei = v->init->isExpInitializer(); | |
2241 if (ei) | |
2242 { | |
2243 e = ei->exp->copy(); // make copy so we can change loc | |
2244 if (e->op == TOKstring || !e->type) | |
2245 e = e->semantic(sc); | |
2246 e = e->implicitCastTo(sc, type); | |
2247 e->loc = loc; | |
2248 return e; | |
2249 } | |
2250 } | |
2251 else | |
2252 { | |
2253 e = type->defaultInit(); | |
2254 e->loc = loc; | |
2255 return e; | |
2256 } | |
2257 } | |
2258 e = new VarExp(loc, v); | |
2259 e->type = type; | |
2260 e = e->semantic(sc); | |
2261 return e->deref(); | |
2262 } | |
2263 fld = s->isFuncLiteralDeclaration(); | |
2264 if (fld) | |
2265 { //printf("'%s' is a function literal\n", fld->toChars()); | |
2266 e = new FuncExp(loc, fld); | |
2267 return e->semantic(sc); | |
2268 } | |
2269 f = s->isFuncDeclaration(); | |
2270 if (f) | |
2271 { //printf("'%s' is a function\n", f->toChars()); | |
1587 | 2272 if (!f->type->deco) |
2273 { | |
2274 error("forward reference to %s", toChars()); | |
1612
081c48283153
Merge DMD r278: bugzilla 370 Compiler stack overflow on recursive...
Leandro Lucarella <llucax@gmail.com>
parents:
1611
diff
changeset
|
2275 return new ErrorExp(); |
1587 | 2276 } |
159 | 2277 return new VarExp(loc, f); |
2278 } | |
2279 cd = s->isClassDeclaration(); | |
2280 if (cd && thiscd && cd->isBaseOf(thiscd, NULL) && sc->func->needThis()) | |
2281 { | |
2282 // We need to add an implicit 'this' if cd is this class or a base class. | |
2283 DotTypeExp *dte; | |
2284 | |
2285 dte = new DotTypeExp(loc, new ThisExp(loc), s); | |
2286 return dte->semantic(sc); | |
2287 } | |
2288 imp = s->isImport(); | |
2289 if (imp) | |
2290 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2291 if (!imp->pkg) |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2292 { error("forward reference of import %s", imp->toChars()); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2293 return this; |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2294 } |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
2295 ScopeExp *ie = new ScopeExp(loc, imp->pkg); |
159 | 2296 return ie->semantic(sc); |
2297 } | |
2298 pkg = s->isPackage(); | |
2299 if (pkg) | |
2300 { | |
2301 ScopeExp *ie; | |
2302 | |
2303 ie = new ScopeExp(loc, pkg); | |
2304 return ie->semantic(sc); | |
2305 } | |
2306 Module *mod = s->isModule(); | |
2307 if (mod) | |
2308 { | |
2309 ScopeExp *ie; | |
2310 | |
2311 ie = new ScopeExp(loc, mod); | |
2312 return ie->semantic(sc); | |
2313 } | |
2314 | |
2315 t = s->getType(); | |
2316 if (t) | |
2317 { | |
2318 return new TypeExp(loc, t); | |
2319 } | |
2320 | |
2321 TupleDeclaration *tup = s->isTupleDeclaration(); | |
2322 if (tup) | |
2323 { | |
2324 e = new TupleExp(loc, tup); | |
2325 e = e->semantic(sc); | |
2326 return e; | |
2327 } | |
2328 | |
2329 TemplateInstance *ti = s->isTemplateInstance(); | |
2330 if (ti && !global.errors) | |
1587 | 2331 { if (!ti->semanticRun) |
159 | 2332 ti->semantic(sc); |
2333 s = ti->inst->toAlias(); | |
2334 if (!s->isTemplateInstance()) | |
2335 goto Lagain; | |
2336 e = new ScopeExp(loc, ti); | |
2337 e = e->semantic(sc); | |
2338 return e; | |
2339 } | |
2340 | |
2341 TemplateDeclaration *td = s->isTemplateDeclaration(); | |
2342 if (td) | |
2343 { | |
2344 e = new TemplateExp(loc, td); | |
2345 e = e->semantic(sc); | |
2346 return e; | |
2347 } | |
2348 | |
2349 Lerr: | |
2350 error("%s '%s' is not a variable", s->kind(), s->toChars()); | |
2351 type = Type::terror; | |
2352 return this; | |
2353 } | |
2354 | |
2355 char *DsymbolExp::toChars() | |
2356 { | |
2357 return s->toChars(); | |
2358 } | |
2359 | |
2360 void DsymbolExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2361 { | |
2362 buf->writestring(s->toChars()); | |
2363 } | |
2364 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2365 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2366 int DsymbolExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2367 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2368 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2369 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2370 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2371 |
159 | 2372 Expression *DsymbolExp::toLvalue(Scope *sc, Expression *e) |
2373 { | |
2374 #if 0 | |
2375 tym = tybasic(e1->ET->Tty); | |
2376 if (!(tyscalar(tym) || | |
2377 tym == TYstruct || | |
2378 tym == TYarray && e->Eoper == TOKaddr)) | |
2379 synerr(EM_lvalue); // lvalue expected | |
2380 #endif | |
2381 return this; | |
2382 } | |
2383 | |
2384 /******************************** ThisExp **************************/ | |
2385 | |
2386 ThisExp::ThisExp(Loc loc) | |
2387 : Expression(loc, TOKthis, sizeof(ThisExp)) | |
2388 { | |
2389 var = NULL; | |
2390 } | |
2391 | |
2392 Expression *ThisExp::semantic(Scope *sc) | |
2393 { FuncDeclaration *fd; | |
2394 FuncDeclaration *fdthis; | |
2395 int nested = 0; | |
2396 | |
2397 #if LOGSEMANTIC | |
2398 printf("ThisExp::semantic()\n"); | |
2399 #endif | |
2400 if (type) | |
2401 { //assert(global.errors || var); | |
2402 return this; | |
2403 } | |
2404 | |
2405 /* Special case for typeof(this) and typeof(super) since both | |
2406 * should work even if they are not inside a non-static member function | |
2407 */ | |
2408 if (sc->intypeof) | |
2409 { | |
2410 // Find enclosing struct or class | |
2411 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2412 { | |
2413 ClassDeclaration *cd; | |
2414 StructDeclaration *sd; | |
2415 | |
2416 if (!s) | |
2417 { | |
2418 error("%s is not in a struct or class scope", toChars()); | |
2419 goto Lerr; | |
2420 } | |
2421 cd = s->isClassDeclaration(); | |
2422 if (cd) | |
2423 { | |
2424 type = cd->type; | |
2425 return this; | |
2426 } | |
2427 sd = s->isStructDeclaration(); | |
2428 if (sd) | |
2429 { | |
2430 type = sd->type->pointerTo(); | |
2431 return this; | |
2432 } | |
2433 } | |
2434 } | |
2435 | |
2436 fdthis = sc->parent->isFuncDeclaration(); | |
2437 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
2438 if (!fd) | |
2439 goto Lerr; | |
2440 | |
2441 assert(fd->vthis); | |
2442 var = fd->vthis; | |
2443 assert(var->parent); | |
2444 type = var->type; | |
2445 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2446 #if 0 | |
2447 if (fd != fdthis) // if nested | |
2448 { | |
2449 fdthis->getLevel(loc, fd); | |
2450 fd->vthis->nestedref = 1; | |
2451 fd->nestedFrameRef = 1; | |
2452 } | |
2453 #endif | |
336 | 2454 if (!sc->intypeof) |
2455 sc->callSuper |= CSXthis; | |
159 | 2456 return this; |
2457 | |
2458 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
|
2459 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
|
2460 type = Type::terror; |
159 | 2461 return this; |
2462 } | |
2463 | |
2464 int ThisExp::isBool(int result) | |
2465 { | |
2466 return result ? TRUE : FALSE; | |
2467 } | |
2468 | |
2469 void ThisExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2470 { | |
2471 buf->writestring("this"); | |
2472 } | |
2473 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2474 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2475 int ThisExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2476 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2477 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2478 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2479 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2480 |
159 | 2481 Expression *ThisExp::toLvalue(Scope *sc, Expression *e) |
2482 { | |
2483 return this; | |
2484 } | |
2485 | |
2486 /******************************** SuperExp **************************/ | |
2487 | |
2488 SuperExp::SuperExp(Loc loc) | |
2489 : ThisExp(loc) | |
2490 { | |
2491 op = TOKsuper; | |
2492 } | |
2493 | |
2494 Expression *SuperExp::semantic(Scope *sc) | |
2495 { FuncDeclaration *fd; | |
2496 FuncDeclaration *fdthis; | |
2497 ClassDeclaration *cd; | |
2498 Dsymbol *s; | |
2499 | |
2500 #if LOGSEMANTIC | |
2501 printf("SuperExp::semantic('%s')\n", toChars()); | |
2502 #endif | |
2503 if (type) | |
2504 return this; | |
2505 | |
2506 /* Special case for typeof(this) and typeof(super) since both | |
2507 * should work even if they are not inside a non-static member function | |
2508 */ | |
2509 if (sc->intypeof) | |
2510 { | |
2511 // Find enclosing class | |
2512 for (Dsymbol *s = sc->parent; 1; s = s->parent) | |
2513 { | |
2514 ClassDeclaration *cd; | |
2515 | |
2516 if (!s) | |
2517 { | |
2518 error("%s is not in a class scope", toChars()); | |
2519 goto Lerr; | |
2520 } | |
2521 cd = s->isClassDeclaration(); | |
2522 if (cd) | |
2523 { | |
2524 cd = cd->baseClass; | |
2525 if (!cd) | |
2526 { error("class %s has no 'super'", s->toChars()); | |
2527 goto Lerr; | |
2528 } | |
2529 type = cd->type; | |
2530 return this; | |
2531 } | |
2532 } | |
2533 } | |
2534 | |
2535 fdthis = sc->parent->isFuncDeclaration(); | |
2536 fd = hasThis(sc); | |
2537 if (!fd) | |
2538 goto Lerr; | |
2539 assert(fd->vthis); | |
2540 var = fd->vthis; | |
2541 assert(var->parent); | |
2542 | |
2543 s = fd->toParent(); | |
2544 while (s && s->isTemplateInstance()) | |
2545 s = s->toParent(); | |
2546 assert(s); | |
2547 cd = s->isClassDeclaration(); | |
2548 //printf("parent is %s %s\n", fd->toParent()->kind(), fd->toParent()->toChars()); | |
2549 if (!cd) | |
2550 goto Lerr; | |
2551 if (!cd->baseClass) | |
2552 { | |
2553 error("no base class for %s", cd->toChars()); | |
2554 type = fd->vthis->type; | |
2555 } | |
2556 else | |
2557 { | |
2558 type = cd->baseClass->type; | |
2559 } | |
2560 | |
2561 var->isVarDeclaration()->checkNestedReference(sc, loc); | |
2562 #if 0 | |
2563 if (fd != fdthis) | |
2564 { | |
2565 fdthis->getLevel(loc, fd); | |
2566 fd->vthis->nestedref = 1; | |
2567 fd->nestedFrameRef = 1; | |
2568 } | |
2569 #endif | |
2570 | |
336 | 2571 if (!sc->intypeof) |
2572 sc->callSuper |= CSXsuper; | |
159 | 2573 return this; |
2574 | |
2575 | |
2576 Lerr: | |
2577 error("'super' is only allowed in non-static class member functions"); | |
2578 type = Type::tint32; | |
2579 return this; | |
2580 } | |
2581 | |
2582 void SuperExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2583 { | |
2584 buf->writestring("super"); | |
2585 } | |
2586 | |
2587 | |
2588 /******************************** NullExp **************************/ | |
2589 | |
2590 NullExp::NullExp(Loc loc) | |
2591 : Expression(loc, TOKnull, sizeof(NullExp)) | |
2592 { | |
2593 committed = 0; | |
2594 } | |
2595 | |
2596 Expression *NullExp::semantic(Scope *sc) | |
2597 { | |
2598 #if LOGSEMANTIC | |
2599 printf("NullExp::semantic('%s')\n", toChars()); | |
2600 #endif | |
2601 // NULL is the same as (void *)0 | |
2602 if (!type) | |
2603 type = Type::tvoid->pointerTo(); | |
2604 return this; | |
2605 } | |
2606 | |
2607 int NullExp::isBool(int result) | |
2608 { | |
2609 return result ? FALSE : TRUE; | |
2610 } | |
2611 | |
2612 void NullExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2613 { | |
2614 buf->writestring("null"); | |
2615 } | |
2616 | |
2617 void NullExp::toMangleBuffer(OutBuffer *buf) | |
2618 { | |
2619 buf->writeByte('n'); | |
2620 } | |
2621 | |
2622 /******************************** StringExp **************************/ | |
2623 | |
2624 StringExp::StringExp(Loc loc, char *string) | |
2625 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2626 { | |
2627 this->string = string; | |
2628 this->len = strlen(string); | |
2629 this->sz = 1; | |
2630 this->committed = 0; | |
2631 this->postfix = 0; | |
2632 } | |
2633 | |
2634 StringExp::StringExp(Loc loc, void *string, size_t len) | |
2635 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2636 { | |
2637 this->string = string; | |
2638 this->len = len; | |
2639 this->sz = 1; | |
2640 this->committed = 0; | |
2641 this->postfix = 0; | |
2642 } | |
2643 | |
2644 StringExp::StringExp(Loc loc, void *string, size_t len, unsigned char postfix) | |
2645 : Expression(loc, TOKstring, sizeof(StringExp)) | |
2646 { | |
2647 this->string = string; | |
2648 this->len = len; | |
2649 this->sz = 1; | |
2650 this->committed = 0; | |
2651 this->postfix = postfix; | |
2652 } | |
2653 | |
2654 #if 0 | |
2655 Expression *StringExp::syntaxCopy() | |
2656 { | |
2657 printf("StringExp::syntaxCopy() %s\n", toChars()); | |
2658 return copy(); | |
2659 } | |
2660 #endif | |
2661 | |
2662 int StringExp::equals(Object *o) | |
2663 { | |
2664 //printf("StringExp::equals('%s')\n", o->toChars()); | |
2665 if (o && o->dyncast() == DYNCAST_EXPRESSION) | |
2666 { Expression *e = (Expression *)o; | |
2667 | |
2668 if (e->op == TOKstring) | |
2669 { | |
2670 return compare(o) == 0; | |
2671 } | |
2672 } | |
2673 return FALSE; | |
2674 } | |
2675 | |
2676 char *StringExp::toChars() | |
2677 { | |
2678 OutBuffer buf; | |
2679 HdrGenState hgs; | |
2680 char *p; | |
2681 | |
2682 memset(&hgs, 0, sizeof(hgs)); | |
2683 toCBuffer(&buf, &hgs); | |
2684 buf.writeByte(0); | |
2685 p = (char *)buf.data; | |
2686 buf.data = NULL; | |
2687 return p; | |
2688 } | |
2689 | |
2690 Expression *StringExp::semantic(Scope *sc) | |
2691 { | |
2692 #if LOGSEMANTIC | |
2693 printf("StringExp::semantic() %s\n", toChars()); | |
2694 #endif | |
2695 if (!type) | |
2696 { OutBuffer buffer; | |
2697 size_t newlen = 0; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2698 const char *p; |
159 | 2699 size_t u; |
2700 unsigned c; | |
2701 | |
2702 switch (postfix) | |
2703 { | |
2704 case 'd': | |
2705 for (u = 0; u < len;) | |
2706 { | |
2707 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2708 if (p) | |
2709 { error("%s", p); | |
2710 break; | |
2711 } | |
2712 else | |
2713 { buffer.write4(c); | |
2714 newlen++; | |
2715 } | |
2716 } | |
2717 buffer.write4(0); | |
2718 string = buffer.extractData(); | |
2719 len = newlen; | |
2720 sz = 4; | |
2721 type = new TypeSArray(Type::tdchar, new IntegerExp(loc, len, Type::tindex)); | |
2722 committed = 1; | |
2723 break; | |
2724 | |
2725 case 'w': | |
2726 for (u = 0; u < len;) | |
2727 { | |
2728 p = utf_decodeChar((unsigned char *)string, len, &u, &c); | |
2729 if (p) | |
2730 { error("%s", p); | |
2731 break; | |
2732 } | |
2733 else | |
2734 { buffer.writeUTF16(c); | |
2735 newlen++; | |
2736 if (c >= 0x10000) | |
2737 newlen++; | |
2738 } | |
2739 } | |
2740 buffer.writeUTF16(0); | |
2741 string = buffer.extractData(); | |
2742 len = newlen; | |
2743 sz = 2; | |
2744 type = new TypeSArray(Type::twchar, new IntegerExp(loc, len, Type::tindex)); | |
2745 committed = 1; | |
2746 break; | |
2747 | |
2748 case 'c': | |
2749 committed = 1; | |
2750 default: | |
2751 type = new TypeSArray(Type::tchar, new IntegerExp(loc, len, Type::tindex)); | |
2752 break; | |
2753 } | |
2754 type = type->semantic(loc, sc); | |
2755 } | |
2756 return this; | |
2757 } | |
2758 | |
2759 /**************************************** | |
2760 * Convert string to char[]. | |
2761 */ | |
2762 | |
2763 StringExp *StringExp::toUTF8(Scope *sc) | |
2764 { | |
2765 if (sz != 1) | |
2766 { // Convert to UTF-8 string | |
2767 committed = 0; | |
2768 Expression *e = castTo(sc, Type::tchar->arrayOf()); | |
2769 e = e->optimize(WANTvalue); | |
2770 assert(e->op == TOKstring); | |
2771 StringExp *se = (StringExp *)e; | |
2772 assert(se->sz == 1); | |
2773 return se; | |
2774 } | |
2775 return this; | |
2776 } | |
2777 | |
2778 int StringExp::compare(Object *obj) | |
2779 { | |
2780 // Used to sort case statement expressions so we can do an efficient lookup | |
2781 StringExp *se2 = (StringExp *)(obj); | |
2782 | |
2783 // This is a kludge so isExpression() in template.c will return 5 | |
2784 // for StringExp's. | |
2785 if (!se2) | |
2786 return 5; | |
2787 | |
2788 assert(se2->op == TOKstring); | |
2789 | |
2790 int len1 = len; | |
2791 int len2 = se2->len; | |
2792 | |
2793 if (len1 == len2) | |
2794 { | |
2795 switch (sz) | |
2796 { | |
2797 case 1: | |
2798 return strcmp((char *)string, (char *)se2->string); | |
2799 | |
2800 case 2: | |
2801 { unsigned u; | |
2802 d_wchar *s1 = (d_wchar *)string; | |
2803 d_wchar *s2 = (d_wchar *)se2->string; | |
2804 | |
2805 for (u = 0; u < len; u++) | |
2806 { | |
2807 if (s1[u] != s2[u]) | |
2808 return s1[u] - s2[u]; | |
2809 } | |
2810 } | |
2811 | |
2812 case 4: | |
2813 { unsigned u; | |
2814 d_dchar *s1 = (d_dchar *)string; | |
2815 d_dchar *s2 = (d_dchar *)se2->string; | |
2816 | |
2817 for (u = 0; u < len; u++) | |
2818 { | |
2819 if (s1[u] != s2[u]) | |
2820 return s1[u] - s2[u]; | |
2821 } | |
2822 } | |
2823 break; | |
2824 | |
2825 default: | |
2826 assert(0); | |
2827 } | |
2828 } | |
2829 return len1 - len2; | |
2830 } | |
2831 | |
2832 int StringExp::isBool(int result) | |
2833 { | |
2834 return result ? TRUE : FALSE; | |
2835 } | |
2836 | |
2837 unsigned StringExp::charAt(size_t i) | |
2838 { unsigned value; | |
2839 | |
2840 switch (sz) | |
2841 { | |
2842 case 1: | |
2843 value = ((unsigned char *)string)[i]; | |
2844 break; | |
2845 | |
2846 case 2: | |
2847 value = ((unsigned short *)string)[i]; | |
2848 break; | |
2849 | |
2850 case 4: | |
2851 value = ((unsigned int *)string)[i]; | |
2852 break; | |
2853 | |
2854 default: | |
2855 assert(0); | |
2856 break; | |
2857 } | |
2858 return value; | |
2859 } | |
2860 | |
2861 void StringExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
2862 { | |
2863 buf->writeByte('"'); | |
2864 for (size_t i = 0; i < len; i++) | |
2865 { unsigned c = charAt(i); | |
2866 | |
2867 switch (c) | |
2868 { | |
2869 case '"': | |
2870 case '\\': | |
2871 if (!hgs->console) | |
2872 buf->writeByte('\\'); | |
2873 default: | |
2874 if (c <= 0xFF) | |
2875 { if (c <= 0x7F && (isprint(c) || hgs->console)) | |
2876 buf->writeByte(c); | |
2877 else | |
2878 buf->printf("\\x%02x", c); | |
2879 } | |
2880 else if (c <= 0xFFFF) | |
2881 buf->printf("\\x%02x\\x%02x", c & 0xFF, c >> 8); | |
2882 else | |
2883 buf->printf("\\x%02x\\x%02x\\x%02x\\x%02x", | |
2884 c & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, c >> 24); | |
2885 break; | |
2886 } | |
2887 } | |
2888 buf->writeByte('"'); | |
2889 if (postfix) | |
2890 buf->writeByte(postfix); | |
2891 } | |
2892 | |
2893 void StringExp::toMangleBuffer(OutBuffer *buf) | |
2894 { char m; | |
2895 OutBuffer tmp; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
2896 const char *p; |
159 | 2897 unsigned c; |
2898 size_t u; | |
2899 unsigned char *q; | |
2900 unsigned qlen; | |
2901 | |
2902 /* Write string in UTF-8 format | |
2903 */ | |
2904 switch (sz) | |
2905 { case 1: | |
2906 m = 'a'; | |
2907 q = (unsigned char *)string; | |
2908 qlen = len; | |
2909 break; | |
2910 case 2: | |
2911 m = 'w'; | |
2912 for (u = 0; u < len; ) | |
2913 { | |
2914 p = utf_decodeWchar((unsigned short *)string, len, &u, &c); | |
2915 if (p) | |
2916 error("%s", p); | |
2917 else | |
2918 tmp.writeUTF8(c); | |
2919 } | |
2920 q = tmp.data; | |
2921 qlen = tmp.offset; | |
2922 break; | |
2923 case 4: | |
2924 m = 'd'; | |
2925 for (u = 0; u < len; u++) | |
2926 { | |
2927 c = ((unsigned *)string)[u]; | |
2928 if (!utf_isValidDchar(c)) | |
2929 error("invalid UCS-32 char \\U%08x", c); | |
2930 else | |
2931 tmp.writeUTF8(c); | |
2932 } | |
2933 q = tmp.data; | |
2934 qlen = tmp.offset; | |
2935 break; | |
2936 default: | |
2937 assert(0); | |
2938 } | |
2939 buf->writeByte(m); | |
2940 buf->printf("%d_", qlen); | |
2941 for (size_t i = 0; i < qlen; i++) | |
2942 buf->printf("%02x", q[i]); | |
2943 } | |
2944 | |
2945 /************************ ArrayLiteralExp ************************************/ | |
2946 | |
2947 // [ e1, e2, e3, ... ] | |
2948 | |
2949 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expressions *elements) | |
2950 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2951 { | |
2952 this->elements = elements; | |
2953 } | |
2954 | |
2955 ArrayLiteralExp::ArrayLiteralExp(Loc loc, Expression *e) | |
2956 : Expression(loc, TOKarrayliteral, sizeof(ArrayLiteralExp)) | |
2957 { | |
2958 elements = new Expressions; | |
2959 elements->push(e); | |
2960 } | |
2961 | |
2962 Expression *ArrayLiteralExp::syntaxCopy() | |
2963 { | |
2964 return new ArrayLiteralExp(loc, arraySyntaxCopy(elements)); | |
2965 } | |
2966 | |
2967 Expression *ArrayLiteralExp::semantic(Scope *sc) | |
2968 { Expression *e; | |
2969 Type *t0 = NULL; | |
2970 | |
2971 #if LOGSEMANTIC | |
2972 printf("ArrayLiteralExp::semantic('%s')\n", toChars()); | |
2973 #endif | |
2974 if (type) | |
2975 return this; | |
2976 | |
2977 // Run semantic() on each element | |
2978 for (int i = 0; i < elements->dim; i++) | |
2979 { e = (Expression *)elements->data[i]; | |
2980 e = e->semantic(sc); | |
2981 elements->data[i] = (void *)e; | |
2982 } | |
2983 expandTuples(elements); | |
2984 for (int i = 0; i < elements->dim; i++) | |
2985 { e = (Expression *)elements->data[i]; | |
2986 | |
2987 if (!e->type) | |
2988 error("%s has no value", e->toChars()); | |
2989 e = resolveProperties(sc, e); | |
2990 | |
2991 unsigned char committed = 1; | |
2992 if (e->op == TOKstring) | |
2993 committed = ((StringExp *)e)->committed; | |
2994 | |
2995 if (!t0) | |
2996 { t0 = e->type; | |
2997 // Convert any static arrays to dynamic arrays | |
2998 if (t0->ty == Tsarray) | |
2999 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3000 t0 = ((TypeSArray *)t0)->next->arrayOf(); |
159 | 3001 e = e->implicitCastTo(sc, t0); |
3002 } | |
3003 } | |
3004 else | |
3005 e = e->implicitCastTo(sc, t0); | |
3006 if (!committed && e->op == TOKstring) | |
3007 { StringExp *se = (StringExp *)e; | |
3008 se->committed = 0; | |
3009 } | |
3010 elements->data[i] = (void *)e; | |
3011 } | |
3012 | |
3013 if (!t0) | |
3014 t0 = Type::tvoid; | |
3015 type = new TypeSArray(t0, new IntegerExp(elements->dim)); | |
3016 type = type->semantic(loc, sc); | |
3017 return this; | |
3018 } | |
3019 | |
3020 int ArrayLiteralExp::checkSideEffect(int flag) | |
3021 { int f = 0; | |
3022 | |
3023 for (size_t i = 0; i < elements->dim; i++) | |
3024 { Expression *e = (Expression *)elements->data[i]; | |
3025 | |
3026 f |= e->checkSideEffect(2); | |
3027 } | |
3028 if (flag == 0 && f == 0) | |
3029 Expression::checkSideEffect(0); | |
3030 return f; | |
3031 } | |
3032 | |
3033 int ArrayLiteralExp::isBool(int result) | |
3034 { | |
3035 size_t dim = elements ? elements->dim : 0; | |
3036 return result ? (dim != 0) : (dim == 0); | |
3037 } | |
3038 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3039 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3040 int ArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3041 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3042 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
|
3043 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3044 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3045 |
159 | 3046 void ArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3047 { | |
3048 buf->writeByte('['); | |
3049 argsToCBuffer(buf, elements, hgs); | |
3050 buf->writeByte(']'); | |
3051 } | |
3052 | |
3053 void ArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3054 { | |
3055 size_t dim = elements ? elements->dim : 0; | |
1372
229e02867307
Fix format-string bugs by adding __attribute__((__format__)) in all applicable
Frits van Bommel <fvbommel wxs.nl>
parents:
1367
diff
changeset
|
3056 buf->printf("A%zu", dim); |
159 | 3057 for (size_t i = 0; i < dim; i++) |
3058 { Expression *e = (Expression *)elements->data[i]; | |
3059 e->toMangleBuffer(buf); | |
3060 } | |
3061 } | |
3062 | |
3063 /************************ AssocArrayLiteralExp ************************************/ | |
3064 | |
3065 // [ key0 : value0, key1 : value1, ... ] | |
3066 | |
3067 AssocArrayLiteralExp::AssocArrayLiteralExp(Loc loc, | |
3068 Expressions *keys, Expressions *values) | |
3069 : Expression(loc, TOKassocarrayliteral, sizeof(AssocArrayLiteralExp)) | |
3070 { | |
3071 assert(keys->dim == values->dim); | |
3072 this->keys = keys; | |
3073 this->values = values; | |
3074 } | |
3075 | |
3076 Expression *AssocArrayLiteralExp::syntaxCopy() | |
3077 { | |
3078 return new AssocArrayLiteralExp(loc, | |
3079 arraySyntaxCopy(keys), arraySyntaxCopy(values)); | |
3080 } | |
3081 | |
3082 Expression *AssocArrayLiteralExp::semantic(Scope *sc) | |
3083 { Expression *e; | |
3084 | |
3085 #if LOGSEMANTIC | |
3086 printf("AssocArrayLiteralExp::semantic('%s')\n", toChars()); | |
3087 #endif | |
3088 | |
3089 // Run semantic() on each element | |
1604
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3090 arrayExpressionSemantic(keys, sc); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3091 arrayExpressionSemantic(values, sc); |
159 | 3092 expandTuples(keys); |
3093 expandTuples(values); | |
3094 if (keys->dim != values->dim) | |
3095 { | |
3096 error("number of keys is %u, must match number of values %u", keys->dim, values->dim); | |
3097 keys->setDim(0); | |
3098 values->setDim(0); | |
3099 } | |
1604
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3100 |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3101 Type *tkey = NULL; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3102 Type *tvalue = NULL; |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3103 keys = arrayExpressionToCommonType(sc, keys, &tkey); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3104 values = arrayExpressionToCommonType(sc, values, &tvalue); |
2afcaab30a6a
Merge DMD r250: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1587
diff
changeset
|
3105 |
159 | 3106 type = new TypeAArray(tvalue, tkey); |
3107 type = type->semantic(loc, sc); | |
3108 return this; | |
3109 } | |
3110 | |
3111 int AssocArrayLiteralExp::checkSideEffect(int flag) | |
3112 { int f = 0; | |
3113 | |
3114 for (size_t i = 0; i < keys->dim; i++) | |
3115 { Expression *key = (Expression *)keys->data[i]; | |
3116 Expression *value = (Expression *)values->data[i]; | |
3117 | |
3118 f |= key->checkSideEffect(2); | |
3119 f |= value->checkSideEffect(2); | |
3120 } | |
3121 if (flag == 0 && f == 0) | |
3122 Expression::checkSideEffect(0); | |
3123 return f; | |
3124 } | |
3125 | |
3126 int AssocArrayLiteralExp::isBool(int result) | |
3127 { | |
3128 size_t dim = keys->dim; | |
3129 return result ? (dim != 0) : (dim == 0); | |
3130 } | |
3131 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3132 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3133 int AssocArrayLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3134 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3135 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3136 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3137 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3138 |
159 | 3139 void AssocArrayLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3140 { | |
3141 buf->writeByte('['); | |
3142 for (size_t i = 0; i < keys->dim; i++) | |
3143 { Expression *key = (Expression *)keys->data[i]; | |
3144 Expression *value = (Expression *)values->data[i]; | |
3145 | |
3146 if (i) | |
3147 buf->writeByte(','); | |
3148 expToCBuffer(buf, hgs, key, PREC_assign); | |
3149 buf->writeByte(':'); | |
3150 expToCBuffer(buf, hgs, value, PREC_assign); | |
3151 } | |
3152 buf->writeByte(']'); | |
3153 } | |
3154 | |
3155 void AssocArrayLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3156 { | |
3157 size_t dim = keys->dim; | |
1372
229e02867307
Fix format-string bugs by adding __attribute__((__format__)) in all applicable
Frits van Bommel <fvbommel wxs.nl>
parents:
1367
diff
changeset
|
3158 buf->printf("A%zu", dim); |
159 | 3159 for (size_t i = 0; i < dim; i++) |
3160 { Expression *key = (Expression *)keys->data[i]; | |
3161 Expression *value = (Expression *)values->data[i]; | |
3162 | |
3163 key->toMangleBuffer(buf); | |
3164 value->toMangleBuffer(buf); | |
3165 } | |
3166 } | |
3167 | |
3168 /************************ StructLiteralExp ************************************/ | |
3169 | |
3170 // sd( e1, e2, e3, ... ) | |
3171 | |
3172 StructLiteralExp::StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements) | |
3173 : Expression(loc, TOKstructliteral, sizeof(StructLiteralExp)) | |
3174 { | |
3175 this->sd = sd; | |
3176 this->elements = elements; | |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
3177 #if IN_DMD |
159 | 3178 this->sym = NULL; |
1146
1860414bf3b7
* Moved ir/irsymbol.cpp/h into ir/irdsymbol.cpp/h.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1133
diff
changeset
|
3179 #endif |
159 | 3180 this->soffset = 0; |
3181 this->fillHoles = 1; | |
3182 } | |
3183 | |
3184 Expression *StructLiteralExp::syntaxCopy() | |
3185 { | |
3186 return new StructLiteralExp(loc, sd, arraySyntaxCopy(elements)); | |
3187 } | |
3188 | |
3189 Expression *StructLiteralExp::semantic(Scope *sc) | |
3190 { Expression *e; | |
3191 | |
3192 #if LOGSEMANTIC | |
3193 printf("StructLiteralExp::semantic('%s')\n", toChars()); | |
3194 #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
|
3195 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
|
3196 return this; |
159 | 3197 |
3198 // Run semantic() on each element | |
3199 for (size_t i = 0; i < elements->dim; i++) | |
3200 { e = (Expression *)elements->data[i]; | |
3201 if (!e) | |
3202 continue; | |
3203 e = e->semantic(sc); | |
3204 elements->data[i] = (void *)e; | |
3205 } | |
3206 expandTuples(elements); | |
3207 size_t offset = 0; | |
3208 for (size_t i = 0; i < elements->dim; i++) | |
3209 { e = (Expression *)elements->data[i]; | |
3210 if (!e) | |
3211 continue; | |
3212 | |
3213 if (!e->type) | |
3214 error("%s has no value", e->toChars()); | |
3215 e = resolveProperties(sc, e); | |
3216 if (i >= sd->fields.dim) | |
3217 { error("more initializers than fields of %s", sd->toChars()); | |
3218 break; | |
3219 } | |
3220 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3221 VarDeclaration *v = s->isVarDeclaration(); | |
3222 assert(v); | |
3223 if (v->offset < offset) | |
3224 error("overlapping initialization for %s", v->toChars()); | |
3225 offset = v->offset + v->type->size(); | |
3226 | |
3227 Type *telem = v->type; | |
3228 while (!e->implicitConvTo(telem) && telem->toBasetype()->ty == Tsarray) | |
3229 { /* Static array initialization, as in: | |
3230 * T[3][5] = e; | |
3231 */ | |
3232 telem = telem->toBasetype()->nextOf(); | |
3233 } | |
3234 | |
3235 e = e->implicitCastTo(sc, telem); | |
3236 | |
3237 elements->data[i] = (void *)e; | |
3238 } | |
3239 | |
3240 /* Fill out remainder of elements[] with default initializers for fields[] | |
3241 */ | |
3242 for (size_t i = elements->dim; i < sd->fields.dim; i++) | |
3243 { Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3244 VarDeclaration *v = s->isVarDeclaration(); | |
3245 assert(v); | |
3246 | |
3247 if (v->offset < offset) | |
3248 { e = NULL; | |
3249 sd->hasUnions = 1; | |
3250 } | |
3251 else | |
3252 { | |
3253 if (v->init) | |
3254 { e = v->init->toExpression(); | |
3255 if (!e) | |
1587 | 3256 { error("cannot make expression out of initializer for %s", v->toChars()); |
3257 e = new ErrorExp(); | |
3258 } | |
3259 else if (v->scope) | |
3260 { // Do deferred semantic anaylsis | |
3261 Initializer *i2 = v->init->syntaxCopy(); | |
3262 i2 = i2->semantic(v->scope, v->type); | |
3263 e = i2->toExpression(); | |
3264 v->scope = NULL; | |
3265 } | |
159 | 3266 } |
3267 else | |
3268 { e = v->type->defaultInit(); | |
3269 e->loc = loc; | |
3270 } | |
3271 offset = v->offset + v->type->size(); | |
3272 } | |
3273 elements->push(e); | |
3274 } | |
3275 | |
3276 type = sd->type; | |
3277 return this; | |
3278 } | |
3279 | |
3280 /************************************** | |
3281 * Gets expression at offset of type. | |
3282 * Returns NULL if not found. | |
3283 */ | |
3284 | |
3285 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
|
3286 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3287 //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
|
3288 // /*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
|
3289 Expression *e = NULL; |
159 | 3290 int i = getFieldIndex(type, offset); |
3291 | |
3292 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
|
3293 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3294 //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
|
3295 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
|
3296 e = (Expression *)elements->data[i]; |
159 | 3297 if (e) |
3298 { | |
1587 | 3299 //printf("e = %s, e->type = %s\n", e->toChars(), e->type->toChars()); |
3300 | |
3301 /* If type is a static array, and e is an initializer for that array, | |
3302 * then the field initializer should be an array literal of e. | |
3303 */ | |
3304 if (e->type != type && type->ty == Tsarray) | |
3305 { TypeSArray *tsa = (TypeSArray *)type; | |
3306 uinteger_t length = tsa->dim->toInteger(); | |
3307 Expressions *z = new Expressions; | |
3308 z->setDim(length); | |
3309 for (int q = 0; q < length; ++q) | |
3310 z->data[q] = e->copy(); | |
3311 e = new ArrayLiteralExp(loc, z); | |
3312 e->type = type; | |
3313 } | |
3314 else | |
3315 { | |
3316 e = e->copy(); | |
3317 e->type = type; | |
3318 } | |
159 | 3319 } |
3320 } | |
3321 return e; | |
3322 } | |
3323 | |
3324 /************************************ | |
3325 * Get index of field. | |
3326 * Returns -1 if not found. | |
3327 */ | |
3328 | |
3329 int StructLiteralExp::getFieldIndex(Type *type, unsigned offset) | |
3330 { | |
3331 /* Find which field offset is by looking at the field offsets | |
3332 */ | |
1587 | 3333 if (elements->dim) |
3334 { | |
3335 for (size_t i = 0; i < sd->fields.dim; i++) | |
3336 { | |
3337 Dsymbol *s = (Dsymbol *)sd->fields.data[i]; | |
3338 VarDeclaration *v = s->isVarDeclaration(); | |
3339 assert(v); | |
3340 | |
3341 if (offset == v->offset && | |
3342 type->size() == v->type->size()) | |
3343 { Expression *e = (Expression *)elements->data[i]; | |
3344 if (e) | |
3345 { | |
3346 return i; | |
3347 } | |
3348 break; | |
3349 } | |
159 | 3350 } |
3351 } | |
3352 return -1; | |
3353 } | |
3354 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3355 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3356 int StructLiteralExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3357 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3358 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3359 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3360 #endif |
159 | 3361 |
3362 Expression *StructLiteralExp::toLvalue(Scope *sc, Expression *e) | |
3363 { | |
3364 return this; | |
3365 } | |
3366 | |
3367 | |
3368 int StructLiteralExp::checkSideEffect(int flag) | |
3369 { int f = 0; | |
3370 | |
3371 for (size_t i = 0; i < elements->dim; i++) | |
3372 { Expression *e = (Expression *)elements->data[i]; | |
3373 if (!e) | |
3374 continue; | |
3375 | |
3376 f |= e->checkSideEffect(2); | |
3377 } | |
3378 if (flag == 0 && f == 0) | |
3379 Expression::checkSideEffect(0); | |
3380 return f; | |
3381 } | |
3382 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3383 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3384 int StructLiteralExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3385 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3386 return arrayExpressionCanThrow(elements); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3387 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3388 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3389 |
159 | 3390 void StructLiteralExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3391 { | |
3392 buf->writestring(sd->toChars()); | |
3393 buf->writeByte('('); | |
3394 argsToCBuffer(buf, elements, hgs); | |
3395 buf->writeByte(')'); | |
3396 } | |
3397 | |
3398 void StructLiteralExp::toMangleBuffer(OutBuffer *buf) | |
3399 { | |
3400 size_t dim = elements ? elements->dim : 0; | |
1372
229e02867307
Fix format-string bugs by adding __attribute__((__format__)) in all applicable
Frits van Bommel <fvbommel wxs.nl>
parents:
1367
diff
changeset
|
3401 buf->printf("S%zu", dim); |
159 | 3402 for (size_t i = 0; i < dim; i++) |
3403 { Expression *e = (Expression *)elements->data[i]; | |
3404 if (e) | |
3405 e->toMangleBuffer(buf); | |
3406 else | |
3407 buf->writeByte('v'); // 'v' for void | |
3408 } | |
3409 } | |
3410 | |
3411 /************************ TypeDotIdExp ************************************/ | |
3412 | |
3413 /* Things like: | |
3414 * int.size | |
3415 * foo.size | |
3416 * (foo).size | |
3417 * cast(foo).size | |
3418 */ | |
3419 | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
3420 Expression *typeDotIdExp(Loc loc, Type *type, Identifier *ident) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
3421 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
3422 return new DotIdExp(loc, new TypeExp(loc, type), ident); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
3423 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
3424 |
159 | 3425 |
3426 /************************************************************/ | |
3427 | |
3428 // Mainly just a placeholder | |
3429 | |
3430 TypeExp::TypeExp(Loc loc, Type *type) | |
3431 : Expression(loc, TOKtype, sizeof(TypeExp)) | |
3432 { | |
3433 //printf("TypeExp::TypeExp(%s)\n", type->toChars()); | |
3434 this->type = type; | |
3435 } | |
3436 | |
876
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3437 Expression *TypeExp::syntaxCopy() |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3438 { |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3439 //printf("TypeExp::syntaxCopy()\n"); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3440 return new TypeExp(loc, type->syntaxCopy()); |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3441 } |
27a379f288bf
Merged DMD 1.039
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
875
diff
changeset
|
3442 |
159 | 3443 Expression *TypeExp::semantic(Scope *sc) |
3444 { | |
3445 //printf("TypeExp::semantic(%s)\n", type->toChars()); | |
3446 type = type->semantic(loc, sc); | |
3447 return this; | |
3448 } | |
3449 | |
3450 void TypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3451 { | |
3452 type->toCBuffer(buf, NULL, hgs); | |
3453 } | |
3454 | |
3455 /************************************************************/ | |
3456 | |
3457 // Mainly just a placeholder | |
3458 | |
3459 ScopeExp::ScopeExp(Loc loc, ScopeDsymbol *pkg) | |
3460 : Expression(loc, TOKimport, sizeof(ScopeExp)) | |
3461 { | |
3462 //printf("ScopeExp::ScopeExp(pkg = '%s')\n", pkg->toChars()); | |
3463 //static int count; if (++count == 38) *(char*)0=0; | |
3464 this->sds = pkg; | |
3465 } | |
3466 | |
3467 Expression *ScopeExp::syntaxCopy() | |
3468 { | |
3469 ScopeExp *se = new ScopeExp(loc, (ScopeDsymbol *)sds->syntaxCopy(NULL)); | |
3470 return se; | |
3471 } | |
3472 | |
3473 Expression *ScopeExp::semantic(Scope *sc) | |
3474 { | |
3475 TemplateInstance *ti; | |
3476 ScopeDsymbol *sds2; | |
3477 | |
3478 #if LOGSEMANTIC | |
3479 printf("+ScopeExp::semantic('%s')\n", toChars()); | |
3480 #endif | |
3481 Lagain: | |
3482 ti = sds->isTemplateInstance(); | |
3483 if (ti && !global.errors) | |
3484 { Dsymbol *s; | |
1587 | 3485 if (!ti->semanticRun) |
159 | 3486 ti->semantic(sc); |
3487 s = ti->inst->toAlias(); | |
3488 sds2 = s->isScopeDsymbol(); | |
3489 if (!sds2) | |
3490 { Expression *e; | |
3491 | |
3492 //printf("s = %s, '%s'\n", s->kind(), s->toChars()); | |
3493 if (ti->withsym) | |
3494 { | |
3495 // Same as wthis.s | |
3496 e = new VarExp(loc, ti->withsym->withstate->wthis); | |
3497 e = new DotVarExp(loc, e, s->isDeclaration()); | |
3498 } | |
3499 else | |
3500 e = new DsymbolExp(loc, s); | |
3501 e = e->semantic(sc); | |
3502 //printf("-1ScopeExp::semantic()\n"); | |
3503 return e; | |
3504 } | |
3505 if (sds2 != sds) | |
3506 { | |
3507 sds = sds2; | |
3508 goto Lagain; | |
3509 } | |
3510 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3511 } | |
3512 else | |
3513 { | |
3514 //printf("sds = %s, '%s'\n", sds->kind(), sds->toChars()); | |
3515 //printf("\tparent = '%s'\n", sds->parent->toChars()); | |
3516 sds->semantic(sc); | |
3517 } | |
3518 type = Type::tvoid; | |
3519 //printf("-2ScopeExp::semantic() %s\n", toChars()); | |
3520 return this; | |
3521 } | |
3522 | |
3523 void ScopeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3524 { | |
3525 if (sds->isTemplateInstance()) | |
3526 { | |
3527 sds->toCBuffer(buf, hgs); | |
3528 } | |
3529 else | |
3530 { | |
3531 buf->writestring(sds->kind()); | |
3532 buf->writestring(" "); | |
3533 buf->writestring(sds->toChars()); | |
3534 } | |
3535 } | |
3536 | |
3537 /********************** TemplateExp **************************************/ | |
3538 | |
3539 // Mainly just a placeholder | |
3540 | |
3541 TemplateExp::TemplateExp(Loc loc, TemplateDeclaration *td) | |
3542 : Expression(loc, TOKtemplate, sizeof(TemplateExp)) | |
3543 { | |
3544 //printf("TemplateExp(): %s\n", td->toChars()); | |
3545 this->td = td; | |
3546 } | |
3547 | |
3548 void TemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
3549 { | |
3550 buf->writestring(td->toChars()); | |
3551 } | |
3552 | |
3553 void TemplateExp::rvalue() | |
3554 { | |
3555 error("template %s has no value", toChars()); | |
3556 } | |
3557 | |
3558 /********************** NewExp **************************************/ | |
3559 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3560 /* thisexp.new(newargs) newtype(arguments) */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3561 |
159 | 3562 NewExp::NewExp(Loc loc, Expression *thisexp, Expressions *newargs, |
3563 Type *newtype, Expressions *arguments) | |
3564 : Expression(loc, TOKnew, sizeof(NewExp)) | |
3565 { | |
3566 this->thisexp = thisexp; | |
3567 this->newargs = newargs; | |
3568 this->newtype = newtype; | |
3569 this->arguments = arguments; | |
3570 member = NULL; | |
3571 allocator = NULL; | |
3572 onstack = 0; | |
3573 } | |
3574 | |
3575 Expression *NewExp::syntaxCopy() | |
3576 { | |
3577 return new NewExp(loc, | |
3578 thisexp ? thisexp->syntaxCopy() : NULL, | |
3579 arraySyntaxCopy(newargs), | |
3580 newtype->syntaxCopy(), arraySyntaxCopy(arguments)); | |
3581 } | |
3582 | |
3583 | |
3584 Expression *NewExp::semantic(Scope *sc) | |
3585 { int i; | |
3586 Type *tb; | |
3587 ClassDeclaration *cdthis = NULL; | |
3588 | |
3589 #if LOGSEMANTIC | |
3590 printf("NewExp::semantic() %s\n", toChars()); | |
3591 if (thisexp) | |
3592 printf("\tthisexp = %s\n", thisexp->toChars()); | |
3593 printf("\tnewtype: %s\n", newtype->toChars()); | |
3594 #endif | |
3595 if (type) // if semantic() already run | |
3596 return this; | |
3597 | |
3598 Lagain: | |
3599 if (thisexp) | |
3600 { thisexp = thisexp->semantic(sc); | |
3601 cdthis = thisexp->type->isClassHandle(); | |
3602 if (cdthis) | |
3603 { | |
3604 sc = sc->push(cdthis); | |
3605 type = newtype->semantic(loc, sc); | |
3606 sc = sc->pop(); | |
3607 } | |
3608 else | |
3609 { | |
3610 error("'this' for nested class must be a class type, not %s", thisexp->type->toChars()); | |
3611 type = newtype->semantic(loc, sc); | |
3612 } | |
3613 } | |
3614 else | |
3615 type = newtype->semantic(loc, sc); | |
3616 newtype = type; // in case type gets cast to something else | |
3617 tb = type->toBasetype(); | |
3618 //printf("tb: %s, deco = %s\n", tb->toChars(), tb->deco); | |
3619 | |
3620 arrayExpressionSemantic(newargs, sc); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
3621 preFunctionParameters(loc, sc, newargs); |
159 | 3622 arrayExpressionSemantic(arguments, sc); |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
3623 preFunctionParameters(loc, sc, arguments); |
159 | 3624 |
3625 if (thisexp && tb->ty != Tclass) | |
3626 error("e.new is only for allocating nested classes, not %s", tb->toChars()); | |
3627 | |
3628 if (tb->ty == Tclass) | |
3629 { TypeFunction *tf; | |
3630 | |
3631 TypeClass *tc = (TypeClass *)(tb); | |
3632 ClassDeclaration *cd = tc->sym->isClassDeclaration(); | |
3633 if (cd->isInterfaceDeclaration()) | |
3634 error("cannot create instance of interface %s", cd->toChars()); | |
3635 else if (cd->isAbstract()) | |
3636 { error("cannot create instance of abstract class %s", cd->toChars()); | |
3637 for (int i = 0; i < cd->vtbl.dim; i++) | |
3638 { FuncDeclaration *fd = ((Dsymbol *)cd->vtbl.data[i])->isFuncDeclaration(); | |
3639 if (fd && fd->isAbstract()) | |
3640 error("function %s is abstract", fd->toChars()); | |
3641 } | |
3642 } | |
3643 checkDeprecated(sc, cd); | |
3644 if (cd->isNested()) | |
3645 { /* We need a 'this' pointer for the nested class. | |
3646 * Ensure we have the right one. | |
3647 */ | |
3648 Dsymbol *s = cd->toParent2(); | |
3649 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
|
3650 FuncDeclaration *fdn = s->isFuncDeclaration(); |
159 | 3651 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
3652 //printf("cd isNested, cdn = %s\n", cdn ? cdn->toChars() : "null"); |
159 | 3653 if (cdn) |
3654 { | |
3655 if (!cdthis) | |
3656 { | |
3657 // Supply an implicit 'this' and try again | |
3658 thisexp = new ThisExp(loc); | |
3659 for (Dsymbol *sp = sc->parent; 1; sp = sp->parent) | |
3660 { if (!sp) | |
3661 { | |
3662 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3663 break; | |
3664 } | |
3665 ClassDeclaration *cdp = sp->isClassDeclaration(); | |
3666 if (!cdp) | |
3667 continue; | |
3668 if (cdp == cdn || cdn->isBaseOf(cdp, NULL)) | |
3669 break; | |
3670 // Add a '.outer' and try again | |
3671 thisexp = new DotIdExp(loc, thisexp, Id::outer); | |
3672 } | |
3673 if (!global.errors) | |
3674 goto Lagain; | |
3675 } | |
3676 if (cdthis) | |
3677 { | |
3678 //printf("cdthis = %s\n", cdthis->toChars()); | |
3679 if (cdthis != cdn && !cdn->isBaseOf(cdthis, NULL)) | |
3680 error("'this' for nested class must be of type %s, not %s", cdn->toChars(), thisexp->type->toChars()); | |
3681 } | |
3682 #if 0 | |
3683 else | |
3684 { | |
3685 for (Dsymbol *sf = sc->func; 1; sf= sf->toParent2()->isFuncDeclaration()) | |
3686 { | |
3687 if (!sf) | |
3688 { | |
3689 error("outer class %s 'this' needed to 'new' nested class %s", cdn->toChars(), cd->toChars()); | |
3690 break; | |
3691 } | |
3692 printf("sf = %s\n", sf->toChars()); | |
3693 AggregateDeclaration *ad = sf->isThis(); | |
3694 if (ad && (ad == cdn || cdn->isBaseOf(ad->isClassDeclaration(), NULL))) | |
3695 break; | |
3696 } | |
3697 } | |
3698 #endif | |
3699 } | |
3700 else if (thisexp) | |
3701 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
|
3702 else if (fdn) |
1587 | 3703 { |
639
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3704 // 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
|
3705 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
|
3706 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3707 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
|
3708 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3709 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
|
3710 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
|
3711 { |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3712 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
|
3713 break; |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3714 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3715 } |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3716 |
8aebdf56c455
Move check for access of context for nested class from backend into frontend.
Christian Kamm <kamm incasoftware de>
parents:
599
diff
changeset
|
3717 } |
159 | 3718 } |
3719 else if (thisexp) | |
3720 error("e.new is only for allocating nested classes"); | |
3721 | |
3722 FuncDeclaration *f = cd->ctor; | |
3723 if (f) | |
3724 { | |
3725 assert(f); | |
1587 | 3726 f = f->overloadResolve(loc, NULL, arguments, sc->module); |
159 | 3727 checkDeprecated(sc, f); |
3728 member = f->isCtorDeclaration(); | |
3729 assert(member); | |
3730 | |
3731 cd->accessCheck(loc, sc, member); | |
3732 | |
3733 tf = (TypeFunction *)f->type; | |
3734 type = tf->next; | |
3735 | |
3736 if (!arguments) | |
3737 arguments = new Expressions(); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
3738 functionParameters(loc, sc, tf, arguments); |
159 | 3739 } |
3740 else | |
3741 { | |
3742 if (arguments && arguments->dim) | |
3743 error("no constructor for %s", cd->toChars()); | |
3744 } | |
3745 | |
3746 if (cd->aggNew) | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3747 { |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
3748 // Prepend the size argument to newargs[] |
891
ea82e2077f9d
Change custom allocator new size argument to size_t instead of uint. See #175.
Christian Kamm <kamm incasoftware de>
parents:
876
diff
changeset
|
3749 Expression *e = new IntegerExp(loc, cd->size(loc), Type::tsize_t); |
159 | 3750 if (!newargs) |
3751 newargs = new Expressions(); | |
3752 newargs->shift(e); | |
3753 | |
1587 | 3754 f = cd->aggNew->overloadResolve(loc, NULL, newargs, sc->module); |
159 | 3755 allocator = f->isNewDeclaration(); |
3756 assert(allocator); | |
3757 | |
3758 tf = (TypeFunction *)f->type; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
3759 functionParameters(loc, sc, tf, newargs); |
159 | 3760 } |
3761 else | |
3762 { | |
3763 if (newargs && newargs->dim) | |
3764 error("no allocator for %s", cd->toChars()); | |
3765 } | |
3766 } | |
3767 else if (tb->ty == Tstruct) | |
3768 { | |
3769 TypeStruct *ts = (TypeStruct *)tb; | |
3770 StructDeclaration *sd = ts->sym; | |
3771 FuncDeclaration *f = sd->aggNew; | |
3772 TypeFunction *tf; | |
3773 | |
3774 if (arguments && arguments->dim) | |
3775 error("no constructor for %s", type->toChars()); | |
3776 | |
3777 if (f) | |
3778 { | |
3779 Expression *e; | |
3780 | |
3781 // Prepend the uint size argument to newargs[] | |
3782 e = new IntegerExp(loc, sd->size(loc), Type::tuns32); | |
3783 if (!newargs) | |
3784 newargs = new Expressions(); | |
3785 newargs->shift(e); | |
3786 | |
1587 | 3787 f = f->overloadResolve(loc, NULL, newargs, sc->module); |
159 | 3788 allocator = f->isNewDeclaration(); |
3789 assert(allocator); | |
3790 | |
3791 tf = (TypeFunction *)f->type; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
3792 functionParameters(loc, sc, tf, newargs); |
159 | 3793 |
3794 e = new VarExp(loc, f); | |
3795 e = new CallExp(loc, e, newargs); | |
3796 e = e->semantic(sc); | |
3797 e->type = type->pointerTo(); | |
3798 return e; | |
3799 } | |
3800 | |
3801 type = type->pointerTo(); | |
3802 } | |
3803 else if (tb->ty == Tarray && (arguments && arguments->dim)) | |
3804 { | |
3805 for (size_t i = 0; i < arguments->dim; i++) | |
3806 { | |
3807 if (tb->ty != Tarray) | |
3808 { error("too many arguments for array"); | |
3809 arguments->dim = i; | |
3810 break; | |
3811 } | |
3812 | |
3813 Expression *arg = (Expression *)arguments->data[i]; | |
3814 arg = resolveProperties(sc, arg); | |
3815 arg = arg->implicitCastTo(sc, Type::tsize_t); | |
3816 if (arg->op == TOKint64 && (long long)arg->toInteger() < 0) | |
3817 error("negative array index %s", arg->toChars()); | |
3818 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
|
3819 tb = ((TypeDArray *)tb)->next->toBasetype(); |
159 | 3820 } |
3821 } | |
3822 else if (tb->isscalar()) | |
3823 { | |
3824 if (arguments && arguments->dim) | |
3825 error("no constructor for %s", type->toChars()); | |
3826 | |
3827 type = type->pointerTo(); | |
3828 } | |
3829 else | |
3830 { | |
3831 error("new can only create structs, dynamic arrays or class objects, not %s's", type->toChars()); | |
3832 type = type->pointerTo(); | |
3833 } | |
3834 | |
3835 //printf("NewExp: '%s'\n", toChars()); | |
3836 //printf("NewExp:type '%s'\n", type->toChars()); | |
3837 | |
3838 return this; | |
3839 } | |
3840 | |
3841 int NewExp::checkSideEffect(int flag) | |
3842 { | |
3843 return 1; | |
3844 } | |
3845 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3846 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3847 int NewExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3848 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3849 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3850 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3851 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3852 |
159 | 3853 void NewExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3854 { int i; | |
3855 | |
3856 if (thisexp) | |
3857 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3858 buf->writeByte('.'); | |
3859 } | |
3860 buf->writestring("new "); | |
3861 if (newargs && newargs->dim) | |
3862 { | |
3863 buf->writeByte('('); | |
3864 argsToCBuffer(buf, newargs, hgs); | |
3865 buf->writeByte(')'); | |
3866 } | |
3867 newtype->toCBuffer(buf, NULL, hgs); | |
3868 if (arguments && arguments->dim) | |
3869 { | |
3870 buf->writeByte('('); | |
3871 argsToCBuffer(buf, arguments, hgs); | |
3872 buf->writeByte(')'); | |
3873 } | |
3874 } | |
3875 | |
3876 /********************** NewAnonClassExp **************************************/ | |
3877 | |
3878 NewAnonClassExp::NewAnonClassExp(Loc loc, Expression *thisexp, | |
3879 Expressions *newargs, ClassDeclaration *cd, Expressions *arguments) | |
3880 : Expression(loc, TOKnewanonclass, sizeof(NewAnonClassExp)) | |
3881 { | |
3882 this->thisexp = thisexp; | |
3883 this->newargs = newargs; | |
3884 this->cd = cd; | |
3885 this->arguments = arguments; | |
3886 } | |
3887 | |
3888 Expression *NewAnonClassExp::syntaxCopy() | |
3889 { | |
3890 return new NewAnonClassExp(loc, | |
3891 thisexp ? thisexp->syntaxCopy() : NULL, | |
3892 arraySyntaxCopy(newargs), | |
3893 (ClassDeclaration *)cd->syntaxCopy(NULL), | |
3894 arraySyntaxCopy(arguments)); | |
3895 } | |
3896 | |
3897 | |
3898 Expression *NewAnonClassExp::semantic(Scope *sc) | |
3899 { | |
3900 #if LOGSEMANTIC | |
3901 printf("NewAnonClassExp::semantic() %s\n", toChars()); | |
336 | 3902 //printf("thisexp = %p\n", thisexp); |
159 | 3903 //printf("type: %s\n", type->toChars()); |
3904 #endif | |
3905 | |
3906 Expression *d = new DeclarationExp(loc, cd); | |
3907 d = d->semantic(sc); | |
3908 | |
3909 Expression *n = new NewExp(loc, thisexp, newargs, cd->type, arguments); | |
3910 | |
3911 Expression *c = new CommaExp(loc, d, n); | |
3912 return c->semantic(sc); | |
3913 } | |
3914 | |
3915 int NewAnonClassExp::checkSideEffect(int flag) | |
3916 { | |
3917 return 1; | |
3918 } | |
3919 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3920 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3921 int NewAnonClassExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3922 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3923 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3924 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3925 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3926 |
159 | 3927 void NewAnonClassExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
3928 { int i; | |
3929 | |
3930 if (thisexp) | |
3931 { expToCBuffer(buf, hgs, thisexp, PREC_primary); | |
3932 buf->writeByte('.'); | |
3933 } | |
3934 buf->writestring("new"); | |
3935 if (newargs && newargs->dim) | |
3936 { | |
3937 buf->writeByte('('); | |
3938 argsToCBuffer(buf, newargs, hgs); | |
3939 buf->writeByte(')'); | |
3940 } | |
3941 buf->writestring(" class "); | |
3942 if (arguments && arguments->dim) | |
3943 { | |
3944 buf->writeByte('('); | |
3945 argsToCBuffer(buf, arguments, hgs); | |
3946 buf->writeByte(')'); | |
3947 } | |
3948 //buf->writestring(" { }"); | |
3949 if (cd) | |
3950 { | |
3951 cd->toCBuffer(buf, hgs); | |
3952 } | |
3953 } | |
3954 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3955 /********************** SymbolExp **************************************/ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3956 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3957 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3958 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
|
3959 : Expression(loc, op, size) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3960 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3961 assert(var); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3962 this->var = var; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3963 this->hasOverloads = hasOverloads; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3964 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3965 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
3966 |
159 | 3967 /********************** SymOffExp **************************************/ |
3968 | |
3969 SymOffExp::SymOffExp(Loc loc, Declaration *var, unsigned offset) | |
3970 : Expression(loc, TOKsymoff, sizeof(SymOffExp)) | |
3971 { | |
3972 assert(var); | |
3973 this->var = var; | |
3974 this->offset = offset; | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1258
diff
changeset
|
3975 m = NULL; |
159 | 3976 VarDeclaration *v = var->isVarDeclaration(); |
3977 if (v && v->needThis()) | |
3978 error("need 'this' for address of %s", v->toChars()); | |
3979 } | |
3980 | |
3981 Expression *SymOffExp::semantic(Scope *sc) | |
3982 { | |
3983 #if LOGSEMANTIC | |
3984 printf("SymOffExp::semantic('%s')\n", toChars()); | |
3985 #endif | |
3986 //var->semantic(sc); | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1258
diff
changeset
|
3987 m = sc->module; |
159 | 3988 if (!type) |
3989 type = var->type->pointerTo(); | |
3990 VarDeclaration *v = var->isVarDeclaration(); | |
3991 if (v) | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
3992 v->checkNestedReference(sc, loc); |
159 | 3993 return this; |
3994 } | |
3995 | |
3996 int SymOffExp::isBool(int result) | |
3997 { | |
3998 return result ? TRUE : FALSE; | |
3999 } | |
4000 | |
4001 void SymOffExp::checkEscape() | |
4002 { | |
4003 VarDeclaration *v = var->isVarDeclaration(); | |
4004 if (v) | |
4005 { | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4006 if (!v->isDataseg() && !(v->storage_class & (STCref | STCout))) |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4007 { /* BUG: This should be allowed: |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4008 * void foo() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4009 * { int a; |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4010 * int* bar() { return &a; } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4011 * } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4012 */ |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4013 error("escaping reference to local %s", v->toChars()); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4014 } |
159 | 4015 } |
4016 } | |
4017 | |
4018 void SymOffExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4019 { | |
4020 if (offset) | |
4021 buf->printf("(& %s+%u)", var->toChars(), offset); | |
4022 else | |
4023 buf->printf("& %s", var->toChars()); | |
4024 } | |
4025 | |
4026 /******************************** VarExp **************************/ | |
4027 | |
4028 VarExp::VarExp(Loc loc, Declaration *var) | |
4029 : Expression(loc, TOKvar, sizeof(VarExp)) | |
4030 { | |
4031 //printf("VarExp(this = %p, '%s')\n", this, var->toChars()); | |
4032 this->var = var; | |
4033 this->type = var->type; | |
4034 } | |
4035 | |
4036 int VarExp::equals(Object *o) | |
4037 { VarExp *ne; | |
4038 | |
4039 if (this == o || | |
4040 (((Expression *)o)->op == TOKvar && | |
4041 ((ne = (VarExp *)o), type->equals(ne->type)) && | |
4042 var == ne->var)) | |
4043 return 1; | |
4044 return 0; | |
4045 } | |
4046 | |
4047 Expression *VarExp::semantic(Scope *sc) | |
4048 { FuncLiteralDeclaration *fd; | |
4049 | |
4050 #if LOGSEMANTIC | |
4051 printf("VarExp::semantic(%s)\n", toChars()); | |
4052 #endif | |
4053 if (!type) | |
4054 { type = var->type; | |
4055 #if 0 | |
4056 if (var->storage_class & STClazy) | |
4057 { | |
4058 TypeFunction *tf = new TypeFunction(NULL, type, 0, LINKd); | |
4059 type = new TypeDelegate(tf); | |
4060 type = type->semantic(loc, sc); | |
4061 } | |
4062 #endif | |
4063 } | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4064 |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4065 /* 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
|
4066 * problems when instantiating imported templates passing private |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4067 * variables as alias template parameters. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4068 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4069 //accessCheck(loc, sc, NULL, var); |
159 | 4070 |
4071 VarDeclaration *v = var->isVarDeclaration(); | |
4072 if (v) | |
4073 { | |
1499
df11cdec45a2
Another shot at fixing the issues with (constant) struct literals and their addresses. See DMD2682, #218, #324.
Christian Kamm <kamm incasoftware de>
parents:
1372
diff
changeset
|
4074 if (v->isSameAsInitializer() && type->toBasetype()->ty != Tsarray && v->init) |
159 | 4075 { |
4076 ExpInitializer *ei = v->init->isExpInitializer(); | |
4077 if (ei) | |
4078 { | |
4079 //ei->exp->implicitCastTo(sc, type)->print(); | |
4080 return ei->exp->implicitCastTo(sc, type); | |
4081 } | |
4082 } | |
4083 v->checkNestedReference(sc, loc); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4084 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4085 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
|
4086 { |
1619
c61782a76dff
Merge DMD r304: refactor invariant => immutable
Leandro Lucarella <llucax@gmail.com>
parents:
1618
diff
changeset
|
4087 if (v->isDataseg() && !v->isImmutable()) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4088 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
|
4089 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4090 #endif |
159 | 4091 } |
4092 #if 0 | |
4093 else if ((fd = var->isFuncLiteralDeclaration()) != NULL) | |
4094 { Expression *e; | |
4095 e = new FuncExp(loc, fd); | |
4096 e->type = type; | |
4097 return e; | |
4098 } | |
4099 #endif | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4100 |
159 | 4101 return this; |
4102 } | |
4103 | |
4104 char *VarExp::toChars() | |
4105 { | |
4106 return var->toChars(); | |
4107 } | |
4108 | |
4109 void VarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4110 { | |
4111 buf->writestring(var->toChars()); | |
4112 } | |
4113 | |
4114 void VarExp::checkEscape() | |
4115 { | |
4116 VarDeclaration *v = var->isVarDeclaration(); | |
4117 if (v) | |
4118 { Type *tb = v->type->toBasetype(); | |
4119 // if reference type | |
4120 if (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tclass) | |
4121 { | |
1530
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1499
diff
changeset
|
4122 if (v->isScope() && !v->noscope) |
05c235309d6f
Make the auto storage class never have the same meaning as scope.
Christian Kamm <kamm incasoftware de>
parents:
1499
diff
changeset
|
4123 error("escaping reference to scope local %s", v->toChars()); |
159 | 4124 else if (v->storage_class & STCvariadic) |
4125 error("escaping reference to variadic parameter %s", v->toChars()); | |
4126 } | |
4127 } | |
4128 } | |
4129 | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4130 void VarExp::checkEscapeRef() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4131 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4132 VarDeclaration *v = var->isVarDeclaration(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4133 if (v) |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4134 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4135 if (!v->isDataseg() && !(v->storage_class & (STCref | STCout))) |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4136 error("escaping reference to local variable %s", v->toChars()); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4137 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4138 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4139 |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4140 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4141 int VarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4142 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4143 if (var->storage_class & STClazy) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4144 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4145 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4146 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4147 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4148 |
159 | 4149 Expression *VarExp::toLvalue(Scope *sc, Expression *e) |
4150 { | |
4151 #if 0 | |
4152 tym = tybasic(e1->ET->Tty); | |
4153 if (!(tyscalar(tym) || | |
4154 tym == TYstruct || | |
4155 tym == TYarray && e->Eoper == TOKaddr)) | |
4156 synerr(EM_lvalue); // lvalue expected | |
4157 #endif | |
4158 if (var->storage_class & STClazy) | |
4159 error("lazy variables cannot be lvalues"); | |
4160 return this; | |
4161 } | |
4162 | |
4163 Expression *VarExp::modifiableLvalue(Scope *sc, Expression *e) | |
4164 { | |
4165 //printf("VarExp::modifiableLvalue('%s')\n", var->toChars()); | |
4166 if (sc->incontract && var->isParameter()) | |
4167 error("cannot modify parameter '%s' in contract", var->toChars()); | |
4168 | |
4169 if (type && type->toBasetype()->ty == Tsarray) | |
4170 error("cannot change reference to static array '%s'", var->toChars()); | |
4171 | |
4172 VarDeclaration *v = var->isVarDeclaration(); | |
4173 if (v && v->canassign == 0 && | |
4174 (var->isConst() || (global.params.Dversion > 1 && var->isFinal()))) | |
4175 error("cannot modify final variable '%s'", var->toChars()); | |
4176 | |
4177 if (var->isCtorinit()) | |
4178 { // It's only modifiable if inside the right constructor | |
4179 Dsymbol *s = sc->func; | |
4180 while (1) | |
4181 { | |
4182 FuncDeclaration *fd = NULL; | |
4183 if (s) | |
4184 fd = s->isFuncDeclaration(); | |
4185 if (fd && | |
4186 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
4187 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
4188 fd->toParent() == var->toParent() | |
4189 ) | |
4190 { | |
4191 VarDeclaration *v = var->isVarDeclaration(); | |
4192 assert(v); | |
4193 v->ctorinit = 1; | |
4194 //printf("setting ctorinit\n"); | |
4195 } | |
4196 else | |
4197 { | |
4198 if (s) | |
4199 { s = s->toParent2(); | |
4200 continue; | |
4201 } | |
4202 else | |
4203 { | |
4204 const char *p = var->isStatic() ? "static " : ""; | |
4205 error("can only initialize %sconst %s inside %sconstructor", | |
4206 p, var->toChars(), p); | |
4207 } | |
4208 } | |
4209 break; | |
4210 } | |
4211 } | |
4212 | |
4213 // See if this expression is a modifiable lvalue (i.e. not const) | |
4214 return toLvalue(sc, e); | |
4215 } | |
4216 | |
4217 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4218 /******************************** OverExp **************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4219 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4220 #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
|
4221 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
|
4222 : 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
|
4223 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4224 //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
|
4225 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
|
4226 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
|
4227 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4228 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4229 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
|
4230 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4231 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
|
4232 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4233 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4234 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
|
4235 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4236 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
|
4237 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4238 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4239 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4240 |
159 | 4241 /******************************** TupleExp **************************/ |
4242 | |
4243 TupleExp::TupleExp(Loc loc, Expressions *exps) | |
4244 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4245 { | |
4246 //printf("TupleExp(this = %p)\n", this); | |
4247 this->exps = exps; | |
4248 this->type = NULL; | |
4249 } | |
4250 | |
4251 | |
4252 TupleExp::TupleExp(Loc loc, TupleDeclaration *tup) | |
4253 : Expression(loc, TOKtuple, sizeof(TupleExp)) | |
4254 { | |
4255 exps = new Expressions(); | |
4256 type = NULL; | |
4257 | |
4258 exps->reserve(tup->objects->dim); | |
4259 for (size_t i = 0; i < tup->objects->dim; i++) | |
4260 { Object *o = (Object *)tup->objects->data[i]; | |
4261 if (o->dyncast() == DYNCAST_EXPRESSION) | |
4262 { | |
4263 Expression *e = (Expression *)o; | |
4264 e = e->syntaxCopy(); | |
4265 exps->push(e); | |
4266 } | |
4267 else if (o->dyncast() == DYNCAST_DSYMBOL) | |
4268 { | |
4269 Dsymbol *s = (Dsymbol *)o; | |
4270 Expression *e = new DsymbolExp(loc, s); | |
4271 exps->push(e); | |
4272 } | |
4273 else if (o->dyncast() == DYNCAST_TYPE) | |
4274 { | |
4275 Type *t = (Type *)o; | |
4276 Expression *e = new TypeExp(loc, t); | |
4277 exps->push(e); | |
4278 } | |
4279 else | |
4280 { | |
4281 error("%s is not an expression", o->toChars()); | |
4282 } | |
4283 } | |
4284 } | |
4285 | |
4286 int TupleExp::equals(Object *o) | |
4287 { TupleExp *ne; | |
4288 | |
4289 if (this == o) | |
4290 return 1; | |
4291 if (((Expression *)o)->op == TOKtuple) | |
4292 { | |
4293 TupleExp *te = (TupleExp *)o; | |
4294 if (exps->dim != te->exps->dim) | |
4295 return 0; | |
4296 for (size_t i = 0; i < exps->dim; i++) | |
4297 { Expression *e1 = (Expression *)exps->data[i]; | |
4298 Expression *e2 = (Expression *)te->exps->data[i]; | |
4299 | |
4300 if (!e1->equals(e2)) | |
4301 return 0; | |
4302 } | |
4303 return 1; | |
4304 } | |
4305 return 0; | |
4306 } | |
4307 | |
4308 Expression *TupleExp::syntaxCopy() | |
4309 { | |
4310 return new TupleExp(loc, arraySyntaxCopy(exps)); | |
4311 } | |
4312 | |
4313 Expression *TupleExp::semantic(Scope *sc) | |
4314 { | |
4315 #if LOGSEMANTIC | |
4316 printf("+TupleExp::semantic(%s)\n", toChars()); | |
4317 #endif | |
4318 if (type) | |
4319 return this; | |
4320 | |
4321 // Run semantic() on each argument | |
4322 for (size_t i = 0; i < exps->dim; i++) | |
4323 { Expression *e = (Expression *)exps->data[i]; | |
4324 | |
4325 e = e->semantic(sc); | |
4326 if (!e->type) | |
4327 { error("%s has no value", e->toChars()); | |
4328 e->type = Type::terror; | |
4329 } | |
4330 exps->data[i] = (void *)e; | |
4331 } | |
4332 | |
4333 expandTuples(exps); | |
4334 if (0 && exps->dim == 1) | |
4335 { | |
4336 return (Expression *)exps->data[0]; | |
4337 } | |
4338 type = new TypeTuple(exps); | |
1587 | 4339 type = type->semantic(loc, sc); |
159 | 4340 //printf("-TupleExp::semantic(%s)\n", toChars()); |
4341 return this; | |
4342 } | |
4343 | |
4344 void TupleExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4345 { | |
4346 buf->writestring("tuple("); | |
4347 argsToCBuffer(buf, exps, hgs); | |
4348 buf->writeByte(')'); | |
4349 } | |
4350 | |
4351 int TupleExp::checkSideEffect(int flag) | |
4352 { int f = 0; | |
4353 | |
4354 for (int i = 0; i < exps->dim; i++) | |
4355 { Expression *e = (Expression *)exps->data[i]; | |
4356 | |
4357 f |= e->checkSideEffect(2); | |
4358 } | |
4359 if (flag == 0 && f == 0) | |
4360 Expression::checkSideEffect(0); | |
4361 return f; | |
4362 } | |
4363 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4364 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4365 int TupleExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4366 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4367 return arrayExpressionCanThrow(exps); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4368 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4369 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4370 |
159 | 4371 void TupleExp::checkEscape() |
4372 { | |
4373 for (size_t i = 0; i < exps->dim; i++) | |
4374 { Expression *e = (Expression *)exps->data[i]; | |
4375 e->checkEscape(); | |
4376 } | |
4377 } | |
4378 | |
4379 /******************************** FuncExp *********************************/ | |
4380 | |
4381 FuncExp::FuncExp(Loc loc, FuncLiteralDeclaration *fd) | |
4382 : Expression(loc, TOKfunction, sizeof(FuncExp)) | |
4383 { | |
4384 this->fd = fd; | |
4385 } | |
4386 | |
4387 Expression *FuncExp::syntaxCopy() | |
4388 { | |
4389 return new FuncExp(loc, (FuncLiteralDeclaration *)fd->syntaxCopy(NULL)); | |
4390 } | |
4391 | |
4392 Expression *FuncExp::semantic(Scope *sc) | |
4393 { | |
4394 #if LOGSEMANTIC | |
4395 printf("FuncExp::semantic(%s)\n", toChars()); | |
4396 #endif | |
4397 if (!type) | |
4398 { | |
4399 fd->semantic(sc); | |
4400 fd->parent = sc->parent; | |
4401 if (global.errors) | |
4402 { | |
4403 } | |
4404 else | |
4405 { | |
4406 fd->semantic2(sc); | |
1587 | 4407 if (!global.errors || |
4408 // need to infer return type | |
4409 (fd->type && fd->type->ty == Tfunction && !fd->type->nextOf())) | |
159 | 4410 { |
4411 fd->semantic3(sc); | |
4412 | |
4413 if (!global.errors && global.params.useInline) | |
4414 fd->inlineScan(); | |
4415 } | |
4416 } | |
4417 | |
1587 | 4418 // need to infer return type |
4419 if (global.errors && fd->type && fd->type->ty == Tfunction && !fd->type->nextOf()) | |
4420 ((TypeFunction *)fd->type)->next = Type::terror; | |
4421 | |
159 | 4422 // Type is a "delegate to" or "pointer to" the function literal |
4423 if (fd->isNested()) | |
4424 { | |
4425 type = new TypeDelegate(fd->type); | |
4426 type = type->semantic(loc, sc); | |
4427 } | |
4428 else | |
4429 { | |
4430 type = fd->type->pointerTo(); | |
4431 } | |
4432 } | |
4433 return this; | |
4434 } | |
4435 | |
4436 char *FuncExp::toChars() | |
4437 { | |
4438 return fd->toChars(); | |
4439 } | |
4440 | |
4441 void FuncExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4442 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
4443 fd->toCBuffer(buf, hgs); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
4444 //buf->writestring(fd->toChars()); |
159 | 4445 } |
4446 | |
4447 | |
4448 /******************************** DeclarationExp **************************/ | |
4449 | |
4450 DeclarationExp::DeclarationExp(Loc loc, Dsymbol *declaration) | |
4451 : Expression(loc, TOKdeclaration, sizeof(DeclarationExp)) | |
4452 { | |
4453 this->declaration = declaration; | |
4454 } | |
4455 | |
4456 Expression *DeclarationExp::syntaxCopy() | |
4457 { | |
4458 return new DeclarationExp(loc, declaration->syntaxCopy(NULL)); | |
4459 } | |
4460 | |
4461 Expression *DeclarationExp::semantic(Scope *sc) | |
4462 { | |
4463 if (type) | |
4464 return this; | |
4465 | |
4466 #if LOGSEMANTIC | |
4467 printf("DeclarationExp::semantic() %s\n", toChars()); | |
4468 #endif | |
4469 | |
4470 /* This is here to support extern(linkage) declaration, | |
4471 * where the extern(linkage) winds up being an AttribDeclaration | |
4472 * wrapper. | |
4473 */ | |
4474 Dsymbol *s = declaration; | |
4475 | |
4476 AttribDeclaration *ad = declaration->isAttribDeclaration(); | |
4477 if (ad) | |
4478 { | |
4479 if (ad->decl && ad->decl->dim == 1) | |
4480 s = (Dsymbol *)ad->decl->data[0]; | |
4481 } | |
4482 | |
4483 if (s->isVarDeclaration()) | |
4484 { // Do semantic() on initializer first, so: | |
4485 // int a = a; | |
4486 // will be illegal. | |
4487 declaration->semantic(sc); | |
4488 s->parent = sc->parent; | |
4489 } | |
4490 | |
4491 //printf("inserting '%s' %p into sc = %p\n", s->toChars(), s, sc); | |
4492 // Insert into both local scope and function scope. | |
4493 // Must be unique in both. | |
4494 if (s->ident) | |
4495 { | |
4496 if (!sc->insert(s)) | |
4497 error("declaration %s is already defined", s->toPrettyChars()); | |
4498 else if (sc->func) | |
4499 { VarDeclaration *v = s->isVarDeclaration(); | |
4500 if ((s->isFuncDeclaration() /*|| v && v->storage_class & STCstatic*/) && | |
4501 !sc->func->localsymtab->insert(s)) | |
4502 error("declaration %s is already defined in another scope in %s", s->toPrettyChars(), sc->func->toChars()); | |
4503 else if (!global.params.useDeprecated) | |
4504 { // Disallow shadowing | |
4505 | |
4506 for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing) | |
4507 { Dsymbol *s2; | |
4508 | |
4509 if (scx->scopesym && scx->scopesym->symtab && | |
4510 (s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL && | |
4511 s != s2) | |
4512 { | |
4513 error("shadowing declaration %s is deprecated", s->toPrettyChars()); | |
4514 } | |
4515 } | |
4516 } | |
4517 } | |
4518 } | |
4519 if (!s->isVarDeclaration()) | |
4520 { | |
1587 | 4521 Scope *sc2 = sc; |
4522 if (sc2->stc & (STCpure | STCnothrow)) | |
4523 sc2 = sc->push(); | |
4524 sc2->stc &= ~(STCpure | STCnothrow); | |
4525 declaration->semantic(sc2); | |
4526 if (sc2 != sc) | |
4527 sc2->pop(); | |
159 | 4528 s->parent = sc->parent; |
4529 } | |
4530 if (!global.errors) | |
4531 { | |
4532 declaration->semantic2(sc); | |
4533 if (!global.errors) | |
4534 { | |
4535 declaration->semantic3(sc); | |
4536 | |
4537 if (!global.errors && global.params.useInline) | |
4538 declaration->inlineScan(); | |
4539 } | |
4540 } | |
4541 | |
4542 type = Type::tvoid; | |
4543 return this; | |
4544 } | |
4545 | |
4546 int DeclarationExp::checkSideEffect(int flag) | |
4547 { | |
4548 return 1; | |
4549 } | |
4550 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4551 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4552 int DeclarationExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4553 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4554 VarDeclaration *v = declaration->isVarDeclaration(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4555 if (v && v->init) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4556 { ExpInitializer *ie = v->init->isExpInitializer(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4557 return ie && ie->exp->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4558 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4559 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4560 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4561 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4562 |
159 | 4563 void DeclarationExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4564 { | |
4565 declaration->toCBuffer(buf, hgs); | |
4566 } | |
4567 | |
4568 | |
4569 /************************ TypeidExp ************************************/ | |
4570 | |
4571 /* | |
4572 * typeid(int) | |
4573 */ | |
4574 | |
4575 TypeidExp::TypeidExp(Loc loc, Type *typeidType) | |
4576 : Expression(loc, TOKtypeid, sizeof(TypeidExp)) | |
4577 { | |
4578 this->typeidType = typeidType; | |
4579 } | |
4580 | |
4581 | |
4582 Expression *TypeidExp::syntaxCopy() | |
4583 { | |
4584 return new TypeidExp(loc, typeidType->syntaxCopy()); | |
4585 } | |
4586 | |
4587 | |
4588 Expression *TypeidExp::semantic(Scope *sc) | |
4589 { Expression *e; | |
4590 | |
4591 #if LOGSEMANTIC | |
4592 printf("TypeidExp::semantic()\n"); | |
4593 #endif | |
4594 typeidType = typeidType->semantic(loc, sc); | |
4595 e = typeidType->getTypeInfo(sc); | |
336 | 4596 if (e->loc.linnum == 0) |
4597 e->loc = loc; // so there's at least some line number info | |
159 | 4598 return e; |
4599 } | |
4600 | |
4601 void TypeidExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4602 { | |
4603 buf->writestring("typeid("); | |
4604 typeidType->toCBuffer(buf, NULL, hgs); | |
4605 buf->writeByte(')'); | |
4606 } | |
4607 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4608 /************************ TraitsExp ************************************/ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4609 #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
|
4610 /* |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4611 * __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
|
4612 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4613 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4614 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
|
4615 : 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
|
4616 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4617 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
|
4618 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
|
4619 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4620 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4621 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4622 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
|
4623 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4624 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
|
4625 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4626 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4627 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4628 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
|
4629 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4630 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
|
4631 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
|
4632 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
|
4633 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4634 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
|
4635 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4636 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
|
4637 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
|
4638 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
|
4639 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4640 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4641 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
|
4642 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4643 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4644 |
159 | 4645 /************************************************************/ |
4646 | |
4647 HaltExp::HaltExp(Loc loc) | |
4648 : Expression(loc, TOKhalt, sizeof(HaltExp)) | |
4649 { | |
4650 } | |
4651 | |
4652 Expression *HaltExp::semantic(Scope *sc) | |
4653 { | |
4654 #if LOGSEMANTIC | |
4655 printf("HaltExp::semantic()\n"); | |
4656 #endif | |
4657 type = Type::tvoid; | |
4658 return this; | |
4659 } | |
4660 | |
4661 int HaltExp::checkSideEffect(int flag) | |
4662 { | |
4663 return 1; | |
4664 } | |
4665 | |
4666 void HaltExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4667 { | |
4668 buf->writestring("halt"); | |
4669 } | |
4670 | |
4671 /************************************************************/ | |
4672 | |
4673 IsExp::IsExp(Loc loc, Type *targ, Identifier *id, enum TOK tok, | |
4674 Type *tspec, enum TOK tok2) | |
4675 : Expression(loc, TOKis, sizeof(IsExp)) | |
4676 { | |
4677 this->targ = targ; | |
4678 this->id = id; | |
4679 this->tok = tok; | |
4680 this->tspec = tspec; | |
4681 this->tok2 = tok2; | |
4682 } | |
4683 | |
4684 Expression *IsExp::syntaxCopy() | |
4685 { | |
4686 return new IsExp(loc, | |
4687 targ->syntaxCopy(), | |
4688 id, | |
4689 tok, | |
4690 tspec ? tspec->syntaxCopy() : NULL, | |
4691 tok2); | |
4692 } | |
4693 | |
4694 Expression *IsExp::semantic(Scope *sc) | |
4695 { Type *tded; | |
4696 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4697 /* 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
|
4698 * 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
|
4699 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4700 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4701 //printf("IsExp::semantic(%s)\n", toChars()); |
159 | 4702 if (id && !(sc->flags & SCOPEstaticif)) |
4703 error("can only declare type aliases within static if conditionals"); | |
4704 | |
4705 unsigned errors_save = global.errors; | |
4706 global.errors = 0; | |
4707 global.gag++; // suppress printing of error messages | |
4708 targ = targ->semantic(loc, sc); | |
4709 global.gag--; | |
4710 unsigned gerrors = global.errors; | |
4711 global.errors = errors_save; | |
4712 | |
4713 if (gerrors) // if any errors happened | |
4714 { // then condition is false | |
4715 goto Lno; | |
4716 } | |
4717 else if (tok2 != TOKreserved) | |
4718 { | |
4719 switch (tok2) | |
4720 { | |
4721 case TOKtypedef: | |
4722 if (targ->ty != Ttypedef) | |
4723 goto Lno; | |
4724 tded = ((TypeTypedef *)targ)->sym->basetype; | |
4725 break; | |
4726 | |
4727 case TOKstruct: | |
4728 if (targ->ty != Tstruct) | |
4729 goto Lno; | |
4730 if (((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4731 goto Lno; | |
4732 tded = targ; | |
4733 break; | |
4734 | |
4735 case TOKunion: | |
4736 if (targ->ty != Tstruct) | |
4737 goto Lno; | |
4738 if (!((TypeStruct *)targ)->sym->isUnionDeclaration()) | |
4739 goto Lno; | |
4740 tded = targ; | |
4741 break; | |
4742 | |
4743 case TOKclass: | |
4744 if (targ->ty != Tclass) | |
4745 goto Lno; | |
4746 if (((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4747 goto Lno; | |
4748 tded = targ; | |
4749 break; | |
4750 | |
4751 case TOKinterface: | |
4752 if (targ->ty != Tclass) | |
4753 goto Lno; | |
4754 if (!((TypeClass *)targ)->sym->isInterfaceDeclaration()) | |
4755 goto Lno; | |
4756 tded = targ; | |
4757 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
|
4758 #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
|
4759 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
|
4760 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
|
4761 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
|
4762 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
|
4763 break; |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4764 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4765 case TOKinvariant: |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4766 case TOKimmutable: |
1619
c61782a76dff
Merge DMD r304: refactor invariant => immutable
Leandro Lucarella <llucax@gmail.com>
parents:
1618
diff
changeset
|
4767 if (!targ->isImmutable()) |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4768 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
|
4769 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
|
4770 break; |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4771 |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4772 case TOKshared: |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4773 if (!targ->isShared()) |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4774 goto Lno; |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4775 tded = targ; |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4776 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
|
4777 #endif |
159 | 4778 |
4779 case TOKsuper: | |
4780 // If class or interface, get the base class and interfaces | |
4781 if (targ->ty != Tclass) | |
4782 goto Lno; | |
4783 else | |
4784 { ClassDeclaration *cd = ((TypeClass *)targ)->sym; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4785 Parameters *args = new Parameters; |
159 | 4786 args->reserve(cd->baseclasses.dim); |
4787 for (size_t i = 0; i < cd->baseclasses.dim; i++) | |
4788 { BaseClass *b = (BaseClass *)cd->baseclasses.data[i]; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4789 args->push(new Parameter(STCin, b->type, NULL, NULL)); |
159 | 4790 } |
4791 tded = new TypeTuple(args); | |
4792 } | |
4793 break; | |
4794 | |
4795 case TOKenum: | |
4796 if (targ->ty != Tenum) | |
4797 goto Lno; | |
4798 tded = ((TypeEnum *)targ)->sym->memtype; | |
4799 break; | |
4800 | |
4801 case TOKdelegate: | |
4802 if (targ->ty != Tdelegate) | |
4803 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
|
4804 tded = ((TypeDelegate *)targ)->next; // the underlying function type |
159 | 4805 break; |
4806 | |
4807 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
|
4808 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
4809 if (targ->ty != Tfunction) |
159 | 4810 goto Lno; |
4811 tded = targ; | |
4812 | |
4813 /* Generate tuple from function parameter types. | |
4814 */ | |
4815 assert(tded->ty == Tfunction); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4816 Parameters *params = ((TypeFunction *)tded)->parameters; |
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4817 size_t dim = Parameter::dim(params); |
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4818 Parameters *args = new Parameters; |
159 | 4819 args->reserve(dim); |
4820 for (size_t i = 0; i < dim; i++) | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4821 { Parameter *arg = Parameter::getNth(params, i); |
159 | 4822 assert(arg && arg->type); |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
4823 args->push(new Parameter(arg->storageClass, arg->type, NULL, NULL)); |
159 | 4824 } |
4825 tded = new TypeTuple(args); | |
4826 break; | |
4827 } | |
4828 case TOKreturn: | |
4829 /* Get the 'return type' for the function, | |
4830 * delegate, or pointer to function. | |
4831 */ | |
4832 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
|
4833 tded = ((TypeFunction *)targ)->next; |
159 | 4834 else if (targ->ty == Tdelegate) |
4835 tded = targ->next->next; | |
4836 else if (targ->ty == Tpointer && targ->next->ty == Tfunction) | |
4837 tded = targ->next->next; | |
4838 else | |
4839 goto Lno; | |
4840 break; | |
4841 | |
4842 default: | |
4843 assert(0); | |
4844 } | |
4845 goto Lyes; | |
4846 } | |
4847 else if (id && tspec) | |
4848 { | |
4849 /* Evaluate to TRUE if targ matches tspec. | |
4850 * If TRUE, declare id as an alias for the specialized type. | |
4851 */ | |
4852 | |
4853 MATCH m; | |
4854 TemplateTypeParameter tp(loc, id, NULL, NULL); | |
4855 | |
4856 TemplateParameters parameters; | |
4857 parameters.setDim(1); | |
4858 parameters.data[0] = (void *)&tp; | |
4859 | |
4860 Objects dedtypes; | |
4861 dedtypes.setDim(1); | |
4862 dedtypes.data[0] = NULL; | |
4863 | |
4864 m = targ->deduceType(NULL, tspec, ¶meters, &dedtypes); | |
4865 if (m == MATCHnomatch || | |
4866 (m != MATCHexact && tok == TOKequal)) | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4867 { |
159 | 4868 goto Lno; |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4869 } |
159 | 4870 else |
4871 { | |
4872 assert(dedtypes.dim == 1); | |
4873 tded = (Type *)dedtypes.data[0]; | |
4874 if (!tded) | |
4875 tded = targ; | |
4876 goto Lyes; | |
4877 } | |
4878 } | |
4879 else if (id) | |
4880 { | |
4881 /* Declare id as an alias for type targ. Evaluate to TRUE | |
4882 */ | |
4883 tded = targ; | |
4884 goto Lyes; | |
4885 } | |
4886 else if (tspec) | |
4887 { | |
4888 /* Evaluate to TRUE if targ matches tspec | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4889 * is(targ == tspec) |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
4890 * is(targ : tspec) |
159 | 4891 */ |
4892 tspec = tspec->semantic(loc, sc); | |
4893 //printf("targ = %s\n", targ->toChars()); | |
4894 //printf("tspec = %s\n", tspec->toChars()); | |
4895 if (tok == TOKcolon) | |
4896 { if (targ->implicitConvTo(tspec)) | |
4897 goto Lyes; | |
4898 else | |
4899 goto Lno; | |
4900 } | |
4901 else /* == */ | |
4902 { if (targ->equals(tspec)) | |
4903 goto Lyes; | |
4904 else | |
4905 goto Lno; | |
4906 } | |
4907 } | |
4908 | |
4909 Lyes: | |
4910 if (id) | |
4911 { | |
4912 Dsymbol *s = new AliasDeclaration(loc, id, tded); | |
4913 s->semantic(sc); | |
4914 sc->insert(s); | |
4915 if (sc->sd) | |
4916 s->addMember(sc, sc->sd, 1); | |
4917 } | |
4918 return new IntegerExp(1); | |
4919 | |
4920 Lno: | |
4921 return new IntegerExp(0); | |
4922 } | |
4923 | |
4924 void IsExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
4925 { | |
4926 buf->writestring("is("); | |
4927 targ->toCBuffer(buf, id, hgs); | |
4928 if (tok2 != TOKreserved) | |
4929 { | |
4930 buf->printf(" %s %s", Token::toChars(tok), Token::toChars(tok2)); | |
4931 } | |
4932 else if (tspec) | |
4933 { | |
4934 if (tok == TOKcolon) | |
4935 buf->writestring(" : "); | |
4936 else | |
4937 buf->writestring(" == "); | |
4938 tspec->toCBuffer(buf, NULL, hgs); | |
4939 } | |
336 | 4940 #if DMDV2 |
159 | 4941 if (parameters) |
4942 { // First parameter is already output, so start with second | |
4943 for (int i = 1; i < parameters->dim; i++) | |
4944 { | |
4945 buf->writeByte(','); | |
4946 TemplateParameter *tp = (TemplateParameter *)parameters->data[i]; | |
4947 tp->toCBuffer(buf, hgs); | |
4948 } | |
4949 } | |
4950 #endif | |
4951 buf->writeByte(')'); | |
4952 } | |
4953 | |
4954 | |
4955 /************************************************************/ | |
4956 | |
4957 UnaExp::UnaExp(Loc loc, enum TOK op, int size, Expression *e1) | |
4958 : Expression(loc, op, size) | |
4959 { | |
4960 this->e1 = e1; | |
4961 } | |
4962 | |
4963 Expression *UnaExp::syntaxCopy() | |
4964 { UnaExp *e; | |
4965 | |
4966 e = (UnaExp *)copy(); | |
4967 e->type = NULL; | |
4968 e->e1 = e->e1->syntaxCopy(); | |
4969 return e; | |
4970 } | |
4971 | |
4972 Expression *UnaExp::semantic(Scope *sc) | |
4973 { | |
4974 #if LOGSEMANTIC | |
4975 printf("UnaExp::semantic('%s')\n", toChars()); | |
4976 #endif | |
4977 e1 = e1->semantic(sc); | |
4978 // if (!e1->type) | |
4979 // error("%s has no value", e1->toChars()); | |
4980 return this; | |
4981 } | |
4982 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4983 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4984 int UnaExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4985 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4986 return e1->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4987 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4988 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
4989 |
159 | 4990 void UnaExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
4991 { | |
4992 buf->writestring(Token::toChars(op)); | |
4993 expToCBuffer(buf, hgs, e1, precedence[op]); | |
4994 } | |
4995 | |
4996 /************************************************************/ | |
4997 | |
4998 BinExp::BinExp(Loc loc, enum TOK op, int size, Expression *e1, Expression *e2) | |
4999 : Expression(loc, op, size) | |
5000 { | |
5001 this->e1 = e1; | |
5002 this->e2 = e2; | |
5003 } | |
5004 | |
5005 Expression *BinExp::syntaxCopy() | |
5006 { BinExp *e; | |
5007 | |
5008 e = (BinExp *)copy(); | |
5009 e->type = NULL; | |
5010 e->e1 = e->e1->syntaxCopy(); | |
5011 e->e2 = e->e2->syntaxCopy(); | |
5012 return e; | |
5013 } | |
5014 | |
5015 Expression *BinExp::semantic(Scope *sc) | |
5016 { | |
5017 #if LOGSEMANTIC | |
5018 printf("BinExp::semantic('%s')\n", toChars()); | |
5019 #endif | |
5020 e1 = e1->semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
5021 if (!e1->type && |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
5022 !(op == TOKassign && e1->op == TOKdottd)) // a.template = e2 |
159 | 5023 { |
5024 error("%s has no value", e1->toChars()); | |
5025 e1->type = Type::terror; | |
5026 } | |
5027 e2 = e2->semantic(sc); | |
5028 if (!e2->type) | |
5029 { | |
5030 error("%s has no value", e2->toChars()); | |
5031 e2->type = Type::terror; | |
5032 } | |
5033 return this; | |
5034 } | |
5035 | |
5036 Expression *BinExp::semanticp(Scope *sc) | |
5037 { | |
5038 BinExp::semantic(sc); | |
5039 e1 = resolveProperties(sc, e1); | |
5040 e2 = resolveProperties(sc, e2); | |
5041 return this; | |
5042 } | |
5043 | |
5044 /*************************** | |
5045 * Common semantic routine for some xxxAssignExp's. | |
5046 */ | |
5047 | |
5048 Expression *BinExp::commonSemanticAssign(Scope *sc) | |
5049 { Expression *e; | |
5050 | |
5051 if (!type) | |
5052 { | |
5053 BinExp::semantic(sc); | |
5054 e2 = resolveProperties(sc, e2); | |
5055 | |
5056 e = op_overload(sc); | |
5057 if (e) | |
5058 return e; | |
5059 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5060 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
|
5061 { // 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
|
5062 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
|
5063 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
|
5064 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
|
5065 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5066 |
159 | 5067 e1 = e1->modifiableLvalue(sc, e1); |
5068 e1->checkScalar(); | |
5069 type = e1->type; | |
5070 if (type->toBasetype()->ty == Tbool) | |
5071 { | |
5072 error("operator not allowed on bool expression %s", toChars()); | |
5073 } | |
5074 typeCombine(sc); | |
5075 e1->checkArithmetic(); | |
5076 e2->checkArithmetic(); | |
5077 | |
5078 if (op == TOKmodass && e2->type->iscomplex()) | |
5079 { error("cannot perform modulo complex arithmetic"); | |
1587 | 5080 return new ErrorExp(); |
159 | 5081 } |
5082 } | |
5083 return this; | |
5084 } | |
5085 | |
5086 Expression *BinExp::commonSemanticAssignIntegral(Scope *sc) | |
5087 { Expression *e; | |
5088 | |
5089 if (!type) | |
5090 { | |
5091 BinExp::semantic(sc); | |
5092 e2 = resolveProperties(sc, e2); | |
5093 | |
5094 e = op_overload(sc); | |
5095 if (e) | |
5096 return e; | |
5097 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5098 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
|
5099 { // 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
|
5100 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
|
5101 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
|
5102 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
|
5103 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5104 |
159 | 5105 e1 = e1->modifiableLvalue(sc, e1); |
5106 e1->checkScalar(); | |
5107 type = e1->type; | |
5108 if (type->toBasetype()->ty == Tbool) | |
5109 { | |
5110 e2 = e2->implicitCastTo(sc, type); | |
5111 } | |
5112 | |
5113 typeCombine(sc); | |
5114 e1->checkIntegral(); | |
5115 e2->checkIntegral(); | |
5116 } | |
5117 return this; | |
5118 } | |
5119 | |
5120 int BinExp::checkSideEffect(int flag) | |
5121 { | |
5122 if (op == TOKplusplus || | |
5123 op == TOKminusminus || | |
5124 op == TOKassign || | |
5125 op == TOKconstruct || | |
5126 op == TOKblit || | |
5127 op == TOKaddass || | |
5128 op == TOKminass || | |
5129 op == TOKcatass || | |
5130 op == TOKmulass || | |
5131 op == TOKdivass || | |
5132 op == TOKmodass || | |
5133 op == TOKshlass || | |
5134 op == TOKshrass || | |
5135 op == TOKushrass || | |
5136 op == TOKandass || | |
5137 op == TOKorass || | |
5138 op == TOKxorass || | |
5139 op == TOKin || | |
5140 op == TOKremove) | |
5141 return 1; | |
5142 return Expression::checkSideEffect(flag); | |
5143 } | |
5144 | |
1587 | 5145 // generate an error if this is a nonsensical *=,/=, or %=, eg real *= imaginary |
5146 void BinExp::checkComplexMulAssign() | |
5147 { | |
5148 // Any multiplication by an imaginary or complex number yields a complex result. | |
5149 // r *= c, i*=c, r*=i, i*=i are all forbidden operations. | |
5150 const char *opstr = Token::toChars(op); | |
5151 if ( e1->type->isreal() && e2->type->iscomplex()) | |
5152 { | |
5153 error("%s %s %s is undefined. Did you mean %s %s %s.re ?", | |
5154 e1->type->toChars(), opstr, e2->type->toChars(), | |
5155 e1->type->toChars(), opstr, e2->type->toChars()); | |
5156 } | |
5157 else if (e1->type->isimaginary() && e2->type->iscomplex()) | |
5158 { | |
5159 error("%s %s %s is undefined. Did you mean %s %s %s.im ?", | |
5160 e1->type->toChars(), opstr, e2->type->toChars(), | |
5161 e1->type->toChars(), opstr, e2->type->toChars()); | |
5162 } | |
5163 else if ((e1->type->isreal() || e1->type->isimaginary()) && | |
5164 e2->type->isimaginary()) | |
5165 { | |
5166 error("%s %s %s is an undefined operation", e1->type->toChars(), | |
5167 opstr, e2->type->toChars()); | |
5168 } | |
5169 } | |
5170 | |
5171 // generate an error if this is a nonsensical += or -=, eg real += imaginary | |
5172 void BinExp::checkComplexAddAssign() | |
5173 { | |
5174 // Addition or subtraction of a real and an imaginary is a complex result. | |
5175 // Thus, r+=i, r+=c, i+=r, i+=c are all forbidden operations. | |
5176 if ( (e1->type->isreal() && (e2->type->isimaginary() || e2->type->iscomplex())) || | |
5177 (e1->type->isimaginary() && (e2->type->isreal() || e2->type->iscomplex())) | |
5178 ) | |
5179 { | |
5180 error("%s %s %s is undefined (result is complex)", | |
5181 e1->type->toChars(), Token::toChars(op), e2->type->toChars()); | |
5182 } | |
5183 } | |
5184 | |
159 | 5185 void BinExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
5186 { | |
5187 expToCBuffer(buf, hgs, e1, precedence[op]); | |
5188 buf->writeByte(' '); | |
5189 buf->writestring(Token::toChars(op)); | |
5190 buf->writeByte(' '); | |
5191 expToCBuffer(buf, hgs, e2, (enum PREC)(precedence[op] + 1)); | |
5192 } | |
5193 | |
5194 int BinExp::isunsigned() | |
5195 { | |
5196 return e1->type->isunsigned() || e2->type->isunsigned(); | |
5197 } | |
5198 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5199 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5200 int BinExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5201 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5202 return e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5203 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5204 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5205 |
159 | 5206 void BinExp::incompatibleTypes() |
5207 { | |
5208 error("incompatible types for ((%s) %s (%s)): '%s' and '%s'", | |
5209 e1->toChars(), Token::toChars(op), e2->toChars(), | |
5210 e1->type->toChars(), e2->type->toChars()); | |
5211 } | |
5212 | |
5213 /************************************************************/ | |
5214 | |
5215 CompileExp::CompileExp(Loc loc, Expression *e) | |
5216 : UnaExp(loc, TOKmixin, sizeof(CompileExp), e) | |
5217 { | |
5218 } | |
5219 | |
5220 Expression *CompileExp::semantic(Scope *sc) | |
5221 { | |
5222 #if LOGSEMANTIC | |
5223 printf("CompileExp::semantic('%s')\n", toChars()); | |
5224 #endif | |
5225 UnaExp::semantic(sc); | |
5226 e1 = resolveProperties(sc, e1); | |
5227 e1 = e1->optimize(WANTvalue | WANTinterpret); | |
5228 if (e1->op != TOKstring) | |
5229 { error("argument to mixin must be a string, not (%s)", e1->toChars()); | |
5230 type = Type::terror; | |
5231 return this; | |
5232 } | |
5233 StringExp *se = (StringExp *)e1; | |
5234 se = se->toUTF8(sc); | |
5235 Parser p(sc->module, (unsigned char *)se->string, se->len, 0); | |
5236 p.loc = loc; | |
5237 p.nextToken(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5238 //printf("p.loc.linnum = %d\n", p.loc.linnum); |
159 | 5239 Expression *e = p.parseExpression(); |
5240 if (p.token.value != TOKeof) | |
5241 error("incomplete mixin expression (%s)", se->toChars()); | |
5242 return e->semantic(sc); | |
5243 } | |
5244 | |
5245 void CompileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5246 { | |
5247 buf->writestring("mixin("); | |
5248 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5249 buf->writeByte(')'); | |
5250 } | |
5251 | |
5252 /************************************************************/ | |
5253 | |
5254 FileExp::FileExp(Loc loc, Expression *e) | |
5255 : UnaExp(loc, TOKmixin, sizeof(FileExp), e) | |
5256 { | |
5257 } | |
5258 | |
5259 Expression *FileExp::semantic(Scope *sc) | |
5260 { char *name; | |
5261 StringExp *se; | |
5262 | |
5263 #if LOGSEMANTIC | |
5264 printf("FileExp::semantic('%s')\n", toChars()); | |
5265 #endif | |
5266 UnaExp::semantic(sc); | |
5267 e1 = resolveProperties(sc, e1); | |
5268 e1 = e1->optimize(WANTvalue); | |
5269 if (e1->op != TOKstring) | |
5270 { error("file name argument must be a string, not (%s)", e1->toChars()); | |
5271 goto Lerror; | |
5272 } | |
5273 se = (StringExp *)e1; | |
5274 se = se->toUTF8(sc); | |
5275 name = (char *)se->string; | |
5276 | |
5277 if (!global.params.fileImppath) | |
5278 { error("need -Jpath switch to import text file %s", name); | |
5279 goto Lerror; | |
5280 } | |
5281 | |
5282 if (name != FileName::name(name)) | |
5283 { error("use -Jpath switch to provide path for filename %s", name); | |
5284 goto Lerror; | |
5285 } | |
5286 | |
5287 name = FileName::searchPath(global.filePath, name, 0); | |
5288 if (!name) | |
5289 { error("file %s cannot be found, check -Jpath", se->toChars()); | |
5290 goto Lerror; | |
5291 } | |
5292 | |
5293 if (global.params.verbose) | |
1587 | 5294 printf("file %s\t(%s)\n", (char *)se->string, name); |
159 | 5295 |
5296 { File f(name); | |
5297 if (f.read()) | |
5298 { error("cannot read file %s", f.toChars()); | |
5299 goto Lerror; | |
5300 } | |
5301 else | |
5302 { | |
5303 f.ref = 1; | |
5304 se = new StringExp(loc, f.buffer, f.len); | |
5305 } | |
5306 } | |
5307 Lret: | |
5308 return se->semantic(sc); | |
5309 | |
5310 Lerror: | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5311 se = new StringExp(loc, (char *)""); |
159 | 5312 goto Lret; |
5313 } | |
5314 | |
5315 void FileExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5316 { | |
5317 buf->writestring("import("); | |
5318 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5319 buf->writeByte(')'); | |
5320 } | |
5321 | |
5322 /************************************************************/ | |
5323 | |
5324 AssertExp::AssertExp(Loc loc, Expression *e, Expression *msg) | |
5325 : UnaExp(loc, TOKassert, sizeof(AssertExp), e) | |
5326 { | |
5327 this->msg = msg; | |
5328 } | |
5329 | |
5330 Expression *AssertExp::syntaxCopy() | |
5331 { | |
5332 AssertExp *ae = new AssertExp(loc, e1->syntaxCopy(), | |
5333 msg ? msg->syntaxCopy() : NULL); | |
5334 return ae; | |
5335 } | |
5336 | |
5337 Expression *AssertExp::semantic(Scope *sc) | |
5338 { | |
5339 #if LOGSEMANTIC | |
5340 printf("AssertExp::semantic('%s')\n", toChars()); | |
5341 #endif | |
5342 UnaExp::semantic(sc); | |
5343 e1 = resolveProperties(sc, e1); | |
5344 // BUG: see if we can do compile time elimination of the Assert | |
5345 e1 = e1->optimize(WANTvalue); | |
5346 e1 = e1->checkToBoolean(); | |
5347 if (msg) | |
5348 { | |
5349 msg = msg->semantic(sc); | |
5350 msg = resolveProperties(sc, msg); | |
5351 msg = msg->implicitCastTo(sc, Type::tchar->arrayOf()); | |
5352 msg = msg->optimize(WANTvalue); | |
5353 } | |
5354 if (e1->isBool(FALSE)) | |
5355 { | |
5356 FuncDeclaration *fd = sc->parent->isFuncDeclaration(); | |
5357 fd->hasReturnExp |= 4; | |
5358 | |
5359 if (!global.params.useAssert) | |
5360 { Expression *e = new HaltExp(loc); | |
5361 e = e->semantic(sc); | |
5362 return e; | |
5363 } | |
5364 } | |
5365 type = Type::tvoid; | |
5366 return this; | |
5367 } | |
5368 | |
5369 int AssertExp::checkSideEffect(int flag) | |
5370 { | |
5371 return 1; | |
5372 } | |
5373 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5374 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5375 int AssertExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5376 { |
1587 | 5377 /* assert()s are non-recoverable errors, so functions that |
5378 * use them can be considered "nothrow" | |
5379 */ | |
5380 return 0; //(global.params.useAssert != 0); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5381 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5382 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5383 |
159 | 5384 void AssertExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
5385 { | |
5386 buf->writestring("assert("); | |
5387 expToCBuffer(buf, hgs, e1, PREC_assign); | |
5388 if (msg) | |
5389 { | |
5390 buf->writeByte(','); | |
5391 expToCBuffer(buf, hgs, msg, PREC_assign); | |
5392 } | |
5393 buf->writeByte(')'); | |
5394 } | |
5395 | |
5396 /************************************************************/ | |
5397 | |
5398 DotIdExp::DotIdExp(Loc loc, Expression *e, Identifier *ident) | |
5399 : UnaExp(loc, TOKdot, sizeof(DotIdExp), e) | |
5400 { | |
5401 this->ident = ident; | |
5402 } | |
5403 | |
5404 Expression *DotIdExp::semantic(Scope *sc) | |
5405 { Expression *e; | |
5406 Expression *eleft; | |
5407 Expression *eright; | |
5408 | |
5409 #if LOGSEMANTIC | |
5410 printf("DotIdExp::semantic(this = %p, '%s')\n", this, toChars()); | |
5411 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
5412 #endif | |
5413 | |
5414 //{ static int z; fflush(stdout); if (++z == 10) *(char*)0=0; } | |
5415 | |
5416 #if 0 | |
5417 /* Don't do semantic analysis if we'll be converting | |
5418 * it to a string. | |
5419 */ | |
5420 if (ident == Id::stringof) | |
5421 { char *s = e1->toChars(); | |
5422 e = new StringExp(loc, s, strlen(s), 'c'); | |
5423 e = e->semantic(sc); | |
5424 return e; | |
5425 } | |
5426 #endif | |
5427 | |
5428 /* Special case: rewrite this.id and super.id | |
5429 * to be classtype.id and baseclasstype.id | |
5430 * if we have no this pointer. | |
5431 */ | |
5432 if ((e1->op == TOKthis || e1->op == TOKsuper) && !hasThis(sc)) | |
5433 { ClassDeclaration *cd; | |
5434 StructDeclaration *sd; | |
5435 AggregateDeclaration *ad; | |
5436 | |
5437 ad = sc->getStructClassScope(); | |
5438 if (ad) | |
5439 { | |
5440 cd = ad->isClassDeclaration(); | |
5441 if (cd) | |
5442 { | |
5443 if (e1->op == TOKthis) | |
5444 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5445 e = typeDotIdExp(loc, cd->type, ident); |
159 | 5446 return e->semantic(sc); |
5447 } | |
5448 else if (cd->baseClass && e1->op == TOKsuper) | |
5449 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5450 e = typeDotIdExp(loc, cd->baseClass->type, ident); |
159 | 5451 return e->semantic(sc); |
5452 } | |
5453 } | |
5454 else | |
5455 { | |
5456 sd = ad->isStructDeclaration(); | |
5457 if (sd) | |
5458 { | |
5459 if (e1->op == TOKthis) | |
5460 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5461 e = typeDotIdExp(loc, sd->type, ident); |
159 | 5462 return e->semantic(sc); |
5463 } | |
5464 } | |
5465 } | |
5466 } | |
5467 } | |
5468 | |
5469 UnaExp::semantic(sc); | |
5470 | |
5471 if (e1->op == TOKdotexp) | |
5472 { | |
5473 DotExp *de = (DotExp *)e1; | |
5474 eleft = de->e1; | |
5475 eright = de->e2; | |
5476 } | |
5477 else | |
5478 { | |
5479 e1 = resolveProperties(sc, e1); | |
5480 eleft = NULL; | |
5481 eright = e1; | |
5482 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5483 #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
|
5484 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
|
5485 { /* '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
|
5486 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5487 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
|
5488 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
|
5489 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
|
5490 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
|
5491 { 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
|
5492 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
|
5493 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
|
5494 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
|
5495 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5496 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
|
5497 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
|
5498 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
|
5499 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5500 #endif |
159 | 5501 |
5502 if (e1->op == TOKtuple && ident == Id::length) | |
5503 { | |
5504 TupleExp *te = (TupleExp *)e1; | |
5505 e = new IntegerExp(loc, te->exps->dim, Type::tsize_t); | |
5506 return e; | |
5507 } | |
5508 | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5509 if (e1->op == TOKdottd) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5510 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5511 error("template %s does not have property %s", e1->toChars(), ident->toChars()); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5512 return e1; |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5513 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5514 |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5515 if (!e1->type) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5516 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5517 error("expression %s does not have property %s", e1->toChars(), ident->toChars()); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5518 return e1; |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5519 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5520 |
159 | 5521 if (eright->op == TOKimport) // also used for template alias's |
5522 { | |
5523 ScopeExp *ie = (ScopeExp *)eright; | |
5524 | |
846
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5525 /* 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
|
5526 * 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
|
5527 * 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
|
5528 */ |
bc982f1ad106
Merged DMD 1.037 frontend
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
841
diff
changeset
|
5529 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
|
5530 (ie->sds->isModule() && ie->sds != sc->module) ? 1 : 0); |
159 | 5531 if (s) |
5532 { | |
5533 s = s->toAlias(); | |
5534 checkDeprecated(sc, s); | |
5535 | |
5536 EnumMember *em = s->isEnumMember(); | |
5537 if (em) | |
5538 { | |
5539 e = em->value; | |
5540 e = e->semantic(sc); | |
5541 return e; | |
5542 } | |
5543 | |
5544 VarDeclaration *v = s->isVarDeclaration(); | |
5545 if (v) | |
5546 { | |
5547 //printf("DotIdExp:: Identifier '%s' is a variable, type '%s'\n", toChars(), v->type->toChars()); | |
5548 if (v->inuse) | |
5549 { | |
5550 error("circular reference to '%s'", v->toChars()); | |
5551 type = Type::tint32; | |
5552 return this; | |
5553 } | |
5554 type = v->type; | |
1499
df11cdec45a2
Another shot at fixing the issues with (constant) struct literals and their addresses. See DMD2682, #218, #324.
Christian Kamm <kamm incasoftware de>
parents:
1372
diff
changeset
|
5555 if (v->isSameAsInitializer()) |
159 | 5556 { |
5557 if (v->init) | |
5558 { | |
5559 ExpInitializer *ei = v->init->isExpInitializer(); | |
5560 if (ei) | |
5561 { | |
5562 //printf("\tei: %p (%s)\n", ei->exp, ei->exp->toChars()); | |
5563 //ei->exp = ei->exp->semantic(sc); | |
5564 if (ei->exp->type == type) | |
5565 { | |
5566 e = ei->exp->copy(); // make copy so we can change loc | |
5567 e->loc = loc; | |
5568 return e; | |
5569 } | |
5570 } | |
5571 } | |
5572 else if (type->isscalar()) | |
5573 { | |
5574 e = type->defaultInit(); | |
5575 e->loc = loc; | |
5576 return e; | |
5577 } | |
5578 } | |
5579 if (v->needThis()) | |
5580 { | |
5581 if (!eleft) | |
5582 eleft = new ThisExp(loc); | |
5583 e = new DotVarExp(loc, eleft, v); | |
5584 e = e->semantic(sc); | |
5585 } | |
5586 else | |
5587 { | |
5588 e = new VarExp(loc, v); | |
5589 if (eleft) | |
5590 { e = new CommaExp(loc, eleft, e); | |
5591 e->type = v->type; | |
5592 } | |
5593 } | |
5594 return e->deref(); | |
5595 } | |
5596 | |
5597 FuncDeclaration *f = s->isFuncDeclaration(); | |
5598 if (f) | |
5599 { | |
5600 //printf("it's a function\n"); | |
5601 if (f->needThis()) | |
5602 { | |
5603 if (!eleft) | |
5604 eleft = new ThisExp(loc); | |
5605 e = new DotVarExp(loc, eleft, f); | |
5606 e = e->semantic(sc); | |
5607 } | |
5608 else | |
5609 { | |
5610 e = new VarExp(loc, f); | |
5611 if (eleft) | |
5612 { e = new CommaExp(loc, eleft, e); | |
5613 e->type = f->type; | |
5614 } | |
5615 } | |
5616 return e; | |
5617 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5618 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5619 OverloadSet *o = s->isOverloadSet(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5620 if (o) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5621 { //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
|
5622 return new OverExp(o); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5623 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5624 #endif |
159 | 5625 |
5626 Type *t = s->getType(); | |
5627 if (t) | |
5628 { | |
5629 return new TypeExp(loc, t); | |
5630 } | |
5631 | |
336 | 5632 TupleDeclaration *tup = s->isTupleDeclaration(); |
5633 if (tup) | |
5634 { | |
5635 if (eleft) | |
5636 error("cannot have e.tuple"); | |
5637 e = new TupleExp(loc, tup); | |
5638 e = e->semantic(sc); | |
5639 return e; | |
5640 } | |
5641 | |
159 | 5642 ScopeDsymbol *sds = s->isScopeDsymbol(); |
5643 if (sds) | |
5644 { | |
5645 //printf("it's a ScopeDsymbol\n"); | |
5646 e = new ScopeExp(loc, sds); | |
5647 e = e->semantic(sc); | |
5648 if (eleft) | |
5649 e = new DotExp(loc, eleft, e); | |
5650 return e; | |
5651 } | |
5652 | |
5653 Import *imp = s->isImport(); | |
5654 if (imp) | |
5655 { | |
5656 ScopeExp *ie; | |
5657 | |
5658 ie = new ScopeExp(loc, imp->pkg); | |
5659 return ie->semantic(sc); | |
5660 } | |
5661 | |
5662 // BUG: handle other cases like in IdentifierExp::semantic() | |
5663 #ifdef DEBUG | |
5664 printf("s = '%s', kind = '%s'\n", s->toChars(), s->kind()); | |
5665 #endif | |
5666 assert(0); | |
5667 } | |
5668 else if (ident == Id::stringof) | |
5669 { char *s = ie->toChars(); | |
5670 e = new StringExp(loc, s, strlen(s), 'c'); | |
5671 e = e->semantic(sc); | |
5672 return e; | |
5673 } | |
5674 error("undefined identifier %s", toChars()); | |
5675 type = Type::tvoid; | |
5676 return this; | |
5677 } | |
5678 else if (e1->type->ty == Tpointer && | |
5679 ident != Id::init && ident != Id::__sizeof && | |
5680 ident != Id::alignof && ident != Id::offsetof && | |
5681 ident != Id::mangleof && ident != Id::stringof) | |
1587 | 5682 { /* Rewrite: |
5683 * p.ident | |
5684 * as: | |
5685 * (*p).ident | |
5686 */ | |
159 | 5687 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
|
5688 e->type = ((TypePointer *)e1->type)->next; |
159 | 5689 return e->type->dotExp(sc, e, ident); |
5690 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5691 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5692 else if (t1b->ty == Tarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5693 t1b->ty == Tsarray || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5694 t1b->ty == Taarray) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5695 { /* 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
|
5696 * e1.ident |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5697 * as: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5698 * .ident(e1) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5699 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5700 unsigned errors = global.errors; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5701 global.gag++; |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
5702 Type *t1 = e1->type; |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5703 e = e1->type->dotExp(sc, e1, ident); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5704 global.gag--; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5705 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
|
5706 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5707 global.errors = errors; |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
5708 e1->type = t1; // kludge to restore type |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5709 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
|
5710 e = new CallExp(loc, e, e1); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5711 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5712 e = e->semantic(sc); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5713 return e; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5714 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5715 #endif |
159 | 5716 else |
5717 { | |
5718 e = e1->type->dotExp(sc, e1, ident); | |
5719 e = e->semantic(sc); | |
5720 return e; | |
5721 } | |
5722 } | |
5723 | |
5724 void DotIdExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5725 { | |
5726 //printf("DotIdExp::toCBuffer()\n"); | |
5727 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5728 buf->writeByte('.'); | |
5729 buf->writestring(ident->toChars()); | |
5730 } | |
5731 | |
5732 /********************** DotTemplateExp ***********************************/ | |
5733 | |
5734 // Mainly just a placeholder | |
5735 | |
5736 DotTemplateExp::DotTemplateExp(Loc loc, Expression *e, TemplateDeclaration *td) | |
5737 : UnaExp(loc, TOKdottd, sizeof(DotTemplateExp), e) | |
5738 | |
5739 { | |
5740 this->td = td; | |
5741 } | |
5742 | |
5743 void DotTemplateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5744 { | |
5745 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5746 buf->writeByte('.'); | |
5747 buf->writestring(td->toChars()); | |
5748 } | |
5749 | |
5750 | |
5751 /************************************************************/ | |
5752 | |
5753 DotVarExp::DotVarExp(Loc loc, Expression *e, Declaration *v) | |
5754 : UnaExp(loc, TOKdotvar, sizeof(DotVarExp), e) | |
5755 { | |
5756 //printf("DotVarExp()\n"); | |
5757 this->var = v; | |
5758 } | |
5759 | |
5760 Expression *DotVarExp::semantic(Scope *sc) | |
5761 { | |
5762 #if LOGSEMANTIC | |
5763 printf("DotVarExp::semantic('%s')\n", toChars()); | |
5764 #endif | |
5765 if (!type) | |
5766 { | |
5767 var = var->toAlias()->isDeclaration(); | |
5768 | |
5769 TupleDeclaration *tup = var->isTupleDeclaration(); | |
5770 if (tup) | |
5771 { /* Replace: | |
5772 * e1.tuple(a, b, c) | |
5773 * with: | |
5774 * tuple(e1.a, e1.b, e1.c) | |
5775 */ | |
5776 Expressions *exps = new Expressions; | |
5777 | |
5778 exps->reserve(tup->objects->dim); | |
5779 for (size_t i = 0; i < tup->objects->dim; i++) | |
5780 { Object *o = (Object *)tup->objects->data[i]; | |
5781 if (o->dyncast() != DYNCAST_EXPRESSION) | |
5782 { | |
5783 error("%s is not an expression", o->toChars()); | |
5784 } | |
5785 else | |
5786 { | |
5787 Expression *e = (Expression *)o; | |
5788 if (e->op != TOKdsymbol) | |
5789 error("%s is not a member", e->toChars()); | |
5790 else | |
5791 { DsymbolExp *ve = (DsymbolExp *)e; | |
5792 | |
5793 e = new DotVarExp(loc, e1, ve->s->isDeclaration()); | |
5794 exps->push(e); | |
5795 } | |
5796 } | |
5797 } | |
5798 Expression *e = new TupleExp(loc, exps); | |
5799 e = e->semantic(sc); | |
5800 return e; | |
5801 } | |
5802 | |
5803 e1 = e1->semantic(sc); | |
5804 type = var->type; | |
5805 if (!type && global.errors) | |
5806 { // var is goofed up, just return 0 | |
1587 | 5807 return new ErrorExp(); |
159 | 5808 } |
5809 assert(type); | |
5810 | |
5811 if (!var->isFuncDeclaration()) // for functions, do checks after overload resolution | |
5812 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5813 Dsymbol *vparent = var->toParent(); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
5814 AggregateDeclaration *ad = vparent ? vparent->isAggregateDeclaration() : NULL; |
336 | 5815 e1 = getRightThis(loc, sc, ad, e1, var); |
5816 if (!sc->noaccesscheck) | |
5817 accessCheck(loc, sc, e1, var); | |
5818 | |
5819 VarDeclaration *v = var->isVarDeclaration(); | |
1499
df11cdec45a2
Another shot at fixing the issues with (constant) struct literals and their addresses. See DMD2682, #218, #324.
Christian Kamm <kamm incasoftware de>
parents:
1372
diff
changeset
|
5820 if (v && v->isSameAsInitializer()) |
336 | 5821 { ExpInitializer *ei = v->getExpInitializer(); |
5822 if (ei) | |
5823 { Expression *e = ei->exp->copy(); | |
5824 e = e->semantic(sc); | |
5825 return e; | |
159 | 5826 } |
5827 } | |
5828 } | |
5829 } | |
5830 //printf("-DotVarExp::semantic('%s')\n", toChars()); | |
5831 return this; | |
5832 } | |
5833 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5834 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5835 int DotVarExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5836 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5837 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5838 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5839 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5840 |
159 | 5841 Expression *DotVarExp::toLvalue(Scope *sc, Expression *e) |
5842 { | |
5843 //printf("DotVarExp::toLvalue(%s)\n", toChars()); | |
5844 return this; | |
5845 } | |
5846 | |
5847 Expression *DotVarExp::modifiableLvalue(Scope *sc, Expression *e) | |
5848 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
5849 #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
|
5850 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
|
5851 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
|
5852 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
|
5853 #endif |
159 | 5854 |
5855 if (var->isCtorinit()) | |
5856 { // It's only modifiable if inside the right constructor | |
5857 Dsymbol *s = sc->func; | |
5858 while (1) | |
5859 { | |
5860 FuncDeclaration *fd = NULL; | |
5861 if (s) | |
5862 fd = s->isFuncDeclaration(); | |
5863 if (fd && | |
5864 ((fd->isCtorDeclaration() && var->storage_class & STCfield) || | |
5865 (fd->isStaticCtorDeclaration() && !(var->storage_class & STCfield))) && | |
5866 fd->toParent() == var->toParent() && | |
5867 e1->op == TOKthis | |
5868 ) | |
5869 { | |
5870 VarDeclaration *v = var->isVarDeclaration(); | |
5871 assert(v); | |
5872 v->ctorinit = 1; | |
5873 //printf("setting ctorinit\n"); | |
5874 } | |
5875 else | |
5876 { | |
5877 if (s) | |
5878 { s = s->toParent2(); | |
5879 continue; | |
5880 } | |
5881 else | |
5882 { | |
5883 const char *p = var->isStatic() ? "static " : ""; | |
5884 error("can only initialize %sconst member %s inside %sconstructor", | |
5885 p, var->toChars(), p); | |
5886 } | |
5887 } | |
5888 break; | |
5889 } | |
5890 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5891 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5892 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5893 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5894 Type *t1 = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5895 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5896 if (!t1->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5897 (t1->ty == Tpointer && !t1->nextOf()->isMutable()) || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5898 !var->type->isMutable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5899 !var->type->isAssignable() || |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5900 var->storage_class & STCmanifest |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5901 ) |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
5902 error("cannot modify const/immutable expression %s", toChars()); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5903 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
5904 #endif |
159 | 5905 return this; |
5906 } | |
5907 | |
5908 void DotVarExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
5909 { | |
5910 expToCBuffer(buf, hgs, e1, PREC_primary); | |
5911 buf->writeByte('.'); | |
5912 buf->writestring(var->toChars()); | |
5913 } | |
5914 | |
5915 /************************************************************/ | |
5916 | |
5917 /* Things like: | |
5918 * foo.bar!(args) | |
5919 */ | |
5920 | |
5921 DotTemplateInstanceExp::DotTemplateInstanceExp(Loc loc, Expression *e, TemplateInstance *ti) | |
5922 : UnaExp(loc, TOKdotti, sizeof(DotTemplateInstanceExp), e) | |
5923 { | |
5924 //printf("DotTemplateInstanceExp()\n"); | |
5925 this->ti = ti; | |
5926 } | |
5927 | |
5928 Expression *DotTemplateInstanceExp::syntaxCopy() | |
5929 { | |
5930 DotTemplateInstanceExp *de = new DotTemplateInstanceExp(loc, | |
5931 e1->syntaxCopy(), | |
5932 (TemplateInstance *)ti->syntaxCopy(NULL)); | |
5933 return de; | |
5934 } | |
5935 | |
5936 Expression *DotTemplateInstanceExp::semantic(Scope *sc) | |
1609
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5937 { |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5938 #if 1 |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5939 #if LOGSEMANTIC |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5940 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5941 #endif |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5942 Expression *eleft; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5943 Expression *e = new DotIdExp(loc, e1, ti->name); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5944 L1: |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5945 e = e->semantic(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5946 if (e->op == TOKdottd) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5947 { |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5948 DotTemplateExp *dte = (DotTemplateExp *)e; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5949 TemplateDeclaration *td = dte->td; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5950 eleft = dte->e1; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5951 ti->tempdecl = td; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5952 ti->semantic(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5953 Dsymbol *s = ti->inst->toAlias(); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5954 Declaration *v = s->isDeclaration(); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5955 if (v) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5956 { e = new DotVarExp(loc, eleft, v); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5957 e = e->semantic(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5958 return e; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5959 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5960 e = new ScopeExp(loc, ti); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5961 e = new DotExp(loc, eleft, e); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5962 e = e->semantic(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5963 return e; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5964 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5965 else if (e->op == TOKimport) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5966 { ScopeExp *se = (ScopeExp *)e; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5967 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5968 if (!td) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5969 { error("%s is not a template", e->toChars()); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5970 return new ErrorExp(); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5971 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5972 ti->tempdecl = td; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5973 e = new ScopeExp(loc, ti); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5974 e = e->semantic(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5975 return e; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5976 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5977 else if (e->op == TOKdotexp) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5978 { DotExp *de = (DotExp *)e; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5979 |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5980 if (de->e2->op == TOKimport) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5981 { // This should *really* be moved to ScopeExp::semantic() |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5982 ScopeExp *se = (ScopeExp *)de->e2; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5983 de->e2 = new DsymbolExp(loc, se->sds); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5984 de->e2 = de->e2->semantic(sc); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5985 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5986 |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5987 if (de->e2->op == TOKtemplate) |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5988 { TemplateExp *te = (TemplateExp *) de->e2; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5989 e = new DotTemplateExp(loc,de->e1,te->td); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5990 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5991 goto L1; |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5992 } |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5993 error("%s isn't a template", e->toChars()); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5994 return new ErrorExp(); |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5995 #else |
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
5996 Dsymbol *s; |
159 | 5997 Dsymbol *s2; |
5998 TemplateDeclaration *td; | |
5999 Expression *e; | |
6000 Identifier *id; | |
6001 Type *t1; | |
6002 Expression *eleft = NULL; | |
6003 Expression *eright; | |
6004 | |
6005 #if LOGSEMANTIC | |
6006 printf("DotTemplateInstanceExp::semantic('%s')\n", toChars()); | |
6007 #endif | |
6008 //e1->print(); | |
6009 //print(); | |
6010 e1 = e1->semantic(sc); | |
6011 t1 = e1->type; | |
6012 if (t1) | |
6013 t1 = t1->toBasetype(); | |
6014 //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
|
6015 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6016 /* 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
|
6017 * 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
|
6018 * 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
|
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 |
159 | 6021 if (e1->op == TOKdotexp) |
6022 { DotExp *de = (DotExp *)e1; | |
6023 eleft = de->e1; | |
6024 eright = de->e2; | |
6025 } | |
6026 else | |
6027 { eleft = NULL; | |
6028 eright = e1; | |
6029 } | |
6030 if (eright->op == TOKimport) | |
6031 { | |
6032 s = ((ScopeExp *)eright)->sds; | |
6033 } | |
6034 else if (e1->op == TOKtype) | |
6035 { | |
6036 s = t1->isClassHandle(); | |
6037 if (!s) | |
6038 { if (t1->ty == Tstruct) | |
6039 s = ((TypeStruct *)t1)->sym; | |
6040 else | |
6041 goto L1; | |
6042 } | |
6043 } | |
6044 else if (t1 && (t1->ty == Tstruct || t1->ty == Tclass)) | |
6045 { | |
6046 s = t1->toDsymbol(sc); | |
6047 eleft = e1; | |
6048 } | |
6049 else if (t1 && t1->ty == Tpointer) | |
6050 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6051 t1 = ((TypePointer *)t1)->next->toBasetype(); |
159 | 6052 if (t1->ty != Tstruct) |
6053 goto L1; | |
6054 s = t1->toDsymbol(sc); | |
6055 eleft = e1; | |
6056 } | |
6057 else | |
6058 { | |
6059 L1: | |
6060 error("template %s is not a member of %s", ti->toChars(), e1->toChars()); | |
6061 goto Lerr; | |
6062 } | |
6063 | |
6064 assert(s); | |
6065 id = ti->name; | |
6066 s2 = s->search(loc, id, 0); | |
6067 if (!s2) | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
6068 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
6069 if (!s->ident) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
6070 error("template identifier %s is not a member of undefined %s", id->toChars(), s->kind()); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
6071 else |
1258
192529570813
Trivial fix for bug #265
Christian Kamm <kamm incasoftware de>
parents:
1228
diff
changeset
|
6072 error("template identifier %s is not a member of %s %s", id->toChars(), s->kind(), s->ident->toChars()); |
159 | 6073 goto Lerr; |
6074 } | |
6075 s = s2; | |
6076 s->semantic(sc); | |
6077 s = s->toAlias(); | |
6078 td = s->isTemplateDeclaration(); | |
6079 if (!td) | |
6080 { | |
6081 error("%s is not a template", id->toChars()); | |
6082 goto Lerr; | |
6083 } | |
6084 if (global.errors) | |
6085 goto Lerr; | |
6086 | |
6087 ti->tempdecl = td; | |
6088 | |
6089 if (eleft) | |
6090 { Declaration *v; | |
6091 | |
6092 ti->semantic(sc); | |
6093 s = ti->inst->toAlias(); | |
6094 v = s->isDeclaration(); | |
6095 if (v) | |
6096 { e = new DotVarExp(loc, eleft, v); | |
6097 e = e->semantic(sc); | |
6098 return e; | |
6099 } | |
6100 } | |
6101 | |
6102 e = new ScopeExp(loc, ti); | |
6103 if (eleft) | |
6104 { | |
6105 e = new DotExp(loc, eleft, e); | |
6106 } | |
6107 e = e->semantic(sc); | |
6108 return e; | |
6109 | |
6110 Lerr: | |
336 | 6111 return new IntegerExp(loc, 0, Type::tint32); |
1609
1d0220dd613a
Merge DMD r274: harmonization
Leandro Lucarella <llucax@gmail.com>
parents:
1607
diff
changeset
|
6112 #endif |
159 | 6113 } |
6114 | |
6115 void DotTemplateInstanceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6116 { | |
6117 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6118 buf->writeByte('.'); | |
6119 ti->toCBuffer(buf, hgs); | |
6120 } | |
6121 | |
6122 /************************************************************/ | |
6123 | |
6124 DelegateExp::DelegateExp(Loc loc, Expression *e, FuncDeclaration *f) | |
6125 : UnaExp(loc, TOKdelegate, sizeof(DelegateExp), e) | |
6126 { | |
6127 this->func = f; | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1258
diff
changeset
|
6128 m = NULL; |
159 | 6129 } |
6130 | |
6131 Expression *DelegateExp::semantic(Scope *sc) | |
6132 { | |
6133 #if LOGSEMANTIC | |
6134 printf("DelegateExp::semantic('%s')\n", toChars()); | |
6135 #endif | |
6136 if (!type) | |
6137 { | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1258
diff
changeset
|
6138 m = sc->module; |
159 | 6139 e1 = e1->semantic(sc); |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6140 // 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
|
6141 type = new TypeDelegate(func->type->syntaxCopy()); |
159 | 6142 type = type->semantic(loc, sc); |
6143 AggregateDeclaration *ad = func->toParent()->isAggregateDeclaration(); | |
336 | 6144 if (func->needThis()) |
6145 e1 = getRightThis(loc, sc, ad, e1, func); | |
159 | 6146 } |
6147 return this; | |
6148 } | |
6149 | |
6150 void DelegateExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6151 { | |
6152 buf->writeByte('&'); | |
6153 if (!func->isNested()) | |
6154 { | |
6155 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6156 buf->writeByte('.'); | |
6157 } | |
6158 buf->writestring(func->toChars()); | |
6159 } | |
6160 | |
6161 /************************************************************/ | |
6162 | |
6163 DotTypeExp::DotTypeExp(Loc loc, Expression *e, Dsymbol *s) | |
6164 : UnaExp(loc, TOKdottype, sizeof(DotTypeExp), e) | |
6165 { | |
6166 this->sym = s; | |
6167 this->type = s->getType(); | |
6168 } | |
6169 | |
6170 Expression *DotTypeExp::semantic(Scope *sc) | |
6171 { | |
6172 #if LOGSEMANTIC | |
6173 printf("DotTypeExp::semantic('%s')\n", toChars()); | |
6174 #endif | |
6175 UnaExp::semantic(sc); | |
6176 return this; | |
6177 } | |
6178 | |
6179 void DotTypeExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
6180 { | |
6181 expToCBuffer(buf, hgs, e1, PREC_primary); | |
6182 buf->writeByte('.'); | |
6183 buf->writestring(sym->toChars()); | |
6184 } | |
6185 | |
6186 /************************************************************/ | |
6187 | |
6188 CallExp::CallExp(Loc loc, Expression *e, Expressions *exps) | |
6189 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
6190 { | |
6191 this->arguments = exps; | |
6192 } | |
6193 | |
6194 CallExp::CallExp(Loc loc, Expression *e) | |
6195 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
6196 { | |
6197 this->arguments = NULL; | |
6198 } | |
6199 | |
6200 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1) | |
6201 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
6202 { | |
6203 Expressions *arguments = new Expressions(); | |
1587 | 6204 if (earg1) |
6205 { arguments->setDim(1); | |
6206 arguments->data[0] = (void *)earg1; | |
6207 } | |
159 | 6208 this->arguments = arguments; |
6209 } | |
6210 | |
6211 CallExp::CallExp(Loc loc, Expression *e, Expression *earg1, Expression *earg2) | |
6212 : UnaExp(loc, TOKcall, sizeof(CallExp), e) | |
6213 { | |
6214 Expressions *arguments = new Expressions(); | |
6215 arguments->setDim(2); | |
6216 arguments->data[0] = (void *)earg1; | |
6217 arguments->data[1] = (void *)earg2; | |
6218 | |
6219 this->arguments = arguments; | |
6220 } | |
6221 | |
6222 Expression *CallExp::syntaxCopy() | |
6223 { | |
6224 return new CallExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
6225 } | |
6226 | |
6227 | |
6228 Expression *CallExp::semantic(Scope *sc) | |
6229 { | |
6230 TypeFunction *tf; | |
6231 FuncDeclaration *f; | |
6232 int i; | |
6233 Type *t1; | |
6234 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
|
6235 Objects *targsi = NULL; // initial list of template arguments |
159 | 6236 |
6237 #if LOGSEMANTIC | |
6238 printf("CallExp::semantic() %s\n", toChars()); | |
6239 #endif | |
6240 if (type) | |
6241 return this; // semantic() already run | |
6242 #if 0 | |
6243 if (arguments && arguments->dim) | |
6244 { | |
6245 Expression *earg = (Expression *)arguments->data[0]; | |
6246 earg->print(); | |
6247 if (earg->type) earg->type->print(); | |
6248 } | |
6249 #endif | |
6250 | |
6251 if (e1->op == TOKdelegate) | |
6252 { DelegateExp *de = (DelegateExp *)e1; | |
6253 | |
6254 e1 = new DotVarExp(de->loc, de->e1, de->func); | |
6255 return semantic(sc); | |
6256 } | |
6257 | |
6258 /* Transform: | |
6259 * array.id(args) into id(array,args) | |
6260 * aa.remove(arg) into delete aa[arg] | |
6261 */ | |
6262 if (e1->op == TOKdot) | |
6263 { | |
6264 // BUG: we should handle array.a.b.c.e(args) too | |
6265 | |
6266 DotIdExp *dotid = (DotIdExp *)(e1); | |
6267 dotid->e1 = dotid->e1->semantic(sc); | |
6268 assert(dotid->e1); | |
6269 if (dotid->e1->type) | |
6270 { | |
6271 TY e1ty = dotid->e1->type->toBasetype()->ty; | |
6272 if (e1ty == Taarray && dotid->ident == Id::remove) | |
6273 { | |
6274 if (!arguments || arguments->dim != 1) | |
6275 { error("expected key as argument to aa.remove()"); | |
6276 goto Lagain; | |
6277 } | |
6278 Expression *key = (Expression *)arguments->data[0]; | |
6279 key = key->semantic(sc); | |
6280 key = resolveProperties(sc, key); | |
6281 key->rvalue(); | |
6282 | |
6283 TypeAArray *taa = (TypeAArray *)dotid->e1->type->toBasetype(); | |
6284 key = key->implicitCastTo(sc, taa->index); | |
6285 key = key->implicitCastTo(sc, taa->key); | |
6286 | |
6287 return new RemoveExp(loc, dotid->e1, key); | |
6288 } | |
6289 else if (e1ty == Tarray || e1ty == Tsarray || e1ty == Taarray) | |
6290 { | |
6291 if (!arguments) | |
6292 arguments = new Expressions(); | |
6293 arguments->shift(dotid->e1); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6294 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6295 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
|
6296 #else |
159 | 6297 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
|
6298 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6299 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6300 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6301 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6302 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6303 #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
|
6304 /* 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
|
6305 * 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
|
6306 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6307 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
|
6308 { 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
|
6309 TemplateInstance *ti = se->sds->isTemplateInstance(); |
1587 | 6310 if (ti && !ti->semanticRun) |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6311 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6312 /* 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
|
6313 * 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
|
6314 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6315 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
|
6316 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
|
6317 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
|
6318 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
|
6319 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
|
6320 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
|
6321 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6322 /* 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
|
6323 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6324 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
|
6325 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
|
6326 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
|
6327 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6328 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6329 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6330 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6331 /* 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
|
6332 * 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
|
6333 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6334 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
|
6335 { 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
|
6336 TemplateInstance *ti = se->ti; |
1587 | 6337 if (!ti->semanticRun) |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6338 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6339 /* 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
|
6340 * 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
|
6341 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6342 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
|
6343 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
|
6344 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
|
6345 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
|
6346 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
|
6347 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
|
6348 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
|
6349 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6350 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
|
6351 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
|
6352 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
|
6353 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6354 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6355 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
|
6356 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6357 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6358 #endif |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6359 |
159 | 6360 istemp = 0; |
6361 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
|
6362 //printf("Lagain: %s\n", toChars()); |
159 | 6363 f = NULL; |
6364 if (e1->op == TOKthis || e1->op == TOKsuper) | |
6365 { | |
6366 // semantic() run later for these | |
6367 } | |
6368 else | |
6369 { | |
6370 UnaExp::semantic(sc); | |
6371 | |
6372 /* Look for e1 being a lazy parameter | |
6373 */ | |
6374 if (e1->op == TOKvar) | |
6375 { VarExp *ve = (VarExp *)e1; | |
6376 | |
6377 if (ve->var->storage_class & STClazy) | |
6378 { | |
6379 TypeFunction *tf = new TypeFunction(NULL, ve->var->type, 0, LINKd); | |
6380 TypeDelegate *t = new TypeDelegate(tf); | |
6381 ve->type = t->semantic(loc, sc); | |
6382 } | |
6383 } | |
6384 | |
6385 if (e1->op == TOKimport) | |
6386 { // Perhaps this should be moved to ScopeExp::semantic() | |
6387 ScopeExp *se = (ScopeExp *)e1; | |
6388 e1 = new DsymbolExp(loc, se->sds); | |
6389 e1 = e1->semantic(sc); | |
6390 } | |
6391 #if 1 // patch for #540 by Oskar Linde | |
6392 else if (e1->op == TOKdotexp) | |
6393 { | |
6394 DotExp *de = (DotExp *) e1; | |
6395 | |
6396 if (de->e2->op == TOKimport) | |
6397 { // This should *really* be moved to ScopeExp::semantic() | |
6398 ScopeExp *se = (ScopeExp *)de->e2; | |
6399 de->e2 = new DsymbolExp(loc, se->sds); | |
6400 de->e2 = de->e2->semantic(sc); | |
6401 } | |
6402 | |
6403 if (de->e2->op == TOKtemplate) | |
6404 { TemplateExp *te = (TemplateExp *) de->e2; | |
6405 e1 = new DotTemplateExp(loc,de->e1,te->td); | |
6406 } | |
6407 } | |
6408 #endif | |
6409 } | |
6410 | |
6411 if (e1->op == TOKcomma) | |
6412 { | |
6413 CommaExp *ce = (CommaExp *)e1; | |
6414 | |
6415 e1 = ce->e2; | |
6416 e1->type = ce->type; | |
6417 ce->e2 = this; | |
6418 ce->type = NULL; | |
6419 return ce->semantic(sc); | |
6420 } | |
6421 | |
6422 t1 = NULL; | |
6423 if (e1->type) | |
6424 t1 = e1->type->toBasetype(); | |
6425 | |
6426 // Check for call operator overload | |
6427 if (t1) | |
6428 { AggregateDeclaration *ad; | |
6429 | |
6430 if (t1->ty == Tstruct) | |
6431 { | |
6432 ad = ((TypeStruct *)t1)->sym; | |
1587 | 6433 #if DMDV2 |
6434 // First look for constructor | |
6435 if (ad->ctor && arguments && arguments->dim) | |
6436 { | |
6437 // Create variable that will get constructed | |
6438 Identifier *idtmp = Lexer::uniqueId("__ctmp"); | |
6439 VarDeclaration *tmp = new VarDeclaration(loc, t1, idtmp, NULL); | |
6440 Expression *av = new DeclarationExp(loc, tmp); | |
6441 av = new CommaExp(loc, av, new VarExp(loc, tmp)); | |
6442 | |
6443 Expression *e; | |
6444 CtorDeclaration *cf = ad->ctor->isCtorDeclaration(); | |
6445 if (cf) | |
6446 e = new DotVarExp(loc, av, cf, 1); | |
6447 else | |
6448 { TemplateDeclaration *td = ad->ctor->isTemplateDeclaration(); | |
6449 assert(td); | |
6450 e = new DotTemplateExp(loc, av, td); | |
6451 } | |
6452 e = new CallExp(loc, e, arguments); | |
6453 #if !STRUCTTHISREF | |
6454 /* Constructors return a pointer to the instance | |
6455 */ | |
6456 e = new PtrExp(loc, e); | |
6457 #endif | |
6458 e = e->semantic(sc); | |
6459 return e; | |
6460 } | |
6461 #endif | |
6462 // No constructor, look for overload of opCall | |
159 | 6463 if (search_function(ad, Id::call)) |
6464 goto L1; // overload of opCall, therefore it's a call | |
6465 | |
6466 if (e1->op != TOKtype) | |
6467 error("%s %s does not overload ()", ad->kind(), ad->toChars()); | |
6468 /* It's a struct literal | |
6469 */ | |
6470 Expression *e = new StructLiteralExp(loc, (StructDeclaration *)ad, arguments); | |
6471 e = e->semantic(sc); | |
6472 e->type = e1->type; // in case e1->type was a typedef | |
6473 return e; | |
6474 } | |
6475 else if (t1->ty == Tclass) | |
6476 { | |
6477 ad = ((TypeClass *)t1)->sym; | |
6478 goto L1; | |
6479 L1: | |
6480 // Rewrite as e1.call(arguments) | |
6481 Expression *e = new DotIdExp(loc, e1, Id::call); | |
6482 e = new CallExp(loc, e, arguments); | |
6483 e = e->semantic(sc); | |
6484 return e; | |
6485 } | |
6486 } | |
6487 | |
6488 arrayExpressionSemantic(arguments, sc); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
6489 preFunctionParameters(loc, sc, arguments); |
159 | 6490 |
6491 if (e1->op == TOKdotvar && t1->ty == Tfunction || | |
6492 e1->op == TOKdottd) | |
6493 { | |
6494 DotVarExp *dve; | |
6495 DotTemplateExp *dte; | |
6496 AggregateDeclaration *ad; | |
6497 UnaExp *ue = (UnaExp *)(e1); | |
336 | 6498 |
159 | 6499 if (e1->op == TOKdotvar) |
6500 { // Do overload resolution | |
6501 dve = (DotVarExp *)(e1); | |
6502 | |
6503 f = dve->var->isFuncDeclaration(); | |
6504 assert(f); | |
1587 | 6505 f = f->overloadResolve(loc, NULL, arguments, sc->module); |
159 | 6506 |
6507 ad = f->toParent()->isAggregateDeclaration(); | |
6508 } | |
6509 else | |
6510 { dte = (DotTemplateExp *)(e1); | |
6511 TemplateDeclaration *td = dte->td; | |
6512 assert(td); | |
6513 if (!arguments) | |
6514 // Should fix deduceFunctionTemplate() so it works on NULL argument | |
6515 arguments = new Expressions(); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6516 f = td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6517 if (!f) |
6518 { type = Type::terror; | |
6519 return this; | |
6520 } | |
6521 ad = td->toParent()->isAggregateDeclaration(); | |
6522 } | |
336 | 6523 if (f->needThis()) |
6524 { | |
6525 ue->e1 = getRightThis(loc, sc, ad, ue->e1, f); | |
6526 } | |
6527 | |
6528 /* Cannot call public functions from inside invariant | |
6529 * (because then the invariant would have infinite recursion) | |
159 | 6530 */ |
336 | 6531 if (sc->func && sc->func->isInvariantDeclaration() && |
6532 ue->e1->op == TOKthis && | |
6533 f->addPostInvariant() | |
159 | 6534 ) |
6535 { | |
336 | 6536 error("cannot call public/export function %s from invariant", f->toChars()); |
159 | 6537 } |
6538 | |
6539 checkDeprecated(sc, f); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6540 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6541 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6542 #endif |
159 | 6543 accessCheck(loc, sc, ue->e1, f); |
6544 if (!f->needThis()) | |
6545 { | |
6546 VarExp *ve = new VarExp(loc, f); | |
6547 e1 = new CommaExp(loc, ue->e1, ve); | |
6548 e1->type = f->type; | |
6549 } | |
6550 else | |
6551 { | |
6552 if (e1->op == TOKdotvar) | |
6553 dve->var = f; | |
6554 else | |
6555 e1 = new DotVarExp(loc, dte->e1, f); | |
6556 e1->type = f->type; | |
6557 | |
6558 // See if we need to adjust the 'this' pointer | |
6559 AggregateDeclaration *ad = f->isThis(); | |
6560 ClassDeclaration *cd = ue->e1->type->isClassHandle(); | |
6561 if (ad && cd && ad->isClassDeclaration() && ad != cd && | |
6562 ue->e1->op != TOKsuper) | |
6563 { | |
6564 ue->e1 = ue->e1->castTo(sc, ad->type); //new CastExp(loc, ue->e1, ad->type); | |
6565 ue->e1 = ue->e1->semantic(sc); | |
6566 } | |
6567 } | |
6568 t1 = e1->type; | |
6569 } | |
6570 else if (e1->op == TOKsuper) | |
6571 { | |
6572 // Base class constructor call | |
6573 ClassDeclaration *cd = NULL; | |
6574 | |
6575 if (sc->func) | |
6576 cd = sc->func->toParent()->isClassDeclaration(); | |
6577 if (!cd || !cd->baseClass || !sc->func->isCtorDeclaration()) | |
6578 { | |
6579 error("super class constructor call must be in a constructor"); | |
6580 type = Type::terror; | |
6581 return this; | |
6582 } | |
6583 else | |
6584 { | |
6585 f = cd->baseClass->ctor; | |
6586 if (!f) | |
6587 { error("no super class constructor for %s", cd->baseClass->toChars()); | |
6588 type = Type::terror; | |
6589 return this; | |
6590 } | |
6591 else | |
6592 { | |
336 | 6593 if (!sc->intypeof) |
6594 { | |
159 | 6595 #if 0 |
336 | 6596 if (sc->callSuper & (CSXthis | CSXsuper)) |
6597 error("reference to this before super()"); | |
159 | 6598 #endif |
336 | 6599 if (sc->noctor || sc->callSuper & CSXlabel) |
6600 error("constructor calls not allowed in loops or after labels"); | |
6601 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6602 error("multiple constructor calls"); | |
6603 sc->callSuper |= CSXany_ctor | CSXsuper_ctor; | |
6604 } | |
159 | 6605 |
1587 | 6606 f = f->overloadResolve(loc, NULL, arguments, sc->module); |
159 | 6607 checkDeprecated(sc, f); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6608 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6609 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6610 #endif |
159 | 6611 e1 = new DotVarExp(e1->loc, e1, f); |
6612 e1 = e1->semantic(sc); | |
6613 t1 = e1->type; | |
6614 } | |
6615 } | |
6616 } | |
6617 else if (e1->op == TOKthis) | |
6618 { | |
6619 // same class constructor call | |
6620 ClassDeclaration *cd = NULL; | |
6621 | |
6622 if (sc->func) | |
6623 cd = sc->func->toParent()->isClassDeclaration(); | |
6624 if (!cd || !sc->func->isCtorDeclaration()) | |
6625 { | |
6626 error("class constructor call must be in a constructor"); | |
6627 type = Type::terror; | |
6628 return this; | |
6629 } | |
6630 else | |
6631 { | |
336 | 6632 if (!sc->intypeof) |
6633 { | |
159 | 6634 #if 0 |
336 | 6635 if (sc->callSuper & (CSXthis | CSXsuper)) |
6636 error("reference to this before super()"); | |
159 | 6637 #endif |
336 | 6638 if (sc->noctor || sc->callSuper & CSXlabel) |
6639 error("constructor calls not allowed in loops or after labels"); | |
6640 if (sc->callSuper & (CSXsuper_ctor | CSXthis_ctor)) | |
6641 error("multiple constructor calls"); | |
6642 sc->callSuper |= CSXany_ctor | CSXthis_ctor; | |
6643 } | |
159 | 6644 |
6645 f = cd->ctor; | |
1587 | 6646 f = f->overloadResolve(loc, NULL, arguments, sc->module); |
159 | 6647 checkDeprecated(sc, f); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6648 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6649 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6650 #endif |
159 | 6651 e1 = new DotVarExp(e1->loc, e1, f); |
6652 e1 = e1->semantic(sc); | |
6653 t1 = e1->type; | |
6654 | |
6655 // BUG: this should really be done by checking the static | |
6656 // call graph | |
6657 if (f == sc->func) | |
6658 error("cyclic constructor call"); | |
6659 } | |
6660 } | |
6661 else if (!t1) | |
6662 { | |
6663 error("function expected before (), not '%s'", e1->toChars()); | |
6664 type = Type::terror; | |
6665 return this; | |
6666 } | |
6667 else if (t1->ty != Tfunction) | |
6668 { | |
6669 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
|
6670 { 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
|
6671 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
|
6672 tf = (TypeFunction *)(td->next); |
159 | 6673 goto Lcheckargs; |
6674 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6675 else if (t1->ty == Tpointer && ((TypePointer *)t1)->next->ty == Tfunction) |
159 | 6676 { Expression *e; |
6677 | |
6678 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
|
6679 t1 = ((TypePointer *)t1)->next; |
159 | 6680 e->type = t1; |
6681 e1 = e; | |
6682 } | |
6683 else if (e1->op == TOKtemplate) | |
6684 { | |
6685 TemplateExp *te = (TemplateExp *)e1; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6686 f = te->td->deduceFunctionTemplate(sc, loc, targsi, NULL, arguments); |
159 | 6687 if (!f) |
6688 { type = Type::terror; | |
6689 return this; | |
6690 } | |
6691 if (f->needThis() && hasThis(sc)) | |
6692 { | |
6693 // Supply an implicit 'this', as in | |
6694 // this.ident | |
6695 | |
6696 e1 = new DotTemplateExp(loc, (new ThisExp(loc))->semantic(sc), te->td); | |
6697 goto Lagain; | |
6698 } | |
6699 | |
6700 e1 = new VarExp(loc, f); | |
6701 goto Lagain; | |
6702 } | |
6703 else | |
6704 { error("function expected before (), not %s of type %s", e1->toChars(), e1->type->toChars()); | |
6705 type = Type::terror; | |
6706 return this; | |
6707 } | |
6708 } | |
6709 else if (e1->op == TOKvar) | |
6710 { | |
6711 // Do overload resolution | |
6712 VarExp *ve = (VarExp *)e1; | |
6713 | |
6714 f = ve->var->isFuncDeclaration(); | |
6715 assert(f); | |
6716 | |
6717 // Look to see if f is really a function template | |
6718 if (0 && !istemp && f->parent) | |
6719 { TemplateInstance *ti = f->parent->isTemplateInstance(); | |
6720 | |
6721 if (ti && | |
6722 (ti->name == f->ident || | |
6723 ti->toAlias()->ident == f->ident) | |
6724 && | |
6725 ti->tempdecl) | |
6726 { | |
6727 /* This is so that one can refer to the enclosing | |
6728 * template, even if it has the same name as a member | |
6729 * of the template, if it has a !(arguments) | |
6730 */ | |
6731 TemplateDeclaration *tempdecl = ti->tempdecl; | |
6732 if (tempdecl->overroot) // if not start of overloaded list of TemplateDeclaration's | |
6733 tempdecl = tempdecl->overroot; // then get the start | |
6734 e1 = new TemplateExp(loc, tempdecl); | |
6735 istemp = 1; | |
6736 goto Lagain; | |
6737 } | |
6738 } | |
6739 | |
1587 | 6740 f = f->overloadResolve(loc, NULL, arguments, sc->module); |
159 | 6741 checkDeprecated(sc, f); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6742 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6743 checkPurity(sc, f); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6744 #endif |
159 | 6745 |
6746 if (f->needThis() && hasThis(sc)) | |
6747 { | |
6748 // Supply an implicit 'this', as in | |
6749 // this.ident | |
6750 | |
6751 e1 = new DotVarExp(loc, new ThisExp(loc), f); | |
6752 goto Lagain; | |
6753 } | |
6754 | |
6755 accessCheck(loc, sc, NULL, f); | |
6756 | |
6757 ve->var = f; | |
6758 ve->type = f->type; | |
6759 t1 = f->type; | |
6760 } | |
6761 assert(t1->ty == Tfunction); | |
6762 tf = (TypeFunction *)(t1); | |
6763 | |
6764 Lcheckargs: | |
6765 assert(tf->ty == Tfunction); | |
6766 type = tf->next; | |
6767 | |
6768 if (!arguments) | |
6769 arguments = new Expressions(); | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
6770 functionParameters(loc, sc, tf, arguments); |
159 | 6771 |
6772 assert(type); | |
6773 | |
6774 if (f && f->tintro) | |
6775 { | |
6776 Type *t = type; | |
6777 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
|
6778 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
|
6779 |
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 (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
|
6781 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
6782 type = tf->next; |
159 | 6783 return castTo(sc, t); |
6784 } | |
6785 } | |
6786 | |
6787 return this; | |
6788 } | |
6789 | |
6790 int CallExp::checkSideEffect(int flag) | |
6791 { | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6792 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6793 if (flag != 2) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6794 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6795 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6796 if (e1->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6797 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6798 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6799 /* 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
|
6800 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6801 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
|
6802 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6803 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6804 if (e->checkSideEffect(2)) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6805 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6806 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6807 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6808 /* 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
|
6809 * then this expression has no side effects. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6810 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6811 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6812 if (t->ty == Tfunction && ((TypeFunction *)t)->ispure) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6813 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6814 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
|
6815 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6816 #endif |
159 | 6817 return 1; |
6818 } | |
6819 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6820 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6821 int CallExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6822 { |
1587 | 6823 //printf("CallExp::canThrow() %s\n", toChars()); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6824 if (e1->canThrow()) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6825 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6826 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6827 /* 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
|
6828 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6829 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
|
6830 { Expression *e = (Expression *)arguments->data[i]; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6831 |
1587 | 6832 if (e && e->canThrow()) |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6833 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6834 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6835 |
1587 | 6836 if (global.errors && !e1->type) |
6837 return 0; // error recovery | |
6838 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6839 /* 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
|
6840 * then this expression cannot throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6841 * Note that pure functions can throw. |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6842 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6843 Type *t = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6844 if (t->ty == Tfunction && ((TypeFunction *)t)->isnothrow) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6845 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6846 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
|
6847 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6848 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6849 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6850 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6851 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6852 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6853 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6854 int CallExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6855 { |
1587 | 6856 // if (type->toBasetype()->ty == Tstruct) |
6857 // return 1; | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6858 Type *tb = e1->type->toBasetype(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6859 if (tb->ty == Tfunction && ((TypeFunction *)tb)->isref) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6860 return 1; // function returns a reference |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6861 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6862 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6863 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
6864 |
159 | 6865 Expression *CallExp::toLvalue(Scope *sc, Expression *e) |
6866 { | |
1587 | 6867 #if 1 |
159 | 6868 if (type->toBasetype()->ty == Tstruct) |
6869 return this; | |
6870 else | |
1587 | 6871 #endif |
159 | 6872 return Expression::toLvalue(sc, e); |
6873 } | |
6874 | |
1587 | 6875 Expression *CallExp::modifiableLvalue(Scope *sc, Expression *e) |
6876 { | |
6877 #if 1 | |
6878 return Expression::modifiableLvalue(sc, e); | |
6879 #else | |
6880 /* Although function return values being usable as "ref" parameters is | |
6881 * unsound, disabling it breaks existing code. | |
6882 * Bugzilla 3167 | |
6883 */ | |
6884 error("cannot assign to function call"); | |
6885 return toLvalue(sc, e); | |
6886 #endif | |
6887 } | |
6888 | |
159 | 6889 void CallExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
6890 { int i; | |
6891 | |
6892 expToCBuffer(buf, hgs, e1, precedence[op]); | |
6893 buf->writeByte('('); | |
6894 argsToCBuffer(buf, arguments, hgs); | |
6895 buf->writeByte(')'); | |
6896 } | |
6897 | |
6898 | |
6899 /************************************************************/ | |
6900 | |
6901 AddrExp::AddrExp(Loc loc, Expression *e) | |
6902 : UnaExp(loc, TOKaddress, sizeof(AddrExp), e) | |
6903 { | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1258
diff
changeset
|
6904 m = NULL; |
159 | 6905 } |
6906 | |
6907 Expression *AddrExp::semantic(Scope *sc) | |
6908 { | |
6909 #if LOGSEMANTIC | |
6910 printf("AddrExp::semantic('%s')\n", toChars()); | |
6911 #endif | |
6912 if (!type) | |
6913 { | |
1358
78038e540342
Fix overload resolution issue in dmd bug 313/314 fix.
Christian Kamm <kamm incasoftware de>
parents:
1258
diff
changeset
|
6914 m = sc->module; |
159 | 6915 UnaExp::semantic(sc); |
6916 e1 = e1->toLvalue(sc, NULL); | |
6917 if (!e1->type) | |
6918 { | |
6919 error("cannot take address of %s", e1->toChars()); | |
1587 | 6920 return new ErrorExp(); |
6921 } | |
6922 if (!e1->type->deco) | |
6923 { | |
6924 /* No deco means semantic() was not run on the type. | |
6925 * We have to run semantic() on the symbol to get the right type: | |
6926 * auto x = &bar; | |
6927 * pure: int bar() { return 1;} | |
6928 * otherwise the 'pure' is missing from the type assigned to x. | |
6929 */ | |
6930 | |
6931 error("forward reference to %s", e1->toChars()); | |
6932 return new ErrorExp(); | |
159 | 6933 } |
6934 type = e1->type->pointerTo(); | |
6935 | |
6936 // See if this should really be a delegate | |
6937 if (e1->op == TOKdotvar) | |
6938 { | |
6939 DotVarExp *dve = (DotVarExp *)e1; | |
6940 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6941 | |
6942 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
|
6943 { Expression *e = new DelegateExp(loc, dve->e1, f); |
159 | 6944 e = e->semantic(sc); |
6945 return e; | |
6946 } | |
6947 } | |
6948 else if (e1->op == TOKvar) | |
6949 { | |
6950 VarExp *dve = (VarExp *)e1; | |
6951 FuncDeclaration *f = dve->var->isFuncDeclaration(); | |
6952 VarDeclaration *v = dve->var->isVarDeclaration(); | |
6953 | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
6954 // 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
|
6955 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
|
6956 { |
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
|
6957 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
|
6958 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
|
6959 } |
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
|
6960 |
159 | 6961 if (f && f->isNested()) |
6962 { Expression *e; | |
6963 | |
6964 e = new DelegateExp(loc, e1, f); | |
6965 e = e->semantic(sc); | |
6966 return e; | |
6967 } | |
6968 } | |
6969 else if (e1->op == TOKarray) | |
6970 { | |
6971 if (e1->type->toBasetype()->ty == Tbit) | |
6972 error("cannot take address of bit in array"); | |
6973 } | |
6974 return optimize(WANTvalue); | |
6975 } | |
6976 return this; | |
6977 } | |
6978 | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
6979 void AddrExp::checkEscape() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
6980 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
6981 e1->checkEscapeRef(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
6982 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
6983 |
159 | 6984 /************************************************************/ |
6985 | |
6986 PtrExp::PtrExp(Loc loc, Expression *e) | |
6987 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6988 { | |
1587 | 6989 // if (e->type) |
6990 // type = ((TypePointer *)e->type)->next; | |
159 | 6991 } |
6992 | |
6993 PtrExp::PtrExp(Loc loc, Expression *e, Type *t) | |
6994 : UnaExp(loc, TOKstar, sizeof(PtrExp), e) | |
6995 { | |
6996 type = t; | |
6997 } | |
6998 | |
6999 Expression *PtrExp::semantic(Scope *sc) | |
1587 | 7000 { |
159 | 7001 #if LOGSEMANTIC |
7002 printf("PtrExp::semantic('%s')\n", toChars()); | |
7003 #endif | |
1587 | 7004 if (!type) |
7005 { | |
7006 UnaExp::semantic(sc); | |
7007 e1 = resolveProperties(sc, e1); | |
7008 if (!e1->type) | |
7009 printf("PtrExp::semantic('%s')\n", toChars()); | |
7010 Type *tb = e1->type->toBasetype(); | |
7011 switch (tb->ty) | |
7012 { | |
7013 case Tpointer: | |
7014 type = tb->next; | |
7015 break; | |
7016 | |
7017 case Tsarray: | |
7018 case Tarray: | |
7019 type = tb->next; | |
7020 e1 = e1->castTo(sc, type->pointerTo()); | |
7021 break; | |
7022 | |
7023 default: | |
7024 error("can only * a pointer, not a '%s'", e1->type->toChars()); | |
7025 return new ErrorExp(); | |
7026 } | |
7027 rvalue(); | |
7028 } | |
159 | 7029 return this; |
7030 } | |
7031 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7032 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7033 int PtrExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7034 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7035 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7036 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7037 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7038 |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7039 void PtrExp::checkEscapeRef() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7040 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7041 e1->checkEscape(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7042 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7043 |
159 | 7044 Expression *PtrExp::toLvalue(Scope *sc, Expression *e) |
7045 { | |
7046 #if 0 | |
7047 tym = tybasic(e1->ET->Tty); | |
7048 if (!(tyscalar(tym) || | |
7049 tym == TYstruct || | |
7050 tym == TYarray && e->Eoper == TOKaddr)) | |
7051 synerr(EM_lvalue); // lvalue expected | |
7052 #endif | |
7053 return this; | |
7054 } | |
7055 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7056 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7057 Expression *PtrExp::modifiableLvalue(Scope *sc, Expression *e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7058 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7059 //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
|
7060 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7061 if (e1->op == TOKsymoff) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7062 { SymOffExp *se = (SymOffExp *)e1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7063 se->var->checkModify(loc, sc, type); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7064 //return toLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7065 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7066 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7067 return Expression::modifiableLvalue(sc, e); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7068 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7069 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7070 |
159 | 7071 void PtrExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
7072 { | |
7073 buf->writeByte('*'); | |
7074 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7075 } | |
7076 | |
7077 /************************************************************/ | |
7078 | |
7079 NegExp::NegExp(Loc loc, Expression *e) | |
7080 : UnaExp(loc, TOKneg, sizeof(NegExp), e) | |
7081 { | |
7082 } | |
7083 | |
7084 Expression *NegExp::semantic(Scope *sc) | |
7085 { Expression *e; | |
7086 | |
7087 #if LOGSEMANTIC | |
7088 printf("NegExp::semantic('%s')\n", toChars()); | |
7089 #endif | |
7090 if (!type) | |
7091 { | |
7092 UnaExp::semantic(sc); | |
7093 e1 = resolveProperties(sc, e1); | |
7094 e = op_overload(sc); | |
7095 if (e) | |
7096 return e; | |
7097 | |
7098 e1->checkNoBool(); | |
1587 | 7099 if (!e1->isArrayOperand()) |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7100 e1->checkArithmetic(); |
159 | 7101 type = e1->type; |
7102 } | |
7103 return this; | |
7104 } | |
7105 | |
7106 /************************************************************/ | |
7107 | |
7108 UAddExp::UAddExp(Loc loc, Expression *e) | |
7109 : UnaExp(loc, TOKuadd, sizeof(UAddExp), e) | |
7110 { | |
7111 } | |
7112 | |
7113 Expression *UAddExp::semantic(Scope *sc) | |
7114 { Expression *e; | |
7115 | |
7116 #if LOGSEMANTIC | |
7117 printf("UAddExp::semantic('%s')\n", toChars()); | |
7118 #endif | |
7119 assert(!type); | |
7120 UnaExp::semantic(sc); | |
7121 e1 = resolveProperties(sc, e1); | |
7122 e = op_overload(sc); | |
7123 if (e) | |
7124 return e; | |
7125 e1->checkNoBool(); | |
7126 e1->checkArithmetic(); | |
7127 return e1; | |
7128 } | |
7129 | |
7130 /************************************************************/ | |
7131 | |
7132 ComExp::ComExp(Loc loc, Expression *e) | |
7133 : UnaExp(loc, TOKtilde, sizeof(ComExp), e) | |
7134 { | |
7135 } | |
7136 | |
7137 Expression *ComExp::semantic(Scope *sc) | |
7138 { Expression *e; | |
7139 | |
7140 if (!type) | |
7141 { | |
7142 UnaExp::semantic(sc); | |
7143 e1 = resolveProperties(sc, e1); | |
7144 e = op_overload(sc); | |
7145 if (e) | |
7146 return e; | |
7147 | |
7148 e1->checkNoBool(); | |
1587 | 7149 if (!e1->isArrayOperand()) |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
7150 e1 = e1->checkIntegral(); |
159 | 7151 type = e1->type; |
7152 } | |
7153 return this; | |
7154 } | |
7155 | |
7156 /************************************************************/ | |
7157 | |
7158 NotExp::NotExp(Loc loc, Expression *e) | |
7159 : UnaExp(loc, TOKnot, sizeof(NotExp), e) | |
7160 { | |
7161 } | |
7162 | |
7163 Expression *NotExp::semantic(Scope *sc) | |
7164 { | |
7165 UnaExp::semantic(sc); | |
7166 e1 = resolveProperties(sc, e1); | |
7167 e1 = e1->checkToBoolean(); | |
7168 type = Type::tboolean; | |
7169 return this; | |
7170 } | |
7171 | |
7172 int NotExp::isBit() | |
7173 { | |
7174 return TRUE; | |
7175 } | |
7176 | |
7177 | |
7178 | |
7179 /************************************************************/ | |
7180 | |
7181 BoolExp::BoolExp(Loc loc, Expression *e, Type *t) | |
7182 : UnaExp(loc, TOKtobool, sizeof(BoolExp), e) | |
7183 { | |
7184 type = t; | |
7185 } | |
7186 | |
7187 Expression *BoolExp::semantic(Scope *sc) | |
7188 { | |
7189 UnaExp::semantic(sc); | |
7190 e1 = resolveProperties(sc, e1); | |
7191 e1 = e1->checkToBoolean(); | |
7192 type = Type::tboolean; | |
7193 return this; | |
7194 } | |
7195 | |
7196 int BoolExp::isBit() | |
7197 { | |
7198 return TRUE; | |
7199 } | |
7200 | |
7201 /************************************************************/ | |
7202 | |
7203 DeleteExp::DeleteExp(Loc loc, Expression *e) | |
7204 : UnaExp(loc, TOKdelete, sizeof(DeleteExp), e) | |
7205 { | |
7206 } | |
7207 | |
7208 Expression *DeleteExp::semantic(Scope *sc) | |
7209 { | |
7210 Type *tb; | |
7211 | |
7212 UnaExp::semantic(sc); | |
7213 e1 = resolveProperties(sc, e1); | |
7214 e1 = e1->toLvalue(sc, NULL); | |
7215 type = Type::tvoid; | |
7216 | |
7217 tb = e1->type->toBasetype(); | |
7218 switch (tb->ty) | |
7219 { case Tclass: | |
7220 { TypeClass *tc = (TypeClass *)tb; | |
7221 ClassDeclaration *cd = tc->sym; | |
7222 | |
7223 if (cd->isCOMinterface()) | |
7224 { /* Because COM classes are deleted by IUnknown.Release() | |
7225 */ | |
7226 error("cannot delete instance of COM interface %s", cd->toChars()); | |
7227 } | |
7228 break; | |
7229 } | |
7230 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
|
7231 tb = ((TypePointer *)tb)->next->toBasetype(); |
159 | 7232 if (tb->ty == Tstruct) |
7233 { | |
7234 TypeStruct *ts = (TypeStruct *)tb; | |
7235 StructDeclaration *sd = ts->sym; | |
7236 FuncDeclaration *f = sd->aggDelete; | |
7237 | |
7238 if (f) | |
7239 { | |
7240 Type *tpv = Type::tvoid->pointerTo(); | |
7241 | |
7242 Expression *e = e1->castTo(sc, tpv); | |
7243 Expression *ec = new VarExp(loc, f); | |
7244 e = new CallExp(loc, ec, e); | |
7245 return e->semantic(sc); | |
7246 } | |
7247 } | |
7248 break; | |
7249 | |
7250 case Tarray: | |
7251 break; | |
7252 | |
7253 default: | |
7254 if (e1->op == TOKindex) | |
7255 { | |
7256 IndexExp *ae = (IndexExp *)(e1); | |
7257 Type *tb1 = ae->e1->type->toBasetype(); | |
7258 if (tb1->ty == Taarray) | |
7259 break; | |
7260 } | |
7261 error("cannot delete type %s", e1->type->toChars()); | |
7262 break; | |
7263 } | |
7264 | |
7265 if (e1->op == TOKindex) | |
7266 { | |
7267 IndexExp *ae = (IndexExp *)(e1); | |
7268 Type *tb1 = ae->e1->type->toBasetype(); | |
7269 if (tb1->ty == Taarray) | |
7270 { if (!global.params.useDeprecated) | |
7271 error("delete aa[key] deprecated, use aa.remove(key)"); | |
7272 } | |
7273 } | |
7274 | |
7275 return this; | |
7276 } | |
7277 | |
7278 int DeleteExp::checkSideEffect(int flag) | |
7279 { | |
7280 return 1; | |
7281 } | |
7282 | |
7283 Expression *DeleteExp::checkToBoolean() | |
7284 { | |
7285 error("delete does not give a boolean result"); | |
7286 return this; | |
7287 } | |
7288 | |
7289 void DeleteExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7290 { | |
7291 buf->writestring("delete "); | |
7292 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7293 } | |
7294 | |
7295 /************************************************************/ | |
7296 | |
7297 CastExp::CastExp(Loc loc, Expression *e, Type *t) | |
7298 : UnaExp(loc, TOKcast, sizeof(CastExp), e) | |
7299 { | |
7300 to = t; | |
7301 } | |
7302 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7303 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7304 /* For cast(const) and cast(immutable) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7305 */ |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7306 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
|
7307 : UnaExp(loc, TOKcast, sizeof(CastExp), e) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7308 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7309 to = NULL; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7310 this->mod = mod; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7311 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7312 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7313 |
159 | 7314 Expression *CastExp::syntaxCopy() |
7315 { | |
7316 return new CastExp(loc, e1->syntaxCopy(), to->syntaxCopy()); | |
7317 } | |
7318 | |
7319 | |
7320 Expression *CastExp::semantic(Scope *sc) | |
7321 { Expression *e; | |
7322 BinExp *b; | |
7323 UnaExp *u; | |
7324 | |
7325 #if LOGSEMANTIC | |
7326 printf("CastExp::semantic('%s')\n", toChars()); | |
7327 #endif | |
7328 | |
7329 //static int x; assert(++x < 10); | |
7330 | |
7331 if (type) | |
7332 return this; | |
7333 UnaExp::semantic(sc); | |
7334 if (e1->type) // if not a tuple | |
7335 { | |
7336 e1 = resolveProperties(sc, e1); | |
7337 to = to->semantic(loc, sc); | |
7338 | |
7339 e = op_overload(sc); | |
7340 if (e) | |
7341 { | |
7342 return e->implicitCastTo(sc, to); | |
7343 } | |
7344 | |
1587 | 7345 if (e1->op == TOKtemplate) |
7346 { | |
7347 error("cannot cast template %s to type %s", e1->toChars(), to->toChars()); | |
7348 return new ErrorExp(); | |
7349 } | |
7350 | |
7351 Type *t1b = e1->type->toBasetype(); | |
159 | 7352 Type *tob = to->toBasetype(); |
7353 if (tob->ty == Tstruct && | |
1587 | 7354 !tob->equals(t1b) && |
159 | 7355 ((TypeStruct *)to)->sym->search(0, Id::call, 0) |
7356 ) | |
7357 { | |
7358 /* Look to replace: | |
7359 * cast(S)t | |
7360 * with: | |
7361 * S(t) | |
7362 */ | |
7363 | |
7364 // Rewrite as to.call(e1) | |
7365 e = new TypeExp(loc, to); | |
7366 e = new DotIdExp(loc, e, Id::call); | |
7367 e = new CallExp(loc, e, e1); | |
7368 e = e->semantic(sc); | |
7369 return e; | |
7370 } | |
1587 | 7371 |
7372 // Struct casts are possible only when the sizes match | |
7373 if (tob->ty == Tstruct || t1b->ty == Tstruct) | |
7374 { | |
7375 size_t fromsize = t1b->size(loc); | |
7376 size_t tosize = tob->size(loc); | |
7377 if (fromsize != tosize) | |
7378 { | |
7379 error("cannot cast from %s to %s", e1->type->toChars(), to->toChars()); | |
7380 return new ErrorExp(); | |
7381 } | |
7382 } | |
159 | 7383 } |
7384 e = e1->castTo(sc, to); | |
7385 return e; | |
7386 } | |
7387 | |
7388 int CastExp::checkSideEffect(int flag) | |
7389 { | |
7390 /* if not: | |
7391 * cast(void) | |
7392 * cast(classtype)func() | |
7393 */ | |
7394 if (!to->equals(Type::tvoid) && | |
7395 !(to->ty == Tclass && e1->op == TOKcall && e1->type->ty == Tclass)) | |
7396 return Expression::checkSideEffect(flag); | |
7397 return 1; | |
7398 } | |
7399 | |
7400 void CastExp::checkEscape() | |
7401 { Type *tb = type->toBasetype(); | |
7402 if (tb->ty == Tarray && e1->op == TOKvar && | |
7403 e1->type->toBasetype()->ty == Tsarray) | |
7404 { VarExp *ve = (VarExp *)e1; | |
7405 VarDeclaration *v = ve->var->isVarDeclaration(); | |
7406 if (v) | |
7407 { | |
7408 if (!v->isDataseg() && !v->isParameter()) | |
7409 error("escaping reference to local %s", v->toChars()); | |
7410 } | |
7411 } | |
7412 } | |
7413 | |
7414 void CastExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7415 { | |
7416 buf->writestring("cast("); | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7417 #if DMDV1 |
159 | 7418 to->toCBuffer(buf, NULL, hgs); |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7419 #else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7420 if (to) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7421 to->toCBuffer(buf, NULL, hgs); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7422 else |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7423 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7424 switch (mod) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7425 { case 0: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7426 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7427 case MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7428 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7429 break; |
1619
c61782a76dff
Merge DMD r304: refactor invariant => immutable
Leandro Lucarella <llucax@gmail.com>
parents:
1618
diff
changeset
|
7430 case MODimmutable: |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7431 buf->writestring(Token::tochars[TOKimmutable]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7432 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7433 case MODshared: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7434 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7435 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7436 case MODshared | MODconst: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7437 buf->writestring(Token::tochars[TOKshared]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7438 buf->writeByte(' '); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7439 buf->writestring(Token::tochars[TOKconst]); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7440 break; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7441 default: |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7442 assert(0); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7443 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7444 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7445 #endif |
159 | 7446 buf->writeByte(')'); |
7447 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7448 } | |
7449 | |
7450 | |
7451 /************************************************************/ | |
7452 | |
7453 SliceExp::SliceExp(Loc loc, Expression *e1, Expression *lwr, Expression *upr) | |
7454 : UnaExp(loc, TOKslice, sizeof(SliceExp), e1) | |
7455 { | |
7456 this->upr = upr; | |
7457 this->lwr = lwr; | |
7458 lengthVar = NULL; | |
7459 } | |
7460 | |
7461 Expression *SliceExp::syntaxCopy() | |
7462 { | |
7463 Expression *lwr = NULL; | |
7464 if (this->lwr) | |
7465 lwr = this->lwr->syntaxCopy(); | |
7466 | |
7467 Expression *upr = NULL; | |
7468 if (this->upr) | |
7469 upr = this->upr->syntaxCopy(); | |
7470 | |
7471 return new SliceExp(loc, e1->syntaxCopy(), lwr, upr); | |
7472 } | |
7473 | |
7474 Expression *SliceExp::semantic(Scope *sc) | |
7475 { Expression *e; | |
7476 AggregateDeclaration *ad; | |
7477 //FuncDeclaration *fd; | |
7478 ScopeDsymbol *sym; | |
7479 | |
7480 #if LOGSEMANTIC | |
7481 printf("SliceExp::semantic('%s')\n", toChars()); | |
7482 #endif | |
7483 if (type) | |
7484 return this; | |
7485 | |
7486 UnaExp::semantic(sc); | |
7487 e1 = resolveProperties(sc, e1); | |
7488 | |
7489 e = this; | |
7490 | |
7491 Type *t = e1->type->toBasetype(); | |
7492 if (t->ty == Tpointer) | |
7493 { | |
7494 if (!lwr || !upr) | |
7495 error("need upper and lower bound to slice pointer"); | |
7496 } | |
7497 else if (t->ty == Tarray) | |
7498 { | |
7499 } | |
7500 else if (t->ty == Tsarray) | |
7501 { | |
7502 } | |
7503 else if (t->ty == Tclass) | |
7504 { | |
7505 ad = ((TypeClass *)t)->sym; | |
7506 goto L1; | |
7507 } | |
7508 else if (t->ty == Tstruct) | |
7509 { | |
7510 ad = ((TypeStruct *)t)->sym; | |
7511 | |
7512 L1: | |
7513 if (search_function(ad, Id::slice)) | |
7514 { | |
7515 // Rewrite as e1.slice(lwr, upr) | |
7516 e = new DotIdExp(loc, e1, Id::slice); | |
7517 | |
7518 if (lwr) | |
7519 { | |
7520 assert(upr); | |
7521 e = new CallExp(loc, e, lwr, upr); | |
7522 } | |
7523 else | |
7524 { assert(!upr); | |
7525 e = new CallExp(loc, e); | |
7526 } | |
7527 e = e->semantic(sc); | |
7528 return e; | |
7529 } | |
7530 goto Lerror; | |
7531 } | |
7532 else if (t->ty == Ttuple) | |
7533 { | |
7534 if (!lwr && !upr) | |
7535 return e1; | |
7536 if (!lwr || !upr) | |
7537 { error("need upper and lower bound to slice tuple"); | |
7538 goto Lerror; | |
7539 } | |
7540 } | |
7541 else | |
7542 goto Lerror; | |
7543 | |
7544 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7545 { | |
7546 sym = new ArrayScopeSymbol(this); | |
7547 sym->loc = loc; | |
7548 sym->parent = sc->scopesym; | |
7549 sc = sc->push(sym); | |
7550 } | |
7551 | |
7552 if (lwr) | |
7553 { lwr = lwr->semantic(sc); | |
7554 lwr = resolveProperties(sc, lwr); | |
7555 lwr = lwr->implicitCastTo(sc, Type::tsize_t); | |
7556 } | |
7557 if (upr) | |
7558 { upr = upr->semantic(sc); | |
7559 upr = resolveProperties(sc, upr); | |
7560 upr = upr->implicitCastTo(sc, Type::tsize_t); | |
7561 } | |
7562 | |
7563 if (t->ty == Tsarray || t->ty == Tarray || t->ty == Ttuple) | |
7564 sc->pop(); | |
7565 | |
7566 if (t->ty == Ttuple) | |
7567 { | |
7568 lwr = lwr->optimize(WANTvalue); | |
7569 upr = upr->optimize(WANTvalue); | |
7570 uinteger_t i1 = lwr->toUInteger(); | |
7571 uinteger_t i2 = upr->toUInteger(); | |
7572 | |
7573 size_t length; | |
7574 TupleExp *te; | |
7575 TypeTuple *tup; | |
7576 | |
7577 if (e1->op == TOKtuple) // slicing an expression tuple | |
7578 { te = (TupleExp *)e1; | |
7579 length = te->exps->dim; | |
7580 } | |
7581 else if (e1->op == TOKtype) // slicing a type tuple | |
7582 { tup = (TypeTuple *)t; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
7583 length = Parameter::dim(tup->arguments); |
159 | 7584 } |
7585 else | |
7586 assert(0); | |
7587 | |
7588 if (i1 <= i2 && i2 <= length) | |
7589 { size_t j1 = (size_t) i1; | |
7590 size_t j2 = (size_t) i2; | |
7591 | |
7592 if (e1->op == TOKtuple) | |
7593 { Expressions *exps = new Expressions; | |
7594 exps->setDim(j2 - j1); | |
7595 for (size_t i = 0; i < j2 - j1; i++) | |
7596 { Expression *e = (Expression *)te->exps->data[j1 + i]; | |
7597 exps->data[i] = (void *)e; | |
7598 } | |
7599 e = new TupleExp(loc, exps); | |
7600 } | |
7601 else | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
7602 { Parameters *args = new Parameters; |
159 | 7603 args->reserve(j2 - j1); |
7604 for (size_t i = j1; i < j2; i++) | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
7605 { Parameter *arg = Parameter::getNth(tup->arguments, i); |
159 | 7606 args->push(arg); |
7607 } | |
7608 e = new TypeExp(e1->loc, new TypeTuple(args)); | |
7609 } | |
7610 e = e->semantic(sc); | |
7611 } | |
7612 else | |
7613 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7614 error("string slice [%ju .. %ju] is out of bounds", i1, i2); |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
7615 e = new IntegerExp(0); |
159 | 7616 } |
7617 return e; | |
7618 } | |
7619 | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7620 if (t->ty == Tarray) |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7621 { |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7622 type = e1->type; |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7623 } |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7624 else |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
7625 type = t->nextOf()->arrayOf(); |
159 | 7626 return e; |
7627 | |
7628 Lerror: | |
7629 char *s; | |
7630 if (t->ty == Tvoid) | |
7631 s = e1->toChars(); | |
7632 else | |
7633 s = t->toChars(); | |
7634 error("%s cannot be sliced with []", s); | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7635 e = new ErrorExp(); |
159 | 7636 return e; |
7637 } | |
7638 | |
7639 void SliceExp::checkEscape() | |
7640 { | |
7641 e1->checkEscape(); | |
7642 } | |
7643 | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7644 void SliceExp::checkEscapeRef() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7645 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7646 e1->checkEscapeRef(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7647 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7648 |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7649 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7650 int SliceExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7651 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7652 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7653 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7654 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7655 |
159 | 7656 Expression *SliceExp::toLvalue(Scope *sc, Expression *e) |
7657 { | |
7658 return this; | |
7659 } | |
7660 | |
7661 Expression *SliceExp::modifiableLvalue(Scope *sc, Expression *e) | |
7662 { | |
7663 error("slice expression %s is not a modifiable lvalue", toChars()); | |
7664 return this; | |
7665 } | |
7666 | |
7667 void SliceExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7668 { | |
7669 expToCBuffer(buf, hgs, e1, precedence[op]); | |
7670 buf->writeByte('['); | |
7671 if (upr || lwr) | |
7672 { | |
7673 if (lwr) | |
7674 expToCBuffer(buf, hgs, lwr, PREC_assign); | |
7675 else | |
7676 buf->writeByte('0'); | |
7677 buf->writestring(".."); | |
7678 if (upr) | |
7679 expToCBuffer(buf, hgs, upr, PREC_assign); | |
7680 else | |
7681 buf->writestring("length"); // BUG: should be array.length | |
7682 } | |
7683 buf->writeByte(']'); | |
7684 } | |
7685 | |
7686 /********************** ArrayLength **************************************/ | |
7687 | |
7688 ArrayLengthExp::ArrayLengthExp(Loc loc, Expression *e1) | |
7689 : UnaExp(loc, TOKarraylength, sizeof(ArrayLengthExp), e1) | |
7690 { | |
7691 } | |
7692 | |
7693 Expression *ArrayLengthExp::semantic(Scope *sc) | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7694 { |
159 | 7695 #if LOGSEMANTIC |
7696 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
7697 #endif | |
7698 if (!type) | |
7699 { | |
7700 UnaExp::semantic(sc); | |
7701 e1 = resolveProperties(sc, e1); | |
7702 | |
7703 type = Type::tsize_t; | |
7704 } | |
7705 return this; | |
7706 } | |
7707 | |
7708 void ArrayLengthExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7709 { | |
7710 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7711 buf->writestring(".length"); | |
7712 } | |
7713 | |
7714 /*********************** ArrayExp *************************************/ | |
7715 | |
7716 // e1 [ i1, i2, i3, ... ] | |
7717 | |
7718 ArrayExp::ArrayExp(Loc loc, Expression *e1, Expressions *args) | |
7719 : UnaExp(loc, TOKarray, sizeof(ArrayExp), e1) | |
7720 { | |
7721 arguments = args; | |
7722 } | |
7723 | |
7724 Expression *ArrayExp::syntaxCopy() | |
7725 { | |
7726 return new ArrayExp(loc, e1->syntaxCopy(), arraySyntaxCopy(arguments)); | |
7727 } | |
7728 | |
7729 Expression *ArrayExp::semantic(Scope *sc) | |
7730 { Expression *e; | |
7731 Type *t1; | |
7732 | |
7733 #if LOGSEMANTIC | |
7734 printf("ArrayExp::semantic('%s')\n", toChars()); | |
7735 #endif | |
7736 UnaExp::semantic(sc); | |
7737 e1 = resolveProperties(sc, e1); | |
7738 | |
7739 t1 = e1->type->toBasetype(); | |
7740 if (t1->ty != Tclass && t1->ty != Tstruct) | |
7741 { // Convert to IndexExp | |
7742 if (arguments->dim != 1) | |
7743 error("only one index allowed to index %s", t1->toChars()); | |
7744 e = new IndexExp(loc, e1, (Expression *)arguments->data[0]); | |
7745 return e->semantic(sc); | |
7746 } | |
7747 | |
7748 // Run semantic() on each argument | |
7749 for (size_t i = 0; i < arguments->dim; i++) | |
7750 { e = (Expression *)arguments->data[i]; | |
7751 | |
7752 e = e->semantic(sc); | |
7753 if (!e->type) | |
7754 error("%s has no value", e->toChars()); | |
7755 arguments->data[i] = (void *)e; | |
7756 } | |
7757 | |
7758 expandTuples(arguments); | |
7759 assert(arguments && arguments->dim); | |
7760 | |
7761 e = op_overload(sc); | |
7762 if (!e) | |
7763 { error("no [] operator overload for type %s", e1->type->toChars()); | |
7764 e = e1; | |
7765 } | |
7766 return e; | |
7767 } | |
7768 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7769 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7770 int ArrayExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7771 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7772 if (type && type->toBasetype()->ty == Tvoid) |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7773 return 0; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7774 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7775 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7776 #endif |
159 | 7777 |
7778 Expression *ArrayExp::toLvalue(Scope *sc, Expression *e) | |
7779 { | |
7780 if (type && type->toBasetype()->ty == Tvoid) | |
7781 error("voids have no value"); | |
7782 return this; | |
7783 } | |
7784 | |
7785 | |
7786 void ArrayExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
7787 { int i; | |
7788 | |
7789 expToCBuffer(buf, hgs, e1, PREC_primary); | |
7790 buf->writeByte('['); | |
7791 argsToCBuffer(buf, arguments, hgs); | |
7792 buf->writeByte(']'); | |
7793 } | |
7794 | |
7795 /************************* DotExp ***********************************/ | |
7796 | |
7797 DotExp::DotExp(Loc loc, Expression *e1, Expression *e2) | |
7798 : BinExp(loc, TOKdotexp, sizeof(DotExp), e1, e2) | |
7799 { | |
7800 } | |
7801 | |
7802 Expression *DotExp::semantic(Scope *sc) | |
7803 { | |
7804 #if LOGSEMANTIC | |
7805 printf("DotExp::semantic('%s')\n", toChars()); | |
7806 if (type) printf("\ttype = %s\n", type->toChars()); | |
7807 #endif | |
7808 e1 = e1->semantic(sc); | |
7809 e2 = e2->semantic(sc); | |
7810 if (e2->op == TOKimport) | |
7811 { | |
7812 ScopeExp *se = (ScopeExp *)e2; | |
7813 TemplateDeclaration *td = se->sds->isTemplateDeclaration(); | |
7814 if (td) | |
7815 { Expression *e = new DotTemplateExp(loc, e1, td); | |
7816 e = e->semantic(sc); | |
7817 return e; | |
7818 } | |
7819 } | |
7820 if (!type) | |
7821 type = e2->type; | |
7822 return this; | |
7823 } | |
7824 | |
7825 | |
7826 /************************* CommaExp ***********************************/ | |
7827 | |
7828 CommaExp::CommaExp(Loc loc, Expression *e1, Expression *e2) | |
7829 : BinExp(loc, TOKcomma, sizeof(CommaExp), e1, e2) | |
7830 { | |
7831 } | |
7832 | |
7833 Expression *CommaExp::semantic(Scope *sc) | |
7834 { | |
7835 if (!type) | |
7836 { BinExp::semanticp(sc); | |
7837 type = e2->type; | |
7838 } | |
7839 return this; | |
7840 } | |
7841 | |
7842 void CommaExp::checkEscape() | |
7843 { | |
7844 e2->checkEscape(); | |
7845 } | |
7846 | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7847 void CommaExp::checkEscapeRef() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7848 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7849 e2->checkEscapeRef(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7850 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
7851 |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7852 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7853 int CommaExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7854 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7855 return e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7856 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7857 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
7858 |
159 | 7859 Expression *CommaExp::toLvalue(Scope *sc, Expression *e) |
7860 { | |
7861 e2 = e2->toLvalue(sc, NULL); | |
7862 return this; | |
7863 } | |
7864 | |
7865 Expression *CommaExp::modifiableLvalue(Scope *sc, Expression *e) | |
7866 { | |
7867 e2 = e2->modifiableLvalue(sc, e); | |
7868 return this; | |
7869 } | |
7870 | |
7871 int CommaExp::isBool(int result) | |
7872 { | |
7873 return e2->isBool(result); | |
7874 } | |
7875 | |
7876 int CommaExp::checkSideEffect(int flag) | |
7877 { | |
7878 if (flag == 2) | |
7879 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
7880 else | |
7881 { | |
7882 // Don't check e1 until we cast(void) the a,b code generation | |
7883 return e2->checkSideEffect(flag); | |
7884 } | |
7885 } | |
7886 | |
7887 /************************** IndexExp **********************************/ | |
7888 | |
7889 // e1 [ e2 ] | |
7890 | |
7891 IndexExp::IndexExp(Loc loc, Expression *e1, Expression *e2) | |
7892 : BinExp(loc, TOKindex, sizeof(IndexExp), e1, e2) | |
7893 { | |
7894 //printf("IndexExp::IndexExp('%s')\n", toChars()); | |
7895 lengthVar = NULL; | |
7896 modifiable = 0; // assume it is an rvalue | |
7897 } | |
7898 | |
7899 Expression *IndexExp::semantic(Scope *sc) | |
7900 { Expression *e; | |
7901 BinExp *b; | |
7902 UnaExp *u; | |
7903 Type *t1; | |
7904 ScopeDsymbol *sym; | |
7905 | |
7906 #if LOGSEMANTIC | |
7907 printf("IndexExp::semantic('%s')\n", toChars()); | |
7908 #endif | |
7909 if (type) | |
7910 return this; | |
7911 if (!e1->type) | |
7912 e1 = e1->semantic(sc); | |
7913 assert(e1->type); // semantic() should already be run on it | |
7914 e = this; | |
7915 | |
7916 // Note that unlike C we do not implement the int[ptr] | |
7917 | |
7918 t1 = e1->type->toBasetype(); | |
7919 | |
7920 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7921 { // Create scope for 'length' variable | |
7922 sym = new ArrayScopeSymbol(this); | |
7923 sym->loc = loc; | |
7924 sym->parent = sc->scopesym; | |
7925 sc = sc->push(sym); | |
7926 } | |
7927 | |
7928 e2 = e2->semantic(sc); | |
7929 if (!e2->type) | |
7930 { | |
7931 error("%s has no value", e2->toChars()); | |
7932 e2->type = Type::terror; | |
7933 } | |
7934 e2 = resolveProperties(sc, e2); | |
7935 | |
7936 if (t1->ty == Tsarray || t1->ty == Tarray || t1->ty == Ttuple) | |
7937 sc = sc->pop(); | |
7938 | |
7939 switch (t1->ty) | |
7940 { | |
7941 case Tpointer: | |
7942 case Tarray: | |
7943 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7944 e->type = t1->next; | |
7945 break; | |
7946 | |
7947 case Tsarray: | |
7948 { | |
7949 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7950 | |
7951 TypeSArray *tsa = (TypeSArray *)t1; | |
7952 | |
7953 #if 0 // Don't do now, because it might be short-circuit evaluated | |
7954 // Do compile time array bounds checking if possible | |
7955 e2 = e2->optimize(WANTvalue); | |
7956 if (e2->op == TOKint64) | |
7957 { | |
1195
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
7958 dinteger_t index = e2->toInteger(); |
e961851fb8be
Merged DMD 1.042.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1168
diff
changeset
|
7959 dinteger_t length = tsa->dim->toInteger(); |
159 | 7960 if (index < 0 || index >= length) |
7961 error("array index [%lld] is outside array bounds [0 .. %lld]", | |
7962 index, length); | |
7963 } | |
7964 #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
|
7965 e->type = t1->nextOf(); |
159 | 7966 break; |
7967 } | |
7968 | |
7969 case Taarray: | |
7970 { TypeAArray *taa = (TypeAArray *)t1; | |
7971 | |
7972 e2 = e2->implicitCastTo(sc, taa->index); // type checking | |
7973 e2 = e2->implicitCastTo(sc, taa->key); // actual argument type | |
7974 type = taa->next; | |
7975 break; | |
7976 } | |
7977 | |
7978 case Ttuple: | |
7979 { | |
7980 e2 = e2->implicitCastTo(sc, Type::tsize_t); | |
7981 e2 = e2->optimize(WANTvalue); | |
7982 uinteger_t index = e2->toUInteger(); | |
7983 size_t length; | |
7984 TupleExp *te; | |
7985 TypeTuple *tup; | |
7986 | |
7987 if (e1->op == TOKtuple) | |
7988 { te = (TupleExp *)e1; | |
7989 length = te->exps->dim; | |
7990 } | |
7991 else if (e1->op == TOKtype) | |
7992 { | |
7993 tup = (TypeTuple *)t1; | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
7994 length = Parameter::dim(tup->arguments); |
159 | 7995 } |
7996 else | |
7997 assert(0); | |
7998 | |
7999 if (index < length) | |
8000 { | |
8001 | |
8002 if (e1->op == TOKtuple) | |
8003 e = (Expression *)te->exps->data[(size_t)index]; | |
8004 else | |
1607
207a8a438dea
Merge DMD r253: refactor: Argument => Parameter
Leandro Lucarella <llucax@gmail.com>
parents:
1605
diff
changeset
|
8005 e = new TypeExp(e1->loc, Parameter::getNth(tup->arguments, (size_t)index)->type); |
159 | 8006 } |
8007 else | |
8008 { | |
1103
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
8009 error("array index [%ju] is outside array bounds [0 .. %zu]", |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
8010 index, length); |
b30fe7e1dbb9
- Updated to DMD frontend 1.041.
Tomas Lindquist Olsen <tomas.l.olsen gmail.com>
parents:
1012
diff
changeset
|
8011 e = e1; |
159 | 8012 } |
8013 break; | |
8014 } | |
8015 | |
8016 default: | |
8017 error("%s must be an array or pointer type, not %s", | |
8018 e1->toChars(), e1->type->toChars()); | |
8019 type = Type::tint32; | |
8020 break; | |
8021 } | |
8022 return e; | |
8023 } | |
8024 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8025 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8026 int IndexExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8027 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8028 return 1; |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8029 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8030 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
8031 |
159 | 8032 Expression *IndexExp::toLvalue(Scope *sc, Expression *e) |
8033 { | |
8034 // if (type && type->toBasetype()->ty == Tvoid) | |
8035 // error("voids have no value"); | |
8036 return this; | |
8037 } | |
8038 | |
8039 Expression *IndexExp::modifiableLvalue(Scope *sc, Expression *e) | |
8040 { | |
8041 //printf("IndexExp::modifiableLvalue(%s)\n", toChars()); | |
8042 modifiable = 1; | |
8043 if (e1->op == TOKstring) | |
8044 error("string literals are immutable"); | |
8045 if (e1->type->toBasetype()->ty == Taarray) | |
8046 e1 = e1->modifiableLvalue(sc, e1); | |
8047 return toLvalue(sc, e); | |
8048 } | |
8049 | |
8050 void IndexExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
8051 { | |
8052 expToCBuffer(buf, hgs, e1, PREC_primary); | |
8053 buf->writeByte('['); | |
8054 expToCBuffer(buf, hgs, e2, PREC_assign); | |
8055 buf->writeByte(']'); | |
8056 } | |
8057 | |
8058 | |
8059 /************************* PostExp ***********************************/ | |
8060 | |
8061 PostExp::PostExp(enum TOK op, Loc loc, Expression *e) | |
8062 : BinExp(loc, op, sizeof(PostExp), e, | |
8063 new IntegerExp(loc, 1, Type::tint32)) | |
8064 { | |
8065 } | |
8066 | |
8067 Expression *PostExp::semantic(Scope *sc) | |
8068 { Expression *e = this; | |
8069 | |
8070 if (!type) | |
8071 { | |
8072 BinExp::semantic(sc); | |
8073 e2 = resolveProperties(sc, e2); | |
8074 | |
8075 e = op_overload(sc); | |
8076 if (e) | |
8077 return e; | |
8078 | |
8079 e = this; | |
8080 e1 = e1->modifiableLvalue(sc, e1); | |
8081 e1->checkScalar(); | |
8082 e1->checkNoBool(); | |
8083 if (e1->type->ty == Tpointer) | |
8084 e = scaleFactor(sc); | |
8085 else | |
8086 e2 = e2->castTo(sc, e1->type); | |
8087 e->type = e1->type; | |
8088 } | |
8089 return e; | |
8090 } | |
8091 | |
8092 void PostExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) | |
8093 { | |
8094 expToCBuffer(buf, hgs, e1, precedence[op]); | |
8095 buf->writestring((op == TOKplusplus) ? (char *)"++" : (char *)"--"); | |
8096 } | |
8097 | |
8098 /************************************************************/ | |
8099 | |
1587 | 8100 /* op can be TOKassign, TOKconstruct, or TOKblit */ |
159 | 8101 |
8102 AssignExp::AssignExp(Loc loc, Expression *e1, Expression *e2) | |
8103 : BinExp(loc, TOKassign, sizeof(AssignExp), e1, e2) | |
8104 { | |
8105 ismemset = 0; | |
8106 } | |
8107 | |
8108 Expression *AssignExp::semantic(Scope *sc) | |
1587 | 8109 { |
159 | 8110 Expression *e1old = e1; |
8111 | |
8112 #if LOGSEMANTIC | |
8113 printf("AssignExp::semantic('%s')\n", toChars()); | |
8114 #endif | |
8115 //printf("e1->op = %d, '%s'\n", e1->op, Token::toChars(e1->op)); | |
1587 | 8116 //printf("e2->op = %d, '%s'\n", e2->op, Token::toChars(e2->op)); |
8117 | |
8118 if (type) | |
8119 return this; | |
8120 | |
8121 if (e2->op == TOKcomma) | |
8122 { /* Rewrite to get rid of the comma from rvalue | |
8123 */ | |
8124 AssignExp *ea = new AssignExp(loc, e1, ((CommaExp *)e2)->e2); | |
8125 ea->op = op; | |
8126 Expression *e = new CommaExp(loc, ((CommaExp *)e2)->e1, ea); | |
8127 return e->semantic(sc); | |
8128 } | |
159 | 8129 |
8130 /* Look for operator overloading of a[i]=value. | |
8131 * Do it before semantic() otherwise the a[i] will have been | |
8132 * converted to a.opIndex() already. | |
8133 */ | |
8134 if (e1->op == TOKarray) | |
1587 | 8135 { |
159 | 8136 ArrayExp *ae = (ArrayExp *)e1; |
8137 AggregateDeclaration *ad; | |
8138 Identifier *id = Id::index; | |
8139 | |
8140 ae->e1 = ae->e1->semantic(sc); | |
1587 | 8141 Type *t1 = ae->e1->type->toBasetype(); |
159 | 8142 if (t1->ty == Tstruct) |
8143 { | |
8144 ad = ((TypeStruct *)t1)->sym; | |
8145 goto L1; | |
8146 } | |
8147 else if (t1->ty == Tclass) | |
8148 { | |
8149 ad = ((TypeClass *)t1)->sym; | |
8150 L1: | |
8151 // Rewrite (a[i] = value) to (a.opIndexAssign(value, i)) | |
8152 if (search_function(ad, Id::indexass)) | |
8153 { Expression *e = new DotIdExp(loc, ae->e1, Id::indexass); | |
8154 Expressions *a = (Expressions *)ae->arguments->copy(); | |
8155 | |
8156 a->insert(0, e2); | |
8157 e = new CallExp(loc, e, a); | |
8158 e = e->semantic(sc); | |
8159 return e; | |
8160 } | |
8161 else | |
8162 { | |
8163 // Rewrite (a[i] = value) to (a.opIndex(i, value)) | |
8164 if (search_function(ad, id)) | |
8165 { Expression *e = new DotIdExp(loc, ae->e1, id); | |
8166 | |
8167 if (1 || !global.params.useDeprecated) | |
8168 error("operator [] assignment overload with opIndex(i, value) illegal, use opIndexAssign(value, i)"); | |
8169 | |
8170 e = new CallExp(loc, e, (Expression *)ae->arguments->data[0], e2); | |
8171 e = e->semantic(sc); | |
8172 return e; | |
8173 } | |
8174 } | |
8175 } | |
8176 } | |
8177 /* Look for operator overloading of a[i..j]=value. | |
8178 * Do it before semantic() otherwise the a[i..j] will have been | |
8179 * converted to a.opSlice() already. | |
8180 */ | |
8181 if (e1->op == TOKslice) | |
8182 { Type *t1; | |
8183 SliceExp *ae = (SliceExp *)e1; | |
8184 AggregateDeclaration *ad; | |
8185 Identifier *id = Id::index; | |
8186 | |
8187 ae->e1 = ae->e1->semantic(sc); | |
8188 ae->e1 = resolveProperties(sc, ae->e1); | |
8189 t1 = ae->e1->type->toBasetype(); | |
8190 if (t1->ty == Tstruct) | |
8191 { | |
8192 ad = ((TypeStruct *)t1)->sym; | |
8193 goto L2; | |
8194 } | |
8195 else if (t1->ty == Tclass) | |
8196 { | |
8197 ad = ((TypeClass *)t1)->sym; | |
8198 L2: | |
8199 // Rewrite (a[i..j] = value) to (a.opIndexAssign(value, i, j)) | |
8200 if (search_function(ad, Id::sliceass)) | |
8201 { Expression *e = new DotIdExp(loc, ae->e1, Id::sliceass); | |
8202 Expressions *a = new Expressions(); | |
8203 | |
8204 a->push(e2); | |
8205 if (ae->lwr) | |
8206 { a->push(ae->lwr); | |
8207 assert(ae->upr); | |
8208 a->push(ae->upr); | |
8209 } | |
8210 else | |
8211 assert(!ae->upr); | |
8212 e = new CallExp(loc, e, a); | |
8213 e = e->semantic(sc); | |
8214 return e; | |
8215 } | |
8216 } | |
8217 } | |
8218 | |
8219 BinExp::semantic(sc); | |
717
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8220 |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8221 if (e1->op == TOKdottd) |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8222 { // 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
|
8223 Expression *e = new CallExp(loc, e1, e2); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8224 e = e->semantic(sc); |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8225 return e; |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8226 } |
a26b0c5d5942
Merged DMD 1.036.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
664
diff
changeset
|
8227 |
159 | 8228 e2 = resolveProperties(sc, e2); |
8229 assert(e1->type); | |
8230 | |
8231 /* Rewrite tuple assignment as a tuple of assignments. | |
8232 */ | |
8233 if (e1->op == TOKtuple && e2->op == TOKtuple) | |
8234 { TupleExp *tup1 = (TupleExp *)e1; | |
8235 TupleExp *tup2 = (TupleExp *)e2; | |
8236 size_t dim = tup1->exps->dim; | |
8237 if (dim != tup2->exps->dim) | |
8238 { | |
8239 error("mismatched tuple lengths, %d and %d", (int)dim, (int)tup2->exps->dim); | |
8240 } | |
8241 else | |
8242 { Expressions *exps = new Expressions; | |
8243 exps->setDim(dim); | |
8244 | |
8245 for (int i = 0; i < dim; i++) | |
8246 { Expression *ex1 = (Expression *)tup1->exps->data[i]; | |
8247 Expression *ex2 = (Expression *)tup2->exps->data[i]; | |
8248 exps->data[i] = (void *) new AssignExp(loc, ex1, ex2); | |
8249 } | |
8250 Expression *e = new TupleExp(loc, exps); | |
8251 e = e->semantic(sc); | |
8252 return e; | |
8253 } | |
8254 } | |
8255 | |
1587 | 8256 // Determine if this is an initialization of a reference |
8257 int refinit = 0; | |
8258 if (op == TOKconstruct && e1->op == TOKvar) | |
8259 { VarExp *ve = (VarExp *)e1; | |
8260 VarDeclaration *v = ve->var->isVarDeclaration(); | |
8261 if (v->storage_class & (STCout | STCref)) | |
8262 refinit = 1; | |
8263 } | |
8264 | |
8265 Type *t1 = e1->type->toBasetype(); | |
159 | 8266 |
8267 if (t1->ty == Tfunction) | |
8268 { // Rewrite f=value to f(value) | |
1587 | 8269 Expression *e = new CallExp(loc, e1, e2); |
159 | 8270 e = e->semantic(sc); |
8271 return e; | |
8272 } | |
8273 | |
8274 /* If it is an assignment from a 'foreign' type, | |
8275 * check for operator overloading. | |
8276 */ | |
8277 if (t1->ty == Tclass || t1->ty == Tstruct) | |
8278 { | |
8279 if (!e2->type->implicitConvTo(e1->type)) | |
8280 { | |
8281 Expression *e = op_overload(sc); | |
8282 if (e) | |
8283 return e; | |
8284 } | |
8285 } | |
8286 | |
8287 e2->rvalue(); | |
8288 | |
8289 if (e1->op == TOKarraylength) | |
8290 { | |
8291 // e1 is not an lvalue, but we let code generator handle it | |
8292 ArrayLengthExp *ale = (ArrayLengthExp *)e1; | |
8293 | |
8294 ale->e1 = ale->e1->modifiableLvalue(sc, e1); | |
8295 } | |
8296 else if (e1->op == TOKslice) | |
8297 ; | |
8298 else | |
8299 { // Try to do a decent error message with the expression | |
8300 // before it got constant folded | |
1587 | 8301 if (op != TOKconstruct) |
8302 e1 = e1->modifiableLvalue(sc, e1old); | |
159 | 8303 } |
8304 | |
8305 if (e1->op == TOKslice && | |
8306 t1->nextOf() && | |
8307 e2->implicitConvTo(t1->nextOf()) | |
8308 // !(t1->nextOf()->equals(e2->type->nextOf())) | |
8309 ) | |
8310 { // memset | |
8311 ismemset = 1; // make it easy for back end to tell what this is | |
8312 e2 = e2->implicitCastTo(sc, t1->next); | |
8313 } | |
1587 | 8314 else if (t1->ty == Tsarray && !refinit) |
159 | 8315 { |
8316 error("cannot assign to static array %s", e1->toChars()); | |
8317 } | |
8318 else | |
8319 { | |
8320 e2 = e2->implicitCastTo(sc, e1->type); | |
8321 } | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8322 |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8323 /* 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
|
8324 */ |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8325 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
|
8326 (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
|
8327 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
|
8328 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
|
8329 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
|
8330 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
|
8331 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8332 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
|
8333 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
|
8334 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8335 |
159 | 8336 type = e1->type; |
8337 assert(type); | |
8338 return this; | |
8339 } | |
8340 | |
8341 Expression *AssignExp::checkToBoolean() | |
8342 { | |
8343 // Things like: | |
8344 // if (a = b) ... | |
8345 // are usually mistakes. | |
8346 | |
8347 error("'=' does not give a boolean result"); | |
8348 return this; | |
8349 } | |
8350 | |
8351 /************************************************************/ | |
8352 | |
8353 AddAssignExp::AddAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8354 : BinExp(loc, TOKaddass, sizeof(AddAssignExp), e1, e2) | |
8355 { | |
8356 } | |
8357 | |
8358 Expression *AddAssignExp::semantic(Scope *sc) | |
8359 { Expression *e; | |
8360 | |
8361 if (type) | |
8362 return this; | |
8363 | |
8364 BinExp::semantic(sc); | |
8365 e2 = resolveProperties(sc, e2); | |
8366 | |
8367 e = op_overload(sc); | |
8368 if (e) | |
8369 return e; | |
8370 | |
8371 Type *tb1 = e1->type->toBasetype(); | |
8372 Type *tb2 = e2->type->toBasetype(); | |
8373 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8374 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
|
8375 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8376 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
|
8377 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
|
8378 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
|
8379 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8380 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8381 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8382 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
|
8383 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8384 |
159 | 8385 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && |
8386 (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
|
8387 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8388 ) |
8389 { | |
8390 type = e1->type; | |
8391 e = this; | |
8392 } | |
8393 else | |
8394 { | |
8395 e1->checkScalar(); | |
8396 e1->checkNoBool(); | |
8397 if (tb1->ty == Tpointer && tb2->isintegral()) | |
8398 e = scaleFactor(sc); | |
8399 else if (tb1->ty == Tbit || tb1->ty == Tbool) | |
8400 { | |
8401 #if 0 | |
8402 // Need to rethink this | |
8403 if (e1->op != TOKvar) | |
8404 { // Rewrite e1+=e2 to (v=&e1),*v=*v+e2 | |
8405 VarDeclaration *v; | |
8406 Expression *ea; | |
8407 Expression *ex; | |
8408 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8409 Identifier *id = Lexer::uniqueId("__name"); |
159 | 8410 |
8411 v = new VarDeclaration(loc, tb1->pointerTo(), id, NULL); | |
8412 v->semantic(sc); | |
8413 if (!sc->insert(v)) | |
8414 assert(0); | |
8415 v->parent = sc->func; | |
8416 | |
8417 ea = new AddrExp(loc, e1); | |
8418 ea = new AssignExp(loc, new VarExp(loc, v), ea); | |
8419 | |
8420 ex = new VarExp(loc, v); | |
8421 ex = new PtrExp(loc, ex); | |
8422 e = new AddExp(loc, ex, e2); | |
8423 e = new CastExp(loc, e, e1->type); | |
8424 e = new AssignExp(loc, ex->syntaxCopy(), e); | |
8425 | |
8426 e = new CommaExp(loc, ea, e); | |
8427 } | |
8428 else | |
8429 #endif | |
8430 { // Rewrite e1+=e2 to e1=e1+e2 | |
8431 // BUG: doesn't account for side effects in e1 | |
8432 // BUG: other assignment operators for bits aren't handled at all | |
8433 e = new AddExp(loc, e1, e2); | |
8434 e = new CastExp(loc, e, e1->type); | |
8435 e = new AssignExp(loc, e1->syntaxCopy(), e); | |
8436 } | |
8437 e = e->semantic(sc); | |
8438 } | |
8439 else | |
8440 { | |
8441 type = e1->type; | |
8442 typeCombine(sc); | |
8443 e1->checkArithmetic(); | |
8444 e2->checkArithmetic(); | |
1587 | 8445 checkComplexAddAssign(); |
159 | 8446 if (type->isreal() || type->isimaginary()) |
8447 { | |
8448 assert(global.errors || e2->type->isfloating()); | |
8449 e2 = e2->castTo(sc, e1->type); | |
8450 } | |
8451 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8452 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8453 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8454 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8455 } |
8456 } | |
8457 return e; | |
8458 } | |
8459 | |
8460 /************************************************************/ | |
8461 | |
8462 MinAssignExp::MinAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8463 : BinExp(loc, TOKminass, sizeof(MinAssignExp), e1, e2) | |
8464 { | |
8465 } | |
8466 | |
8467 Expression *MinAssignExp::semantic(Scope *sc) | |
8468 { Expression *e; | |
8469 | |
8470 if (type) | |
8471 return this; | |
8472 | |
8473 BinExp::semantic(sc); | |
8474 e2 = resolveProperties(sc, e2); | |
8475 | |
8476 e = op_overload(sc); | |
8477 if (e) | |
8478 return e; | |
8479 | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8480 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
|
8481 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8482 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
|
8483 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
|
8484 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
|
8485 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8486 |
159 | 8487 e1 = e1->modifiableLvalue(sc, e1); |
8488 e1->checkScalar(); | |
8489 e1->checkNoBool(); | |
8490 if (e1->type->ty == Tpointer && e2->type->isintegral()) | |
8491 e = scaleFactor(sc); | |
8492 else | |
8493 { | |
8494 e1 = e1->checkArithmetic(); | |
8495 e2 = e2->checkArithmetic(); | |
1587 | 8496 checkComplexAddAssign(); |
159 | 8497 type = e1->type; |
8498 typeCombine(sc); | |
8499 if (type->isreal() || type->isimaginary()) | |
8500 { | |
8501 assert(e2->type->isfloating()); | |
8502 e2 = e2->castTo(sc, e1->type); | |
8503 } | |
8504 e = this; | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8505 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8506 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8507 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8508 } |
8509 return e; | |
8510 } | |
8511 | |
8512 /************************************************************/ | |
8513 | |
8514 CatAssignExp::CatAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8515 : BinExp(loc, TOKcatass, sizeof(CatAssignExp), e1, e2) | |
8516 { | |
8517 } | |
8518 | |
8519 Expression *CatAssignExp::semantic(Scope *sc) | |
8520 { Expression *e; | |
8521 | |
8522 BinExp::semantic(sc); | |
8523 e2 = resolveProperties(sc, e2); | |
8524 | |
8525 e = op_overload(sc); | |
8526 if (e) | |
8527 return e; | |
8528 | |
8529 if (e1->op == TOKslice) | |
8530 { SliceExp *se = (SliceExp *)e1; | |
8531 | |
8532 if (se->e1->type->toBasetype()->ty == Tsarray) | |
8533 error("cannot append to static array %s", se->e1->type->toChars()); | |
8534 } | |
8535 | |
8536 e1 = e1->modifiableLvalue(sc, e1); | |
8537 | |
8538 Type *tb1 = e1->type->toBasetype(); | |
8539 Type *tb2 = e2->type->toBasetype(); | |
8540 | |
8541 e2->rvalue(); | |
8542 | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8543 Type *tb1next = tb1->nextOf(); |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8544 |
159 | 8545 if ((tb1->ty == Tarray) && |
8546 (tb2->ty == Tarray || tb2->ty == Tsarray) && | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8547 (e2->implicitConvTo(e1->type) |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8548 #if DMDV2 |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8549 || tb2->nextOf()->implicitConvTo(tb1next) |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8550 #endif |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8551 ) |
159 | 8552 ) |
8553 { // Append array | |
8554 e2 = e2->castTo(sc, e1->type); | |
8555 type = e1->type; | |
8556 e = this; | |
8557 } | |
8558 else if ((tb1->ty == Tarray) && | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8559 e2->implicitConvTo(tb1next) |
159 | 8560 ) |
8561 { // Append element | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8562 e2 = e2->castTo(sc, tb1next); |
159 | 8563 type = e1->type; |
8564 e = this; | |
8565 } | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8566 else if (tb1->ty == Tarray && |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8567 (tb1next->ty == Tchar || tb1next->ty == Twchar) && |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8568 e2->implicitConvTo(Type::tdchar) |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8569 ) |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8570 { // Append dchar to char[] or wchar[] |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8571 e2 = e2->castTo(sc, Type::tdchar); |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8572 type = e1->type; |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8573 e = this; |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8574 |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8575 /* Do not allow appending wchar to char[] because if wchar happens |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8576 * to be a surrogate pair, nothing good can result. |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8577 */ |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8578 } |
159 | 8579 else |
8580 { | |
8581 error("cannot append type %s to type %s", tb2->toChars(), tb1->toChars()); | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8582 e = new ErrorExp(); |
159 | 8583 } |
8584 return e; | |
8585 } | |
8586 | |
8587 /************************************************************/ | |
8588 | |
8589 MulAssignExp::MulAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8590 : BinExp(loc, TOKmulass, sizeof(MulAssignExp), e1, e2) | |
8591 { | |
8592 } | |
8593 | |
8594 Expression *MulAssignExp::semantic(Scope *sc) | |
8595 { Expression *e; | |
8596 | |
8597 BinExp::semantic(sc); | |
8598 e2 = resolveProperties(sc, e2); | |
8599 | |
8600 e = op_overload(sc); | |
8601 if (e) | |
8602 return e; | |
8603 | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8604 #if DMDV2 |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8605 if (e1->op == TOKarraylength) |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8606 { |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8607 e = ArrayLengthExp::rewriteOpAssign(this); |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8608 e = e->semantic(sc); |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8609 return e; |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8610 } |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8611 #endif |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8612 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8613 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
|
8614 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8615 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
|
8616 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
|
8617 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
|
8618 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8619 |
159 | 8620 e1 = e1->modifiableLvalue(sc, e1); |
8621 e1->checkScalar(); | |
8622 e1->checkNoBool(); | |
8623 type = e1->type; | |
8624 typeCombine(sc); | |
8625 e1->checkArithmetic(); | |
8626 e2->checkArithmetic(); | |
1587 | 8627 checkComplexMulAssign(); |
159 | 8628 if (e2->type->isfloating()) |
8629 { Type *t1; | |
8630 Type *t2; | |
8631 | |
8632 t1 = e1->type; | |
8633 t2 = e2->type; | |
8634 if (t1->isreal()) | |
8635 { | |
8636 if (t2->isimaginary() || t2->iscomplex()) | |
8637 { | |
8638 e2 = e2->castTo(sc, t1); | |
8639 } | |
8640 } | |
8641 else if (t1->isimaginary()) | |
8642 { | |
8643 if (t2->isimaginary() || t2->iscomplex()) | |
8644 { | |
8645 switch (t1->ty) | |
8646 { | |
8647 case Timaginary32: t2 = Type::tfloat32; break; | |
8648 case Timaginary64: t2 = Type::tfloat64; break; | |
8649 case Timaginary80: t2 = Type::tfloat80; break; | |
8650 default: | |
8651 assert(0); | |
8652 } | |
8653 e2 = e2->castTo(sc, t2); | |
8654 } | |
8655 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8656 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8657 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8658 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
159 | 8659 } |
8660 return this; | |
8661 } | |
8662 | |
8663 /************************************************************/ | |
8664 | |
8665 DivAssignExp::DivAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8666 : BinExp(loc, TOKdivass, sizeof(DivAssignExp), e1, e2) | |
8667 { | |
8668 } | |
8669 | |
8670 Expression *DivAssignExp::semantic(Scope *sc) | |
8671 { Expression *e; | |
8672 | |
8673 BinExp::semantic(sc); | |
8674 e2 = resolveProperties(sc, e2); | |
8675 | |
8676 e = op_overload(sc); | |
8677 if (e) | |
8678 return e; | |
8679 | |
1605
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8680 #if DMDV2 |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8681 if (e1->op == TOKarraylength) |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8682 { |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8683 e = ArrayLengthExp::rewriteOpAssign(this); |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8684 e = e->semantic(sc); |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8685 return e; |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8686 } |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8687 #endif |
1d5721f9ae18
[WIP] Merge DMD r251: bugzilla 111 (appending a dchar to a char[])
Leandro Lucarella <llucax@gmail.com>
parents:
1604
diff
changeset
|
8688 |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8689 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
|
8690 { // T[] -= ... |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8691 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
|
8692 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
|
8693 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
|
8694 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8695 |
159 | 8696 e1 = e1->modifiableLvalue(sc, e1); |
8697 e1->checkScalar(); | |
8698 e1->checkNoBool(); | |
8699 type = e1->type; | |
8700 typeCombine(sc); | |
8701 e1->checkArithmetic(); | |
8702 e2->checkArithmetic(); | |
1587 | 8703 checkComplexMulAssign(); |
159 | 8704 if (e2->type->isimaginary()) |
8705 { Type *t1; | |
8706 Type *t2; | |
8707 | |
8708 t1 = e1->type; | |
8709 if (t1->isreal()) | |
8710 { // x/iv = i(-x/v) | |
8711 // Therefore, the result is 0 | |
8712 e2 = new CommaExp(loc, e2, new RealExp(loc, 0, t1)); | |
8713 e2->type = t1; | |
8714 e = new AssignExp(loc, e1, e2); | |
8715 e->type = t1; | |
8716 return e; | |
8717 } | |
8718 else if (t1->isimaginary()) | |
8719 { Expression *e; | |
8720 | |
8721 switch (t1->ty) | |
8722 { | |
8723 case Timaginary32: t2 = Type::tfloat32; break; | |
8724 case Timaginary64: t2 = Type::tfloat64; break; | |
8725 case Timaginary80: t2 = Type::tfloat80; break; | |
8726 default: | |
8727 assert(0); | |
8728 } | |
8729 e2 = e2->castTo(sc, t2); | |
8730 e = new AssignExp(loc, e1, e2); | |
8731 e->type = t1; | |
8732 return e; | |
8733 } | |
8734 } | |
1012
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8735 |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8736 if (e2->type->iscomplex() && !type->iscomplex()) |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8737 error("Cannot assign %s to %s", e2->type->toChars(), type->toChars()); |
39cf8fa483fd
Error gracefully for +=, -=, *=, /= if rhs is complex but lhs isn't.
Christian Kamm <kamm incasoftware de>
parents:
891
diff
changeset
|
8738 |
159 | 8739 return this; |
8740 } | |
8741 | |
8742 /************************************************************/ | |
8743 | |
8744 ModAssignExp::ModAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8745 : BinExp(loc, TOKmodass, sizeof(ModAssignExp), e1, e2) | |
8746 { | |
8747 } | |
8748 | |
8749 Expression *ModAssignExp::semantic(Scope *sc) | |
8750 { | |
1587 | 8751 BinExp::semantic(sc); |
8752 checkComplexMulAssign(); | |
159 | 8753 return commonSemanticAssign(sc); |
8754 } | |
8755 | |
8756 /************************************************************/ | |
8757 | |
8758 ShlAssignExp::ShlAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8759 : BinExp(loc, TOKshlass, sizeof(ShlAssignExp), e1, e2) | |
8760 { | |
8761 } | |
8762 | |
8763 Expression *ShlAssignExp::semantic(Scope *sc) | |
8764 { Expression *e; | |
8765 | |
8766 //printf("ShlAssignExp::semantic()\n"); | |
8767 BinExp::semantic(sc); | |
8768 e2 = resolveProperties(sc, e2); | |
8769 | |
8770 e = op_overload(sc); | |
8771 if (e) | |
8772 return e; | |
8773 | |
8774 e1 = e1->modifiableLvalue(sc, e1); | |
8775 e1->checkScalar(); | |
8776 e1->checkNoBool(); | |
8777 type = e1->type; | |
8778 typeCombine(sc); | |
8779 e1->checkIntegral(); | |
8780 e2 = e2->checkIntegral(); | |
8781 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8782 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8783 return this; |
8784 } | |
8785 | |
8786 /************************************************************/ | |
8787 | |
8788 ShrAssignExp::ShrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8789 : BinExp(loc, TOKshrass, sizeof(ShrAssignExp), e1, e2) | |
8790 { | |
8791 } | |
8792 | |
8793 Expression *ShrAssignExp::semantic(Scope *sc) | |
8794 { Expression *e; | |
8795 | |
8796 BinExp::semantic(sc); | |
8797 e2 = resolveProperties(sc, e2); | |
8798 | |
8799 e = op_overload(sc); | |
8800 if (e) | |
8801 return e; | |
8802 | |
8803 e1 = e1->modifiableLvalue(sc, e1); | |
8804 e1->checkScalar(); | |
8805 e1->checkNoBool(); | |
8806 type = e1->type; | |
8807 typeCombine(sc); | |
8808 e1->checkIntegral(); | |
8809 e2 = e2->checkIntegral(); | |
8810 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8811 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8812 return this; |
8813 } | |
8814 | |
8815 /************************************************************/ | |
8816 | |
8817 UshrAssignExp::UshrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8818 : BinExp(loc, TOKushrass, sizeof(UshrAssignExp), e1, e2) | |
8819 { | |
8820 } | |
8821 | |
8822 Expression *UshrAssignExp::semantic(Scope *sc) | |
8823 { Expression *e; | |
8824 | |
8825 BinExp::semantic(sc); | |
8826 e2 = resolveProperties(sc, e2); | |
8827 | |
8828 e = op_overload(sc); | |
8829 if (e) | |
8830 return e; | |
8831 | |
8832 e1 = e1->modifiableLvalue(sc, e1); | |
8833 e1->checkScalar(); | |
8834 e1->checkNoBool(); | |
8835 type = e1->type; | |
8836 typeCombine(sc); | |
8837 e1->checkIntegral(); | |
8838 e2 = e2->checkIntegral(); | |
8839 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
8840 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 8841 return this; |
8842 } | |
8843 | |
8844 /************************************************************/ | |
8845 | |
8846 AndAssignExp::AndAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8847 : BinExp(loc, TOKandass, sizeof(AndAssignExp), e1, e2) | |
8848 { | |
8849 } | |
8850 | |
8851 Expression *AndAssignExp::semantic(Scope *sc) | |
8852 { | |
8853 return commonSemanticAssignIntegral(sc); | |
8854 } | |
8855 | |
8856 /************************************************************/ | |
8857 | |
8858 OrAssignExp::OrAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8859 : BinExp(loc, TOKorass, sizeof(OrAssignExp), e1, e2) | |
8860 { | |
8861 } | |
8862 | |
8863 Expression *OrAssignExp::semantic(Scope *sc) | |
8864 { | |
8865 return commonSemanticAssignIntegral(sc); | |
8866 } | |
8867 | |
8868 /************************************************************/ | |
8869 | |
8870 XorAssignExp::XorAssignExp(Loc loc, Expression *e1, Expression *e2) | |
8871 : BinExp(loc, TOKxorass, sizeof(XorAssignExp), e1, e2) | |
8872 { | |
8873 } | |
8874 | |
8875 Expression *XorAssignExp::semantic(Scope *sc) | |
8876 { | |
8877 return commonSemanticAssignIntegral(sc); | |
8878 } | |
8879 | |
8880 /************************* AddExp *****************************/ | |
8881 | |
8882 AddExp::AddExp(Loc loc, Expression *e1, Expression *e2) | |
8883 : BinExp(loc, TOKadd, sizeof(AddExp), e1, e2) | |
8884 { | |
8885 } | |
8886 | |
8887 Expression *AddExp::semantic(Scope *sc) | |
8888 { Expression *e; | |
8889 | |
8890 #if LOGSEMANTIC | |
8891 printf("AddExp::semantic('%s')\n", toChars()); | |
8892 #endif | |
8893 if (!type) | |
8894 { | |
8895 BinExp::semanticp(sc); | |
8896 | |
8897 e = op_overload(sc); | |
8898 if (e) | |
8899 return e; | |
8900 | |
8901 Type *tb1 = e1->type->toBasetype(); | |
8902 Type *tb2 = e2->type->toBasetype(); | |
8903 | |
8904 if ((tb1->ty == Tarray || tb1->ty == Tsarray) && | |
8905 (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
|
8906 tb1->nextOf()->equals(tb2->nextOf()) |
159 | 8907 ) |
8908 { | |
8909 type = e1->type; | |
8910 e = this; | |
8911 } | |
8912 else if (tb1->ty == Tpointer && e2->type->isintegral() || | |
8913 tb2->ty == Tpointer && e1->type->isintegral()) | |
8914 e = scaleFactor(sc); | |
8915 else if (tb1->ty == Tpointer && tb2->ty == Tpointer) | |
8916 { | |
8917 incompatibleTypes(); | |
8918 type = e1->type; | |
8919 e = this; | |
8920 } | |
8921 else | |
8922 { | |
8923 typeCombine(sc); | |
8924 if ((e1->type->isreal() && e2->type->isimaginary()) || | |
8925 (e1->type->isimaginary() && e2->type->isreal())) | |
8926 { | |
8927 switch (type->toBasetype()->ty) | |
8928 { | |
8929 case Tfloat32: | |
8930 case Timaginary32: | |
8931 type = Type::tcomplex32; | |
8932 break; | |
8933 | |
8934 case Tfloat64: | |
8935 case Timaginary64: | |
8936 type = Type::tcomplex64; | |
8937 break; | |
8938 | |
8939 case Tfloat80: | |
8940 case Timaginary80: | |
8941 type = Type::tcomplex80; | |
8942 break; | |
8943 | |
8944 default: | |
8945 assert(0); | |
8946 } | |
8947 } | |
8948 e = this; | |
8949 } | |
8950 return e; | |
8951 } | |
8952 return this; | |
8953 } | |
8954 | |
8955 /************************************************************/ | |
8956 | |
8957 MinExp::MinExp(Loc loc, Expression *e1, Expression *e2) | |
8958 : BinExp(loc, TOKmin, sizeof(MinExp), e1, e2) | |
8959 { | |
8960 } | |
8961 | |
8962 Expression *MinExp::semantic(Scope *sc) | |
8963 { Expression *e; | |
8964 Type *t1; | |
8965 Type *t2; | |
8966 | |
8967 #if LOGSEMANTIC | |
8968 printf("MinExp::semantic('%s')\n", toChars()); | |
8969 #endif | |
8970 if (type) | |
8971 return this; | |
8972 | |
8973 BinExp::semanticp(sc); | |
8974 | |
8975 e = op_overload(sc); | |
8976 if (e) | |
8977 return e; | |
8978 | |
8979 e = this; | |
8980 t1 = e1->type->toBasetype(); | |
8981 t2 = e2->type->toBasetype(); | |
8982 if (t1->ty == Tpointer) | |
8983 { | |
8984 if (t2->ty == Tpointer) | |
8985 { // Need to divide the result by the stride | |
8986 // Replace (ptr - ptr) with (ptr - ptr) / stride | |
8987 d_int64 stride; | |
8988 Expression *e; | |
8989 | |
8990 typeCombine(sc); // make sure pointer types are compatible | |
8991 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
|
8992 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
|
8993 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
|
8994 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8995 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
|
8996 } |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8997 else |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8998 { |
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
8999 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
|
9000 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
|
9001 } |
159 | 9002 return e; |
9003 } | |
9004 else if (t2->isintegral()) | |
9005 e = scaleFactor(sc); | |
9006 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
|
9007 { error("incompatible types for minus"); |
159 | 9008 return new IntegerExp(0); |
9009 } | |
9010 } | |
9011 else if (t2->ty == Tpointer) | |
9012 { | |
9013 type = e2->type; | |
9014 error("can't subtract pointer from %s", e1->type->toChars()); | |
9015 return new IntegerExp(0); | |
9016 } | |
9017 else | |
9018 { | |
9019 typeCombine(sc); | |
9020 t1 = e1->type->toBasetype(); | |
9021 t2 = e2->type->toBasetype(); | |
9022 if ((t1->isreal() && t2->isimaginary()) || | |
9023 (t1->isimaginary() && t2->isreal())) | |
9024 { | |
9025 switch (type->ty) | |
9026 { | |
9027 case Tfloat32: | |
9028 case Timaginary32: | |
9029 type = Type::tcomplex32; | |
9030 break; | |
9031 | |
9032 case Tfloat64: | |
9033 case Timaginary64: | |
9034 type = Type::tcomplex64; | |
9035 break; | |
9036 | |
9037 case Tfloat80: | |
9038 case Timaginary80: | |
9039 type = Type::tcomplex80; | |
9040 break; | |
9041 | |
9042 default: | |
9043 assert(0); | |
9044 } | |
9045 } | |
9046 } | |
9047 return e; | |
9048 } | |
9049 | |
9050 /************************* CatExp *****************************/ | |
9051 | |
9052 CatExp::CatExp(Loc loc, Expression *e1, Expression *e2) | |
9053 : BinExp(loc, TOKcat, sizeof(CatExp), e1, e2) | |
9054 { | |
9055 } | |
9056 | |
9057 Expression *CatExp::semantic(Scope *sc) | |
9058 { Expression *e; | |
9059 | |
9060 //printf("CatExp::semantic() %s\n", toChars()); | |
9061 if (!type) | |
9062 { | |
9063 BinExp::semanticp(sc); | |
9064 e = op_overload(sc); | |
9065 if (e) | |
9066 return e; | |
9067 | |
9068 Type *tb1 = e1->type->toBasetype(); | |
9069 Type *tb2 = e2->type->toBasetype(); | |
9070 | |
9071 | |
9072 /* BUG: Should handle things like: | |
9073 * char c; | |
9074 * c ~ ' ' | |
9075 * ' ' ~ c; | |
9076 */ | |
9077 | |
9078 #if 0 | |
9079 e1->type->print(); | |
9080 e2->type->print(); | |
9081 #endif | |
9082 if ((tb1->ty == Tsarray || tb1->ty == Tarray) && | |
9083 e2->type->equals(tb1->next)) | |
9084 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9085 type = tb1->nextOf()->arrayOf(); |
159 | 9086 if (tb2->ty == Tarray) |
9087 { // Make e2 into [e2] | |
9088 e2 = new ArrayLiteralExp(e2->loc, e2); | |
9089 e2->type = type; | |
9090 } | |
9091 return this; | |
9092 } | |
9093 else if ((tb2->ty == Tsarray || tb2->ty == Tarray) && | |
9094 e1->type->equals(tb2->next)) | |
9095 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9096 type = tb2->nextOf()->arrayOf(); |
159 | 9097 if (tb1->ty == Tarray) |
9098 { // Make e1 into [e1] | |
9099 e1 = new ArrayLiteralExp(e1->loc, e1); | |
9100 e1->type = type; | |
9101 } | |
9102 return this; | |
9103 } | |
9104 | |
9105 typeCombine(sc); | |
9106 | |
9107 if (type->toBasetype()->ty == Tsarray) | |
9108 type = type->toBasetype()->next->arrayOf(); | |
9109 #if 0 | |
9110 e1->type->print(); | |
9111 e2->type->print(); | |
9112 type->print(); | |
9113 print(); | |
9114 #endif | |
9115 if (e1->op == TOKstring && e2->op == TOKstring) | |
9116 e = optimize(WANTvalue); | |
9117 else if (e1->type->equals(e2->type) && | |
9118 (e1->type->toBasetype()->ty == Tarray || | |
9119 e1->type->toBasetype()->ty == Tsarray)) | |
9120 { | |
9121 e = this; | |
9122 } | |
9123 else | |
9124 { | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9125 //printf("(%s) ~ (%s)\n", e1->toChars(), e2->toChars()); |
159 | 9126 error("Can only concatenate arrays, not (%s ~ %s)", |
9127 e1->type->toChars(), e2->type->toChars()); | |
9128 type = Type::tint32; | |
9129 e = this; | |
9130 } | |
9131 e->type = e->type->semantic(loc, sc); | |
9132 return e; | |
9133 } | |
9134 return this; | |
9135 } | |
9136 | |
9137 /************************************************************/ | |
9138 | |
9139 MulExp::MulExp(Loc loc, Expression *e1, Expression *e2) | |
9140 : BinExp(loc, TOKmul, sizeof(MulExp), e1, e2) | |
9141 { | |
9142 } | |
9143 | |
9144 Expression *MulExp::semantic(Scope *sc) | |
9145 { Expression *e; | |
9146 | |
9147 #if 0 | |
9148 printf("MulExp::semantic() %s\n", toChars()); | |
9149 #endif | |
9150 if (type) | |
9151 { | |
9152 return this; | |
9153 } | |
9154 | |
9155 BinExp::semanticp(sc); | |
9156 e = op_overload(sc); | |
9157 if (e) | |
9158 return e; | |
9159 | |
9160 typeCombine(sc); | |
1587 | 9161 if (!e1->isArrayOperand()) |
9162 e1->checkArithmetic(); | |
9163 if (!e2->isArrayOperand()) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9164 e2->checkArithmetic(); |
159 | 9165 if (type->isfloating()) |
9166 { Type *t1 = e1->type; | |
9167 Type *t2 = e2->type; | |
9168 | |
9169 if (t1->isreal()) | |
9170 { | |
9171 type = t2; | |
9172 } | |
9173 else if (t2->isreal()) | |
9174 { | |
9175 type = t1; | |
9176 } | |
9177 else if (t1->isimaginary()) | |
9178 { | |
9179 if (t2->isimaginary()) | |
9180 { Expression *e; | |
9181 | |
9182 switch (t1->ty) | |
9183 { | |
9184 case Timaginary32: type = Type::tfloat32; break; | |
9185 case Timaginary64: type = Type::tfloat64; break; | |
9186 case Timaginary80: type = Type::tfloat80; break; | |
9187 default: assert(0); | |
9188 } | |
9189 | |
9190 // iy * iv = -yv | |
9191 e1->type = type; | |
9192 e2->type = type; | |
9193 e = new NegExp(loc, this); | |
9194 e = e->semantic(sc); | |
9195 return e; | |
9196 } | |
9197 else | |
9198 type = t2; // t2 is complex | |
9199 } | |
9200 else if (t2->isimaginary()) | |
9201 { | |
9202 type = t1; // t1 is complex | |
9203 } | |
9204 } | |
9205 return this; | |
9206 } | |
9207 | |
9208 /************************************************************/ | |
9209 | |
9210 DivExp::DivExp(Loc loc, Expression *e1, Expression *e2) | |
9211 : BinExp(loc, TOKdiv, sizeof(DivExp), e1, e2) | |
9212 { | |
9213 } | |
9214 | |
9215 Expression *DivExp::semantic(Scope *sc) | |
9216 { Expression *e; | |
9217 | |
9218 if (type) | |
9219 return this; | |
9220 | |
9221 BinExp::semanticp(sc); | |
9222 e = op_overload(sc); | |
9223 if (e) | |
9224 return e; | |
9225 | |
9226 typeCombine(sc); | |
1587 | 9227 if (!e1->isArrayOperand()) |
9228 e1->checkArithmetic(); | |
9229 if (!e2->isArrayOperand()) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9230 e2->checkArithmetic(); |
159 | 9231 if (type->isfloating()) |
9232 { Type *t1 = e1->type; | |
9233 Type *t2 = e2->type; | |
9234 | |
9235 if (t1->isreal()) | |
9236 { | |
9237 type = t2; | |
9238 if (t2->isimaginary()) | |
9239 { Expression *e; | |
9240 | |
9241 // x/iv = i(-x/v) | |
9242 e2->type = t1; | |
9243 e = new NegExp(loc, this); | |
9244 e = e->semantic(sc); | |
9245 return e; | |
9246 } | |
9247 } | |
9248 else if (t2->isreal()) | |
9249 { | |
9250 type = t1; | |
9251 } | |
9252 else if (t1->isimaginary()) | |
9253 { | |
9254 if (t2->isimaginary()) | |
9255 { | |
9256 switch (t1->ty) | |
9257 { | |
9258 case Timaginary32: type = Type::tfloat32; break; | |
9259 case Timaginary64: type = Type::tfloat64; break; | |
9260 case Timaginary80: type = Type::tfloat80; break; | |
9261 default: assert(0); | |
9262 } | |
9263 } | |
9264 else | |
9265 type = t2; // t2 is complex | |
9266 } | |
9267 else if (t2->isimaginary()) | |
9268 { | |
9269 type = t1; // t1 is complex | |
9270 } | |
9271 } | |
9272 return this; | |
9273 } | |
9274 | |
9275 /************************************************************/ | |
9276 | |
9277 ModExp::ModExp(Loc loc, Expression *e1, Expression *e2) | |
9278 : BinExp(loc, TOKmod, sizeof(ModExp), e1, e2) | |
9279 { | |
9280 } | |
9281 | |
9282 Expression *ModExp::semantic(Scope *sc) | |
9283 { Expression *e; | |
9284 | |
9285 if (type) | |
9286 return this; | |
9287 | |
9288 BinExp::semanticp(sc); | |
9289 e = op_overload(sc); | |
9290 if (e) | |
9291 return e; | |
9292 | |
9293 typeCombine(sc); | |
1587 | 9294 if (!e1->isArrayOperand()) |
9295 e1->checkArithmetic(); | |
9296 if (!e2->isArrayOperand()) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9297 e2->checkArithmetic(); |
159 | 9298 if (type->isfloating()) |
9299 { type = e1->type; | |
9300 if (e2->type->iscomplex()) | |
9301 { error("cannot perform modulo complex arithmetic"); | |
9302 return new IntegerExp(0); | |
9303 } | |
9304 } | |
9305 return this; | |
9306 } | |
9307 | |
9308 /************************************************************/ | |
9309 | |
9310 ShlExp::ShlExp(Loc loc, Expression *e1, Expression *e2) | |
9311 : BinExp(loc, TOKshl, sizeof(ShlExp), e1, e2) | |
9312 { | |
9313 } | |
9314 | |
9315 Expression *ShlExp::semantic(Scope *sc) | |
9316 { Expression *e; | |
9317 | |
9318 //printf("ShlExp::semantic(), type = %p\n", type); | |
9319 if (!type) | |
9320 { BinExp::semanticp(sc); | |
9321 e = op_overload(sc); | |
9322 if (e) | |
9323 return e; | |
9324 e1 = e1->checkIntegral(); | |
9325 e2 = e2->checkIntegral(); | |
9326 e1 = e1->integralPromotions(sc); | |
9327 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9328 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 9329 type = e1->type; |
9330 } | |
9331 return this; | |
9332 } | |
9333 | |
9334 /************************************************************/ | |
9335 | |
9336 ShrExp::ShrExp(Loc loc, Expression *e1, Expression *e2) | |
9337 : BinExp(loc, TOKshr, sizeof(ShrExp), e1, e2) | |
9338 { | |
9339 } | |
9340 | |
9341 Expression *ShrExp::semantic(Scope *sc) | |
9342 { Expression *e; | |
9343 | |
9344 if (!type) | |
9345 { BinExp::semanticp(sc); | |
9346 e = op_overload(sc); | |
9347 if (e) | |
9348 return e; | |
9349 e1 = e1->checkIntegral(); | |
9350 e2 = e2->checkIntegral(); | |
9351 e1 = e1->integralPromotions(sc); | |
9352 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9353 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 9354 type = e1->type; |
9355 } | |
9356 return this; | |
9357 } | |
9358 | |
9359 /************************************************************/ | |
9360 | |
9361 UshrExp::UshrExp(Loc loc, Expression *e1, Expression *e2) | |
9362 : BinExp(loc, TOKushr, sizeof(UshrExp), e1, e2) | |
9363 { | |
9364 } | |
9365 | |
9366 Expression *UshrExp::semantic(Scope *sc) | |
9367 { Expression *e; | |
9368 | |
9369 if (!type) | |
9370 { BinExp::semanticp(sc); | |
9371 e = op_overload(sc); | |
9372 if (e) | |
9373 return e; | |
9374 e1 = e1->checkIntegral(); | |
9375 e2 = e2->checkIntegral(); | |
9376 e1 = e1->integralPromotions(sc); | |
9377 //e2 = e2->castTo(sc, Type::tshiftcnt); | |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
9378 e2 = e2->castTo(sc, e1->type); // LDC |
159 | 9379 type = e1->type; |
9380 } | |
9381 return this; | |
9382 } | |
9383 | |
9384 /************************************************************/ | |
9385 | |
9386 AndExp::AndExp(Loc loc, Expression *e1, Expression *e2) | |
9387 : BinExp(loc, TOKand, sizeof(AndExp), e1, e2) | |
9388 { | |
9389 } | |
9390 | |
9391 Expression *AndExp::semantic(Scope *sc) | |
9392 { Expression *e; | |
9393 | |
9394 if (!type) | |
9395 { BinExp::semanticp(sc); | |
9396 e = op_overload(sc); | |
9397 if (e) | |
9398 return e; | |
9399 if (e1->type->toBasetype()->ty == Tbool && | |
9400 e2->type->toBasetype()->ty == Tbool) | |
9401 { | |
9402 type = e1->type; | |
9403 e = this; | |
9404 } | |
9405 else | |
9406 { | |
9407 typeCombine(sc); | |
1587 | 9408 if (!e1->isArrayOperand()) |
9409 e1->checkIntegral(); | |
9410 if (!e2->isArrayOperand()) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9411 e2->checkIntegral(); |
159 | 9412 } |
9413 } | |
9414 return this; | |
9415 } | |
9416 | |
9417 /************************************************************/ | |
9418 | |
9419 OrExp::OrExp(Loc loc, Expression *e1, Expression *e2) | |
9420 : BinExp(loc, TOKor, sizeof(OrExp), e1, e2) | |
9421 { | |
9422 } | |
9423 | |
9424 Expression *OrExp::semantic(Scope *sc) | |
9425 { Expression *e; | |
9426 | |
9427 if (!type) | |
9428 { BinExp::semanticp(sc); | |
9429 e = op_overload(sc); | |
9430 if (e) | |
9431 return e; | |
9432 if (e1->type->toBasetype()->ty == Tbool && | |
9433 e2->type->toBasetype()->ty == Tbool) | |
9434 { | |
9435 type = e1->type; | |
9436 e = this; | |
9437 } | |
9438 else | |
9439 { | |
9440 typeCombine(sc); | |
1587 | 9441 if (!e1->isArrayOperand()) |
9442 e1->checkIntegral(); | |
9443 if (!e2->isArrayOperand()) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9444 e2->checkIntegral(); |
159 | 9445 } |
9446 } | |
9447 return this; | |
9448 } | |
9449 | |
9450 /************************************************************/ | |
9451 | |
9452 XorExp::XorExp(Loc loc, Expression *e1, Expression *e2) | |
9453 : BinExp(loc, TOKxor, sizeof(XorExp), e1, e2) | |
9454 { | |
9455 } | |
9456 | |
9457 Expression *XorExp::semantic(Scope *sc) | |
9458 { Expression *e; | |
9459 | |
9460 if (!type) | |
9461 { BinExp::semanticp(sc); | |
9462 e = op_overload(sc); | |
9463 if (e) | |
9464 return e; | |
9465 if (e1->type->toBasetype()->ty == Tbool && | |
9466 e2->type->toBasetype()->ty == Tbool) | |
9467 { | |
9468 type = e1->type; | |
9469 e = this; | |
9470 } | |
9471 else | |
9472 { | |
9473 typeCombine(sc); | |
1587 | 9474 if (!e1->isArrayOperand()) |
9475 e1->checkIntegral(); | |
9476 if (!e2->isArrayOperand()) | |
510
6aee82889553
Merged DMD 1.034, array operations are not yet implemented ;)
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
486
diff
changeset
|
9477 e2->checkIntegral(); |
159 | 9478 } |
9479 } | |
9480 return this; | |
9481 } | |
9482 | |
9483 | |
9484 /************************************************************/ | |
9485 | |
9486 OrOrExp::OrOrExp(Loc loc, Expression *e1, Expression *e2) | |
9487 : BinExp(loc, TOKoror, sizeof(OrOrExp), e1, e2) | |
9488 { | |
9489 } | |
9490 | |
9491 Expression *OrOrExp::semantic(Scope *sc) | |
9492 { | |
9493 unsigned cs1; | |
9494 | |
9495 // same as for AndAnd | |
9496 e1 = e1->semantic(sc); | |
9497 e1 = resolveProperties(sc, e1); | |
9498 e1 = e1->checkToPointer(); | |
9499 e1 = e1->checkToBoolean(); | |
9500 cs1 = sc->callSuper; | |
9501 | |
9502 if (sc->flags & SCOPEstaticif) | |
9503 { | |
9504 /* If in static if, don't evaluate e2 if we don't have to. | |
9505 */ | |
9506 e1 = e1->optimize(WANTflags); | |
9507 if (e1->isBool(TRUE)) | |
9508 { | |
9509 return new IntegerExp(loc, 1, Type::tboolean); | |
9510 } | |
9511 } | |
9512 | |
9513 e2 = e2->semantic(sc); | |
9514 sc->mergeCallSuper(loc, cs1); | |
9515 e2 = resolveProperties(sc, e2); | |
9516 e2 = e2->checkToPointer(); | |
9517 | |
9518 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
|
9519 if (e2->type->ty == Tvoid) |
159 | 9520 type = Type::tvoid; |
9521 if (e2->op == TOKtype || e2->op == TOKimport) | |
9522 error("%s is not an expression", e2->toChars()); | |
9523 return this; | |
9524 } | |
9525 | |
9526 Expression *OrOrExp::checkToBoolean() | |
9527 { | |
9528 e2 = e2->checkToBoolean(); | |
9529 return this; | |
9530 } | |
9531 | |
9532 int OrOrExp::isBit() | |
9533 { | |
9534 return TRUE; | |
9535 } | |
9536 | |
9537 int OrOrExp::checkSideEffect(int flag) | |
9538 { | |
9539 if (flag == 2) | |
9540 { | |
9541 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9542 } | |
9543 else | |
9544 { e1->checkSideEffect(1); | |
9545 return e2->checkSideEffect(flag); | |
9546 } | |
9547 } | |
9548 | |
9549 /************************************************************/ | |
9550 | |
9551 AndAndExp::AndAndExp(Loc loc, Expression *e1, Expression *e2) | |
9552 : BinExp(loc, TOKandand, sizeof(AndAndExp), e1, e2) | |
9553 { | |
9554 } | |
9555 | |
9556 Expression *AndAndExp::semantic(Scope *sc) | |
9557 { | |
9558 unsigned cs1; | |
9559 | |
9560 // same as for OrOr | |
9561 e1 = e1->semantic(sc); | |
9562 e1 = resolveProperties(sc, e1); | |
9563 e1 = e1->checkToPointer(); | |
9564 e1 = e1->checkToBoolean(); | |
9565 cs1 = sc->callSuper; | |
9566 | |
9567 if (sc->flags & SCOPEstaticif) | |
9568 { | |
9569 /* If in static if, don't evaluate e2 if we don't have to. | |
9570 */ | |
9571 e1 = e1->optimize(WANTflags); | |
9572 if (e1->isBool(FALSE)) | |
9573 { | |
9574 return new IntegerExp(loc, 0, Type::tboolean); | |
9575 } | |
9576 } | |
9577 | |
9578 e2 = e2->semantic(sc); | |
9579 sc->mergeCallSuper(loc, cs1); | |
9580 e2 = resolveProperties(sc, e2); | |
9581 e2 = e2->checkToPointer(); | |
9582 | |
9583 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
|
9584 if (e2->type->ty == Tvoid) |
159 | 9585 type = Type::tvoid; |
9586 if (e2->op == TOKtype || e2->op == TOKimport) | |
9587 error("%s is not an expression", e2->toChars()); | |
9588 return this; | |
9589 } | |
9590 | |
9591 Expression *AndAndExp::checkToBoolean() | |
9592 { | |
9593 e2 = e2->checkToBoolean(); | |
9594 return this; | |
9595 } | |
9596 | |
9597 int AndAndExp::isBit() | |
9598 { | |
9599 return TRUE; | |
9600 } | |
9601 | |
9602 int AndAndExp::checkSideEffect(int flag) | |
9603 { | |
9604 if (flag == 2) | |
9605 { | |
9606 return e1->checkSideEffect(2) || e2->checkSideEffect(2); | |
9607 } | |
9608 else | |
9609 { | |
9610 e1->checkSideEffect(1); | |
9611 return e2->checkSideEffect(flag); | |
9612 } | |
9613 } | |
9614 | |
9615 /************************************************************/ | |
9616 | |
9617 InExp::InExp(Loc loc, Expression *e1, Expression *e2) | |
9618 : BinExp(loc, TOKin, sizeof(InExp), e1, e2) | |
9619 { | |
9620 } | |
9621 | |
9622 Expression *InExp::semantic(Scope *sc) | |
9623 { Expression *e; | |
9624 | |
9625 if (type) | |
9626 return this; | |
9627 | |
9628 BinExp::semanticp(sc); | |
9629 e = op_overload(sc); | |
9630 if (e) | |
9631 return e; | |
9632 | |
9633 //type = Type::tboolean; | |
9634 Type *t2b = e2->type->toBasetype(); | |
9635 if (t2b->ty != Taarray) | |
9636 { | |
9637 error("rvalue of in expression must be an associative array, not %s", e2->type->toChars()); | |
9638 type = Type::terror; | |
9639 } | |
9640 else | |
9641 { | |
9642 TypeAArray *ta = (TypeAArray *)t2b; | |
9643 | |
9644 // Convert key to type of key | |
9645 e1 = e1->implicitCastTo(sc, ta->index); | |
9646 | |
9647 // 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
|
9648 type = ta->nextOf()->pointerTo(); |
159 | 9649 } |
9650 return this; | |
9651 } | |
9652 | |
9653 int InExp::isBit() | |
9654 { | |
9655 return FALSE; | |
9656 } | |
9657 | |
9658 | |
9659 /************************************************************/ | |
9660 | |
9661 /* This deletes the key e1 from the associative array e2 | |
9662 */ | |
9663 | |
9664 RemoveExp::RemoveExp(Loc loc, Expression *e1, Expression *e2) | |
9665 : BinExp(loc, TOKremove, sizeof(RemoveExp), e1, e2) | |
9666 { | |
9667 type = Type::tvoid; | |
9668 } | |
9669 | |
9670 /************************************************************/ | |
9671 | |
9672 CmpExp::CmpExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9673 : BinExp(loc, op, sizeof(CmpExp), e1, e2) | |
9674 { | |
9675 } | |
9676 | |
9677 Expression *CmpExp::semantic(Scope *sc) | |
9678 { Expression *e; | |
9679 | |
9680 #if LOGSEMANTIC | |
9681 printf("CmpExp::semantic('%s')\n", toChars()); | |
9682 #endif | |
9683 if (type) | |
9684 return this; | |
9685 | |
9686 BinExp::semanticp(sc); | |
9687 | |
1587 | 9688 Type *t1 = e1->type->toBasetype(); |
9689 Type *t2 = e2->type->toBasetype(); | |
9690 if (t1->ty == Tclass && e2->op == TOKnull || | |
9691 t2->ty == Tclass && e1->op == TOKnull) | |
159 | 9692 { |
9693 error("do not use null when comparing class types"); | |
9694 } | |
9695 | |
9696 e = op_overload(sc); | |
9697 if (e) | |
9698 { | |
1367
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9699 if (!e->type->isscalar() && e->type->equals(e1->type)) |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9700 { |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9701 error("recursive opCmp expansion"); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9702 e = new ErrorExp(); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9703 } |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9704 else |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9705 { e = new CmpExp(op, loc, e, new IntegerExp(loc, 0, Type::tint32)); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9706 e = e->semantic(sc); |
8026319762be
Merged DMD 1.045 !!!
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents:
1358
diff
changeset
|
9707 } |
159 | 9708 return e; |
9709 } | |
9710 | |
1587 | 9711 /* Disallow comparing T[]==T and T==T[] |
9712 */ | |
9713 if (e1->op == TOKslice && t1->ty == Tarray && e2->implicitConvTo(t1->nextOf()) || | |
9714 e2->op == TOKslice && t2->ty == Tarray && e1->implicitConvTo(t2->nextOf())) | |
9715 { | |
9716 incompatibleTypes(); | |
9717 return new ErrorExp(); | |
9718 } | |
9719 | |
159 | 9720 typeCombine(sc); |
9721 type = Type::tboolean; | |
9722 | |
9723 // Special handling for array comparisons | |
9724 t1 = e1->type->toBasetype(); | |
9725 t2 = e2->type->toBasetype(); | |
9726 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9727 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9728 { | |
9729 if (!t1->next->equals(t2->next)) | |
9730 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9731 e = this; | |
9732 } | |
9733 else if (t1->ty == Tstruct || t2->ty == Tstruct || | |
9734 (t1->ty == Tclass && t2->ty == Tclass)) | |
9735 { | |
9736 if (t2->ty == Tstruct) | |
9737 error("need member function opCmp() for %s %s to compare", t2->toDsymbol(sc)->kind(), t2->toChars()); | |
9738 else | |
9739 error("need member function opCmp() for %s %s to compare", t1->toDsymbol(sc)->kind(), t1->toChars()); | |
9740 e = this; | |
9741 } | |
9742 #if 1 | |
9743 else if (t1->iscomplex() || t2->iscomplex()) | |
9744 { | |
9745 error("compare not defined for complex operands"); | |
9746 e = new IntegerExp(0); | |
9747 } | |
9748 #endif | |
9749 else | |
9750 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
|
9751 //printf("CmpExp: %s\n", e->toChars()); |
159 | 9752 return e; |
9753 } | |
9754 | |
9755 int CmpExp::isBit() | |
9756 { | |
9757 return TRUE; | |
9758 } | |
9759 | |
9760 | |
9761 /************************************************************/ | |
9762 | |
9763 EqualExp::EqualExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9764 : BinExp(loc, op, sizeof(EqualExp), e1, e2) | |
9765 { | |
9766 assert(op == TOKequal || op == TOKnotequal); | |
9767 } | |
9768 | |
9769 Expression *EqualExp::semantic(Scope *sc) | |
9770 { Expression *e; | |
9771 | |
9772 //printf("EqualExp::semantic('%s')\n", toChars()); | |
9773 if (type) | |
9774 return this; | |
9775 | |
9776 BinExp::semanticp(sc); | |
9777 | |
9778 /* Before checking for operator overloading, check to see if we're | |
9779 * comparing the addresses of two statics. If so, we can just see | |
9780 * if they are the same symbol. | |
9781 */ | |
9782 if (e1->op == TOKaddress && e2->op == TOKaddress) | |
9783 { AddrExp *ae1 = (AddrExp *)e1; | |
9784 AddrExp *ae2 = (AddrExp *)e2; | |
9785 | |
9786 if (ae1->e1->op == TOKvar && ae2->e1->op == TOKvar) | |
9787 { VarExp *ve1 = (VarExp *)ae1->e1; | |
9788 VarExp *ve2 = (VarExp *)ae2->e1; | |
9789 | |
9790 if (ve1->var == ve2->var /*|| ve1->var->toSymbol() == ve2->var->toSymbol()*/) | |
9791 { | |
9792 // They are the same, result is 'true' for ==, 'false' for != | |
9793 e = new IntegerExp(loc, (op == TOKequal), Type::tboolean); | |
9794 return e; | |
9795 } | |
9796 } | |
9797 } | |
9798 | |
1587 | 9799 Type *t1 = e1->type->toBasetype(); |
9800 Type *t2 = e2->type->toBasetype(); | |
9801 if (t1->ty == Tclass && e2->op == TOKnull || | |
9802 t2->ty == Tclass && e1->op == TOKnull) | |
159 | 9803 { |
9804 error("use '%s' instead of '%s' when comparing with null", | |
9805 Token::toChars(op == TOKequal ? TOKidentity : TOKnotidentity), | |
9806 Token::toChars(op)); | |
9807 } | |
9808 | |
9809 //if (e2->op != TOKnull) | |
9810 { | |
9811 e = op_overload(sc); | |
9812 if (e) | |
9813 { | |
9814 if (op == TOKnotequal) | |
9815 { | |
9816 e = new NotExp(e->loc, e); | |
9817 e = e->semantic(sc); | |
9818 } | |
9819 return e; | |
9820 } | |
9821 } | |
9822 | |
1587 | 9823 /* Disallow comparing T[]==T and T==T[] |
9824 */ | |
9825 if (e1->op == TOKslice && t1->ty == Tarray && e2->implicitConvTo(t1->nextOf()) || | |
9826 e2->op == TOKslice && t2->ty == Tarray && e1->implicitConvTo(t2->nextOf())) | |
9827 { | |
9828 incompatibleTypes(); | |
9829 return new ErrorExp(); | |
9830 } | |
9831 | |
159 | 9832 e = typeCombine(sc); |
9833 type = Type::tboolean; | |
9834 | |
9835 // Special handling for array comparisons | |
9836 t1 = e1->type->toBasetype(); | |
9837 t2 = e2->type->toBasetype(); | |
9838 if ((t1->ty == Tarray || t1->ty == Tsarray) && | |
9839 (t2->ty == Tarray || t2->ty == Tsarray)) | |
9840 { | |
9841 if (!t1->next->equals(t2->next)) | |
9842 error("array comparison type mismatch, %s vs %s", t1->next->toChars(), t2->next->toChars()); | |
9843 } | |
9844 else | |
9845 { | |
9846 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9847 { | |
9848 // Cast both to complex | |
9849 e1 = e1->castTo(sc, Type::tcomplex80); | |
9850 e2 = e2->castTo(sc, Type::tcomplex80); | |
9851 } | |
9852 } | |
9853 return e; | |
9854 } | |
9855 | |
9856 int EqualExp::isBit() | |
9857 { | |
9858 return TRUE; | |
9859 } | |
9860 | |
9861 | |
9862 | |
9863 /************************************************************/ | |
9864 | |
9865 IdentityExp::IdentityExp(enum TOK op, Loc loc, Expression *e1, Expression *e2) | |
9866 : BinExp(loc, op, sizeof(IdentityExp), e1, e2) | |
9867 { | |
9868 } | |
9869 | |
9870 Expression *IdentityExp::semantic(Scope *sc) | |
9871 { | |
9872 if (type) | |
9873 return this; | |
9874 | |
9875 BinExp::semanticp(sc); | |
9876 type = Type::tboolean; | |
9877 typeCombine(sc); | |
9878 if (e1->type != e2->type && e1->type->isfloating() && e2->type->isfloating()) | |
9879 { | |
9880 // Cast both to complex | |
9881 e1 = e1->castTo(sc, Type::tcomplex80); | |
9882 e2 = e2->castTo(sc, Type::tcomplex80); | |
9883 } | |
9884 return this; | |
9885 } | |
9886 | |
9887 int IdentityExp::isBit() | |
9888 { | |
9889 return TRUE; | |
9890 } | |
9891 | |
9892 | |
9893 /****************************************************************/ | |
9894 | |
9895 CondExp::CondExp(Loc loc, Expression *econd, Expression *e1, Expression *e2) | |
9896 : BinExp(loc, TOKquestion, sizeof(CondExp), e1, e2) | |
9897 { | |
9898 this->econd = econd; | |
9899 } | |
9900 | |
9901 Expression *CondExp::syntaxCopy() | |
9902 { | |
9903 return new CondExp(loc, econd->syntaxCopy(), e1->syntaxCopy(), e2->syntaxCopy()); | |
9904 } | |
9905 | |
9906 | |
9907 Expression *CondExp::semantic(Scope *sc) | |
9908 { Type *t1; | |
9909 Type *t2; | |
9910 unsigned cs0; | |
9911 unsigned cs1; | |
9912 | |
9913 #if LOGSEMANTIC | |
9914 printf("CondExp::semantic('%s')\n", toChars()); | |
9915 #endif | |
9916 if (type) | |
9917 return this; | |
9918 | |
9919 econd = econd->semantic(sc); | |
9920 econd = resolveProperties(sc, econd); | |
9921 econd = econd->checkToPointer(); | |
9922 econd = econd->checkToBoolean(); | |
9923 | |
9924 #if 0 /* this cannot work right because the types of e1 and e2 | |
9925 * both contribute to the type of the result. | |
9926 */ | |
9927 if (sc->flags & SCOPEstaticif) | |
9928 { | |
9929 /* If in static if, don't evaluate what we don't have to. | |
9930 */ | |
9931 econd = econd->optimize(WANTflags); | |
9932 if (econd->isBool(TRUE)) | |
9933 { | |
9934 e1 = e1->semantic(sc); | |
9935 e1 = resolveProperties(sc, e1); | |
9936 return e1; | |
9937 } | |
9938 else if (econd->isBool(FALSE)) | |
9939 { | |
9940 e2 = e2->semantic(sc); | |
9941 e2 = resolveProperties(sc, e2); | |
9942 return e2; | |
9943 } | |
9944 } | |
9945 #endif | |
9946 | |
9947 | |
9948 cs0 = sc->callSuper; | |
9949 e1 = e1->semantic(sc); | |
9950 e1 = resolveProperties(sc, e1); | |
9951 cs1 = sc->callSuper; | |
9952 sc->callSuper = cs0; | |
9953 e2 = e2->semantic(sc); | |
9954 e2 = resolveProperties(sc, e2); | |
9955 sc->mergeCallSuper(loc, cs1); | |
9956 | |
9957 | |
9958 // If either operand is void, the result is void | |
9959 t1 = e1->type; | |
9960 t2 = e2->type; | |
9961 if (t1->ty == Tvoid || t2->ty == Tvoid) | |
9962 type = Type::tvoid; | |
9963 else if (t1 == t2) | |
9964 type = t1; | |
9965 else | |
9966 { | |
9967 typeCombine(sc); | |
9968 switch (e1->type->toBasetype()->ty) | |
9969 { | |
9970 case Tcomplex32: | |
9971 case Tcomplex64: | |
9972 case Tcomplex80: | |
9973 e2 = e2->castTo(sc, e1->type); | |
9974 break; | |
9975 } | |
9976 switch (e2->type->toBasetype()->ty) | |
9977 { | |
9978 case Tcomplex32: | |
9979 case Tcomplex64: | |
9980 case Tcomplex80: | |
9981 e1 = e1->castTo(sc, e2->type); | |
9982 break; | |
9983 } | |
9984 } | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9985 #if 0 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9986 printf("res: %s\n", type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9987 printf("e1 : %s\n", e1->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9988 printf("e2 : %s\n", e2->type->toChars()); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9989 #endif |
159 | 9990 return this; |
9991 } | |
9992 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9993 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9994 int CondExp::isLvalue() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9995 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9996 return e1->isLvalue() && e2->isLvalue(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9997 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9998 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
9999 |
159 | 10000 Expression *CondExp::toLvalue(Scope *sc, Expression *ex) |
10001 { | |
10002 PtrExp *e; | |
10003 | |
10004 // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) | |
10005 e = new PtrExp(loc, this, type); | |
10006 | |
10007 e1 = e1->addressOf(sc); | |
10008 //e1 = e1->toLvalue(sc, NULL); | |
10009 | |
10010 e2 = e2->addressOf(sc); | |
10011 //e2 = e2->toLvalue(sc, NULL); | |
10012 | |
10013 typeCombine(sc); | |
10014 | |
10015 type = e2->type; | |
10016 return e; | |
10017 } | |
10018 | |
10019 Expression *CondExp::modifiableLvalue(Scope *sc, Expression *e) | |
10020 { | |
10021 error("conditional expression %s is not a modifiable lvalue", toChars()); | |
10022 return this; | |
10023 } | |
10024 | |
10025 void CondExp::checkEscape() | |
10026 { | |
10027 e1->checkEscape(); | |
10028 e2->checkEscape(); | |
10029 } | |
10030 | |
1618
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
10031 void CondExp::checkEscapeRef() |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
10032 { |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
10033 e1->checkEscapeRef(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
10034 e2->checkEscapeRef(); |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
10035 } |
a87f1d6ff48e
Merge DMD r303: harmonize
Leandro Lucarella <llucax@gmail.com>
parents:
1616
diff
changeset
|
10036 |
159 | 10037 |
10038 Expression *CondExp::checkToBoolean() | |
10039 { | |
10040 e1 = e1->checkToBoolean(); | |
10041 e2 = e2->checkToBoolean(); | |
10042 return this; | |
10043 } | |
10044 | |
10045 int CondExp::checkSideEffect(int flag) | |
10046 { | |
10047 if (flag == 2) | |
10048 { | |
10049 return econd->checkSideEffect(2) || | |
10050 e1->checkSideEffect(2) || | |
10051 e2->checkSideEffect(2); | |
10052 } | |
10053 else | |
10054 { | |
10055 econd->checkSideEffect(1); | |
10056 e1->checkSideEffect(flag); | |
10057 return e2->checkSideEffect(flag); | |
10058 } | |
10059 } | |
10060 | |
875
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10061 #if DMDV2 |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10062 int CondExp::canThrow() |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10063 { |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10064 return econd->canThrow() || e1->canThrow() || e2->canThrow(); |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10065 } |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10066 #endif |
330f999ade44
Merged DMD 1.038
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents:
846
diff
changeset
|
10067 |
159 | 10068 void CondExp::toCBuffer(OutBuffer *buf, HdrGenState *hgs) |
10069 { | |
10070 expToCBuffer(buf, hgs, econd, PREC_oror); | |
10071 buf->writestring(" ? "); | |
10072 expToCBuffer(buf, hgs, e1, PREC_expr); | |
10073 buf->writestring(" : "); | |
10074 expToCBuffer(buf, hgs, e2, PREC_cond); | |
10075 } | |
10076 | |
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
|
10077 /************************************************************/ |
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
|
10078 |
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
|
10079 #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
|
10080 |
664
eef8ac26c66c
Some missed LLVMDC -> LDC.
Christian Kamm <kamm incasoftware de>
parents:
639
diff
changeset
|
10081 // 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
|
10082 |
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
|
10083 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
|
10084 : 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
|
10085 { |
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
|
10086 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
|
10087 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
|
10088 } |
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
|
10089 |
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
|
10090 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
|
10091 { |
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
|
10092 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
|
10093 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
|
10094 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
|
10095 } |
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
|
10096 |
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
|
10097 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
|
10098 { |
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
|
10099 // 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
|
10100 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
|
10101 } |
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
|
10102 |
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
|
10103 #endif |