source: src/AST/Init.hpp @ 9131e54

arm-ehcleanup-dtorsjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 9131e54 was 9131e54, checked in by Aaron Moss <a3moss@…>, 3 years ago

Added Init for new AST

  • Property mode set to 100644
File size: 3.6 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/// 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 Init* accept( Visitor& v ) override = 0;
52private:
53        virtual 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        Init* accept( Visitor& v ) override { return v.visit( this ); }
66private:
67        SingleInit* clone() const override { return new SingleInit{ *this }; }
68};
69
70/// Initializer recursively composed of a list of initializers.
71/// Used to initialize an array or aggregate: `int a[] = { 1, 2, 3 }`
72class ListInit final : public Init {
73public:
74        /// list of initializers
75        std::vector<ptr<Init>> initializers;
76        /// list of designators; order/length is consistent with initializers
77        std::vector<ptr<Designation>> designations;
78
79        ListInit( const CodeLocation& loc, std::vector<ptr<Init>>&& is, 
80                std::vector<ptr<Designation>>&& ds = {}, bool mc = false );
81       
82        using iterator = std::vector<ptr<Init>>::iterator;
83        using const_iterator = std::vector<ptr<Init>>::const_iterator;
84        iterator begin() { return initializers.begin(); }
85        iterator end() { return initializers.end(); }
86        const_iterator begin() const { return initializers.begin(); }
87        const_iterator end() const { return initializers.end(); }
88
89        Init* accept( Visitor& v ) override { return v.visit( this ); }
90private:
91        ListInit* clone() const override { return new ListInit{ *this }; }
92};
93
94/// Either a constructor expression or a C-style initializer.
95/// Should not be necessary to create manually; instead set `maybeConstructed` true on `SingleInit`
96/// or `ListInit` if the object should be constructed.
97class ConstructorInit final : public Init {
98public:
99        ptr<Stmt> ctor;
100        ptr<Stmt> dtor;
101        /// C-style initializer made up of SingleInit/ListInit nodes to use as a fallback if an
102        /// appropriate constructor definition is not found by the resolver.
103        ptr<Init> init;
104
105        ConstructorInit( const CodeLocation& loc, Stmt* ctor, Stmt* dtor, Init* init )
106        : Init( loc, true ), ctor( ctor ), dtor( dtor ), init( init ) {}
107
108        Init* accept( Visitor& v ) override { return v.visit( this ); }
109private:
110        ConstructorInit* clone() const override { return new ConstructorInit{ *this }; }
111};
112
113}
114
115// Local Variables: //
116// tab-width: 4 //
117// mode: c++ //
118// compile-command: "make install" //
119// End: //
Note: See TracBrowser for help on using the repository browser.