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