source: src/AST/Decl.hpp @ 74e3263

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 74e3263 was 07de76b, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

remove file TypeVar?.h* and put TypeVar::Kind into TypeDecl?, move LinkageSpec?.* from directory Parse to SynTree?

  • Property mode set to 100644
File size: 12.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
[312029a]11// Last Modified By : Peter A. Buhr
[07de76b]12// Last Modified On : Fri Dec 13 17:38:33 2019
13// Update Count     : 29
[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
[41b24c8]35#define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(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        /// Get canonical declaration for unique ID
57        static readonly<Decl> fromId( UniqueId id );
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        /// This field is generated by SymTab::Validate::Pass2
70        std::string mangleName;
[14cebb7a]71        /// Stores the scope level at which the variable was declared.
[2bb4a01]72        /// Used to access shadowed identifiers.
73        int scopeLevel = 0;
74
75        std::vector<ptr<Attribute>> attributes;
[a300e4a]76        Function::Specs funcSpec;
[2bb4a01]77        ptr<Expr> asmName;
78        bool isDeleted = false;
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
[23f99e1]90        virtual void set_type(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 = {},
107                Linkage::Spec linkage = Linkage::C, 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; }
[23f99e1]113        void set_type( 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
[8a5530c]121/// Object declaration `int foo()`
[23f99e1]122class FunctionDecl : public DeclWithType {
123public:
124        ptr<FunctionType> type;
125        ptr<CompoundStmt> stmts;
[d76c588]126        std::vector< ptr<Expr> > withExprs;
[23f99e1]127
[07de76b]128        FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type,
[23f99e1]129                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
130                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
131        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
132          stmts( stmts ) {}
133
[8a5530c]134        const Type * get_type() const override;
135        void set_type(Type * t) override;
[23f99e1]136
137        bool has_body() const { return stmts; }
138
[07de76b]139        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]140private:
141        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]142        MUTATE_FRIEND
[77a3f41]143};
144
[360b2e13]145/// Base class for named type aliases
146class NamedTypeDecl : public Decl {
147public:
148        ptr<Type> base;
[54e41b3]149        std::vector<ptr<TypeDecl>> params;
[360b2e13]150        std::vector<ptr<DeclWithType>> assertions;
151
[e0115286]152        NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]153                Type* b, Linkage::Spec spec = Linkage::Cforall )
[54e41b3]154        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
[360b2e13]155
156        /// Produces a name for the kind of alias
[312029a]157        virtual const char * typeString() const = 0;
[360b2e13]158
159private:
160        NamedTypeDecl* clone() const override = 0;
[f3cc5b6]161        MUTATE_FRIEND
[360b2e13]162};
163
164/// Cforall type variable: `dtype T`
165class TypeDecl final : public NamedTypeDecl {
[07de76b]166  public:
167        enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
168
169        Kind kind;
[360b2e13]170        bool sized;
171        ptr<Type> init;
172
173        /// Data extracted from a type decl
174        struct Data {
[07de76b]175                Kind kind;
[360b2e13]176                bool isComplete;
177
[07de76b]178                Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
[d76c588]179                Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
[07de76b]180                Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
[d76c588]181                Data( const Data & d1, const Data & d2 )
[07de76b]182                        : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
[360b2e13]183
[07de76b]184                bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
185                bool operator!=( const Data & o ) const { return !(*this == o); }
[360b2e13]186        };
187
[07de76b]188        TypeDecl( const CodeLocation & loc, const std::string & name, Storage::Classes storage, Type * b,
189                          Kind k, bool s, Type * i = nullptr )
190                : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ),
191                init( i ) {}
[360b2e13]192
[312029a]193        const char * typeString() const override;
[360b2e13]194        /// Produces a name for generated code
[312029a]195        const char * genTypeString() const;
[360b2e13]196
[9e1d485]197        /// convenience accessor to match Type::isComplete()
198        bool isComplete() { return sized; }
199
[23f99e1]200        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[07de76b]201  private:
[23f99e1]202        TypeDecl * clone() const override { return new TypeDecl{ *this }; }
[f3cc5b6]203        MUTATE_FRIEND
[360b2e13]204};
205
[d76c588]206std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
207
[360b2e13]208/// C-style typedef `typedef Foo Bar`
209class TypedefDecl final : public NamedTypeDecl {
210public:
[e0115286]211        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]212                Type* b, Linkage::Spec spec = Linkage::Cforall )
213        : NamedTypeDecl( loc, name, storage, b, spec ) {}
214
[312029a]215        const char * typeString() const override { return "typedef"; }
[360b2e13]216
[23f99e1]217        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]218private:
[23f99e1]219        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]220        MUTATE_FRIEND
[360b2e13]221};
222
[a300e4a]223/// Aggregate type declaration base class
224class AggregateDecl : public Decl {
225public:
[312029a]226        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
227        static const char * aggrString( Aggregate aggr );
228
[a300e4a]229        std::vector<ptr<Decl>> members;
[54e41b3]230        std::vector<ptr<TypeDecl>> params;
[a300e4a]231        std::vector<ptr<Attribute>> attributes;
232        bool body = false;
233        readonly<AggregateDecl> parent = {};
234
[14cebb7a]235        AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]236                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]237        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]238          attributes( std::move(attrs) ) {}
[14cebb7a]239
[a300e4a]240        AggregateDecl* set_body( bool b ) { body = b; return this; }
241
[ed3935da]242        /// Produces a name for the kind of aggregate
[312029a]243        virtual const char * typeString() const = 0;
[ed3935da]244
[f3cc5b6]245private:
246        AggregateDecl * clone() const override = 0;
247        MUTATE_FRIEND
[a300e4a]248};
249
250/// struct declaration `struct Foo { ... };`
251class StructDecl final : public AggregateDecl {
252public:
[312029a]253        Aggregate kind;
[a300e4a]254
[14cebb7a]255        StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]256                Aggregate kind = Struct,
[a300e4a]257                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
258        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
259
[312029a]260        bool is_coroutine() { return kind == Coroutine; }
261        bool is_monitor() { return kind == Monitor; }
262        bool is_thread() { return kind == Thread; }
[a300e4a]263
[23f99e1]264        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]265
[312029a]266        const char * typeString() const override { return aggrString( kind ); }
[ed3935da]267
[a300e4a]268private:
[23f99e1]269        StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]270        MUTATE_FRIEND
[a300e4a]271};
272
273/// union declaration `union Foo { ... };`
274class UnionDecl final : public AggregateDecl {
275public:
[14cebb7a]276        UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]277                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
278        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
279
[23f99e1]280        const Decl * accept( Visitor& v ) const override { return v.visit( this ); }
[ed3935da]281
[312029a]282        const char * typeString() const override { return aggrString( Union ); }
[ed3935da]283
[a300e4a]284private:
[23f99e1]285        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]286        MUTATE_FRIEND
[a300e4a]287};
288
289/// enum declaration `enum Foo { ... };`
290class EnumDecl final : public AggregateDecl {
291public:
[14cebb7a]292        EnumDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]293                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
294        : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
295
296        /// gets the integer value for this enumerator, returning true iff value found
[9d6e7fa9]297        bool valueOf( const Decl * enumerator, long long& value ) const;
[a300e4a]298
[23f99e1]299        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]300
[312029a]301        const char * typeString() const override { return aggrString( Enum ); }
[ed3935da]302
[a300e4a]303private:
[23f99e1]304        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
[f3cc5b6]305        MUTATE_FRIEND
[a300e4a]306
307        /// Map from names to enumerator values; kept private for lazy initialization
308        mutable std::unordered_map< std::string, long long > enumValues;
309};
310
311/// trait declaration `trait Foo( ... ) { ... };`
312class TraitDecl final : public AggregateDecl {
313public:
[14cebb7a]314        TraitDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]315                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
316        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
317
[23f99e1]318        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]319
[312029a]320        const char * typeString() const override { return "trait"; }
[ed3935da]321
[a300e4a]322private:
[23f99e1]323        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
[f3cc5b6]324        MUTATE_FRIEND
[2bb4a01]325};
326
[e67991f]327/// With statement `with (...) ...`
328class WithStmt final : public Decl {
329public:
330        std::vector<ptr<Expr>> exprs;
331        ptr<Stmt> stmt;
332
333        WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
334        : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
335
336        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
337private:
338        WithStmt * clone() const override { return new WithStmt{ *this }; }
339        MUTATE_FRIEND
340};
341
[23f99e1]342class AsmDecl : public Decl {
343public:
344        ptr<AsmStmt> stmt;
345
[07de76b]346        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[23f99e1]347        : Decl( loc, "", {}, {} ), stmt(stmt) {}
348
[07de76b]349        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]350private:
[07de76b]351        AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]352        MUTATE_FRIEND
[23f99e1]353};
354
355class StaticAssertDecl : public Decl {
356public:
[112fe04]357        ptr<Expr> cond;
[23f99e1]358        ptr<ConstantExpr> msg;   // string literal
359
360        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[112fe04]361        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
[23f99e1]362
[07de76b]363        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]364private:
365        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]366        MUTATE_FRIEND
[23f99e1]367};
[e0115286]368
[2bb4a01]369}
370
[f3cc5b6]371#undef MUTATE_FRIEND
372
[2bb4a01]373// Local Variables: //
374// tab-width: 4 //
375// mode: c++ //
376// compile-command: "make install" //
377// End: //
Note: See TracBrowser for help on using the repository browser.