source: src/SynTree/Declaration.h@ 59c7e3e

ADT ast-experimental
Last change on this file since 59c7e3e was b0d9ff7, checked in by JiadaL <j82liang@…>, 3 years ago

Fix up the QualifiedNameExpr. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

  • Property mode set to 100644
File size: 18.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 : Henry Xue
12// Last Modified On : Tue Jul 20 04:10:50 2021
13// Update Count : 160
14//
15
16#pragma once
17
18#include <cassert> // for assertf
19#include <iosfwd> // for ostream
20#include <list> // for list
21#include <unordered_map> // for unordered_map
22#include <string> // for string, operator+, allocator, to_string
23
24#include "BaseSyntaxNode.h" // for BaseSyntaxNode
25#include "Mutator.h" // for Mutator
26#include "LinkageSpec.h" // for Spec, Cforall
27#include "SynTree.h" // for UniqueId
28#include "SynTree/Type.h" // for Type, Type::StorageClasses, Type::Fu...
29#include "Visitor.h" // for Visitor
30
31class AsmStmt;
32class Attribute;
33class CompoundStmt;
34class ConstantExpr;
35class Expression;
36class Initializer;
37class TypeDecl;
38
39class Declaration : public BaseSyntaxNode {
40 public:
41 std::string name;
42 LinkageSpec::Spec linkage;
43 bool extension = false;
44
45 Declaration( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
46 Declaration( const Declaration & other );
47 virtual ~Declaration();
48
49 const std::string & get_name() const { return name; }
50 void set_name( std::string newValue ) { name = newValue; }
51
52 Type::StorageClasses get_storageClasses() const { return storageClasses; }
53
54 LinkageSpec::Spec get_linkage() const { return linkage; }
55 void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
56
57 UniqueId get_uniqueId() const { return uniqueId; }
58
59 bool get_extension() const { return extension; }
60 Declaration * set_extension( bool exten ) { extension = exten; return this; }
61
62 void fixUniqueId( void );
63 virtual Declaration * clone() const override = 0;
64 virtual void accept( Visitor & v ) override = 0;
65 virtual void accept( Visitor & v ) const override = 0;
66 virtual Declaration * acceptMutator( Mutator & m ) override = 0;
67 virtual void print( std::ostream & os, Indenter indent = {} ) const override = 0;
68 virtual void printShort( std::ostream & os, Indenter indent = {} ) const = 0;
69
70 UniqueId uniqueId;
71 Type::StorageClasses storageClasses;
72 private:
73};
74
75class DeclarationWithType : public Declaration {
76 public:
77 // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
78 std::string mangleName;
79 // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
80 int scopeLevel = 0;
81
82 Expression * asmName;
83 std::list< Attribute * > attributes;
84 bool isDeleted = false;
85
86 DeclarationWithType( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
87 DeclarationWithType( const DeclarationWithType & other );
88 virtual ~DeclarationWithType();
89
90 std::string get_mangleName() const { return mangleName; }
91 DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
92
93 std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
94
95 int get_scopeLevel() const { return scopeLevel; }
96 DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
97
98 Expression * get_asmName() const { return asmName; }
99 DeclarationWithType * set_asmName( Expression * newValue ) { asmName = newValue; return this; }
100
101 std::list< Attribute * >& get_attributes() { return attributes; }
102 const std::list< Attribute * >& get_attributes() const { return attributes; }
103
104 Type::FuncSpecifiers get_funcSpec() const { return fs; }
105 //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
106
107 virtual DeclarationWithType * clone() const override = 0;
108 virtual DeclarationWithType * acceptMutator( Mutator & m ) override = 0;
109
110 virtual Type * get_type() const = 0;
111 virtual void set_type(Type *) = 0;
112
113 private:
114 Type::FuncSpecifiers fs;
115};
116
117class ObjectDecl : public DeclarationWithType {
118 typedef DeclarationWithType Parent;
119 public:
120 Type * type;
121 Initializer * init;
122 Expression * bitfieldWidth;
123
124 ObjectDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression * bitfieldWidth, Type * type, Initializer * init,
125 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
126 ObjectDecl( const ObjectDecl & other );
127 virtual ~ObjectDecl();
128
129 virtual Type * get_type() const override { return type; }
130 virtual void set_type(Type * newType) override { type = newType; }
131
132 Initializer * get_init() const { return init; }
133 void set_init( Initializer * newValue ) { init = newValue; }
134
135 Expression * get_bitfieldWidth() const { return bitfieldWidth; }
136 void set_bitfieldWidth( Expression * newValue ) { bitfieldWidth = newValue; }
137
138 static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
139
140 virtual ObjectDecl * clone() const override { return new ObjectDecl( *this ); }
141 virtual void accept( Visitor & v ) override { v.visit( this ); }
142 virtual void accept( Visitor & v ) const override { v.visit( this ); }
143 virtual DeclarationWithType * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
144 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
145 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
146
147 void checkAssignedValue() const;
148};
149
150class FunctionDecl : public DeclarationWithType {
151 typedef DeclarationWithType Parent;
152 public:
153 FunctionType * type;
154 CompoundStmt * statements;
155 std::list< Expression * > withExprs;
156
157 FunctionDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType * type, CompoundStmt * statements,
158 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
159 FunctionDecl( const FunctionDecl & other );
160 virtual ~FunctionDecl();
161
162 virtual Type * get_type() const override { return type; }
163 virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
164
165 FunctionType * get_functionType() const { return type; }
166 void set_functionType( FunctionType * newValue ) { type = newValue; }
167 CompoundStmt * get_statements() const { return statements; }
168 void set_statements( CompoundStmt * newValue ) { statements = newValue; }
169 bool has_body() const { return NULL != statements; }
170
171 static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
172
173 virtual FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
174 virtual void accept( Visitor & v ) override { v.visit( this ); }
175 virtual void accept( Visitor & v ) const override { v.visit( this ); }
176 virtual DeclarationWithType * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
177 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
178 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
179};
180
181class NamedTypeDecl : public Declaration {
182 typedef Declaration Parent;
183 public:
184 Type * base;
185 std::list< DeclarationWithType * > assertions;
186
187 NamedTypeDecl( const std::string & name, Type::StorageClasses scs, Type * type );
188 NamedTypeDecl( const NamedTypeDecl & other );
189 virtual ~NamedTypeDecl();
190
191 Type * get_base() const { return base; }
192 void set_base( Type * newValue ) { base = newValue; }
193 std::list< DeclarationWithType * >& get_assertions() { return assertions; }
194
195 virtual const char * typeString() const = 0;
196
197 virtual NamedTypeDecl * clone() const override = 0;
198 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
199 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
200};
201
202class TypeDecl : public NamedTypeDecl {
203 typedef NamedTypeDecl Parent;
204 public:
205 enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
206
207 Kind kind;
208 bool sized;
209 Type * init;
210
211 /// Data extracted from a type decl
212 struct Data {
213 Kind kind;
214 bool isComplete;
215
216 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
217 Data( const TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
218 Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
219 Data( const Data & d1, const Data & d2 )
220 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
221
222 bool operator==( const Data & other ) const { return kind == other.kind && isComplete == other.isComplete; }
223 bool operator!=( const Data & other ) const { return !(*this == other);}
224 };
225
226 TypeDecl( const std::string & name, Type::StorageClasses scs, Type * type, Kind kind, bool sized, Type * init = nullptr );
227 TypeDecl( const TypeDecl & other );
228 virtual ~TypeDecl();
229
230 Kind get_kind() const { return kind; }
231
232 Type * get_init() const { return init; }
233 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
234
235 bool isComplete() const { return sized; }
236 bool get_sized() const { return sized; }
237 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
238
239 virtual const char * typeString() const override;
240 virtual const char * genTypeString() const;
241
242 virtual TypeDecl * clone() const override { return new TypeDecl( *this ); }
243 virtual void accept( Visitor & v ) override { v.visit( this ); }
244 virtual void accept( Visitor & v ) const override { v.visit( this ); }
245 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
246 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
247};
248
249class TypedefDecl : public NamedTypeDecl {
250 typedef NamedTypeDecl Parent;
251 public:
252 TypedefDecl( const std::string & name, CodeLocation location, Type::StorageClasses scs, Type * type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
253 : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
254
255 TypedefDecl( const TypedefDecl & other ) : Parent( other ) {}
256
257 virtual const char * typeString() const override;
258
259 virtual TypedefDecl * clone() const override { return new TypedefDecl( *this ); }
260 virtual void accept( Visitor & v ) override { v.visit( this ); }
261 virtual void accept( Visitor & v ) const override { v.visit( this ); }
262 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
263 private:
264};
265
266class AggregateDecl : public Declaration {
267 typedef Declaration Parent;
268 public:
269 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
270 static const char * aggrString( Aggregate aggr );
271
272 std::list<Declaration*> members;
273 std::list<TypeDecl*> parameters;
274 bool body;
275 std::list< Attribute * > attributes;
276 AggregateDecl * parent = nullptr;
277
278 AggregateDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
279 AggregateDecl( const AggregateDecl & other );
280 virtual ~AggregateDecl();
281
282 std::list<Declaration*>& get_members() { return members; }
283 std::list<TypeDecl*>& get_parameters() { return parameters; }
284
285 std::list< Attribute * >& get_attributes() { return attributes; }
286 const std::list< Attribute * >& get_attributes() const { return attributes; }
287
288 bool has_body() const { return body; }
289 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
290
291 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
292 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
293 protected:
294 virtual const char * typeString() const = 0;
295};
296
297class StructDecl : public AggregateDecl {
298 typedef AggregateDecl Parent;
299 public:
300 StructDecl( const std::string & name, Aggregate kind = Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ) {}
301 StructDecl( const StructDecl & other ) : Parent( other ), kind( other.kind ) {}
302
303 bool is_coroutine() { return kind == Coroutine; }
304 bool is_exception() { return kind == Exception; }
305 bool is_generator() { return kind == Generator; }
306 bool is_monitor () { return kind == Monitor ; }
307 bool is_thread () { return kind == Thread ; }
308
309 // Make a type instance of this declaration.
310 StructInstType * makeInst( std::list< Expression * > const & parameters );
311 StructInstType * makeInst( std::list< Expression * > && parameters );
312
313 virtual StructDecl * clone() const override { return new StructDecl( *this ); }
314 virtual void accept( Visitor & v ) override { v.visit( this ); }
315 virtual void accept( Visitor & v ) const override { v.visit( this ); }
316 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
317 Aggregate kind;
318 private:
319 virtual const char * typeString() const override;
320};
321
322class UnionDecl : public AggregateDecl {
323 typedef AggregateDecl Parent;
324 public:
325 UnionDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
326 UnionDecl( const UnionDecl & other ) : Parent( other ) {}
327
328 virtual UnionDecl * clone() const override { return new UnionDecl( *this ); }
329 virtual void accept( Visitor & v ) override { v.visit( this ); }
330 virtual void accept( Visitor & v ) const override { v.visit( this ); }
331 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
332 private:
333 virtual const char * typeString() const override;
334};
335
336class EnumDecl : public AggregateDecl {
337 typedef AggregateDecl Parent;
338 public:
339 bool isTyped;
340 Type * base;
341
342 EnumDecl( const std::string & name,
343 const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
344 bool isTyped = false, LinkageSpec::Spec linkage = LinkageSpec::Cforall,
345 Type * baseType = nullptr )
346 : Parent( name, attributes, linkage ),isTyped(isTyped), base( baseType ) {}
347 EnumDecl( const EnumDecl & other )
348 : Parent( other ), isTyped( other.isTyped), base( other.base ) {}
349 bool valueOf( Declaration * enumerator, long long int & value );
350 virtual EnumDecl * clone() const override { return new EnumDecl( *this ); }
351 virtual void accept( Visitor & v ) override { v.visit( this ); }
352 virtual void accept( Visitor & v ) const override { v.visit( this ); }
353 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
354
355 std::unordered_map< std::string, long long int > enumValues; // This attribute is unused
356 virtual void print( std::ostream & os, Indenter indent = {} ) const override final;
357 private:
358 // std::unordered_map< std::string, long long int > enumValues;
359 virtual const char * typeString() const override;
360};
361
362class TraitDecl : public AggregateDecl {
363 typedef AggregateDecl Parent;
364 public:
365 TraitDecl( const std::string & name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
366 assertf( attributes.empty(), "attribute unsupported for traits" );
367 }
368 TraitDecl( const TraitDecl & other ) : Parent( other ) {}
369
370 virtual TraitDecl * clone() const override { return new TraitDecl( *this ); }
371 virtual void accept( Visitor & v ) override { v.visit( this ); }
372 virtual void accept( Visitor & v ) const override { v.visit( this ); }
373 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
374 private:
375 virtual const char * typeString() const override;
376};
377
378class WithStmt : public Declaration {
379public:
380 std::list< Expression * > exprs;
381 Statement * stmt;
382
383 WithStmt( const std::list< Expression * > & exprs, Statement * stmt );
384 WithStmt( const WithStmt & other );
385 virtual ~WithStmt();
386
387 virtual WithStmt * clone() const override { return new WithStmt( *this ); }
388 virtual void accept( Visitor & v ) override { v.visit( this ); }
389 virtual void accept( Visitor & v ) const override { v.visit( this ); }
390 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
391 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
392 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override { print(os, indent); }
393};
394
395class AsmDecl : public Declaration {
396 public:
397 AsmStmt * stmt;
398
399 AsmDecl( AsmStmt * stmt );
400 AsmDecl( const AsmDecl & other );
401 virtual ~AsmDecl();
402
403 AsmStmt * get_stmt() { return stmt; }
404 void set_stmt( AsmStmt * newValue ) { stmt = newValue; }
405
406 virtual AsmDecl * clone() const override { return new AsmDecl( *this ); }
407 virtual void accept( Visitor & v ) override { v.visit( this ); }
408 virtual void accept( Visitor & v ) const override { v.visit( this ); }
409 virtual AsmDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
410 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
411 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
412};
413
414class DirectiveDecl : public Declaration {
415 public:
416 DirectiveStmt * stmt;
417
418 DirectiveDecl( DirectiveStmt * stmt );
419 DirectiveDecl( const DirectiveDecl & other );
420 virtual ~DirectiveDecl();
421
422 DirectiveStmt * get_stmt() { return stmt; }
423 void set_stmt( DirectiveStmt * newValue ) { stmt = newValue; }
424
425 virtual DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
426 virtual void accept( Visitor & v ) override { v.visit( this ); }
427 virtual void accept( Visitor & v ) const override { v.visit( this ); }
428 virtual DirectiveDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
429 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
430 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
431};
432
433class StaticAssertDecl : public Declaration {
434public:
435 Expression * condition;
436 ConstantExpr * message; // string literal
437
438 StaticAssertDecl( Expression * condition, ConstantExpr * message );
439 StaticAssertDecl( const StaticAssertDecl & other );
440 virtual ~StaticAssertDecl();
441
442 virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
443 virtual void accept( Visitor & v ) override { v.visit( this ); }
444 virtual void accept( Visitor & v ) const override { v.visit( this ); }
445 virtual StaticAssertDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
446 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
447 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
448};
449
450std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
451
452// Local Variables: //
453// tab-width: 4 //
454// mode: c++ //
455// compile-command: "make install" //
456// End: //
Note: See TracBrowser for help on using the repository browser.