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 : Peter A. Buhr |
---|
12 | // Last Modified On : Sat Aug 6 08:52:53 2016 |
---|
13 | // Update Count : 35 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef EXPRESSION_H |
---|
17 | #define EXPRESSION_H |
---|
18 | |
---|
19 | #include <map> |
---|
20 | #include <memory> |
---|
21 | #include "SynTree.h" |
---|
22 | #include "Visitor.h" |
---|
23 | #include "Mutator.h" |
---|
24 | #include "Constant.h" |
---|
25 | #include "Common/UniqueName.h" |
---|
26 | |
---|
27 | /// Expression is the root type for all expressions |
---|
28 | class Expression { |
---|
29 | public: |
---|
30 | Expression( Expression *_aname = nullptr ); |
---|
31 | Expression( const Expression &other ); |
---|
32 | virtual ~Expression(); |
---|
33 | |
---|
34 | Type *& get_result() { return result; } |
---|
35 | void set_result( Type *newValue ) { result = newValue; } |
---|
36 | bool has_result() const { return result != nullptr; } |
---|
37 | |
---|
38 | TypeSubstitution *get_env() const { return env; } |
---|
39 | void set_env( TypeSubstitution *newValue ) { env = newValue; } |
---|
40 | Expression *get_argName() const { return argName; } |
---|
41 | void set_argName( Expression *name ) { argName = name; } |
---|
42 | bool get_extension() const { return extension; } |
---|
43 | Expression * set_extension( bool exten ) { extension = exten; return this; } |
---|
44 | |
---|
45 | virtual Expression *clone() const = 0; |
---|
46 | virtual void accept( Visitor &v ) = 0; |
---|
47 | virtual Expression *acceptMutator( Mutator &m ) = 0; |
---|
48 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
49 | protected: |
---|
50 | Type * result; |
---|
51 | TypeSubstitution *env; |
---|
52 | Expression* argName; // if expression is used as an argument, it can be "designated" by this name |
---|
53 | bool extension = false; |
---|
54 | }; |
---|
55 | |
---|
56 | /// ParamEntry contains the i.d. of a declaration and a type that is derived from that declaration, |
---|
57 | /// but subject to decay-to-pointer and type parameter renaming |
---|
58 | struct ParamEntry { |
---|
59 | ParamEntry(): decl( 0 ), actualType( 0 ), formalType( 0 ), expr( 0 ) {} |
---|
60 | ParamEntry( UniqueId decl, Type *actualType, Type *formalType, Expression* expr ): decl( decl ), actualType( actualType ), formalType( formalType ), expr( expr ) {} |
---|
61 | ParamEntry( const ParamEntry &other ); |
---|
62 | ~ParamEntry(); |
---|
63 | ParamEntry &operator=( const ParamEntry &other ); |
---|
64 | |
---|
65 | UniqueId decl; |
---|
66 | Type *actualType; |
---|
67 | Type *formalType; |
---|
68 | Expression* expr; |
---|
69 | }; |
---|
70 | |
---|
71 | typedef std::map< UniqueId, ParamEntry > InferredParams; |
---|
72 | |
---|
73 | /// ApplicationExpr represents the application of a function to a set of parameters. This is the result of running an |
---|
74 | /// UntypedExpr through the expression analyzer. |
---|
75 | class ApplicationExpr : public Expression { |
---|
76 | public: |
---|
77 | ApplicationExpr( Expression *function ); |
---|
78 | ApplicationExpr( const ApplicationExpr &other ); |
---|
79 | virtual ~ApplicationExpr(); |
---|
80 | |
---|
81 | Expression *get_function() const { return function; } |
---|
82 | void set_function( Expression *newValue ) { function = newValue; } |
---|
83 | std::list<Expression *>& get_args() { return args; } |
---|
84 | InferredParams &get_inferParams() { return inferParams; } |
---|
85 | |
---|
86 | virtual ApplicationExpr *clone() const { return new ApplicationExpr( *this ); } |
---|
87 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
88 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
89 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
90 | private: |
---|
91 | Expression *function; |
---|
92 | std::list<Expression *> args; |
---|
93 | InferredParams inferParams; |
---|
94 | }; |
---|
95 | |
---|
96 | /// UntypedExpr represents the application of a function to a set of parameters, but where the particular overload for |
---|
97 | /// the function name has not yet been determined. Most operators are converted into functional form automatically, to |
---|
98 | /// permit operator overloading. |
---|
99 | class UntypedExpr : public Expression { |
---|
100 | public: |
---|
101 | UntypedExpr( Expression *function, const std::list<Expression *> &args = std::list< Expression * >(), Expression *_aname = nullptr ); |
---|
102 | UntypedExpr( const UntypedExpr &other ); |
---|
103 | virtual ~UntypedExpr(); |
---|
104 | |
---|
105 | Expression *get_function() const { return function; } |
---|
106 | void set_function( Expression *newValue ) { function = newValue; } |
---|
107 | |
---|
108 | void set_args( std::list<Expression *> &listArgs ) { args = listArgs; } |
---|
109 | std::list<Expression*>::iterator begin_args() { return args.begin(); } |
---|
110 | std::list<Expression*>::iterator end_args() { return args.end(); } |
---|
111 | std::list<Expression*>& get_args() { return args; } |
---|
112 | |
---|
113 | static UntypedExpr * createDeref( Expression * arg ); |
---|
114 | static UntypedExpr * createAssign( Expression * arg1, Expression * arg2 ); |
---|
115 | |
---|
116 | virtual UntypedExpr *clone() const { return new UntypedExpr( *this ); } |
---|
117 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
118 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
119 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
120 | virtual void printArgs(std::ostream &os, int indent = 0) const; |
---|
121 | private: |
---|
122 | Expression *function; |
---|
123 | std::list<Expression*> args; |
---|
124 | }; |
---|
125 | |
---|
126 | /// NameExpr contains a name whose meaning is still not determined |
---|
127 | class NameExpr : public Expression { |
---|
128 | public: |
---|
129 | NameExpr( std::string name, Expression *_aname = nullptr ); |
---|
130 | NameExpr( const NameExpr &other ); |
---|
131 | virtual ~NameExpr(); |
---|
132 | |
---|
133 | const std::string &get_name() const { return name; } |
---|
134 | void set_name( std::string newValue ) { name = newValue; } |
---|
135 | |
---|
136 | virtual NameExpr *clone() const { return new NameExpr( *this ); } |
---|
137 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
138 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
139 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
140 | private: |
---|
141 | std::string name; |
---|
142 | }; |
---|
143 | |
---|
144 | // The following classes are used to represent expression types that cannot be converted into |
---|
145 | // function-call format. |
---|
146 | |
---|
147 | /// AddressExpr represents a address-of expression, e.g. &e |
---|
148 | class AddressExpr : public Expression { |
---|
149 | public: |
---|
150 | AddressExpr( Expression *arg, Expression *_aname = nullptr ); |
---|
151 | AddressExpr( const AddressExpr &other ); |
---|
152 | virtual ~AddressExpr(); |
---|
153 | |
---|
154 | Expression *get_arg() const { return arg; } |
---|
155 | void set_arg(Expression *newValue ) { arg = newValue; } |
---|
156 | |
---|
157 | virtual AddressExpr *clone() const { return new AddressExpr( *this ); } |
---|
158 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
159 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
160 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
161 | private: |
---|
162 | Expression *arg; |
---|
163 | }; |
---|
164 | |
---|
165 | // xxx - this doesn't appear to actually be hooked in anywhere. We should use this instead of the "&&"" UntypedExpr hack |
---|
166 | class LabelAddressExpr : public Expression { |
---|
167 | public: |
---|
168 | LabelAddressExpr( Expression *arg ); |
---|
169 | LabelAddressExpr( const LabelAddressExpr &other ); |
---|
170 | virtual ~LabelAddressExpr(); |
---|
171 | |
---|
172 | Expression *get_arg() const { return arg; } |
---|
173 | void set_arg(Expression *newValue ) { arg = newValue; } |
---|
174 | |
---|
175 | virtual LabelAddressExpr *clone() const { return new LabelAddressExpr( *this ); } |
---|
176 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
177 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
178 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
179 | private: |
---|
180 | Expression *arg; |
---|
181 | }; |
---|
182 | |
---|
183 | /// CastExpr represents a type cast expression, e.g. (int)e |
---|
184 | class CastExpr : public Expression { |
---|
185 | public: |
---|
186 | CastExpr( Expression *arg, Expression *_aname = nullptr ); |
---|
187 | CastExpr( Expression *arg, Type *toType, Expression *_aname = nullptr ); |
---|
188 | CastExpr( const CastExpr &other ); |
---|
189 | virtual ~CastExpr(); |
---|
190 | |
---|
191 | Expression *get_arg() const { return arg; } |
---|
192 | void set_arg(Expression *newValue ) { arg = newValue; } |
---|
193 | |
---|
194 | virtual CastExpr *clone() const { return new CastExpr( *this ); } |
---|
195 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
196 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
197 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
198 | private: |
---|
199 | Expression *arg; |
---|
200 | }; |
---|
201 | |
---|
202 | /// UntypedMemberExpr represents a member selection operation, e.g. q.p before processing by the expression analyzer |
---|
203 | class UntypedMemberExpr : public Expression { |
---|
204 | public: |
---|
205 | UntypedMemberExpr( Expression *member, Expression *aggregate, Expression *_aname = nullptr ); |
---|
206 | UntypedMemberExpr( const UntypedMemberExpr &other ); |
---|
207 | virtual ~UntypedMemberExpr(); |
---|
208 | |
---|
209 | Expression * get_member() const { return member; } |
---|
210 | void set_member( Expression * newValue ) { member = newValue; } |
---|
211 | Expression *get_aggregate() const { return aggregate; } |
---|
212 | void set_aggregate( Expression *newValue ) { aggregate = newValue; } |
---|
213 | |
---|
214 | virtual UntypedMemberExpr *clone() const { return new UntypedMemberExpr( *this ); } |
---|
215 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
216 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
217 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
218 | private: |
---|
219 | Expression *member; |
---|
220 | Expression *aggregate; |
---|
221 | }; |
---|
222 | |
---|
223 | /// MemberExpr represents a member selection operation, e.g. q.p after processing by the expression analyzer |
---|
224 | class MemberExpr : public Expression { |
---|
225 | public: |
---|
226 | MemberExpr( DeclarationWithType *member, Expression *aggregate, Expression *_aname = nullptr ); |
---|
227 | MemberExpr( const MemberExpr &other ); |
---|
228 | virtual ~MemberExpr(); |
---|
229 | |
---|
230 | DeclarationWithType *get_member() const { return member; } |
---|
231 | void set_member( DeclarationWithType *newValue ) { member = newValue; } |
---|
232 | Expression *get_aggregate() const { return aggregate; } |
---|
233 | void set_aggregate( Expression *newValue ) { aggregate = newValue; } |
---|
234 | |
---|
235 | virtual MemberExpr *clone() const { return new MemberExpr( *this ); } |
---|
236 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
237 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
238 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
239 | private: |
---|
240 | DeclarationWithType *member; |
---|
241 | Expression *aggregate; |
---|
242 | }; |
---|
243 | |
---|
244 | /// VariableExpr represents an expression that simply refers to the value of a named variable |
---|
245 | class VariableExpr : public Expression { |
---|
246 | public: |
---|
247 | VariableExpr( DeclarationWithType *var, Expression *_aname = nullptr ); |
---|
248 | VariableExpr( const VariableExpr &other ); |
---|
249 | virtual ~VariableExpr(); |
---|
250 | |
---|
251 | DeclarationWithType *get_var() const { return var; } |
---|
252 | void set_var( DeclarationWithType *newValue ) { var = newValue; } |
---|
253 | |
---|
254 | virtual VariableExpr *clone() const { return new VariableExpr( *this ); } |
---|
255 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
256 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
257 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
258 | private: |
---|
259 | DeclarationWithType *var; |
---|
260 | }; |
---|
261 | |
---|
262 | /// ConstantExpr represents an expression that simply refers to the value of a constant |
---|
263 | class ConstantExpr : public Expression { |
---|
264 | public: |
---|
265 | ConstantExpr( Constant constant, Expression *_aname = nullptr ); |
---|
266 | ConstantExpr( const ConstantExpr &other ); |
---|
267 | virtual ~ConstantExpr(); |
---|
268 | |
---|
269 | Constant *get_constant() { return &constant; } |
---|
270 | void set_constant( const Constant &newValue ) { constant = newValue; } |
---|
271 | |
---|
272 | virtual ConstantExpr *clone() const { return new ConstantExpr( *this ); } |
---|
273 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
274 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
275 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
276 | private: |
---|
277 | Constant constant; |
---|
278 | }; |
---|
279 | |
---|
280 | /// SizeofExpr represents a sizeof expression (could be sizeof(int) or sizeof 3+4) |
---|
281 | class SizeofExpr : public Expression { |
---|
282 | public: |
---|
283 | SizeofExpr( Expression *expr, Expression *_aname = nullptr ); |
---|
284 | SizeofExpr( const SizeofExpr &other ); |
---|
285 | SizeofExpr( Type *type, Expression *_aname = nullptr ); |
---|
286 | virtual ~SizeofExpr(); |
---|
287 | |
---|
288 | Expression *get_expr() const { return expr; } |
---|
289 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
290 | Type *get_type() const { return type; } |
---|
291 | void set_type( Type *newValue ) { type = newValue; } |
---|
292 | bool get_isType() const { return isType; } |
---|
293 | void set_isType( bool newValue ) { isType = newValue; } |
---|
294 | |
---|
295 | virtual SizeofExpr *clone() const { return new SizeofExpr( *this ); } |
---|
296 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
297 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
298 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
299 | private: |
---|
300 | Expression *expr; |
---|
301 | Type *type; |
---|
302 | bool isType; |
---|
303 | }; |
---|
304 | |
---|
305 | /// AlignofExpr represents an alignof expression |
---|
306 | class AlignofExpr : public Expression { |
---|
307 | public: |
---|
308 | AlignofExpr( Expression *expr, Expression *_aname = nullptr ); |
---|
309 | AlignofExpr( const AlignofExpr &other ); |
---|
310 | AlignofExpr( Type *type, Expression *_aname = nullptr ); |
---|
311 | virtual ~AlignofExpr(); |
---|
312 | |
---|
313 | Expression *get_expr() const { return expr; } |
---|
314 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
315 | Type *get_type() const { return type; } |
---|
316 | void set_type( Type *newValue ) { type = newValue; } |
---|
317 | bool get_isType() const { return isType; } |
---|
318 | void set_isType( bool newValue ) { isType = newValue; } |
---|
319 | |
---|
320 | virtual AlignofExpr *clone() const { return new AlignofExpr( *this ); } |
---|
321 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
322 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
323 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
324 | private: |
---|
325 | Expression *expr; |
---|
326 | Type *type; |
---|
327 | bool isType; |
---|
328 | }; |
---|
329 | |
---|
330 | /// UntypedOffsetofExpr represents an offsetof expression before resolution |
---|
331 | class UntypedOffsetofExpr : public Expression { |
---|
332 | public: |
---|
333 | UntypedOffsetofExpr( Type *type, const std::string &member, Expression *_aname = nullptr ); |
---|
334 | UntypedOffsetofExpr( const UntypedOffsetofExpr &other ); |
---|
335 | virtual ~UntypedOffsetofExpr(); |
---|
336 | |
---|
337 | std::string get_member() const { return member; } |
---|
338 | void set_member( const std::string &newValue ) { member = newValue; } |
---|
339 | Type *get_type() const { return type; } |
---|
340 | void set_type( Type *newValue ) { type = newValue; } |
---|
341 | |
---|
342 | virtual UntypedOffsetofExpr *clone() const { return new UntypedOffsetofExpr( *this ); } |
---|
343 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
344 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
345 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
346 | private: |
---|
347 | Type *type; |
---|
348 | std::string member; |
---|
349 | }; |
---|
350 | |
---|
351 | /// OffsetofExpr represents an offsetof expression |
---|
352 | class OffsetofExpr : public Expression { |
---|
353 | public: |
---|
354 | OffsetofExpr( Type *type, DeclarationWithType *member, Expression *_aname = nullptr ); |
---|
355 | OffsetofExpr( const OffsetofExpr &other ); |
---|
356 | virtual ~OffsetofExpr(); |
---|
357 | |
---|
358 | Type *get_type() const { return type; } |
---|
359 | void set_type( Type *newValue ) { type = newValue; } |
---|
360 | DeclarationWithType *get_member() const { return member; } |
---|
361 | void set_member( DeclarationWithType *newValue ) { member = newValue; } |
---|
362 | |
---|
363 | virtual OffsetofExpr *clone() const { return new OffsetofExpr( *this ); } |
---|
364 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
365 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
366 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
367 | private: |
---|
368 | Type *type; |
---|
369 | DeclarationWithType *member; |
---|
370 | }; |
---|
371 | |
---|
372 | /// Expression representing a pack of field-offsets for a generic type |
---|
373 | class OffsetPackExpr : public Expression { |
---|
374 | public: |
---|
375 | OffsetPackExpr( StructInstType *type_, Expression *aname_ = 0 ); |
---|
376 | OffsetPackExpr( const OffsetPackExpr &other ); |
---|
377 | virtual ~OffsetPackExpr(); |
---|
378 | |
---|
379 | StructInstType *get_type() const { return type; } |
---|
380 | void set_type( StructInstType *newValue ) { type = newValue; } |
---|
381 | |
---|
382 | virtual OffsetPackExpr *clone() const { return new OffsetPackExpr( *this ); } |
---|
383 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
384 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
385 | |
---|
386 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
387 | |
---|
388 | private: |
---|
389 | StructInstType *type; |
---|
390 | }; |
---|
391 | |
---|
392 | /// AttrExpr represents an @attribute expression (like sizeof, but user-defined) |
---|
393 | class AttrExpr : public Expression { |
---|
394 | public: |
---|
395 | AttrExpr(Expression *attr, Expression *expr, Expression *_aname = nullptr ); |
---|
396 | AttrExpr( const AttrExpr &other ); |
---|
397 | AttrExpr( Expression *attr, Type *type, Expression *_aname = nullptr ); |
---|
398 | virtual ~AttrExpr(); |
---|
399 | |
---|
400 | Expression *get_attr() const { return attr; } |
---|
401 | void set_attr( Expression *newValue ) { attr = newValue; } |
---|
402 | Expression *get_expr() const { return expr; } |
---|
403 | void set_expr( Expression *newValue ) { expr = newValue; } |
---|
404 | Type *get_type() const { return type; } |
---|
405 | void set_type( Type *newValue ) { type = newValue; } |
---|
406 | bool get_isType() const { return isType; } |
---|
407 | void set_isType( bool newValue ) { isType = newValue; } |
---|
408 | |
---|
409 | virtual AttrExpr *clone() const { return new AttrExpr( *this ); } |
---|
410 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
411 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
412 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
413 | private: |
---|
414 | Expression *attr; |
---|
415 | Expression *expr; |
---|
416 | Type *type; |
---|
417 | bool isType; |
---|
418 | }; |
---|
419 | |
---|
420 | /// LogicalExpr represents a short-circuit boolean expression (&& or ||) |
---|
421 | class LogicalExpr : public Expression { |
---|
422 | public: |
---|
423 | LogicalExpr( Expression *arg1, Expression *arg2, bool andp = true, Expression *_aname = nullptr ); |
---|
424 | LogicalExpr( const LogicalExpr &other ); |
---|
425 | virtual ~LogicalExpr(); |
---|
426 | |
---|
427 | bool get_isAnd() const { return isAnd; } |
---|
428 | Expression *get_arg1() { return arg1; } |
---|
429 | void set_arg1( Expression *newValue ) { arg1 = newValue; } |
---|
430 | Expression *get_arg2() const { return arg2; } |
---|
431 | void set_arg2( Expression *newValue ) { arg2 = newValue; } |
---|
432 | |
---|
433 | virtual LogicalExpr *clone() const { return new LogicalExpr( *this ); } |
---|
434 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
435 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
436 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
437 | private: |
---|
438 | Expression *arg1; |
---|
439 | Expression *arg2; |
---|
440 | bool isAnd; |
---|
441 | }; |
---|
442 | |
---|
443 | /// ConditionalExpr represents the three-argument conditional ( p ? a : b ) |
---|
444 | class ConditionalExpr : public Expression { |
---|
445 | public: |
---|
446 | ConditionalExpr( Expression *arg1, Expression *arg2, Expression *arg3, Expression *_aname = nullptr ); |
---|
447 | ConditionalExpr( const ConditionalExpr &other ); |
---|
448 | virtual ~ConditionalExpr(); |
---|
449 | |
---|
450 | Expression *get_arg1() const { return arg1; } |
---|
451 | void set_arg1( Expression *newValue ) { arg1 = newValue; } |
---|
452 | Expression *get_arg2() const { return arg2; } |
---|
453 | void set_arg2( Expression *newValue ) { arg2 = newValue; } |
---|
454 | Expression *get_arg3() const { return arg3; } |
---|
455 | void set_arg3( Expression *newValue ) { arg3 = newValue; } |
---|
456 | |
---|
457 | virtual ConditionalExpr *clone() const { return new ConditionalExpr( *this ); } |
---|
458 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
459 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
460 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
461 | private: |
---|
462 | Expression *arg1; |
---|
463 | Expression *arg2; |
---|
464 | Expression *arg3; |
---|
465 | }; |
---|
466 | |
---|
467 | /// CommaExpr represents the sequence operator ( a, b ) |
---|
468 | class CommaExpr : public Expression { |
---|
469 | public: |
---|
470 | CommaExpr( Expression *arg1, Expression *arg2, Expression *_aname = nullptr ); |
---|
471 | CommaExpr( const CommaExpr &other ); |
---|
472 | virtual ~CommaExpr(); |
---|
473 | |
---|
474 | Expression *get_arg1() const { return arg1; } |
---|
475 | void set_arg1( Expression *newValue ) { arg1 = newValue; } |
---|
476 | Expression *get_arg2() const { return arg2; } |
---|
477 | void set_arg2( Expression *newValue ) { arg2 = newValue; } |
---|
478 | |
---|
479 | virtual CommaExpr *clone() const { return new CommaExpr( *this ); } |
---|
480 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
481 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
482 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
483 | private: |
---|
484 | Expression *arg1; |
---|
485 | Expression *arg2; |
---|
486 | }; |
---|
487 | |
---|
488 | /// TypeExpr represents a type used in an expression (e.g. as a type generator parameter) |
---|
489 | class TypeExpr : public Expression { |
---|
490 | public: |
---|
491 | TypeExpr( Type *type ); |
---|
492 | TypeExpr( const TypeExpr &other ); |
---|
493 | virtual ~TypeExpr(); |
---|
494 | |
---|
495 | Type *get_type() const { return type; } |
---|
496 | void set_type( Type *newValue ) { type = newValue; } |
---|
497 | |
---|
498 | virtual TypeExpr *clone() const { return new TypeExpr( *this ); } |
---|
499 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
500 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
501 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
502 | private: |
---|
503 | Type *type; |
---|
504 | }; |
---|
505 | |
---|
506 | /// AsmExpr represents a GCC 'asm constraint operand' used in an asm statement: [output] "=f" (result) |
---|
507 | class AsmExpr : public Expression { |
---|
508 | public: |
---|
509 | AsmExpr( Expression *inout, ConstantExpr *constraint, Expression *operand ) : inout( inout ), constraint( constraint ), operand( operand ) {} |
---|
510 | AsmExpr( const AsmExpr & other ); |
---|
511 | virtual ~AsmExpr() { delete inout; delete constraint; delete operand; }; |
---|
512 | |
---|
513 | Expression *get_inout() const { return inout; } |
---|
514 | void set_inout( Expression *newValue ) { inout = newValue; } |
---|
515 | |
---|
516 | ConstantExpr *get_constraint() const { return constraint; } |
---|
517 | void set_constraint( ConstantExpr *newValue ) { constraint = newValue; } |
---|
518 | |
---|
519 | Expression *get_operand() const { return operand; } |
---|
520 | void set_operand( Expression *newValue ) { operand = newValue; } |
---|
521 | |
---|
522 | virtual AsmExpr *clone() const { return new AsmExpr( *this ); } |
---|
523 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
524 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
525 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
526 | private: |
---|
527 | // https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints |
---|
528 | Expression *inout; |
---|
529 | ConstantExpr *constraint; |
---|
530 | Expression *operand; |
---|
531 | }; |
---|
532 | |
---|
533 | /// ImplicitCopyCtorExpr represents the application of a function to a set of parameters, |
---|
534 | /// along with a set of copy constructor calls, one for each argument. |
---|
535 | class ImplicitCopyCtorExpr : public Expression { |
---|
536 | public: |
---|
537 | ImplicitCopyCtorExpr( ApplicationExpr * callExpr ); |
---|
538 | ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other ); |
---|
539 | virtual ~ImplicitCopyCtorExpr(); |
---|
540 | |
---|
541 | ApplicationExpr *get_callExpr() const { return callExpr; } |
---|
542 | void set_callExpr( ApplicationExpr *newValue ) { callExpr = newValue; } |
---|
543 | |
---|
544 | std::list< ObjectDecl * > & get_tempDecls() { return tempDecls; } |
---|
545 | void set_tempDecls( std::list< ObjectDecl * > newValue ) { tempDecls = newValue; } |
---|
546 | |
---|
547 | std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; } |
---|
548 | void set_returnDecls( std::list< ObjectDecl * > newValue ) { returnDecls = newValue; } |
---|
549 | |
---|
550 | std::list< Expression * > & get_dtors() { return dtors; } |
---|
551 | void set_dtors( std::list< Expression * > newValue ) { dtors = newValue; } |
---|
552 | |
---|
553 | virtual ImplicitCopyCtorExpr *clone() const { return new ImplicitCopyCtorExpr( *this ); } |
---|
554 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
555 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
556 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
557 | private: |
---|
558 | ApplicationExpr * callExpr; |
---|
559 | std::list< ObjectDecl * > tempDecls; |
---|
560 | std::list< ObjectDecl * > returnDecls; |
---|
561 | std::list< Expression * > dtors; |
---|
562 | }; |
---|
563 | |
---|
564 | /// ConstructorExpr represents the use of a constructor in an expression context, e.g. int * x = malloc() { 5 }; |
---|
565 | class ConstructorExpr : public Expression { |
---|
566 | public: |
---|
567 | ConstructorExpr( Expression * callExpr ); |
---|
568 | ConstructorExpr( const ConstructorExpr & other ); |
---|
569 | ~ConstructorExpr(); |
---|
570 | |
---|
571 | Expression *get_callExpr() const { return callExpr; } |
---|
572 | void set_callExpr( Expression *newValue ) { callExpr = newValue; } |
---|
573 | |
---|
574 | virtual ConstructorExpr *clone() const { return new ConstructorExpr( *this ); } |
---|
575 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
576 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
577 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
578 | private: |
---|
579 | Expression * callExpr; |
---|
580 | }; |
---|
581 | |
---|
582 | /// CompoundLiteralExpr represents a C99 'compound literal' |
---|
583 | class CompoundLiteralExpr : public Expression { |
---|
584 | public: |
---|
585 | CompoundLiteralExpr( Type * type, Initializer * initializer ); |
---|
586 | CompoundLiteralExpr( const CompoundLiteralExpr &other ); |
---|
587 | virtual ~CompoundLiteralExpr(); |
---|
588 | |
---|
589 | Type * get_type() const { return type; } |
---|
590 | void set_type( Type * t ) { type = t; } |
---|
591 | |
---|
592 | Initializer * get_initializer() const { return initializer; } |
---|
593 | void set_initializer( Initializer * i ) { initializer = i; } |
---|
594 | |
---|
595 | virtual CompoundLiteralExpr *clone() const { return new CompoundLiteralExpr( *this ); } |
---|
596 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
597 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
598 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
599 | private: |
---|
600 | Type * type; |
---|
601 | Initializer * initializer; |
---|
602 | }; |
---|
603 | |
---|
604 | /// ValofExpr represents a GCC 'lambda expression' |
---|
605 | class UntypedValofExpr : public Expression { |
---|
606 | public: |
---|
607 | UntypedValofExpr( Statement *_body, Expression *_aname = nullptr ) : Expression( _aname ), body ( _body ) {} |
---|
608 | UntypedValofExpr( const UntypedValofExpr & other ); |
---|
609 | virtual ~UntypedValofExpr(); |
---|
610 | |
---|
611 | Expression *get_value(); |
---|
612 | Statement *get_body() const { return body; } |
---|
613 | |
---|
614 | virtual UntypedValofExpr *clone() const { return new UntypedValofExpr( *this ); } |
---|
615 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
616 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
617 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
618 | private: |
---|
619 | Statement *body; |
---|
620 | }; |
---|
621 | |
---|
622 | /// RangeExpr represents a range e.g. '3 ... 5' or '1~10' |
---|
623 | class RangeExpr : public Expression { |
---|
624 | public: |
---|
625 | RangeExpr( Expression *low, Expression *high ); |
---|
626 | RangeExpr( const RangeExpr &other ); |
---|
627 | |
---|
628 | Expression * get_low() const { return low; } |
---|
629 | Expression * get_high() const { return high; } |
---|
630 | RangeExpr * set_low( Expression *low ) { RangeExpr::low = low; return this; } |
---|
631 | RangeExpr * set_high( Expression *high ) { RangeExpr::high = high; return this; } |
---|
632 | |
---|
633 | virtual RangeExpr *clone() const { return new RangeExpr( *this ); } |
---|
634 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
635 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
636 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
637 | private: |
---|
638 | Expression *low, *high; |
---|
639 | }; |
---|
640 | |
---|
641 | /// TupleExpr represents a tuple expression ( [a, b, c] ) |
---|
642 | class TupleExpr : public Expression { |
---|
643 | public: |
---|
644 | TupleExpr( const std::list< Expression * > & exprs = std::list< Expression * >(), Expression *_aname = nullptr ); |
---|
645 | TupleExpr( const TupleExpr &other ); |
---|
646 | virtual ~TupleExpr(); |
---|
647 | |
---|
648 | void set_exprs( std::list<Expression*> newValue ) { exprs = newValue; } |
---|
649 | std::list<Expression*>& get_exprs() { return exprs; } |
---|
650 | |
---|
651 | virtual TupleExpr *clone() const { return new TupleExpr( *this ); } |
---|
652 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
653 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
654 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
655 | private: |
---|
656 | std::list<Expression*> exprs; |
---|
657 | }; |
---|
658 | |
---|
659 | /// TupleIndexExpr represents an element selection operation on a tuple value, e.g. t.3 after processing by the expression analyzer |
---|
660 | class TupleIndexExpr : public Expression { |
---|
661 | public: |
---|
662 | TupleIndexExpr( Expression * tuple, unsigned int index ); |
---|
663 | TupleIndexExpr( const TupleIndexExpr &other ); |
---|
664 | virtual ~TupleIndexExpr(); |
---|
665 | |
---|
666 | Expression * get_tuple() const { return tuple; } |
---|
667 | int get_index() const { return index; } |
---|
668 | TupleIndexExpr * set_tuple( Expression *newValue ) { tuple = newValue; return this; } |
---|
669 | TupleIndexExpr * set_index( unsigned int newValue ) { index = newValue; return this; } |
---|
670 | |
---|
671 | virtual TupleIndexExpr *clone() const { return new TupleIndexExpr( *this ); } |
---|
672 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
673 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
674 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
675 | private: |
---|
676 | Expression * tuple; |
---|
677 | unsigned int index; |
---|
678 | }; |
---|
679 | |
---|
680 | /// MemberTupleExpr represents a tuple member selection operation on a struct type, e.g. s.[a, b, c] after processing by the expression analyzer |
---|
681 | class MemberTupleExpr : public Expression { |
---|
682 | public: |
---|
683 | MemberTupleExpr( Expression * member, Expression * aggregate, Expression * _aname = nullptr ); |
---|
684 | MemberTupleExpr( const MemberTupleExpr &other ); |
---|
685 | virtual ~MemberTupleExpr(); |
---|
686 | |
---|
687 | Expression * get_member() const { return member; } |
---|
688 | Expression * get_aggregate() const { return aggregate; } |
---|
689 | MemberTupleExpr * set_member( Expression *newValue ) { member = newValue; return this; } |
---|
690 | MemberTupleExpr * set_aggregate( Expression *newValue ) { aggregate = newValue; return this; } |
---|
691 | |
---|
692 | virtual MemberTupleExpr *clone() const { return new MemberTupleExpr( *this ); } |
---|
693 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
694 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
695 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
696 | private: |
---|
697 | Expression * member; |
---|
698 | Expression * aggregate; |
---|
699 | }; |
---|
700 | |
---|
701 | /// TupleAssignExpr represents a multiple assignment operation, where both sides of the assignment have tuple type, e.g. [a, b, c] = [d, e, f];, a mass assignment operation, where the left hand side has tuple type and the right hand side does not, e.g. [a, b, c] = 5.0;, or a tuple ctor/dtor expression |
---|
702 | class TupleAssignExpr : public Expression { |
---|
703 | public: |
---|
704 | TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls, Expression * _aname = nullptr ); |
---|
705 | TupleAssignExpr( const TupleAssignExpr &other ); |
---|
706 | virtual ~TupleAssignExpr(); |
---|
707 | |
---|
708 | std::list< Expression * > & get_assigns() { return assigns; } |
---|
709 | std::list< ObjectDecl * > & get_tempDecls() { return tempDecls; } |
---|
710 | |
---|
711 | virtual TupleAssignExpr *clone() const { return new TupleAssignExpr( *this ); } |
---|
712 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
713 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
714 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
715 | private: |
---|
716 | std::list< Expression * > assigns; // assignment expressions that use tempDecls |
---|
717 | std::list< ObjectDecl * > tempDecls; // temporaries for address of lhs exprs |
---|
718 | }; |
---|
719 | |
---|
720 | /// StmtExpr represents a GCC 'statement expression', e.g. ({ int x = 5; x; }) |
---|
721 | class StmtExpr : public Expression { |
---|
722 | public: |
---|
723 | StmtExpr( CompoundStmt *statements ); |
---|
724 | StmtExpr( const StmtExpr & other ); |
---|
725 | virtual ~StmtExpr(); |
---|
726 | |
---|
727 | CompoundStmt * get_statements() const { return statements; } |
---|
728 | StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; } |
---|
729 | |
---|
730 | virtual StmtExpr *clone() const { return new StmtExpr( *this ); } |
---|
731 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
732 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
733 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
734 | private: |
---|
735 | CompoundStmt * statements; |
---|
736 | }; |
---|
737 | |
---|
738 | class UniqueExpr : public Expression { |
---|
739 | public: |
---|
740 | UniqueExpr( Expression * expr, long long idVal = -1 ); |
---|
741 | UniqueExpr( const UniqueExpr & other ); |
---|
742 | ~UniqueExpr(); |
---|
743 | |
---|
744 | Expression * get_expr() const { return expr; } |
---|
745 | UniqueExpr * set_expr( Expression * newValue ) { expr = newValue; return this; } |
---|
746 | |
---|
747 | ObjectDecl * get_object() const { return object; } |
---|
748 | UniqueExpr * set_object( ObjectDecl * newValue ) { object = newValue; return this; } |
---|
749 | |
---|
750 | VariableExpr * get_var() const { return var; } |
---|
751 | UniqueExpr * set_var( VariableExpr * newValue ) { var = newValue; return this; } |
---|
752 | |
---|
753 | int get_id() const { return id; } |
---|
754 | |
---|
755 | virtual UniqueExpr *clone() const { return new UniqueExpr( *this ); } |
---|
756 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
757 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
758 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
759 | private: |
---|
760 | Expression * expr; |
---|
761 | ObjectDecl * object; |
---|
762 | VariableExpr * var; |
---|
763 | int id; |
---|
764 | static long long count; |
---|
765 | }; |
---|
766 | |
---|
767 | std::ostream & operator<<( std::ostream & out, const Expression * expr ); |
---|
768 | |
---|
769 | #endif // EXPRESSION_H |
---|
770 | |
---|
771 | // Local Variables: // |
---|
772 | // tab-width: 4 // |
---|
773 | // mode: c++ // |
---|
774 | // compile-command: "make install" // |
---|
775 | // End: // |
---|