source: translator/SynTree/Expression.h @ a32b204

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since a32b204 was 51b7345, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

initial commit

  • Property mode set to 100644
File size: 17.8 KB
Line 
1/*
2 * This file is part of the Cforall project
3 *
4 * $Id: Expression.h,v 1.31 2005/08/29 20:59:25 rcbilson Exp $
5 *
6 */
7
8#ifndef EXPRESSION_H
9#define EXPRESSION_H
10
11#include <map>
12#include "SynTree.h"
13#include "Visitor.h"
14#include "Mutator.h"
15#include "Constant.h"
16
17
18class Expression
19{
20public:
21    Expression(Expression *_aname = 0 );
22    Expression( const Expression &other );
23    virtual ~Expression();
24
25    std::list<Type *>& get_results() { return results; }
26    void add_result(Type *t);
27
28    TypeSubstitution *get_env() const { return env; }
29    void set_env( TypeSubstitution *newValue ) { env = newValue; }
30    Expression *get_argName() const { return argName; }
31    void set_argName( Expression *name ) { argName = name; }
32
33    virtual Expression *clone() const = 0;
34    virtual void accept( Visitor &v ) = 0;
35    virtual Expression *acceptMutator( Mutator &m ) = 0;
36    virtual void print( std::ostream &os, int indent = 0 ) const;
37   
38protected:
39    std::list<Type *> results;
40    TypeSubstitution *env;
41    Expression* argName; // if expression is used as an argument, it can be "designated" by this name
42};
43
44// ParamEntry contains the i.d. of a declaration and a type that is derived from that declaration,
45// but subject to decay-to-pointer and type parameter renaming
46struct ParamEntry
47{
48    ParamEntry(): decl( 0 ), actualType( 0 ), formalType( 0 ), expr( 0 ) {}
49    ParamEntry( UniqueId decl, Type *actualType, Type *formalType, Expression* expr ): decl( decl ), actualType( actualType ), formalType( formalType ), expr( expr ) {}
50    ParamEntry( const ParamEntry &other );
51    ~ParamEntry();
52    ParamEntry &operator=( const ParamEntry &other );
53
54    UniqueId decl;
55    Type *actualType;
56    Type *formalType;
57    Expression* expr;
58};
59
60typedef std::map< UniqueId, ParamEntry > InferredParams;
61
62// ApplicationExpr represents the application of a function to a set of parameters.  This is the
63// result of running an UntypedExpr through the expression analyzer.
64class ApplicationExpr : public Expression
65{
66public:
67    ApplicationExpr( Expression *function );
68    ApplicationExpr( const ApplicationExpr &other );
69    virtual ~ApplicationExpr();
70
71    Expression *get_function() const { return function; }
72    void set_function( Expression *newValue ) { function = newValue; }
73    std::list<Expression *>& get_args() { return args; }
74    InferredParams &get_inferParams() { return inferParams; }
75
76    virtual ApplicationExpr *clone() const { return new ApplicationExpr( *this ); }
77    virtual void accept( Visitor &v ) { v.visit( this ); }
78    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
79    virtual void print( std::ostream &os, int indent = 0 ) const;
80   
81private:
82    Expression *function;
83    std::list<Expression *> args;
84    InferredParams inferParams;
85};
86
87// UntypedExpr represents the application of a function to a set of parameters, but where the
88// particular overload for the function name has not yet been determined.  Most operators are
89// converted into functional form automatically, to permit operator overloading.
90class UntypedExpr : public Expression
91{
92public:
93    UntypedExpr( Expression *function, Expression *_aname = 0 );
94    UntypedExpr( const UntypedExpr &other );
95    UntypedExpr( Expression *function, std::list<Expression *> &args, Expression *_aname = 0 );
96    virtual ~UntypedExpr();
97
98    Expression *get_function() const { return function; }
99    void set_function( Expression *newValue ) { function = newValue; }
100
101    void set_args( std::list<Expression *> &listArgs ) { args = listArgs; }
102    std::list<Expression*>::iterator begin_args() { return args.begin(); }
103    std::list<Expression*>::iterator end_args() { return args.end(); }
104    std::list<Expression*>& get_args() { return args; }
105
106    virtual UntypedExpr *clone() const { return new UntypedExpr( *this ); }
107    virtual void accept( Visitor &v ) { v.visit( this ); }
108    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
109    virtual void print( std::ostream &os, int indent = 0 ) const;
110    virtual void printArgs(std::ostream &os, int indent = 0) const;
111   
112private:
113   
114    Expression *function;
115    std::list<Expression*> args;
116};
117
118// this class contains a name whose meaning is still not determined
119class NameExpr : public Expression
120{
121public:
122    NameExpr( std::string name, Expression *_aname = 0 );
123    NameExpr( const NameExpr &other );
124    virtual ~NameExpr();
125
126    std::string get_name() const { return name; }
127    void set_name( std::string newValue ) { name = newValue; }
128
129    virtual NameExpr *clone() const { return new NameExpr( *this ); }
130    virtual void accept( Visitor &v ) { v.visit( this ); }
131    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
132    virtual void print( std::ostream &os, int indent = 0 ) const;
133   
134private:
135   
136    std::string name;
137};
138
139// The following classes are used to represent expression types that cannot be converted into
140// function-call format.
141
142// AddressExpr represents a address-of expression, e.g. &e
143class AddressExpr : public Expression
144{
145public:
146    AddressExpr( Expression *arg, Expression *_aname = 0 );
147    AddressExpr( const AddressExpr &other );
148    virtual ~AddressExpr();
149
150    Expression *get_arg() const { return arg; }
151    void set_arg(Expression *newValue ) { arg = newValue; }
152
153    virtual AddressExpr *clone() const { return new AddressExpr( *this ); }
154    virtual void accept( Visitor &v ) { v.visit( this ); }
155    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
156    virtual void print( std::ostream &os, int indent = 0 ) const;
157   
158private:
159    Expression *arg;
160};
161
162class LabelAddressExpr : public Expression
163{
164public:
165    LabelAddressExpr( Expression *arg );
166    LabelAddressExpr( const AddressExpr &other );
167    virtual ~LabelAddressExpr();
168
169    Expression *get_arg() const { return arg; }
170    void set_arg(Expression *newValue ) { arg = newValue; }
171
172    virtual LabelAddressExpr *clone() const { return new LabelAddressExpr( *this ); }
173    virtual void accept( Visitor &v ) { v.visit( this ); }
174    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
175    virtual void print( std::ostream &os, int indent = 0 ) const;
176   
177private:
178    Expression *arg;
179};
180
181// CastExpr represents a type cast expression, e.g. (int)e
182class CastExpr : public Expression
183{
184public:
185    CastExpr( Expression *arg, Expression *_aname = 0 );
186    CastExpr( Expression *arg, Type *toType, Expression *_aname = 0 );
187    CastExpr( const CastExpr &other );
188    virtual ~CastExpr();
189
190    Expression *get_arg() const { return arg; }
191    void set_arg(Expression *newValue ) { arg = newValue; }
192
193    virtual CastExpr *clone() const { return new CastExpr( *this ); }
194    virtual void accept( Visitor &v ) { v.visit( this ); }
195    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
196    virtual void print( std::ostream &os, int indent = 0 ) const;
197   
198private:
199    Expression *arg;
200};
201
202// UntypedMemberExpr represents a member selection operation, e.g. q.p
203// before processing by the expression analyzer
204class UntypedMemberExpr : public Expression
205{
206public:
207    UntypedMemberExpr( std::string member, Expression *aggregate, Expression *_aname = 0 );
208    UntypedMemberExpr( const UntypedMemberExpr &other );
209    virtual ~UntypedMemberExpr();
210
211    std::string get_member() const { return member; }
212    void set_member( const std::string &newValue ) { member = newValue; }
213    Expression *get_aggregate() const { return aggregate; }
214    void set_aggregate( Expression *newValue ) { aggregate = newValue; }
215
216    virtual UntypedMemberExpr *clone() const { return new UntypedMemberExpr( *this ); }
217    virtual void accept( Visitor &v ) { v.visit( this ); }
218    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
219    virtual void print( std::ostream &os, int indent = 0 ) const;
220   
221private:
222    std::string member;
223    Expression *aggregate;
224};
225
226// MemberExpr represents a member selection operation, e.g. q.p
227// after processing by the expression analyzer
228class MemberExpr : public Expression
229{
230public:
231    MemberExpr( DeclarationWithType *member, Expression *aggregate, Expression *_aname = 0 );
232    MemberExpr( const MemberExpr &other );
233    virtual ~MemberExpr();
234
235    DeclarationWithType *get_member() const { return member; }
236    void set_member( DeclarationWithType *newValue ) { member = newValue; }
237    Expression *get_aggregate() const { return aggregate; }
238    void set_aggregate( Expression *newValue ) { aggregate = newValue; }
239
240    virtual MemberExpr *clone() const { return new MemberExpr( *this ); }
241    virtual void accept( Visitor &v ) { v.visit( this ); }
242    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
243    virtual void print( std::ostream &os, int indent = 0 ) const;
244   
245private:
246    DeclarationWithType *member;
247    Expression *aggregate;
248};
249
250// VariableExpr represents an expression that simply refers to the value of a named variable
251class VariableExpr : public Expression
252{
253public:
254    VariableExpr( DeclarationWithType *var, Expression *_aname = 0 );
255    VariableExpr( const VariableExpr &other );
256    virtual ~VariableExpr();
257
258    DeclarationWithType *get_var() const { return var; }
259    void set_var( DeclarationWithType *newValue ) { var = newValue; }
260
261    virtual VariableExpr *clone() const { return new VariableExpr( *this ); }
262    virtual void accept( Visitor &v ) { v.visit( this ); }
263    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
264    virtual void print( std::ostream &os, int indent = 0 ) const;
265   
266private:
267    DeclarationWithType *var;
268};
269
270// ConstantExpr represents an expression that simply refers to the value of a constant
271class ConstantExpr : public Expression
272{
273public:
274    ConstantExpr( Constant constant, Expression *_aname = 0 );
275    ConstantExpr( const ConstantExpr &other );
276    virtual ~ConstantExpr();
277
278    Constant *get_constant() { return &constant; }
279    void set_constant( const Constant &newValue ) { constant = newValue; }
280
281    virtual ConstantExpr *clone() const { return new ConstantExpr( *this ); }
282    virtual void accept( Visitor &v ) { v.visit( this ); }
283    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
284    virtual void print( std::ostream &os, int indent = 0 ) const;
285   
286private:
287    Constant constant;
288};
289
290// SizeofExpr represents a sizeof expression (could be sizeof(int) or sizeof 3+4)
291class SizeofExpr : public Expression
292{
293public:
294    SizeofExpr( Expression *expr, Expression *_aname = 0 );
295    SizeofExpr( const SizeofExpr &other );
296    SizeofExpr( Type *type, Expression *_aname = 0 );
297    virtual ~SizeofExpr();
298
299    Expression *get_expr() const { return expr; }
300    void set_expr( Expression *newValue ) { expr = newValue; }
301    Type *get_type() const { return type; }
302    void set_type( Type *newValue ) { type = newValue; }
303    bool get_isType() const { return isType; }
304    void set_isType( bool newValue ) { isType = newValue; }
305
306    virtual SizeofExpr *clone() const { return new SizeofExpr( *this ); }
307    virtual void accept( Visitor &v ) { v.visit( this ); }
308    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
309    virtual void print( std::ostream &os, int indent = 0 ) const;
310   
311private:
312    Expression *expr;
313    Type *type;
314    bool isType;
315};
316
317// AttrExpr represents an @attribute expression (like sizeof, but user-defined)
318class AttrExpr : public Expression
319{
320public:
321    AttrExpr(Expression *attr, Expression *expr, Expression *_aname = 0 );
322    AttrExpr( const AttrExpr &other );
323    AttrExpr( Expression *attr, Type *type, Expression *_aname = 0 );
324    virtual ~AttrExpr();
325
326    Expression *get_attr() const { return attr; }
327    void set_attr( Expression *newValue ) { attr = newValue; }
328    Expression *get_expr() const { return expr; }
329    void set_expr( Expression *newValue ) { expr = newValue; }
330    Type *get_type() const { return type; }
331    void set_type( Type *newValue ) { type = newValue; }
332    bool get_isType() const { return isType; }
333    void set_isType( bool newValue ) { isType = newValue; }
334
335    virtual AttrExpr *clone() const { return new AttrExpr( *this ); }
336    virtual void accept( Visitor &v ) { v.visit( this ); }
337    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
338    virtual void print( std::ostream &os, int indent = 0 ) const;
339   
340private:
341    Expression *attr;
342    Expression *expr;
343    Type *type;
344    bool isType;
345};
346
347// LogicalExpr represents a short-circuit boolean expression (&& or ||)
348class LogicalExpr : public Expression
349{
350public:
351    LogicalExpr( Expression *arg1, Expression *arg2, bool andp = true, Expression *_aname = 0 );
352    LogicalExpr( const LogicalExpr &other );
353    virtual ~LogicalExpr();
354
355    bool get_isAnd() const { return isAnd; }
356    Expression *get_arg1() { return arg1; }
357    void set_arg1( Expression *newValue ) { arg1 = newValue; }
358    Expression *get_arg2() const { return arg2; }
359    void set_arg2( Expression *newValue ) { arg2 = newValue; }
360
361    virtual LogicalExpr *clone() const { return new LogicalExpr( *this ); }
362    virtual void accept( Visitor &v ) { v.visit( this ); }
363    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
364    virtual void print( std::ostream &os, int indent = 0 ) const;
365   
366private:
367    Expression *arg1;
368    Expression *arg2;
369    bool isAnd;
370};
371
372// ConditionalExpr represents the three-argument conditional ( p ? a : b )
373class ConditionalExpr : public Expression
374{
375public:
376    ConditionalExpr( Expression *arg1, Expression *arg2, Expression *arg3, Expression *_aname = 0 );
377    ConditionalExpr( const ConditionalExpr &other );
378    virtual ~ConditionalExpr();
379
380    Expression *get_arg1() const { return arg1; }
381    void set_arg1( Expression *newValue ) { arg1 = newValue; }
382    Expression *get_arg2() const { return arg2; }
383    void set_arg2( Expression *newValue ) { arg2 = newValue; }
384    Expression *get_arg3() const { return arg3; }
385    void set_arg3( Expression *newValue ) { arg3 = newValue; }
386
387    virtual ConditionalExpr *clone() const { return new ConditionalExpr( *this ); }
388    virtual void accept( Visitor &v ) { v.visit( this ); }
389    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
390    virtual void print( std::ostream &os, int indent = 0 ) const;
391   
392private:
393    Expression *arg1;
394    Expression *arg2;
395    Expression *arg3;
396};
397
398// CommaExpr represents the sequence operator ( a, b )
399class CommaExpr : public Expression
400{
401public:
402    CommaExpr( Expression *arg1, Expression *arg2, Expression *_aname = 0 );
403    CommaExpr( const CommaExpr &other );
404    virtual ~CommaExpr();
405
406    Expression *get_arg1() const { return arg1; }
407    void set_arg1( Expression *newValue ) { arg1 = newValue; }
408    Expression *get_arg2() const { return arg2; }
409    void set_arg2( Expression *newValue ) { arg2 = newValue; }
410
411    virtual CommaExpr *clone() const { return new CommaExpr( *this ); }
412    virtual void accept( Visitor &v ) { v.visit( this ); }
413    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
414    virtual void print( std::ostream &os, int indent = 0 ) const;
415   
416private:
417    Expression *arg1;
418    Expression *arg2;
419};
420
421// TupleExpr represents a tuple expression ( [a, b, c] )
422class TupleExpr : public Expression
423{
424public:
425    TupleExpr( Expression *_aname = 0 );
426    TupleExpr( const TupleExpr &other );
427    virtual ~TupleExpr();
428
429    void set_exprs( std::list<Expression*> newValue ) { exprs = newValue; }
430    std::list<Expression*>& get_exprs() { return exprs; }
431
432    virtual TupleExpr *clone() const { return new TupleExpr( *this ); }
433    virtual void accept( Visitor &v ) { v.visit( this ); }
434    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
435    virtual void print( std::ostream &os, int indent = 0 ) const;
436   
437private:
438    std::list<Expression*> exprs;
439};
440
441// SolvedTupleExpr represents a TupleExpr whose components have been type-resolved. It is effectively a shell for the code generator to work on
442class SolvedTupleExpr : public Expression
443{
444public:
445
446    SolvedTupleExpr( Expression *_aname = 0 ) : Expression( _aname ) {}
447    SolvedTupleExpr( std::list<Expression *> &, Expression *_aname = 0 );
448    SolvedTupleExpr( const SolvedTupleExpr &other );
449    virtual ~SolvedTupleExpr() {}
450
451    std::list<Expression*> &get_exprs() { return exprs; }
452
453    virtual SolvedTupleExpr *clone() const { return new SolvedTupleExpr( *this ); }
454    virtual void accept( Visitor &v ) { v.visit( this ); }
455    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
456    virtual void print( std::ostream &os, int indent = 0 ) const;
457private:
458    std::list<Expression*> exprs;
459};
460
461// TypeExpr represents a type used in an expression (e.g. as a type generator parameter)
462class TypeExpr : public Expression
463{
464public:
465    TypeExpr( Type *type );
466    TypeExpr( const TypeExpr &other );
467    virtual ~TypeExpr();
468
469    Type *get_type() const { return type; }
470    void set_type( Type *newValue ) { type = newValue; }
471
472    virtual TypeExpr *clone() const { return new TypeExpr( *this ); }
473    virtual void accept( Visitor &v ) { v.visit( this ); }
474    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
475    virtual void print( std::ostream &os, int indent = 0 ) const;
476   
477private:
478    Type *type;
479};
480
481// ValofExpr represents a GCC 'lambda expression'
482class UntypedValofExpr : public Expression
483{
484public:
485    UntypedValofExpr( Statement *_body, Expression *_aname = 0 ) : Expression( _aname ), body ( _body ) {}
486    virtual ~UntypedValofExpr() {}
487
488    Expression *get_value();
489    Statement *get_body() const { return body; }
490
491    virtual UntypedValofExpr *clone() const { return new UntypedValofExpr( *this ); }
492    virtual void accept( Visitor &v ) { v.visit( this ); }
493    virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
494    virtual void print( std::ostream &os, int indent = 0 ) const;
495   
496private:
497    Statement *body;
498};
499
500
501#endif /* #ifndef EXPRESSION_H */
502
503/*
504    Local Variables:
505    mode: c++
506    End:
507*/
Note: See TracBrowser for help on using the repository browser.