source: src/SynTree/Expression.h @ 9939dc3

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 9939dc3 was 6e50a6b, checked in by Michael Brooks <mlbrooks@…>, 3 years ago

Implementing language-provided syntax for (array) dimensions.

Former z(i) and Z(N) macros are eliminated.

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