source: src/SynTree/Initializer.h @ e464759

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 e464759 was 6013bd7, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt at named designators

  • Property mode set to 100644
File size: 5.4 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 : Peter A. Buhr
12// Last Modified On : Thu Mar 23 16:12:42 2017
13// Update Count     : 20
14//
15
16#ifndef INITIALIZER_H
17#define INITIALIZER_H
18
19#include <cassert>
20
21#include "BaseSyntaxNode.h"
22#include "Mutator.h"
23#include "SynTree.h"
24#include "Type.h"
25#include "Visitor.h"
26
27const std::list<Expression*> noDesignators;
28
29// Initializer: base class for object initializers (provide default values)
30class Initializer : public BaseSyntaxNode {
31  public:
32        //      Initializer( std::string _name = std::string(""), int _pos = 0 );
33        Initializer( bool maybeConstructed );
34        Initializer( const Initializer & other );
35        virtual ~Initializer();
36
37        static std::string designator_name( Expression *designator );
38
39        //      void set_name( std::string newValue ) { name = newValue; }
40        //      std::string get_name() const { return name; }
41
42        //      void set_pos( int newValue ) { pos = newValue; }
43        //      int get_pos() const { return pos; }
44        virtual void set_designators( std::list<Expression *> & ) { assert(false); }
45        virtual std::list<Expression *> &get_designators() {
46                assert(false);
47                std::list<Expression *> *ret = 0; return *ret;  // never reached
48        }
49
50        bool get_maybeConstructed() { return maybeConstructed; }
51
52        virtual Initializer *clone() const = 0;
53        virtual void accept( Visitor &v ) = 0;
54        virtual Initializer *acceptMutator( Mutator &m ) = 0;
55        virtual void print( std::ostream &os, int indent = 0 );
56  private:
57        //      std::string name;
58        //      int pos;
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        SingleInit( Expression *value, const std::list< Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
66        SingleInit( const SingleInit &other );
67        virtual ~SingleInit();
68
69        Expression *get_value() { return value; }
70        void set_value( Expression *newValue ) { value = newValue; }
71
72        std::list<Expression *> &get_designators() { return designators; }
73        void set_designators( std::list<Expression *> &newValue ) { designators = 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 );
79  private:
80        //Constant *value;
81        Expression *value;      // has to be a compile-time constant
82        std::list< Expression * > designators;
83};
84
85// ListInit represents an initializer that is composed recursively of a list of initializers; this is used to initialize
86// an array or aggregate
87class ListInit : public Initializer {
88  public:
89        ListInit( const std::list<Initializer*> &initializers,
90                          const std::list<Expression *> &designators = std::list< Expression * >(), bool maybeConstructed = false );
91        ListInit( const ListInit & other );
92        virtual ~ListInit();
93
94        void set_designators( std::list<Expression *> &newValue ) { designators = newValue; }
95        std::list<Expression *> &get_designators() { return designators; }
96        void set_initializers( std::list<Initializer*> &newValue ) { initializers = newValue; }
97        std::list<Initializer*> &get_initializers() { return initializers; }
98
99        typedef std::list<Initializer*>::iterator iterator;
100        iterator begin() { return initializers.begin(); }
101        iterator end() { 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 );
107  private:
108        std::list<Initializer*> initializers;  // order *is* important
109        std::list<Expression *> designators;
110};
111
112// ConstructorInit represents an initializer that is either a constructor expression or
113// a C-style initializer.
114// It should not be necessary to create ConstructorInit nodes manually. Instead, set maybeConstructed
115// to true on SingleInit or ListInit constructors if object should be constructed.
116class ConstructorInit : public Initializer {
117  public:
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 );
133
134  private:
135        Statement * ctor;
136        Statement * dtor;
137        // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback
138        // if an appropriate constructor definition is not found by the resolver
139        Initializer * init;
140};
141
142std::ostream & operator<<( std::ostream & out, Initializer * init );
143
144#endif // INITIALIZER_H
145
146// Local Variables: //
147// tab-width: 4 //
148// mode: c++ //
149// compile-command: "make install" //
150// End: //
Note: See TracBrowser for help on using the repository browser.