source: src/SynTree/Expression.h @ b18b0b5

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 b18b0b5 was baf7fee, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

added output operators for AST types, made makeTyVars public in PolyMutator?

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