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