source: src/SynTree/Expression.h@ 00b046f

Last change on this file since 00b046f was 46da46b, checked in by Fangren Yu <f37yu@…>, 2 years ago

current progress

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