Changeset a300e4a for src/AST/Decl.hpp


Ignore:
Timestamp:
May 9, 2019, 5:17:51 PM (5 years ago)
Author:
Aaron Moss <a3moss@…>
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:
3e46cc8
Parents:
2bb4a01
Message:

Add some decls to the new AST

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/AST/Decl.hpp

    r2bb4a01 ra300e4a  
    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 TypeDecl;
    3137
    3238/// Base declaration class
     
    6672
    6773        std::vector<ptr<Attribute>> attributes;
    68         Function::Specs funcSpecs;
     74        Function::Specs funcSpec;
    6975        ptr<Expr> asmName;
    7076        bool isDeleted = false;
     
    7379                Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs )
    7480        : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ),
    75                 funcSpecs(fs), asmName() {}
     81                funcSpec(fs), asmName() {}
     82       
     83        std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
     84
     85        /// Get type of this declaration. May be generated by subclass
     86        virtual const Type* type() const = 0;
     87        /// Set type of this declaration. May be verified by subclass
     88        virtual void set_type(Type*) = 0;
     89
     90        virtual DeclWithType* accept( Visitor& v ) override = 0;
     91private:
     92        virtual DeclWithType* clone() const override = 0;
     93};
     94
     95/// Aggregate type declaration base class
     96class AggregateDecl : public Decl {
     97public:
     98        std::vector<ptr<Decl>> members;
     99        std::vector<ptr<TypeDecl>> parameters;
     100        std::vector<ptr<Attribute>> attributes;
     101        bool body = false;
     102        readonly<AggregateDecl> parent = {};
     103
     104        AggregateDecl( const CodeLocation& loc, const std::string& name,
     105                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     106        : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(),
     107          attributes( std::move(attrs) ) {}
     108       
     109        AggregateDecl* set_body( bool b ) { body = b; return this; }
     110
     111protected:
     112        /// Produces a name for the kind of aggregate
     113        virtual std::string typeString() const = 0;
     114};
     115
     116/// struct declaration `struct Foo { ... };`
     117class StructDecl final : public AggregateDecl {
     118public:
     119        DeclarationNode::Aggregate kind;
     120
     121        StructDecl( const CodeLocation& loc, const std::string& name,
     122                DeclarationNode::Aggregate kind = DeclarationNode::Struct,
     123                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     124        : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {}
     125
     126        bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
     127        bool is_monitor() { return kind == DeclarationNode::Monitor; }
     128        bool is_thread() { return kind == DeclarationNode::Thread; }
     129
     130        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     131private:
     132        StructDecl* clone() const override { return new StructDecl{ *this }; }
     133
     134        std::string typeString() const override { return "struct"; }
     135};
     136
     137/// union declaration `union Foo { ... };`
     138class UnionDecl final : public AggregateDecl {
     139public:
     140        UnionDecl( const CodeLocation& loc, const std::string& name,
     141                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     142        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
     143
     144        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     145private:
     146        UnionDecl* clone() const override { return new UnionDecl{ *this }; }
     147
     148        std::string typeString() const override { return "union"; }
     149};
     150
     151/// enum declaration `enum Foo { ... };`
     152class EnumDecl final : public AggregateDecl {
     153public:
     154        EnumDecl( const CodeLocation& loc, const std::string& name,
     155                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     156        : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {}
     157
     158        /// gets the integer value for this enumerator, returning true iff value found
     159        bool valueOf( Decl* enumerator, long long& value ) const;
     160
     161        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     162private:
     163        EnumDecl* clone() const override { return new EnumDecl{ *this }; }
     164
     165        std::string typeString() const override { return "enum"; }
     166
     167        /// Map from names to enumerator values; kept private for lazy initialization
     168        mutable std::unordered_map< std::string, long long > enumValues;
     169};
     170
     171/// trait declaration `trait Foo( ... ) { ... };`
     172class TraitDecl final : public AggregateDecl {
     173public:
     174        TraitDecl( const CodeLocation& loc, const std::string& name,
     175                std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall )
     176        : AggregateDecl( loc, name, std::move(attrs), linkage ) {}
     177
     178        Decl* accept( Visitor& v ) override { return v.visit( this ); }
     179private:
     180        TraitDecl* clone() const override { return new TraitDecl{ *this }; }
     181
     182        std::string typeString() const override { return "trait"; }
    76183};
    77184
Note: See TracChangeset for help on using the changeset viewer.