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 | std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; } |
---|
546 | std::list< Expression * > & get_dtors() { return dtors; } |
---|
547 | |
---|
548 | virtual ImplicitCopyCtorExpr *clone() const { return new ImplicitCopyCtorExpr( *this ); } |
---|
549 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
550 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
551 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
552 | private: |
---|
553 | ApplicationExpr * callExpr; |
---|
554 | std::list< ObjectDecl * > tempDecls; |
---|
555 | std::list< ObjectDecl * > returnDecls; |
---|
556 | std::list< Expression * > dtors; |
---|
557 | }; |
---|
558 | |
---|
559 | /// ConstructorExpr represents the use of a constructor in an expression context, e.g. int * x = malloc() { 5 }; |
---|
560 | class ConstructorExpr : public Expression { |
---|
561 | public: |
---|
562 | ConstructorExpr( Expression * callExpr ); |
---|
563 | ConstructorExpr( const ConstructorExpr & other ); |
---|
564 | ~ConstructorExpr(); |
---|
565 | |
---|
566 | Expression *get_callExpr() const { return callExpr; } |
---|
567 | void set_callExpr( Expression *newValue ) { callExpr = newValue; } |
---|
568 | |
---|
569 | virtual ConstructorExpr *clone() const { return new ConstructorExpr( *this ); } |
---|
570 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
571 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
572 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
573 | private: |
---|
574 | Expression * callExpr; |
---|
575 | }; |
---|
576 | |
---|
577 | /// CompoundLiteralExpr represents a C99 'compound literal' |
---|
578 | class CompoundLiteralExpr : public Expression { |
---|
579 | public: |
---|
580 | CompoundLiteralExpr( Type * type, Initializer * initializer ); |
---|
581 | CompoundLiteralExpr( const CompoundLiteralExpr &other ); |
---|
582 | virtual ~CompoundLiteralExpr(); |
---|
583 | |
---|
584 | Type * get_type() const { return type; } |
---|
585 | void set_type( Type * t ) { type = t; } |
---|
586 | |
---|
587 | Initializer * get_initializer() const { return initializer; } |
---|
588 | void set_initializer( Initializer * i ) { initializer = i; } |
---|
589 | |
---|
590 | virtual CompoundLiteralExpr *clone() const { return new CompoundLiteralExpr( *this ); } |
---|
591 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
592 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
593 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
594 | private: |
---|
595 | Type * type; |
---|
596 | Initializer * initializer; |
---|
597 | }; |
---|
598 | |
---|
599 | /// ValofExpr represents a GCC 'lambda expression' |
---|
600 | class UntypedValofExpr : public Expression { |
---|
601 | public: |
---|
602 | UntypedValofExpr( Statement *_body, Expression *_aname = nullptr ) : Expression( _aname ), body ( _body ) {} |
---|
603 | UntypedValofExpr( const UntypedValofExpr & other ); |
---|
604 | virtual ~UntypedValofExpr(); |
---|
605 | |
---|
606 | Expression *get_value(); |
---|
607 | Statement *get_body() const { return body; } |
---|
608 | |
---|
609 | virtual UntypedValofExpr *clone() const { return new UntypedValofExpr( *this ); } |
---|
610 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
611 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
612 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
613 | private: |
---|
614 | Statement *body; |
---|
615 | }; |
---|
616 | |
---|
617 | /// RangeExpr represents a range e.g. '3 ... 5' or '1~10' |
---|
618 | class RangeExpr : public Expression { |
---|
619 | public: |
---|
620 | RangeExpr( Expression *low, Expression *high ); |
---|
621 | RangeExpr( const RangeExpr &other ); |
---|
622 | |
---|
623 | Expression * get_low() const { return low; } |
---|
624 | Expression * get_high() const { return high; } |
---|
625 | RangeExpr * set_low( Expression *low ) { RangeExpr::low = low; return this; } |
---|
626 | RangeExpr * set_high( Expression *high ) { RangeExpr::high = high; return this; } |
---|
627 | |
---|
628 | virtual RangeExpr *clone() const { return new RangeExpr( *this ); } |
---|
629 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
630 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
631 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
632 | private: |
---|
633 | Expression *low, *high; |
---|
634 | }; |
---|
635 | |
---|
636 | /// TupleExpr represents a tuple expression ( [a, b, c] ) |
---|
637 | class TupleExpr : public Expression { |
---|
638 | public: |
---|
639 | TupleExpr( const std::list< Expression * > & exprs = std::list< Expression * >(), Expression *_aname = nullptr ); |
---|
640 | TupleExpr( const TupleExpr &other ); |
---|
641 | virtual ~TupleExpr(); |
---|
642 | |
---|
643 | void set_exprs( std::list<Expression*> newValue ) { exprs = newValue; } |
---|
644 | std::list<Expression*>& get_exprs() { return exprs; } |
---|
645 | |
---|
646 | virtual TupleExpr *clone() const { return new TupleExpr( *this ); } |
---|
647 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
648 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
649 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
650 | private: |
---|
651 | std::list<Expression*> exprs; |
---|
652 | }; |
---|
653 | |
---|
654 | /// TupleIndexExpr represents an element selection operation on a tuple value, e.g. t.3 after processing by the expression analyzer |
---|
655 | class TupleIndexExpr : public Expression { |
---|
656 | public: |
---|
657 | TupleIndexExpr( Expression * tuple, unsigned int index ); |
---|
658 | TupleIndexExpr( const TupleIndexExpr &other ); |
---|
659 | virtual ~TupleIndexExpr(); |
---|
660 | |
---|
661 | Expression * get_tuple() const { return tuple; } |
---|
662 | int get_index() const { return index; } |
---|
663 | TupleIndexExpr * set_tuple( Expression *newValue ) { tuple = newValue; return this; } |
---|
664 | TupleIndexExpr * set_index( unsigned int newValue ) { index = newValue; return this; } |
---|
665 | |
---|
666 | virtual TupleIndexExpr *clone() const { return new TupleIndexExpr( *this ); } |
---|
667 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
668 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
669 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
670 | private: |
---|
671 | Expression * tuple; |
---|
672 | unsigned int index; |
---|
673 | }; |
---|
674 | |
---|
675 | /// MemberTupleExpr represents a tuple member selection operation on a struct type, e.g. s.[a, b, c] after processing by the expression analyzer |
---|
676 | class MemberTupleExpr : public Expression { |
---|
677 | public: |
---|
678 | MemberTupleExpr( Expression * member, Expression * aggregate, Expression * _aname = nullptr ); |
---|
679 | MemberTupleExpr( const MemberTupleExpr &other ); |
---|
680 | virtual ~MemberTupleExpr(); |
---|
681 | |
---|
682 | Expression * get_member() const { return member; } |
---|
683 | Expression * get_aggregate() const { return aggregate; } |
---|
684 | MemberTupleExpr * set_member( Expression *newValue ) { member = newValue; return this; } |
---|
685 | MemberTupleExpr * set_aggregate( Expression *newValue ) { aggregate = newValue; return this; } |
---|
686 | |
---|
687 | virtual MemberTupleExpr *clone() const { return new MemberTupleExpr( *this ); } |
---|
688 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
689 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
690 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
691 | private: |
---|
692 | Expression * member; |
---|
693 | Expression * aggregate; |
---|
694 | }; |
---|
695 | |
---|
696 | /// 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 |
---|
697 | class TupleAssignExpr : public Expression { |
---|
698 | public: |
---|
699 | TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls, Expression * _aname = nullptr ); |
---|
700 | TupleAssignExpr( const TupleAssignExpr &other ); |
---|
701 | virtual ~TupleAssignExpr(); |
---|
702 | |
---|
703 | TupleAssignExpr * set_stmtExpr( StmtExpr * newValue ) { stmtExpr = newValue; return this; } |
---|
704 | StmtExpr * get_stmtExpr() const { return stmtExpr; } |
---|
705 | |
---|
706 | virtual TupleAssignExpr *clone() const { return new TupleAssignExpr( *this ); } |
---|
707 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
708 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
709 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
710 | private: |
---|
711 | StmtExpr * stmtExpr = nullptr; |
---|
712 | }; |
---|
713 | |
---|
714 | /// StmtExpr represents a GCC 'statement expression', e.g. ({ int x = 5; x; }) |
---|
715 | class StmtExpr : public Expression { |
---|
716 | public: |
---|
717 | StmtExpr( CompoundStmt *statements ); |
---|
718 | StmtExpr( const StmtExpr & other ); |
---|
719 | virtual ~StmtExpr(); |
---|
720 | |
---|
721 | CompoundStmt * get_statements() const { return statements; } |
---|
722 | StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; } |
---|
723 | |
---|
724 | std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; } |
---|
725 | std::list< Expression * > & get_dtors() { return dtors; } |
---|
726 | |
---|
727 | virtual StmtExpr *clone() const { return new StmtExpr( *this ); } |
---|
728 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
729 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
730 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
731 | private: |
---|
732 | CompoundStmt * statements; |
---|
733 | std::list< ObjectDecl * > returnDecls; // return variable(s) for stmt expression |
---|
734 | std::list< Expression * > dtors; // destructor(s) for return variable(s) |
---|
735 | }; |
---|
736 | |
---|
737 | class UniqueExpr : public Expression { |
---|
738 | public: |
---|
739 | UniqueExpr( Expression * expr, long long idVal = -1 ); |
---|
740 | UniqueExpr( const UniqueExpr & other ); |
---|
741 | ~UniqueExpr(); |
---|
742 | |
---|
743 | Expression * get_expr() const { return expr; } |
---|
744 | UniqueExpr * set_expr( Expression * newValue ) { expr = newValue; return this; } |
---|
745 | |
---|
746 | ObjectDecl * get_object() const { return object; } |
---|
747 | UniqueExpr * set_object( ObjectDecl * newValue ) { object = newValue; return this; } |
---|
748 | |
---|
749 | VariableExpr * get_var() const { return var; } |
---|
750 | UniqueExpr * set_var( VariableExpr * newValue ) { var = newValue; return this; } |
---|
751 | |
---|
752 | int get_id() const { return id; } |
---|
753 | |
---|
754 | virtual UniqueExpr *clone() const { return new UniqueExpr( *this ); } |
---|
755 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
756 | virtual Expression *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
757 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
758 | private: |
---|
759 | Expression * expr; |
---|
760 | ObjectDecl * object; |
---|
761 | VariableExpr * var; |
---|
762 | int id; |
---|
763 | static long long count; |
---|
764 | }; |
---|
765 | |
---|
766 | std::ostream & operator<<( std::ostream & out, const Expression * expr ); |
---|
767 | |
---|
768 | #endif // EXPRESSION_H |
---|
769 | |
---|
770 | // Local Variables: // |
---|
771 | // tab-width: 4 // |
---|
772 | // mode: c++ // |
---|
773 | // compile-command: "make install" // |
---|
774 | // End: // |
---|