Changes in src/AST/Decl.hpp [99da267:427854b]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.hpp
r99da267 r427854b 9 9 // Author : Aaron B. Moss 10 10 // Created On : Thu May 9 10:00:00 2019 11 // Last Modified By : Aaron B. Moss12 // Last Modified On : Thu May 9 10:00:00201913 // Update Count : 111 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Dec 13 17:38:33 2019 13 // Update Count : 29 14 14 // 15 15 … … 20 20 #include <unordered_map> 21 21 #include <vector> 22 #include <algorithm> 22 23 23 24 #include "FunctionSpec.hpp" … … 27 28 #include "ParseNode.hpp" 28 29 #include "StorageClasses.hpp" 29 #include "TypeVar.hpp"30 30 #include "Visitor.hpp" 31 #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate 31 #include "Common/utility.h" 32 #include "Common/SemanticError.h" // error_str 32 33 33 34 // Must be included in *all* AST classes; should be #undef'd at the end of the file 34 #define MUTATE_FRIEND \ 35 template<typename node_t> friend node_t * mutate(const node_t * node); \ 36 template<typename node_t> friend node_t * shallowCopy(const node_t * node); 35 #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); 37 36 38 37 namespace ast { … … 89 88 virtual const Type * get_type() const = 0; 90 89 /// Set type of this declaration. May be verified by subclass 91 virtual void set_type( const Type *) = 0;90 virtual void set_type(Type *) = 0; 92 91 93 92 const DeclWithType * accept( Visitor & v ) const override = 0; … … 104 103 ptr<Expr> bitfieldWidth; 105 104 106 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 107 const Init * init = nullptr, Storage::Classes storage = {}, 108 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 105 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 106 const Init * init = nullptr, Storage::Classes storage = {}, 107 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 109 108 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) 110 109 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), … … 112 111 113 112 const Type* get_type() const override { return type; } 114 void set_type( constType * ty ) override { type = ty; }113 void set_type( Type * ty ) override { type = ty; } 115 114 116 115 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } … … 127 126 std::vector< ptr<Expr> > withExprs; 128 127 129 FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type,128 FunctionDecl( const CodeLocation & loc, const std::string & name, FunctionType * type, 130 129 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, 131 130 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) … … 134 133 135 134 const Type * get_type() const override; 136 void set_type( const Type * t) override;135 void set_type(Type * t) override; 137 136 138 137 bool has_body() const { return stmts; } 139 138 140 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); }139 const DeclWithType * accept( Visitor & v ) const override { return v.visit( this ); } 141 140 private: 142 141 FunctionDecl * clone() const override { return new FunctionDecl( *this ); } … … 151 150 std::vector<ptr<DeclWithType>> assertions; 152 151 153 NamedTypeDecl( 154 const CodeLocation & loc, const std::string & name, Storage::Classes storage, 155 const Type * b, Linkage::Spec spec = Linkage::Cforall ) 152 NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, 153 Type* b, Linkage::Spec spec = Linkage::Cforall ) 156 154 : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {} 157 155 158 156 /// Produces a name for the kind of alias 159 virtual std::stringtypeString() const = 0;157 virtual const char * typeString() const = 0; 160 158 161 159 private: … … 166 164 /// Cforall type variable: `dtype T` 167 165 class TypeDecl final : public NamedTypeDecl { 168 public: 169 TypeVar::Kind kind; 166 public: 167 enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS }; 168 169 Kind kind; 170 170 bool sized; 171 171 ptr<Type> init; … … 173 173 /// Data extracted from a type decl 174 174 struct Data { 175 TypeVar::Kind kind;175 Kind kind; 176 176 bool isComplete; 177 177 178 Data() : kind( (TypeVar::Kind)-1), isComplete( false ) {}178 Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {} 179 179 Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {} 180 Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {}180 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {} 181 181 Data( const Data & d1, const Data & d2 ) 182 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} 183 184 bool operator== ( const Data & o ) const { 185 return kind == o.kind && isComplete == o.isComplete; 186 } 187 bool operator!= ( const Data & o ) const { return !(*this == o); } 182 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} 183 184 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; } 185 bool operator!=( const Data & o ) const { return !(*this == o); } 188 186 }; 189 187 190 TypeDecl( 191 const CodeLocation & loc, const std::string & name, Storage::Classes storage, 192 const Type * b, TypeVar::Kind k, bool s, const Type * i = nullptr ) 193 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), 194 init( i ) {} 195 196 std::string typeString() const override; 188 TypeDecl( const CodeLocation & loc, const std::string & name, Storage::Classes storage, Type * b, 189 Kind k, bool s, Type * i = nullptr ) 190 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ), 191 init( i ) {} 192 193 const char * typeString() const override; 197 194 /// Produces a name for generated code 198 std::stringgenTypeString() const;195 const char * genTypeString() const; 199 196 200 197 /// convenience accessor to match Type::isComplete() … … 202 199 203 200 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 204 private:201 private: 205 202 TypeDecl * clone() const override { return new TypeDecl{ *this }; } 206 203 MUTATE_FRIEND … … 216 213 : NamedTypeDecl( loc, name, storage, b, spec ) {} 217 214 218 std::stringtypeString() const override { return "typedef"; }215 const char * typeString() const override { return "typedef"; } 219 216 220 217 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } … … 227 224 class AggregateDecl : public Decl { 228 225 public: 226 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate }; 227 static const char * aggrString( Aggregate aggr ); 228 229 229 std::vector<ptr<Decl>> members; 230 230 std::vector<ptr<TypeDecl>> params; … … 241 241 242 242 /// Produces a name for the kind of aggregate 243 virtual std::stringtypeString() const = 0;243 virtual const char * typeString() const = 0; 244 244 245 245 private: … … 251 251 class StructDecl final : public AggregateDecl { 252 252 public: 253 DeclarationNode::Aggregate kind;253 Aggregate kind; 254 254 255 255 StructDecl( const CodeLocation& loc, const std::string& name, 256 DeclarationNode::Aggregate kind = DeclarationNode::Struct,256 Aggregate kind = Struct, 257 257 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) 258 258 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} 259 259 260 bool is_coroutine() { return kind == DeclarationNode::Coroutine; } 261 bool is_monitor() { return kind == DeclarationNode::Monitor; } 262 bool is_thread() { return kind == DeclarationNode::Thread; } 263 264 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 265 266 std::string typeString() const override { return "struct"; } 260 bool is_coroutine() { return kind == Coroutine; } 261 bool is_generator() { return kind == Generator; } 262 bool is_monitor () { return kind == Monitor ; } 263 bool is_thread () { return kind == Thread ; } 264 265 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 266 267 const char * typeString() const override { return aggrString( kind ); } 267 268 268 269 private: … … 280 281 const Decl * accept( Visitor& v ) const override { return v.visit( this ); } 281 282 282 std::string typeString() const override { return "union"; }283 const char * typeString() const override { return aggrString( Union ); } 283 284 284 285 private: … … 299 300 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 300 301 301 std::string typeString() const override { return "enum"; }302 const char * typeString() const override { return aggrString( Enum ); } 302 303 303 304 private: … … 318 319 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 319 320 320 std::stringtypeString() const override { return "trait"; }321 const char * typeString() const override { return "trait"; } 321 322 322 323 private: … … 325 326 }; 326 327 328 /// With statement `with (...) ...` 329 class WithStmt final : public Decl { 330 public: 331 std::vector<ptr<Expr>> exprs; 332 ptr<Stmt> stmt; 333 334 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt ) 335 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {} 336 337 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 338 private: 339 WithStmt * clone() const override { return new WithStmt{ *this }; } 340 MUTATE_FRIEND 341 }; 342 327 343 class AsmDecl : public Decl { 328 344 public: 329 345 ptr<AsmStmt> stmt; 330 346 331 AsmDecl( const CodeLocation & loc, AsmStmt * stmt )347 AsmDecl( const CodeLocation & loc, AsmStmt * stmt ) 332 348 : Decl( loc, "", {}, {} ), stmt(stmt) {} 333 349 334 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); }335 private: 336 AsmDecl * clone() const override { return new AsmDecl( *this ); }350 const AsmDecl * accept( Visitor & v ) const override { return v.visit( this ); } 351 private: 352 AsmDecl * clone() const override { return new AsmDecl( *this ); } 337 353 MUTATE_FRIEND 338 354 }; … … 346 362 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {} 347 363 348 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); }364 const StaticAssertDecl * accept( Visitor & v ) const override { return v.visit( this ); } 349 365 private: 350 366 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
Note:
See TracChangeset
for help on using the changeset viewer.