source: src/AST/Decl.hpp@ 890f67a

ADT ast-experimental
Last change on this file since 890f67a was b859f59, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Reordered fields in FunctionDecl and removed some trailing white-space.

  • Property mode set to 100644
File size: 14.0 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
12// Last Modified On : Thu May 5 12:09:00 2022
13// Update Count : 33
[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"
[07de76b]31#include "Common/utility.h"
32#include "Common/SemanticError.h" // error_str
[2bb4a01]33
[f3cc5b6]34// Must be included in *all* AST classes; should be #undef'd at the end of the file
[99da267]35#define MUTATE_FRIEND \
[91a72ef]36 template<typename node_t> friend node_t * mutate(const node_t * node); \
[99da267]37 template<typename node_t> friend node_t * shallowCopy(const node_t * node);
[f3cc5b6]38
[2bb4a01]39namespace ast {
[a300e4a]40
[2bb4a01]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
[14cebb7a]50 Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]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
[23f99e1]61 const Decl * accept( Visitor & v ) const override = 0;
[2bb4a01]62private:
[23f99e1]63 Decl * clone() const override = 0;
[f3cc5b6]64 MUTATE_FRIEND
[2bb4a01]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;
[14cebb7a]73 /// Stores the scope level at which the variable was declared.
[2bb4a01]74 /// Used to access shadowed identifiers.
75 int scopeLevel = 0;
76
77 std::vector<ptr<Attribute>> attributes;
[a300e4a]78 Function::Specs funcSpec;
[2bb4a01]79 ptr<Expr> asmName;
80 bool isDeleted = false;
[16ba4a6f]81 bool isTypeFixed = false;
[2bb4a01]82
[14cebb7a]83 DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]84 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
[14cebb7a]85 : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
[a300e4a]86 funcSpec(fs), asmName() {}
[14cebb7a]87
[a300e4a]88 std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
89
90 /// Get type of this declaration. May be generated by subclass
[6d51bd7]91 virtual const Type * get_type() const = 0;
[a300e4a]92 /// Set type of this declaration. May be verified by subclass
[e0e9a0b]93 virtual void set_type( const Type * ) = 0;
[a300e4a]94
[23f99e1]95 const DeclWithType * accept( Visitor & v ) const override = 0;
[a300e4a]96private:
[23f99e1]97 DeclWithType * clone() const override = 0;
[f3cc5b6]98 MUTATE_FRIEND
[a300e4a]99};
100
[77a3f41]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
[e67991f]108 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
109 const Init * init = nullptr, Storage::Classes storage = {},
[e8616b6]110 Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr,
[2a8f0c1]111 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
[14cebb7a]112 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
[77a3f41]113 init( init ), bitfieldWidth( bitWd ) {}
[14cebb7a]114
[77a3f41]115 const Type* get_type() const override { return type; }
[e0e9a0b]116 void set_type( const Type * ty ) override { type = ty; }
[77a3f41]117
[23f99e1]118 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
[77a3f41]119private:
[23f99e1]120 ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
[f3cc5b6]121 MUTATE_FRIEND
[23f99e1]122};
123
[8a5530c]124/// Object declaration `int foo()`
[23f99e1]125class FunctionDecl : public DeclWithType {
126public:
[a00a2c1]127 std::vector<ptr<TypeDecl>> type_params;
128 std::vector<ptr<DeclWithType>> assertions;
[b859f59]129 std::vector<ptr<DeclWithType>> params;
130 std::vector<ptr<DeclWithType>> returns;
[954c954]131 // declared type, derived from parameter declarations
[23f99e1]132 ptr<FunctionType> type;
[b8ab91a]133 /// Null for the forward declaration of a function.
[23f99e1]134 ptr<CompoundStmt> stmts;
[d76c588]135 std::vector< ptr<Expr> > withExprs;
[23f99e1]136
[7edd5c1]137 // The difference between the two constructors is in how they handle
138 // assertions. The first constructor uses the assertions from the type
139 // parameters, in the style of the old ast, and puts them on the type.
140 // The second takes an explicite list of assertions and builds a list of
141 // references to them on the type.
142
[490fb92e]143 FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
[954c954]144 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]145 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[490fb92e]146 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false);
[7edd5c1]147
148 FunctionDecl( const CodeLocation & location, const std::string & name,
149 std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
150 std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]151 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[7edd5c1]152 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, bool isVarArgs = false);
[23f99e1]153
[8a5530c]154 const Type * get_type() const override;
[e0e9a0b]155 void set_type( const Type * t ) override;
[23f99e1]156
157 bool has_body() const { return stmts; }
158
[07de76b]159 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]160private:
161 FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]162 MUTATE_FRIEND
[77a3f41]163};
164
[360b2e13]165/// Base class for named type aliases
166class NamedTypeDecl : public Decl {
167public:
168 ptr<Type> base;
169 std::vector<ptr<DeclWithType>> assertions;
170
[7030dab]171 NamedTypeDecl(
[e0e9a0b]172 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
173 const Type * b, Linkage::Spec spec = Linkage::Cforall )
[6a45bd78]174 : Decl( loc, name, storage, spec ), base( b ), assertions() {}
[360b2e13]175
176 /// Produces a name for the kind of alias
[312029a]177 virtual const char * typeString() const = 0;
[360b2e13]178
179private:
180 NamedTypeDecl* clone() const override = 0;
[f3cc5b6]181 MUTATE_FRIEND
[360b2e13]182};
183
184/// Cforall type variable: `dtype T`
185class TypeDecl final : public NamedTypeDecl {
[07de76b]186 public:
[6e50a6b]187 enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
[07de76b]188
189 Kind kind;
[360b2e13]190 bool sized;
191 ptr<Type> init;
192
193 /// Data extracted from a type decl
194 struct Data {
[07de76b]195 Kind kind;
[360b2e13]196 bool isComplete;
197
[07de76b]198 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
[d76c588]199 Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
[07de76b]200 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
[d76c588]201 Data( const Data & d1, const Data & d2 )
[07de76b]202 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
[360b2e13]203
[07de76b]204 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
205 bool operator!=( const Data & o ) const { return !(*this == o); }
[360b2e13]206 };
207
[7030dab]208 TypeDecl(
209 const CodeLocation & loc, const std::string & name, Storage::Classes storage,
[e3bc51c]210 const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
211 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
[9e1d485]212 init( i ) {}
[360b2e13]213
[312029a]214 const char * typeString() const override;
[360b2e13]215 /// Produces a name for generated code
[312029a]216 const char * genTypeString() const;
[360b2e13]217
[9e1d485]218 /// convenience accessor to match Type::isComplete()
[3606fe4]219 bool isComplete() const { return sized; }
[9e1d485]220
[23f99e1]221 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[07de76b]222 private:
[23f99e1]223 TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[f3cc5b6]224 MUTATE_FRIEND
[360b2e13]225};
226
[d76c588]227std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
228
[360b2e13]229/// C-style typedef `typedef Foo Bar`
230class TypedefDecl final : public NamedTypeDecl {
231public:
[e0115286]232 TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]233 Type* b, Linkage::Spec spec = Linkage::Cforall )
234 : NamedTypeDecl( loc, name, storage, b, spec ) {}
235
[312029a]236 const char * typeString() const override { return "typedef"; }
[360b2e13]237
[23f99e1]238 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]239private:
[23f99e1]240 TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]241 MUTATE_FRIEND
[360b2e13]242};
243
[a300e4a]244/// Aggregate type declaration base class
245class AggregateDecl : public Decl {
246public:
[312029a]247 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
248 static const char * aggrString( Aggregate aggr );
249
[a300e4a]250 std::vector<ptr<Decl>> members;
[54e41b3]251 std::vector<ptr<TypeDecl>> params;
[a300e4a]252 std::vector<ptr<Attribute>> attributes;
253 bool body = false;
254 readonly<AggregateDecl> parent = {};
255
[14cebb7a]256 AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]257 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]258 : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]259 attributes( std::move(attrs) ) {}
[14cebb7a]260
[a300e4a]261 AggregateDecl* set_body( bool b ) { body = b; return this; }
262
[ed3935da]263 /// Produces a name for the kind of aggregate
[312029a]264 virtual const char * typeString() const = 0;
[ed3935da]265
[f3cc5b6]266private:
267 AggregateDecl * clone() const override = 0;
268 MUTATE_FRIEND
[a300e4a]269};
270
271/// struct declaration `struct Foo { ... };`
272class StructDecl final : public AggregateDecl {
273public:
[312029a]274 Aggregate kind;
[a300e4a]275
[14cebb7a]276 StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]277 Aggregate kind = Struct,
[a300e4a]278 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
279 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
280
[3cc1111]281 bool is_coroutine() const { return kind == Coroutine; }
282 bool is_generator() const { return kind == Generator; }
283 bool is_monitor () const { return kind == Monitor ; }
284 bool is_thread () const { return kind == Thread ; }
[a300e4a]285
[23f99e1]286 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]287
[312029a]288 const char * typeString() const override { return aggrString( kind ); }
[ed3935da]289
[a300e4a]290private:
[23f99e1]291 StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]292 MUTATE_FRIEND
[a300e4a]293};
294
295/// union declaration `union Foo { ... };`
296class UnionDecl final : public AggregateDecl {
297public:
[14cebb7a]298 UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]299 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
300 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
301
[23f99e1]302 const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[ed3935da]303
[312029a]304 const char * typeString() const override { return aggrString( Union ); }
[ed3935da]305
[a300e4a]306private:
[23f99e1]307 UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]308 MUTATE_FRIEND
[a300e4a]309};
310
311/// enum declaration `enum Foo { ... };`
312class EnumDecl final : public AggregateDecl {
313public:
[b0d9ff7]314 bool isTyped;
[3e54399]315 ptr<Type> base;
[f135b50]316
[b0d9ff7]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,
[7b71402]320 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
[b0d9ff7]321 : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), enumValues(enumValues) {}
[a300e4a]322
323 /// gets the integer value for this enumerator, returning true iff value found
[f135b50]324 // Maybe it is not used in producing the enum value
[9d6e7fa9]325 bool valueOf( const Decl * enumerator, long long& value ) const;
[a300e4a]326
[23f99e1]327 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]328
[312029a]329 const char * typeString() const override { return aggrString( Enum ); }
[ed3935da]330
[4559b34]331
[a300e4a]332private:
[23f99e1]333 EnumDecl * clone() const override { return new EnumDecl{ *this }; }
[f3cc5b6]334 MUTATE_FRIEND
[a300e4a]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:
[14cebb7a]343 TraitDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]344 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
345 : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
346
[23f99e1]347 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]348
[312029a]349 const char * typeString() const override { return "trait"; }
[ed3935da]350
[a300e4a]351private:
[23f99e1]352 TraitDecl * clone() const override { return new TraitDecl{ *this }; }
[f3cc5b6]353 MUTATE_FRIEND
[2bb4a01]354};
355
[e67991f]356/// With statement `with (...) ...`
357class WithStmt final : public Decl {
358public:
359 std::vector<ptr<Expr>> exprs;
360 ptr<Stmt> stmt;
361
362 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
363 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
364
365 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
366private:
367 WithStmt * clone() const override { return new WithStmt{ *this }; }
368 MUTATE_FRIEND
369};
370
[23f99e1]371class AsmDecl : public Decl {
372public:
373 ptr<AsmStmt> stmt;
374
[07de76b]375 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[23f99e1]376 : Decl( loc, "", {}, {} ), stmt(stmt) {}
377
[07de76b]378 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]379private:
[07de76b]380 AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]381 MUTATE_FRIEND
[23f99e1]382};
383
[2d019af]384/// C-preprocessor directive `#...`
385class DirectiveDecl : public Decl {
386public:
387 ptr<DirectiveStmt> stmt;
388
389 DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
390 : Decl( loc, "", {}, {} ), stmt(stmt) {}
391
392 const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); }
393private:
394 DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
395 MUTATE_FRIEND
396};
397
[23f99e1]398class StaticAssertDecl : public Decl {
399public:
[112fe04]400 ptr<Expr> cond;
[23f99e1]401 ptr<ConstantExpr> msg; // string literal
402
403 StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[112fe04]404 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
[23f99e1]405
[07de76b]406 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]407private:
408 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]409 MUTATE_FRIEND
[23f99e1]410};
[e0115286]411
[2bb4a01]412}
413
[f3cc5b6]414#undef MUTATE_FRIEND
415
[2bb4a01]416// Local Variables: //
417// tab-width: 4 //
418// mode: c++ //
419// compile-command: "make install" //
420// End: //
Note: See TracBrowser for help on using the repository browser.