source: src/AST/Decl.hpp @ 76d73fc

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 76d73fc was 954c954, checked in by Fangren Yu <f37yu@…>, 4 years ago

Move function argument and return variable declarations from FunctionType? to FunctionDecl?

  • Property mode set to 100644
File size: 12.5 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
[99da267]35#define MUTATE_FRIEND \
36    template<typename node_t> friend node_t * mutate(const node_t * node); \
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;
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 = {},
109                Linkage::Spec linkage = Linkage::C, 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
[8a5530c]123/// Object declaration `int foo()`
[23f99e1]124class FunctionDecl : public DeclWithType {
125public:
[954c954]126        std::vector<ptr<DeclWithType>> params;
127        std::vector<ptr<DeclWithType>> returns;
128        // declared type, derived from parameter declarations
[23f99e1]129        ptr<FunctionType> type;
130        ptr<CompoundStmt> stmts;
[d76c588]131        std::vector< ptr<Expr> > withExprs;
[23f99e1]132
[954c954]133        FunctionDecl( const CodeLocation & loc, const std::string & name, 
134                std::vector<ptr<DeclWithType>>&& params, std::vector<ptr<DeclWithType>>&& returns,
[23f99e1]135                CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C,
136                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
[954c954]137        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), params(std::move(params)), returns(std::move(returns)),
[23f99e1]138          stmts( stmts ) {}
139
[8a5530c]140        const Type * get_type() const override;
[e0e9a0b]141        void set_type( const Type * t ) override;
[23f99e1]142
143        bool has_body() const { return stmts; }
144
[07de76b]145        const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]146private:
147        FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
[f3cc5b6]148        MUTATE_FRIEND
[77a3f41]149};
150
[360b2e13]151/// Base class for named type aliases
152class NamedTypeDecl : public Decl {
153public:
154        ptr<Type> base;
[54e41b3]155        std::vector<ptr<TypeDecl>> params;
[360b2e13]156        std::vector<ptr<DeclWithType>> assertions;
157
[7030dab]158        NamedTypeDecl(
[e0e9a0b]159                const CodeLocation & loc, const std::string & name, Storage::Classes storage,
160                const Type * b, Linkage::Spec spec = Linkage::Cforall )
[54e41b3]161        : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {}
[360b2e13]162
163        /// Produces a name for the kind of alias
[312029a]164        virtual const char * typeString() const = 0;
[360b2e13]165
166private:
167        NamedTypeDecl* clone() const override = 0;
[f3cc5b6]168        MUTATE_FRIEND
[360b2e13]169};
170
171/// Cforall type variable: `dtype T`
172class TypeDecl final : public NamedTypeDecl {
[07de76b]173  public:
174        enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS };
175
176        Kind kind;
[360b2e13]177        bool sized;
178        ptr<Type> init;
179
180        /// Data extracted from a type decl
181        struct Data {
[07de76b]182                Kind kind;
[360b2e13]183                bool isComplete;
184
[07de76b]185                Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
[d76c588]186                Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {}
[07de76b]187                Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}
[d76c588]188                Data( const Data & d1, const Data & d2 )
[07de76b]189                        : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
[360b2e13]190
[07de76b]191                bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; }
192                bool operator!=( const Data & o ) const { return !(*this == o); }
[360b2e13]193        };
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()
206        bool isComplete() { return sized; }
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
[d76c588]214std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & );
215
[360b2e13]216/// C-style typedef `typedef Foo Bar`
217class TypedefDecl final : public NamedTypeDecl {
218public:
[e0115286]219        TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
[360b2e13]220                Type* b, Linkage::Spec spec = Linkage::Cforall )
221        : NamedTypeDecl( loc, name, storage, b, spec ) {}
222
[312029a]223        const char * typeString() const override { return "typedef"; }
[360b2e13]224
[23f99e1]225        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[360b2e13]226private:
[23f99e1]227        TypedefDecl * clone() const override { return new TypedefDecl{ *this }; }
[f3cc5b6]228        MUTATE_FRIEND
[360b2e13]229};
230
[a300e4a]231/// Aggregate type declaration base class
232class AggregateDecl : public Decl {
233public:
[312029a]234        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };
235        static const char * aggrString( Aggregate aggr );
236
[a300e4a]237        std::vector<ptr<Decl>> members;
[54e41b3]238        std::vector<ptr<TypeDecl>> params;
[a300e4a]239        std::vector<ptr<Attribute>> attributes;
240        bool body = false;
241        readonly<AggregateDecl> parent = {};
242
[14cebb7a]243        AggregateDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]244                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
[54e41b3]245        : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(),
[a300e4a]246          attributes( std::move(attrs) ) {}
[14cebb7a]247
[a300e4a]248        AggregateDecl* set_body( bool b ) { body = b; return this; }
249
[ed3935da]250        /// Produces a name for the kind of aggregate
[312029a]251        virtual const char * typeString() const = 0;
[ed3935da]252
[f3cc5b6]253private:
254        AggregateDecl * clone() const override = 0;
255        MUTATE_FRIEND
[a300e4a]256};
257
258/// struct declaration `struct Foo { ... };`
259class StructDecl final : public AggregateDecl {
260public:
[312029a]261        Aggregate kind;
[a300e4a]262
[14cebb7a]263        StructDecl( const CodeLocation& loc, const std::string& name,
[312029a]264                Aggregate kind = Struct,
[a300e4a]265                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
266        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
267
[312029a]268        bool is_coroutine() { return kind == Coroutine; }
[427854b]269        bool is_generator() { return kind == Generator; }
270        bool is_monitor  () { return kind == Monitor  ; }
271        bool is_thread   () { return kind == Thread   ; }
[a300e4a]272
[23f99e1]273        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]274
[312029a]275        const char * typeString() const override { return aggrString( kind ); }
[ed3935da]276
[a300e4a]277private:
[23f99e1]278        StructDecl * clone() const override { return new StructDecl{ *this }; }
[f3cc5b6]279        MUTATE_FRIEND
[a300e4a]280};
281
282/// union declaration `union Foo { ... };`
283class UnionDecl final : public AggregateDecl {
284public:
[14cebb7a]285        UnionDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]286                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
287        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
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( Union ); }
[ed3935da]292
[a300e4a]293private:
[23f99e1]294        UnionDecl * clone() const override { return new UnionDecl{ *this }; }
[f3cc5b6]295        MUTATE_FRIEND
[a300e4a]296};
297
298/// enum declaration `enum Foo { ... };`
299class EnumDecl final : public AggregateDecl {
300public:
[14cebb7a]301        EnumDecl( 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 ), enumValues() {}
304
305        /// gets the integer value for this enumerator, returning true iff value found
[9d6e7fa9]306        bool valueOf( const Decl * enumerator, long long& value ) const;
[a300e4a]307
[23f99e1]308        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]309
[312029a]310        const char * typeString() const override { return aggrString( Enum ); }
[ed3935da]311
[a300e4a]312private:
[23f99e1]313        EnumDecl * clone() const override { return new EnumDecl{ *this }; }
[f3cc5b6]314        MUTATE_FRIEND
[a300e4a]315
316        /// Map from names to enumerator values; kept private for lazy initialization
317        mutable std::unordered_map< std::string, long long > enumValues;
318};
319
320/// trait declaration `trait Foo( ... ) { ... };`
321class TraitDecl final : public AggregateDecl {
322public:
[14cebb7a]323        TraitDecl( const CodeLocation& loc, const std::string& name,
[a300e4a]324                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
325        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
326
[23f99e1]327        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
[ed3935da]328
[312029a]329        const char * typeString() const override { return "trait"; }
[ed3935da]330
[a300e4a]331private:
[23f99e1]332        TraitDecl * clone() const override { return new TraitDecl{ *this }; }
[f3cc5b6]333        MUTATE_FRIEND
[2bb4a01]334};
335
[e67991f]336/// With statement `with (...) ...`
337class WithStmt final : public Decl {
338public:
339        std::vector<ptr<Expr>> exprs;
340        ptr<Stmt> stmt;
341
342        WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )
343        : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}
344
345        const Decl * accept( Visitor & v ) const override { return v.visit( this ); }
346private:
347        WithStmt * clone() const override { return new WithStmt{ *this }; }
348        MUTATE_FRIEND
349};
350
[23f99e1]351class AsmDecl : public Decl {
352public:
353        ptr<AsmStmt> stmt;
354
[07de76b]355        AsmDecl( const CodeLocation & loc, AsmStmt * stmt )
[23f99e1]356        : Decl( loc, "", {}, {} ), stmt(stmt) {}
357
[07de76b]358        const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]359private:
[07de76b]360        AsmDecl * clone() const override { return new AsmDecl( *this ); }
[f3cc5b6]361        MUTATE_FRIEND
[23f99e1]362};
363
364class StaticAssertDecl : public Decl {
365public:
[112fe04]366        ptr<Expr> cond;
[23f99e1]367        ptr<ConstantExpr> msg;   // string literal
368
369        StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg )
[112fe04]370        : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {}
[23f99e1]371
[07de76b]372        const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }
[23f99e1]373private:
374        StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
[f3cc5b6]375        MUTATE_FRIEND
[23f99e1]376};
[e0115286]377
[2bb4a01]378}
379
[f3cc5b6]380#undef MUTATE_FRIEND
381
[2bb4a01]382// Local Variables: //
383// tab-width: 4 //
384// mode: c++ //
385// compile-command: "make install" //
386// End: //
Note: See TracBrowser for help on using the repository browser.