source: src/SynTree/Declaration.h@ fde0a58

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since fde0a58 was 0c730d9, checked in by Henry Xue <y58xue@…>, 4 years ago

Translate exception declarations

  • Property mode set to 100644
File size: 18.2 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
148class FunctionDecl : public DeclarationWithType {
149 typedef DeclarationWithType Parent;
150 public:
151 FunctionType * type;
152 CompoundStmt * statements;
153 std::list< Expression * > withExprs;
154
155 FunctionDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType * type, CompoundStmt * statements,
156 const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
157 FunctionDecl( const FunctionDecl & other );
158 virtual ~FunctionDecl();
159
160 virtual Type * get_type() const override { return type; }
161 virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
162
163 FunctionType * get_functionType() const { return type; }
164 void set_functionType( FunctionType * newValue ) { type = newValue; }
165 CompoundStmt * get_statements() const { return statements; }
166 void set_statements( CompoundStmt * newValue ) { statements = newValue; }
167 bool has_body() const { return NULL != statements; }
168
169 static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
170
171 virtual FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
172 virtual void accept( Visitor & v ) override { v.visit( this ); }
173 virtual void accept( Visitor & v ) const override { v.visit( this ); }
174 virtual DeclarationWithType * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
175 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
176 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
177};
178
179class NamedTypeDecl : public Declaration {
180 typedef Declaration Parent;
181 public:
182 Type * base;
183 std::list< DeclarationWithType * > assertions;
184
185 NamedTypeDecl( const std::string & name, Type::StorageClasses scs, Type * type );
186 NamedTypeDecl( const NamedTypeDecl & other );
187 virtual ~NamedTypeDecl();
188
189 Type * get_base() const { return base; }
190 void set_base( Type * newValue ) { base = newValue; }
191 std::list< DeclarationWithType * >& get_assertions() { return assertions; }
192
193 virtual const char * typeString() const = 0;
194
195 virtual NamedTypeDecl * clone() const override = 0;
196 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
197 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
198};
199
200class TypeDecl : public NamedTypeDecl {
201 typedef NamedTypeDecl Parent;
202 public:
203 enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
204
205 Kind kind;
206 bool sized;
207 Type * init;
208
209 /// Data extracted from a type decl
210 struct Data {
211 Kind kind;
212 bool isComplete;
213
214 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
215 Data( const TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
216 Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
217 Data( const Data & d1, const Data & d2 )
218 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
219
220 bool operator==( const Data & other ) const { return kind == other.kind && isComplete == other.isComplete; }
221 bool operator!=( const Data & other ) const { return !(*this == other);}
222 };
223
224 TypeDecl( const std::string & name, Type::StorageClasses scs, Type * type, Kind kind, bool sized, Type * init = nullptr );
225 TypeDecl( const TypeDecl & other );
226 virtual ~TypeDecl();
227
228 Kind get_kind() const { return kind; }
229
230 Type * get_init() const { return init; }
231 TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
232
233 bool isComplete() const { return sized; }
234 bool get_sized() const { return sized; }
235 TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
236
237 virtual const char * typeString() const override;
238 virtual const char * genTypeString() const;
239
240 virtual TypeDecl * clone() const override { return new TypeDecl( *this ); }
241 virtual void accept( Visitor & v ) override { v.visit( this ); }
242 virtual void accept( Visitor & v ) const override { v.visit( this ); }
243 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
244 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
245};
246
247class TypedefDecl : public NamedTypeDecl {
248 typedef NamedTypeDecl Parent;
249 public:
250 TypedefDecl( const std::string & name, CodeLocation location, Type::StorageClasses scs, Type * type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
251 : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
252
253 TypedefDecl( const TypedefDecl & other ) : Parent( other ) {}
254
255 virtual const char * typeString() const override;
256
257 virtual TypedefDecl * clone() const override { return new TypedefDecl( *this ); }
258 virtual void accept( Visitor & v ) override { v.visit( this ); }
259 virtual void accept( Visitor & v ) const override { v.visit( this ); }
260 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
261 private:
262};
263
264class AggregateDecl : public Declaration {
265 typedef Declaration Parent;
266 public:
267 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
268 static const char * aggrString( Aggregate aggr );
269
270 std::list<Declaration*> members;
271 std::list<TypeDecl*> parameters;
272 bool body;
273 std::list< Attribute * > attributes;
274 AggregateDecl * parent = nullptr;
275
276 AggregateDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
277 AggregateDecl( const AggregateDecl & other );
278 virtual ~AggregateDecl();
279
280 std::list<Declaration*>& get_members() { return members; }
281 std::list<TypeDecl*>& get_parameters() { return parameters; }
282
283 std::list< Attribute * >& get_attributes() { return attributes; }
284 const std::list< Attribute * >& get_attributes() const { return attributes; }
285
286 bool has_body() const { return body; }
287 AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
288
289 virtual void print( std::ostream & os, Indenter indent = {} ) const override final;
290 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
291 protected:
292 virtual const char * typeString() const = 0;
293};
294
295class StructDecl : public AggregateDecl {
296 typedef AggregateDecl Parent;
297 public:
298 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 ) {}
299 StructDecl( const StructDecl & other ) : Parent( other ), kind( other.kind ) {}
300
301 bool is_coroutine() { return kind == Coroutine; }
302 bool is_exception() { return kind == Exception; }
303 bool is_generator() { return kind == Generator; }
304 bool is_monitor () { return kind == Monitor ; }
305 bool is_thread () { return kind == Thread ; }
306
307 // Make a type instance of this declaration.
308 StructInstType * makeInst( std::list< Expression * > const & parameters );
309 StructInstType * makeInst( std::list< Expression * > && parameters );
310
311 virtual StructDecl * clone() const override { return new StructDecl( *this ); }
312 virtual void accept( Visitor & v ) override { v.visit( this ); }
313 virtual void accept( Visitor & v ) const override { v.visit( this ); }
314 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
315 Aggregate kind;
316 private:
317 virtual const char * typeString() const override;
318};
319
320class UnionDecl : public AggregateDecl {
321 typedef AggregateDecl Parent;
322 public:
323 UnionDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
324 UnionDecl( const UnionDecl & other ) : Parent( other ) {}
325
326 virtual UnionDecl * clone() const override { return new UnionDecl( *this ); }
327 virtual void accept( Visitor & v ) override { v.visit( this ); }
328 virtual void accept( Visitor & v ) const override { v.visit( this ); }
329 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
330 private:
331 virtual const char * typeString() const override;
332};
333
334class EnumDecl : public AggregateDecl {
335 typedef AggregateDecl Parent;
336 public:
337 EnumDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
338 EnumDecl( const EnumDecl & other ) : Parent( other ) {}
339
340 bool valueOf( Declaration * enumerator, long long int & value );
341
342 virtual EnumDecl * clone() const override { return new EnumDecl( *this ); }
343 virtual void accept( Visitor & v ) override { v.visit( this ); }
344 virtual void accept( Visitor & v ) const override { v.visit( this ); }
345 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
346 private:
347 std::unordered_map< std::string, long long int > enumValues;
348 virtual const char * typeString() const override;
349};
350
351class TraitDecl : public AggregateDecl {
352 typedef AggregateDecl Parent;
353 public:
354 TraitDecl( const std::string & name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
355 assertf( attributes.empty(), "attribute unsupported for traits" );
356 }
357 TraitDecl( const TraitDecl & other ) : Parent( other ) {}
358
359 virtual TraitDecl * clone() const override { return new TraitDecl( *this ); }
360 virtual void accept( Visitor & v ) override { v.visit( this ); }
361 virtual void accept( Visitor & v ) const override { v.visit( this ); }
362 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
363 private:
364 virtual const char * typeString() const override;
365};
366
367class WithStmt : public Declaration {
368public:
369 std::list< Expression * > exprs;
370 Statement * stmt;
371
372 WithStmt( const std::list< Expression * > & exprs, Statement * stmt );
373 WithStmt( const WithStmt & other );
374 virtual ~WithStmt();
375
376 virtual WithStmt * clone() const override { return new WithStmt( *this ); }
377 virtual void accept( Visitor & v ) override { v.visit( this ); }
378 virtual void accept( Visitor & v ) const override { v.visit( this ); }
379 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
380 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
381 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override { print(os, indent); }
382};
383
384class AsmDecl : public Declaration {
385 public:
386 AsmStmt * stmt;
387
388 AsmDecl( AsmStmt * stmt );
389 AsmDecl( const AsmDecl & other );
390 virtual ~AsmDecl();
391
392 AsmStmt * get_stmt() { return stmt; }
393 void set_stmt( AsmStmt * newValue ) { stmt = newValue; }
394
395 virtual AsmDecl * clone() const override { return new AsmDecl( *this ); }
396 virtual void accept( Visitor & v ) override { v.visit( this ); }
397 virtual void accept( Visitor & v ) const override { v.visit( this ); }
398 virtual AsmDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
399 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
400 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
401};
402
403class DirectiveDecl : public Declaration {
404 public:
405 DirectiveStmt * stmt;
406
407 DirectiveDecl( DirectiveStmt * stmt );
408 DirectiveDecl( const DirectiveDecl & other );
409 virtual ~DirectiveDecl();
410
411 DirectiveStmt * get_stmt() { return stmt; }
412 void set_stmt( DirectiveStmt * newValue ) { stmt = newValue; }
413
414 virtual DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
415 virtual void accept( Visitor & v ) override { v.visit( this ); }
416 virtual void accept( Visitor & v ) const override { v.visit( this ); }
417 virtual DirectiveDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
418 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
419 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
420};
421
422class StaticAssertDecl : public Declaration {
423public:
424 Expression * condition;
425 ConstantExpr * message; // string literal
426
427 StaticAssertDecl( Expression * condition, ConstantExpr * message );
428 StaticAssertDecl( const StaticAssertDecl & other );
429 virtual ~StaticAssertDecl();
430
431 virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
432 virtual void accept( Visitor & v ) override { v.visit( this ); }
433 virtual void accept( Visitor & v ) const override { v.visit( this ); }
434 virtual StaticAssertDecl * acceptMutator( Mutator & m ) override { return m.mutate( this ); }
435 virtual void print( std::ostream & os, Indenter indent = {} ) const override;
436 virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
437};
438
439std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
440
441// Local Variables: //
442// tab-width: 4 //
443// mode: c++ //
444// compile-command: "make install" //
445// End: //
Note: See TracBrowser for help on using the repository browser.