source: src/AST/Decl.hpp@ 1ccae59

Last change on this file since 1ccae59 was a1da039, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Make all new declarations have a properly defined LinkageSpec. Also some general clean-up.

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