source: src/SynTree/Declaration.h@ 9236060

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 9236060 was 65cdc1e, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Syntax Nodes give public access to the fields with effective public access.X

  • Property mode set to 100644
File size: 13.8 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// Declaration.h --
8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Aug 9 14:45:00 2017
13// Update Count : 126
14//
15
16#pragma once
17
18#include <string>
19
20#include "BaseSyntaxNode.h"
21#include "Mutator.h"
22#include "Visitor.h"
23#include "SynTree.h"
24#include "Parser/LinkageSpec.h"
25#include "Parser/ParseNode.h"
26
27class Declaration : public BaseSyntaxNode {
28 public:
29 std::string name;
30 LinkageSpec::Spec linkage;
31 bool extension = false;
32
33 Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
34 Declaration( const Declaration &other );
35 virtual ~Declaration();
36
37 const std::string &get_name() const { return name; }
38 void set_name( std::string newValue ) { name = newValue; }
39
40 Type::StorageClasses get_storageClasses() const { return storageClasses; }
41
42 LinkageSpec::Spec get_linkage() const { return linkage; }
43 void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
44
45 UniqueId get_uniqueId() const { return uniqueId; }
46
47 bool get_extension() const { return extension; }
48 Declaration *set_extension( bool exten ) { extension = exten; return this; }
49
50 void fixUniqueId( void );
51 virtual Declaration *clone() const = 0;
52 virtual void accept( Visitor &v ) = 0;
53 virtual Declaration *acceptMutator( Mutator &m ) = 0;
54 virtual void print( std::ostream &os, int indent = 0 ) const = 0;
55 virtual void printShort( std::ostream &os, int indent = 0 ) const = 0;
56
57 static void dumpIds( std::ostream &os );
58 static Declaration *declFromId( UniqueId id );
59
60 private:
61 Type::StorageClasses storageClasses;
62 UniqueId uniqueId;
63};
64
65class DeclarationWithType : public Declaration {
66 public:
67 // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
68 std::string mangleName;
69 // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
70 int scopeLevel = 0;
71
72 ConstantExpr *asmName;
73 std::list< Attribute * > attributes;
74
75 DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
76 DeclarationWithType( const DeclarationWithType &other );
77 virtual ~DeclarationWithType();
78
79 std::string get_mangleName() const { return mangleName; }
80 DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
81
82 std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
83
84 int get_scopeLevel() const { return scopeLevel; }
85 DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
86
87 ConstantExpr *get_asmName() const { return asmName; }
88 DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; }
89
90 std::list< Attribute * >& get_attributes() { return attributes; }
91 const std::list< Attribute * >& get_attributes() const { return attributes; }
92
93 Type::FuncSpecifiers get_funcSpec() const { return fs; }
94 //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
95
96 virtual DeclarationWithType *clone() const = 0;
97 virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
98
99 virtual Type *get_type() const = 0;
100 virtual void set_type(Type *) = 0;
101
102 private:
103 Type::FuncSpecifiers fs;
104};
105
106class ObjectDecl : public DeclarationWithType {
107 typedef DeclarationWithType Parent;
108 public:
109 Type *type;
110 Initializer *init;
111 Expression *bitfieldWidth;
112
113 ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
114 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
115 ObjectDecl( const ObjectDecl &other );
116 virtual ~ObjectDecl();
117
118 virtual Type *get_type() const { return type; }
119 virtual void set_type(Type *newType) { type = newType; }
120
121 Initializer *get_init() const { return init; }
122 void set_init( Initializer *newValue ) { init = newValue; }
123
124 Expression *get_bitfieldWidth() const { return bitfieldWidth; }
125 void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
126
127 virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
128 virtual void accept( Visitor &v ) { v.visit( this ); }
129 virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
130 virtual void print( std::ostream &os, int indent = 0 ) const;
131 virtual void printShort( std::ostream &os, int indent = 0 ) const;
132};
133
134class FunctionDecl : public DeclarationWithType {
135 typedef DeclarationWithType Parent;
136 public:
137 FunctionType *type;
138 CompoundStmt *statements;
139
140 FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
141 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
142 FunctionDecl( const FunctionDecl &other );
143 virtual ~FunctionDecl();
144
145 Type *get_type() const;
146 virtual void set_type(Type *);
147
148 FunctionType *get_functionType() const { return type; }
149 void set_functionType( FunctionType *newValue ) { type = newValue; }
150 CompoundStmt *get_statements() const { return statements; }
151 void set_statements( CompoundStmt *newValue ) { statements = newValue; }
152
153 virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
154 virtual void accept( Visitor &v ) { v.visit( this ); }
155 virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
156 virtual void print( std::ostream &os, int indent = 0 ) const;
157 virtual void printShort( std::ostream &os, int indent = 0 ) const;
158};
159
160class NamedTypeDecl : public Declaration {
161 typedef Declaration Parent;
162 public:
163 Type *base;
164 std::list< TypeDecl* > parameters;
165 std::list< DeclarationWithType* > assertions;
166
167 NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
168 NamedTypeDecl( const NamedTypeDecl &other );
169 virtual ~NamedTypeDecl();
170
171 Type *get_base() const { return base; }
172 void set_base( Type *newValue ) { base = newValue; }
173 std::list< TypeDecl* >& get_parameters() { return parameters; }
174 std::list< DeclarationWithType* >& get_assertions() { return assertions; }
175
176 virtual std::string typeString() const = 0;
177
178 virtual NamedTypeDecl *clone() const = 0;
179 virtual void print( std::ostream &os, int indent = 0 ) const;
180 virtual void printShort( std::ostream &os, int indent = 0 ) const;
181};
182
183class TypeDecl : public NamedTypeDecl {
184 typedef NamedTypeDecl Parent;
185 public:
186 enum Kind { Any, Dtype, Ftype, Ttype };
187
188 Type * init;
189 bool sized;
190
191 /// Data extracted from a type decl
192 struct Data {
193 TypeDecl::Kind kind;
194 bool isComplete;
195 Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
196 Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
197 Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
198 bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
199 bool operator!=(const Data & other) const { return !(*this == other);}
200 };
201
202 TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, Type * init = nullptr );
203 TypeDecl( const TypeDecl &other );
204 virtual ~TypeDecl();
205
206 Kind get_kind() const { return kind; }
207
208 Type * get_init() const { return init; }
209 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
210
211 bool isComplete() const { return kind == Any || sized; }
212 bool get_sized() const { return sized; }
213 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
214
215 virtual std::string typeString() const;
216 virtual std::string genTypeString() const;
217
218 virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
219 virtual void accept( Visitor &v ) { v.visit( this ); }
220 virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
221 virtual void print( std::ostream &os, int indent = 0 ) const;
222
223 private:
224 Kind kind;
225};
226
227class TypedefDecl : public NamedTypeDecl {
228 typedef NamedTypeDecl Parent;
229 public:
230 TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); }
231 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
232
233 virtual std::string typeString() const;
234
235 virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
236 virtual void accept( Visitor &v ) { v.visit( this ); }
237 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
238 private:
239};
240
241class AggregateDecl : public Declaration {
242 typedef Declaration Parent;
243 public:
244 std::list<Declaration*> members;
245 std::list<TypeDecl*> parameters;
246 bool body;
247 std::list< Attribute * > attributes;
248
249 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
250 AggregateDecl( const AggregateDecl &other );
251 virtual ~AggregateDecl();
252
253 std::list<Declaration*>& get_members() { return members; }
254 std::list<TypeDecl*>& get_parameters() { return parameters; }
255
256 std::list< Attribute * >& get_attributes() { return attributes; }
257 const std::list< Attribute * >& get_attributes() const { return attributes; }
258
259 bool has_body() const { return body; }
260 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
261
262 virtual void print( std::ostream &os, int indent = 0 ) const;
263 virtual void printShort( std::ostream &os, int indent = 0 ) const;
264 protected:
265 virtual std::string typeString() const = 0;
266};
267
268class StructDecl : public AggregateDecl {
269 typedef AggregateDecl Parent;
270 public:
271 StructDecl( const std::string &name, DeclarationNode::Aggregate kind = DeclarationNode::Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ), tagged( false ), parent_name( "" ) {}
272 StructDecl( const std::string &name, const std::string *parent, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( DeclarationNode::Struct ), tagged( true ), parent_name( parent ? *parent : "" ) {}
273 StructDecl( const StructDecl &other ) : Parent( other ) {}
274
275 bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
276 bool is_monitor() { return kind == DeclarationNode::Monitor; }
277 bool is_thread() { return kind == DeclarationNode::Thread; }
278
279 // Tagged/Tree Structure Excetion
280 bool get_tagged() { return tagged; }
281 void set_tagged( bool newValue ) { tagged = newValue; }
282 bool has_parent() { return parent_name != ""; }
283 std::string get_parentName() { return parent_name; }
284
285 virtual StructDecl *clone() const { return new StructDecl( *this ); }
286 virtual void accept( Visitor &v ) { v.visit( this ); }
287 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
288 private:
289 DeclarationNode::Aggregate kind;
290 virtual std::string typeString() const;
291
292 bool tagged;
293 std::string parent_name;
294};
295
296class UnionDecl : public AggregateDecl {
297 typedef AggregateDecl Parent;
298 public:
299 UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
300 UnionDecl( const UnionDecl &other ) : Parent( other ) {}
301
302 virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
303 virtual void accept( Visitor &v ) { v.visit( this ); }
304 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
305 private:
306 virtual std::string typeString() const;
307};
308
309class EnumDecl : public AggregateDecl {
310 typedef AggregateDecl Parent;
311 public:
312 EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
313 EnumDecl( const EnumDecl &other ) : Parent( other ) {}
314
315 virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
316 virtual void accept( Visitor &v ) { v.visit( this ); }
317 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
318 private:
319 virtual std::string typeString() const;
320};
321
322class TraitDecl : public AggregateDecl {
323 typedef AggregateDecl Parent;
324 public:
325 TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) {
326 assertf( attributes.empty(), "attribute unsupported for traits" );
327 }
328 TraitDecl( const TraitDecl &other ) : Parent( other ) {}
329
330 virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
331 virtual void accept( Visitor &v ) { v.visit( this ); }
332 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
333 private:
334 virtual std::string typeString() const;
335};
336
337class AsmDecl : public Declaration {
338 public:
339 AsmStmt *stmt;
340
341 AsmDecl( AsmStmt *stmt );
342 AsmDecl( const AsmDecl &other );
343 virtual ~AsmDecl();
344
345 AsmStmt *get_stmt() { return stmt; }
346 void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
347
348 virtual AsmDecl *clone() const { return new AsmDecl( *this ); }
349 virtual void accept( Visitor &v ) { v.visit( this ); }
350 virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
351 virtual void print( std::ostream &os, int indent = 0 ) const;
352 virtual void printShort( std::ostream &os, int indent = 0 ) const;
353};
354
355std::ostream & operator<<( std::ostream & out, const Declaration * decl );
356std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
357
358// Local Variables: //
359// tab-width: 4 //
360// mode: c++ //
361// compile-command: "make install" //
362// End: //
Note: See TracBrowser for help on using the repository browser.