source: src/SynTree/Initializer.h @ 6cbc25a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 6cbc25a was f1b1e4c, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

can construct global const objects, except with intrinsic constructors

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