[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 |
---|
| 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 | |
---|
[a300e4a] | 18 | #include <string> // for string, to_string |
---|
| 19 | #include <unordered_map> |
---|
[2bb4a01] | 20 | #include <vector> |
---|
| 21 | |
---|
[a300e4a] | 22 | #include "FunctionSpec.hpp" |
---|
[2bb4a01] | 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" |
---|
[a300e4a] | 28 | #include "Type.hpp" // for Type, ptr<Type> |
---|
[2bb4a01] | 29 | #include "Visitor.hpp" |
---|
[a300e4a] | 30 | #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate |
---|
[2bb4a01] | 31 | |
---|
| 32 | namespace ast { |
---|
[a300e4a] | 33 | |
---|
[2bb4a01] | 34 | class Attribute; |
---|
| 35 | class Expr; |
---|
[77a3f41] | 36 | class Init; |
---|
[a300e4a] | 37 | class TypeDecl; |
---|
[2bb4a01] | 38 | |
---|
| 39 | /// Base declaration class |
---|
| 40 | class Decl : public ParseNode { |
---|
| 41 | public: |
---|
| 42 | std::string name; |
---|
| 43 | Storage::Classes storage; |
---|
| 44 | Linkage::Spec linkage; |
---|
| 45 | UniqueId uniqueId = 0; |
---|
| 46 | bool extension = false; |
---|
| 47 | |
---|
[14cebb7a] | 48 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
[2bb4a01] | 49 | Linkage::Spec linkage ) |
---|
| 50 | : ParseNode( loc ), name( name ), storage( storage ), linkage( linkage ) {} |
---|
| 51 | |
---|
| 52 | Decl* set_extension( bool ex ) { extension = ex; return this; } |
---|
| 53 | |
---|
| 54 | /// Ensures this node has a unique ID |
---|
| 55 | void fixUniqueId(); |
---|
| 56 | /// Get canonical declaration for unique ID |
---|
| 57 | static readonly<Decl> fromId( UniqueId id ); |
---|
| 58 | |
---|
| 59 | virtual Decl* accept( Visitor& v ) override = 0; |
---|
| 60 | private: |
---|
| 61 | virtual Decl* clone() const override = 0; |
---|
| 62 | }; |
---|
| 63 | |
---|
| 64 | /// Typed declaration base class |
---|
| 65 | class DeclWithType : public Decl { |
---|
| 66 | public: |
---|
| 67 | /// Represents the type with all types and typedefs expanded. |
---|
| 68 | /// This field is generated by SymTab::Validate::Pass2 |
---|
| 69 | std::string mangleName; |
---|
[14cebb7a] | 70 | /// Stores the scope level at which the variable was declared. |
---|
[2bb4a01] | 71 | /// Used to access shadowed identifiers. |
---|
| 72 | int scopeLevel = 0; |
---|
| 73 | |
---|
| 74 | std::vector<ptr<Attribute>> attributes; |
---|
[a300e4a] | 75 | Function::Specs funcSpec; |
---|
[2bb4a01] | 76 | ptr<Expr> asmName; |
---|
| 77 | bool isDeleted = false; |
---|
| 78 | |
---|
[14cebb7a] | 79 | DeclWithType( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
[2bb4a01] | 80 | Linkage::Spec linkage, std::vector<ptr<Attribute>>&& attrs, Function::Specs fs ) |
---|
[14cebb7a] | 81 | : Decl( loc, name, storage, linkage ), mangleName(), attributes( std::move(attrs) ), |
---|
[a300e4a] | 82 | funcSpec(fs), asmName() {} |
---|
[14cebb7a] | 83 | |
---|
[a300e4a] | 84 | std::string scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); } |
---|
| 85 | |
---|
| 86 | /// Get type of this declaration. May be generated by subclass |
---|
[77a3f41] | 87 | virtual const Type* get_type() const = 0; |
---|
[a300e4a] | 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; |
---|
| 92 | private: |
---|
| 93 | virtual DeclWithType* clone() const override = 0; |
---|
| 94 | }; |
---|
| 95 | |
---|
[77a3f41] | 96 | /// Object declaration `Foo foo = 42;` |
---|
| 97 | class ObjectDecl final : public DeclWithType { |
---|
| 98 | public: |
---|
| 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, |
---|
[14cebb7a] | 104 | Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, Expr* bitWd = nullptr, |
---|
[77a3f41] | 105 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) |
---|
[14cebb7a] | 106 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
[77a3f41] | 107 | init( init ), bitfieldWidth( bitWd ) {} |
---|
[14cebb7a] | 108 | |
---|
[77a3f41] | 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 ); } |
---|
| 113 | private: |
---|
| 114 | ObjectDecl* clone() const override { return new ObjectDecl{ *this }; } |
---|
| 115 | }; |
---|
| 116 | |
---|
[a300e4a] | 117 | /// Aggregate type declaration base class |
---|
| 118 | class AggregateDecl : public Decl { |
---|
| 119 | public: |
---|
| 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 | |
---|
[14cebb7a] | 126 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 127 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
[14cebb7a] | 128 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), parameters(), |
---|
[a300e4a] | 129 | attributes( std::move(attrs) ) {} |
---|
[14cebb7a] | 130 | |
---|
[a300e4a] | 131 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
| 132 | |
---|
| 133 | protected: |
---|
| 134 | /// Produces a name for the kind of aggregate |
---|
| 135 | virtual std::string typeString() const = 0; |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | /// struct declaration `struct Foo { ... };` |
---|
| 139 | class StructDecl final : public AggregateDecl { |
---|
| 140 | public: |
---|
| 141 | DeclarationNode::Aggregate kind; |
---|
| 142 | |
---|
[14cebb7a] | 143 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
| 144 | DeclarationNode::Aggregate kind = DeclarationNode::Struct, |
---|
[a300e4a] | 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 ); } |
---|
| 153 | private: |
---|
| 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 { ... };` |
---|
| 160 | class UnionDecl final : public AggregateDecl { |
---|
| 161 | public: |
---|
[14cebb7a] | 162 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 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 ); } |
---|
| 167 | private: |
---|
| 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 { ... };` |
---|
| 174 | class EnumDecl final : public AggregateDecl { |
---|
| 175 | public: |
---|
[14cebb7a] | 176 | EnumDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 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 ); } |
---|
| 184 | private: |
---|
| 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( ... ) { ... };` |
---|
| 194 | class TraitDecl final : public AggregateDecl { |
---|
| 195 | public: |
---|
[14cebb7a] | 196 | TraitDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 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 ); } |
---|
| 201 | private: |
---|
| 202 | TraitDecl* clone() const override { return new TraitDecl{ *this }; } |
---|
| 203 | |
---|
| 204 | std::string typeString() const override { return "trait"; } |
---|
[2bb4a01] | 205 | }; |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | // Local Variables: // |
---|
| 210 | // tab-width: 4 // |
---|
| 211 | // mode: c++ // |
---|
| 212 | // compile-command: "make install" // |
---|
| 213 | // End: // |
---|