source: src/SynTree/Expression.h@ 665f432

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 665f432 was 665f432, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fixed trac #149 where operand names in asm statements where incorrectly resolved (i.e., should not have been resolved)

  • Property mode set to 100644
File size: 37.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 : Andrew Beach
12// Last Modified On : Thr Aug 15 13:46:00 2019
13// Update Count : 54
14//
15
16#pragma once
17
18#include <iosfwd> // for ostream
19#include <list> // for list, list<>::iterator
20#include <map> // for map, map<>::value_compare
21#include <memory> // for allocator, unique_ptr
22#include <string> // for string
23#include <vector> // for vector
24
25#include "BaseSyntaxNode.h" // for BaseSyntaxNode
26#include "Constant.h" // for Constant
27#include "Initializer.h" // for Designation (ptr only), Initializer
28#include "Label.h" // for Label
29#include "Mutator.h" // for Mutator
30#include "SynTree.h" // for UniqueId
31#include "Visitor.h" // for Visitor
32
33
34struct ParamEntry;
35
36typedef std::map< UniqueId, ParamEntry > InferredParams;
37
38/// ParamEntry contains the i.d. of a declaration and a type that is derived from that declaration,
39/// but subject to decay-to-pointer and type parameter renaming
40struct ParamEntry {
41 ParamEntry(): decl( 0 ), declptr( nullptr ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {}
42 ParamEntry( UniqueId decl, Declaration * declptr, Type * actualType, Type * formalType, Expression* expr );
43 ParamEntry( const ParamEntry & other );
44 ParamEntry( ParamEntry && other );
45 ~ParamEntry();
46 ParamEntry & operator=( ParamEntry && other );
47
48 UniqueId const decl;
49 Declaration * const declptr;
50 Type * const actualType;
51 Type * const formalType;
52 Expression * expr;
53};
54
55/// Expression is the root type for all expressions
56class Expression : public BaseSyntaxNode {
57 public:
58 Type * result;
59 TypeSubstitution * env;
60 bool extension = false;
61 InferredParams inferParams; ///< Post-resolution inferred parameter slots
62 std::vector<UniqueId> resnSlots; ///< Pre-resolution inferred parameter slots
63
64 // xxx - should turn inferParams+resnSlots into a union to save some memory
65
66 Expression();
67 Expression( const Expression & other );
68 virtual ~Expression();
69
70 Type *& get_result() { return result; }
71 const Type * get_result() const { return result; }
72 void set_result( Type * newValue ) { result = newValue; }
73 virtual bool get_lvalue() const;
74
75 TypeSubstitution * get_env() const { return env; }
76 void set_env( TypeSubstitution * newValue ) { env = newValue; }
77 bool get_extension() const { return extension; }
78 Expression * set_extension( bool exten ) { extension = exten; return this; }
79
80 // move other's inferParams to this
81 void spliceInferParams( Expression * other );
82
83 virtual Expression * clone() const override = 0;
84 virtual void accept( Visitor & v ) override = 0;
85 virtual void accept( Visitor & v ) const override = 0;
86 virtual Expression * acceptMutator( Mutator & m ) override = 0;
87 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
88};
89
90/// ApplicationExpr represents the application of a function to a set of parameters. This is the result of running an
91/// UntypedExpr through the expression analyzer.
92class ApplicationExpr : public Expression {
93 public:
94 Expression * function;
95 std::list<Expression *> args;
96
97 ApplicationExpr( Expression * function, const std::list<Expression *> & args = std::list< Expression * >() );
98 ApplicationExpr( const ApplicationExpr & other );
99 virtual ~ApplicationExpr();
100
101 bool get_lvalue() const final;
102
103 Expression * get_function() const { return function; }
104 void set_function( Expression * newValue ) { function = newValue; }
105 std::list<Expression *>& get_args() { return args; }
106
107 virtual ApplicationExpr * clone() const override { return new ApplicationExpr( * this ); }
108 virtual void accept( Visitor & v ) override { v.visit( this ); }
109 virtual void accept( Visitor & v ) const override { v.visit( this ); }
110 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
111 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
112};
113
114/// UntypedExpr represents the application of a function to a set of parameters, but where the particular overload for
115/// the function name has not yet been determined. Most operators are converted into functional form automatically, to
116/// permit operator overloading.
117class UntypedExpr : public Expression {
118 public:
119 Expression * function;
120 std::list<Expression*> args;
121
122 UntypedExpr( Expression * function, const std::list<Expression *> & args = std::list< Expression * >() );
123 UntypedExpr( const UntypedExpr & other );
124 virtual ~UntypedExpr();
125
126 bool get_lvalue() const final;
127
128 Expression * get_function() const { return function; }
129 void set_function( Expression * newValue ) { function = newValue; }
130
131 std::list<Expression*>::iterator begin_args() { return args.begin(); }
132 std::list<Expression*>::iterator end_args() { return args.end(); }
133 std::list<Expression*>& get_args() { return args; }
134
135 static UntypedExpr * createDeref( Expression * arg );
136 static UntypedExpr * createAssign( Expression * arg1, Expression * arg2 );
137
138 virtual UntypedExpr * clone() const override { return new UntypedExpr( * this ); }
139 virtual void accept( Visitor & v ) override { v.visit( this ); }
140 virtual void accept( Visitor & v ) const override { v.visit( this ); }
141 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
142 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
143};
144
145/// NameExpr contains a name whose meaning is still not determined
146class NameExpr : public Expression {
147 public:
148 std::string name;
149
150 NameExpr( std::string name );
151 NameExpr( const NameExpr & other );
152 virtual ~NameExpr();
153
154 const std::string & get_name() const { return name; }
155 void set_name( std::string newValue ) { name = newValue; }
156
157 virtual NameExpr * clone() const override { return new NameExpr( * this ); }
158 virtual void accept( Visitor & v ) override { v.visit( this ); }
159 virtual void accept( Visitor & v ) const override { v.visit( this ); }
160 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
161 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
162};
163
164// The following classes are used to represent expression types that cannot be converted into
165// function-call format.
166
167/// AddressExpr represents a address-of expression, e.g. & e
168class AddressExpr : public Expression {
169 public:
170 Expression * arg;
171
172 AddressExpr( Expression * arg );
173 AddressExpr( const AddressExpr & other );
174 virtual ~AddressExpr();
175
176 Expression * get_arg() const { return arg; }
177 void set_arg(Expression * newValue ) { arg = newValue; }
178
179 virtual AddressExpr * clone() const override { return new AddressExpr( * this ); }
180 virtual void accept( Visitor & v ) override { v.visit( this ); }
181 virtual void accept( Visitor & v ) const override { v.visit( this ); }
182 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
183 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
184};
185
186// GCC &&label
187// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html
188class LabelAddressExpr : public Expression {
189 public:
190 Label arg;
191
192 LabelAddressExpr( const Label &arg );
193 LabelAddressExpr( const LabelAddressExpr & other );
194 virtual ~LabelAddressExpr();
195
196 virtual LabelAddressExpr * clone() const override { return new LabelAddressExpr( * this ); }
197 virtual void accept( Visitor & v ) override { v.visit( this ); }
198 virtual void accept( Visitor & v ) const override { v.visit( this ); }
199 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
200 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
201};
202
203/// CastExpr represents a type cast expression, e.g. (int)e
204class CastExpr : public Expression {
205 public:
206 Expression * arg;
207 bool isGenerated = true; // cast generated implicitly by code generation or explicit in program
208
209 CastExpr( Expression * arg, bool isGenerated = true );
210 CastExpr( Expression * arg, Type * toType, bool isGenerated = true );
211 CastExpr( Expression * arg, void * ) = delete; // prevent accidentally passing pointers for isGenerated in the first constructor
212 CastExpr( const CastExpr & other );
213 virtual ~CastExpr();
214
215 bool get_lvalue() const final;
216
217 Expression * get_arg() const { return arg; }
218 void set_arg( Expression * newValue ) { arg = newValue; }
219
220 virtual CastExpr * clone() const override { return new CastExpr( * this ); }
221 virtual void accept( Visitor & v ) override { v.visit( this ); }
222 virtual void accept( Visitor & v ) const override { v.visit( this ); }
223 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
224 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
225};
226
227/// KeywordCastExpr represents a cast to 'keyword types', e.g. (thread &)t
228class KeywordCastExpr : public Expression {
229public:
230 Expression * arg;
231 enum Target {
232 Coroutine, Thread, Monitor, NUMBER_OF_TARGETS
233 } target;
234
235 KeywordCastExpr( Expression * arg, Target target );
236 KeywordCastExpr( const KeywordCastExpr & other );
237 virtual ~KeywordCastExpr();
238
239 const std::string & targetString() const;
240
241 virtual KeywordCastExpr * clone() const override { return new KeywordCastExpr( * this ); }
242 virtual void accept( Visitor & v ) override { v.visit( this ); }
243 virtual void accept( Visitor & v ) const override { v.visit( this ); }
244 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
245 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
246};
247
248/// VirtualCastExpr repersents a virtual dynamic cast, e.g. (virtual exception)e
249class VirtualCastExpr : public Expression {
250 public:
251 Expression * arg;
252
253 VirtualCastExpr( Expression * arg, Type * toType );
254 VirtualCastExpr( const VirtualCastExpr & other );
255 virtual ~VirtualCastExpr();
256
257 Expression * get_arg() const { return arg; }
258 void set_arg( Expression * newValue ) { arg = newValue; }
259
260 virtual VirtualCastExpr * clone() const override { return new VirtualCastExpr( * this ); }
261 virtual void accept( Visitor & v ) override { v.visit( this ); }
262 virtual void accept( Visitor & v ) const override { v.visit( this ); }
263 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
264 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
265};
266
267/// UntypedMemberExpr represents a member selection operation, e.g. q.p before processing by the expression analyzer
268class UntypedMemberExpr : public Expression {
269 public:
270 Expression * member;
271 Expression * aggregate;
272
273 UntypedMemberExpr( Expression * member, Expression * aggregate );
274 UntypedMemberExpr( const UntypedMemberExpr & other );
275 virtual ~UntypedMemberExpr();
276
277 bool get_lvalue() const final;
278
279 Expression * get_member() const { return member; }
280 void set_member( Expression * newValue ) { member = newValue; }
281 Expression * get_aggregate() const { return aggregate; }
282 void set_aggregate( Expression * newValue ) { aggregate = newValue; }
283
284 virtual UntypedMemberExpr * clone() const override { return new UntypedMemberExpr( * this ); }
285 virtual void accept( Visitor & v ) override { v.visit( this ); }
286 virtual void accept( Visitor & v ) const override { v.visit( this ); }
287 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
288 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
289};
290
291/// MemberExpr represents a member selection operation, e.g. q.p after processing by the expression analyzer.
292/// Does not take ownership of member.
293class MemberExpr : public Expression {
294 public:
295 DeclarationWithType * member;
296 Expression * aggregate;
297
298 MemberExpr( DeclarationWithType * member, Expression * aggregate );
299 MemberExpr( const MemberExpr & other );
300 virtual ~MemberExpr();
301
302 bool get_lvalue() const final;
303
304 DeclarationWithType * get_member() const { return member; }
305 void set_member( DeclarationWithType * newValue ) { member = newValue; }
306 Expression * get_aggregate() const { return aggregate; }
307 void set_aggregate( Expression * newValue ) { aggregate = newValue; }
308
309 virtual MemberExpr * clone() const override { return new MemberExpr( * this ); }
310 virtual void accept( Visitor & v ) override { v.visit( this ); }
311 virtual void accept( Visitor & v ) const override { v.visit( this ); }
312 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
313 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
314};
315
316/// VariableExpr represents an expression that simply refers to the value of a named variable.
317/// Does not take ownership of var.
318class VariableExpr : public Expression {
319 public:
320 DeclarationWithType * var;
321
322 VariableExpr();
323 VariableExpr( DeclarationWithType * var );
324 VariableExpr( const VariableExpr & other );
325 virtual ~VariableExpr();
326
327 bool get_lvalue() const final;
328
329 DeclarationWithType * get_var() const { return var; }
330 void set_var( DeclarationWithType * newValue ) { var = newValue; }
331
332 static VariableExpr * functionPointer( FunctionDecl * decl );
333
334 virtual VariableExpr * clone() const override { return new VariableExpr( * this ); }
335 virtual void accept( Visitor & v ) override { v.visit( this ); }
336 virtual void accept( Visitor & v ) const override { v.visit( this ); }
337 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
338 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
339};
340
341/// ConstantExpr represents an expression that simply refers to the value of a constant
342class ConstantExpr : public Expression {
343 public:
344 Constant constant;
345
346 ConstantExpr( Constant constant );
347 ConstantExpr( const ConstantExpr & other );
348 virtual ~ConstantExpr();
349
350 Constant * get_constant() { return & constant; }
351 const Constant * get_constant() const { return & constant; }
352 void set_constant( const Constant & newValue ) { constant = newValue; }
353
354 long long int intValue() const;
355
356 virtual ConstantExpr * clone() const override { return new ConstantExpr( * this ); }
357 virtual void accept( Visitor & v ) override { v.visit( this ); }
358 virtual void accept( Visitor & v ) const override { v.visit( this ); }
359 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
360 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
361};
362
363/// SizeofExpr represents a sizeof expression (could be sizeof(int) or sizeof 3+4)
364class SizeofExpr : public Expression {
365 public:
366 Expression * expr;
367 Type * type;
368 bool isType;
369
370 SizeofExpr( Expression * expr );
371 SizeofExpr( const SizeofExpr & other );
372 SizeofExpr( Type * type );
373 virtual ~SizeofExpr();
374
375 Expression * get_expr() const { return expr; }
376 void set_expr( Expression * newValue ) { expr = newValue; }
377 Type * get_type() const { return type; }
378 void set_type( Type * newValue ) { type = newValue; }
379 bool get_isType() const { return isType; }
380 void set_isType( bool newValue ) { isType = newValue; }
381
382 virtual SizeofExpr * clone() const override { return new SizeofExpr( * this ); }
383 virtual void accept( Visitor & v ) override { v.visit( this ); }
384 virtual void accept( Visitor & v ) const override { v.visit( this ); }
385 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
386 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
387};
388
389/// AlignofExpr represents an alignof expression
390class AlignofExpr : public Expression {
391 public:
392 Expression * expr;
393 Type * type;
394 bool isType;
395
396 AlignofExpr( Expression * expr );
397 AlignofExpr( const AlignofExpr & other );
398 AlignofExpr( Type * type );
399 virtual ~AlignofExpr();
400
401 Expression * get_expr() const { return expr; }
402 void set_expr( Expression * newValue ) { expr = newValue; }
403 Type * get_type() const { return type; }
404 void set_type( Type * newValue ) { type = newValue; }
405 bool get_isType() const { return isType; }
406 void set_isType( bool newValue ) { isType = newValue; }
407
408 virtual AlignofExpr * clone() const override { return new AlignofExpr( * this ); }
409 virtual void accept( Visitor & v ) override { v.visit( this ); }
410 virtual void accept( Visitor & v ) const override { v.visit( this ); }
411 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
412 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
413};
414
415/// UntypedOffsetofExpr represents an offsetof expression before resolution
416class UntypedOffsetofExpr : public Expression {
417 public:
418 Type * type;
419 std::string member;
420
421 UntypedOffsetofExpr( Type * type, const std::string & member );
422 UntypedOffsetofExpr( const UntypedOffsetofExpr & other );
423 virtual ~UntypedOffsetofExpr();
424
425 std::string get_member() const { return member; }
426 void set_member( const std::string & newValue ) { member = newValue; }
427 Type * get_type() const { return type; }
428 void set_type( Type * newValue ) { type = newValue; }
429
430 virtual UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr( * this ); }
431 virtual void accept( Visitor & v ) override { v.visit( this ); }
432 virtual void accept( Visitor & v ) const override { v.visit( this ); }
433 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
434 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
435};
436
437/// OffsetofExpr represents an offsetof expression
438class OffsetofExpr : public Expression {
439 public:
440 Type * type;
441 DeclarationWithType * member;
442
443 OffsetofExpr( Type * type, DeclarationWithType * member );
444 OffsetofExpr( const OffsetofExpr & other );
445 virtual ~OffsetofExpr();
446
447 Type * get_type() const { return type; }
448 void set_type( Type * newValue ) { type = newValue; }
449 DeclarationWithType * get_member() const { return member; }
450 void set_member( DeclarationWithType * newValue ) { member = newValue; }
451
452 virtual OffsetofExpr * clone() const override { return new OffsetofExpr( * this ); }
453 virtual void accept( Visitor & v ) override { v.visit( this ); }
454 virtual void accept( Visitor & v ) const override { v.visit( this ); }
455 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
456 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
457};
458
459/// Expression representing a pack of field-offsets for a generic type
460class OffsetPackExpr : public Expression {
461public:
462 StructInstType * type;
463
464 OffsetPackExpr( StructInstType * type );
465 OffsetPackExpr( const OffsetPackExpr & other );
466 virtual ~OffsetPackExpr();
467
468 StructInstType * get_type() const { return type; }
469 void set_type( StructInstType * newValue ) { type = newValue; }
470
471 virtual OffsetPackExpr * clone() const override { return new OffsetPackExpr( * this ); }
472 virtual void accept( Visitor & v ) override { v.visit( this ); }
473 virtual void accept( Visitor & v ) const override { v.visit( this ); }
474 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
475 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
476};
477
478/// LogicalExpr represents a short-circuit boolean expression (&& or ||)
479class LogicalExpr : public Expression {
480 public:
481 Expression * arg1;
482 Expression * arg2;
483
484 LogicalExpr( Expression * arg1, Expression * arg2, bool andp = true );
485 LogicalExpr( const LogicalExpr & other );
486 virtual ~LogicalExpr();
487
488 bool get_isAnd() const { return isAnd; }
489 Expression * get_arg1() { return arg1; }
490 void set_arg1( Expression * newValue ) { arg1 = newValue; }
491 Expression * get_arg2() const { return arg2; }
492 void set_arg2( Expression * newValue ) { arg2 = newValue; }
493
494 virtual LogicalExpr * clone() const override { return new LogicalExpr( * this ); }
495 virtual void accept( Visitor & v ) override { v.visit( this ); }
496 virtual void accept( Visitor & v ) const override { v.visit( this ); }
497 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
498 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
499
500 private:
501 bool isAnd;
502};
503
504/// ConditionalExpr represents the three-argument conditional ( p ? a : b )
505class ConditionalExpr : public Expression {
506 public:
507 Expression * arg1;
508 Expression * arg2;
509 Expression * arg3;
510
511 ConditionalExpr( Expression * arg1, Expression * arg2, Expression * arg3 );
512 ConditionalExpr( const ConditionalExpr & other );
513 virtual ~ConditionalExpr();
514
515 bool get_lvalue() const final;
516
517 Expression * get_arg1() const { return arg1; }
518 void set_arg1( Expression * newValue ) { arg1 = newValue; }
519 Expression * get_arg2() const { return arg2; }
520 void set_arg2( Expression * newValue ) { arg2 = newValue; }
521 Expression * get_arg3() const { return arg3; }
522 void set_arg3( Expression * newValue ) { arg3 = newValue; }
523
524 virtual ConditionalExpr * clone() const override { return new ConditionalExpr( * this ); }
525 virtual void accept( Visitor & v ) override { v.visit( this ); }
526 virtual void accept( Visitor & v ) const override { v.visit( this ); }
527 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
528 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
529};
530
531/// CommaExpr represents the sequence operator ( a, b )
532class CommaExpr : public Expression {
533 public:
534 Expression * arg1;
535 Expression * arg2;
536
537 CommaExpr( Expression * arg1, Expression * arg2 );
538 CommaExpr( const CommaExpr & other );
539 virtual ~CommaExpr();
540
541 bool get_lvalue() const final;
542
543 Expression * get_arg1() const { return arg1; }
544 void set_arg1( Expression * newValue ) { arg1 = newValue; }
545 Expression * get_arg2() const { return arg2; }
546 void set_arg2( Expression * newValue ) { arg2 = newValue; }
547
548 virtual CommaExpr * clone() const override { return new CommaExpr( * this ); }
549 virtual void accept( Visitor & v ) override { v.visit( this ); }
550 virtual void accept( Visitor & v ) const override { v.visit( this ); }
551 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
552 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
553};
554
555/// TypeExpr represents a type used in an expression (e.g. as a type generator parameter)
556class TypeExpr : public Expression {
557 public:
558 Type * type;
559
560 TypeExpr( Type * type );
561 TypeExpr( const TypeExpr & other );
562 virtual ~TypeExpr();
563
564 Type * get_type() const { return type; }
565 void set_type( Type * newValue ) { type = newValue; }
566
567 virtual TypeExpr * clone() const override { return new TypeExpr( * this ); }
568 virtual void accept( Visitor & v ) override { v.visit( this ); }
569 virtual void accept( Visitor & v ) const override { v.visit( this ); }
570 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
571 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
572};
573
574/// AsmExpr represents a GCC 'asm constraint operand' used in an asm statement: [output] "=f" (result)
575class AsmExpr : public Expression {
576 public:
577 std::string inout;
578 Expression * constraint;
579 Expression * operand;
580
581 AsmExpr( const std::string * _inout, Expression * constraint, Expression * operand ) : inout( _inout ? *_inout : "" ), constraint( constraint ), operand( operand ) { delete _inout; }
582 AsmExpr( const AsmExpr & other );
583 virtual ~AsmExpr() { delete constraint; delete operand; };
584
585 virtual AsmExpr * clone() const override { return new AsmExpr( * this ); }
586 virtual void accept( Visitor & v ) override { v.visit( this ); }
587 virtual void accept( Visitor & v ) const override { v.visit( this ); }
588 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
589 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
590
591 // https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
592};
593
594/// ImplicitCopyCtorExpr represents the application of a function to a set of parameters,
595/// along with a set of copy constructor calls, one for each argument.
596class ImplicitCopyCtorExpr : public Expression {
597public:
598 ApplicationExpr * callExpr = nullptr;
599
600 ImplicitCopyCtorExpr( ApplicationExpr * callExpr );
601 ImplicitCopyCtorExpr( const ImplicitCopyCtorExpr & other );
602 virtual ~ImplicitCopyCtorExpr();
603
604 virtual ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr( * this ); }
605 virtual void accept( Visitor & v ) override { v.visit( this ); }
606 virtual void accept( Visitor & v ) const override { v.visit( this ); }
607 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
608 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
609};
610
611/// ConstructorExpr represents the use of a constructor in an expression context, e.g. int * x = malloc() { 5 };
612class ConstructorExpr : public Expression {
613public:
614 Expression * callExpr;
615
616 ConstructorExpr( Expression * callExpr );
617 ConstructorExpr( const ConstructorExpr & other );
618 ~ConstructorExpr();
619
620 bool get_lvalue() const final;
621
622 Expression * get_callExpr() const { return callExpr; }
623 void set_callExpr( Expression * newValue ) { callExpr = newValue; }
624
625 virtual ConstructorExpr * clone() const override { return new ConstructorExpr( * this ); }
626 virtual void accept( Visitor & v ) override { v.visit( this ); }
627 virtual void accept( Visitor & v ) const override { v.visit( this ); }
628 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
629 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
630};
631
632/// CompoundLiteralExpr represents a C99 'compound literal'
633class CompoundLiteralExpr : public Expression {
634 public:
635 Initializer * initializer;
636
637 CompoundLiteralExpr( Type * type, Initializer * initializer );
638 CompoundLiteralExpr( const CompoundLiteralExpr & other );
639 virtual ~CompoundLiteralExpr();
640
641 bool get_lvalue() const final;
642
643 Initializer * get_initializer() const { return initializer; }
644 void set_initializer( Initializer * i ) { initializer = i; }
645
646 virtual CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr( * this ); }
647 virtual void accept( Visitor & v ) override { v.visit( this ); }
648 virtual void accept( Visitor & v ) const override { v.visit( this ); }
649 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
650 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
651};
652
653/// RangeExpr represents a range e.g. '3 ... 5' or '1~10'
654class RangeExpr : public Expression {
655 public:
656 Expression * low, * high;
657
658 RangeExpr( Expression * low, Expression * high );
659 RangeExpr( const RangeExpr & other );
660
661 Expression * get_low() const { return low; }
662 Expression * get_high() const { return high; }
663 RangeExpr * set_low( Expression * low ) { RangeExpr::low = low; return this; }
664 RangeExpr * set_high( Expression * high ) { RangeExpr::high = high; return this; }
665
666 virtual RangeExpr * clone() const override { return new RangeExpr( * this ); }
667 virtual void accept( Visitor & v ) override { v.visit( this ); }
668 virtual void accept( Visitor & v ) const override { v.visit( this ); }
669 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
670 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
671};
672
673/// UntypedTupleExpr represents a tuple expression ( [a, b, c] ) before resolution
674class UntypedTupleExpr : public Expression {
675 public:
676 std::list<Expression*> exprs;
677
678 UntypedTupleExpr( const std::list< Expression * > & exprs );
679 UntypedTupleExpr( const UntypedTupleExpr & other );
680 virtual ~UntypedTupleExpr();
681
682 std::list<Expression*>& get_exprs() { return exprs; }
683
684 virtual UntypedTupleExpr * clone() const override { return new UntypedTupleExpr( * this ); }
685 virtual void accept( Visitor & v ) override { v.visit( this ); }
686 virtual void accept( Visitor & v ) const override { v.visit( this ); }
687 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
688 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
689};
690
691/// TupleExpr represents a tuple expression ( [a, b, c] )
692class TupleExpr : public Expression {
693 public:
694 std::list<Expression*> exprs;
695
696 TupleExpr( const std::list< Expression * > & exprs );
697 TupleExpr( const TupleExpr & other );
698 virtual ~TupleExpr();
699
700 bool get_lvalue() const final;
701
702 std::list<Expression*>& get_exprs() { return exprs; }
703
704 virtual TupleExpr * clone() const override { return new TupleExpr( * this ); }
705 virtual void accept( Visitor & v ) override { v.visit( this ); }
706 virtual void accept( Visitor & v ) const override { v.visit( this ); }
707 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
708 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
709};
710
711/// TupleIndexExpr represents an element selection operation on a tuple value, e.g. t.3 after processing by the expression analyzer
712class TupleIndexExpr : public Expression {
713 public:
714 Expression * tuple;
715 unsigned int index;
716
717 TupleIndexExpr( Expression * tuple, unsigned int index );
718 TupleIndexExpr( const TupleIndexExpr & other );
719 virtual ~TupleIndexExpr();
720
721 bool get_lvalue() const final;
722
723 Expression * get_tuple() const { return tuple; }
724 int get_index() const { return index; }
725 TupleIndexExpr * set_tuple( Expression * newValue ) { tuple = newValue; return this; }
726 TupleIndexExpr * set_index( unsigned int newValue ) { index = newValue; return this; }
727
728 virtual TupleIndexExpr * clone() const override { return new TupleIndexExpr( * this ); }
729 virtual void accept( Visitor & v ) override { v.visit( this ); }
730 virtual void accept( Visitor & v ) const override { v.visit( this ); }
731 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
732 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
733};
734
735/// 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
736class TupleAssignExpr : public Expression {
737 public:
738 StmtExpr * stmtExpr = nullptr;
739
740 TupleAssignExpr( const std::list< Expression * > & assigns, const std::list< ObjectDecl * > & tempDecls );
741 TupleAssignExpr( const TupleAssignExpr & other );
742 virtual ~TupleAssignExpr();
743
744 TupleAssignExpr * set_stmtExpr( StmtExpr * newValue ) { stmtExpr = newValue; return this; }
745 StmtExpr * get_stmtExpr() const { return stmtExpr; }
746
747 virtual TupleAssignExpr * clone() const override { return new TupleAssignExpr( * this ); }
748 virtual void accept( Visitor & v ) override { v.visit( this ); }
749 virtual void accept( Visitor & v ) const override { v.visit( this ); }
750 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
751 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
752
753 friend class ConverterNewToOld;
754 private:
755 TupleAssignExpr( StmtExpr * stmts );
756};
757
758/// StmtExpr represents a GCC 'statement expression', e.g. ({ int x = 5; x; })
759class StmtExpr : public Expression {
760public:
761 CompoundStmt * statements;
762 std::list< ObjectDecl * > returnDecls; // return variable(s) for stmt expression
763 std::list< Expression * > dtors; // destructor(s) for return variable(s)
764
765 // readonly
766 ExprStmt * resultExpr = nullptr;
767
768 StmtExpr( CompoundStmt * statements );
769 StmtExpr( const StmtExpr & other );
770 virtual ~StmtExpr();
771
772 bool get_lvalue() const final;
773
774 CompoundStmt * get_statements() const { return statements; }
775 StmtExpr * set_statements( CompoundStmt * newValue ) { statements = newValue; return this; }
776
777 // call to set the result type of this StmtExpr based on its body
778 void computeResult();
779
780 std::list< ObjectDecl * > & get_returnDecls() { return returnDecls; }
781 std::list< Expression * > & get_dtors() { return dtors; }
782
783 virtual StmtExpr * clone() const override { return new StmtExpr( * this ); }
784 virtual void accept( Visitor & v ) override { v.visit( this ); }
785 virtual void accept( Visitor & v ) const override { v.visit( this ); }
786 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
787 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
788};
789
790class UniqueExpr : public Expression {
791public:
792 Expression * expr;
793 ObjectDecl * object;
794 VariableExpr * var;
795
796 UniqueExpr( Expression * expr, long long idVal = -1 );
797 UniqueExpr( const UniqueExpr & other );
798 ~UniqueExpr();
799
800 Expression * get_expr() const { return expr; }
801 UniqueExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }
802
803 ObjectDecl * get_object() const { return object; }
804 UniqueExpr * set_object( ObjectDecl * newValue ) { object = newValue; return this; }
805
806 VariableExpr * get_var() const { return var; }
807 UniqueExpr * set_var( VariableExpr * newValue ) { var = newValue; return this; }
808
809 int get_id() const { return id; }
810
811 virtual UniqueExpr * clone() const override { return new UniqueExpr( * this ); }
812 virtual void accept( Visitor & v ) override { v.visit( this ); }
813 virtual void accept( Visitor & v ) const override { v.visit( this ); }
814 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
815 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
816
817private:
818 int id;
819 static long long count;
820};
821
822struct InitAlternative {
823public:
824 Type * type = nullptr;
825 Designation * designation = nullptr;
826 InitAlternative( Type * type, Designation * designation );
827 InitAlternative( const InitAlternative & other );
828 InitAlternative & operator=( const Initializer & other ) = delete; // at the moment this isn't used, and I don't want to implement it
829 ~InitAlternative();
830};
831
832class UntypedInitExpr : public Expression {
833public:
834 Expression * expr;
835 std::list<InitAlternative> initAlts;
836
837 UntypedInitExpr( Expression * expr, const std::list<InitAlternative> & initAlts );
838 UntypedInitExpr( const UntypedInitExpr & other );
839 ~UntypedInitExpr();
840
841 Expression * get_expr() const { return expr; }
842 UntypedInitExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }
843
844 std::list<InitAlternative> & get_initAlts() { return initAlts; }
845
846 virtual UntypedInitExpr * clone() const override { return new UntypedInitExpr( * this ); }
847 virtual void accept( Visitor & v ) override { v.visit( this ); }
848 virtual void accept( Visitor & v ) const override { v.visit( this ); }
849 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
850 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
851};
852
853class InitExpr : public Expression {
854public:
855 Expression * expr;
856 Designation * designation;
857
858 InitExpr( Expression * expr, Designation * designation );
859 InitExpr( const InitExpr & other );
860 ~InitExpr();
861
862 Expression * get_expr() const { return expr; }
863 InitExpr * set_expr( Expression * newValue ) { expr = newValue; return this; }
864
865 Designation * get_designation() const { return designation; }
866 InitExpr * set_designation( Designation * newValue ) { designation = newValue; return this; }
867
868 virtual InitExpr * clone() const override { return new InitExpr( * this ); }
869 virtual void accept( Visitor & v ) override { v.visit( this ); }
870 virtual void accept( Visitor & v ) const override { v.visit( this ); }
871 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
872 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
873};
874
875/// expression that contains a deleted identifier - should never make it past the resolver.
876class DeletedExpr : public Expression {
877public:
878 Expression * expr;
879 Declaration * deleteStmt;
880
881 DeletedExpr( Expression * expr, Declaration * deleteStmt );
882 DeletedExpr( const DeletedExpr & other );
883 ~DeletedExpr();
884
885 virtual DeletedExpr * clone() const override { return new DeletedExpr( * this ); }
886 virtual void accept( Visitor & v ) override { v.visit( this ); }
887 virtual void accept( Visitor & v ) const override { v.visit( this ); }
888 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
889 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
890};
891
892/// expression wrapping the use of a default argument - should never make it past the resolver.
893class DefaultArgExpr : public Expression {
894public:
895 Expression * expr;
896
897 DefaultArgExpr( Expression * expr );
898 DefaultArgExpr( const DefaultArgExpr & other );
899 ~DefaultArgExpr();
900
901 virtual DefaultArgExpr * clone() const override { return new DefaultArgExpr( * this ); }
902 virtual void accept( Visitor & v ) override { v.visit( this ); }
903 virtual void accept( Visitor & v ) const override { v.visit( this ); }
904 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
905 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
906};
907
908/// C11 _Generic expression
909class GenericExpr : public Expression {
910public:
911 struct Association {
912 Type * type = nullptr;
913 Expression * expr = nullptr;
914 bool isDefault = false;
915
916 Association( Type * type, Expression * expr );
917 Association( Expression * expr );
918 Association( const Association & other );
919 Association & operator=( const Association & other ) = delete; // at the moment this isn't used, and I don't want to implement it
920 ~Association();
921 };
922
923 Expression * control;
924 std::list<Association> associations;
925
926 GenericExpr( Expression * control, const std::list<Association> & assoc );
927 GenericExpr( const GenericExpr & other );
928 virtual ~GenericExpr();
929
930 virtual GenericExpr * clone() const override { return new GenericExpr( * this ); }
931 virtual void accept( Visitor & v ) override { v.visit( this ); }
932 virtual void accept( Visitor & v ) const override { v.visit( this ); }
933 virtual Expression * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
934 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
935};
936
937// Local Variables: //
938// tab-width: 4 //
939// mode: c++ //
940// compile-command: "make install" //
941// End: //
Note: See TracBrowser for help on using the repository browser.