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 : Sat Jul 22 09:52:02 2017 |
---|
13 | // Update Count : 21 |
---|
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. |
---|
27 | class Designation : public BaseSyntaxNode { |
---|
28 | public: |
---|
29 | Designation( const std::list< Expression * > & designators ); |
---|
30 | Designation( const Designation & other ); |
---|
31 | virtual ~Designation(); |
---|
32 | |
---|
33 | std::list< Expression * > & get_designators() { return designators; } |
---|
34 | |
---|
35 | virtual Designation * clone() const { return new Designation( *this ); }; |
---|
36 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
37 | virtual Designation * acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
38 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
39 | private: |
---|
40 | std::list< Expression * > designators; |
---|
41 | }; |
---|
42 | |
---|
43 | const std::list<Designation *> noDesignators; |
---|
44 | |
---|
45 | // Initializer: base class for object initializers (provide default values) |
---|
46 | class 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) |
---|
63 | class SingleInit : public Initializer { |
---|
64 | public: |
---|
65 | SingleInit( Expression *value, 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 | 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 ) const; |
---|
76 | private: |
---|
77 | //Constant *value; |
---|
78 | Expression *value; // has to be a compile-time constant |
---|
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 |
---|
83 | class ListInit : public Initializer { |
---|
84 | public: |
---|
85 | ListInit( const std::list<Initializer*> &initializers, |
---|
86 | const std::list<Designation *> &designators = {}, bool maybeConstructed = false ); |
---|
87 | ListInit( const ListInit & other ); |
---|
88 | virtual ~ListInit(); |
---|
89 | |
---|
90 | std::list<Designation *> & get_designations() { return designations; } |
---|
91 | std::list<Initializer *> & get_initializers() { return initializers; } |
---|
92 | |
---|
93 | typedef std::list<Initializer*>::iterator iterator; |
---|
94 | typedef std::list<Initializer*>::const_iterator const_iterator; |
---|
95 | iterator begin() { return initializers.begin(); } |
---|
96 | iterator end() { return initializers.end(); } |
---|
97 | const_iterator begin() const { return initializers.begin(); } |
---|
98 | const_iterator end() const { return initializers.end(); } |
---|
99 | |
---|
100 | virtual ListInit *clone() const { return new ListInit( *this ); } |
---|
101 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
102 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
103 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
104 | private: |
---|
105 | std::list<Initializer *> initializers; // order *is* important |
---|
106 | std::list<Designation *> designations; // order/length is consistent with initializers |
---|
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. |
---|
113 | class ConstructorInit : public Initializer { |
---|
114 | public: |
---|
115 | ConstructorInit( Statement * ctor, Statement * dtor, Initializer * init ); |
---|
116 | ConstructorInit( const ConstructorInit &other ); |
---|
117 | virtual ~ConstructorInit(); |
---|
118 | |
---|
119 | void set_ctor( Statement * newValue ) { ctor = newValue; } |
---|
120 | Statement * get_ctor() const { return ctor; } |
---|
121 | void set_dtor( Statement * newValue ) { dtor = newValue; } |
---|
122 | Statement * get_dtor() const { return dtor; } |
---|
123 | void set_init( Initializer * newValue ) { init = newValue; } |
---|
124 | Initializer * get_init() const { return init; } |
---|
125 | |
---|
126 | ConstructorInit *clone() const { return new ConstructorInit( *this ); } |
---|
127 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
128 | virtual Initializer *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
129 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
130 | |
---|
131 | private: |
---|
132 | Statement * ctor; |
---|
133 | Statement * dtor; |
---|
134 | // C-style initializer made up of SingleInit and ListInit nodes to use as a fallback |
---|
135 | // if an appropriate constructor definition is not found by the resolver |
---|
136 | Initializer * init; |
---|
137 | }; |
---|
138 | |
---|
139 | std::ostream & operator<<( std::ostream & out, const Initializer * init ); |
---|
140 | std::ostream & operator<<( std::ostream & out, const Designation * des ); |
---|
141 | |
---|
142 | // Local Variables: // |
---|
143 | // tab-width: 4 // |
---|
144 | // mode: c++ // |
---|
145 | // compile-command: "make install" // |
---|
146 | // End: // |
---|