source: src/SynTree/Expression.h@ 8a13c47

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 8a13c47 was 312029a, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

move enum Aggregate from DeclarationNode to AggregateDecl, add control-keyword field-dereference to replace control-keyword cast

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