source: src/AST/Init.hpp @ 489bacf

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 489bacf was 41b24c8, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

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

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