source: src/AST/Decl.hpp @ 4883712

Last change on this file since 4883712 was 6a0b043, checked in by Andrew Beach <ajbeach@…>, 7 months ago

Did some investigation of WithStmt?. It may not be possible to convert it to a Stmt without changing how SymbolTable? handles the with clauses.

  • 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"
[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        std::string mangleName;
[14cebb7a]72        /// Stores the scope level at which the variable was declared.
[2bb4a01]73        /// Used to access shadowed identifiers.
74        int scopeLevel = 0;
75
76        std::vector<ptr<Attribute>> attributes;
[a300e4a]77        Function::Specs funcSpec;
[2bb4a01]78        ptr<Expr> asmName;
79        bool isDeleted = false;
[16ba4a6]80        bool isTypeFixed = false;
[2bb4a01]81
[14cebb7a]82        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[2bb4a01]83                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
[14cebb7a]84        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
[a300e4a]85                funcSpec(fs), asmName() {}
[14cebb7a]86
[a300e4a]87        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
88
89        /// Get type of this declaration. May be generated by subclass
[6d51bd7]90        virtual const Type * get_type() const = 0;
[a300e4a]91        /// Set type of this declaration. May be verified by subclass
[e0e9a0b]92        virtual void set_type( const Type * ) = 0;
[a300e4a]93
[23f99e1]94        const DeclWithType * accept( Visitor & v ) const override = 0;
[a300e4a]95private:
[23f99e1]96        DeclWithType * clone() const override = 0;
[f3cc5b6]97        MUTATE_FRIEND
[a300e4a]98};
99
[77a3f41]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
[e67991f]107        ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type,
108                const Init * init = nullptr, Storage::Classes storage = {},
[e8616b6]109                Linkage::Spec linkage = Linkage::Cforall, const Expr * bitWd = nullptr,
[2a8f0c1]110                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
[14cebb7a]111        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
[77a3f41]112          init( init ), bitfieldWidth( bitWd ) {}
[14cebb7a]113
[77a3f41]114        const Type* get_type() const override { return type; }
[e0e9a0b]115        void set_type( const Type * ty ) override { type = ty; }
[77a3f41]116
[23f99e1]117        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
[77a3f41]118private:
[23f99e1]119        ObjectDecl * clone() const override { return new ObjectDecl{ *this }; }
[f3cc5b6]120        MUTATE_FRIEND
[23f99e1]121};
122
[3e94a23]123/// Function variable arguments flag
124enum ArgumentFlag { FixedArgs, VariableArgs };
125
[8a5530c]126/// Object declaration `int foo()`
[a1da039]127class FunctionDecl final : public DeclWithType {
[23f99e1]128public:
[a00a2c1]129        std::vector<ptr<TypeDecl>> type_params;
130        std::vector<ptr<DeclWithType>> assertions;
[b859f59]131        std::vector<ptr<DeclWithType>> params;
132        std::vector<ptr<DeclWithType>> returns;
[954c954]133        // declared type, derived from parameter declarations
[23f99e1]134        ptr<FunctionType> type;
[b8ab91a]135        /// Null for the forward declaration of a function.
[23f99e1]136        ptr<CompoundStmt> stmts;
[d76c588]137        std::vector< ptr<Expr> > withExprs;
[23f99e1]138
[7edd5c1]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
[490fb92e]145        FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
[954c954]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 );
[7edd5c1]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,
[3992098]153                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]154                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[23f99e1]155
[8a5530c]156        const Type * get_type() const override;
[e0e9a0b]157        void set_type( const Type * t ) override;
[23f99e1]158
159        bool has_body() const { return stmts; }
160
[07de76b]161        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]162private:
163        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]164        MUTATE_FRIEND
[77a3f41]165};
166
[360b2e13]167/// Base class for named type aliases
168class NamedTypeDecl : public Decl {
169public:
170        ptr<Type> base;
171        std::vector<ptr<DeclWithType>> assertions;
172
[7030dab]173        NamedTypeDecl(
[e0e9a0b]174                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
175                const Type * b, Linkage::Spec spec = Linkage::Cforall )
[6a45bd78]176        : Decl( loc, name, storage, spec ), base( b ), assertions() {}
[360b2e13]177
178        /// Produces a name for the kind of alias
[312029a]179        virtual const char * typeString() const = 0;
[360b2e13]180
181private:
182        NamedTypeDecl* clone() const override = 0;
[f3cc5b6]183        MUTATE_FRIEND
[360b2e13]184};
185
186/// Cforall type variable: `dtype T`
187class TypeDecl final : public NamedTypeDecl {
[07de76b]188  public:
[6e50a6b]189        enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
[07de76b]190
191        Kind kind;
[360b2e13]192        bool sized;
193        ptr<Type> init;
194
[7030dab]195        TypeDecl(
196                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
[e3bc51c]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 ),
[9e1d485]199          init( i ) {}
[360b2e13]200
[312029a]201        const char * typeString() const override;
[360b2e13]202        /// Produces a name for generated code
[312029a]203        const char * genTypeString() const;
[360b2e13]204
[9e1d485]205        /// convenience accessor to match Type::isComplete()
[3606fe4]206        bool isComplete() const { return sized; }
[9e1d485]207
[23f99e1]208        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[07de76b]209  private:
[23f99e1]210        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[f3cc5b6]211        MUTATE_FRIEND
[360b2e13]212};
213
[93c10de]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 & );
[d76c588]230
[360b2e13]231/// C-style typedef `typedef Foo Bar`
232class TypedefDecl final : public NamedTypeDecl {
233public:
[e0115286]234        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]235                Type* b, Linkage::Spec spec = Linkage::Cforall )
236        : NamedTypeDecl( loc, name, storage, b, spec ) {}
237
[312029a]238        const char * typeString() const override { return "typedef"; }
[360b2e13]239
[23f99e1]240        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]241private:
[23f99e1]242        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]243        MUTATE_FRIEND
[360b2e13]244};
245
[a300e4a]246/// Aggregate type declaration base class
247class AggregateDecl : public Decl {
248public:
[312029a]249        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
250        static const char * aggrString( Aggregate aggr );
251
[a300e4a]252        std::vector<ptr<Decl>> members;
[54e41b3]253        std::vector<ptr<TypeDecl>> params;
[a300e4a]254        std::vector<ptr<Attribute>> attributes;
255        bool body = false;
256        readonly<AggregateDecl> parent = {};
257
[14cebb7a]258        AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]259                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]260        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]261          attributes( std::move(attrs) ) {}
[14cebb7a]262
[a300e4a]263        AggregateDecl* set_body( bool b ) { body = b; return this; }
264
[ed3935da]265        /// Produces a name for the kind of aggregate
[312029a]266        virtual const char * typeString() const = 0;
[ed3935da]267
[f3cc5b6]268private:
269        AggregateDecl * clone() const override = 0;
270        MUTATE_FRIEND
[a300e4a]271};
272
273/// struct declaration `struct Foo { ... };`
274class StructDecl final : public AggregateDecl {
275public:
[312029a]276        Aggregate kind;
[a300e4a]277
[14cebb7a]278        StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]279                Aggregate kind = Struct,
[a300e4a]280                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
281        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
282
[3cc1111]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   ; }
[a300e4a]287
[23f99e1]288        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]289
[312029a]290        const char * typeString() const override { return aggrString( kind ); }
[ed3935da]291
[a300e4a]292private:
[23f99e1]293        StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]294        MUTATE_FRIEND
[a300e4a]295};
296
297/// union declaration `union Foo { ... };`
298class UnionDecl final : public AggregateDecl {
299public:
[14cebb7a]300        UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]301                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
302        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
303
[23f99e1]304        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[ed3935da]305
[312029a]306        const char * typeString() const override { return aggrString( Union ); }
[ed3935da]307
[a300e4a]308private:
[23f99e1]309        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]310        MUTATE_FRIEND
[a300e4a]311};
312
313/// enum declaration `enum Foo { ... };`
314class EnumDecl final : public AggregateDecl {
315public:
[a1da039]316        // isTyped indicated if the enum has a declaration like:
[94c98f0e]317        // enum (type_optional) Name {...}
[a1da039]318        bool isTyped;
319        // if isTyped == true && base.get() == nullptr, it is a "void" type enum
320        ptr<Type> base;
[e4d7c1c]321        enum class EnumHiding { Visible, Hide } hide;
[f135b50]322
[e4d7c1c]323        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = 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 >() )
[e4d7c1c]327        : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), 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
[4559b34]337
[a300e4a]338private:
[23f99e1]339        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
[f3cc5b6]340        MUTATE_FRIEND
[a300e4a]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:
[14cebb7a]349        TraitDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]350                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
351        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
352
[23f99e1]353        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]354
[312029a]355        const char * typeString() const override { return "trait"; }
[ed3935da]356
[a300e4a]357private:
[23f99e1]358        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
[f3cc5b6]359        MUTATE_FRIEND
[2bb4a01]360};
361
[e67991f]362/// With statement `with (...) ...`
[6a0b043]363/// This is a statement lexically, but a Decl is needed for the SymbolTable.
[e67991f]364class WithStmt final : public Decl {
365public:
366        std::vector<ptr<Expr>> exprs;
367        ptr<Stmt> stmt;
368
369        WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
370        : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
371
372        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
373private:
374        WithStmt * clone() const override { return new WithStmt{ *this }; }
375        MUTATE_FRIEND
376};
377
[94c98f0e]378/// Assembly declaration: `asm ... ( "..." : ... )`
[a1da039]379class AsmDecl final : public Decl {
[23f99e1]380public:
381        ptr<AsmStmt> stmt;
382
[07de76b]383        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[a1da039]384        : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
[23f99e1]385
[07de76b]386        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]387private:
[07de76b]388        AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]389        MUTATE_FRIEND
[23f99e1]390};
391
[2d019af]392/// C-preprocessor directive `#...`
[a1da039]393class DirectiveDecl final : public Decl {
[2d019af]394public:
395        ptr<DirectiveStmt> stmt;
396
397        DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
[a1da039]398        : Decl( loc, "", {}, Linkage::C ), stmt(stmt) {}
[2d019af]399
400        const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); }
401private:
402        DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
403        MUTATE_FRIEND
404};
405
[19a8c40]406/// Static Assertion `_Static_assert( ... , ... );`
[a1da039]407class StaticAssertDecl final : public Decl {
[23f99e1]408public:
[112fe04]409        ptr<Expr> cond;
[23f99e1]410        ptr<ConstantExpr> msg;   // string literal
411
412        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[a1da039]413        : Decl( loc, "", {}, Linkage::C ), cond( condition ), msg( msg ) {}
[23f99e1]414
[07de76b]415        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]416private:
417        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]418        MUTATE_FRIEND
[23f99e1]419};
[e0115286]420
[19a8c40]421/// Inline Member Declaration `inline TypeName;`
[71806e0]422class InlineMemberDecl final : public DeclWithType {
[e874605]423public:
424        ptr<Type> type;
425
[71806e0]426        InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
[e874605]427                Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
428                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
429        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ) {}
430
431        const Type * get_type() const override { return type; }
432        void set_type( const Type * ty ) override { type = ty; }
433
434        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
435private:
[71806e0]436        InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
[e874605]437        MUTATE_FRIEND
438};
[19a8c40]439
[2bb4a01]440}
441
[f3cc5b6]442#undef MUTATE_FRIEND
443
[2bb4a01]444// Local Variables: //
445// tab-width: 4 //
446// mode: c++ //
447// compile-command: "make install" //
448// End: //
Note: See TracBrowser for help on using the repository browser.