root/branches/dmdfe/expression.h

Revision 740, 36.3 kB (checked in by Gregor, 1 year ago)

MERGE: DMD 1.019

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