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