source: src/SynTree/Expression.h @ b81fd95

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since b81fd95 was b81fd95, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Fix bug where pointer and reference types allow unsound initialization and return. Fixes #189

There are two instances of the same basic change, which is using conversionCost instead of castCost for resolving...
A: an InitExpr?, always; affects variable initializations
B: a CastExpr?, for type-system-generated casts only; affects function returns

Changing the behaviour of the typechecker on initialization (do A) and cast (do B):
src/ResolvExpr/AlternativeFinder.cc
src/SynTree/Expression.h
testsinit1.*

Making type of string literal consistent with how C defines it (accommodate A):
src/Parser/ExpressionNode.cc

Making type system happy with incumbent use of void* (accommodate A):
libcfa/src/concurrency/kernel.cfa
libcfa/src/containers/list.hfa
tests/bugs/66.cfa
tests/avltree/avl1.cfa
tests/concurrent/signal/block.cfa
tests/searchsort.cfa

Making type system happy with incumbent plan-9 downcast (accommodate B):
libcfa/src/containers/list.hfa

Fixing previously incorrect constness of declarations (accommodate A):
tests/exceptions/defaults.cfa
libcfa/src/iostream.hfa

Fixing previously incorrect isGenerated classification of casts that desugaring introduces (accommodate B):
src/Concurrency/Keywords.cc
src/Concurrency/Waitfor.cc

Working around trac #207 (revealed by A):
tests/io2.cfa

Working around trac #208 (speculatively created by B):
libcfa/src/bits/locks.hfa
libcfa/src/concurrency/preemption.cfa

Misc:
tests/exceptions/conditional.cfa (accommodate A)

a _msg function for an exception was declared with wrong return type, so it was not compatible for assignment into the vtable instance

libcfa/src/stdlib.hfa

the compiler now prohibits a prior attempt to call a nonexistent realloc overload; calling alloc_align in its place

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