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 : Tue Jul 12 21:03:17 2016
|
---|
13 | // Update Count : 39
|
---|
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 | bool has_body() const { return body; }
|
---|
213 | AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
|
---|
214 |
|
---|
215 | virtual void print( std::ostream &os, int indent = 0 ) const;
|
---|
216 | virtual void printShort( std::ostream &os, int indent = 0 ) const;
|
---|
217 | protected:
|
---|
218 | virtual std::string typeString() const = 0;
|
---|
219 |
|
---|
220 | private:
|
---|
221 | std::list<Declaration*> members;
|
---|
222 | std::list<TypeDecl*> parameters;
|
---|
223 | bool body;
|
---|
224 | };
|
---|
225 |
|
---|
226 | class StructDecl : public AggregateDecl {
|
---|
227 | typedef AggregateDecl Parent;
|
---|
228 | public:
|
---|
229 | StructDecl( const std::string &name ) : Parent( name ) {}
|
---|
230 | StructDecl( const StructDecl &other ) : Parent( other ) {}
|
---|
231 |
|
---|
232 | virtual StructDecl *clone() const { return new StructDecl( *this ); }
|
---|
233 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
234 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
235 | private:
|
---|
236 | virtual std::string typeString() const;
|
---|
237 | };
|
---|
238 |
|
---|
239 | class UnionDecl : public AggregateDecl {
|
---|
240 | typedef AggregateDecl Parent;
|
---|
241 | public:
|
---|
242 | UnionDecl( const std::string &name ) : Parent( name ) {}
|
---|
243 | UnionDecl( const UnionDecl &other ) : Parent( other ) {}
|
---|
244 |
|
---|
245 | virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
|
---|
246 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
247 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
248 | private:
|
---|
249 | virtual std::string typeString() const;
|
---|
250 | };
|
---|
251 |
|
---|
252 | class EnumDecl : public AggregateDecl {
|
---|
253 | typedef AggregateDecl Parent;
|
---|
254 | public:
|
---|
255 | EnumDecl( const std::string &name ) : Parent( name ) {}
|
---|
256 | EnumDecl( const EnumDecl &other ) : Parent( other ) {}
|
---|
257 |
|
---|
258 | virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
|
---|
259 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
260 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
261 | private:
|
---|
262 | virtual std::string typeString() const;
|
---|
263 | };
|
---|
264 |
|
---|
265 | class TraitDecl : public AggregateDecl {
|
---|
266 | typedef AggregateDecl Parent;
|
---|
267 | public:
|
---|
268 | TraitDecl( const std::string &name ) : Parent( name ) {}
|
---|
269 | TraitDecl( const TraitDecl &other ) : Parent( other ) {}
|
---|
270 |
|
---|
271 | virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
|
---|
272 | virtual void accept( Visitor &v ) { v.visit( this ); }
|
---|
273 | virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
|
---|
274 | private:
|
---|
275 | virtual std::string typeString() const;
|
---|
276 | };
|
---|
277 |
|
---|
278 | std::ostream & operator<<( std::ostream & out, Declaration * decl );
|
---|
279 |
|
---|
280 | #endif // DECLARATION_H
|
---|
281 |
|
---|
282 | // Local Variables: //
|
---|
283 | // tab-width: 4 //
|
---|
284 | // mode: c++ //
|
---|
285 | // compile-command: "make install" //
|
---|
286 | // End: //
|
---|