Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Mangler.cc

    r3e5dd913 r07de76b  
    1010// Created On       : Sun May 17 21:40:29 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Nov 18 12:01:38 2020
    13 // Update Count     : 64
     12// Last Modified On : Fri Dec 13 23:43:49 2019
     13// Update Count     : 28
    1414//
    1515#include "Mangler.h"
     
    6565                                void postvisit( const QualifiedType * qualType );
    6666
    67                                 std::string get_mangleName() { return mangleName; }
     67                                std::string get_mangleName() { return mangleName.str(); }
    6868                          private:
    69                                 std::string mangleName;         ///< Mangled name being constructed
     69                                std::ostringstream mangleName;  ///< Mangled name being constructed
    7070                                typedef std::map< std::string, std::pair< int, int > > VarMapType;
    7171                                VarMapType varNums;             ///< Map of type variables to indices
     
    127127                                        isTopLevel = false;
    128128                                } // if
    129                                 mangleName += Encoding::manglePrefix;
    130                                 const CodeGen::OperatorInfo * opInfo = CodeGen::operatorLookup( declaration->get_name() );
    131                                 if ( opInfo ) {
    132                                         mangleName += std::to_string( opInfo->outputName.size() ) + opInfo->outputName;
     129                                mangleName << Encoding::manglePrefix;
     130                                CodeGen::OperatorInfo opInfo;
     131                                if ( operatorLookup( declaration->get_name(), opInfo ) ) {
     132                                        mangleName << opInfo.outputName.size() << opInfo.outputName;
    133133                                } else {
    134                                         mangleName += std::to_string( declaration->name.size() ) + declaration->name;
     134                                        mangleName << declaration->name.size() << declaration->name;
    135135                                } // if
    136136                                maybeAccept( declaration->get_type(), *visitor );
     
    139139                                        // so they need a different name mangling
    140140                                        if ( declaration->get_linkage() == LinkageSpec::AutoGen ) {
    141                                                 mangleName += Encoding::autogen;
     141                                                mangleName << Encoding::autogen;
    142142                                        } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) {
    143                                                 mangleName += Encoding::intrinsic;
     143                                                mangleName << Encoding::intrinsic;
    144144                                        } else {
    145145                                                // if we add another kind of overridable function, this has to change
     
    160160                        void Mangler_old::postvisit( const VoidType * voidType ) {
    161161                                printQualifiers( voidType );
    162                                 mangleName += Encoding::void_t;
     162                                mangleName << Encoding::void_t;
    163163                        }
    164164
     
    166166                                printQualifiers( basicType );
    167167                                assertf( basicType->kind < BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->kind );
    168                                 mangleName += Encoding::basicTypes[ basicType->kind ];
     168                                mangleName << Encoding::basicTypes[ basicType->kind ];
    169169                        }
    170170
     
    172172                                printQualifiers( pointerType );
    173173                                // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers
    174                                 if ( ! dynamic_cast<FunctionType *>( pointerType->base ) ) mangleName += Encoding::pointer;
     174                                if ( ! dynamic_cast<FunctionType *>( pointerType->base ) ) mangleName << Encoding::pointer;
    175175                                maybeAccept( pointerType->base, *visitor );
    176176                        }
     
    179179                                // TODO: encode dimension
    180180                                printQualifiers( arrayType );
    181                                 mangleName += Encoding::array + "0";
     181                                mangleName << Encoding::array << "0";
    182182                                maybeAccept( arrayType->base, *visitor );
    183183                        }
     
    204204                        void Mangler_old::postvisit( const FunctionType * functionType ) {
    205205                                printQualifiers( functionType );
    206                                 mangleName += Encoding::function;
     206                                mangleName << Encoding::function;
    207207                                // turn on inFunctionType so that printQualifiers does not print most qualifiers for function parameters,
    208208                                // since qualifiers on outermost parameter type do not differentiate function types, e.g.,
     
    211211                                inFunctionType = true;
    212212                                std::list< Type* > returnTypes = getTypes( functionType->returnVals );
    213                                 if (returnTypes.empty()) mangleName += Encoding::void_t;
     213                                if (returnTypes.empty()) mangleName << Encoding::void_t;
    214214                                else acceptAll( returnTypes, *visitor );
    215                                 mangleName += "_";
     215                                mangleName << "_";
    216216                                std::list< Type* > paramTypes = getTypes( functionType->parameters );
    217217                                acceptAll( paramTypes, *visitor );
    218                                 mangleName += "_";
     218                                mangleName << "_";
    219219                        }
    220220
     
    222222                                printQualifiers( refType );
    223223
    224                                 mangleName += prefix + std::to_string( refType->name.length() ) + refType->name;
     224                                mangleName << prefix << refType->name.length() << refType->name;
    225225
    226226                                if ( mangleGenericParams ) {
    227227                                        const std::list< Expression* > & params = refType->parameters;
    228228                                        if ( ! params.empty() ) {
    229                                                 mangleName += "_";
     229                                                mangleName << "_";
    230230                                                for ( const Expression * param : params ) {
    231231                                                        const TypeExpr * paramType = dynamic_cast< const TypeExpr * >( param );
     
    233233                                                        maybeAccept( paramType->type, *visitor );
    234234                                                }
    235                                                 mangleName += "_";
     235                                                mangleName << "_";
    236236                                        }
    237237                                }
     
    262262                                        // are first found and prefixing with the appropriate encoding for the type class.
    263263                                        assertf( varNum->second.second < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", varNum->second.second );
    264                                         mangleName += Encoding::typeVariables[varNum->second.second] + std::to_string( varNum->second.first );
     264                                        mangleName << Encoding::typeVariables[varNum->second.second] << varNum->second.first;
    265265                                } // if
    266266                        }
     
    268268                        void Mangler_old::postvisit( const TraitInstType * inst ) {
    269269                                printQualifiers( inst );
    270                                 mangleName += std::to_string( inst->name.size() ) + inst->name;
     270                                mangleName << inst->name.size() << inst->name;
    271271                        }
    272272
    273273                        void Mangler_old::postvisit( const TupleType * tupleType ) {
    274274                                printQualifiers( tupleType );
    275                                 mangleName += Encoding::tuple + std::to_string( tupleType->types.size() );
     275                                mangleName << Encoding::tuple << tupleType->types.size();
    276276                                acceptAll( tupleType->types, *visitor );
    277277                        }
     
    280280                                printQualifiers( varArgsType );
    281281                                static const std::string vargs = "__builtin_va_list";
    282                                 mangleName += Encoding::type + std::to_string( vargs.size() ) + vargs;
     282                                mangleName << Encoding::type << vargs.size() << vargs;
    283283                        }
    284284
    285285                        void Mangler_old::postvisit( const ZeroType * ) {
    286                                 mangleName += Encoding::zero;
     286                                mangleName << Encoding::zero;
    287287                        }
    288288
    289289                        void Mangler_old::postvisit( const OneType * ) {
    290                                 mangleName += Encoding::one;
     290                                mangleName << Encoding::one;
    291291                        }
    292292
     
    296296                                        // N marks the start of a qualified type
    297297                                        inQualifiedType = true;
    298                                         mangleName += Encoding::qualifiedTypeStart;
     298                                        mangleName << Encoding::qualifiedTypeStart;
    299299                                }
    300300                                maybeAccept( qualType->parent, *visitor );
     
    303303                                        // E marks the end of a qualified type
    304304                                        inQualifiedType = false;
    305                                         mangleName += Encoding::qualifiedTypeEnd;
     305                                        mangleName << Encoding::qualifiedTypeEnd;
    306306                                }
    307307                        }
     
    315315                                assertf(false, "Mangler_old should not visit typedecl: %s", toCString(decl));
    316316                                assertf( decl->kind < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->kind );
    317                                 mangleName += Encoding::typeVariables[ decl->kind ] + std::to_string( decl->name.length() ) + decl->name;
     317                                mangleName << Encoding::typeVariables[ decl->kind ] << ( decl->name.length() ) << decl->name;
    318318                        }
    319319
     
    330330                                        std::list< std::string > assertionNames;
    331331                                        int dcount = 0, fcount = 0, vcount = 0, acount = 0;
    332                                         mangleName += Encoding::forall;
     332                                        mangleName << Encoding::forall;
    333333                                        for ( const TypeDecl * i : type->forall ) {
    334334                                                switch ( i->kind ) {
     
    354354                                                } // for
    355355                                        } // for
    356                                         mangleName += std::to_string( dcount ) + "_" + std::to_string( fcount ) + "_" + std::to_string( vcount ) + "_" + std::to_string( acount ) + "_";
    357                                         for(const auto & a : assertionNames) mangleName += a;
    358 //                                      std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
    359                                         mangleName += "_";
     356                                        mangleName << dcount << "_" << fcount << "_" << vcount << "_" << acount << "_";
     357                                        std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
     358                                        mangleName << "_";
    360359                                } // if
    361360                                if ( ! inFunctionType ) {
    362361                                        // these qualifiers do not distinguish the outermost type of a function parameter
    363362                                        if ( type->get_const() ) {
    364                                                 mangleName += Encoding::qualifiers.at(Type::Const);
     363                                                mangleName << Encoding::qualifiers.at(Type::Const);
    365364                                        } // if
    366365                                        if ( type->get_volatile() ) {
    367                                                 mangleName += Encoding::qualifiers.at(Type::Volatile);
     366                                                mangleName << Encoding::qualifiers.at(Type::Volatile);
    368367                                        } // if
    369368                                        // Removed due to restrict not affecting function compatibility in GCC
    370369                                        // if ( type->get_isRestrict() ) {
    371                                         //      mangleName += "E";
     370                                        //      mangleName << "E";
    372371                                        // } // if
    373372                                        if ( type->get_atomic() ) {
    374                                                 mangleName += Encoding::qualifiers.at(Type::Atomic);
     373                                                mangleName << Encoding::qualifiers.at(Type::Atomic);
    375374                                        } // if
    376375                                }
    377376                                if ( type->get_mutex() ) {
    378                                         mangleName += Encoding::qualifiers.at(Type::Mutex);
     377                                        mangleName << Encoding::qualifiers.at(Type::Mutex);
    379378                                } // if
    380379                                if ( inFunctionType ) {
     
    384383                                }
    385384                        }
    386                 } // namespace
     385                }       // namespace
    387386        } // namespace Mangler
    388387} // namespace SymTab
     
    418417                        void postvisit( const ast::QualifiedType * qualType );
    419418
    420                         std::string get_mangleName() { return mangleName; }
     419                        std::string get_mangleName() { return mangleName.str(); }
    421420                  private:
    422                         std::string mangleName;         ///< Mangled name being constructed
     421                        std::ostringstream mangleName;  ///< Mangled name being constructed
    423422                        typedef std::map< std::string, std::pair< int, int > > VarMapType;
    424423                        VarMapType varNums;             ///< Map of type variables to indices
     
    438437                  private:
    439438                        void mangleDecl( const ast::DeclWithType *declaration );
    440                         void mangleRef( const ast::BaseInstType *refType, std::string prefix );
     439                        void mangleRef( const ast::ReferenceToType *refType, std::string prefix );
    441440
    442441                        void printQualifiers( const ast::Type *type );
     
    448447                ast::Pass<Mangler_new> mangler( mode );
    449448                maybeAccept( decl, mangler );
    450                 return mangler.core.get_mangleName();
     449                return mangler.pass.get_mangleName();
    451450        }
    452451
     
    471470                                isTopLevel = false;
    472471                        } // if
    473                         mangleName += Encoding::manglePrefix;
    474                         const CodeGen::OperatorInfo * opInfo = CodeGen::operatorLookup( decl->name );
    475                         if ( opInfo ) {
    476                                 mangleName += std::to_string( opInfo->outputName.size() ) + opInfo->outputName;
     472                        mangleName << Encoding::manglePrefix;
     473                        CodeGen::OperatorInfo opInfo;
     474                        if ( operatorLookup( decl->name, opInfo ) ) {
     475                                mangleName << opInfo.outputName.size() << opInfo.outputName;
    477476                        } else {
    478                                 mangleName += std::to_string( decl->name.size() ) + decl->name;
     477                                mangleName << decl->name.size() << decl->name;
    479478                        } // if
    480479                        maybeAccept( decl->get_type(), *visitor );
     
    483482                                // so they need a different name mangling
    484483                                if ( decl->linkage == ast::Linkage::AutoGen ) {
    485                                         mangleName += Encoding::autogen;
     484                                        mangleName << Encoding::autogen;
    486485                                } else if ( decl->linkage == ast::Linkage::Intrinsic ) {
    487                                         mangleName += Encoding::intrinsic;
     486                                        mangleName << Encoding::intrinsic;
    488487                                } else {
    489488                                        // if we add another kind of overridable function, this has to change
     
    504503                void Mangler_new::postvisit( const ast::VoidType * voidType ) {
    505504                        printQualifiers( voidType );
    506                         mangleName += Encoding::void_t;
     505                        mangleName << Encoding::void_t;
    507506                }
    508507
     
    510509                        printQualifiers( basicType );
    511510                        assertf( basicType->kind < ast::BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->kind );
    512                         mangleName += Encoding::basicTypes[ basicType->kind ];
     511                        mangleName << Encoding::basicTypes[ basicType->kind ];
    513512                }
    514513
     
    516515                        printQualifiers( pointerType );
    517516                        // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers
    518                         if ( ! pointerType->base.as<ast::FunctionType>() ) mangleName += Encoding::pointer;
     517                        if ( ! pointerType->base.as<ast::FunctionType>() ) mangleName << Encoding::pointer;
    519518                        maybe_accept( pointerType->base.get(), *visitor );
    520519                }
     
    523522                        // TODO: encode dimension
    524523                        printQualifiers( arrayType );
    525                         mangleName += Encoding::array + "0";
     524                        mangleName << Encoding::array << "0";
    526525                        maybeAccept( arrayType->base.get(), *visitor );
    527526                }
     
    546545                void Mangler_new::postvisit( const ast::FunctionType * functionType ) {
    547546                        printQualifiers( functionType );
    548                         mangleName += Encoding::function;
     547                        mangleName << Encoding::function;
    549548                        // turn on inFunctionType so that printQualifiers does not print most qualifiers for function parameters,
    550549                        // since qualifiers on outermost parameter type do not differentiate function types, e.g.,
     
    552551                        GuardValue( inFunctionType );
    553552                        inFunctionType = true;
    554                         if (functionType->returns.empty()) mangleName += Encoding::void_t;
    555                         else accept_each( functionType->returns, *visitor );
    556                         mangleName += "_";
    557                         accept_each( functionType->params, *visitor );
    558                         mangleName += "_";
    559                 }
    560 
    561                 void Mangler_new::mangleRef( const ast::BaseInstType * refType, std::string prefix ) {
     553                        std::vector< ast::ptr< ast::Type > > returnTypes = getTypes( functionType->returns );
     554                        if (returnTypes.empty()) mangleName << Encoding::void_t;
     555                        else accept_each( returnTypes, *visitor );
     556                        mangleName << "_";
     557                        std::vector< ast::ptr< ast::Type > > paramTypes = getTypes( functionType->params );
     558                        accept_each( paramTypes, *visitor );
     559                        mangleName << "_";
     560                }
     561
     562                void Mangler_new::mangleRef( const ast::ReferenceToType * refType, std::string prefix ) {
    562563                        printQualifiers( refType );
    563564
    564                         mangleName += prefix + std::to_string( refType->name.length() ) + refType->name;
     565                        mangleName << prefix << refType->name.length() << refType->name;
    565566
    566567                        if ( mangleGenericParams ) {
    567568                                if ( ! refType->params.empty() ) {
    568                                         mangleName += "_";
     569                                        mangleName << "_";
    569570                                        for ( const ast::Expr * param : refType->params ) {
    570571                                                auto paramType = dynamic_cast< const ast::TypeExpr * >( param );
     
    572573                                                maybeAccept( paramType->type.get(), *visitor );
    573574                                        }
    574                                         mangleName += "_";
     575                                        mangleName << "_";
    575576                                }
    576577                        }
     
    601602                                // are first found and prefixing with the appropriate encoding for the type class.
    602603                                assertf( varNum->second.second < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", varNum->second.second );
    603                                 mangleName += Encoding::typeVariables[varNum->second.second] + std::to_string( varNum->second.first );
     604                                mangleName << Encoding::typeVariables[varNum->second.second] << varNum->second.first;
    604605                        } // if
    605606                }
     
    607608                void Mangler_new::postvisit( const ast::TraitInstType * inst ) {
    608609                        printQualifiers( inst );
    609                         mangleName += std::to_string( inst->name.size() ) + inst->name;
     610                        mangleName << inst->name.size() << inst->name;
    610611                }
    611612
    612613                void Mangler_new::postvisit( const ast::TupleType * tupleType ) {
    613614                        printQualifiers( tupleType );
    614                         mangleName += Encoding::tuple + std::to_string( tupleType->types.size() );
     615                        mangleName << Encoding::tuple << tupleType->types.size();
    615616                        accept_each( tupleType->types, *visitor );
    616617                }
     
    619620                        printQualifiers( varArgsType );
    620621                        static const std::string vargs = "__builtin_va_list";
    621                         mangleName += Encoding::type + std::to_string( vargs.size() ) + vargs;
     622                        mangleName << Encoding::type << vargs.size() << vargs;
    622623                }
    623624
    624625                void Mangler_new::postvisit( const ast::ZeroType * ) {
    625                         mangleName += Encoding::zero;
     626                        mangleName << Encoding::zero;
    626627                }
    627628
    628629                void Mangler_new::postvisit( const ast::OneType * ) {
    629                         mangleName += Encoding::one;
     630                        mangleName << Encoding::one;
    630631                }
    631632
     
    635636                                // N marks the start of a qualified type
    636637                                inQualifiedType = true;
    637                                 mangleName += Encoding::qualifiedTypeStart;
     638                                mangleName << Encoding::qualifiedTypeStart;
    638639                        }
    639640                        maybeAccept( qualType->parent.get(), *visitor );
     
    642643                                // E marks the end of a qualified type
    643644                                inQualifiedType = false;
    644                                 mangleName += Encoding::qualifiedTypeEnd;
     645                                mangleName << Encoding::qualifiedTypeEnd;
    645646                        }
    646647                }
     
    654655                        assertf(false, "Mangler_new should not visit typedecl: %s", toCString(decl));
    655656                        assertf( decl->kind < ast::TypeDecl::Kind::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->kind );
    656                         mangleName += Encoding::typeVariables[ decl->kind ] + std::to_string( decl->name.length() ) + decl->name;
     657                        mangleName << Encoding::typeVariables[ decl->kind ] << ( decl->name.length() ) << decl->name;
    657658                }
    658659
     
    666667                        // skip if not including qualifiers
    667668                        if ( typeMode ) return;
    668                         if ( auto ptype = dynamic_cast< const ast::FunctionType * >(type) ) {
     669                        if ( auto ptype = dynamic_cast< const ast::ParameterizedType * >(type) ) {
    669670                                if ( ! ptype->forall.empty() ) {
    670671                                        std::list< std::string > assertionNames;
    671672                                        int dcount = 0, fcount = 0, vcount = 0, acount = 0;
    672                                         mangleName += Encoding::forall;
    673                                         for ( auto & decl : ptype->forall ) {
     673                                        mangleName << Encoding::forall;
     674                                        for ( const ast::TypeDecl * decl : ptype->forall ) {
    674675                                                switch ( decl->kind ) {
    675676                                                case ast::TypeDecl::Kind::Dtype:
     
    686687                                                } // switch
    687688                                                varNums[ decl->name ] = std::make_pair( nextVarNum, (int)decl->kind );
     689                                                for ( const ast::DeclWithType * assert : decl->assertions ) {
     690                                                        ast::Pass<Mangler_new> sub_mangler(
     691                                                                mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums );
     692                                                        assert->accept( sub_mangler );
     693                                                        assertionNames.push_back( sub_mangler.pass.get_mangleName() );
     694                                                        acount++;
     695                                                } // for
    688696                                        } // for
    689                                         for ( auto & assert : ptype->assertions ) {
    690                                                 ast::Pass<Mangler_new> sub_mangler(
    691                                                         mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums );
    692                                                 assert->var->accept( sub_mangler );
    693                                                 assertionNames.push_back( sub_mangler.core.get_mangleName() );
    694                                                 acount++;
    695                                         } // for
    696                                         mangleName += std::to_string( dcount ) + "_" + std::to_string( fcount ) + "_" + std::to_string( vcount ) + "_" + std::to_string( acount ) + "_";
    697                                         for(const auto & a : assertionNames) mangleName += a;
    698 //                                      std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
    699                                         mangleName += "_";
     697                                        mangleName << dcount << "_" << fcount << "_" << vcount << "_" << acount << "_";
     698                                        std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
     699                                        mangleName << "_";
    700700                                } // if
    701701                        } // if
     
    703703                                // these qualifiers do not distinguish the outermost type of a function parameter
    704704                                if ( type->is_const() ) {
    705                                         mangleName += Encoding::qualifiers.at(Type::Const);
     705                                        mangleName << Encoding::qualifiers.at(Type::Const);
    706706                                } // if
    707707                                if ( type->is_volatile() ) {
    708                                         mangleName += Encoding::qualifiers.at(Type::Volatile);
     708                                        mangleName << Encoding::qualifiers.at(Type::Volatile);
    709709                                } // if
    710710                                // Removed due to restrict not affecting function compatibility in GCC
    711711                                // if ( type->get_isRestrict() ) {
    712                                 //      mangleName += "E";
     712                                //      mangleName << "E";
    713713                                // } // if
    714714                                if ( type->is_atomic() ) {
    715                                         mangleName += Encoding::qualifiers.at(Type::Atomic);
     715                                        mangleName << Encoding::qualifiers.at(Type::Atomic);
    716716                                } // if
    717717                        }
    718718                        if ( type->is_mutex() ) {
    719                                 mangleName += Encoding::qualifiers.at(Type::Mutex);
     719                                mangleName << Encoding::qualifiers.at(Type::Mutex);
    720720                        } // if
    721721                        if ( inFunctionType ) {
Note: See TracChangeset for help on using the changeset viewer.