[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 | |
---|
[d76c588] | 18 | #include <iosfwd> |
---|
[a300e4a] | 19 | #include <string> // for string, to_string |
---|
| 20 | #include <unordered_map> |
---|
[2bb4a01] | 21 | #include <vector> |
---|
| 22 | |
---|
[a300e4a] | 23 | #include "FunctionSpec.hpp" |
---|
[2bb4a01] | 24 | #include "Fwd.hpp" // for UniqueId |
---|
| 25 | #include "LinkageSpec.hpp" |
---|
| 26 | #include "Node.hpp" // for ptr, readonly |
---|
| 27 | #include "ParseNode.hpp" |
---|
| 28 | #include "StorageClasses.hpp" |
---|
[9e1d485] | 29 | #include "TypeVar.hpp" |
---|
[2bb4a01] | 30 | #include "Visitor.hpp" |
---|
[a300e4a] | 31 | #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate |
---|
[2bb4a01] | 32 | |
---|
[f3cc5b6] | 33 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
[41b24c8] | 34 | #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); |
---|
[f3cc5b6] | 35 | |
---|
[2bb4a01] | 36 | namespace ast { |
---|
[a300e4a] | 37 | |
---|
[2bb4a01] | 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 | |
---|
[14cebb7a] | 47 | Decl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
[2bb4a01] | 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 | |
---|
[23f99e1] | 58 | const Decl * accept( Visitor & v ) const override = 0; |
---|
[2bb4a01] | 59 | private: |
---|
[23f99e1] | 60 | Decl * clone() const override = 0; |
---|
[f3cc5b6] | 61 | MUTATE_FRIEND |
---|
[2bb4a01] | 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 |
---|
[6d51bd7] | 87 | virtual const Type * get_type() const = 0; |
---|
[a300e4a] | 88 | /// Set type of this declaration. May be verified by subclass |
---|
[23f99e1] | 89 | virtual void set_type(Type *) = 0; |
---|
[a300e4a] | 90 | |
---|
[23f99e1] | 91 | const DeclWithType * accept( Visitor & v ) const override = 0; |
---|
[a300e4a] | 92 | private: |
---|
[23f99e1] | 93 | DeclWithType * clone() const override = 0; |
---|
[f3cc5b6] | 94 | MUTATE_FRIEND |
---|
[a300e4a] | 95 | }; |
---|
| 96 | |
---|
[77a3f41] | 97 | /// Object declaration `Foo foo = 42;` |
---|
| 98 | class ObjectDecl final : public DeclWithType { |
---|
| 99 | public: |
---|
| 100 | ptr<Type> type; |
---|
| 101 | ptr<Init> init; |
---|
| 102 | ptr<Expr> bitfieldWidth; |
---|
| 103 | |
---|
[f474e91] | 104 | ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, |
---|
[2a8f0c1] | 105 | const Init * init = nullptr, Storage::Classes storage = {}, |
---|
| 106 | Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, |
---|
| 107 | std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) |
---|
[14cebb7a] | 108 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
[77a3f41] | 109 | init( init ), bitfieldWidth( bitWd ) {} |
---|
[14cebb7a] | 110 | |
---|
[77a3f41] | 111 | const Type* get_type() const override { return type; } |
---|
[23f99e1] | 112 | void set_type( Type * ty ) override { type = ty; } |
---|
[77a3f41] | 113 | |
---|
[23f99e1] | 114 | const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
[77a3f41] | 115 | private: |
---|
[23f99e1] | 116 | ObjectDecl * clone() const override { return new ObjectDecl{ *this }; } |
---|
[f3cc5b6] | 117 | MUTATE_FRIEND |
---|
[23f99e1] | 118 | }; |
---|
| 119 | |
---|
[8a5530c] | 120 | /// Object declaration `int foo()` |
---|
[23f99e1] | 121 | class FunctionDecl : public DeclWithType { |
---|
| 122 | public: |
---|
| 123 | ptr<FunctionType> type; |
---|
| 124 | ptr<CompoundStmt> stmts; |
---|
[d76c588] | 125 | std::vector< ptr<Expr> > withExprs; |
---|
[23f99e1] | 126 | |
---|
| 127 | FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type, |
---|
| 128 | CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, |
---|
| 129 | std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) |
---|
| 130 | : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), |
---|
| 131 | stmts( stmts ) {} |
---|
| 132 | |
---|
[8a5530c] | 133 | const Type * get_type() const override; |
---|
| 134 | void set_type(Type * t) override; |
---|
[23f99e1] | 135 | |
---|
| 136 | bool has_body() const { return stmts; } |
---|
| 137 | |
---|
| 138 | const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
| 139 | private: |
---|
| 140 | FunctionDecl * clone() const override { return new FunctionDecl( *this ); } |
---|
[f3cc5b6] | 141 | MUTATE_FRIEND |
---|
[77a3f41] | 142 | }; |
---|
| 143 | |
---|
[360b2e13] | 144 | /// Base class for named type aliases |
---|
| 145 | class NamedTypeDecl : public Decl { |
---|
| 146 | public: |
---|
| 147 | ptr<Type> base; |
---|
[54e41b3] | 148 | std::vector<ptr<TypeDecl>> params; |
---|
[360b2e13] | 149 | std::vector<ptr<DeclWithType>> assertions; |
---|
| 150 | |
---|
[e0115286] | 151 | NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
[360b2e13] | 152 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
[54e41b3] | 153 | : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {} |
---|
[360b2e13] | 154 | |
---|
| 155 | /// Produces a name for the kind of alias |
---|
| 156 | virtual std::string typeString() const = 0; |
---|
| 157 | |
---|
| 158 | private: |
---|
| 159 | NamedTypeDecl* clone() const override = 0; |
---|
[f3cc5b6] | 160 | MUTATE_FRIEND |
---|
[360b2e13] | 161 | }; |
---|
| 162 | |
---|
| 163 | /// Cforall type variable: `dtype T` |
---|
| 164 | class TypeDecl final : public NamedTypeDecl { |
---|
| 165 | public: |
---|
[9e1d485] | 166 | TypeVar::Kind kind; |
---|
[360b2e13] | 167 | bool sized; |
---|
| 168 | ptr<Type> init; |
---|
| 169 | |
---|
| 170 | /// Data extracted from a type decl |
---|
| 171 | struct Data { |
---|
[9e1d485] | 172 | TypeVar::Kind kind; |
---|
[360b2e13] | 173 | bool isComplete; |
---|
| 174 | |
---|
[9e1d485] | 175 | Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {} |
---|
[d76c588] | 176 | Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {} |
---|
[9e1d485] | 177 | Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {} |
---|
[d76c588] | 178 | Data( const Data & d1, const Data & d2 ) |
---|
[360b2e13] | 179 | : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} |
---|
| 180 | |
---|
[d76c588] | 181 | bool operator== ( const Data & o ) const { |
---|
[360b2e13] | 182 | return kind == o.kind && isComplete == o.isComplete; |
---|
| 183 | } |
---|
[d76c588] | 184 | bool operator!= ( const Data & o ) const { return !(*this == o); } |
---|
[360b2e13] | 185 | }; |
---|
| 186 | |
---|
[e0115286] | 187 | TypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, Type* b, |
---|
[9e1d485] | 188 | TypeVar::Kind k, bool s, Type* i = nullptr ) |
---|
[8a5530c] | 189 | : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), |
---|
[9e1d485] | 190 | init( i ) {} |
---|
[360b2e13] | 191 | |
---|
| 192 | std::string typeString() const override; |
---|
| 193 | /// Produces a name for generated code |
---|
| 194 | std::string genTypeString() const; |
---|
| 195 | |
---|
[9e1d485] | 196 | /// convenience accessor to match Type::isComplete() |
---|
| 197 | bool isComplete() { return sized; } |
---|
| 198 | |
---|
[23f99e1] | 199 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[360b2e13] | 200 | private: |
---|
[23f99e1] | 201 | TypeDecl * clone() const override { return new TypeDecl{ *this }; } |
---|
[f3cc5b6] | 202 | MUTATE_FRIEND |
---|
[92f5279] | 203 | friend class ParameterizedType; // to allow deep clones |
---|
[360b2e13] | 204 | }; |
---|
| 205 | |
---|
[d76c588] | 206 | std::ostream & operator<< ( std::ostream &, const TypeDecl::Data & ); |
---|
| 207 | |
---|
[360b2e13] | 208 | /// C-style typedef `typedef Foo Bar` |
---|
| 209 | class TypedefDecl final : public NamedTypeDecl { |
---|
| 210 | public: |
---|
[e0115286] | 211 | TypedefDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, |
---|
[360b2e13] | 212 | Type* b, Linkage::Spec spec = Linkage::Cforall ) |
---|
| 213 | : NamedTypeDecl( loc, name, storage, b, spec ) {} |
---|
| 214 | |
---|
| 215 | std::string typeString() const override { return "typedef"; } |
---|
| 216 | |
---|
[23f99e1] | 217 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[360b2e13] | 218 | private: |
---|
[23f99e1] | 219 | TypedefDecl * clone() const override { return new TypedefDecl{ *this }; } |
---|
[f3cc5b6] | 220 | MUTATE_FRIEND |
---|
[360b2e13] | 221 | }; |
---|
| 222 | |
---|
[a300e4a] | 223 | /// Aggregate type declaration base class |
---|
| 224 | class AggregateDecl : public Decl { |
---|
| 225 | public: |
---|
| 226 | std::vector<ptr<Decl>> members; |
---|
[54e41b3] | 227 | std::vector<ptr<TypeDecl>> params; |
---|
[a300e4a] | 228 | std::vector<ptr<Attribute>> attributes; |
---|
| 229 | bool body = false; |
---|
| 230 | readonly<AggregateDecl> parent = {}; |
---|
| 231 | |
---|
[14cebb7a] | 232 | AggregateDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 233 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
[54e41b3] | 234 | : Decl( loc, name, Storage::Classes{}, linkage ), members(), params(), |
---|
[a300e4a] | 235 | attributes( std::move(attrs) ) {} |
---|
[14cebb7a] | 236 | |
---|
[a300e4a] | 237 | AggregateDecl* set_body( bool b ) { body = b; return this; } |
---|
| 238 | |
---|
[ed3935da] | 239 | /// Produces a name for the kind of aggregate |
---|
| 240 | virtual std::string typeString() const = 0; |
---|
| 241 | |
---|
[f3cc5b6] | 242 | private: |
---|
| 243 | AggregateDecl * clone() const override = 0; |
---|
| 244 | MUTATE_FRIEND |
---|
[a300e4a] | 245 | }; |
---|
| 246 | |
---|
| 247 | /// struct declaration `struct Foo { ... };` |
---|
| 248 | class StructDecl final : public AggregateDecl { |
---|
| 249 | public: |
---|
| 250 | DeclarationNode::Aggregate kind; |
---|
| 251 | |
---|
[14cebb7a] | 252 | StructDecl( const CodeLocation& loc, const std::string& name, |
---|
| 253 | DeclarationNode::Aggregate kind = DeclarationNode::Struct, |
---|
[a300e4a] | 254 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
| 255 | : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} |
---|
| 256 | |
---|
| 257 | bool is_coroutine() { return kind == DeclarationNode::Coroutine; } |
---|
| 258 | bool is_monitor() { return kind == DeclarationNode::Monitor; } |
---|
| 259 | bool is_thread() { return kind == DeclarationNode::Thread; } |
---|
| 260 | |
---|
[23f99e1] | 261 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[ed3935da] | 262 | |
---|
| 263 | std::string typeString() const override { return "struct"; } |
---|
| 264 | |
---|
[a300e4a] | 265 | private: |
---|
[23f99e1] | 266 | StructDecl * clone() const override { return new StructDecl{ *this }; } |
---|
[f3cc5b6] | 267 | MUTATE_FRIEND |
---|
[a300e4a] | 268 | }; |
---|
| 269 | |
---|
| 270 | /// union declaration `union Foo { ... };` |
---|
| 271 | class UnionDecl final : public AggregateDecl { |
---|
| 272 | public: |
---|
[14cebb7a] | 273 | UnionDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 274 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
| 275 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
| 276 | |
---|
[23f99e1] | 277 | const Decl * accept( Visitor& v ) const override { return v.visit( this ); } |
---|
[ed3935da] | 278 | |
---|
| 279 | std::string typeString() const override { return "union"; } |
---|
| 280 | |
---|
[a300e4a] | 281 | private: |
---|
[23f99e1] | 282 | UnionDecl * clone() const override { return new UnionDecl{ *this }; } |
---|
[f3cc5b6] | 283 | MUTATE_FRIEND |
---|
[a300e4a] | 284 | }; |
---|
| 285 | |
---|
| 286 | /// enum declaration `enum Foo { ... };` |
---|
| 287 | class EnumDecl final : public AggregateDecl { |
---|
| 288 | public: |
---|
[14cebb7a] | 289 | EnumDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 290 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
| 291 | : AggregateDecl( loc, name, std::move(attrs), linkage ), enumValues() {} |
---|
| 292 | |
---|
| 293 | /// gets the integer value for this enumerator, returning true iff value found |
---|
[9d6e7fa9] | 294 | bool valueOf( const Decl * enumerator, long long& value ) const; |
---|
[a300e4a] | 295 | |
---|
[23f99e1] | 296 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[ed3935da] | 297 | |
---|
| 298 | std::string typeString() const override { return "enum"; } |
---|
| 299 | |
---|
[a300e4a] | 300 | private: |
---|
[23f99e1] | 301 | EnumDecl * clone() const override { return new EnumDecl{ *this }; } |
---|
[f3cc5b6] | 302 | MUTATE_FRIEND |
---|
[a300e4a] | 303 | |
---|
| 304 | /// Map from names to enumerator values; kept private for lazy initialization |
---|
| 305 | mutable std::unordered_map< std::string, long long > enumValues; |
---|
| 306 | }; |
---|
| 307 | |
---|
| 308 | /// trait declaration `trait Foo( ... ) { ... };` |
---|
| 309 | class TraitDecl final : public AggregateDecl { |
---|
| 310 | public: |
---|
[14cebb7a] | 311 | TraitDecl( const CodeLocation& loc, const std::string& name, |
---|
[a300e4a] | 312 | std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) |
---|
| 313 | : AggregateDecl( loc, name, std::move(attrs), linkage ) {} |
---|
| 314 | |
---|
[23f99e1] | 315 | const Decl * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[ed3935da] | 316 | |
---|
| 317 | std::string typeString() const override { return "trait"; } |
---|
| 318 | |
---|
[a300e4a] | 319 | private: |
---|
[23f99e1] | 320 | TraitDecl * clone() const override { return new TraitDecl{ *this }; } |
---|
[f3cc5b6] | 321 | MUTATE_FRIEND |
---|
[2bb4a01] | 322 | }; |
---|
| 323 | |
---|
[23f99e1] | 324 | class AsmDecl : public Decl { |
---|
| 325 | public: |
---|
| 326 | ptr<AsmStmt> stmt; |
---|
| 327 | |
---|
| 328 | AsmDecl( const CodeLocation & loc, AsmStmt *stmt ) |
---|
| 329 | : Decl( loc, "", {}, {} ), stmt(stmt) {} |
---|
| 330 | |
---|
| 331 | const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
| 332 | private: |
---|
| 333 | AsmDecl *clone() const override { return new AsmDecl( *this ); } |
---|
[f3cc5b6] | 334 | MUTATE_FRIEND |
---|
[23f99e1] | 335 | }; |
---|
| 336 | |
---|
| 337 | class StaticAssertDecl : public Decl { |
---|
| 338 | public: |
---|
[112fe04] | 339 | ptr<Expr> cond; |
---|
[23f99e1] | 340 | ptr<ConstantExpr> msg; // string literal |
---|
| 341 | |
---|
| 342 | StaticAssertDecl( const CodeLocation & loc, const Expr * condition, const ConstantExpr * msg ) |
---|
[112fe04] | 343 | : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {} |
---|
[23f99e1] | 344 | |
---|
| 345 | const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); } |
---|
| 346 | private: |
---|
| 347 | StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); } |
---|
[f3cc5b6] | 348 | MUTATE_FRIEND |
---|
[23f99e1] | 349 | }; |
---|
[e0115286] | 350 | |
---|
[2bb4a01] | 351 | } |
---|
| 352 | |
---|
[f3cc5b6] | 353 | #undef MUTATE_FRIEND |
---|
| 354 | |
---|
[2bb4a01] | 355 | // Local Variables: // |
---|
| 356 | // tab-width: 4 // |
---|
| 357 | // mode: c++ // |
---|
| 358 | // compile-command: "make install" // |
---|
| 359 | // End: // |
---|