source: src/AST/Decl.hpp@ b28ce93

Last change on this file since b28ce93 was 90e683b, checked in by Andrew Beach <ajbeach@…>, 8 months ago

I set out to do a enum rework. It ended up being much the same and I unwound the core rework. But I hope the new names are a bit clearer and other minor fixes are helpful, so I am keeping those.

  • Property mode set to 100644
File size: 15.2 KB
RevLine 
[2bb4a01]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
[7edd5c1]11// Last Modified By : Andrew Beach
[3e94a23]12// Last Modified On : Wed Apr 5 10:42:00 2023
13// Update Count : 35
[2bb4a01]14//
15
16#pragma once
17
[d76c588]18#include <iosfwd>
[a300e4a]19#include <string> // for string, to_string
20#include <unordered_map>
[2bb4a01]21#include <vector>
[07de76b]22#include <algorithm>
[2bb4a01]23
[a300e4a]24#include "FunctionSpec.hpp"
[2bb4a01]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
[f3cc5b6]32// Must be included in *all* AST classes; should be #undef'd at the end of the file
[99da267]33#define MUTATE_FRIEND \
[91a72ef]34 template<typename node_t> friend node_t * mutate(const node_t * node); \
[99da267]35 template<typename node_t> friend node_t * shallowCopy(const node_t * node);
[f3cc5b6]36
[2bb4a01]37namespace ast {
[a300e4a]38
[2bb4a01]39/// Base declaration class
40class Decl : public ParseNode {
41public:
42 std::string name;
43 Storage::Classes storage;
44 Linkage::Spec linkage;
45 UniqueId uniqueId = 0;
46 bool extension = false;
47
[14cebb7a]48 Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]49 Linkage::Spec linkage )
50 : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
51
52 /// Ensures this node has a unique ID
53 void fixUniqueId();
54
[23f99e1]55 const Decl * accept( Visitor & v ) const override = 0;
[2bb4a01]56private:
[23f99e1]57 Decl * clone() const override = 0;
[f3cc5b6]58 MUTATE_FRIEND
[2bb4a01]59};
60
61/// Typed declaration base class
62class DeclWithType : public Decl {
63public:
64 /// Represents the type with all types and typedefs expanded.
65 std::string mangleName;
[14cebb7a]66 /// Stores the scope level at which the variable was declared.
[2bb4a01]67 /// Used to access shadowed identifiers.
68 int scopeLevel = 0;
69
70 std::vector<ptr<Attribute>> attributes;
[a300e4a]71 Function::Specs funcSpec;
[2bb4a01]72 ptr<Expr> asmName;
73 bool isDeleted = false;
[16ba4a6f]74 bool isTypeFixed = false;
[85855b0]75 bool isHidden = false;
[956b389]76 bool isMember = false;
[2bb4a01]77
[14cebb7a]78 DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]79 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
[14cebb7a]80 : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
[a300e4a]81 funcSpec(fs), asmName() {}
[14cebb7a]82
[a300e4a]83 std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
84
85 /// Get type of this declaration. May be generated by subclass
[6d51bd7]86 virtual const Type * get_type() const = 0;
[a300e4a]87 /// Set type of this declaration. May be verified by subclass
[e0e9a0b]88 virtual void set_type( const Type * ) = 0;
[a300e4a]89
[23f99e1]90 const DeclWithType * accept( Visitor & v ) const override = 0;
[a300e4a]91private:
[23f99e1]92 DeclWithType * clone() const override = 0;
[f3cc5b6]93 MUTATE_FRIEND
[a300e4a]94};
95
[77a3f41]96/// Object declaration `Foo foo = 42;`
97class ObjectDecl final : public DeclWithType {
98public:
99 ptr<Type> type;
100 ptr<Init> init;
101 ptr<Expr> bitfieldWidth;
102
[e67991f]103 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
104 const Init * init = nullptr, Storage::Classes storage = {},
[e8616b6]105 Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr,
[2a8f0c1]106 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
[14cebb7a]107 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
[77a3f41]108 init( init ), bitfieldWidth( bitWd ) {}
[14cebb7a]109
[77a3f41]110 const Type* get_type() const override { return type; }
[e0e9a0b]111 void set_type( const Type * ty ) override { type = ty; }
[77a3f41]112
[23f99e1]113 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
[77a3f41]114private:
[23f99e1]115 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
[f3cc5b6]116 MUTATE_FRIEND
[23f99e1]117};
118
[3e94a23]119/// Function variable arguments flag
120enum ArgumentFlag { FixedArgs, VariableArgs };
121
[2d6add4]122/// Function declaration `int foo()`
[a1da039]123class FunctionDecl final : public DeclWithType {
[23f99e1]124public:
[a00a2c1]125 std::vector<ptr<TypeDecl>> type_params;
126 std::vector<ptr<DeclWithType>> assertions;
[b859f59]127 std::vector<ptr<DeclWithType>> params;
128 std::vector<ptr<DeclWithType>> returns;
[954c954]129 // declared type, derived from parameter declarations
[23f99e1]130 ptr<FunctionType> type;
[b8ab91a]131 /// Null for the forward declaration of a function.
[23f99e1]132 ptr<CompoundStmt> stmts;
[d76c588]133 std::vector< ptr<Expr> > withExprs;
[23f99e1]134
[37273c8]135 /// Monomorphic Function Constructor:
136 FunctionDecl( const CodeLocation & locaction, const std::string & name,
[954c954]137 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]138 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]139 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[7edd5c1]140
[37273c8]141 /// Polymorphic Function Constructor:
[7edd5c1]142 FunctionDecl( const CodeLocation & location, const std::string & name,
143 std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
144 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]145 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]146 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[23f99e1]147
[8a5530c]148 const Type * get_type() const override;
[e0e9a0b]149 void set_type( const Type * t ) override;
[23f99e1]150
151 bool has_body() const { return stmts; }
152
[07de76b]153 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]154private:
155 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]156 MUTATE_FRIEND
[77a3f41]157};
158
[360b2e13]159/// Base class for named type aliases
160class NamedTypeDecl : public Decl {
161public:
162 ptr<Type> base;
163 std::vector<ptr<DeclWithType>> assertions;
164
[7030dab]165 NamedTypeDecl(
[e0e9a0b]166 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
167 const Type * b, Linkage::Spec spec = Linkage::Cforall )
[6a45bd78]168 : Decl( loc, name, storage, spec ), base( b ), assertions() {}
[360b2e13]169
170 /// Produces a name for the kind of alias
[312029a]171 virtual const char * typeString() const = 0;
[360b2e13]172
173private:
174 NamedTypeDecl* clone() const override = 0;
[f3cc5b6]175 MUTATE_FRIEND
[360b2e13]176};
177
178/// Cforall type variable: `dtype T`
179class TypeDecl final : public NamedTypeDecl {
[07de76b]180 public:
[6e50a6b]181 enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
[07de76b]182
183 Kind kind;
[360b2e13]184 bool sized;
185 ptr<Type> init;
186
[7030dab]187 TypeDecl(
188 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
[e3bc51c]189 const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
190 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
[9e1d485]191 init( i ) {}
[360b2e13]192
[312029a]193 const char * typeString() const override;
[360b2e13]194 /// Produces a name for generated code
[312029a]195 const char * genTypeString() const;
[360b2e13]196
[9e1d485]197 /// convenience accessor to match Type::isComplete()
[3606fe4]198 bool isComplete() const { return sized; }
[9e1d485]199
[23f99e1]200 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[07de76b]201 private:
[23f99e1]202 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[f3cc5b6]203 MUTATE_FRIEND
[360b2e13]204};
205
[93c10de]206/// Data extracted from a TypeDecl.
207struct TypeData {
208 TypeDecl::Kind kind;
209 bool isComplete;
210
211 TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {}
212 TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
213 TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {}
214 TypeData( const TypeData & d1, const TypeData & d2 )
215 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
216
217 bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; }
218 bool operator!=( const TypeData & o ) const { return !(*this == o); }
219};
220
221std::ostream & operator<< ( std::ostream &, const TypeData & );
[d76c588]222
[360b2e13]223/// C-style typedef `typedef Foo Bar`
224class TypedefDecl final : public NamedTypeDecl {
225public:
[e0115286]226 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]227 Type* b, Linkage::Spec spec = Linkage::Cforall )
228 : NamedTypeDecl( loc, name, storage, b, spec ) {}
229
[312029a]230 const char * typeString() const override { return "typedef"; }
[360b2e13]231
[23f99e1]232 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]233private:
[23f99e1]234 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]235 MUTATE_FRIEND
[360b2e13]236};
237
[a300e4a]238/// Aggregate type declaration base class
239class AggregateDecl : public Decl {
240public:
[312029a]241 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
242 static const char * aggrString( Aggregate aggr );
243
[a300e4a]244 std::vector<ptr<Decl>> members;
[54e41b3]245 std::vector<ptr<TypeDecl>> params;
[a300e4a]246 std::vector<ptr<Attribute>> attributes;
247 bool body = false;
248 readonly<AggregateDecl> parent = {};
249
[14cebb7a]250 AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]251 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]252 : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]253 attributes( std::move(attrs) ) {}
[14cebb7a]254
[a300e4a]255 AggregateDecl* set_body( bool b ) { body = b; return this; }
256
[ed3935da]257 /// Produces a name for the kind of aggregate
[312029a]258 virtual const char * typeString() const = 0;
[ed3935da]259
[f3cc5b6]260private:
261 AggregateDecl * clone() const override = 0;
262 MUTATE_FRIEND
[a300e4a]263};
264
265/// struct declaration `struct Foo { ... };`
266class StructDecl final : public AggregateDecl {
267public:
[312029a]268 Aggregate kind;
[a300e4a]269
[14cebb7a]270 StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]271 Aggregate kind = Struct,
[a300e4a]272 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
273 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
274
[3cc1111]275 bool is_coroutine() const { return kind == Coroutine; }
276 bool is_generator() const { return kind == Generator; }
277 bool is_monitor () const { return kind == Monitor ; }
278 bool is_thread () const { return kind == Thread ; }
[a300e4a]279
[23f99e1]280 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]281
[312029a]282 const char * typeString() const override { return aggrString( kind ); }
[ed3935da]283
[a300e4a]284private:
[23f99e1]285 StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]286 MUTATE_FRIEND
[a300e4a]287};
288
289/// union declaration `union Foo { ... };`
290class UnionDecl final : public AggregateDecl {
291public:
[14cebb7a]292 UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]293 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
294 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
295
[23f99e1]296 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[ed3935da]297
[312029a]298 const char * typeString() const override { return aggrString( Union ); }
[ed3935da]299
[a300e4a]300private:
[23f99e1]301 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]302 MUTATE_FRIEND
[a300e4a]303};
304
[90be0cf]305/// Enumeration attribute kind.
[af746cc]306enum class EnumAttribute{ Value, Posn, Label };
[90be0cf]307
[90e683b]308/// enum declaration `enum Foo { ... };` or `enum(...) Foo { ... };`
[a300e4a]309class EnumDecl final : public AggregateDecl {
310public:
[5ccc733]311 // isCfa indicated if the enum has a declaration like:
[94c98f0e]312 // enum (type_optional) Name {...}
[5ccc733]313 bool isCfa;
[8c55d34]314 // if isCfa == true && base.get() == nullptr, it is a "opaque" enum
[a1da039]315 ptr<Type> base;
[e4d7c1c]316 enum class EnumHiding { Visible, Hide } hide;
[85855b0]317 std::vector< ast::ptr<ast::EnumInstType>> inlinedDecl; // child enums
318
[90e683b]319 bool is_c_enum () const { return !isCfa; }
320 bool is_opaque_enum() const { return isCfa && nullptr == base; }
321 bool is_typed_enum () const { return isCfa && nullptr != base; }
322
[5ccc733]323 EnumDecl( const CodeLocation& loc, const std::string& name, bool isCfa = false,
[b0d9ff7]324 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
[e4d7c1c]325 Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide,
[7b71402]326 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
[5ccc733]327 : AggregateDecl( loc, name, std::move(attrs), linkage ), isCfa(isCfa), base(base), hide(hide), enumValues(enumValues) {}
[a300e4a]328
329 /// gets the integer value for this enumerator, returning true iff value found
[f135b50]330 // Maybe it is not used in producing the enum value
[9d6e7fa9]331 bool valueOf( const Decl * enumerator, long long& value ) const;
[a300e4a]332
[23f99e1]333 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]334
[312029a]335 const char * typeString() const override { return aggrString( Enum ); }
[ed3935da]336
[a300e4a]337private:
[23f99e1]338 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
[f3cc5b6]339 MUTATE_FRIEND
[a300e4a]340
341 /// Map from names to enumerator values; kept private for lazy initialization
342 mutable std::unordered_map< std::string, long long > enumValues;
343};
344
345/// trait declaration `trait Foo( ... ) { ... };`
346class TraitDecl final : public AggregateDecl {
347public:
[14cebb7a]348 TraitDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]349 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
350 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
351
[23f99e1]352 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]353
[312029a]354 const char * typeString() const override { return "trait"; }
[ed3935da]355
[a300e4a]356private:
[23f99e1]357 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
[f3cc5b6]358 MUTATE_FRIEND
[2bb4a01]359};
360
[e67991f]361/// With statement `with (...) ...`
[6a0b043]362/// This is a statement lexically, but a Decl is needed for the SymbolTable.
[e67991f]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
[94c98f0e]377/// Assembly declaration: `asm ... ( "..." : ... )`
[a1da039]378class AsmDecl final : public Decl {
[23f99e1]379public:
380 ptr<AsmStmt> stmt;
381
[07de76b]382 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[a1da039]383 : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
[23f99e1]384
[07de76b]385 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]386private:
[07de76b]387 AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]388 MUTATE_FRIEND
[23f99e1]389};
390
[2d019af]391/// C-preprocessor directive `#...`
[a1da039]392class DirectiveDecl final : public Decl {
[2d019af]393public:
394 ptr<DirectiveStmt> stmt;
395
396 DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
[a1da039]397 : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
[2d019af]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
[19a8c40]405/// Static Assertion `_Static_assert( ... , ... );`
[a1da039]406class StaticAssertDecl final : public Decl {
[23f99e1]407public:
[112fe04]408 ptr<Expr> cond;
[23f99e1]409 ptr<ConstantExpr> msg; // string literal
410
411 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[a1da039]412 : Decl( loc, "", {}, Linkage::C ), cond( condition ), msg( msg ) {}
[23f99e1]413
[07de76b]414 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]415private:
416 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]417 MUTATE_FRIEND
[23f99e1]418};
[e0115286]419
[19a8c40]420/// Inline Member Declaration `inline TypeName;`
[71806e0]421class InlineMemberDecl final : public DeclWithType {
[e874605]422public:
423 ptr<Type> type;
424
[71806e0]425 InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
[e874605]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:
[71806e0]435 InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
[e874605]436 MUTATE_FRIEND
437};
[19a8c40]438
[2bb4a01]439}
440
[f3cc5b6]441#undef MUTATE_FRIEND
442
[2bb4a01]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.