source: src/AST/Init.hpp @ 4dab7e8

Last change on this file since 4dab7e8 was 94c98f0e, checked in by Andrew Beach <ajbeach@…>, 20 months ago

Added/cleaned some AST comments.

  • Property mode set to 100644
File size: 4.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 <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 \
28    template<typename node_t> friend node_t * mutate(const node_t * node); \
29        template<typename node_t> friend node_t * shallowCopy(const node_t * node);
30
31namespace ast {
32
33class Expr;
34class Stmt;
35
36/// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an
37/// object being initialized
38class Designation final : public ParseNode {
39public:
40        std::deque<ptr<Expr>> designators;
41
42        Designation( const CodeLocation& loc, std::deque<ptr<Expr>>&& ds = {} )
43        : ParseNode( loc ), designators( std::move(ds) ) {}
44
45        const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
46private:
47        Designation* clone() const override { return new Designation{ *this }; }
48        MUTATE_FRIEND
49};
50
51/// Flag for whether to construct from initialzier
52enum ConstructFlag { NoConstruct, MaybeConstruct };
53
54/// Object initializer base class
55class Init : public ParseNode {
56public:
57        ConstructFlag maybeConstructed;
58
59        Init( const CodeLocation & loc, ConstructFlag mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
60
61        const Init * accept( Visitor & v ) const override = 0;
62private:
63        Init * clone() const override = 0;
64        MUTATE_FRIEND
65};
66
67/// Initializer for a common object: `int x = 4`
68class SingleInit final : public Init {
69public:
70        /// value to initialize to. Must be compile-time constant.
71        ptr<Expr> value;
72
73        SingleInit( const CodeLocation & loc, const Expr * val, ConstructFlag mc = NoConstruct )
74        : Init( loc, mc ), value( val ) {}
75
76        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
77private:
78        SingleInit * clone() const override { return new SingleInit{ *this }; }
79        MUTATE_FRIEND
80};
81
82/// Initializer recursively composed of a list of initializers.
83/// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }`
84class ListInit final : public Init {
85public:
86        /// list of initializers
87        std::vector<ptr<Init>> initializers;
88        /// list of designators; order/length is consistent with initializers
89        std::vector<ptr<Designation>> designations;
90
91        ListInit( const CodeLocation & loc, std::vector<ptr<Init>> && is,
92                std::vector<ptr<Designation>> && ds = {}, ConstructFlag mc = NoConstruct );
93
94        using iterator = std::vector<ptr<Init>>::iterator;
95        using const_iterator = std::vector<ptr<Init>>::const_iterator;
96        iterator begin() { return initializers.begin(); }
97        iterator end() { return initializers.end(); }
98        const_iterator begin() const { return initializers.begin(); }
99        const_iterator end() const { return initializers.end(); }
100        size_t size() const { return initializers.size(); }
101
102        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
103private:
104        ListInit * clone() const override { return new ListInit{ *this }; }
105        MUTATE_FRIEND
106};
107
108/// Either a constructor expression or a C-style initializer.
109/// Should not be necessary to create manually; instead set `maybeConstructed` true on `SingleInit`
110/// or `ListInit` if the object should be constructed.
111class ConstructorInit final : public Init {
112public:
113        ptr<Stmt> ctor;
114        ptr<Stmt> dtor;
115        /// C-style initializer made up of SingleInit/ListInit nodes to use as a fallback if an
116        /// appropriate constructor definition is not found by the resolver.
117        ptr<Init> init;
118
119        ConstructorInit(
120                const CodeLocation & loc, const Stmt * ctor, const Stmt * dtor, const Init * init )
121        : Init( loc, MaybeConstruct ), ctor( ctor ), dtor( dtor ), init( init ) {}
122
123        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
124private:
125        ConstructorInit * clone() const override { return new ConstructorInit{ *this }; }
126        MUTATE_FRIEND
127};
128
129}
130
131#undef MUTATE_FRIEND
132
133// Local Variables: //
134// tab-width: 4 //
135// mode: c++ //
136// compile-command: "make install" //
137// End: //
Note: See TracBrowser for help on using the repository browser.