[9131e54] | 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 | // Init.hpp --
|
---|
| 8 | //
|
---|
| 9 | // Author : Aaron B. Moss
|
---|
| 10 | // Created On : Fri May 10 10:30:00 2019
|
---|
| 11 | // Last Modified By : Aaron B. Moss
|
---|
| 12 | // Created On : Fri May 10 10:30:00 2019
|
---|
| 13 | // Update Count : 1
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | #include <utility> // for move
|
---|
| 19 | #include <vector>
|
---|
| 20 |
|
---|
| 21 | #include "ParseNode.hpp"
|
---|
| 22 | #include "Node.hpp" // for ptr
|
---|
| 23 | #include "Visitor.hpp"
|
---|
| 24 |
|
---|
[f3cc5b6] | 25 | // Must be included in *all* AST classes; should be #undef'd at the end of the file
|
---|
[41b24c8] | 26 | #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node);
|
---|
[f3cc5b6] | 27 |
|
---|
[9131e54] | 28 | namespace ast {
|
---|
| 29 |
|
---|
| 30 | class Expr;
|
---|
| 31 | class Stmt;
|
---|
| 32 |
|
---|
[e0115286] | 33 | /// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an
|
---|
[9131e54] | 34 | /// object being initialized
|
---|
| 35 | class Designation final : public ParseNode {
|
---|
| 36 | public:
|
---|
| 37 | std::vector<ptr<Expr>> designators;
|
---|
| 38 |
|
---|
[e0115286] | 39 | Designation( const CodeLocation& loc, std::vector<ptr<Expr>>&& ds = {} )
|
---|
[9131e54] | 40 | : ParseNode( loc ), designators( std::move(ds) ) {}
|
---|
| 41 |
|
---|
[23f99e1] | 42 | const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
|
---|
[9131e54] | 43 | private:
|
---|
[23f99e1] | 44 | Designation* clone() const override { return new Designation{ *this }; }
|
---|
[f3cc5b6] | 45 | MUTATE_FRIEND
|
---|
[9131e54] | 46 | };
|
---|
| 47 |
|
---|
[9e1d485] | 48 | /// Flag for whether to construct from initialzier
|
---|
| 49 | enum ConstructFlag { DoConstruct, MaybeConstruct };
|
---|
| 50 |
|
---|
[9131e54] | 51 | /// Object initializer base class
|
---|
| 52 | class Init : public ParseNode {
|
---|
| 53 | public:
|
---|
[9e1d485] | 54 | ConstructFlag maybeConstructed;
|
---|
[9131e54] | 55 |
|
---|
[9e1d485] | 56 | Init( const CodeLocation& loc, ConstructFlag mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
|
---|
[9131e54] | 57 |
|
---|
[23f99e1] | 58 | const Init * accept( Visitor& v ) const override = 0;
|
---|
[9131e54] | 59 | private:
|
---|
[87701b6] | 60 | Init * clone() const override = 0;
|
---|
[f3cc5b6] | 61 | MUTATE_FRIEND
|
---|
[9131e54] | 62 | };
|
---|
| 63 |
|
---|
| 64 | /// Initializer for a common object: `int x = 4`
|
---|
| 65 | class SingleInit final : public Init {
|
---|
| 66 | public:
|
---|
| 67 | /// value to initialize to. Must be compile-time constant.
|
---|
| 68 | ptr<Expr> value;
|
---|
| 69 |
|
---|
[9e1d485] | 70 | SingleInit( const CodeLocation& loc, Expr* val, ConstructFlag mc = DoConstruct )
|
---|
[9131e54] | 71 | : Init( loc, mc ), value( val ) {}
|
---|
| 72 |
|
---|
[23f99e1] | 73 | const Init * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[9131e54] | 74 | private:
|
---|
[23f99e1] | 75 | SingleInit * clone() const override { return new SingleInit{ *this }; }
|
---|
[f3cc5b6] | 76 | MUTATE_FRIEND
|
---|
[9131e54] | 77 | };
|
---|
| 78 |
|
---|
| 79 | /// Initializer recursively composed of a list of initializers.
|
---|
| 80 | /// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }`
|
---|
| 81 | class ListInit final : public Init {
|
---|
| 82 | public:
|
---|
| 83 | /// list of initializers
|
---|
| 84 | std::vector<ptr<Init>> initializers;
|
---|
| 85 | /// list of designators; order/length is consistent with initializers
|
---|
| 86 | std::vector<ptr<Designation>> designations;
|
---|
| 87 |
|
---|
[e0115286] | 88 | ListInit( const CodeLocation& loc, std::vector<ptr<Init>>&& is,
|
---|
[9e1d485] | 89 | std::vector<ptr<Designation>>&& ds = {}, ConstructFlag mc = DoConstruct );
|
---|
[e0115286] | 90 |
|
---|
[9131e54] | 91 | using iterator = std::vector<ptr<Init>>::iterator;
|
---|
| 92 | using const_iterator = std::vector<ptr<Init>>::const_iterator;
|
---|
| 93 | iterator begin() { return initializers.begin(); }
|
---|
| 94 | iterator end() { return initializers.end(); }
|
---|
| 95 | const_iterator begin() const { return initializers.begin(); }
|
---|
| 96 | const_iterator end() const { return initializers.end(); }
|
---|
| 97 |
|
---|
[23f99e1] | 98 | const Init * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[9131e54] | 99 | private:
|
---|
[23f99e1] | 100 | ListInit * clone() const override { return new ListInit{ *this }; }
|
---|
[f3cc5b6] | 101 | MUTATE_FRIEND
|
---|
[9131e54] | 102 | };
|
---|
| 103 |
|
---|
| 104 | /// Either a constructor expression or a C-style initializer.
|
---|
[e0115286] | 105 | /// Should not be necessary to create manually; instead set `maybeConstructed` true on `SingleInit`
|
---|
[9131e54] | 106 | /// or `ListInit` if the object should be constructed.
|
---|
| 107 | class ConstructorInit final : public Init {
|
---|
| 108 | public:
|
---|
| 109 | ptr<Stmt> ctor;
|
---|
| 110 | ptr<Stmt> dtor;
|
---|
[e0115286] | 111 | /// C-style initializer made up of SingleInit/ListInit nodes to use as a fallback if an
|
---|
[9131e54] | 112 | /// appropriate constructor definition is not found by the resolver.
|
---|
| 113 | ptr<Init> init;
|
---|
| 114 |
|
---|
| 115 | ConstructorInit( const CodeLocation& loc, Stmt* ctor, Stmt* dtor, Init* init )
|
---|
[9e1d485] | 116 | : Init( loc, DoConstruct ), ctor( ctor ), dtor( dtor ), init( init ) {}
|
---|
[9131e54] | 117 |
|
---|
[23f99e1] | 118 | const Init * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[9131e54] | 119 | private:
|
---|
[23f99e1] | 120 | ConstructorInit * clone() const override { return new ConstructorInit{ *this }; }
|
---|
[f3cc5b6] | 121 | MUTATE_FRIEND
|
---|
[9131e54] | 122 | };
|
---|
| 123 |
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[f3cc5b6] | 126 | #undef MUTATE_FRIEND
|
---|
| 127 |
|
---|
[9131e54] | 128 | // Local Variables: //
|
---|
| 129 | // tab-width: 4 //
|
---|
| 130 | // mode: c++ //
|
---|
| 131 | // compile-command: "make install" //
|
---|
| 132 | // End: //
|
---|