[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 | |
---|
| 25 | namespace ast { |
---|
| 26 | |
---|
| 27 | class Expr; |
---|
| 28 | class Stmt; |
---|
| 29 | |
---|
[e0115286] | 30 | /// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an |
---|
[9131e54] | 31 | /// object being initialized |
---|
| 32 | class Designation final : public ParseNode { |
---|
| 33 | public: |
---|
| 34 | std::vector<ptr<Expr>> designators; |
---|
| 35 | |
---|
[e0115286] | 36 | Designation( const CodeLocation& loc, std::vector<ptr<Expr>>&& ds = {} ) |
---|
[9131e54] | 37 | : ParseNode( loc ), designators( std::move(ds) ) {} |
---|
| 38 | |
---|
| 39 | Designation* accept( Visitor& v ) override { return v.visit( this ); } |
---|
| 40 | private: |
---|
| 41 | Designation* clone() const override { return new Designation{ *this }; } |
---|
| 42 | }; |
---|
| 43 | |
---|
| 44 | /// Object initializer base class |
---|
| 45 | class Init : public ParseNode { |
---|
| 46 | public: |
---|
| 47 | bool maybeConstructed; |
---|
| 48 | |
---|
| 49 | Init( const CodeLocation& loc, bool mc ) : ParseNode( loc ), maybeConstructed( mc ) {} |
---|
| 50 | |
---|
| 51 | virtual Init* accept( Visitor& v ) override = 0; |
---|
| 52 | private: |
---|
| 53 | virtual Init* clone() const override = 0; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | /// Initializer for a common object: `int x = 4` |
---|
| 57 | class SingleInit final : public Init { |
---|
| 58 | public: |
---|
| 59 | /// value to initialize to. Must be compile-time constant. |
---|
| 60 | ptr<Expr> value; |
---|
| 61 | |
---|
[e0115286] | 62 | SingleInit( const CodeLocation& loc, Expr* val, bool mc = false ) |
---|
[9131e54] | 63 | : Init( loc, mc ), value( val ) {} |
---|
| 64 | |
---|
| 65 | Init* accept( Visitor& v ) override { return v.visit( this ); } |
---|
| 66 | private: |
---|
| 67 | SingleInit* clone() const override { return new SingleInit{ *this }; } |
---|
| 68 | }; |
---|
| 69 | |
---|
| 70 | /// Initializer recursively composed of a list of initializers. |
---|
| 71 | /// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }` |
---|
| 72 | class ListInit final : public Init { |
---|
| 73 | public: |
---|
| 74 | /// list of initializers |
---|
| 75 | std::vector<ptr<Init>> initializers; |
---|
| 76 | /// list of designators; order/length is consistent with initializers |
---|
| 77 | std::vector<ptr<Designation>> designations; |
---|
| 78 | |
---|
[e0115286] | 79 | ListInit( const CodeLocation& loc, std::vector<ptr<Init>>&& is, |
---|
[9131e54] | 80 | std::vector<ptr<Designation>>&& ds = {}, bool mc = false ); |
---|
[e0115286] | 81 | |
---|
[9131e54] | 82 | using iterator = std::vector<ptr<Init>>::iterator; |
---|
| 83 | using const_iterator = std::vector<ptr<Init>>::const_iterator; |
---|
| 84 | iterator begin() { return initializers.begin(); } |
---|
| 85 | iterator end() { return initializers.end(); } |
---|
| 86 | const_iterator begin() const { return initializers.begin(); } |
---|
| 87 | const_iterator end() const { return initializers.end(); } |
---|
| 88 | |
---|
| 89 | Init* accept( Visitor& v ) override { return v.visit( this ); } |
---|
| 90 | private: |
---|
| 91 | ListInit* clone() const override { return new ListInit{ *this }; } |
---|
| 92 | }; |
---|
| 93 | |
---|
| 94 | /// Either a constructor expression or a C-style initializer. |
---|
[e0115286] | 95 | /// Should not be necessary to create manually; instead set `maybeConstructed` true on `SingleInit` |
---|
[9131e54] | 96 | /// or `ListInit` if the object should be constructed. |
---|
| 97 | class ConstructorInit final : public Init { |
---|
| 98 | public: |
---|
| 99 | ptr<Stmt> ctor; |
---|
| 100 | ptr<Stmt> dtor; |
---|
[e0115286] | 101 | /// C-style initializer made up of SingleInit/ListInit nodes to use as a fallback if an |
---|
[9131e54] | 102 | /// appropriate constructor definition is not found by the resolver. |
---|
| 103 | ptr<Init> init; |
---|
| 104 | |
---|
| 105 | ConstructorInit( const CodeLocation& loc, Stmt* ctor, Stmt* dtor, Init* init ) |
---|
| 106 | : Init( loc, true ), ctor( ctor ), dtor( dtor ), init( init ) {} |
---|
| 107 | |
---|
| 108 | Init* accept( Visitor& v ) override { return v.visit( this ); } |
---|
| 109 | private: |
---|
| 110 | ConstructorInit* clone() const override { return new ConstructorInit{ *this }; } |
---|
| 111 | }; |
---|
| 112 | |
---|
[e0115286] | 113 | |
---|
| 114 | //================================================================================================= |
---|
| 115 | /// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency |
---|
| 116 | /// remove only if there is a better solution |
---|
| 117 | /// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with |
---|
| 118 | /// forward declarations |
---|
| 119 | inline void increment( const class Init * node, Node::ref_type ref ) { node->increment( ref ); } |
---|
| 120 | inline void decrement( const class Init * node, Node::ref_type ref ) { node->decrement( ref ); } |
---|
| 121 | inline void increment( const class SingleInit * node, Node::ref_type ref ) { node->increment( ref ); } |
---|
| 122 | inline void decrement( const class SingleInit * node, Node::ref_type ref ) { node->decrement( ref ); } |
---|
| 123 | inline void increment( const class ListInit * node, Node::ref_type ref ) { node->increment( ref ); } |
---|
| 124 | inline void decrement( const class ListInit * node, Node::ref_type ref ) { node->decrement( ref ); } |
---|
| 125 | inline void increment( const class ConstructorInit * node, Node::ref_type ref ) { node->increment( ref ); } |
---|
| 126 | inline void decrement( const class ConstructorInit * node, Node::ref_type ref ) { node->decrement( ref ); } |
---|
[9131e54] | 127 | } |
---|
| 128 | |
---|
| 129 | // Local Variables: // |
---|
| 130 | // tab-width: 4 // |
---|
| 131 | // mode: c++ // |
---|
| 132 | // compile-command: "make install" // |
---|
| 133 | // End: // |
---|