source: src/AST/Init.hpp@ f2e482cb

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f2e482cb was 89c2f7c9, checked in by Aaron Moss <a3moss@…>, 7 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

  • Property mode set to 100644
File size: 5.3 KB
Line 
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
25namespace ast {
26
27class Expr;
28class Stmt;
29
30/// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an
31/// object being initialized
32class Designation final : public ParseNode {
33public:
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 const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
40private:
41 Designation* clone() const override { return new Designation{ *this }; }
42};
43
44/// Flag for whether to construct from initialzier
45enum ConstructFlag { DoConstruct, MaybeConstruct };
46
47/// Object initializer base class
48class Init : public ParseNode {
49public:
50 ConstructFlag maybeConstructed;
51
52 Init( const CodeLocation& loc, ConstructFlag mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
53
54 const Init * accept( Visitor& v ) const override = 0;
55private:
56 const Init * clone() const override = 0;
57};
58
59/// Initializer for a common object: `int x = 4`
60class SingleInit final : public Init {
61public:
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 const Init * accept( Visitor & v ) const override { return v.visit( this ); }
69private:
70 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 }`
79class ListInit final : public Init {
80public:
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 const Init * accept( Visitor & v ) const override { return v.visit( this ); }
97private:
98 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.
108class ConstructorInit final : public Init {
109public:
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 const Init * accept( Visitor & v ) const override { return v.visit( this ); }
120private:
121 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
134inline void increment( const class Init * node, Node::ref_type ref ) { node->increment( ref ); }
135inline void decrement( const class Init * node, Node::ref_type ref ) { node->decrement( ref ); }
136inline void increment( const class SingleInit * node, Node::ref_type ref ) { node->increment( ref ); }
137inline void decrement( const class SingleInit * node, Node::ref_type ref ) { node->decrement( ref ); }
138inline void increment( const class ListInit * node, Node::ref_type ref ) { node->increment( ref ); }
139inline void decrement( const class ListInit * node, Node::ref_type ref ) { node->decrement( ref ); }
140inline void increment( const class ConstructorInit * node, Node::ref_type ref ) { node->increment( ref ); }
141inline 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: //
Note: See TracBrowser for help on using the repository browser.