root/branches/dmdfe-2.0/expression.h

Revision 914, 40.4 kB (checked in by Gregor, 3 months ago)

MERGE: DMD 2.019

Line 
1 // Compiler implementation of the D programming language
2 // Copyright (c) 1999-2008 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
9
10 #ifndef DMD_EXPRESSION_H
11 #define DMD_EXPRESSION_H
12
13 #include "mars.h"
14 #include "identifier.h"
15 #include "lexer.h"
16 #include "arraytypes.h"
17
18 struct Type;
19 struct Scope;
20 struct TupleDeclaration;
21 struct VarDeclaration;
22 struct FuncDeclaration;
23 struct FuncLiteralDeclaration;
24 struct Declaration;
25 struct CtorDeclaration;
26 struct NewDeclaration;
27 struct Dsymbol;
28 struct Import;
29 struct Module;
30 struct ScopeDsymbol;
31 struct InlineCostState;
32 struct InlineDoState;
33 struct InlineScanState;
34 struct Expression;
35 struct Declaration;
36 struct AggregateDeclaration;
37 struct StructDeclaration;
38 struct TemplateInstance;
39 struct TemplateDeclaration;
40 struct ClassDeclaration;
41 struct HdrGenState;
42 struct BinExp;
43 struct InterState;
44 struct Symbol;      // back end symbol
45 struct OverloadSet;
46
47 enum TOK;
48
49 // Back end
50 struct IRState;
51 struct dt_t;
52
53 #ifdef IN_GCC
54 union tree_node; typedef union tree_node elem;
55 #else
56 struct elem;
57 #endif
58
59 void initPrecedence();
60
61 Expression *resolveProperties(Scope *sc, Expression *e);
62 void accessCheck(Loc loc, Scope *sc, Expression *e, Declaration *d);
63 Expression *build_overload(Loc loc, Scope *sc, Expression *ethis, Expression *earg, Identifier *id);
64 Dsymbol *search_function(ScopeDsymbol *ad, Identifier *funcid);
65 void inferApplyArgTypes(enum TOK op, Arguments *arguments, Expression *aggr);
66 void argExpTypesToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs);
67 void argsToCBuffer(OutBuffer *buf, Expressions *arguments, HdrGenState *hgs);
68 void expandTuples(Expressions *exps);
69 FuncDeclaration *hasThis(Scope *sc);
70 Expression *fromConstInitializer(int result, Expression *e);
71 int arrayExpressionCanThrow(Expressions *exps);
72
73 struct Expression : Object
74 {
75     Loc loc;            // file location
76     enum TOK op;        // handy to minimize use of dynamic_cast
77     Type *type;         // !=NULL means that semantic() has been run
78     int size;           // # of bytes in Expression so we can copy() it
79
80     Expression(Loc loc, enum TOK op, int size);
81     Expression *copy();
82     virtual Expression *syntaxCopy();
83     virtual Expression *semantic(Scope *sc);
84
85     int dyncast() { return DYNCAST_EXPRESSION; }    // kludge for template.isExpression()
86
87     void print();
88     char *toChars();
89     virtual void dump(int indent);
90     void error(const char *format, ...);
91     virtual void rvalue();
92
93     static Expression *combine(Expression *e1, Expression *e2);
94     static Expressions *arraySyntaxCopy(Expressions *exps);
95
96     virtual integer_t toInteger();
97     virtual uinteger_t toUInteger();
98     virtual real_t toReal();
99     virtual real_t toImaginary();
100     virtual complex_t toComplex();
101     virtual void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
102     virtual void toMangleBuffer(OutBuffer *buf);
103     virtual int isLvalue();
104     virtual Expression *toLvalue(Scope *sc, Expression *e);
105     virtual Expression *modifiableLvalue(Scope *sc, Expression *e);
106     Expression *implicitCastTo(Scope *sc, Type *t);
107     virtual MATCH implicitConvTo(Type *t);
108     virtual Expression *castTo(Scope *sc, Type *t);
109     virtual void checkEscape();
110     void checkScalar();
111     void checkNoBool();
112     Expression *checkIntegral();
113     Expression *checkArithmetic();
114     void checkDeprecated(Scope *sc, Dsymbol *s);
115     virtual Expression *checkToBoolean();
116     Expression *checkToPointer();
117     Expression *addressOf(Scope *sc);
118     Expression *deref();
119     Expression *integralPromotions(Scope *sc);
120
121     Expression *toDelegate(Scope *sc, Type *t);
122     virtual void scanForNestedRef(Scope *sc);
123
124     virtual Expression *optimize(int result);
125     #define WANTflags   1
126     #define WANTvalue   2
127     #define WANTinterpret 4
128
129     virtual Expression *interpret(InterState *istate);
130
131     virtual int isConst();
132     virtual int isBool(int result);
133     virtual int isBit();
134     virtual int checkSideEffect(int flag);
135     virtual int canThrow();
136
137     virtual int inlineCost(InlineCostState *ics);
138     virtual Expression *doInline(InlineDoState *ids);
139     virtual Expression *inlineScan(InlineScanState *iss);
140
141     // For operator overloading
142     virtual int isCommutative();
143     virtual Identifier *opId();
144     virtual Identifier *opId_r();
145
146     // For array ops
147     virtual void buildArrayIdent(OutBuffer *buf, Expressions *arguments);
148     virtual Expression *buildArrayLoop(Arguments *fparams);
149 };
150
151 struct IntegerExp : Expression
152 {
153     integer_t value;
154
155     IntegerExp(Loc loc, integer_t value, Type *type);
156     IntegerExp(integer_t value);
157     int equals(Object *o);
158     Expression *semantic(Scope *sc);
159     Expression *interpret(InterState *istate);
160     char *toChars();
161     void dump(int indent);
162     integer_t toInteger();
163     real_t toReal();
164     real_t toImaginary();
165     complex_t toComplex();
166     int isConst();
167     int isBool(int result);
168     MATCH implicitConvTo(Type *t);
169     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
170     void toMangleBuffer(OutBuffer *buf);
171     Expression *toLvalue(Scope *sc, Expression *e);
172 };
173
174 struct RealExp : Expression
175 {
176     real_t value;
177
178     RealExp(Loc loc, real_t value, Type *type);
179     int equals(Object *o);
180     Expression *semantic(Scope *sc);
181     Expression *interpret(InterState *istate);
182     char *toChars();
183     integer_t toInteger();
184     uinteger_t toUInteger();
185     real_t toReal();
186     real_t toImaginary();
187     complex_t toComplex();
188     Expression *castTo(Scope *sc, Type *t);
189     int isConst();
190     int isBool(int result);
191     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
192     void toMangleBuffer(OutBuffer *buf);
193 };
194
195 struct ComplexExp : Expression
196 {
197     complex_t value;
198
199     ComplexExp(Loc loc, complex_t value, Type *type);
200     int equals(Object *o);
201     Expression *semantic(Scope *sc);
202     Expression *interpret(InterState *istate);
203     char *toChars();
204     integer_t toInteger();
205     uinteger_t toUInteger();
206     real_t toReal();
207     real_t toImaginary();
208     complex_t toComplex();
209     Expression *castTo(Scope *sc, Type *t);
210     int isConst();
211     int isBool(int result);
212     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
213     void toMangleBuffer(OutBuffer *buf);
214 #ifdef _DH
215     OutBuffer hexp;
216 #endif
217 };
218
219 struct IdentifierExp : Expression
220 {
221     Identifier *ident;
222     Declaration *var;
223
224     IdentifierExp(Loc loc, Identifier *ident);
225     IdentifierExp(Loc loc, Declaration *var);
226     Expression *semantic(Scope *sc);
227     char *toChars();
228     void dump(int indent);
229     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
230     int isLvalue();
231     Expression *toLvalue(Scope *sc, Expression *e);
232 };
233
234 struct DollarExp : IdentifierExp
235 {
236     DollarExp(Loc loc);
237 };
238
239 struct DsymbolExp : Expression
240 {
241     Dsymbol *s;
242     int hasOverloads;
243
244     DsymbolExp(Loc loc, Dsymbol *s, int hasOverloads = 0);
245     Expression *semantic(Scope *sc);
246     char *toChars();
247     void dump(int indent);
248     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
249     int isLvalue();
250     Expression *toLvalue(Scope *sc, Expression *e);
251 };
252
253 struct ThisExp : Expression
254 {
255     Declaration *var;
256
257     ThisExp(Loc loc);
258     Expression *semantic(Scope *sc);
259     int isBool(int result);
260     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
261     int isLvalue();
262     Expression *toLvalue(Scope *sc, Expression *e);
263     void scanForNestedRef(Scope *sc);
264
265     int inlineCost(InlineCostState *ics);
266     Expression *doInline(InlineDoState *ids);
267     //Expression *inlineScan(InlineScanState *iss);
268 };
269
270 struct SuperExp : ThisExp
271 {
272     SuperExp(Loc loc);
273     Expression *semantic(Scope *sc);
274     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
275     void scanForNestedRef(Scope *sc);
276
277     int inlineCost(InlineCostState *ics);
278     Expression *doInline(InlineDoState *ids);
279     //Expression *inlineScan(InlineScanState *iss);
280 };
281
282 struct NullExp : Expression
283 {
284     unsigned char committed;    // !=0 if type is committed
285
286     NullExp(Loc loc);
287     Expression *semantic(Scope *sc);
288     int isBool(int result);
289     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
290     void toMangleBuffer(OutBuffer *buf);
291     MATCH implicitConvTo(Type *t);
292     Expression *castTo(Scope *sc, Type *t);
293     Expression *interpret(InterState *istate);
294 };
295
296 struct StringExp : Expression
297 {
298     void *string;   // char, wchar, or dchar data
299     size_t len;     // number of chars, wchars, or dchars
300     unsigned char sz;   // 1: char, 2: wchar, 4: dchar
301     unsigned char committed;    // !=0 if type is committed
302     unsigned char postfix;  // 'c', 'w', 'd'
303
304     StringExp(Loc loc, char *s);
305     StringExp(Loc loc, void *s, size_t len);
306     StringExp(Loc loc, void *s, size_t len, unsigned char postfix);
307     //Expression *syntaxCopy();
308     int equals(Object *o);
309     char *toChars();
310     Expression *semantic(Scope *sc);
311     Expression *interpret(InterState *istate);
312     StringExp *toUTF8(Scope *sc);
313     MATCH implicitConvTo(Type *t);
314     Expression *castTo(Scope *sc, Type *t);
315     int compare(Object *obj);
316     int isBool(int result);
317     unsigned charAt(size_t i);
318     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
319     void toMangleBuffer(OutBuffer *buf);
320 };
321
322 // Tuple
323
324 struct TupleExp : Expression
325 {
326     Expressions *exps;
327
328     TupleExp(Loc loc, Expressions *exps);
329     TupleExp(Loc loc, TupleDeclaration *tup);
330     Expression *syntaxCopy();
331     int equals(Object *o);
332     Expression *semantic(Scope *sc);
333     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
334     void scanForNestedRef(Scope *sc);
335     void checkEscape();
336     int checkSideEffect(int flag);
337     Expression *optimize(int result);
338     Expression *interpret(InterState *istate);
339     Expression *castTo(Scope *sc, Type *t);
340     int canThrow();
341
342     int inlineCost(InlineCostState *ics);
343     Expression *doInline(InlineDoState *ids);
344     Expression *inlineScan(InlineScanState *iss);
345 };
346
347 struct ArrayLiteralExp : Expression
348 {
349     Expressions *elements;
350
351     ArrayLiteralExp(Loc loc, Expressions *elements);
352     ArrayLiteralExp(Loc loc, Expression *e);
353
354     Expression *syntaxCopy();
355     Expression *semantic(Scope *sc);
356     int isBool(int result);
357     int checkSideEffect(int flag);
358     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
359     void toMangleBuffer(OutBuffer *buf);
360     void scanForNestedRef(Scope *sc);
361     Expression *optimize(int result);
362     Expression *interpret(InterState *istate);
363     MATCH implicitConvTo(Type *t);
364     Expression *castTo(Scope *sc, Type *t);
365     int canThrow();
366
367     int inlineCost(InlineCostState *ics);
368     Expression *doInline(InlineDoState *ids);
369     Expression *inlineScan(InlineScanState *iss);
370 };
371
372 struct AssocArrayLiteralExp : Expression
373 {
374     Expressions *keys;
375     Expressions *values;
376
377     AssocArrayLiteralExp(Loc loc, Expressions *keys, Expressions *values);
378
379     Expression *syntaxCopy();
380     Expression *semantic(Scope *sc);
381     int isBool(int result);
382     int checkSideEffect(int flag);
383     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
384     void toMangleBuffer(OutBuffer *buf);
385     void scanForNestedRef(Scope *sc);
386     Expression *optimize(int result);
387     Expression *interpret(InterState *istate);
388     MATCH implicitConvTo(Type *t);
389     Expression *castTo(Scope *sc, Type *t);
390     int canThrow();
391
392     int inlineCost(InlineCostState *ics);
393     Expression *doInline(InlineDoState *ids);
394     Expression *inlineScan(InlineScanState *iss);
395 };
396
397 struct StructLiteralExp : Expression
398 {
399     StructDeclaration *sd;      // which aggregate this is for
400     Expressions *elements;  // parallels sd->fields[] with
401                 // NULL entries for fields to skip
402
403     Symbol *sym;        // back end symbol to initialize with literal
404     size_t soffset;     // offset from start of s
405     int fillHoles;      // fill alignment 'holes' with zero
406
407     StructLiteralExp(Loc loc, StructDeclaration *sd, Expressions *elements);
408
409     Expression *syntaxCopy();
410     Expression *semantic(Scope *sc);
411     Expression *getField(Type *type, unsigned offset);
412     int getFieldIndex(Type *type, unsigned offset);
413     int checkSideEffect(int flag);
414     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
415     void toMangleBuffer(OutBuffer *buf);
416     void scanForNestedRef(Scope *sc);
417     Expression *optimize(int result);
418     Expression *interpret(InterState *istate);
419     int isLvalue();
420     Expression *toLvalue(Scope *sc, Expression *e);
421     int canThrow();
422     MATCH implicitConvTo(Type *t);
423
424     int inlineCost(InlineCostState *ics);
425     Expression *doInline(InlineDoState *ids);
426     Expression *inlineScan(InlineScanState *iss);
427 };
428
429 struct TypeDotIdExp : Expression
430 {
431     Identifier *ident;
432
433     TypeDotIdExp(Loc loc, Type *type, Identifier *ident);
434     Expression *syntaxCopy();
435     Expression *semantic(Scope *sc);
436     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
437 };
438
439 struct TypeExp : Expression
440 {
441     TypeExp(Loc loc, Type *type);
442     Expression *semantic(Scope *sc);
443     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
444     Expression *optimize(int result);
445 };
446
447 struct ScopeExp : Expression
448 {
449     ScopeDsymbol *sds;
450
451     ScopeExp(Loc loc, ScopeDsymbol *sds);
452     Expression *syntaxCopy();
453     Expression *semantic(Scope *sc);
454     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
455 };
456
457 struct TemplateExp : Expression
458 {
459     TemplateDeclaration *td;
460
461     TemplateExp(Loc loc, TemplateDeclaration *td);
462     void rvalue();
463     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
464 };
465
466 struct NewExp : Expression
467 {
468     /* thisexp.new(newargs) newtype(arguments)
469      */
470     Expression *thisexp;    // if !NULL, 'this' for class being allocated
471     Expressions *newargs;   // Array of Expression's to call new operator
472     Type *newtype;
473     Expressions *arguments; // Array of Expression's
474
475     CtorDeclaration *member;    // constructor function
476     NewDeclaration *allocator;  // allocator function
477     int onstack;        // allocate on stack
478
479     NewExp(Loc loc, Expression *thisexp, Expressions *newargs,
480     Type *newtype, Expressions *arguments);
481     Expression *syntaxCopy();
482     Expression *semantic(Scope *sc);
483     int checkSideEffect(int flag);
484     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
485     void scanForNestedRef(Scope *sc);
486     int canThrow();
487
488     //int inlineCost(InlineCostState *ics);
489     Expression *doInline(InlineDoState *ids);
490     //Expression *inlineScan(InlineScanState *iss);
491 };
492
493 struct NewAnonClassExp : Expression
494 {
495     /* thisexp.new(newargs) class baseclasses { } (arguments)
496      */
497     Expression *thisexp;    // if !NULL, 'this' for class being allocated
498     Expressions *newargs;   // Array of Expression's to call new operator
499     ClassDeclaration *cd;   // class being instantiated
500     Expressions *arguments; // Array of Expression's to call class constructor
501
502     NewAnonClassExp(Loc loc, Expression *thisexp, Expressions *newargs,
503     ClassDeclaration *cd, Expressions *arguments);
504     Expression *syntaxCopy();
505     Expression *semantic(Scope *sc);
506     int checkSideEffect(int flag);
507     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
508     int canThrow();
509 };
510
511 struct SymbolExp : Expression
512 {
513     Declaration *var;
514     int hasOverloads;
515
516     SymbolExp(Loc loc, enum TOK op, int size, Declaration *var, int hasOverloads);
517 };
518
519 // Offset from symbol
520
521 struct SymOffExp : SymbolExp
522 {
523     unsigned offset;
524
525     SymOffExp(Loc loc, Declaration *var, unsigned offset, int hasOverloads = 0);
526     Expression *semantic(Scope *sc);
527     void checkEscape();
528     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
529     int isConst();
530     int isBool(int result);
531     Expression *doInline(InlineDoState *ids);
532     MATCH implicitConvTo(Type *t);
533     Expression *castTo(Scope *sc, Type *t);
534     void scanForNestedRef(Scope *sc);
535 };
536
537 // Variable
538
539 struct VarExp : SymbolExp
540 {
541     VarExp(Loc loc, Declaration *var, int hasOverloads = 0);
542     int equals(Object *o);
543     Expression *semantic(Scope *sc);
544     Expression *optimize(int result);
545     Expression *interpret(InterState *istate);
546     void dump(int indent);
547     char *toChars();
548     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
549     void checkEscape();
550     int isLvalue();
551     Expression *toLvalue(Scope *sc, Expression *e);
552     Expression *modifiableLvalue(Scope *sc, Expression *e);
553     void scanForNestedRef(Scope *sc);
554
555     int inlineCost(InlineCostState *ics);
556     Expression *doInline(InlineDoState *ids);
557     //Expression *inlineScan(InlineScanState *iss);
558 };
559
560 #if V2
561 // Overload Set
562
563 struct OverExp : Expression
564 {
565     OverloadSet *vars;
566
567     OverExp(OverloadSet *s);
568     int isLvalue();
569     Expression *toLvalue(Scope *sc, Expression *e);
570 };
571 #endif
572
573 // Function/Delegate literal
574
575 struct FuncExp : Expression
576 {
577     FuncLiteralDeclaration *fd;
578
579     FuncExp(Loc loc, FuncLiteralDeclaration *fd);
580     Expression *syntaxCopy();
581     Expression *semantic(Scope *sc);
582     void scanForNestedRef(Scope *sc);
583     char *toChars();
584     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
585
586     int inlineCost(InlineCostState *ics);
587     //Expression *doInline(InlineDoState *ids);
588     //Expression *inlineScan(InlineScanState *iss);
589 };
590
591 // Declaration of a symbol
592
593 struct DeclarationExp : Expression
594 {
595     Dsymbol *declaration;
596
597     DeclarationExp(Loc loc, Dsymbol *declaration);
598     Expression *syntaxCopy();
599     Expression *semantic(Scope *sc);
600     Expression *interpret(InterState *istate);
601     int checkSideEffect(int flag);
602     void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
603     void scanForNestedRef(Scope *sc);
604     int canThrow();
605
606     int inlineCost(InlineCostState *ics);
607     Expression *doInline(InlineDoState *ids);
608     Expression