source: src/AST/Decl.hpp @ ab9c1b3

Last change on this file since ab9c1b3 was 37273c8, checked in by Andrew Beach <ajbeach@…>, 10 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
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"
[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
[23f99e1]59        const Decl * accept( Visitor & v ) const override = 0;
[2bb4a01]60private:
[23f99e1]61        Decl * clone() const override = 0;
[f3cc5b6]62        MUTATE_FRIEND
[2bb4a01]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;
[14cebb7a]70        /// Stores the scope level at which the variable was declared.
[2bb4a01]71        /// Used to access shadowed identifiers.
72        int scopeLevel = 0;
73
74        std::vector<ptr<Attribute>> attributes;
[a300e4a]75        Function::Specs funcSpec;
[2bb4a01]76        ptr<Expr> asmName;
77        bool isDeleted = false;
[16ba4a6]78        bool isTypeFixed = false;
[2bb4a01]79
[14cebb7a]80        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]81                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
[14cebb7a]82        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
[a300e4a]83                funcSpec(fs), asmName() {}
[14cebb7a]84
[a300e4a]85        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
86
87        /// Get type of this declaration. May be generated by subclass
[6d51bd7]88        virtual const Type * get_type() const = 0;
[a300e4a]89        /// Set type of this declaration. May be verified by subclass
[e0e9a0b]90        virtual void set_type( const Type * ) = 0;
[a300e4a]91
[23f99e1]92        const DeclWithType * accept( Visitor & v ) const override = 0;
[a300e4a]93private:
[23f99e1]94        DeclWithType * clone() const override = 0;
[f3cc5b6]95        MUTATE_FRIEND
[a300e4a]96};
97
[77a3f41]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
[e67991f]105        ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
106                const Init * init = nullptr, Storage::Classes storage = {},
[e8616b6]107                Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr,
[2a8f0c1]108                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
[14cebb7a]109        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
[77a3f41]110          init( init ), bitfieldWidth( bitWd ) {}
[14cebb7a]111
[77a3f41]112        const Type* get_type() const override { return type; }
[e0e9a0b]113        void set_type( const Type * ty ) override { type = ty; }
[77a3f41]114
[23f99e1]115        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
[77a3f41]116private:
[23f99e1]117        ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
[f3cc5b6]118        MUTATE_FRIEND
[23f99e1]119};
120
[3e94a23]121/// Function variable arguments flag
122enum ArgumentFlag { FixedArgs, VariableArgs };
123
[8a5530c]124/// Object declaration `int foo()`
[a1da039]125class FunctionDecl final : public DeclWithType {
[23f99e1]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
[37273c8]137        /// Monomorphic Function Constructor:
138        FunctionDecl( const CodeLocation & locaction, const std::string & name,
[954c954]139                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]140                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]141                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[7edd5c1]142
[37273c8]143        /// Polymorphic Function Constructor:
[7edd5c1]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,
[3992098]147                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]148                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[23f99e1]149
[8a5530c]150        const Type * get_type() const override;
[e0e9a0b]151        void set_type( const Type * t ) override;
[23f99e1]152
153        bool has_body() const { return stmts; }
154
[07de76b]155        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]156private:
157        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]158        MUTATE_FRIEND
[77a3f41]159};
160
[360b2e13]161/// Base class for named type aliases
162class NamedTypeDecl : public Decl {
163public:
164        ptr<Type> base;
165        std::vector<ptr<DeclWithType>> assertions;
166
[7030dab]167        NamedTypeDecl(
[e0e9a0b]168                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
169                const Type * b, Linkage::Spec spec = Linkage::Cforall )
[6a45bd78]170        : Decl( loc, name, storage, spec ), base( b ), assertions() {}
[360b2e13]171
172        /// Produces a name for the kind of alias
[312029a]173        virtual const char * typeString() const = 0;
[360b2e13]174
175private:
176        NamedTypeDecl* clone() const override = 0;
[f3cc5b6]177        MUTATE_FRIEND
[360b2e13]178};
179
180/// Cforall type variable: `dtype T`
181class TypeDecl final : public NamedTypeDecl {
[07de76b]182  public:
[6e50a6b]183        enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
[07de76b]184
185        Kind kind;
[360b2e13]186        bool sized;
187        ptr<Type> init;
188
[7030dab]189        TypeDecl(
190                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
[e3bc51c]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 ),
[9e1d485]193          init( i ) {}
[360b2e13]194
[312029a]195        const char * typeString() const override;
[360b2e13]196        /// Produces a name for generated code
[312029a]197        const char * genTypeString() const;
[360b2e13]198
[9e1d485]199        /// convenience accessor to match Type::isComplete()
[3606fe4]200        bool isComplete() const { return sized; }
[9e1d485]201
[23f99e1]202        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[07de76b]203  private:
[23f99e1]204        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[f3cc5b6]205        MUTATE_FRIEND
[360b2e13]206};
207
[93c10de]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 & );
[d76c588]224
[360b2e13]225/// C-style typedef `typedef Foo Bar`
226class TypedefDecl final : public NamedTypeDecl {
227public:
[e0115286]228        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]229                Type* b, Linkage::Spec spec = Linkage::Cforall )
230        : NamedTypeDecl( loc, name, storage, b, spec ) {}
231
[312029a]232        const char * typeString() const override { return "typedef"; }
[360b2e13]233
[23f99e1]234        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]235private:
[23f99e1]236        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]237        MUTATE_FRIEND
[360b2e13]238};
239
[a300e4a]240/// Aggregate type declaration base class
241class AggregateDecl : public Decl {
242public:
[312029a]243        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
244        static const char * aggrString( Aggregate aggr );
245
[a300e4a]246        std::vector<ptr<Decl>> members;
[54e41b3]247        std::vector<ptr<TypeDecl>> params;
[a300e4a]248        std::vector<ptr<Attribute>> attributes;
249        bool body = false;
250        readonly<AggregateDecl> parent = {};
251
[14cebb7a]252        AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]253                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]254        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]255          attributes( std::move(attrs) ) {}
[14cebb7a]256
[a300e4a]257        AggregateDecl* set_body( bool b ) { body = b; return this; }
258
[ed3935da]259        /// Produces a name for the kind of aggregate
[312029a]260        virtual const char * typeString() const = 0;
[ed3935da]261
[f3cc5b6]262private:
263        AggregateDecl * clone() const override = 0;
264        MUTATE_FRIEND
[a300e4a]265};
266
267/// struct declaration `struct Foo { ... };`
268class StructDecl final : public AggregateDecl {
269public:
[312029a]270        Aggregate kind;
[a300e4a]271
[14cebb7a]272        StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]273                Aggregate kind = Struct,
[a300e4a]274                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
275        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
276
[3cc1111]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   ; }
[a300e4a]281
[23f99e1]282        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]283
[312029a]284        const char * typeString() const override { return aggrString( kind ); }
[ed3935da]285
[a300e4a]286private:
[23f99e1]287        StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]288        MUTATE_FRIEND
[a300e4a]289};
290
291/// union declaration `union Foo { ... };`
292class UnionDecl final : public AggregateDecl {
293public:
[14cebb7a]294        UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]295                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
296        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
297
[23f99e1]298        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[ed3935da]299
[312029a]300        const char * typeString() const override { return aggrString( Union ); }
[ed3935da]301
[a300e4a]302private:
[23f99e1]303        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]304        MUTATE_FRIEND
[a300e4a]305};
306
307/// enum declaration `enum Foo { ... };`
308class EnumDecl final : public AggregateDecl {
309public:
[a1da039]310        // isTyped indicated if the enum has a declaration like:
[94c98f0e]311        // enum (type_optional) Name {...}
[a1da039]312        bool isTyped;
313        // if isTyped == true && base.get() == nullptr, it is a "void" type enum
314        ptr<Type> base;
[e4d7c1c]315        enum class EnumHiding { Visible, Hide } hide;
[f135b50]316
[e4d7c1c]317        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
[b0d9ff7]318                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
[e4d7c1c]319                Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide,
[7b71402]320                std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
[e4d7c1c]321        : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), 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 (...) ...`
[6a0b043]357/// This is a statement lexically, but a Decl is needed for the SymbolTable.
[e67991f]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
[94c98f0e]372/// Assembly declaration: `asm ... ( "..." : ... )`
[a1da039]373class AsmDecl final : public Decl {
[23f99e1]374public:
375        ptr<AsmStmt> stmt;
376
[07de76b]377        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[a1da039]378        : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
[23f99e1]379
[07de76b]380        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]381private:
[07de76b]382        AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]383        MUTATE_FRIEND
[23f99e1]384};
385
[2d019af]386/// C-preprocessor directive `#...`
[a1da039]387class DirectiveDecl final : public Decl {
[2d019af]388public:
389        ptr<DirectiveStmt> stmt;
390
391        DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
[a1da039]392        : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
[2d019af]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
[19a8c40]400/// Static Assertion `_Static_assert( ... , ... );`
[a1da039]401class StaticAssertDecl final : public Decl {
[23f99e1]402public:
[112fe04]403        ptr<Expr> cond;
[23f99e1]404        ptr<ConstantExpr> msg;   // string literal
405
406        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[a1da039]407        : Decl( loc, "", {}, Linkage::C ), cond( condition ), msg( msg ) {}
[23f99e1]408
[07de76b]409        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]410private:
411        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]412        MUTATE_FRIEND
[23f99e1]413};
[e0115286]414
[19a8c40]415/// Inline Member Declaration `inline TypeName;`
[71806e0]416class InlineMemberDecl final : public DeclWithType {
[e874605]417public:
418        ptr<Type> type;
419
[71806e0]420        InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
[e874605]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:
[71806e0]430        InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
[e874605]431        MUTATE_FRIEND
432};
[19a8c40]433
[2bb4a01]434}
435
[f3cc5b6]436#undef MUTATE_FRIEND
437
[2bb4a01]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.