source: src/SynTree/Expression.h @ 421edab

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 421edab was 25a054f, checked in by Aaron Moss <a3moss@…>, 8 years ago

Add OffsetofExpr? for field offsets

  • Property mode set to 100644
File size: 20.3 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/// OffsetofExpr represents an offsetof expression
322class OffsetofExpr : public Expression {
323  public:
324        OffsetofExpr( Type *type, DeclarationWithType *member, Expression *_aname = 0 );
325        OffsetofExpr( const OffsetofExpr &other );
326        virtual ~OffsetofExpr();
327
328        Type *get_type() const { return type; }
329        void set_type( Type *newValue ) { type = newValue; }
330        DeclarationWithType *get_member() const { return member; }
331        void set_member( DeclarationWithType *newValue ) { member = newValue; }
332
333        virtual OffsetofExpr *clone() const { return new OffsetofExpr( *this ); }
334        virtual void accept( Visitor &v ) { v.visit( this ); }
335        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
336        virtual void print( std::ostream &os, int indent = 0 ) const;
337  private:
338        Type *type;
339        DeclarationWithType *member;
340};
341
342/// AttrExpr represents an @attribute expression (like sizeof, but user-defined)
343class AttrExpr : public Expression {
344  public:
345        AttrExpr(Expression *attr, Expression *expr, Expression *_aname = 0 );
346        AttrExpr( const AttrExpr &other );
347        AttrExpr( Expression *attr, Type *type, Expression *_aname = 0 );
348        virtual ~AttrExpr();
349
350        Expression *get_attr() const { return attr; }
351        void set_attr( Expression *newValue ) { attr = newValue; }
352        Expression *get_expr() const { return expr; }
353        void set_expr( Expression *newValue ) { expr = newValue; }
354        Type *get_type() const { return type; }
355        void set_type( Type *newValue ) { type = newValue; }
356        bool get_isType() const { return isType; }
357        void set_isType( bool newValue ) { isType = newValue; }
358
359        virtual AttrExpr *clone() const { return new AttrExpr( *this ); }
360        virtual void accept( Visitor &v ) { v.visit( this ); }
361        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
362        virtual void print( std::ostream &os, int indent = 0 ) const;
363  private:
364        Expression *attr;
365        Expression *expr;
366        Type *type;
367        bool isType;
368};
369
370/// LogicalExpr represents a short-circuit boolean expression (&& or ||)
371class LogicalExpr : public Expression {
372  public:
373        LogicalExpr( Expression *arg1, Expression *arg2, bool andp = true, Expression *_aname = 0 );
374        LogicalExpr( const LogicalExpr &other );
375        virtual ~LogicalExpr();
376
377        bool get_isAnd() const { return isAnd; }
378        Expression *get_arg1() { return arg1; }
379        void set_arg1( Expression *newValue ) { arg1 = newValue; }
380        Expression *get_arg2() const { return arg2; }
381        void set_arg2( Expression *newValue ) { arg2 = newValue; }
382
383        virtual LogicalExpr *clone() const { return new LogicalExpr( *this ); }
384        virtual void accept( Visitor &v ) { v.visit( this ); }
385        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
386        virtual void print( std::ostream &os, int indent = 0 ) const;
387  private:
388        Expression *arg1;
389        Expression *arg2;
390        bool isAnd;
391};
392
393/// ConditionalExpr represents the three-argument conditional ( p ? a : b )
394class ConditionalExpr : public Expression {
395  public:
396        ConditionalExpr( Expression *arg1, Expression *arg2, Expression *arg3, Expression *_aname = 0 );
397        ConditionalExpr( const ConditionalExpr &other );
398        virtual ~ConditionalExpr();
399
400        Expression *get_arg1() const { return arg1; }
401        void set_arg1( Expression *newValue ) { arg1 = newValue; }
402        Expression *get_arg2() const { return arg2; }
403        void set_arg2( Expression *newValue ) { arg2 = newValue; }
404        Expression *get_arg3() const { return arg3; }
405        void set_arg3( Expression *newValue ) { arg3 = newValue; }
406
407        virtual ConditionalExpr *clone() const { return new ConditionalExpr( *this ); }
408        virtual void accept( Visitor &v ) { v.visit( this ); }
409        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
410        virtual void print( std::ostream &os, int indent = 0 ) const;
411  private:
412        Expression *arg1;
413        Expression *arg2;
414        Expression *arg3;
415};
416
417/// CommaExpr represents the sequence operator ( a, b )
418class CommaExpr : public Expression {
419  public:
420        CommaExpr( Expression *arg1, Expression *arg2, Expression *_aname = 0 );
421        CommaExpr( const CommaExpr &other );
422        virtual ~CommaExpr();
423
424        Expression *get_arg1() const { return arg1; }
425        void set_arg1( Expression *newValue ) { arg1 = newValue; }
426        Expression *get_arg2() const { return arg2; }
427        void set_arg2( Expression *newValue ) { arg2 = newValue; }
428
429        virtual CommaExpr *clone() const { return new CommaExpr( *this ); }
430        virtual void accept( Visitor &v ) { v.visit( this ); }
431        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
432        virtual void print( std::ostream &os, int indent = 0 ) const;
433  private:
434        Expression *arg1;
435        Expression *arg2;
436};
437
438/// TupleExpr represents a tuple expression ( [a, b, c] )
439class TupleExpr : public Expression {
440  public:
441        TupleExpr( Expression *_aname = 0 );
442        TupleExpr( const TupleExpr &other );
443        virtual ~TupleExpr();
444
445        void set_exprs( std::list<Expression*> newValue ) { exprs = newValue; }
446        std::list<Expression*>& get_exprs() { return exprs; }
447
448        virtual TupleExpr *clone() const { return new TupleExpr( *this ); }
449        virtual void accept( Visitor &v ) { v.visit( this ); }
450        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
451        virtual void print( std::ostream &os, int indent = 0 ) const;
452  private:
453        std::list<Expression*> exprs;
454};
455
456/// SolvedTupleExpr represents a TupleExpr whose components have been type-resolved. It is effectively a shell for the code generator to work on
457class SolvedTupleExpr : public Expression {
458  public:
459        SolvedTupleExpr( Expression *_aname = 0 ) : Expression( _aname ) {}
460        SolvedTupleExpr( std::list<Expression *> &, Expression *_aname = 0 );
461        SolvedTupleExpr( const SolvedTupleExpr &other );
462        virtual ~SolvedTupleExpr() {}
463
464        std::list<Expression*> &get_exprs() { return exprs; }
465
466        virtual SolvedTupleExpr *clone() const { return new SolvedTupleExpr( *this ); }
467        virtual void accept( Visitor &v ) { v.visit( this ); }
468        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
469        virtual void print( std::ostream &os, int indent = 0 ) const;
470  private:
471        std::list<Expression*> exprs;
472};
473
474/// TypeExpr represents a type used in an expression (e.g. as a type generator parameter)
475class TypeExpr : public Expression {
476  public:
477        TypeExpr( Type *type );
478        TypeExpr( const TypeExpr &other );
479        virtual ~TypeExpr();
480
481        Type *get_type() const { return type; }
482        void set_type( Type *newValue ) { type = newValue; }
483
484        virtual TypeExpr *clone() const { return new TypeExpr( *this ); }
485        virtual void accept( Visitor &v ) { v.visit( this ); }
486        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
487        virtual void print( std::ostream &os, int indent = 0 ) const;
488  private:
489        Type *type;
490};
491
492/// AsmExpr represents a GCC 'asm constraint operand' used in an asm statement: [output] "=f" (result)
493class AsmExpr : public Expression {
494  public:
495        AsmExpr( Expression *inout, ConstantExpr *constraint, Expression *operand ) : inout( inout ), constraint( constraint ), operand( operand ) {}
496        virtual ~AsmExpr() { delete inout; delete constraint; delete operand; };
497
498        Expression *get_inout() const { return inout; }
499        void set_inout( Expression *newValue ) { inout = newValue; }
500
501        ConstantExpr *get_constraint() const { return constraint; }
502        void set_constraint( ConstantExpr *newValue ) { constraint = newValue; }
503
504        Expression *get_operand() const { return operand; }
505        void set_operand( Expression *newValue ) { operand = newValue; }
506
507        virtual AsmExpr *clone() const { return new AsmExpr( *this ); }
508        virtual void accept( Visitor &v ) { v.visit( this ); }
509        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
510        virtual void print( std::ostream &os, int indent = 0 ) const;
511  private:
512        // https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
513        Expression *inout;
514        ConstantExpr *constraint;
515        Expression *operand;
516};
517
518/// ValofExpr represents a GCC 'lambda expression'
519class UntypedValofExpr : public Expression {
520  public:
521        UntypedValofExpr( Statement *_body, Expression *_aname = 0 ) : Expression( _aname ), body ( _body ) {}
522        virtual ~UntypedValofExpr() {}
523
524        Expression *get_value();
525        Statement *get_body() const { return body; }
526
527        virtual UntypedValofExpr *clone() const { return new UntypedValofExpr( *this ); }
528        virtual void accept( Visitor &v ) { v.visit( this ); }
529        virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); }
530        virtual void print( std::ostream &os, int indent = 0 ) const;
531  private:
532        Statement *body;
533};
534
535std::ostream & operator<<( std::ostream & out, Expression * expr );
536
537#endif // EXPRESSION_H
538
539// Local Variables: //
540// tab-width: 4 //
541// mode: c++ //
542// compile-command: "make install" //
543// End: //
Note: See TracBrowser for help on using the repository browser.