source: src/AST/Init.hpp @ a86b1b85

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a86b1b85 was 234b1cb, checked in by Aaron Moss <a3moss@…>, 5 years ago

Port TupleAssignment? to new AST

  • Property mode set to 100644
File size: 4.1 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 <deque>
19#include <utility>        // for move
20#include <vector>
21
22#include "ParseNode.hpp"
23#include "Node.hpp"       // for ptr
24#include "Visitor.hpp"
25
26// Must be included in *all* AST classes; should be #undef'd at the end of the file
27#define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node);
28
29namespace ast {
30
31class Expr;
32class Stmt;
33
34/// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an
35/// object being initialized
36class Designation final : public ParseNode {
37public:
38        std::deque<ptr<Expr>> designators;
39
40        Designation( const CodeLocation& loc, std::deque<ptr<Expr>>&& ds = {} )
41        : ParseNode( loc ), designators( std::move(ds) ) {}
42
43        const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
44private:
45        Designation* clone() const override { return new Designation{ *this }; }
46        MUTATE_FRIEND
47};
48
49/// Flag for whether to construct from initialzier
50enum ConstructFlag { DoConstruct, MaybeConstruct };
51
52/// Object initializer base class
53class Init : public ParseNode {
54public:
55        ConstructFlag maybeConstructed;
56
57        Init( const CodeLocation & loc, ConstructFlag mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
58
59        const Init * accept( Visitor & v ) const override = 0;
60private:
61        Init * clone() const override = 0;
62        MUTATE_FRIEND
63};
64
65/// Initializer for a common object: `int x = 4`
66class SingleInit final : public Init {
67public:
68        /// value to initialize to. Must be compile-time constant.
69        ptr<Expr> value;
70
71        SingleInit( const CodeLocation & loc, const Expr * val, ConstructFlag mc = DoConstruct )
72        : Init( loc, mc ), value( val ) {}
73
74        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
75private:
76        SingleInit * clone() const override { return new SingleInit{ *this }; }
77        MUTATE_FRIEND
78};
79
80/// Initializer recursively composed of a list of initializers.
81/// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }`
82class ListInit final : public Init {
83public:
84        /// list of initializers
85        std::vector<ptr<Init>> initializers;
86        /// list of designators; order/length is consistent with initializers
87        std::vector<ptr<Designation>> designations;
88
89        ListInit( const CodeLocation & loc, std::vector<ptr<Init>> && is,
90                std::vector<ptr<Designation>> && ds = {}, ConstructFlag mc = DoConstruct );
91
92        using iterator = std::vector<ptr<Init>>::iterator;
93        using const_iterator = std::vector<ptr<Init>>::const_iterator;
94        iterator begin() { return initializers.begin(); }
95        iterator end() { return initializers.end(); }
96        const_iterator begin() const { return initializers.begin(); }
97        const_iterator end() const { return initializers.end(); }
98
99        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
100private:
101        ListInit * clone() const override { return new ListInit{ *this }; }
102        MUTATE_FRIEND
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( 
117                const CodeLocation & loc, const Stmt * ctor, const Stmt * dtor, const Init * init )
118        : Init( loc, DoConstruct ), ctor( ctor ), dtor( dtor ), init( init ) {}
119
120        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
121private:
122        ConstructorInit * clone() const override { return new ConstructorInit{ *this }; }
123        MUTATE_FRIEND
124};
125
126}
127
128#undef MUTATE_FRIEND
129
130// Local Variables: //
131// tab-width: 4 //
132// mode: c++ //
133// compile-command: "make install" //
134// End: //
Note: See TracBrowser for help on using the repository browser.