source: src/AST/Decl.hpp @ 3e135c8

Last change on this file since 3e135c8 was 8c55d34, checked in by Andrew Beach <ajbeach@…>, 2 weeks ago

Fixed spelling mistake. (I'm 99% sure.)

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