- Timestamp:
- Jun 4, 2019, 5:53:39 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 67130fe, 93744b5
- Parents:
- 9519aba
- Location:
- src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Node.hpp
r9519aba r1346914 18 18 #include <cassert> 19 19 #include <iosfwd> 20 21 #include "Common/ErrorObjects.h" // for SemanticErrorException 20 22 21 23 namespace ast { … … 100 102 } 101 103 104 /// Call a visitor on a collection of nodes, throwing any exceptions when completed 105 template< typename Container > 106 void accept_each( const Container & c, Visitor & v ) { 107 SemanticErrorException errors; 108 for ( const auto & i : c ) { 109 try { 110 if ( i ) { 111 i->accept( v ); 112 } 113 } catch ( SemanticErrorException & e ) { 114 errors.append( e ); 115 } 116 } 117 if ( ! errors.isEmpty() ) { 118 throw errors; 119 } 120 } 121 102 122 /// Base class for the smart pointer types 103 123 /// should never really be used. -
src/SymTab/Mangler.cc
r9519aba r1346914 399 399 Mangler_new( const Mangler_new & ) = delete; 400 400 401 void previsit( ast::Node * ) { visit_children = false; }402 403 void postvisit( ast::ObjectDecl * declaration );404 void postvisit( ast::FunctionDecl * declaration );405 void postvisit( ast::TypeDecl * declaration );406 407 void postvisit( ast::VoidType * voidType );408 void postvisit( ast::BasicType * basicType );409 void postvisit( ast::PointerType * pointerType );410 void postvisit( ast::ArrayType * arrayType );411 void postvisit( ast::ReferenceType * refType );412 void postvisit( ast::FunctionType * functionType );413 void postvisit( ast::StructInstType * aggregateUseType );414 void postvisit( ast::UnionInstType * aggregateUseType );415 void postvisit( ast::EnumInstType * aggregateUseType );416 void postvisit( ast::TypeInstType * aggregateUseType );417 void postvisit( ast::TraitInstType * inst );418 void postvisit( ast::TupleType * tupleType );419 void postvisit( ast::VarArgsType * varArgsType );420 void postvisit( ast::ZeroType * zeroType );421 void postvisit( ast::OneType * oneType );422 void postvisit( ast::QualifiedType * qualType );401 void previsit( const ast::Node * ) { visit_children = false; } 402 403 void postvisit( const ast::ObjectDecl * declaration ); 404 void postvisit( const ast::FunctionDecl * declaration ); 405 void postvisit( const ast::TypeDecl * declaration ); 406 407 void postvisit( const ast::VoidType * voidType ); 408 void postvisit( const ast::BasicType * basicType ); 409 void postvisit( const ast::PointerType * pointerType ); 410 void postvisit( const ast::ArrayType * arrayType ); 411 void postvisit( const ast::ReferenceType * refType ); 412 void postvisit( const ast::FunctionType * functionType ); 413 void postvisit( const ast::StructInstType * aggregateUseType ); 414 void postvisit( const ast::UnionInstType * aggregateUseType ); 415 void postvisit( const ast::EnumInstType * aggregateUseType ); 416 void postvisit( const ast::TypeInstType * aggregateUseType ); 417 void postvisit( const ast::TraitInstType * inst ); 418 void postvisit( const ast::TupleType * tupleType ); 419 void postvisit( const ast::VarArgsType * varArgsType ); 420 void postvisit( const ast::ZeroType * zeroType ); 421 void postvisit( const ast::OneType * oneType ); 422 void postvisit( const ast::QualifiedType * qualType ); 423 423 424 424 std::string get_mangleName() { return mangleName.str(); } … … 441 441 442 442 private: 443 void mangleDecl( ast::DeclWithType *declaration );444 void mangleRef( ast::ReferenceToType *refType, std::string prefix );445 446 void printQualifiers( ast::Type *type );443 void mangleDecl( const ast::DeclWithType *declaration ); 444 void mangleRef( const ast::ReferenceToType *refType, std::string prefix ); 445 446 void printQualifiers( const ast::Type *type ); 447 447 }; // Mangler_new 448 448 } // namespace … … 468 468 mangleGenericParams( mangleGenericParams ) {} 469 469 470 void Mangler_new::mangleDecl( ast::DeclWithType * decl ) {470 void Mangler_new::mangleDecl( const ast::DeclWithType * decl ) { 471 471 bool wasTopLevel = isTopLevel; 472 472 if ( isTopLevel ) { … … 498 498 } 499 499 500 void Mangler_new::postvisit( ast::ObjectDecl * decl ) {500 void Mangler_new::postvisit( const ast::ObjectDecl * decl ) { 501 501 mangleDecl( decl ); 502 502 } 503 503 504 void Mangler_new::postvisit( ast::FunctionDecl * decl ) {504 void Mangler_new::postvisit( const ast::FunctionDecl * decl ) { 505 505 mangleDecl( decl ); 506 506 } 507 507 508 void Mangler_new::postvisit( ast::VoidType * voidType ) {508 void Mangler_new::postvisit( const ast::VoidType * voidType ) { 509 509 printQualifiers( voidType ); 510 510 mangleName << Encoding::void_t; 511 511 } 512 512 513 void Mangler_new::postvisit( ast::BasicType * basicType ) {513 void Mangler_new::postvisit( const ast::BasicType * basicType ) { 514 514 printQualifiers( basicType ); 515 515 assertf( basicType->kind < ast::BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->kind ); … … 517 517 } 518 518 519 void Mangler_new::postvisit( ast::PointerType * pointerType ) {519 void Mangler_new::postvisit( const ast::PointerType * pointerType ) { 520 520 printQualifiers( pointerType ); 521 521 // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers … … 524 524 } 525 525 526 void Mangler_new::postvisit( ast::ArrayType * arrayType ) {526 void Mangler_new::postvisit( const ast::ArrayType * arrayType ) { 527 527 // TODO: encode dimension 528 528 printQualifiers( arrayType ); … … 531 531 } 532 532 533 void Mangler_new::postvisit( ast::ReferenceType * refType ) {533 void Mangler_new::postvisit( const ast::ReferenceType * refType ) { 534 534 // don't print prefix (e.g. 'R') for reference types so that references and non-references do not overload. 535 535 // Further, do not print the qualifiers for a reference type (but do run printQualifers because of TypeDecls, etc.), … … 541 541 } 542 542 543 namespace { 544 inline std::vector< ast::ptr< ast::Type > > getTypes( const std::vector< ast::ptr< ast::DeclWithType > > & decls ) { 545 std::vector< ast::ptr< ast::Type > > ret; 546 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 547 std::mem_fun( &ast::DeclWithType::get_type ) ); 548 return ret; 549 } 550 } 551 552 void Mangler_new::postvisit( ast::FunctionType * functionType ) { 543 inline std::vector< ast::ptr< ast::Type > > getTypes( const std::vector< ast::ptr< ast::DeclWithType > > & decls ) { 544 std::vector< ast::ptr< ast::Type > > ret; 545 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 546 std::mem_fun( &ast::DeclWithType::get_type ) ); 547 return ret; 548 } 549 550 void Mangler_new::postvisit( const ast::FunctionType * functionType ) { 553 551 printQualifiers( functionType ); 554 552 mangleName << Encoding::function; … … 560 558 std::vector< ast::ptr< ast::Type > > returnTypes = getTypes( functionType->returns ); 561 559 if (returnTypes.empty()) mangleName << Encoding::void_t; 562 else accept All( returnTypes, *visitor );560 else accept_each( returnTypes, *visitor ); 563 561 mangleName << "_"; 564 562 std::vector< ast::ptr< ast::Type > > paramTypes = getTypes( functionType->params ); 565 accept All( paramTypes, *visitor );563 accept_each( paramTypes, *visitor ); 566 564 mangleName << "_"; 567 565 } 568 566 569 void Mangler_new::mangleRef( ast::ReferenceToType * refType, std::string prefix ) {567 void Mangler_new::mangleRef( const ast::ReferenceToType * refType, std::string prefix ) { 570 568 printQualifiers( refType ); 571 569 … … 573 571 574 572 if ( mangleGenericParams ) { 575 std::vector< ast::ptr< ast::Expr > >& params = refType->params; 576 if ( ! params.empty() ) { 573 if ( ! refType->params.empty() ) { 577 574 mangleName << "_"; 578 for ( std::vector< ast::ptr< ast::Expr > >::const_iterator param = params.begin(); param != params.end(); ++param) {579 const ast::TypeExpr *paramType = param->as< ast::TypeExpr >();580 assertf(paramType, "Aggregate parameters should be type expressions: %s", toCString( *param));575 for ( const ast::Expr * param : refType->params ) { 576 auto paramType = dynamic_cast< const ast::TypeExpr * >( param ); 577 assertf(paramType, "Aggregate parameters should be type expressions: %s", toCString(param)); 581 578 maybeAccept( paramType->type.get(), *visitor ); 582 579 } … … 586 583 } 587 584 588 void Mangler_new::postvisit( ast::StructInstType * aggregateUseType ) {585 void Mangler_new::postvisit( const ast::StructInstType * aggregateUseType ) { 589 586 mangleRef( aggregateUseType, Encoding::struct_t ); 590 587 } 591 588 592 void Mangler_new::postvisit( ast::UnionInstType * aggregateUseType ) {589 void Mangler_new::postvisit( const ast::UnionInstType * aggregateUseType ) { 593 590 mangleRef( aggregateUseType, Encoding::union_t ); 594 591 } 595 592 596 void Mangler_new::postvisit( ast::EnumInstType * aggregateUseType ) {593 void Mangler_new::postvisit( const ast::EnumInstType * aggregateUseType ) { 597 594 mangleRef( aggregateUseType, Encoding::enum_t ); 598 595 } 599 596 600 void Mangler_new::postvisit( ast::TypeInstType * typeInst ) {597 void Mangler_new::postvisit( const ast::TypeInstType * typeInst ) { 601 598 VarMapType::iterator varNum = varNums.find( typeInst->name ); 602 599 if ( varNum == varNums.end() ) { … … 614 611 } 615 612 616 void Mangler_new::postvisit( ast::TraitInstType * inst ) {613 void Mangler_new::postvisit( const ast::TraitInstType * inst ) { 617 614 printQualifiers( inst ); 618 615 mangleName << inst->name.size() << inst->name; 619 616 } 620 617 621 void Mangler_new::postvisit( ast::TupleType * tupleType ) {618 void Mangler_new::postvisit( const ast::TupleType * tupleType ) { 622 619 printQualifiers( tupleType ); 623 620 mangleName << Encoding::tuple << tupleType->types.size(); 624 accept All( tupleType->types, *visitor );625 } 626 627 void Mangler_new::postvisit( ast::VarArgsType * varArgsType ) {621 accept_each( tupleType->types, *visitor ); 622 } 623 624 void Mangler_new::postvisit( const ast::VarArgsType * varArgsType ) { 628 625 printQualifiers( varArgsType ); 629 626 static const std::string vargs = "__builtin_va_list"; … … 631 628 } 632 629 633 void Mangler_new::postvisit( ast::ZeroType * ) {630 void Mangler_new::postvisit( const ast::ZeroType * ) { 634 631 mangleName << Encoding::zero; 635 632 } 636 633 637 void Mangler_new::postvisit( ast::OneType * ) {634 void Mangler_new::postvisit( const ast::OneType * ) { 638 635 mangleName << Encoding::one; 639 636 } 640 637 641 void Mangler_new::postvisit( ast::QualifiedType * qualType ) {638 void Mangler_new::postvisit( const ast::QualifiedType * qualType ) { 642 639 bool inqual = inQualifiedType; 643 640 if (! inqual ) { … … 655 652 } 656 653 657 void Mangler_new::postvisit( ast::TypeDecl * decl ) {654 void Mangler_new::postvisit( const ast::TypeDecl * decl ) { 658 655 // TODO: is there any case where mangling a TypeDecl makes sense? If so, this code needs to be 659 656 // fixed to ensure that two TypeDecls mangle to the same name when they are the same type and vice versa. … … 672 669 } 673 670 674 void Mangler_new::printQualifiers( ast::Type * type ) {671 void Mangler_new::printQualifiers( const ast::Type * type ) { 675 672 // skip if not including qualifiers 676 673 if ( typeMode ) return; 677 if ( a st::ParameterizedType * ptype = dynamic_cast<ast::ParameterizedType * >(type) ) {674 if ( auto ptype = dynamic_cast< const ast::ParameterizedType * >(type) ) { 678 675 if ( ! ptype->forall.empty() ) { 679 676 std::list< std::string > assertionNames; 680 677 int dcount = 0, fcount = 0, vcount = 0, acount = 0; 681 678 mangleName << Encoding::forall; 682 for ( ast::ParameterizedType::ForallList::iterator i = ptype->forall.begin(); i != ptype->forall.end(); ++i) {683 switch ( (*i)->kind ) {684 679 for ( const ast::TypeDecl * decl : ptype->forall ) { 680 switch ( decl->kind ) { 681 case ast::TypeVar::Kind::Dtype: 685 682 dcount++; 686 683 break; 687 684 case ast::TypeVar::Kind::Ftype: 688 685 fcount++; 689 686 break; 690 687 case ast::TypeVar::Kind::Ttype: 691 688 vcount++; 692 689 break; 693 690 default: 694 691 assert( false ); 695 692 } // switch 696 varNums[ (*i)->name ] = std::make_pair( nextVarNum, (int)(*i)->kind );697 for ( std::vector< ast::ptr< ast::DeclWithType > >::const_iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert) {693 varNums[ decl->name ] = std::make_pair( nextVarNum, (int)decl->kind ); 694 for ( const ast::DeclWithType * assert : decl->assertions ) { 698 695 ast::Pass<Mangler_new> sub_mangler( 699 696 mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums ); 700 (*assert)->accept( sub_mangler );697 assert->accept( sub_mangler ); 701 698 assertionNames.push_back( sub_mangler.pass.get_mangleName() ); 702 699 acount++;
Note: See TracChangeset
for help on using the changeset viewer.