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