source: src/SynTree/Initializer.h @ d104b02

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d104b02 was 65cdc1e, checked in by Andrew Beach <ajbeach@…>, 7 years ago

Syntax Nodes give public access to the fields with effective public access.X

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