source: src/AST/Decl.hpp @ 835d6e8

ADTast-experimental
Last change on this file since 835d6e8 was 3e94a23, checked in by Andrew Beach <ajbeach@…>, 15 months ago

More use of the new AST named boolean types.

  • 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"
[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;
[16ba4a6]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
[3e94a23]124/// Function variable arguments flag
125enum ArgumentFlag { FixedArgs, VariableArgs };
126
[8a5530c]127/// Object declaration `int foo()`
[23f99e1]128class FunctionDecl : public DeclWithType {
129public:
[a00a2c1]130        std::vector<ptr<TypeDecl>> type_params;
131        std::vector<ptr<DeclWithType>> assertions;
[b859f59]132        std::vector<ptr<DeclWithType>> params;
133        std::vector<ptr<DeclWithType>> returns;
[954c954]134        // declared type, derived from parameter declarations
[23f99e1]135        ptr<FunctionType> type;
[b8ab91a]136        /// Null for the forward declaration of a function.
[23f99e1]137        ptr<CompoundStmt> stmts;
[d76c588]138        std::vector< ptr<Expr> > withExprs;
[23f99e1]139
[7edd5c1]140        // The difference between the two constructors is in how they handle
141        // assertions. The first constructor uses the assertions from the type
142        // parameters, in the style of the old ast, and puts them on the type.
143        // The second takes an explicite list of assertions and builds a list of
144        // references to them on the type.
145
[490fb92e]146        FunctionDecl( const CodeLocation & loc, const std::string & name, std::vector<ptr<TypeDecl>>&& forall,
[954c954]147                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]148                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]149                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[7edd5c1]150
151        FunctionDecl( const CodeLocation & location, const std::string & name,
152                std::vector<ptr<TypeDecl>>&& forall, std::vector<ptr<DeclWithType>>&& assertions,
153                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[3992098]154                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
[3e94a23]155                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}, ArgumentFlag isVarArgs = FixedArgs );
[23f99e1]156
[8a5530c]157        const Type * get_type() const override;
[e0e9a0b]158        void set_type( const Type * t ) override;
[23f99e1]159
160        bool has_body() const { return stmts; }
161
[07de76b]162        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]163private:
164        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]165        MUTATE_FRIEND
[77a3f41]166};
167
[360b2e13]168/// Base class for named type aliases
169class NamedTypeDecl : public Decl {
170public:
171        ptr<Type> base;
172        std::vector<ptr<DeclWithType>> assertions;
173
[7030dab]174        NamedTypeDecl(
[e0e9a0b]175                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
176                const Type * b, Linkage::Spec spec = Linkage::Cforall )
[6a45bd78]177        : Decl( loc, name, storage, spec ), base( b ), assertions() {}
[360b2e13]178
179        /// Produces a name for the kind of alias
[312029a]180        virtual const char * typeString() const = 0;
[360b2e13]181
182private:
183        NamedTypeDecl* clone() const override = 0;
[f3cc5b6]184        MUTATE_FRIEND
[360b2e13]185};
186
187/// Cforall type variable: `dtype T`
188class TypeDecl final : public NamedTypeDecl {
[07de76b]189  public:
[6e50a6b]190        enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
[07de76b]191
192        Kind kind;
[360b2e13]193        bool sized;
194        ptr<Type> init;
195
[7030dab]196        TypeDecl(
197                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
[e3bc51c]198                const Type * b, TypeDecl::Kind k, bool s, const Type * i = nullptr )
199        : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeDecl::Ttype || s ),
[9e1d485]200          init( i ) {}
[360b2e13]201
[312029a]202        const char * typeString() const override;
[360b2e13]203        /// Produces a name for generated code
[312029a]204        const char * genTypeString() const;
[360b2e13]205
[9e1d485]206        /// convenience accessor to match Type::isComplete()
[3606fe4]207        bool isComplete() const { return sized; }
[9e1d485]208
[23f99e1]209        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[07de76b]210  private:
[23f99e1]211        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[f3cc5b6]212        MUTATE_FRIEND
[360b2e13]213};
214
[93c10de]215/// Data extracted from a TypeDecl.
216struct TypeData {
217        TypeDecl::Kind kind;
218        bool isComplete;
219
220        TypeData() : kind( TypeDecl::NUMBER_OF_KINDS ), isComplete( false ) {}
221        TypeData( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
222        TypeData( TypeDecl::Kind k, bool c ) : kind( k ), isComplete( c ) {}
223        TypeData( const TypeData & d1, const TypeData & d2 )
224                : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
225
226        bool operator==( const TypeData & o ) const { return kind == o.kind && isComplete == o.isComplete; }
227        bool operator!=( const TypeData & o ) const { return !(*this == o); }
228};
229
230std::ostream & operator<< ( std::ostream &, const TypeData & );
[d76c588]231
[360b2e13]232/// C-style typedef `typedef Foo Bar`
233class TypedefDecl final : public NamedTypeDecl {
234public:
[e0115286]235        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]236                Type* b, Linkage::Spec spec = Linkage::Cforall )
237        : NamedTypeDecl( loc, name, storage, b, spec ) {}
238
[312029a]239        const char * typeString() const override { return "typedef"; }
[360b2e13]240
[23f99e1]241        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]242private:
[23f99e1]243        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]244        MUTATE_FRIEND
[360b2e13]245};
246
[a300e4a]247/// Aggregate type declaration base class
248class AggregateDecl : public Decl {
249public:
[312029a]250        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
251        static const char * aggrString( Aggregate aggr );
252
[a300e4a]253        std::vector<ptr<Decl>> members;
[54e41b3]254        std::vector<ptr<TypeDecl>> params;
[a300e4a]255        std::vector<ptr<Attribute>> attributes;
256        bool body = false;
257        readonly<AggregateDecl> parent = {};
258
[14cebb7a]259        AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]260                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]261        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]262          attributes( std::move(attrs) ) {}
[14cebb7a]263
[a300e4a]264        AggregateDecl* set_body( bool b ) { body = b; return this; }
265
[ed3935da]266        /// Produces a name for the kind of aggregate
[312029a]267        virtual const char * typeString() const = 0;
[ed3935da]268
[f3cc5b6]269private:
270        AggregateDecl * clone() const override = 0;
271        MUTATE_FRIEND
[a300e4a]272};
273
274/// struct declaration `struct Foo { ... };`
275class StructDecl final : public AggregateDecl {
276public:
[312029a]277        Aggregate kind;
[a300e4a]278
[14cebb7a]279        StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]280                Aggregate kind = Struct,
[a300e4a]281                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
282        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
283
[3cc1111]284        bool is_coroutine() const { return kind == Coroutine; }
285        bool is_generator() const { return kind == Generator; }
286        bool is_monitor  () const { return kind == Monitor  ; }
287        bool is_thread   () const { return kind == Thread   ; }
[a300e4a]288
[23f99e1]289        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]290
[312029a]291        const char * typeString() const override { return aggrString( kind ); }
[ed3935da]292
[a300e4a]293private:
[23f99e1]294        StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]295        MUTATE_FRIEND
[a300e4a]296};
297
298/// union declaration `union Foo { ... };`
299class UnionDecl final : public AggregateDecl {
300public:
[14cebb7a]301        UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]302                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
303        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
304
[23f99e1]305        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[ed3935da]306
[312029a]307        const char * typeString() const override { return aggrString( Union ); }
[ed3935da]308
[a300e4a]309private:
[23f99e1]310        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]311        MUTATE_FRIEND
[a300e4a]312};
313
314/// enum declaration `enum Foo { ... };`
315class EnumDecl final : public AggregateDecl {
316public:
[5408b59]317        bool isTyped; // isTyped indicated if the enum has a declaration like:
318        // enum (type_optional) Name {...}
319        ptr<Type> base; // if isTyped == true && base.get() == nullptr, it is a "void" type enum
[e4d7c1c]320        enum class EnumHiding { Visible, Hide } hide;
[f135b50]321
[e4d7c1c]322        EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false,
[b0d9ff7]323                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall,
[e4d7c1c]324                Type const * base = nullptr, EnumHiding hide = EnumHiding::Hide,
[7b71402]325                std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() )
[e4d7c1c]326        : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), enumValues(enumValues) {}
[a300e4a]327
328        /// gets the integer value for this enumerator, returning true iff value found
[f135b50]329        // Maybe it is not used in producing the enum value
[9d6e7fa9]330        bool valueOf( const Decl * enumerator, long long& value ) const;
[a300e4a]331
[23f99e1]332        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]333
[312029a]334        const char * typeString() const override { return aggrString( Enum ); }
[ed3935da]335
[4559b34]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 (...) ...`
362class WithStmt final : public Decl {
363public:
364        std::vector<ptr<Expr>> exprs;
365        ptr<Stmt> stmt;
366
367        WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
368        : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
369
370        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
371private:
372        WithStmt * clone() const override { return new WithStmt{ *this }; }
373        MUTATE_FRIEND
374};
375
[23f99e1]376class AsmDecl : public Decl {
377public:
378        ptr<AsmStmt> stmt;
379
[07de76b]380        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[23f99e1]381        : Decl( loc, "", {}, {} ), stmt(stmt) {}
382
[07de76b]383        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]384private:
[07de76b]385        AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]386        MUTATE_FRIEND
[23f99e1]387};
388
[2d019af]389/// C-preprocessor directive `#...`
390class DirectiveDecl : public Decl {
391public:
392        ptr<DirectiveStmt> stmt;
393
394        DirectiveDecl( const CodeLocation & loc, DirectiveStmt * stmt )
395        : Decl( loc, "", {}, {} ), stmt(stmt) {}
396
397        const DirectiveDecl * accept( Visitor & v ) const override { return v.visit( this ); }
398private:
399        DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
400        MUTATE_FRIEND
401};
402
[19a8c40]403/// Static Assertion `_Static_assert( ... , ... );`
[23f99e1]404class StaticAssertDecl : public Decl {
405public:
[112fe04]406        ptr<Expr> cond;
[23f99e1]407        ptr<ConstantExpr> msg;   // string literal
408
409        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[112fe04]410        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
[23f99e1]411
[07de76b]412        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]413private:
414        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]415        MUTATE_FRIEND
[23f99e1]416};
[e0115286]417
[19a8c40]418/// Inline Member Declaration `inline TypeName;`
[71806e0]419class InlineMemberDecl final : public DeclWithType {
[e874605]420public:
421        ptr<Type> type;
422
[71806e0]423        InlineMemberDecl( const CodeLocation & loc, const std::string & name, const Type * type,
[e874605]424                Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::Cforall,
425                std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} )
426        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ) {}
427
428        const Type * get_type() const override { return type; }
429        void set_type( const Type * ty ) override { type = ty; }
430
431        const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); }
432private:
[71806e0]433        InlineMemberDecl * clone() const override { return new InlineMemberDecl{ *this }; }
[e874605]434        MUTATE_FRIEND
435};
[19a8c40]436
[2bb4a01]437}
438
[f3cc5b6]439#undef MUTATE_FRIEND
440
[2bb4a01]441// Local Variables: //
442// tab-width: 4 //
443// mode: c++ //
444// compile-command: "make install" //
445// End: //
Note: See TracBrowser for help on using the repository browser.