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
RevLine 
[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
25namespace ast {
26
27class Expr;
28class Stmt;
29
[e0115286]30/// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an
[9131e54]31/// object being initialized
32class Designation final : public ParseNode {
33public:
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
[23f99e1]39 const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
[9131e54]40private:
[23f99e1]41 Designation* clone() const override { return new Designation{ *this }; }
[9131e54]42};
43
[9e1d485]44/// Flag for whether to construct from initialzier
45enum ConstructFlag { DoConstruct, MaybeConstruct };
46
[9131e54]47/// Object initializer base class
48class Init : public ParseNode {
49public:
[9e1d485]50 ConstructFlag maybeConstructed;
[9131e54]51
[9e1d485]52 Init( const CodeLocation& loc, ConstructFlag mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
[9131e54]53
[23f99e1]54 const Init * accept( Visitor& v ) const override = 0;
[9131e54]55private:
[23f99e1]56 const Init * clone() const override = 0;
[9131e54]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
[9e1d485]65 SingleInit( const CodeLocation& loc, Expr* val, ConstructFlag mc = DoConstruct )
[9131e54]66 : Init( loc, mc ), value( val ) {}
67
[23f99e1]68 const Init * accept( Visitor & v ) const override { return v.visit( this ); }
[9131e54]69private:
[23f99e1]70 SingleInit * clone() const override { return new SingleInit{ *this }; }
[6d51bd7]71
72 /// Must be copied in ALL derived classes
73 template<typename node_t>
74 friend auto mutate(const node_t * node);
[9131e54]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
[e0115286]86 ListInit( const CodeLocation& loc, std::vector<ptr<Init>>&& is,
[9e1d485]87 std::vector<ptr<Designation>>&& ds = {}, ConstructFlag mc = DoConstruct );
[e0115286]88
[9131e54]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
[23f99e1]96 const Init * accept( Visitor & v ) const override { return v.visit( this ); }
[9131e54]97private:
[23f99e1]98 ListInit * clone() const override { return new ListInit{ *this }; }
[6d51bd7]99
100 /// Must be copied in ALL derived classes
101 template<typename node_t>
102 friend auto mutate(const node_t * node);
[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.
108class ConstructorInit final : public Init {
109public:
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
116 ConstructorInit( const CodeLocation& loc, Stmt* ctor, Stmt* dtor, Init* init )
[9e1d485]117 : Init( loc, DoConstruct ), ctor( ctor ), dtor( dtor ), init( init ) {}
[9131e54]118
[23f99e1]119 const Init * accept( Visitor & v ) const override { return v.visit( this ); }
[9131e54]120private:
[23f99e1]121 ConstructorInit * clone() const override { return new ConstructorInit{ *this }; }
[6d51bd7]122
123 /// Must be copied in ALL derived classes
124 template<typename node_t>
125 friend auto mutate(const node_t * node);
[9131e54]126};
127
[e0115286]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 ); }
[9131e54]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.