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 |
---|
11 | // Last Modified By : Aaron B. Moss |
---|
12 | // Last Modified On : Thu May 9 10:00:00 2019 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <string> // for string, to_string |
---|
19 | #include <unordered_map> |
---|
20 | #include <vector> |
---|
21 | |
---|
22 | #include "FunctionSpec.hpp" |
---|
23 | #include "Fwd.hpp" // for UniqueId |
---|
24 | #include "LinkageSpec.hpp" |
---|
25 | #include "Node.hpp" // for ptr, readonly |
---|
26 | #include "ParseNode.hpp" |
---|
27 | #include "StorageClasses.hpp" |
---|
28 | #include "Type.hpp" // for Type, ptr<Type> |
---|
29 | #include "Visitor.hpp" |
---|
30 | #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate |
---|
31 | |
---|
32 | namespace ast { |
---|
33 | |
---|
34 | class Attribute; |
---|
35 | class Expr; |
---|
36 | class TypeDecl; |
---|
37 | |
---|
38 | /// Base declaration class |
---|
39 | class Decl : public ParseNode { |
---|
40 | public: |
---|
41 | std::string name; |
---|
42 | Storage::Classes storage; |
---|
43 | Linkage::Spec linkage; |
---|
44 | UniqueId uniqueId = 0; |
---|
45 | bool extension = false; |
---|
46 | |
---|
47 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
48 | Linkage::Spec linkage ) |
---|
49 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
50 | |
---|
51 | Decl* set_extension( bool ex ) { extension = ex; return this; } |
---|
52 | |
---|
53 | /// Ensures this node has a unique ID |
---|
54 | void fixUniqueId(); |
---|
55 | /// Get canonical declaration for unique ID |
---|
56 | static readonly<Decl> fromId( UniqueId id ); |
---|
57 | |
---|
58 | virtual Decl* accept( Visitor& v ) override = 0; |
---|
59 | private: |
---|
60 | virtual Decl* clone() const override = 0; |
---|
61 | }; |
---|
62 | |
---|
63 | /// Typed declaration base class |
---|
64 | class DeclWithType : public Decl { |
---|
65 | public: |
---|
66 | /// Represents the type with all types and typedefs expanded. |
---|
67 | /// This field is generated by SymTab::Validate::Pass2 |
---|
68 | std::string mangleName; |
---|
69 | /// Stores the scope level at which the variable was declared. |
---|
70 | /// Used to access shadowed identifiers. |
---|
71 | int scopeLevel = 0; |
---|
72 | |
---|
73 | std::vector<ptr<Attribute>> attributes; |
---|
74 | Function::Specs funcSpec; |
---|
75 | ptr<Expr> asmName; |
---|
76 | bool isDeleted = false; |
---|
77 | |
---|
78 | DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
79 | Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) |
---|
80 | : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ), |
---|
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"; } |
---|
183 | }; |
---|
184 | |
---|
185 | } |
---|
186 | |
---|
187 | // Local Variables: // |
---|
188 | // tab-width: 4 // |
---|
189 | // mode: c++ // |
---|
190 | // compile-command: "make install" // |
---|
191 | // End: // |
---|