source: src/AST/Decl.hpp@ 3a513d89

ADT
Last change on this file since 3a513d89 was 561354f, checked in by JiadaL <j82liang@…>, 2 years ago

Save progress

  • Property mode set to 100644
File size: 15.9 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// Decl.hpp --
8//
9// Author : Aaron B. Moss
10// Created On : Thu May 9 10:00:00 2019
11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Apr 5 10:42:00 2023
13// Update Count : 35
14//
15
16#pragma once
17
18#include <iosfwd>
19#include <string> // for string, to_string
20#include <unordered_map>
21#include <vector>
22#include <algorithm>
23
24#include "FunctionSpec.hpp"
25#include "Fwd.hpp" // for UniqueId
26#include "LinkageSpec.hpp"
27#include "Node.hpp" // for ptr, readonly
28#include "ParseNode.hpp"
29#include "StorageClasses.hpp"
30#include "Visitor.hpp"
31#include "Common/utility.h"
32#include "Common/SemanticError.h" // error_str
33
34// Must be included in *all* AST classes; should be #undef'd at the end of the file
35#define MUTATE_FRIEND \
36 template<typename node_t> friend node_t * mutate(const node_t * node); \
37 template<typename node_t> friend node_t * shallowCopy(const node_t * node);
38
39namespace ast {
40
41/// Base declaration class
42class Decl : public ParseNode {
43public:
44 std::string name;
45 Storage::Classes storage;
46 Linkage::Spec linkage;
47 UniqueId uniqueId = 0;
48 bool extension = false;
49
50 Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
51 Linkage::Spec linkage )
52 : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
53
54 Decl* set_extension( bool ex ) { extension = ex; return this; }
55
56 /// Ensures this node has a unique ID
57 void fixUniqueId();
58 /// Get canonical declaration for unique ID
59 static readonly<Decl> fromId( UniqueId id );
60
61 const Decl * accept( Visitor & v ) const override = 0;
62private:
63 Decl * clone() const override = 0;
64 MUTATE_FRIEND
65};
66
67/// Typed declaration base class
68class DeclWithType : public Decl {
69public:
70 /// Represents the type with all types and typedefs expanded.
71 /// This field is generated by SymTab::Validate::Pass2
72 std::string mangleName;
73 /// Stores the scope level at which the variable was declared.
74 /// Used to access shadowed identifiers.
75 int scopeLevel = 0;
76
77 std::vector<ptr<Attribute>> attributes;
78 Function::Specs funcSpec;
79 ptr<Expr> asmName;
80 bool isDeleted = false;
81 bool isTypeFixed = false;
82
83 DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
84 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
85 : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
86 funcSpec(fs), asmName() {}
87
88 std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
89
90 /// Get type of this declaration. May be generated by subclass
91 virtual const Type * get_type() const = 0;
92 /// Set type of this declaration. May be verified by subclass
93 virtual void set_type( const Type * ) = 0;
94
95 const DeclWithType * accept( Visitor & v ) const override = 0;
96private:
97 DeclWithType * clone() const override = 0;
98 MUTATE_FRIEND
99};
100
101/// Object declaration `Foo foo = 42;`
102class ObjectDecl final : public DeclWithType {
103public:
104 ptr<Type> type;
105 ptr<Init> init;
106 ptr<Expr> bitfieldWidth;
107
108 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
109 const Init * init = nullptr, Storage::Classes storage = {},
110 Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr,
111 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
112 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
113 init( init ), bitfieldWidth( bitWd ) {}
114
115 const Type* get_type() const override { return type; }
116 void set_type( const Type * ty ) override { type = ty; }
117
118 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
119private:
120 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
121 MUTATE_FRIEND
122};
123
124/// Function variable arguments flag
125enum ArgumentFlag { FixedArgs, VariableArgs };
126
127/// Object declaration `int foo()`
128class FunctionDecl : public DeclWithType {
129public:
130 std::vector<ptr<TypeDecl>> type_params;
131 std::vector<ptr<DeclWithType>> assertions;
132 std::vector<ptr<DeclWithType>> params;
133 std::vector<ptr<DeclWithType>> returns;
134 // declared type, derived from parameter declarations
135 ptr<FunctionType> type;
136 /// Null for the forward declaration of a function.
137 ptr<CompoundStmt> stmts;
138 std::vector< ptr<Expr> > withExprs;
139
140 // The difference between the two constructors is in how they handle
141 // assertions. The first constructor uses the assertions from the type
142 // parameters, in the style of the old ast, and puts them on the type.
143 // The second takes an explicite list of assertions and builds a list of
144 // references to them on the type.
145
146 FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
147 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
148 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
149 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
150
151 FunctionDecl( const CodeLocation & location, const std::string & name,
152 std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
153 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
154 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
155 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
156
157 const Type * get_type() const override;
158 void set_type( const Type * t ) override;
159
160 bool has_body() const { return stmts; }
161
162 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
163private:
164 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
165 MUTATE_FRIEND
166};
167
168/// Base class for named type aliases
169class NamedTypeDecl : public Decl {
170public:
171 ptr<Type> base;
172 std::vector<ptr<DeclWithType>> assertions;
173
174 NamedTypeDecl(
175 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
176 const Type * b, Linkage::Spec spec = Linkage::Cforall )
177 : Decl( loc, name, storage, spec ), base( b ), assertions() {}
178
179 /// Produces a name for the kind of alias
180 virtual const char * typeString() const = 0;
181
182private:
183 NamedTypeDecl* clone() const override = 0;
184 MUTATE_FRIEND
185};
186
187/// Cforall type variable: `dtype T`
188class TypeDecl final : public NamedTypeDecl {
189 public:
190 enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
191
192 Kind kind;
193 bool sized;
194 ptr<Type> init;
195
196 TypeDecl(
197 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
198 const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
199 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
200 init( i ) {}
201
202 const char * typeString() const override;
203 /// Produces a name for generated code
204 const char * genTypeString() const;
205
206 /// convenience accessor to match Type::isComplete()
207 bool isComplete() const { return sized; }
208
209 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
210 private:
211 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
212 MUTATE_FRIEND
213};
214
215/// Data extracted from a TypeDecl.
216struct TypeData {
217 TypeDecl::Kind kind;
218 bool isComplete;
219
220 TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {}
221 TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
222 TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {}
223 TypeData( const TypeData & d1, const TypeData & d2 )
224 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
225
226 bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; }
227 bool operator!=( const TypeData & o ) const { return !(*this == o); }
228};
229
230std::ostream & operator<< ( std::ostream &, const TypeData & );
231
232/// C-style typedef `typedef Foo Bar`
233class TypedefDecl final : public NamedTypeDecl {
234public:
235 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
236 Type* b, Linkage::Spec spec = Linkage::Cforall )
237 : NamedTypeDecl( loc, name, storage, b, spec ) {}
238
239 const char * typeString() const override { return "typedef"; }
240
241 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
242private:
243 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
244 MUTATE_FRIEND
245};
246
247/// Aggregate type declaration base class
248class AggregateDecl : public Decl {
249public:
250 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, Adt };
251 static const char * aggrString( Aggregate aggr );
252
253 std::vector<ptr<Decl>> members;
254 std::vector<ptr<TypeDecl>> params;
255 std::vector<ptr<Attribute>> attributes;
256 bool body = false;
257 readonly<AggregateDecl> parent = {};
258
259 AggregateDecl( const CodeLocation& loc, const std::string& name,
260 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
261 : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
262 attributes( std::move(attrs) ) {}
263
264 AggregateDecl* set_body( bool b ) { body = b; return this; }
265
266 /// Produces a name for the kind of aggregate
267 virtual const char * typeString() const = 0;
268
269private:
270 AggregateDecl * clone() const override = 0;
271 MUTATE_FRIEND
272};
273
274/// struct declaration `struct Foo { ... };`
275class StructDecl final : public AggregateDecl {
276public:
277 Aggregate kind;
278
279 StructDecl( const CodeLocation& loc, const std::string& name,
280 Aggregate kind = Struct,
281 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
282 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
283
284 bool is_coroutine() const { return kind == Coroutine; }
285 bool is_generator() const { return kind == Generator; }
286 bool is_monitor () const { return kind == Monitor ; }
287 bool is_thread () const { return kind == Thread ; }
288 bool is_adt () const { return kind == Adt ; }
289
290 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
291
292 const char * typeString() const override { return aggrString( kind ); }
293
294private:
295 StructDecl * clone() const override { return new StructDecl{ *this }; }
296 MUTATE_FRIEND
297};
298
299/// union declaration `union Foo { ... };`
300class UnionDecl final : public AggregateDecl {
301public:
302 UnionDecl( const CodeLocation& loc, const std::string& name,
303 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
304 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
305
306 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
307
308 const char * typeString() const override { return aggrString( Union ); }
309
310private:
311 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
312 MUTATE_FRIEND
313};
314
315/// enum declaration `enum Foo { ... };`
316class EnumDecl final : public AggregateDecl {
317public:
318 bool isTyped; // isTyped indicated if the enum has a declaration like:
319 // enum (type_optional) Name {...}
320 ptr<Type> base; // if isTyped == true && base.get() == nullptr, it is a "void" type enum
321 enum class EnumHiding { Visible, Hide } hide;
322
323 EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
324 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
325 Type const * base = nullptr, EnumHiding hide = EnumHiding::Visible,
326 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
327 : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), enumValues(enumValues) {}
328
329 /// gets the integer value for this enumerator, returning true iff value found
330 // Maybe it is not used in producing the enum value
331 bool valueOf( const Decl * enumerator, long long& value ) const;
332
333 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
334
335 const char * typeString() const override { return aggrString( Enum ); }
336
337
338private:
339 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
340 MUTATE_FRIEND
341
342 /// Map from names to enumerator values; kept private for lazy initialization
343 mutable std::unordered_map< std::string, long long > enumValues;
344};
345
346class AdtDecl final : public AggregateDecl {
347public:
348 std::vector<ptr<StructDecl>> data_constructors; // Todo: members?
349 ptr<UnionDecl> data_union;
350 ptr<EnumDecl> tag;
351 ptr<StructDecl> tag_union;
352
353 AdtDecl( const CodeLocation& loc, const std::string& name,
354 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
355 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
356
357 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
358
359 const char * typeString() const override { return aggrString( Adt ); }
360
361private:
362 AdtDecl * clone() const override { return new AdtDecl{ *this }; }
363 MUTATE_FRIEND
364};
365
366/// trait declaration `trait Foo( ... ) { ... };`
367class TraitDecl final : public AggregateDecl {
368public:
369 TraitDecl( const CodeLocation& loc, const std::string& name,
370 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
371 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
372
373 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
374 const char * typeString() const override { return "trait"; }
375
376private:
377 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
378 MUTATE_FRIEND
379};
380
381/// With statement `with (...) ...`
382class WithStmt final : public Decl {
383public:
384 std::vector<ptr<Expr>> exprs;
385 ptr<Stmt> stmt;
386
387 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
388 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
389
390 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
391private:
392 WithStmt * clone() const override { return new WithStmt{ *this }; }
393 MUTATE_FRIEND
394};
395
396/// Assembly declaration: `asm ... ( "..." : ... )`
397class AsmDecl : public Decl {
398public:
399 ptr<AsmStmt> stmt;
400
401 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
402 : Decl( loc, "", {}, {} ), stmt(stmt) {}
403
404 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
405private:
406 AsmDecl * clone() const override { return new AsmDecl( *this ); }
407 MUTATE_FRIEND
408};
409
410/// C-preprocessor directive `#...`
411class DirectiveDecl : public Decl {
412public:
413 ptr<DirectiveStmt> stmt;
414
415 DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
416 : Decl( loc, "", {}, {} ), stmt(stmt) {}
417
418 const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); }
419private:
420 DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
421 MUTATE_FRIEND
422};
423
424/// Static Assertion `_Static_assert( ... , ... );`
425class StaticAssertDecl : public Decl {
426public:
427 ptr<Expr> cond;
428 ptr<ConstantExpr> msg; // string literal
429
430 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
431 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
432
433 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
434private:
435 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
436 MUTATE_FRIEND
437};
438
439/// Inline Member Declaration `inline TypeName;`
440class InlineMemberDecl final : public DeclWithType {
441public:
442 ptr<Type> type;
443
444 InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
445 Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
446 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
447 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ) {}
448
449 const Type * get_type() const override { return type; }
450 void set_type( const Type * ty ) override { type = ty; }
451
452 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
453private:
454 InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
455 MUTATE_FRIEND
456};
457
458}
459
460#undef MUTATE_FRIEND
461
462// Local Variables: //
463// tab-width: 4 //
464// mode: c++ //
465// compile-command: "make install" //
466// End: //
Note: See TracBrowser for help on using the repository browser.