source: src/AST/Init.hpp @ 54db6ba

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

Fixes to the new templated pass and started on conversions

  • Property mode set to 100644
File size: 5.2 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        virtual const Designation* accept( Visitor& v ) const override { return v.visit( this ); }
40private:
41        virtual Designation* clone() const override { return new Designation{ *this }; }
42};
43
44/// Object initializer base class
45class Init : public ParseNode {
46public:
47        bool maybeConstructed;
48
49        Init( const CodeLocation& loc, bool mc ) : ParseNode( loc ), maybeConstructed( mc ) {}
50
51        virtual const Init * accept( Visitor& v ) const override = 0;
52private:
53        virtual const Init * clone() const override = 0;
54};
55
56/// Initializer for a common object: `int x = 4`
57class SingleInit final : public Init {
58public:
59        /// value to initialize to. Must be compile-time constant.
60        ptr<Expr> value;
61
62        SingleInit( const CodeLocation& loc, Expr* val, bool mc = false )
63        : Init( loc, mc ), value( val ) {}
64
65        virtual const Init * accept( Visitor & v ) const override { return v.visit( this ); }
66private:
67        virtual SingleInit * clone() const override { return new SingleInit{ *this }; }
68
69        /// Must be copied in ALL derived classes
70        template<typename node_t>
71        friend auto mutate(const node_t * node);
72};
73
74/// Initializer recursively composed of a list of initializers.
75/// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }`
76class ListInit final : public Init {
77public:
78        /// list of initializers
79        std::vector<ptr<Init>> initializers;
80        /// list of designators; order/length is consistent with initializers
81        std::vector<ptr<Designation>> designations;
82
83        ListInit( const CodeLocation& loc, std::vector<ptr<Init>>&& is,
84                std::vector<ptr<Designation>>&& ds = {}, bool mc = false );
85
86        using iterator = std::vector<ptr<Init>>::iterator;
87        using const_iterator = std::vector<ptr<Init>>::const_iterator;
88        iterator begin() { return initializers.begin(); }
89        iterator end() { return initializers.end(); }
90        const_iterator begin() const { return initializers.begin(); }
91        const_iterator end() const { return initializers.end(); }
92
93        virtual const Init * accept( Visitor & v ) const override { return v.visit( this ); }
94private:
95        virtual ListInit * clone() const override { return new ListInit{ *this }; }
96
97        /// Must be copied in ALL derived classes
98        template<typename node_t>
99        friend auto mutate(const node_t * node);
100};
101
102/// Either a constructor expression or a C-style initializer.
103/// Should not be necessary to create manually; instead set `maybeConstructed` true on `SingleInit`
104/// or `ListInit` if the object should be constructed.
105class ConstructorInit final : public Init {
106public:
107        ptr<Stmt> ctor;
108        ptr<Stmt> dtor;
109        /// C-style initializer made up of SingleInit/ListInit nodes to use as a fallback if an
110        /// appropriate constructor definition is not found by the resolver.
111        ptr<Init> init;
112
113        ConstructorInit( const CodeLocation& loc, Stmt* ctor, Stmt* dtor, Init* init )
114        : Init( loc, true ), ctor( ctor ), dtor( dtor ), init( init ) {}
115
116        virtual const Init * accept( Visitor & v ) const override { return v.visit( this ); }
117private:
118        virtual ConstructorInit * clone() const override { return new ConstructorInit{ *this }; }
119
120        /// Must be copied in ALL derived classes
121        template<typename node_t>
122        friend auto mutate(const node_t * node);
123};
124
125
126//=================================================================================================
127/// This disgusting and giant piece of boiler-plate is here to solve a cyclic dependency
128/// remove only if there is a better solution
129/// The problem is that ast::ptr< ... > uses increment/decrement which won't work well with
130/// forward declarations
131inline void increment( const class Init * node, Node::ref_type ref ) { node->increment( ref ); }
132inline void decrement( const class Init * node, Node::ref_type ref ) { node->decrement( ref ); }
133inline void increment( const class SingleInit * node, Node::ref_type ref ) { node->increment( ref ); }
134inline void decrement( const class SingleInit * node, Node::ref_type ref ) { node->decrement( ref ); }
135inline void increment( const class ListInit * node, Node::ref_type ref ) { node->increment( ref ); }
136inline void decrement( const class ListInit * node, Node::ref_type ref ) { node->decrement( ref ); }
137inline void increment( const class ConstructorInit * node, Node::ref_type ref ) { node->increment( ref ); }
138inline void decrement( const class ConstructorInit * node, Node::ref_type ref ) { node->decrement( ref ); }
139}
140
141// Local Variables: //
142// tab-width: 4 //
143// mode: c++ //
144// compile-command: "make install" //
145// End: //
Note: See TracBrowser for help on using the repository browser.