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 | // Declaration.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 Jun 30 21:17:24 2016 |
---|
13 | // Update Count : 38 |
---|
14 | // |
---|
15 | |
---|
16 | #ifndef DECLARATION_H |
---|
17 | #define DECLARATION_H |
---|
18 | |
---|
19 | #include "SynTree.h" |
---|
20 | #include "Visitor.h" |
---|
21 | #include "Mutator.h" |
---|
22 | #include "Parser/LinkageSpec.h" |
---|
23 | #include "Parser/ParseNode.h" |
---|
24 | #include <string> |
---|
25 | |
---|
26 | class Declaration { |
---|
27 | public: |
---|
28 | Declaration( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage ); |
---|
29 | Declaration( const Declaration &other ); |
---|
30 | virtual ~Declaration(); |
---|
31 | |
---|
32 | const std::string &get_name() const { return name; } |
---|
33 | void set_name( std::string newValue ) { name = newValue; } |
---|
34 | DeclarationNode::StorageClass get_storageClass() const { return storageClass; } |
---|
35 | void set_storageClass( DeclarationNode::StorageClass newValue ) { storageClass = newValue; } |
---|
36 | LinkageSpec::Type get_linkage() const { return linkage; } |
---|
37 | void set_linkage( LinkageSpec::Type newValue ) { linkage = newValue; } |
---|
38 | bool get_isInline() const { return isInline; } |
---|
39 | void set_isInline( bool newValue ) { isInline = newValue; } |
---|
40 | bool get_isNoreturn() const { return isNoreturn; } |
---|
41 | void set_isNoreturn( bool newValue ) { isNoreturn = newValue; } |
---|
42 | UniqueId get_uniqueId() const { return uniqueId; } |
---|
43 | bool get_extension() const { return extension; } |
---|
44 | Declaration *set_extension( bool exten ) { extension = exten; return this; } |
---|
45 | |
---|
46 | void fixUniqueId( void ); |
---|
47 | virtual Declaration *clone() const = 0; |
---|
48 | virtual void accept( Visitor &v ) = 0; |
---|
49 | virtual Declaration *acceptMutator( Mutator &m ) = 0; |
---|
50 | virtual void print( std::ostream &os, int indent = 0 ) const = 0; |
---|
51 | virtual void printShort( std::ostream &os, int indent = 0 ) const = 0; |
---|
52 | |
---|
53 | static void dumpIds( std::ostream &os ); |
---|
54 | static Declaration *declFromId( UniqueId id ); |
---|
55 | private: |
---|
56 | std::string name; |
---|
57 | DeclarationNode::StorageClass storageClass; |
---|
58 | LinkageSpec::Type linkage; |
---|
59 | bool isInline, isNoreturn; |
---|
60 | UniqueId uniqueId; |
---|
61 | bool extension = false; |
---|
62 | }; |
---|
63 | |
---|
64 | class DeclarationWithType : public Declaration { |
---|
65 | public: |
---|
66 | DeclarationWithType( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage ); |
---|
67 | DeclarationWithType( const DeclarationWithType &other ); |
---|
68 | virtual ~DeclarationWithType(); |
---|
69 | |
---|
70 | std::string get_mangleName() const { return mangleName; } |
---|
71 | void set_mangleName( std::string newValue ) { mangleName = newValue; } |
---|
72 | |
---|
73 | std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
74 | |
---|
75 | int get_scopeLevel() const { return scopeLevel; } |
---|
76 | void set_scopeLevel( int newValue ) { scopeLevel = newValue; } |
---|
77 | |
---|
78 | virtual DeclarationWithType *clone() const = 0; |
---|
79 | virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0; |
---|
80 | |
---|
81 | virtual Type *get_type() const = 0; |
---|
82 | virtual void set_type(Type *) = 0; |
---|
83 | private: |
---|
84 | // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2 |
---|
85 | std::string mangleName; |
---|
86 | // need to remember the scope level at which the variable was declared, so that |
---|
87 | // shadowed identifiers can be accessed |
---|
88 | int scopeLevel = 0; |
---|
89 | }; |
---|
90 | |
---|
91 | class ObjectDecl : public DeclarationWithType { |
---|
92 | typedef DeclarationWithType Parent; |
---|
93 | public: |
---|
94 | ObjectDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, Expression *bitfieldWidth, Type *type, Initializer *init, bool isInline = false, bool isNoreturn = false ); |
---|
95 | ObjectDecl( const ObjectDecl &other ); |
---|
96 | virtual ~ObjectDecl(); |
---|
97 | |
---|
98 | virtual Type *get_type() const { return type; } |
---|
99 | virtual void set_type(Type *newType) { type = newType; } |
---|
100 | |
---|
101 | Initializer *get_init() const { return init; } |
---|
102 | void set_init( Initializer *newValue ) { init = newValue; } |
---|
103 | Expression *get_bitfieldWidth() const { return bitfieldWidth; } |
---|
104 | void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; } |
---|
105 | |
---|
106 | virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); } |
---|
107 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
108 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
109 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
110 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
111 | private: |
---|
112 | Type *type; |
---|
113 | Initializer *init; |
---|
114 | Expression *bitfieldWidth; |
---|
115 | }; |
---|
116 | |
---|
117 | class FunctionDecl : public DeclarationWithType { |
---|
118 | typedef DeclarationWithType Parent; |
---|
119 | public: |
---|
120 | FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, const std::list< Attribute * > attributes = std::list< Attribute * >() ); |
---|
121 | FunctionDecl( const FunctionDecl &other ); |
---|
122 | virtual ~FunctionDecl(); |
---|
123 | |
---|
124 | Type *get_type() const; |
---|
125 | virtual void set_type(Type *); |
---|
126 | |
---|
127 | FunctionType *get_functionType() const { return type; } |
---|
128 | void set_functionType( FunctionType *newValue ) { type = newValue; } |
---|
129 | CompoundStmt *get_statements() const { return statements; } |
---|
130 | void set_statements( CompoundStmt *newValue ) { statements = newValue; } |
---|
131 | std::list< std::string >& get_oldIdents() { return oldIdents; } |
---|
132 | std::list< Declaration* >& get_oldDecls() { return oldDecls; } |
---|
133 | std::list< Attribute * >& get_attributes() { return attributes; } |
---|
134 | |
---|
135 | virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); } |
---|
136 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
137 | virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
138 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
139 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
140 | private: |
---|
141 | FunctionType *type; |
---|
142 | CompoundStmt *statements; |
---|
143 | std::list< std::string > oldIdents; |
---|
144 | std::list< Declaration* > oldDecls; |
---|
145 | std::list< Attribute * > attributes; |
---|
146 | }; |
---|
147 | |
---|
148 | class NamedTypeDecl : public Declaration { |
---|
149 | typedef Declaration Parent; |
---|
150 | public: |
---|
151 | NamedTypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ); |
---|
152 | NamedTypeDecl( const TypeDecl &other ); |
---|
153 | virtual ~NamedTypeDecl(); |
---|
154 | |
---|
155 | Type *get_base() const { return base; } |
---|
156 | void set_base( Type *newValue ) { base = newValue; } |
---|
157 | std::list< TypeDecl* >& get_parameters() { return parameters; } |
---|
158 | std::list< DeclarationWithType* >& get_assertions() { return assertions; } |
---|
159 | |
---|
160 | virtual NamedTypeDecl *clone() const = 0; |
---|
161 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
162 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
163 | protected: |
---|
164 | virtual std::string typeString() const = 0; |
---|
165 | private: |
---|
166 | Type *base; |
---|
167 | std::list< TypeDecl* > parameters; |
---|
168 | std::list< DeclarationWithType* > assertions; |
---|
169 | }; |
---|
170 | |
---|
171 | class TypeDecl : public NamedTypeDecl { |
---|
172 | typedef NamedTypeDecl Parent; |
---|
173 | public: |
---|
174 | enum Kind { Any, Dtype, Ftype }; |
---|
175 | |
---|
176 | TypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type, Kind kind ); |
---|
177 | TypeDecl( const TypeDecl &other ); |
---|
178 | |
---|
179 | Kind get_kind() const { return kind; } |
---|
180 | |
---|
181 | virtual TypeDecl *clone() const { return new TypeDecl( *this ); } |
---|
182 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
183 | virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
184 | private: |
---|
185 | virtual std::string typeString() const; |
---|
186 | Kind kind; |
---|
187 | }; |
---|
188 | |
---|
189 | class TypedefDecl : public NamedTypeDecl { |
---|
190 | typedef NamedTypeDecl Parent; |
---|
191 | public: |
---|
192 | TypedefDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ) : Parent( name, sc, type ) {} |
---|
193 | TypedefDecl( const TypedefDecl &other ) : Parent( other ) {} |
---|
194 | |
---|
195 | virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); } |
---|
196 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
197 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
198 | private: |
---|
199 | virtual std::string typeString() const; |
---|
200 | }; |
---|
201 | |
---|
202 | class AggregateDecl : public Declaration { |
---|
203 | typedef Declaration Parent; |
---|
204 | public: |
---|
205 | AggregateDecl( const std::string &name ); |
---|
206 | AggregateDecl( const AggregateDecl &other ); |
---|
207 | virtual ~AggregateDecl(); |
---|
208 | |
---|
209 | std::list<Declaration*>& get_members() { return members; } |
---|
210 | std::list<TypeDecl*>& get_parameters() { return parameters; } |
---|
211 | |
---|
212 | virtual void print( std::ostream &os, int indent = 0 ) const; |
---|
213 | virtual void printShort( std::ostream &os, int indent = 0 ) const; |
---|
214 | protected: |
---|
215 | virtual std::string typeString() const = 0; |
---|
216 | |
---|
217 | private: |
---|
218 | std::list<Declaration*> members; |
---|
219 | std::list<TypeDecl*> parameters; |
---|
220 | }; |
---|
221 | |
---|
222 | class StructDecl : public AggregateDecl { |
---|
223 | typedef AggregateDecl Parent; |
---|
224 | public: |
---|
225 | StructDecl( const std::string &name ) : Parent( name ) {} |
---|
226 | StructDecl( const StructDecl &other ) : Parent( other ) {} |
---|
227 | |
---|
228 | virtual StructDecl *clone() const { return new StructDecl( *this ); } |
---|
229 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
230 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
231 | |
---|
232 | private: |
---|
233 | virtual std::string typeString() const; |
---|
234 | }; |
---|
235 | |
---|
236 | class UnionDecl : public AggregateDecl { |
---|
237 | typedef AggregateDecl Parent; |
---|
238 | public: |
---|
239 | UnionDecl( const std::string &name ) : Parent( name ) {} |
---|
240 | UnionDecl( const UnionDecl &other ) : Parent( other ) {} |
---|
241 | |
---|
242 | virtual UnionDecl *clone() const { return new UnionDecl( *this ); } |
---|
243 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
244 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
245 | private: |
---|
246 | virtual std::string typeString() const; |
---|
247 | }; |
---|
248 | |
---|
249 | class EnumDecl : public AggregateDecl { |
---|
250 | typedef AggregateDecl Parent; |
---|
251 | public: |
---|
252 | EnumDecl( const std::string &name ) : Parent( name ) {} |
---|
253 | EnumDecl( const EnumDecl &other ) : Parent( other ) {} |
---|
254 | |
---|
255 | virtual EnumDecl *clone() const { return new EnumDecl( *this ); } |
---|
256 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
257 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
258 | private: |
---|
259 | virtual std::string typeString() const; |
---|
260 | }; |
---|
261 | |
---|
262 | class TraitDecl : public AggregateDecl { |
---|
263 | typedef AggregateDecl Parent; |
---|
264 | public: |
---|
265 | TraitDecl( const std::string &name ) : Parent( name ) {} |
---|
266 | TraitDecl( const TraitDecl &other ) : Parent( other ) {} |
---|
267 | |
---|
268 | virtual TraitDecl *clone() const { return new TraitDecl( *this ); } |
---|
269 | virtual void accept( Visitor &v ) { v.visit( this ); } |
---|
270 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); } |
---|
271 | private: |
---|
272 | virtual std::string typeString() const; |
---|
273 | }; |
---|
274 | |
---|
275 | std::ostream & operator<<( std::ostream & out, Declaration * decl ); |
---|
276 | |
---|
277 | #endif // DECLARATION_H |
---|
278 | |
---|
279 | // Local Variables: // |
---|
280 | // tab-width: 4 // |
---|
281 | // mode: c++ // |
---|
282 | // compile-command: "make install" // |
---|
283 | // End: // |
---|