Changes in / [2b78949:044ae62]
- Location:
- src
- Files:
-
- 35 edited
-
AST/Convert.cpp (modified) (3 diffs)
-
AST/Decl.cpp (modified) (1 diff)
-
AST/Decl.hpp (modified) (5 diffs)
-
AST/Fwd.hpp (modified) (1 diff)
-
AST/Node.cpp (modified) (1 diff)
-
AST/Pass.hpp (modified) (1 diff)
-
AST/Pass.impl.hpp (modified) (2 diffs)
-
AST/Pass.proto.hpp (modified) (2 diffs)
-
AST/Print.cpp (modified) (1 diff)
-
AST/SymbolTable.cpp (modified) (1 diff)
-
AST/SymbolTable.hpp (modified) (3 diffs)
-
AST/Visitor.hpp (modified) (1 diff)
-
CodeGen/CodeGenerator.cc (modified) (2 diffs)
-
CodeGen/CodeGenerator.h (modified) (2 diffs)
-
Common/CodeLocationTools.cpp (modified) (1 diff)
-
Common/PassVisitor.h (modified) (3 diffs)
-
Common/PassVisitor.impl.h (modified) (2 diffs)
-
Common/PassVisitor.proto.h (modified) (2 diffs)
-
Parser/DeclarationNode.cc (modified) (3 diffs)
-
Parser/DeclarationNode.h (modified) (3 diffs)
-
Parser/TypeData.cc (modified) (8 diffs)
-
Parser/TypeData.h (modified) (4 diffs)
-
Parser/lex.ll (modified) (1 diff)
-
Parser/parser.yy (modified) (4 diffs)
-
SymTab/Indexer.cc (modified) (1 diff)
-
SymTab/Indexer.h (modified) (3 diffs)
-
SynTree/AggregateDecl.cc (modified) (1 diff)
-
SynTree/Declaration.h (modified) (2 diffs)
-
SynTree/Mutator.h (modified) (1 diff)
-
SynTree/SynTree.h (modified) (1 diff)
-
SynTree/Visitor.h (modified) (1 diff)
-
Validate/Autogen.cpp (modified) (4 diffs)
-
Validate/HoistStruct.cpp (modified) (1 diff)
-
Validate/HoistStruct.hpp (modified) (1 diff)
-
Validate/NoIdSymbolTable.hpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Convert.cpp
r2b78949 r044ae62 283 283 decl->parent = get<AggregateDecl>().accept1( node->parent ); 284 284 declPostamble( decl, node ); 285 return nullptr; // ??285 return nullptr; 286 286 } 287 287 … … 320 320 LinkageSpec::Spec( node->linkage.val ), 321 321 get<Type>().accept1(node->base) 322 ); 323 // decl->data_constructors = get<StructDecl>().acceptL( node->data_constructors ); 324 // decl->data_union = get<UnionDecl>().accept1( node->data_union ); 325 // decl->tag = get<EnumDecl>().accept1( node->tag ); 326 // decl->tag_union = get<StructDecl>().accept1( node->tag_union ); 327 return aggregatePostamble( decl, node ); 328 } 329 330 const ast::Decl * visit( const ast::AdtDecl * node ) override final { 331 if ( inCache(node) ) return nullptr; 332 auto decl = new AdtDecl( 333 node->name, 334 get<Attribute>().acceptL( node->attributes ), 335 LinkageSpec::Spec( node->linkage.val ), 336 get<StructDecl>().acceptL( node->data_constructors ), 337 get<UnionDecl>().accept1( node->data_union ), 338 get<EnumDecl>().accept1( node->tag ), 339 get<StructDecl>().accept1( node->tag_union ) 322 340 ); 323 341 return aggregatePostamble( decl, node ); … … 1790 1808 } 1791 1809 1810 virtual void visit( const AdtDecl * old ) override final { 1811 if ( inCache( old ) ) return; 1812 auto decl = new ast::AdtDecl( 1813 old->location, 1814 old->name, 1815 GET_ACCEPT_V(attributes, Attribute), 1816 { old->linkage.val } 1817 ); 1818 cache.emplace( old, decl ); 1819 decl->parent = GET_ACCEPT_1(parent, AggregateDecl); 1820 decl->body = old->body; 1821 decl->params = GET_ACCEPT_V(parameters, TypeDecl); 1822 decl->members = GET_ACCEPT_V(members, Decl); 1823 decl->extension = old->extension; 1824 decl->uniqueId = old->uniqueId; 1825 decl->storage = { old->storageClasses.val }; 1826 decl->data_constructors = GET_ACCEPT_V( data_constructors, StructDecl ); 1827 decl->data_union = GET_ACCEPT_1( data_union, UnionDecl ); 1828 decl->tag = GET_ACCEPT_1( tag, EnumDecl ); 1829 decl->tag_union = GET_ACCEPT_1( tag_union, StructDecl ); 1830 this->node = decl; 1831 } 1832 1792 1833 virtual void visit( const TraitDecl * old ) override final { 1793 1834 if ( inCache( old ) ) return; -
src/AST/Decl.cpp
r2b78949 r044ae62 132 132 133 133 // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations. 134 static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };134 static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName", "adt" }; 135 135 136 136 const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) { -
src/AST/Decl.hpp
r2b78949 r044ae62 248 248 class AggregateDecl : public Decl { 249 249 public: 250 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };250 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, Adt }; 251 251 static const char * aggrString( Aggregate aggr ); 252 252 … … 286 286 bool is_monitor () const { return kind == Monitor ; } 287 287 bool is_thread () const { return kind == Thread ; } 288 bool is_adt () const { return kind == Adt ; } 288 289 289 290 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } … … 322 323 EnumDecl( const CodeLocation& loc, const std::string& name, bool isTyped = false, 323 324 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall, 324 Type const * base = nullptr, EnumHiding hide = EnumHiding:: Hide,325 Type const * base = nullptr, EnumHiding hide = EnumHiding::Visible, 325 326 std::unordered_map< std::string, long long > enumValues = std::unordered_map< std::string, long long >() ) 326 327 : AggregateDecl( loc, name, std::move(attrs), linkage ), isTyped(isTyped), base(base), hide(hide), enumValues(enumValues) {} … … 343 344 }; 344 345 346 class AdtDecl final : public AggregateDecl { 347 public: 348 std::vector<ptr<StructDecl>> data_constructors; // Todo: members? 349 ptr<UnionDecl> data_union; 350 ptr<EnumDecl> tag; 351 ptr<StructDecl> tag_union; 352 353 AdtDecl( const CodeLocation& loc, const std::string& name, 354 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) 355 : AggregateDecl( loc, name, std::move(attrs), linkage ) {} 356 357 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 358 359 const char * typeString() const override { return aggrString( Adt ); } 360 361 private: 362 AdtDecl * clone() const override { return new AdtDecl{ *this }; } 363 MUTATE_FRIEND 364 }; 365 345 366 /// trait declaration `trait Foo( ... ) { ... };` 346 367 class TraitDecl final : public AggregateDecl { … … 351 372 352 373 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 353 354 374 const char * typeString() const override { return "trait"; } 355 375 -
src/AST/Fwd.hpp
r2b78949 r044ae62 32 32 class UnionDecl; 33 33 class EnumDecl; 34 class AdtDecl; 34 35 class TraitDecl; 35 36 class NamedTypeDecl; -
src/AST/Node.cpp
r2b78949 r044ae62 128 128 template class ast::ptr_base< ast::EnumDecl, ast::Node::ref_type::weak >; 129 129 template class ast::ptr_base< ast::EnumDecl, ast::Node::ref_type::strong >; 130 template class ast::ptr_base< ast::AdtDecl, ast::Node::ref_type::weak >; 131 template class ast::ptr_base< ast::AdtDecl, ast::Node::ref_type::strong >; 130 132 template class ast::ptr_base< ast::TraitDecl, ast::Node::ref_type::weak >; 131 133 template class ast::ptr_base< ast::TraitDecl, ast::Node::ref_type::strong >; -
src/AST/Pass.hpp
r2b78949 r044ae62 139 139 const ast::Decl * visit( const ast::UnionDecl * ) override final; 140 140 const ast::Decl * visit( const ast::EnumDecl * ) override final; 141 const ast::Decl * visit( const ast::AdtDecl * ) override final; 141 142 const ast::Decl * visit( const ast::TraitDecl * ) override final; 142 143 const ast::Decl * visit( const ast::TypeDecl * ) override final; -
src/AST/Pass.impl.hpp
r2b78949 r044ae62 620 620 maybe_accept( node, &EnumDecl::members ); 621 621 maybe_accept( node, &EnumDecl::attributes ); 622 623 // maybe_accept( node, &EnumDecl::data_constructors ); 624 // maybe_accept( node, &EnumDecl::data_union ); 625 // maybe_accept( node, &EnumDecl::tag ); 626 // maybe_accept( node, &EnumDecl::tag_union ); 622 627 } else { 623 628 maybe_accept( node, &EnumDecl::base ); … … 625 630 maybe_accept( node, &EnumDecl::members ); 626 631 maybe_accept( node, &EnumDecl::attributes ); 627 } 632 633 // maybe_accept( node, &EnumDecl::data_constructors ); 634 // maybe_accept( node, &EnumDecl::data_union ); 635 // maybe_accept( node, &EnumDecl::tag ); 636 // maybe_accept( node, &EnumDecl::tag_union ); 637 } 638 } 639 640 VISIT_END( Decl, node ); 641 } 642 643 template< typename core_t > 644 const ast::Decl * ast::Pass< core_t >::visit( const ast::AdtDecl * node ) { 645 VISIT_START( node ); 646 647 __pass::symtab::addAdt( core, 0, node ); 648 649 if ( __visit_children() ) { 650 guard_symtab guard { *this }; 651 maybe_accept( node, &AdtDecl::params ); 652 maybe_accept( node, &AdtDecl::members ); 653 maybe_accept( node, &AdtDecl::attributes ); 654 655 maybe_accept( node, &AdtDecl::data_constructors ); 656 maybe_accept( node, &AdtDecl::data_union ); 657 maybe_accept( node, &AdtDecl::tag ); 658 maybe_accept( node, &AdtDecl::tag_union ); 628 659 } 629 660 -
src/AST/Pass.proto.hpp
r2b78949 r044ae62 458 458 SYMTAB_FUNC1( addStruct , const StructDecl * ); 459 459 SYMTAB_FUNC1( addEnum , const EnumDecl * ); 460 SYMTAB_FUNC1( addAdt , const AdtDecl * ); 460 461 SYMTAB_FUNC1( addUnion , const UnionDecl * ); 461 462 SYMTAB_FUNC1( addTrait , const TraitDecl * ); … … 488 489 489 490 template<typename core_t> 491 static inline auto addAdtFwd( core_t & core, int, const ast::AdtDecl * decl ) -> decltype( core.symtab.addAdt( decl ), void() ) { 492 ast::AdtDecl * fwd = new ast::AdtDecl( decl->location, decl->name ); 493 for ( const auto & param : decl->params ) { 494 fwd->params.push_back( deepCopy( param.get() ) ); 495 } 496 // Experimental 497 for ( const auto & ctor : decl->data_constructors ) { 498 addStructFwd( core, 0, ctor ); 499 } 500 core.symtab.addAdt( fwd ); 501 } 502 503 template<typename core_t> 504 static inline auto addAdtFwd( core_t &, long, const ast::AdtDecl) {} 505 506 template<typename core_t> 490 507 static inline auto addStruct( core_t & core, int, const std::string & str ) -> decltype( core.symtab.addStruct( str ), void() ) { 491 508 if ( ! core.symtab.lookupStruct( str ) ) { -
src/AST/Print.cpp
r2b78949 r044ae62 438 438 } 439 439 440 virtual const ast::Decl * visit( const ast::AdtDecl * node ) override final { 441 print(node); 442 return node; 443 } 444 440 445 virtual const ast::Decl * visit( const ast::TraitDecl * node ) override final { 441 446 print(node); -
src/AST/SymbolTable.cpp
r2b78949 r044ae62 358 358 } 359 359 360 void SymbolTable::addAdt( const AdtDecl *decl ) { 361 ++*stats().add_calls; 362 const std::string &id = decl->name; 363 364 if ( ! adtTable ) { 365 adtTable = AdtTable::new_ptr(); 366 } else { 367 ++*stats().map_lookups; 368 auto existing = adtTable->find( id ); 369 if ( existing != adtTable->end() 370 && existing->second.scope == scope 371 && addedDeclConflicts( existing->second.decl, decl ) ) return; 372 373 } 374 375 lazyInitScope(); 376 ++*stats().map_mutations; 377 adtTable = adtTable->set( id, scoped<AdtDecl>{ decl, scope }); 378 } 379 360 380 void SymbolTable::addUnion( const std::string &id ) { 361 381 addUnion( new UnionDecl( CodeLocation(), id ) ); -
src/AST/SymbolTable.hpp
r2b78949 r044ae62 72 72 using StructTable = PersistentMap< std::string, scoped<StructDecl> >; 73 73 using EnumTable = PersistentMap< std::string, scoped<EnumDecl> >; 74 using AdtTable = PersistentMap< std::string, scoped<AdtDecl> >; 74 75 using UnionTable = PersistentMap< std::string, scoped<UnionDecl> >; 75 76 using TraitTable = PersistentMap< std::string, scoped<TraitDecl> >; … … 80 81 EnumTable::Ptr enumTable; ///< enum namespace 81 82 UnionTable::Ptr unionTable; ///< union namespace 83 AdtTable::Ptr adtTable; ///< adt namespace 82 84 TraitTable::Ptr traitTable; ///< trait namespace 83 85 IdTable::Ptr specialFunctionTable[NUMBER_OF_KINDS]; … … 138 140 /// Adds an enum declaration to the symbol table 139 141 void addEnum( const EnumDecl * decl ); 142 /// Adds an adt declaration to the symbol table 143 void addAdt( const AdtDecl * decl ); 140 144 /// Adds a union declaration to the symbol table by name 141 145 void addUnion( const std::string & id ); -
src/AST/Visitor.hpp
r2b78949 r044ae62 27 27 virtual const ast::Decl * visit( const ast::UnionDecl * ) = 0; 28 28 virtual const ast::Decl * visit( const ast::EnumDecl * ) = 0; 29 virtual const ast::Decl * visit( const ast::AdtDecl * ) = 0; 29 30 virtual const ast::Decl * visit( const ast::TraitDecl * ) = 0; 30 31 virtual const ast::Decl * visit( const ast::TypeDecl * ) = 0; -
src/CodeGen/CodeGenerator.cc
r2b78949 r044ae62 295 295 } 296 296 297 void CodeGenerator::handleData( EnumDecl * ) { 298 // output << " /** data type */" << endl; 299 // for ( StructDecl * decl : dataDecl->data_constructors ) { 300 // postvisit(decl); 301 // output << ";" << endl; 302 // } 303 // postvisit( dataDecl->data_union ); 304 // output << ";" << endl; 305 // postvisit( dataDecl->tag ); 306 // output << ";" << endl; 307 // postvisit( dataDecl->tag_union ); 308 // output << ";" << endl; 309 assert(false); 310 } 311 297 312 void CodeGenerator::postvisit( EnumDecl * enumDecl ) { 298 extension( enumDecl ); 313 // if ( enumDecl->data_constructors.size() > 0 ) return handleData( enumDecl ); 314 extension( enumDecl ); 299 315 std::list< Declaration* > &memb = enumDecl->get_members(); 300 316 if (enumDecl->base && ! memb.empty()) { … … 332 348 } // if 333 349 } // if 350 } 351 352 void CodeGenerator::postvisit( AdtDecl * ) { 353 // TODO 334 354 } 335 355 -
src/CodeGen/CodeGenerator.h
r2b78949 r044ae62 53 53 void postvisit( UnionDecl * aggregateDecl ); 54 54 void postvisit( EnumDecl * aggregateDecl ); 55 void postvisit( AdtDecl * AggregateDecl ); 55 56 void postvisit( TraitDecl * aggregateDecl ); 56 57 void postvisit( TypedefDecl * typeDecl ); … … 165 166 void handleTypedef( NamedTypeDecl *namedType ); 166 167 std::string mangleName( DeclarationWithType * decl ); 168 169 void handleData( EnumDecl * EnumDecl ); 167 170 }; // CodeGenerator 168 171 -
src/Common/CodeLocationTools.cpp
r2b78949 r044ae62 105 105 macro(UnionDecl, Decl) \ 106 106 macro(EnumDecl, Decl) \ 107 macro(AdtDecl, Decl) \ 107 108 macro(TraitDecl, Decl) \ 108 109 macro(TypeDecl, Decl) \ -
src/Common/PassVisitor.h
r2b78949 r044ae62 69 69 virtual void visit( EnumDecl * aggregateDecl ) override final; 70 70 virtual void visit( const EnumDecl * aggregateDecl ) override final; 71 virtual void visit( AdtDecl * aggregateDecl ) override final; 72 virtual void visit( const AdtDecl * AggregateDecl ) override final; 71 73 virtual void visit( TraitDecl * aggregateDecl ) override final; 72 74 virtual void visit( const TraitDecl * aggregateDecl ) override final; … … 269 271 virtual Declaration * mutate( UnionDecl * aggregateDecl ) override final; 270 272 virtual Declaration * mutate( EnumDecl * aggregateDecl ) override final; 273 virtual Declaration * mutate( AdtDecl * aggregateDecl ) override final; 271 274 virtual Declaration * mutate( TraitDecl * aggregateDecl ) override final; 272 275 virtual Declaration * mutate( TypeDecl * typeDecl ) override final; … … 439 442 void indexerAddStructFwd( const StructDecl * node ) { indexer_impl_addStructFwd( pass, 0, node ); } 440 443 void indexerAddEnum ( const EnumDecl * node ) { indexer_impl_addEnum ( pass, 0, node ); } 444 void indexerAddAdt ( const AdtDecl * node ) { indexer_impl_addAdt ( pass, 0, node ); } 445 void indexerAddAdtFwd ( const AdtDecl * node ) { indexer_impl_addAdtFwd ( pass, 0, node ); } 441 446 void indexerAddUnion ( const std::string & id ) { indexer_impl_addUnion ( pass, 0, id ); } 442 447 void indexerAddUnion ( const UnionDecl * node ) { indexer_impl_addUnion ( pass, 0, node ); } -
src/Common/PassVisitor.impl.h
r2b78949 r044ae62 754 754 755 755 // unlike structs, traits, and unions, enums inject their members into the global scope 756 // if ( node->base ) maybeAccept_impl( node->base, *this ); // Need this? Maybe not?757 756 maybeAccept_impl( node->parameters, *this ); 758 757 maybeAccept_impl( node->members , *this ); … … 783 782 784 783 // unlike structs, traits, and unions, enums inject their members into the global scope 784 maybeMutate_impl( node->parameters, *this ); 785 maybeMutate_impl( node->members , *this ); 786 maybeMutate_impl( node->attributes, *this ); 787 788 MUTATE_END( Declaration, node ); 789 } 790 791 template< typename pass_type > 792 void PassVisitor< pass_type >::visit( AdtDecl * node ) { 793 VISIT_START( node ); 794 795 indexerAddAdtFwd( node ); 796 797 // unlike structs, traits, and unions, enums inject their members into the global scope 798 maybeAccept_impl( node->data_constructors, *this ); 799 maybeAccept_impl( node->data_union, *this ); 800 maybeAccept_impl( node->tag, *this ); 801 802 maybeAccept_impl( node->parameters, *this ); 803 maybeAccept_impl( node->members , *this ); 804 maybeAccept_impl( node->attributes, *this ); 805 806 VISIT_END( node ); 807 } 808 809 template< typename pass_type > 810 void PassVisitor< pass_type >::visit( const AdtDecl * node ) { 811 VISIT_START( node ); 812 813 indexerAddAdtFwd( node ); 814 815 maybeAccept_impl( node->data_constructors, *this ); 816 maybeAccept_impl( node->data_union, *this ); 817 maybeAccept_impl( node->tag, *this ); 818 819 maybeAccept_impl( node->parameters, *this ); 820 maybeAccept_impl( node->members , *this ); 821 maybeAccept_impl( node->attributes, *this ); 822 823 824 VISIT_END( node ); 825 } 826 827 template< typename pass_type > 828 Declaration * PassVisitor< pass_type >::mutate( AdtDecl * node ) { 829 MUTATE_START( node ); 830 831 indexerAddAdtFwd( node ); 832 833 maybeMutate_impl( node->data_constructors, *this ); 834 maybeMutate_impl( node->data_union, *this ); 835 maybeMutate_impl( node->tag, *this ); 836 785 837 maybeMutate_impl( node->parameters, *this ); 786 838 maybeMutate_impl( node->members , *this ); -
src/Common/PassVisitor.proto.h
r2b78949 r044ae62 233 233 INDEXER_FUNC1( addStruct , const StructDecl * ); 234 234 INDEXER_FUNC1( addEnum , const EnumDecl * ); 235 INDEXER_FUNC1( addAdt , const AdtDecl * ); 235 236 INDEXER_FUNC1( addUnion , const UnionDecl * ); 236 237 INDEXER_FUNC1( addTrait , const TraitDecl * ); … … 251 252 252 253 template<typename pass_type> 254 static inline auto indexer_impl_addAdtFwd( pass_type & pass, int, const AdtDecl * decl ) -> decltype( pass.indexer.addAdt( decl ), void() ) { 255 AdtDecl * fwd = new AdtDecl( decl->name ); 256 cloneAll( decl->parameters, fwd->parameters ); 257 258 // Experimental 259 for ( const StructDecl * ctor : fwd->data_constructors ) { 260 indexer_impl_addStructFwd( pass, 0, ctor ); 261 } 262 263 pass.indexer.addAdt( fwd ); 264 } 265 266 template<typename pass_type> 267 static inline auto indexer_impl_addAdtFwd( pass_type &, long, const AdtDecl * ) {} 268 269 template<typename pass_type> 253 270 static inline auto indexer_impl_addUnionFwd( pass_type & pass, int, const UnionDecl * decl ) -> decltype( pass.indexer.addUnion( decl ), void() ) { 254 271 UnionDecl * fwd = new UnionDecl( decl->name ); -
src/Parser/DeclarationNode.cc
r2b78949 r044ae62 279 279 } // DeclarationNode::newEnum 280 280 281 DeclarationNode * DeclarationNode::newAdt( const string * name, DeclarationNode * constructors ) { 282 assert( name ); 283 DeclarationNode * newnode = new DeclarationNode; 284 newnode->type = new TypeData( TypeData::Adt ); 285 newnode->type->adt.name = name; 286 newnode->type->adt.data_constructors = constructors; 287 return newnode; 288 } // DeclarationNode::newAdt 289 290 281 291 DeclarationNode * DeclarationNode::newName( const string * name ) { 282 292 DeclarationNode * newnode = new DeclarationNode; … … 305 315 } // if 306 316 } // DeclarationNode::newEnumValueGeneric 317 318 DeclarationNode * DeclarationNode::newDataConstructor( const string * name ) { 319 DeclarationNode * newnode = newName(name); 320 return newnode; 321 } 307 322 308 323 DeclarationNode * DeclarationNode::newEnumInLine( const string name ) { … … 1083 1098 } 1084 1099 return nullptr; 1100 } 1101 1102 std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode ) { 1103 std::vector<ast::ptr<ast::StructDecl>> outputList; 1104 std::back_insert_iterator<std::vector<ast::ptr<ast::StructDecl>>> out( outputList ); 1105 for ( const DeclarationNode * cur = firstNode; cur; cur = strict_next( cur ) ) { 1106 // td->kind == TypeData::Symbolic 1107 assert( cur->type->kind == TypeData::Symbolic ); 1108 const std::string * name = cur->name; 1109 auto ctor = new ast::StructDecl( cur->location, 1110 std::string(*name), 1111 ast::AggregateDecl::Aggregate::Struct 1112 ); 1113 ctor->set_body(true); 1114 TypeData * td = cur->type; 1115 TypeData::Symbolic_t st = td->symbolic; 1116 DeclarationNode * params = st.params; 1117 1118 if ( params ) { 1119 buildList( params, ctor->members ); 1120 } 1121 1122 for ( std::size_t i = 0; i < ctor->members.size(); ++i ) { 1123 assert(ctor->members[i]->name == ""); 1124 ast::Decl * member = ctor->members[i].get_and_mutate(); 1125 member->name = "field_" + std::to_string(i); 1126 } 1127 *out++ = ctor; 1128 } 1129 return outputList; 1130 } 1131 1132 ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList ) { 1133 ast::UnionDecl * out = new ast::UnionDecl( loc, "temp_data_union" ); 1134 // size_t index = 0; 1135 if ( typeList.size() > 0 ) out->set_body( true ); 1136 size_t i = 0; 1137 for (const ast::ptr<ast::StructDecl> structDecl : typeList ) { 1138 ast::StructInstType * inst = new ast::StructInstType(structDecl); 1139 ast::ObjectDecl * instObj = new ast::ObjectDecl( 1140 structDecl->location, 1141 "option_" + std::to_string(i), 1142 inst 1143 ); 1144 i++; 1145 out->members.push_back( instObj ); 1146 1147 } 1148 return out; 1149 } 1150 1151 ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList ) { 1152 ast::EnumDecl * out = new ast::EnumDecl( loc, "temp_data_tag" ); 1153 if ( typeList.size() > 0 ) out->set_body( true ); 1154 for ( const ast::ptr<ast::StructDecl> structDecl : typeList ) { 1155 ast::EnumInstType * inst = new ast::EnumInstType( out ); 1156 assert( inst->base != nullptr ); 1157 ast::ObjectDecl * instObj = new ast::ObjectDecl( 1158 structDecl->location, 1159 structDecl->name, 1160 inst 1161 ); 1162 out->members.push_back( instObj ); 1163 } 1164 return out; 1165 } 1166 1167 ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tags, const ast::UnionDecl * data_union ) { 1168 assert( tags->members.size() == data_union->members.size() ); 1169 ast::StructDecl * out = new ast::StructDecl( data->location, *(data->adt.name) ); 1170 out->kind = ast::AggregateDecl::Adt; 1171 1172 out->set_body( true ); 1173 1174 ast::EnumInstType * tag = new ast::EnumInstType( tags ); 1175 ast::ObjectDecl * tag_obj = new ast::ObjectDecl( 1176 data->location, 1177 "tag", 1178 tag 1179 ); 1180 ast::UnionInstType * value = new ast::UnionInstType( data_union ); 1181 ast::ObjectDecl * value_obj = new ast::ObjectDecl( 1182 data->location, 1183 "value", 1184 value 1185 ); 1186 1187 out->members.push_back( value_obj ); 1188 out->members.push_back( tag_obj ); 1189 return out; 1085 1190 } 1086 1191 -
src/Parser/DeclarationNode.h
r2b78949 r044ae62 76 76 static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message ); 77 77 78 // Experimental algebric data type 79 static DeclarationNode * newAdt( const std::string * name, DeclarationNode * constructors ); 80 static DeclarationNode * newDataConstructor( const std::string * name ); 81 // static DeclarationNode * newDataConstructor( const std::string * name, DeclarationNode * typeSpecifiers ); 82 78 83 DeclarationNode(); 79 84 ~DeclarationNode(); … … 156 161 ExpressionNode * bitfieldWidth = nullptr; 157 162 std::unique_ptr<ExpressionNode> enumeratorValue; 163 158 164 bool hasEllipsis = false; 159 165 ast::Linkage::Spec linkage; … … 211 217 void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList ); 212 218 219 std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode ); 220 ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::StructDecl>> & typeList ); 221 ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::StructDecl>> & typeList ); 222 ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tag, const ast::UnionDecl * data_union ); 223 213 224 template<typename AstType, typename NodeType, 214 225 template<typename, typename...> class Container, typename... Args> -
src/Parser/TypeData.cc
r2b78949 r044ae62 59 59 enumeration.anon = false; 60 60 break; 61 case Adt: 62 adt.name = nullptr; 63 adt.data_constructors = nullptr; 64 break; 61 65 case Aggregate: 62 66 aggregate.kind = ast::AggregateDecl::NoAggregate; … … 160 164 delete qualified.child; 161 165 break; 166 case Adt: 167 delete adt.data_constructors; 168 delete adt.name; 169 break; 162 170 } // switch 163 171 } // TypeData::~TypeData … … 217 225 newtype->enumeration.body = enumeration.body; 218 226 newtype->enumeration.anon = enumeration.anon; 227 newtype->enumeration.data_constructors = maybeClone( enumeration.data_constructors ); 228 break; 229 case Adt: 230 newtype->adt.data_constructors = maybeClone( enumeration.data_constructors ); 231 newtype->adt.name = new string ( *adt.name ); 219 232 break; 220 233 case Symbolic: … … 459 472 case Enum: 460 473 return enumeration.name; 474 case Adt: 475 return adt.name; 461 476 case Symbolic: 462 477 case SymbolicInst: … … 822 837 case TypeData::Symbolic: 823 838 case TypeData::Enum: 839 case TypeData::Adt: 824 840 case TypeData::Aggregate: 825 841 assert( false ); … … 1260 1276 ); 1261 1277 buildList( td->enumeration.constants, ret->members ); 1278 if ( td->enumeration.data_constructors != nullptr ) { 1279 assert( false ); 1280 // ret->data_constructors = buildDataConstructors( td->enumeration.data_constructors ); 1281 // ret->data_union = buildDataUnion( td->location, ret->data_constructors ); 1282 // ret->tag = buildTag( td->location, ret->data_constructors ); 1283 // ret->tag_union = buildTaggedUnions( td, ret->tag.get(), ret->data_union.get() ); 1284 } 1285 1286 // if ( ret->data_constructors.size() > 0 ) ret->isData = true; 1262 1287 auto members = ret->members.begin(); 1263 1288 ret->hide = td->enumeration.hiding == EnumHiding::Hide ? ast::EnumDecl::EnumHiding::Hide : ast::EnumDecl::EnumHiding::Visible; … … 1286 1311 return ret; 1287 1312 } // buildEnum 1313 1314 ast::AdtDecl * buildAdt(const TypeData * td, 1315 std::vector<ast::ptr<ast::Attribute>> && attributes, 1316 ast::Linkage::Spec linkage ) { 1317 assert( td->kind == TypeData::Adt ); 1318 ast::AdtDecl * ret = new ast::AdtDecl( td->location, *(td->adt.name) ); 1319 ret->data_constructors = buildDataConstructors( td->adt.data_constructors ); 1320 ret->data_union = buildDataUnion( td->location, ret->data_constructors ); 1321 ret->tag = buildTag( td->location, ret->data_constructors ); 1322 ret->tag_union = buildTaggedUnions( td, ret->tag.get(), ret->data_union.get() ); 1323 return ret; 1324 } 1288 1325 1289 1326 … … 1429 1466 } else if ( td->kind == TypeData::Enum ) { 1430 1467 return buildEnum( td, std::move( attributes ), linkage ); 1468 } else if ( td->kind == TypeData::Adt) { 1469 return buildAdt( td, std::move( attributes), linkage ); 1431 1470 } else if ( td->kind == TypeData::Symbolic ) { 1432 1471 return buildSymbolic( td, std::move( attributes ), name, scs, linkage ); -
src/Parser/TypeData.h
r2b78949 r044ae62 25 25 struct TypeData { 26 26 enum Kind { Basic, Pointer, Reference, Array, Function, Aggregate, AggregateInst, Enum, EnumConstant, Symbolic, 27 SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, Unknown };27 SymbolicInst, Tuple, Basetypeof, Typeof, Vtable, Builtin, GlobalScope, Qualified, Adt, Ctor, Unknown }; 28 28 29 29 struct Aggregate_t { … … 58 58 bool typed; 59 59 EnumHiding hiding; 60 bool isData = false; 61 62 DeclarationNode * data_constructors = nullptr; 63 }; 64 65 struct ADT_t { 66 const std::string * name = nullptr; 67 DeclarationNode * data_constructors; 60 68 }; 61 69 … … 98 106 Array_t array; 99 107 Enumeration_t enumeration; 108 ADT_t adt; 109 100 110 Function_t function; 101 111 Symbolic_t symbolic; … … 124 134 ast::TypeDecl * buildVariable( const TypeData * ); 125 135 ast::EnumDecl * buildEnum( const TypeData *, std::vector<ast::ptr<ast::Attribute>> &&, ast::Linkage::Spec ); 136 ast::EnumDecl * buildAst( const TypeData *, std::vector<ast::ptr<ast::Attribute>> &&, ast::Linkage::Spec ); 126 137 ast::TypeInstType * buildSymbolicInst( const TypeData * ); 127 138 ast::TupleType * buildTuple( const TypeData * ); -
src/Parser/lex.ll
r2b78949 r044ae62 353 353 with { KEYWORD_RETURN(WITH); } // CFA 354 354 zero_t { NUMERIC_RETURN(ZERO_T); } // CFA 355 _DATA_ { KEYWORD_RETURN(DATA); } // Experimental 355 356 356 357 /* identifier */ -
src/Parser/parser.yy
r2b78949 r044ae62 340 340 %token SIZEOF TYPEOF VA_LIST VA_ARG AUTO_TYPE // GCC 341 341 %token OFFSETOF BASETYPEOF TYPEID // CFA 342 %token ENUM STRUCT UNION 342 %token ENUM STRUCT UNION DATA 343 343 %token EXCEPTION // CFA 344 344 %token GENERATOR COROUTINE MONITOR THREAD // CFA … … 455 455 %type<decl> enumerator_list enum_type enum_type_nobody 456 456 %type<init> enumerator_value_opt 457 458 %type<decl> value_list 459 %type<decl> data_constructor type_specifier_list 457 460 458 461 %type<decl> external_definition external_definition_list external_definition_list_opt … … 2449 2452 } 2450 2453 | enum_type 2454 /* | algebric_data_type */ 2451 2455 ; 2452 2456 … … 2702 2706 } 2703 2707 | enum_type_nobody 2704 ; 2708 | DATA identifier 2709 { typedefTable.makeTypedef( *$2 ); } 2710 '{' value_list '}' 2711 { 2712 $$ = DeclarationNode::newAdt( $2, $5 ); 2713 } 2714 ; 2715 2716 value_list: 2717 data_constructor 2718 { 2719 $$ = $1; 2720 } 2721 /* | identifier_or_type_name '(' type_specifier ')' 2722 { 2723 $$ = DeclarationNode::newEnumValueGeneric( $1, nullptr ); 2724 } */ 2725 /* | data_constructor '|' value_list */ 2726 | value_list '|' data_constructor 2727 { 2728 { $$ = $1->appendList( $3 ); } 2729 } 2730 ; 2731 2732 data_constructor: 2733 identifier_or_type_name 2734 { 2735 typedefTable.makeTypedef( *$1 ); 2736 $$ = DeclarationNode::newTypeDecl( $1, nullptr );; 2737 } 2738 | identifier_or_type_name '(' type_specifier_list ')' 2739 { 2740 typedefTable.makeTypedef( *$1 ); 2741 $$ = DeclarationNode::newTypeDecl( $1, $3 ); 2742 } 2743 2744 type_specifier_list: 2745 type_specifier 2746 /* | type_specifier ',' type_specifier_list */ 2747 | type_specifier_list ',' type_specifier 2748 { 2749 $$ = $1->appendList($3); 2750 } 2751 ; 2752 2705 2753 2706 2754 hide_opt: -
src/SymTab/Indexer.cc
r2b78949 r044ae62 606 606 } 607 607 608 void Indexer::addAdt( const AdtDecl * decl ) { 609 ++*stats().add_calls; 610 const std::string & id = decl->name; 611 612 if ( ! adtTable ) { 613 adtTable = AdtTable::new_ptr(); 614 } else { 615 ++* stats().map_lookups; 616 auto existing = adtTable->find( id ); 617 if ( existing != adtTable->end() 618 && existing->second.scope == scope 619 && addedDeclConflicts( existing->second.decl, decl ) ) return; 620 621 } 622 623 lazyInitScope(); 624 ++* stats().map_mutations; 625 adtTable = adtTable->set( id, Scoped<AdtDecl>{ decl, scope} ); 626 } 627 608 628 void Indexer::addUnion( const std::string & id ) { 609 629 addUnion( new UnionDecl( id ) ); -
src/SymTab/Indexer.h
r2b78949 r044ae62 89 89 void addStruct( const StructDecl * decl ); 90 90 void addEnum( const EnumDecl * decl ); 91 void addAdt( const AdtDecl * decl ); 91 92 void addUnion( const std::string & id ); 92 93 void addUnion( const UnionDecl * decl ); … … 124 125 using UnionTable = PersistentMap< std::string, Scoped<UnionDecl> >; 125 126 using TraitTable = PersistentMap< std::string, Scoped<TraitDecl> >; 127 using AdtTable = PersistentMap< std::string, Scoped<AdtDecl> >; 126 128 127 129 IdTable::Ptr idTable; ///< identifier namespace … … 131 133 UnionTable::Ptr unionTable; ///< union namespace 132 134 TraitTable::Ptr traitTable; ///< trait namespace 135 AdtTable::Ptr adtTable; ///< adt namespace 133 136 134 137 Ptr prevScope; ///< reference to indexer for parent scope -
src/SynTree/AggregateDecl.cc
r2b78949 r044ae62 29 29 30 30 // These must harmonize with the corresponding AggregateDecl::Aggregate enumerations. 31 static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName" };31 static const char * aggregateNames[] = { "struct", "union", "enum", "exception", "trait", "generator", "coroutine", "monitor", "thread", "NoAggregateName", "data" }; 32 32 33 33 const char * AggregateDecl::aggrString( AggregateDecl::Aggregate aggr ) { -
src/SynTree/Declaration.h
r2b78949 r044ae62 268 268 typedef Declaration Parent; 269 269 public: 270 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };270 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, ADT }; 271 271 static const char * aggrString( Aggregate aggr ); 272 272 … … 362 362 }; 363 363 364 class AdtDecl : public AggregateDecl { 365 typedef AggregateDecl Parent; 366 public: 367 std::list<StructDecl*> data_constructors; 368 UnionDecl * data_union; 369 EnumDecl * tag; 370 StructDecl * tag_union; 371 372 AdtDecl( const std::string & name, 373 const std::list< Attribute * > & attributes = std::list< class Attribute * >(), 374 LinkageSpec::Spec linkage = LinkageSpec::Cforall, 375 const std::list< StructDecl* > data_constructors = std::list< StructDecl * >(), 376 UnionDecl * data_union = nullptr, EnumDecl * tag = nullptr, StructDecl * tag_union = nullptr ) 377 : Parent( name, attributes, linkage ), data_constructors(data_constructors), 378 data_union( data_union ), tag( tag ), tag_union( tag_union ) {} 379 380 AdtDecl( const AdtDecl & other ) 381 : Parent( other ) {} 382 383 virtual AdtDecl * clone() const override { return new AdtDecl( *this ); } 384 virtual void accept( Visitor & v ) override { v.visit( this ); } 385 virtual void accept( Visitor & v ) const override { v.visit( this ); } 386 387 virtual Declaration * acceptMutator( Mutator & m ) override { return m.mutate( this ); } 388 virtual void print( std::ostream & os, Indenter indent = {} ) const override final { 389 os << "AdtDecl ... " << indent; 390 } 391 392 private: 393 virtual const char * typeString() const override { 394 return "AdtDecl"; 395 } 396 }; 397 364 398 class TraitDecl : public AggregateDecl { 365 399 typedef AggregateDecl Parent; -
src/SynTree/Mutator.h
r2b78949 r044ae62 30 30 virtual Declaration * mutate( UnionDecl * aggregateDecl ) = 0; 31 31 virtual Declaration * mutate( EnumDecl * aggregateDecl ) = 0; 32 virtual Declaration * mutate( AdtDecl * aggregateDecl ) = 0; 32 33 virtual Declaration * mutate( TraitDecl * aggregateDecl ) = 0; 33 34 virtual Declaration * mutate( TypeDecl * typeDecl ) = 0; -
src/SynTree/SynTree.h
r2b78949 r044ae62 31 31 class UnionDecl; 32 32 class EnumDecl; 33 class AdtDecl; 33 34 class TraitDecl; 34 35 class NamedTypeDecl; -
src/SynTree/Visitor.h
r2b78949 r044ae62 37 37 virtual void visit( EnumDecl * node ) { visit( const_cast<const EnumDecl *>(node) ); } 38 38 virtual void visit( const EnumDecl * aggregateDecl ) = 0; 39 virtual void visit( AdtDecl * node ) { visit( const_cast<const AdtDecl *>(node) ); } 40 virtual void visit( const AdtDecl * node ) = 0; 39 41 virtual void visit( TraitDecl * node ) { visit( const_cast<const TraitDecl *>(node) ); } 40 42 virtual void visit( const TraitDecl * aggregateDecl ) = 0; -
src/Validate/Autogen.cpp
r2b78949 r044ae62 125 125 // Built-ins do not use autogeneration. 126 126 bool shouldAutogen() const final { return !decl->linkage.is_builtin && !structHasFlexibleArray(decl); } 127 void genADTFuncs(); 128 void getADTFuncBody(const ast::ObjectDecl * lhs, ast::FunctionDecl * func); 127 129 private: 128 130 void genFuncBody( ast::FunctionDecl * decl ) final; … … 238 240 if ( !enumDecl->body ) return; 239 241 240 // if ( auto enumBaseType = enumDecl->base ) {241 // if ( auto enumBaseTypeAsStructInst = dynamic_cast<const ast::StructInstType *>(enumBaseType.get()) ) {242 // const ast::StructDecl * structDecl = enumBaseTypeAsStructInst->base.get();243 // this->previsit( structDecl );244 // }245 // }246 247 242 ast::EnumInstType enumInst( enumDecl->name ); 248 243 enumInst.base = enumDecl; … … 264 259 } 265 260 StructFuncGenerator gen( structDecl, &structInst, functionNesting ); 261 262 gen.genADTFuncs(); 266 263 gen.generateAndAppendFunctions( declsToAddAfter ); 267 264 } … … 475 472 } 476 473 produceDecl( decl ); 474 } 475 } 476 477 void StructFuncGenerator::getADTFuncBody( 478 const ast::ObjectDecl * lhs, 479 ast::FunctionDecl * func 480 ) { 481 const CodeLocation& location = func->location; 482 assert( decl->members.size() == 2 ); 483 auto first = (decl->members[0]).as<ast::ObjectDecl>(); 484 assert(first != nullptr); 485 auto firstType = first->type; 486 auto unionInstDecl = firstType.as<ast::UnionInstType>(); 487 assert(unionInstDecl != nullptr); 488 489 auto unionDecl = unionInstDecl->base; 490 491 const ast::ObjectDecl * dstParam = 492 func->params.front().strict_as<ast::ObjectDecl>(); 493 const ast::ObjectDecl * srcParam = 494 func->params.back().strict_as<ast::ObjectDecl>(); 495 496 ast::Expr * srcSelect = new ast::VariableExpr( location, srcParam ); 497 498 ast::CompoundStmt * stmts = new ast::CompoundStmt( location ); 499 500 InitTweak::InitExpander_new srcParamTweak( srcSelect ); 501 ast::Expr * dstSelect = 502 new ast::MemberExpr( 503 location, 504 lhs, 505 new ast::MemberExpr( 506 location, 507 first, 508 new ast::CastExpr( 509 location, 510 new ast::VariableExpr( location, dstParam ), 511 dstParam->type.strict_as<ast::ReferenceType>()->base 512 ) 513 ) 514 ); 515 auto stmt = genImplicitCall( 516 srcParamTweak, dstSelect, location, func->name, 517 first, SymTab::LoopForward 518 ); 519 stmts->push_back( stmt ); 520 func->stmts = stmts; 521 } 522 523 void StructFuncGenerator::genADTFuncs() { 524 if ( decl->kind != ast::AggregateDecl::Adt ) return; 525 assert( decl->members.size() == 2 ); 526 auto first = (decl->members[0]).as<ast::ObjectDecl>(); 527 assert(first != nullptr); 528 auto firstType = first->type; 529 auto unionInstDecl = firstType.as<ast::UnionInstType>(); 530 assert(unionInstDecl != nullptr); 531 auto unionDecl = unionInstDecl->base; 532 533 // for (auto mem: unionDecl->members) { 534 for ( std::size_t i = 0; i < unionDecl->members.size(); ++i ) { 535 auto mem = unionDecl->members[i]; 536 const ast::ObjectDecl * mem_as_obj = mem.as<ast::ObjectDecl>(); 537 assert( mem_as_obj ); 538 auto mem_type = mem_as_obj->type.as<ast::StructInstType>(); 539 assert( mem_type ); 540 auto location = getLocation(); 541 ast::FunctionDecl * func = new ast::FunctionDecl( 542 getLocation(), 543 "?{}", // name 544 {}, //forall 545 { dstParam(), new ast::ObjectDecl( getLocation(), "_src", ast::deepCopy( mem_type ) ) }, // params 546 {}, // returns 547 {}, // statements 548 // Use static storage if we are at the top level. 549 (0 < functionNesting) ? ast::Storage::Classes() : ast::Storage::Static, 550 proto_linkage, 551 std::vector<ast::ptr<ast::Attribute>>(), 552 // Auto-generated routines are inline to avoid conflicts. 553 ast::Function::Specs( ast::Function::Inline ) 554 ); 555 getADTFuncBody(mem_as_obj, func); 556 func->fixUniqueId(); 557 produceForwardDecl(func); 558 if ( CodeGen::isAssignment( func->name ) ) { 559 appendReturnThis( func ); 560 } 561 produceDecl( func ); 477 562 } 478 563 } -
src/Validate/HoistStruct.cpp
r2b78949 r044ae62 138 138 } 139 139 140 void hoistAdt( [[maybe_unused]] ast::TranslationUnit & trnaslationUnit ) { 141 142 } 143 140 144 } // namespace Validate 141 145 -
src/Validate/HoistStruct.hpp
r2b78949 r044ae62 24 24 /// Flattens nested type declarations. (Run right after Fix Qualified Types.) 25 25 void hoistStruct( ast::TranslationUnit & translationUnit ); 26 void hoistAdt( ast::TranslationUnit & trnaslationUnit ); 26 27 27 28 } -
src/Validate/NoIdSymbolTable.hpp
r2b78949 r044ae62 44 44 FORWARD_1( addStruct, const ast::StructDecl * ) 45 45 FORWARD_1( addEnum , const ast::EnumDecl * ) 46 FORWARD_1( addAdt, const ast::AdtDecl * ) 46 47 FORWARD_1( addUnion , const ast::UnionDecl * ) 47 48 FORWARD_1( addTrait , const ast::TraitDecl * )
Note:
See TracChangeset
for help on using the changeset viewer.