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