source: src/SynTree/Initializer.h @ 9c35431

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9c35431 was 50377a4, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Refactor tree print code to use Indenter

  • Property mode set to 100644
File size: 5.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// Initializer.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Aug  9 10:19:00 2017
13// Update Count     : 22
14//
15
16#pragma once
17
18#include <iosfwd>            // for ostream
19#include <list>              // for list, list<>::const_iterator, list<>::it...
20
21#include "BaseSyntaxNode.h"  // for BaseSyntaxNode
22#include "Mutator.h"         // for Mutator
23#include "Visitor.h"         // for Visitor
24
25class Expression;
26class Statement;
27
28// Designation: list of designator (NameExpr, VariableExpr, and ConstantExpr) expressions that specify an object being initialized.
29class Designation : public BaseSyntaxNode {
30public:
31        std::list< Expression * > designators;
32
33        Designation( const std::list< Expression * > & designators );
34        Designation( const Designation & other );
35        virtual ~Designation();
36
37        std::list< Expression * > & get_designators() { return designators; }
38
39        virtual Designation * clone() const override { return new Designation( *this ); };
40        virtual void accept( Visitor &v ) override { v.visit( this ); }
41        virtual Designation * acceptMutator( Mutator &m ) override { return m.mutate( this ); }
42        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
43};
44
45const std::list<Designation *> noDesignators;
46
47// Initializer: base class for object initializers (provide default values)
48class Initializer : public BaseSyntaxNode {
49  public:
50        Initializer( bool maybeConstructed );
51        Initializer( const Initializer & other );
52        virtual ~Initializer();
53
54        bool get_maybeConstructed() { return maybeConstructed; }
55
56        virtual Initializer *clone() const override = 0;
57        virtual void accept( Visitor &v ) override = 0;
58        virtual Initializer *acceptMutator( Mutator &m ) override = 0;
59        virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0;
60  private:
61        bool maybeConstructed;
62};
63
64// SingleInit represents an initializer for a common object (e.g., int x = 4)
65class SingleInit : public Initializer {
66  public:
67        //Constant *value;
68        Expression *value;      // has to be a compile-time constant
69
70        SingleInit( Expression *value, bool maybeConstructed = false );
71        SingleInit( const SingleInit &other );
72        virtual ~SingleInit();
73
74        Expression *get_value() { return value; }
75        void set_value( Expression *newValue ) { value = newValue; }
76
77        virtual SingleInit *clone() const override { return new SingleInit( *this); }
78        virtual void accept( Visitor &v ) override { v.visit( this ); }
79        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
80        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
81};
82
83// ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize
84// an array or aggregate
85class ListInit : public Initializer {
86  public:
87        std::list<Initializer *> initializers;  // order *is* important
88        std::list<Designation *> designations;  // order/length is consistent with initializers
89
90        ListInit( const std::list<Initializer*> &initializers,
91                          const std::list<Designation *> &designators = {}, bool maybeConstructed = false );
92        ListInit( const ListInit & other );
93        virtual ~ListInit();
94
95        std::list<Designation *> & get_designations() { return designations; }
96        std::list<Initializer *> & get_initializers() { return initializers; }
97
98        typedef std::list<Initializer*>::iterator iterator;
99        typedef std::list<Initializer*>::const_iterator const_iterator;
100        iterator begin() { return initializers.begin(); }
101        iterator end() { return initializers.end(); }
102        const_iterator begin() const { return initializers.begin(); }
103        const_iterator end() const { return initializers.end(); }
104
105        virtual ListInit *clone() const override { return new ListInit( *this ); }
106        virtual void accept( Visitor &v ) override { v.visit( this ); }
107        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
108        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
109};
110
111// ConstructorInit represents an initializer that is either a constructor expression or
112// a C-style initializer.
113// It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
114// to true on SingleInit or ListInit constructors if object should be constructed.
115class ConstructorInit : public Initializer {
116  public:
117        Statement * ctor;
118        Statement * dtor;
119        // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
120        // if an appropriate constructor definition is not found by the resolver
121        Initializer * init;
122
123        ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init );
124        ConstructorInit( const ConstructorInit &other );
125        virtual ~ConstructorInit();
126
127        void set_ctor( Statement * newValue ) { ctor = newValue; }
128        Statement * get_ctor() const { return ctor; }
129        void set_dtor( Statement * newValue ) { dtor = newValue; }
130        Statement * get_dtor() const { return dtor; }
131        void set_init( Initializer * newValue ) { init = newValue; }
132        Initializer * get_init() const { return init; }
133
134        ConstructorInit *clone() const override { return new ConstructorInit( *this ); }
135        virtual void accept( Visitor &v ) override { v.visit( this ); }
136        virtual Initializer *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
137        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
138
139  private:
140};
141
142// Local Variables: //
143// tab-width: 4 //
144// mode: c++ //
145// compile-command: "make install" //
146// End: //
Note: See TracBrowser for help on using the repository browser.