Changeset 36354b1 for src/AST/Decl.hpp


Ignore:
Timestamp:
May 10, 2019, 12:09:05 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
9131e54, cdcd53dc
Parents:
292d599b (diff), 14cebb7a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.hpp

    r292d599b r36354b1  
    1616#pragma once
    1717
    18 #include <string>
     18#include <string>              // for string, to_string
     19#include <unordered_map>
    1920#include <vector>
    2021
     22#include "FunctionSpec.hpp"
    2123#include "Fwd.hpp"             // for UniqueId
    2224#include "LinkageSpec.hpp"
     
    2426#include "ParseNode.hpp"
    2527#include "StorageClasses.hpp"
     28#include "Type.hpp"            // for Type, ptr<Type>
    2629#include "Visitor.hpp"
     30#include "Parser/ParseNode.h"  // for DeclarationNode::Aggregate
    2731
    2832namespace ast {
     33
    2934class Attribute;
    3035class Expr;
     36class Init;
     37class TypeDecl;
    3138
    3239/// Base declaration class
     
    3946        bool extension = false;
    4047
    41         Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, 
     48        Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
    4249                Linkage::Spec linkage )
    4350        : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {}
     
    6168        /// This field is generated by SymTab::Validate::Pass2
    6269        std::string mangleName;
    63         /// Stores the scope level at which the variable was declared. 
     70        /// Stores the scope level at which the variable was declared.
    6471        /// Used to access shadowed identifiers.
    6572        int scopeLevel = 0;
    6673
    6774        std::vector<ptr<Attribute>> attributes;
    68         Function::Specs funcSpecs;
     75        Function::Specs funcSpec;
    6976        ptr<Expr> asmName;
    7077        bool isDeleted = false;
    7178
    72         DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, 
     79        DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage,
    7380                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
    74         : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
    75                 funcSpecs(fs), asmName() {}
     81        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
     82                funcSpec(fs), asmName() {}
     83
     84        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
     85
     86        /// Get type of this declaration. May be generated by subclass
     87        virtual const Type* get_type() const = 0;
     88        /// Set type of this declaration. May be verified by subclass
     89        virtual void set_type(Type*) = 0;
     90
     91        virtual DeclWithType* accept( Visitor& v ) override = 0;
     92private:
     93        virtual DeclWithType* clone() const override = 0;
     94};
     95
     96/// Object declaration `Foo foo = 42;`
     97class ObjectDecl final : public DeclWithType {
     98public:
     99        ptr<Type> type;
     100        ptr<Init> init;
     101        ptr<Expr> bitfieldWidth;
     102
     103        ObjectDecl( const CodeLocation& loc, const std::string& name, Type* type, Init* init = nullptr,
     104                Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, Expr* bitWd = nullptr,
     105                std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {})
     106        : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ),
     107          init( init ), bitfieldWidth( bitWd ) {}
     108
     109        const Type* get_type() const override { return type; }
     110        void set_type( Type* ty ) override { type = ty; }
     111
     112        DeclWithType* accept( Visitor& v ) override { return v.visit( this ); }
     113private:
     114        ObjectDecl* clone() const override { return new ObjectDecl{ *this }; }
     115};
     116
     117/// Aggregate type declaration base class
     118class AggregateDecl : public Decl {
     119public:
     120        std::vector<ptr<Decl>> members;
     121        std::vector<ptr<TypeDecl>> parameters;
     122        std::vector<ptr<Attribute>> attributes;
     123        bool body = false;
     124        readonly<AggregateDecl> parent = {};
     125
     126        AggregateDecl( const CodeLocation& loc, const std::string& name,
     127                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     128        : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(),
     129          attributes( std::move(attrs) ) {}
     130
     131        AggregateDecl* set_body( bool b ) { body = b; return this; }
     132
     133protected:
     134        /// Produces a name for the kind of aggregate
     135        virtual std::string typeString() const = 0;
     136};
     137
     138/// struct declaration `struct Foo { ... };`
     139class StructDecl final : public AggregateDecl {
     140public:
     141        DeclarationNode::Aggregate kind;
     142
     143        StructDecl( const CodeLocation& loc, const std::string& name,
     144                DeclarationNode::Aggregate kind = DeclarationNode::Struct,
     145                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     146        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
     147
     148        bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
     149        bool is_monitor() { return kind == DeclarationNode::Monitor; }
     150        bool is_thread() { return kind == DeclarationNode::Thread; }
     151
     152        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     153private:
     154        StructDecl* clone() const override { return new StructDecl{ *this }; }
     155
     156        std::string typeString() const override { return "struct"; }
     157};
     158
     159/// union declaration `union Foo { ... };`
     160class UnionDecl final : public AggregateDecl {
     161public:
     162        UnionDecl( const CodeLocation& loc, const std::string& name,
     163                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     164        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
     165
     166        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     167private:
     168        UnionDecl* clone() const override { return new UnionDecl{ *this }; }
     169
     170        std::string typeString() const override { return "union"; }
     171};
     172
     173/// enum declaration `enum Foo { ... };`
     174class EnumDecl final : public AggregateDecl {
     175public:
     176        EnumDecl( const CodeLocation& loc, const std::string& name,
     177                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     178        : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
     179
     180        /// gets the integer value for this enumerator, returning true iff value found
     181        bool valueOf( Decl* enumerator, long long& value ) const;
     182
     183        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     184private:
     185        EnumDecl* clone() const override { return new EnumDecl{ *this }; }
     186
     187        std::string typeString() const override { return "enum"; }
     188
     189        /// Map from names to enumerator values; kept private for lazy initialization
     190        mutable std::unordered_map< std::string, long long > enumValues;
     191};
     192
     193/// trait declaration `trait Foo( ... ) { ... };`
     194class TraitDecl final : public AggregateDecl {
     195public:
     196        TraitDecl( const CodeLocation& loc, const std::string& name,
     197                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     198        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
     199
     200        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     201private:
     202        TraitDecl* clone() const override { return new TraitDecl{ *this }; }
     203
     204        std::string typeString() const override { return "trait"; }
    76205};
    77206
Note: See TracChangeset for help on using the changeset viewer.