source: src/AST/Decl.hpp@ dd900b5

Last change on this file since dd900b5 was 37273c8, checked in by Andrew Beach <ajbeach@…>, 23 months ago

Removed the old-ast-compatable FunctionDecl constructor. However, enough cases pass nothing polymorphic along some of the uses of the constructor now go to a new monomorphic function constructor.

  • Property mode set to 100644
File size: 15.0 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
59 const Decl * accept( Visitor & v ) const override = 0;
60private:
61 Decl * clone() const override = 0;
62 MUTATE_FRIEND
63};
64
65/// Typed declaration base class
66class DeclWithType : public Decl {
67public:
68 /// Represents the type with all types and typedefs expanded.
69 std::string mangleName;
70 /// Stores the scope level at which the variable was declared.
71 /// Used to access shadowed identifiers.
72 int scopeLevel = 0;
73
74 std::vector<ptr<Attribute>> attributes;
75 Function::Specs funcSpec;
76 ptr<Expr> asmName;
77 bool isDeleted = false;
78 bool isTypeFixed = false;
79
80 DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
81 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
82 : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
83 funcSpec(fs), asmName() {}
84
85 std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
86
87 /// Get type of this declaration. May be generated by subclass
88 virtual const Type * get_type() const = 0;
89 /// Set type of this declaration. May be verified by subclass
90 virtual void set_type( const Type * ) = 0;
91
92 const DeclWithType * accept( Visitor & v ) const override = 0;
93private:
94 DeclWithType * clone() const override = 0;
95 MUTATE_FRIEND
96};
97
98/// Object declaration `Foo foo = 42;`
99class ObjectDecl final : public DeclWithType {
100public:
101 ptr<Type> type;
102 ptr<Init> init;
103 ptr<Expr> bitfieldWidth;
104
105 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
106 const Init * init = nullptr, Storage::Classes storage = {},
107 Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr,
108 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
109 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
110 init( init ), bitfieldWidth( bitWd ) {}
111
112 const Type* get_type() const override { return type; }
113 void set_type( const Type * ty ) override { type = ty; }
114
115 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
116private:
117 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
118 MUTATE_FRIEND
119};
120
121/// Function variable arguments flag
122enum ArgumentFlag { FixedArgs, VariableArgs };
123
124/// Object declaration `int foo()`
125class FunctionDecl final : public DeclWithType {
126public:
127 std::vector<ptr<TypeDecl>> type_params;
128 std::vector<ptr<DeclWithType>> assertions;
129 std::vector<ptr<DeclWithType>> params;
130 std::vector<ptr<DeclWithType>> returns;
131 // declared type, derived from parameter declarations
132 ptr<FunctionType> type;
133 /// Null for the forward declaration of a function.
134 ptr<CompoundStmt> stmts;
135 std::vector< ptr<Expr> > withExprs;
136
137 /// Monomorphic Function Constructor:
138 FunctionDecl( const CodeLocation & locaction, const std::string & name,
139 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
140 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
141 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
142
143 /// Polymorphic Function Constructor:
144 FunctionDecl( const CodeLocation & location, const std::string & name,
145 std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
146 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
147 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
148 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
149
150 const Type * get_type() const override;
151 void set_type( const Type * t ) override;
152
153 bool has_body() const { return stmts; }
154
155 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
156private:
157 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
158 MUTATE_FRIEND
159};
160
161/// Base class for named type aliases
162class NamedTypeDecl : public Decl {
163public:
164 ptr<Type> base;
165 std::vector<ptr<DeclWithType>> assertions;
166
167 NamedTypeDecl(
168 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
169 const Type * b, Linkage::Spec spec = Linkage::Cforall )
170 : Decl( loc, name, storage, spec ), base( b ), assertions() {}
171
172 /// Produces a name for the kind of alias
173 virtual const char * typeString() const = 0;
174
175private:
176 NamedTypeDecl* clone() const override = 0;
177 MUTATE_FRIEND
178};
179
180/// Cforall type variable: `dtype T`
181class TypeDecl final : public NamedTypeDecl {
182 public:
183 enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
184
185 Kind kind;
186 bool sized;
187 ptr<Type> init;
188
189 TypeDecl(
190 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
191 const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
192 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
193 init( i ) {}
194
195 const char * typeString() const override;
196 /// Produces a name for generated code
197 const char * genTypeString() const;
198
199 /// convenience accessor to match Type::isComplete()
200 bool isComplete() const { return sized; }
201
202 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
203 private:
204 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
205 MUTATE_FRIEND
206};
207
208/// Data extracted from a TypeDecl.
209struct TypeData {
210 TypeDecl::Kind kind;
211 bool isComplete;
212
213 TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {}
214 TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
215 TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {}
216 TypeData( const TypeData & d1, const TypeData & d2 )
217 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
218
219 bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; }
220 bool operator!=( const TypeData & o ) const { return !(*this == o); }
221};
222
223std::ostream & operator<< ( std::ostream &, const TypeData & );
224
225/// C-style typedef `typedef Foo Bar`
226class TypedefDecl final : public NamedTypeDecl {
227public:
228 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
229 Type* b, Linkage::Spec spec = Linkage::Cforall )
230 : NamedTypeDecl( loc, name, storage, b, spec ) {}
231
232 const char * typeString() const override { return "typedef"; }
233
234 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
235private:
236 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
237 MUTATE_FRIEND
238};
239
240/// Aggregate type declaration base class
241class AggregateDecl : public Decl {
242public:
243 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
244 static const char * aggrString( Aggregate aggr );
245
246 std::vector<ptr<Decl>> members;
247 std::vector<ptr<TypeDecl>> params;
248 std::vector<ptr<Attribute>> attributes;
249 bool body = false;
250 readonly<AggregateDecl> parent = {};
251
252 AggregateDecl( const CodeLocation& loc, const std::string& name,
253 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
254 : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
255 attributes( std::move(attrs) ) {}
256
257 AggregateDecl* set_body( bool b ) { body = b; return this; }
258
259 /// Produces a name for the kind of aggregate
260 virtual const char * typeString() const = 0;
261
262private:
263 AggregateDecl * clone() const override = 0;
264 MUTATE_FRIEND
265};
266
267/// struct declaration `struct Foo { ... };`
268class StructDecl final : public AggregateDecl {
269public:
270 Aggregate kind;
271
272 StructDecl( const CodeLocation& loc, const std::string& name,
273 Aggregate kind = Struct,
274 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
275 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
276
277 bool is_coroutine() const { return kind == Coroutine; }
278 bool is_generator() const { return kind == Generator; }
279 bool is_monitor () const { return kind == Monitor ; }
280 bool is_thread () const { return kind == Thread ; }
281
282 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
283
284 const char * typeString() const override { return aggrString( kind ); }
285
286private:
287 StructDecl * clone() const override { return new StructDecl{ *this }; }
288 MUTATE_FRIEND
289};
290
291/// union declaration `union Foo { ... };`
292class UnionDecl final : public AggregateDecl {
293public:
294 UnionDecl( const CodeLocation& loc, const std::string& name,
295 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
296 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
297
298 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
299
300 const char * typeString() const override { return aggrString( Union ); }
301
302private:
303 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
304 MUTATE_FRIEND
305};
306
307/// enum declaration `enum Foo { ... };`
308class EnumDecl final : public AggregateDecl {
309public:
310 // isTyped indicated if the enum has a declaration like:
311 // enum (type_optional) Name {...}
312 bool isTyped;
313 // if isTyped == true && base.get() == nullptr, it is a "void" type enum
314 ptr<Type> base;
315 enum class EnumHiding { Visible, Hide } hide;
316
317 EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
318 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
319 Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide,
320 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
321 : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), enumValues(enumValues) {}
322
323 /// gets the integer value for this enumerator, returning true iff value found
324 // Maybe it is not used in producing the enum value
325 bool valueOf( const Decl * enumerator, long long& value ) const;
326
327 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
328
329 const char * typeString() const override { return aggrString( Enum ); }
330
331
332private:
333 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
334 MUTATE_FRIEND
335
336 /// Map from names to enumerator values; kept private for lazy initialization
337 mutable std::unordered_map< std::string, long long > enumValues;
338};
339
340/// trait declaration `trait Foo( ... ) { ... };`
341class TraitDecl final : public AggregateDecl {
342public:
343 TraitDecl( const CodeLocation& loc, const std::string& name,
344 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
345 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
346
347 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
348
349 const char * typeString() const override { return "trait"; }
350
351private:
352 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
353 MUTATE_FRIEND
354};
355
356/// With statement `with (...) ...`
357/// This is a statement lexically, but a Decl is needed for the SymbolTable.
358class WithStmt final : public Decl {
359public:
360 std::vector<ptr<Expr>> exprs;
361 ptr<Stmt> stmt;
362
363 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
364 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
365
366 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
367private:
368 WithStmt * clone() const override { return new WithStmt{ *this }; }
369 MUTATE_FRIEND
370};
371
372/// Assembly declaration: `asm ... ( "..." : ... )`
373class AsmDecl final : public Decl {
374public:
375 ptr<AsmStmt> stmt;
376
377 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
378 : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
379
380 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
381private:
382 AsmDecl * clone() const override { return new AsmDecl( *this ); }
383 MUTATE_FRIEND
384};
385
386/// C-preprocessor directive `#...`
387class DirectiveDecl final : public Decl {
388public:
389 ptr<DirectiveStmt> stmt;
390
391 DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
392 : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
393
394 const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); }
395private:
396 DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
397 MUTATE_FRIEND
398};
399
400/// Static Assertion `_Static_assert( ... , ... );`
401class StaticAssertDecl final : public Decl {
402public:
403 ptr<Expr> cond;
404 ptr<ConstantExpr> msg; // string literal
405
406 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
407 : Decl( loc, "", {}, Linkage::C ), cond( condition ), msg( msg ) {}
408
409 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
410private:
411 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
412 MUTATE_FRIEND
413};
414
415/// Inline Member Declaration `inline TypeName;`
416class InlineMemberDecl final : public DeclWithType {
417public:
418 ptr<Type> type;
419
420 InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
421 Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
422 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
423 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ) {}
424
425 const Type * get_type() const override { return type; }
426 void set_type( const Type * ty ) override { type = ty; }
427
428 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
429private:
430 InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
431 MUTATE_FRIEND
432};
433
434}
435
436#undef MUTATE_FRIEND
437
438// Local Variables: //
439// tab-width: 4 //
440// mode: c++ //
441// compile-command: "make install" //
442// End: //
Note: See TracBrowser for help on using the repository browser.