source: src/SynTree/Expression.h@ 50a8aa9

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 50a8aa9 was bbf17d5, checked in by JiadaL <j82liang@…>, 3 years ago

Basic Defining of QualifiedNameExpr; save for debugging

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