Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Mangler.cc

    r0e761e40 re73becf  
    7373                                bool mangleGenericParams;       ///< Include generic parameters in name mangling if true
    7474                                bool inFunctionType = false;    ///< Include type qualifiers if false.
    75                                 bool inQualifiedType = false;   ///< Add start/end delimiters around qualified type
    7675
    7776                                void mangleDecl( DeclarationWithType *declaration );
     
    111110                                        isTopLevel = false;
    112111                                } // if
    113                                 mangleName << Encoding::manglePrefix;
     112                                mangleName << "__";
    114113                                CodeGen::OperatorInfo opInfo;
    115114                                if ( operatorLookup( declaration->get_name(), opInfo ) ) {
    116                                         mangleName << opInfo.outputName.size() << opInfo.outputName;
     115                                        mangleName << opInfo.outputName;
    117116                                } else {
    118                                         mangleName << declaration->name.size() << declaration->name;
     117                                        mangleName << declaration->get_name();
    119118                                } // if
     119                                mangleName << "__";
    120120                                maybeAccept( declaration->get_type(), *visitor );
    121121                                if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) {
     
    123123                                        // so they need a different name mangling
    124124                                        if ( declaration->get_linkage() == LinkageSpec::AutoGen ) {
    125                                                 mangleName << Encoding::autogen;
     125                                                mangleName << "autogen__";
    126126                                        } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) {
    127                                                 mangleName << Encoding::intrinsic;
     127                                                mangleName << "intrinsic__";
    128128                                        } else {
    129129                                                // if we add another kind of overridable function, this has to change
     
    144144                        void Mangler::postvisit( VoidType * voidType ) {
    145145                                printQualifiers( voidType );
    146                                 mangleName << Encoding::void_t;
     146                                mangleName << "v";
    147147                        }
    148148
    149149                        void Mangler::postvisit( BasicType * basicType ) {
     150                                static const char *btLetter[] = {
     151                                        "b",    // Bool
     152                                        "c",    // Char
     153                                        "Sc",   // SignedChar
     154                                        "Uc",   // UnsignedChar
     155                                        "s",    // ShortSignedInt
     156                                        "Us",   // ShortUnsignedInt
     157                                        "i",    // SignedInt
     158                                        "Ui",   // UnsignedInt
     159                                        "l",    // LongSignedInt
     160                                        "Ul",   // LongUnsignedInt
     161                                        "q",    // LongLongSignedInt
     162                                        "Uq",   // LongLongUnsignedInt
     163                                        "f",    // Float
     164                                        "d",    // Double
     165                                        "r",    // LongDouble
     166                                        "Xf",   // FloatComplex
     167                                        "Xd",   // DoubleComplex
     168                                        "Xr",   // LongDoubleComplex
     169                                        "If",   // FloatImaginary
     170                                        "Id",   // DoubleImaginary
     171                                        "Ir",   // LongDoubleImaginary
     172                                        "w",    // SignedInt128
     173                                        "Uw",   // UnsignedInt128
     174                                        "x",    // Float80
     175                                        "y",    // Float128
     176                                };
     177                                static_assert(
     178                                        sizeof(btLetter)/sizeof(btLetter[0]) == BasicType::NUMBER_OF_BASIC_TYPES,
     179                                        "Each basic type kind should have a corresponding mangler letter"
     180                                );
     181
    150182                                printQualifiers( basicType );
    151                                 assertf( basicType->get_kind() < BasicType::NUMBER_OF_BASIC_TYPES, "Unhandled basic type: %d", basicType->get_kind() );
    152                                 mangleName << Encoding::basicTypes[ basicType->get_kind() ];
     183                                assert( basicType->get_kind() < sizeof(btLetter)/sizeof(btLetter[0]) );
     184                                mangleName << btLetter[ basicType->get_kind() ];
    153185                        }
    154186
     
    156188                                printQualifiers( pointerType );
    157189                                // mangle void (*f)() and void f() to the same name to prevent overloading on functions and function pointers
    158                                 if ( ! dynamic_cast<FunctionType *>( pointerType->base ) ) mangleName << Encoding::pointer;
     190                                if ( ! dynamic_cast<FunctionType *>( pointerType->base ) ) mangleName << "P";
    159191                                maybeAccept( pointerType->base, *visitor );
    160192                        }
     
    163195                                // TODO: encode dimension
    164196                                printQualifiers( arrayType );
    165                                 mangleName << Encoding::array << "0";
     197                                mangleName << "A0";
    166198                                maybeAccept( arrayType->base, *visitor );
    167199                        }
     
    188220                        void Mangler::postvisit( FunctionType * functionType ) {
    189221                                printQualifiers( functionType );
    190                                 mangleName << Encoding::function;
     222                                mangleName << "F";
    191223                                // turn on inFunctionType so that printQualifiers does not print most qualifiers for function parameters,
    192224                                // since qualifiers on outermost parameter type do not differentiate function types, e.g.,
     
    195227                                inFunctionType = true;
    196228                                std::list< Type* > returnTypes = getTypes( functionType->returnVals );
    197                                 if (returnTypes.empty()) mangleName << Encoding::void_t;
    198                                 else acceptAll( returnTypes, *visitor );
     229                                acceptAll( returnTypes, *visitor );
    199230                                mangleName << "_";
    200231                                std::list< Type* > paramTypes = getTypes( functionType->parameters );
     
    206237                                printQualifiers( refType );
    207238
    208                                 mangleName << prefix << refType->name.length() << refType->name;
     239                                mangleName << ( refType->name.length() + prefix.length() ) << prefix << refType->name;
    209240
    210241                                if ( mangleGenericParams ) {
     
    223254
    224255                        void Mangler::postvisit( StructInstType * aggregateUseType ) {
    225                                 mangleRef( aggregateUseType, Encoding::struct_t );
     256                                mangleRef( aggregateUseType, "s" );
    226257                        }
    227258
    228259                        void Mangler::postvisit( UnionInstType * aggregateUseType ) {
    229                                 mangleRef( aggregateUseType, Encoding::union_t );
     260                                mangleRef( aggregateUseType, "u" );
    230261                        }
    231262
    232263                        void Mangler::postvisit( EnumInstType * aggregateUseType ) {
    233                                 mangleRef( aggregateUseType, Encoding::enum_t );
     264                                mangleRef( aggregateUseType, "e" );
    234265                        }
    235266
     
    237268                                VarMapType::iterator varNum = varNums.find( typeInst->get_name() );
    238269                                if ( varNum == varNums.end() ) {
    239                                         mangleRef( typeInst, Encoding::type );
     270                                        mangleRef( typeInst, "t" );
    240271                                } else {
    241272                                        printQualifiers( typeInst );
    242                                         // Note: Can't use name here, since type variable names do not actually disambiguate a function, e.g.
    243                                         //   forall(dtype T) void f(T);
    244                                         //   forall(dtype S) void f(S);
    245                                         // are equivalent and should mangle the same way. This is accomplished by numbering the type variables when they
    246                                         // are first found and prefixing with the appropriate encoding for the type class.
    247                                         assertf( varNum->second.second < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", varNum->second.second );
    248                                         mangleName << Encoding::typeVariables[varNum->second.second] << varNum->second.first;
     273                                        std::ostringstream numStream;
     274                                        numStream << varNum->second.first;
     275                                        switch ( (TypeDecl::Kind )varNum->second.second ) {
     276                                          case TypeDecl::Dtype:
     277                                                mangleName << "d";
     278                                                break;
     279                                          case TypeDecl::Ftype:
     280                                                mangleName << "f";
     281                                                break;
     282                                                case TypeDecl::Ttype:
     283                                                mangleName << "tVARGS";
     284                                                break;
     285                                                default:
     286                                                assert( false );
     287                                        } // switch
     288                                        mangleName << numStream.str();
    249289                                } // if
    250290                        }
     
    252292                        void Mangler::postvisit( TraitInstType * inst ) {
    253293                                printQualifiers( inst );
    254                                 mangleName << inst->name.size() << inst->name;
     294                                mangleName << "_Y" << inst->name << "_";
    255295                        }
    256296
    257297                        void Mangler::postvisit( TupleType * tupleType ) {
    258298                                printQualifiers( tupleType );
    259                                 mangleName << Encoding::tuple << tupleType->types.size();
     299                                mangleName << "T";
    260300                                acceptAll( tupleType->types, *visitor );
     301                                mangleName << "_";
    261302                        }
    262303
    263304                        void Mangler::postvisit( VarArgsType * varArgsType ) {
    264305                                printQualifiers( varArgsType );
    265                                 static const std::string vargs = "__builtin_va_list";
    266                                 mangleName << Encoding::type << vargs.size() << vargs;
     306                                mangleName << "VARGS";
    267307                        }
    268308
    269309                        void Mangler::postvisit( ZeroType * ) {
    270                                 mangleName << Encoding::zero;
     310                                mangleName << "Z";
    271311                        }
    272312
    273313                        void Mangler::postvisit( OneType * ) {
    274                                 mangleName << Encoding::one;
     314                                mangleName << "O";
    275315                        }
    276316
    277317                        void Mangler::postvisit( QualifiedType * qualType ) {
    278                                 bool inqual = inQualifiedType;
    279                                 if (! inqual ) {
    280                                         // N marks the start of a qualified type
    281                                         inQualifiedType = true;
    282                                         mangleName << Encoding::qualifiedTypeStart;
    283                                 }
    284318                                maybeAccept( qualType->parent, *visitor );
     319                                mangleName << "__";
    285320                                maybeAccept( qualType->child, *visitor );
    286                                 if ( ! inqual ) {
    287                                         // E marks the end of a qualified type
    288                                         inQualifiedType = false;
    289                                         mangleName << Encoding::qualifiedTypeEnd;
    290                                 }
    291321                        }
    292322
    293323                        void Mangler::postvisit( TypeDecl * decl ) {
    294                                 // TODO: is there any case where mangling a TypeDecl makes sense? If so, this code needs to be
    295                                 // fixed to ensure that two TypeDecls mangle to the same name when they are the same type and vice versa.
    296                                 // Note: The current scheme may already work correctly for this case, I have not thought about this deeply
    297                                 // and the case has not yet come up in practice. Alternatively, if not then this code can be removed
    298                                 // aside from the assert false.
    299                                 assertf(false, "Mangler should not visit typedecl: %s", toCString(decl));
    300                                 assertf( decl->get_kind() < TypeDecl::NUMBER_OF_KINDS, "Unhandled type variable kind: %d", decl->get_kind() );
    301                                 mangleName << Encoding::typeVariables[ decl->get_kind() ] << ( decl->name.length() ) << decl->name;
     324                                static const char *typePrefix[] = { "BT", "BD", "BF" };
     325                                mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name;
    302326                        }
    303327
     
    313337                                if ( ! type->get_forall().empty() ) {
    314338                                        std::list< std::string > assertionNames;
    315                                         int dcount = 0, fcount = 0, vcount = 0, acount = 0;
    316                                         mangleName << Encoding::forall;
     339                                        int tcount = 0, dcount = 0, fcount = 0, vcount = 0;
     340                                        mangleName << "A";
    317341                                        for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) {
    318342                                                switch ( (*i)->get_kind() ) {
     
    337361                                                        (*assert)->accept( sub_mangler );
    338362                                                        assertionNames.push_back( sub_mangler.pass.mangleName.str() );
    339                                                         acount++;
    340363                                                } // for
    341364                                        } // for
    342                                         mangleName << dcount << "_" << fcount << "_" << vcount << "_" << acount << "_";
     365                                        mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_";
    343366                                        std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) );
    344367                                        mangleName << "_";
     
    347370                                        // these qualifiers do not distinguish the outermost type of a function parameter
    348371                                        if ( type->get_const() ) {
    349                                                 mangleName << Encoding::qualifiers.at(Type::Const);
     372                                                mangleName << "C";
    350373                                        } // if
    351374                                        if ( type->get_volatile() ) {
    352                                                 mangleName << Encoding::qualifiers.at(Type::Volatile);
     375                                                mangleName << "V";
    353376                                        } // if
    354377                                        // Removed due to restrict not affecting function compatibility in GCC
     
    357380                                        // } // if
    358381                                        if ( type->get_atomic() ) {
    359                                                 mangleName << Encoding::qualifiers.at(Type::Atomic);
     382                                                mangleName << "A";
    360383                                        } // if
    361384                                }
    362385                                if ( type->get_mutex() ) {
    363                                         mangleName << Encoding::qualifiers.at(Type::Mutex);
     386                                        mangleName << "M";
    364387                                } // if
    365388                                if ( type->get_lvalue() ) {
    366389                                        // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues
    367                                         mangleName << Encoding::qualifiers.at(Type::Lvalue);
     390                                        mangleName << "L";
    368391                                }
    369392
Note: See TracChangeset for help on using the changeset viewer.