source: src/SynTree/Declaration.h@ f31cb3e

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 f31cb3e was 294647b, checked in by Thierry Delisle <tdelisle@…>, 9 years ago

Filename and linenumber handling for parsing errors

  • Property mode set to 100644
File size: 12.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 : Peter A. Buhr
12// Last Modified On : Thu Feb 9 14:27:08 2017
13// Update Count : 56
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 bool get_isInline() const { return isInline; }
41 void set_isInline( bool newValue ) { isInline = newValue; }
42 bool get_isNoreturn() const { return isNoreturn; }
43 void set_isNoreturn( bool newValue ) { isNoreturn = newValue; }
44 UniqueId get_uniqueId() const { return uniqueId; }
45 bool get_extension() const { return extension; }
46 Declaration *set_extension( bool exten ) { extension = exten; return this; }
47
48 void fixUniqueId( void );
49 virtual Declaration *clone() const = 0;
50 virtual void accept( Visitor &v ) = 0;
51 virtual Declaration *acceptMutator( Mutator &m ) = 0;
52 virtual void print( std::ostream &os, int indent = 0 ) const = 0;
53 virtual void printShort( std::ostream &os, int indent = 0 ) const = 0;
54
55 static void dumpIds( std::ostream &os );
56 static Declaration *declFromId( UniqueId id );
57 private:
58 std::string name;
59 DeclarationNode::StorageClass storageClass;
60 LinkageSpec::Spec linkage;
61 bool isInline, isNoreturn;
62 UniqueId uniqueId;
63 bool extension = false;
64};
65
66class DeclarationWithType : public Declaration {
67 public:
68 DeclarationWithType( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes );
69 DeclarationWithType( const DeclarationWithType &other );
70 virtual ~DeclarationWithType();
71
72 std::string get_mangleName() const { return mangleName; }
73 DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
74
75 std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
76
77 int get_scopeLevel() const { return scopeLevel; }
78 DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
79
80 ConstantExpr *get_asmName() const { return asmName; }
81 DeclarationWithType * set_asmName( ConstantExpr *newValue ) { asmName = newValue; return this; }
82
83 std::list< Attribute * >& get_attributes() { return attributes; }
84 const std::list< Attribute * >& get_attributes() const { return attributes; }
85
86 virtual DeclarationWithType *clone() const = 0;
87 virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
88
89 virtual Type *get_type() const = 0;
90 virtual void set_type(Type *) = 0;
91 private:
92 // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
93 std::string mangleName;
94 // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
95 int scopeLevel = 0;
96
97 ConstantExpr *asmName;
98 std::list< Attribute * > attributes;
99};
100
101class ObjectDecl : public DeclarationWithType {
102 typedef DeclarationWithType Parent;
103 public:
104 ObjectDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init, const std::list< Attribute * > attributes = std::list< Attribute * >(), bool isInline = false, bool isNoreturn = false );
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, bool isInline, bool isNoreturn, const std::list< Attribute * > attributes = std::list< Attribute * >() );
132 FunctionDecl( const FunctionDecl &other );
133 virtual ~FunctionDecl();
134
135 Type *get_type() const;
136 virtual void set_type(Type *);
137
138 FunctionType *get_functionType() const { return type; }
139 void set_functionType( FunctionType *newValue ) { type = newValue; }
140 CompoundStmt *get_statements() const { return statements; }
141 void set_statements( CompoundStmt *newValue ) { statements = newValue; }
142 std::list< std::string >& get_oldIdents() { return oldIdents; }
143 std::list< Declaration* >& get_oldDecls() { return oldDecls; }
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};
156
157class NamedTypeDecl : public Declaration {
158 typedef Declaration Parent;
159 public:
160 NamedTypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type );
161 NamedTypeDecl( const NamedTypeDecl &other );
162 virtual ~NamedTypeDecl();
163
164 Type *get_base() const { return base; }
165 void set_base( Type *newValue ) { base = newValue; }
166 std::list< TypeDecl* >& get_parameters() { return parameters; }
167 std::list< DeclarationWithType* >& get_assertions() { return assertions; }
168
169 virtual NamedTypeDecl *clone() const = 0;
170 virtual void print( std::ostream &os, int indent = 0 ) const;
171 virtual void printShort( std::ostream &os, int indent = 0 ) const;
172 protected:
173 virtual std::string typeString() const = 0;
174 private:
175 Type *base;
176 std::list< TypeDecl* > parameters;
177 std::list< DeclarationWithType* > assertions;
178};
179
180class TypeDecl : public NamedTypeDecl {
181 typedef NamedTypeDecl Parent;
182 public:
183 enum Kind { Any, Dtype, Ftype, Ttype };
184 /// Data extracted from a type decl
185 struct Data {
186 TypeDecl::Kind kind;
187 bool isComplete;
188 Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
189 Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
190 Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
191 bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
192 bool operator!=(const Data & other) const { return !(*this == other);}
193 };
194
195 TypeDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type, Kind kind );
196 TypeDecl( const TypeDecl &other );
197
198 Kind get_kind() const { return kind; }
199
200 bool isComplete() const { return kind == Any || sized; }
201 bool get_sized() const { return sized; }
202 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
203
204 virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
205 virtual void accept( Visitor &v ) { v.visit( this ); }
206 virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
207 private:
208 virtual std::string typeString() const;
209 Kind kind;
210 bool sized;
211};
212
213class TypedefDecl : public NamedTypeDecl {
214 typedef NamedTypeDecl Parent;
215 public:
216 TypedefDecl( const std::string &name, DeclarationNode::StorageClass sc, Type *type ) : Parent( name, sc, type ) {}
217 TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
218
219 virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
220 virtual void accept( Visitor &v ) { v.visit( this ); }
221 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
222 private:
223 virtual std::string typeString() const;
224};
225
226class AggregateDecl : public Declaration {
227 typedef Declaration Parent;
228 public:
229 AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() );
230 AggregateDecl( const AggregateDecl &other );
231 virtual ~AggregateDecl();
232
233 std::list<Declaration*>& get_members() { return members; }
234 std::list<TypeDecl*>& get_parameters() { return parameters; }
235
236 std::list< Attribute * >& get_attributes() { return attributes; }
237 const std::list< Attribute * >& get_attributes() const { return attributes; }
238
239 bool has_body() const { return body; }
240 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
241
242 virtual void print( std::ostream &os, int indent = 0 ) const;
243 virtual void printShort( std::ostream &os, int indent = 0 ) const;
244 protected:
245 virtual std::string typeString() const = 0;
246
247 private:
248 std::list<Declaration*> members;
249 std::list<TypeDecl*> parameters;
250 bool body;
251 std::list< Attribute * > attributes;
252};
253
254class StructDecl : public AggregateDecl {
255 typedef AggregateDecl Parent;
256 public:
257 StructDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {}
258 StructDecl( const StructDecl &other ) : Parent( other ) {}
259
260 virtual StructDecl *clone() const { return new StructDecl( *this ); }
261 virtual void accept( Visitor &v ) { v.visit( this ); }
262 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
263 private:
264 virtual std::string typeString() const;
265};
266
267class UnionDecl : public AggregateDecl {
268 typedef AggregateDecl Parent;
269 public:
270 UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {}
271 UnionDecl( const UnionDecl &other ) : Parent( other ) {}
272
273 virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
274 virtual void accept( Visitor &v ) { v.visit( this ); }
275 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
276 private:
277 virtual std::string typeString() const;
278};
279
280class EnumDecl : public AggregateDecl {
281 typedef AggregateDecl Parent;
282 public:
283 EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >() ) : Parent( name, attributes ) {}
284 EnumDecl( const EnumDecl &other ) : Parent( other ) {}
285
286 virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
287 virtual void accept( Visitor &v ) { v.visit( this ); }
288 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
289 private:
290 virtual std::string typeString() const;
291};
292
293class TraitDecl : public AggregateDecl {
294 typedef AggregateDecl Parent;
295 public:
296 TraitDecl( const std::string &name, const std::list< Attribute * > & attributes ) : Parent( name ) {
297 assertf( attributes.empty(), "attribute unsupported for traits" );
298 }
299 TraitDecl( const TraitDecl &other ) : Parent( other ) {}
300
301 virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
302 virtual void accept( Visitor &v ) { v.visit( this ); }
303 virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
304 private:
305 virtual std::string typeString() const;
306};
307
308class AsmDecl : public Declaration {
309 public:
310 AsmDecl( AsmStmt *stmt );
311 AsmDecl( const AsmDecl &other );
312 virtual ~AsmDecl();
313
314 AsmStmt *get_stmt() { return stmt; }
315 void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
316
317 virtual AsmDecl *clone() const { return new AsmDecl( *this ); }
318 virtual void accept( Visitor &v ) { v.visit( this ); }
319 virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
320 virtual void print( std::ostream &os, int indent = 0 ) const;
321 virtual void printShort( std::ostream &os, int indent = 0 ) const;
322 private:
323 AsmStmt *stmt;
324};
325
326std::ostream & operator<<( std::ostream & out, const Declaration * decl );
327std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
328
329#endif // DECLARATION_H
330
331// Local Variables: //
332// tab-width: 4 //
333// mode: c++ //
334// compile-command: "make install" //
335// End: //
Note: See TracBrowser for help on using the repository browser.