Changes in src/AST/Decl.hpp [2bb4a01:a300e4a]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.hpp
r2bb4a01 ra300e4a 16 16 #pragma once 17 17 18 #include <string> 18 #include <string> // for string, to_string 19 #include <unordered_map> 19 20 #include <vector> 20 21 22 #include "FunctionSpec.hpp" 21 23 #include "Fwd.hpp" // for UniqueId 22 24 #include "LinkageSpec.hpp" … … 24 26 #include "ParseNode.hpp" 25 27 #include "StorageClasses.hpp" 28 #include "Type.hpp" // for Type, ptr<Type> 26 29 #include "Visitor.hpp" 30 #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate 27 31 28 32 namespace ast { 33 29 34 class Attribute; 30 35 class Expr; 36 class TypeDecl; 31 37 32 38 /// Base declaration class … … 66 72 67 73 std::vector<ptr<Attribute>> attributes; 68 Function::Specs funcSpec s;74 Function::Specs funcSpec; 69 75 ptr<Expr> asmName; 70 76 bool isDeleted = false; … … 73 79 Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) 74 80 : 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; 91 private: 92 virtual DeclWithType* clone() const override = 0; 93 }; 94 95 /// Aggregate type declaration base class 96 class AggregateDecl : public Decl { 97 public: 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 111 protected: 112 /// Produces a name for the kind of aggregate 113 virtual std::string typeString() const = 0; 114 }; 115 116 /// struct declaration `struct Foo { ... };` 117 class StructDecl final : public AggregateDecl { 118 public: 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 ); } 131 private: 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 { ... };` 138 class UnionDecl final : public AggregateDecl { 139 public: 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 ); } 145 private: 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 { ... };` 152 class EnumDecl final : public AggregateDecl { 153 public: 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 ); } 162 private: 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( ... ) { ... };` 172 class TraitDecl final : public AggregateDecl { 173 public: 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 ); } 179 private: 180 TraitDecl* clone() const override { return new TraitDecl{ *this }; } 181 182 std::string typeString() const override { return "trait"; } 76 183 }; 77 184
Note: See TracChangeset
for help on using the changeset viewer.