source: src/AST/Init.hpp @ aaeacf4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since aaeacf4 was 60aaa51d, checked in by Aaron Moss <a3moss@…>, 5 years ago

More resolver porting; mostly CurrentObject?

  • Property mode set to 100644
File size: 4.1 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
[60aaa51d]18#include <deque>
[9131e54]19#include <utility>        // for move
20#include <vector>
21
22#include "ParseNode.hpp"
23#include "Node.hpp"       // for ptr
24#include "Visitor.hpp"
25
[f3cc5b6]26// Must be included in *all* AST classes; should be #undef'd at the end of the file
[41b24c8]27#define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node);
[f3cc5b6]28
[9131e54]29namespace ast {
30
31class Expr;
32class Stmt;
33
[e0115286]34/// List of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an
[9131e54]35/// object being initialized
36class Designation final : public ParseNode {
37public:
[60aaa51d]38        std::deque<ptr<Expr>> designators;
[9131e54]39
[60aaa51d]40        Designation( const CodeLocation& loc, std::deque<ptr<Expr>>&& ds = {} )
[9131e54]41        : ParseNode( loc ), designators( std::move(ds) ) {}
42
[23f99e1]43        const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
[9131e54]44private:
[23f99e1]45        Designation* clone() const override { return new Designation{ *this }; }
[f3cc5b6]46        MUTATE_FRIEND
[9131e54]47};
48
[9e1d485]49/// Flag for whether to construct from initialzier
50enum ConstructFlag { DoConstruct, MaybeConstruct };
51
[9131e54]52/// Object initializer base class
53class Init : public ParseNode {
54public:
[9e1d485]55        ConstructFlag maybeConstructed;
[9131e54]56
[9e1d485]57        Init( const CodeLocation& loc, ConstructFlag mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
[9131e54]58
[23f99e1]59        const Init * accept( Visitor& v ) const override = 0;
[9131e54]60private:
[87701b6]61        Init * clone() const override = 0;
[f3cc5b6]62        MUTATE_FRIEND
[9131e54]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
[9e1d485]71        SingleInit( const CodeLocation& loc, Expr* val, ConstructFlag mc = DoConstruct )
[9131e54]72        : Init( loc, mc ), value( val ) {}
73
[23f99e1]74        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
[9131e54]75private:
[23f99e1]76        SingleInit * clone() const override { return new SingleInit{ *this }; }
[f3cc5b6]77        MUTATE_FRIEND
[9131e54]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
[e0115286]89        ListInit( const CodeLocation& loc, std::vector<ptr<Init>>&& is,
[9e1d485]90                std::vector<ptr<Designation>>&& ds = {}, ConstructFlag mc = DoConstruct );
[e0115286]91
[9131e54]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
[23f99e1]99        const Init * accept( Visitor & v ) const override { return v.visit( this ); }
[9131e54]100private:
[23f99e1]101        ListInit * clone() const override { return new ListInit{ *this }; }
[f3cc5b6]102        MUTATE_FRIEND
[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 }; }
[f3cc5b6]122        MUTATE_FRIEND
[9131e54]123};
124
125}
126
[f3cc5b6]127#undef MUTATE_FRIEND
128
[9131e54]129// Local Variables: //
130// tab-width: 4 //
131// mode: c++ //
132// compile-command: "make install" //
133// End: //
Note: See TracBrowser for help on using the repository browser.