source: src/SynTree/Declaration.h@ 2988eeb

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 2988eeb was dd020c0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

first attempt to create function specifiers

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