source: src/SynTree/Expression.h @ 3b0c8cb

ADTarm-ehenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3b0c8cb was 3b0c8cb, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Implemented Keyword cast in the alternative finder to remove aliassing problems

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