source: src/AST/Init.hpp @ 9e1d485

ADTarm-ehcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9e1d485 was 9e1d485, checked in by Aaron Moss <a3moss@…>, 4 years ago

First draft of ast::Type with subclasses

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