source: src/SynTree/Expression.h @ 6f096d2

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 6f096d2 was 7870799, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Cast cost and conversion cost now take constant parameters.
This required supporting visiting const node.
The PassVisitor? can now visit const nodes but not when using the Indexer

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