Changeset d7d9a60 for src/SymTab
- Timestamp:
- Jan 17, 2018, 11:17:42 AM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 36a2367
- Parents:
- ad51cc2
- Location:
- src/SymTab
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Mangler.cc
rad51cc2 rd7d9a60 23 23 24 24 #include "CodeGen/OperatorTable.h" // for OperatorInfo, operatorLookup 25 #include "Common/PassVisitor.h" 25 26 #include "Common/SemanticError.h" // for SemanticError 26 27 #include "Common/utility.h" // for toString … … 31 32 32 33 namespace SymTab { 33 std::string Mangler::mangleType( Type * ty ){34 Mangler mangler( false, true, true );35 maybeAccept( ty, mangler );36 return mangler.get_mangleName();37 }38 39 std::string Mangler::mangleConcrete( Type* ty ) { 40 Mangler mangler( false, false, false );41 maybeAccept( ty, mangler ); 42 return mangler.get_mangleName();43 }44 45 Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) 46 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ), mangleGenericParams( mangleGenericParams ) {}47 48 Mangler::Mangler( const Mangler &rhs ) : mangleName() {49 varNums = rhs.varNums;50 nextVarNum = rhs.nextVarNum;51 isTopLevel = rhs.isTopLevel;52 mangleOverridable = rhs.mangleOverridable;53 typeMode = rhs.typeMode;54 }55 56 void Mangler::mangleDecl( DeclarationWithType * declaration ) {57 bool wasTopLevel = isTopLevel;58 if ( isTopLevel ) {59 varNums.clear();60 nextVarNum = 0; 61 isTopLevel = false;62 } // if63 mangleName << "__";64 CodeGen::OperatorInfo opInfo;65 if ( operatorLookup( declaration->get_name(), opInfo ) ) {66 mangleName << opInfo.outputName;67 } else {68 mangleName << declaration->get_name();69 } // if70 mangleName << "__";71 maybeAccept( declaration->get_type(), *this ); 72 if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) {73 // want to be able to override autogenerated and intrinsic routines,74 // so they need a different name mangling 75 if ( declaration->get_linkage() == LinkageSpec::AutoGen ) {76 mangleName << "autogen__";77 } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) {78 mangleName << "intrinsic__"; 79 } else{80 // if we add another kind of overridable function, this has to change81 assert( false && "unknown overrideable linkage");82 } // if34 namespace Mangler { 35 namespace { 36 /// Mangles names to a unique C identifier 37 struct Mangler : public WithShortCircuiting, public WithVisitorRef<Mangler> { 38 Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ); 39 Mangler( const Mangler & ); 40 41 void previsit( BaseSyntaxNode * ) { visit_children = false; } 42 43 void postvisit( ObjectDecl * declaration ); 44 void postvisit( FunctionDecl * declaration ); 45 void postvisit( TypeDecl * declaration ); 46 47 void postvisit( VoidType * voidType ); 48 void postvisit( BasicType * basicType ); 49 void postvisit( PointerType * pointerType ); 50 void postvisit( ArrayType * arrayType ); 51 void postvisit( ReferenceType * refType ); 52 void postvisit( FunctionType * functionType ); 53 void postvisit( StructInstType * aggregateUseType ); 54 void postvisit( UnionInstType * aggregateUseType ); 55 void postvisit( EnumInstType * aggregateUseType ); 56 void postvisit( TypeInstType * aggregateUseType ); 57 void postvisit( TupleType * tupleType ); 58 void postvisit( VarArgsType * varArgsType ); 59 void postvisit( ZeroType * zeroType ); 60 void postvisit( OneType * oneType ); 61 62 std::string get_mangleName() { return mangleName.str(); } 63 private: 64 std::ostringstream mangleName; ///< Mangled name being constructed 65 typedef std::map< std::string, std::pair< int, int > > VarMapType; 66 VarMapType varNums; ///< Map of type variables to indices 67 int nextVarNum; ///< Next type variable index 68 bool isTopLevel; ///< Is the Mangler at the top level 69 bool mangleOverridable; ///< Specially mangle overridable built-in methods 70 bool typeMode; ///< Produce a unique mangled name for a type 71 bool mangleGenericParams; ///< Include generic parameters in name mangling if true 72 73 void mangleDecl( DeclarationWithType *declaration ); 74 void mangleRef( ReferenceToType *refType, std::string prefix ); 75 76 void printQualifiers( Type *type ); 77 }; // Mangler 78 } // namespace 79 80 std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) { 81 PassVisitor<Mangler> mangler( mangleOverridable, typeMode, mangleGenericParams ); 82 maybeAccept( decl, mangler ); 83 return mangler.pass.get_mangleName(); 83 84 } 84 isTopLevel = wasTopLevel; 85 } 86 87 void Mangler::visit( ObjectDecl * declaration ) { 88 mangleDecl( declaration ); 89 } 90 91 void Mangler::visit( FunctionDecl * declaration ) { 92 mangleDecl( declaration ); 93 } 94 95 void Mangler::visit( VoidType * voidType ) { 96 printQualifiers( voidType ); 97 mangleName << "v"; 98 } 99 100 void Mangler::visit( BasicType * basicType ) { 101 static const char *btLetter[] = { 102 "b", // Bool 103 "c", // Char 104 "Sc", // SignedChar 105 "Uc", // UnsignedChar 106 "s", // ShortSignedInt 107 "Us", // ShortUnsignedInt 108 "i", // SignedInt 109 "Ui", // UnsignedInt 110 "l", // LongSignedInt 111 "Ul", // LongUnsignedInt 112 "q", // LongLongSignedInt 113 "Uq", // LongLongUnsignedInt 114 "f", // Float 115 "d", // Double 116 "r", // LongDouble 117 "Xf", // FloatComplex 118 "Xd", // DoubleComplex 119 "Xr", // LongDoubleComplex 120 "If", // FloatImaginary 121 "Id", // DoubleImaginary 122 "Ir", // LongDoubleImaginary 123 "w", // SignedInt128 124 "Uw", // UnsignedInt128 125 }; 126 127 printQualifiers( basicType ); 128 mangleName << btLetter[ basicType->get_kind() ]; 129 } 130 131 void Mangler::visit( PointerType * pointerType ) { 132 printQualifiers( pointerType ); 133 mangleName << "P"; 134 maybeAccept( pointerType->get_base(), *this ); 135 } 136 137 void Mangler::visit( ArrayType * arrayType ) { 138 // TODO: encode dimension 139 printQualifiers( arrayType ); 140 mangleName << "A0"; 141 maybeAccept( arrayType->get_base(), *this ); 142 } 143 144 void Mangler::visit( ReferenceType * refType ) { 145 printQualifiers( refType ); 146 mangleName << "R"; 147 maybeAccept( refType->get_base(), *this ); 148 } 149 150 namespace { 151 inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { 152 std::list< Type* > ret; 153 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 154 std::mem_fun( &DeclarationWithType::get_type ) ); 155 return ret; 85 86 std::string mangleType( Type * ty ) { 87 PassVisitor<Mangler> mangler( false, true, true ); 88 maybeAccept( ty, mangler ); 89 return mangler.pass.get_mangleName(); 156 90 } 157 } 158 159 void Mangler::visit( FunctionType * functionType ) { 160 printQualifiers( functionType ); 161 mangleName << "F"; 162 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); 163 acceptAll( returnTypes, *this ); 164 mangleName << "_"; 165 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); 166 acceptAll( paramTypes, *this ); 167 mangleName << "_"; 168 } 169 170 void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) { 171 printQualifiers( refType ); 172 173 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); 174 175 if ( mangleGenericParams ) { 176 std::list< Expression* >& params = refType->get_parameters(); 177 if ( ! params.empty() ) { 91 92 std::string mangleConcrete( Type * ty ) { 93 PassVisitor<Mangler> mangler( false, false, false ); 94 maybeAccept( ty, mangler ); 95 return mangler.pass.get_mangleName(); 96 } 97 98 namespace { 99 Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) 100 : nextVarNum( 0 ), isTopLevel( true ), mangleOverridable( mangleOverridable ), typeMode( typeMode ), mangleGenericParams( mangleGenericParams ) {} 101 102 Mangler::Mangler( const Mangler &rhs ) : mangleName() { 103 varNums = rhs.varNums; 104 nextVarNum = rhs.nextVarNum; 105 isTopLevel = rhs.isTopLevel; 106 mangleOverridable = rhs.mangleOverridable; 107 typeMode = rhs.typeMode; 108 } 109 110 void Mangler::mangleDecl( DeclarationWithType * declaration ) { 111 bool wasTopLevel = isTopLevel; 112 if ( isTopLevel ) { 113 varNums.clear(); 114 nextVarNum = 0; 115 isTopLevel = false; 116 } // if 117 mangleName << "__"; 118 CodeGen::OperatorInfo opInfo; 119 if ( operatorLookup( declaration->get_name(), opInfo ) ) { 120 mangleName << opInfo.outputName; 121 } else { 122 mangleName << declaration->get_name(); 123 } // if 124 mangleName << "__"; 125 maybeAccept( declaration->get_type(), *visitor ); 126 if ( mangleOverridable && LinkageSpec::isOverridable( declaration->get_linkage() ) ) { 127 // want to be able to override autogenerated and intrinsic routines, 128 // so they need a different name mangling 129 if ( declaration->get_linkage() == LinkageSpec::AutoGen ) { 130 mangleName << "autogen__"; 131 } else if ( declaration->get_linkage() == LinkageSpec::Intrinsic ) { 132 mangleName << "intrinsic__"; 133 } else { 134 // if we add another kind of overridable function, this has to change 135 assert( false && "unknown overrideable linkage" ); 136 } // if 137 } 138 isTopLevel = wasTopLevel; 139 } 140 141 void Mangler::postvisit( ObjectDecl * declaration ) { 142 mangleDecl( declaration ); 143 } 144 145 void Mangler::postvisit( FunctionDecl * declaration ) { 146 mangleDecl( declaration ); 147 } 148 149 void Mangler::postvisit( VoidType * voidType ) { 150 printQualifiers( voidType ); 151 mangleName << "v"; 152 } 153 154 void Mangler::postvisit( BasicType * basicType ) { 155 static const char *btLetter[] = { 156 "b", // Bool 157 "c", // Char 158 "Sc", // SignedChar 159 "Uc", // UnsignedChar 160 "s", // ShortSignedInt 161 "Us", // ShortUnsignedInt 162 "i", // SignedInt 163 "Ui", // UnsignedInt 164 "l", // LongSignedInt 165 "Ul", // LongUnsignedInt 166 "q", // LongLongSignedInt 167 "Uq", // LongLongUnsignedInt 168 "f", // Float 169 "d", // Double 170 "r", // LongDouble 171 "Xf", // FloatComplex 172 "Xd", // DoubleComplex 173 "Xr", // LongDoubleComplex 174 "If", // FloatImaginary 175 "Id", // DoubleImaginary 176 "Ir", // LongDoubleImaginary 177 "w", // SignedInt128 178 "Uw", // UnsignedInt128 179 }; 180 181 printQualifiers( basicType ); 182 mangleName << btLetter[ basicType->get_kind() ]; 183 } 184 185 void Mangler::postvisit( PointerType * pointerType ) { 186 printQualifiers( pointerType ); 187 mangleName << "P"; 188 maybeAccept( pointerType->get_base(), *visitor ); 189 } 190 191 void Mangler::postvisit( ArrayType * arrayType ) { 192 // TODO: encode dimension 193 printQualifiers( arrayType ); 194 mangleName << "A0"; 195 maybeAccept( arrayType->get_base(), *visitor ); 196 } 197 198 void Mangler::postvisit( ReferenceType * refType ) { 199 printQualifiers( refType ); 200 mangleName << "R"; 201 maybeAccept( refType->get_base(), *visitor ); 202 } 203 204 namespace { 205 inline std::list< Type* > getTypes( const std::list< DeclarationWithType* > decls ) { 206 std::list< Type* > ret; 207 std::transform( decls.begin(), decls.end(), std::back_inserter( ret ), 208 std::mem_fun( &DeclarationWithType::get_type ) ); 209 return ret; 210 } 211 } 212 213 void Mangler::postvisit( FunctionType * functionType ) { 214 printQualifiers( functionType ); 215 mangleName << "F"; 216 std::list< Type* > returnTypes = getTypes( functionType->get_returnVals() ); 217 acceptAll( returnTypes, *visitor ); 178 218 mangleName << "_"; 179 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { 180 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 181 assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str()); 182 maybeAccept( paramType->get_type(), *this ); 219 std::list< Type* > paramTypes = getTypes( functionType->get_parameters() ); 220 acceptAll( paramTypes, *visitor ); 221 mangleName << "_"; 222 } 223 224 void Mangler::mangleRef( ReferenceToType * refType, std::string prefix ) { 225 printQualifiers( refType ); 226 227 mangleName << ( refType->get_name().length() + prefix.length() ) << prefix << refType->get_name(); 228 229 if ( mangleGenericParams ) { 230 std::list< Expression* >& params = refType->get_parameters(); 231 if ( ! params.empty() ) { 232 mangleName << "_"; 233 for ( std::list< Expression* >::const_iterator param = params.begin(); param != params.end(); ++param ) { 234 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); 235 assertf(paramType, "Aggregate parameters should be type expressions: %s", toString(*param).c_str()); 236 maybeAccept( paramType->get_type(), *visitor ); 237 } 238 mangleName << "_"; 239 } 183 240 } 241 } 242 243 void Mangler::postvisit( StructInstType * aggregateUseType ) { 244 mangleRef( aggregateUseType, "s" ); 245 } 246 247 void Mangler::postvisit( UnionInstType * aggregateUseType ) { 248 mangleRef( aggregateUseType, "u" ); 249 } 250 251 void Mangler::postvisit( EnumInstType * aggregateUseType ) { 252 mangleRef( aggregateUseType, "e" ); 253 } 254 255 void Mangler::postvisit( TypeInstType * typeInst ) { 256 VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); 257 if ( varNum == varNums.end() ) { 258 mangleRef( typeInst, "t" ); 259 } else { 260 printQualifiers( typeInst ); 261 std::ostringstream numStream; 262 numStream << varNum->second.first; 263 switch ( (TypeDecl::Kind )varNum->second.second ) { 264 case TypeDecl::Dtype: 265 mangleName << "d"; 266 break; 267 case TypeDecl::Ftype: 268 mangleName << "f"; 269 break; 270 case TypeDecl::Ttype: 271 mangleName << "tVARGS"; 272 break; 273 default: 274 assert( false ); 275 } // switch 276 mangleName << numStream.str(); 277 } // if 278 } 279 280 void Mangler::postvisit( TupleType * tupleType ) { 281 printQualifiers( tupleType ); 282 mangleName << "T"; 283 acceptAll( tupleType->types, *visitor ); 184 284 mangleName << "_"; 185 285 } 186 } 187 } 188 189 void Mangler::visit( StructInstType * aggregateUseType ) { 190 mangleRef( aggregateUseType, "s" ); 191 } 192 193 void Mangler::visit( UnionInstType * aggregateUseType ) { 194 mangleRef( aggregateUseType, "u" ); 195 } 196 197 void Mangler::visit( EnumInstType * aggregateUseType ) { 198 mangleRef( aggregateUseType, "e" ); 199 } 200 201 void Mangler::visit( TypeInstType * typeInst ) { 202 VarMapType::iterator varNum = varNums.find( typeInst->get_name() ); 203 if ( varNum == varNums.end() ) { 204 mangleRef( typeInst, "t" ); 205 } else { 206 printQualifiers( typeInst ); 207 std::ostringstream numStream; 208 numStream << varNum->second.first; 209 switch ( (TypeDecl::Kind )varNum->second.second ) { 210 case TypeDecl::Dtype: 211 mangleName << "d"; 212 break; 213 case TypeDecl::Ftype: 214 mangleName << "f"; 215 break; 216 case TypeDecl::Ttype: 217 mangleName << "tVARGS"; 218 break; 219 default: 220 assert( false ); 221 } // switch 222 mangleName << numStream.str(); 223 } // if 224 } 225 226 void Mangler::visit( TupleType * tupleType ) { 227 printQualifiers( tupleType ); 228 mangleName << "T"; 229 acceptAll( tupleType->types, *this ); 230 mangleName << "_"; 231 } 232 233 void Mangler::visit( VarArgsType * varArgsType ) { 234 printQualifiers( varArgsType ); 235 mangleName << "VARGS"; 236 } 237 238 void Mangler::visit( ZeroType * ) { 239 mangleName << "Z"; 240 } 241 242 void Mangler::visit( OneType * ) { 243 mangleName << "O"; 244 } 245 246 void Mangler::visit( TypeDecl * decl ) { 247 static const char *typePrefix[] = { "BT", "BD", "BF" }; 248 mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name; 249 } 250 251 void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { 252 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { 253 os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; 254 } // for 255 } 256 257 void Mangler::printQualifiers( Type * type ) { 258 // skip if not including qualifiers 259 if ( typeMode ) return; 260 261 if ( ! type->get_forall().empty() ) { 262 std::list< std::string > assertionNames; 263 int tcount = 0, dcount = 0, fcount = 0, vcount = 0; 264 mangleName << "A"; 265 for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { 266 switch ( (*i)->get_kind() ) { 267 case TypeDecl::Dtype: 268 dcount++; 269 break; 270 case TypeDecl::Ftype: 271 fcount++; 272 break; 273 case TypeDecl::Ttype: 274 vcount++; 275 break; 276 default: 277 assert( false ); 278 } // switch 279 varNums[ (*i)->name ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() ); 280 for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { 281 Mangler sub_mangler( mangleOverridable, typeMode, mangleGenericParams ); 282 sub_mangler.nextVarNum = nextVarNum; 283 sub_mangler.isTopLevel = false; 284 sub_mangler.varNums = varNums; 285 (*assert)->accept( sub_mangler ); 286 assertionNames.push_back( sub_mangler.mangleName.str() ); 286 287 void Mangler::postvisit( VarArgsType * varArgsType ) { 288 printQualifiers( varArgsType ); 289 mangleName << "VARGS"; 290 } 291 292 void Mangler::postvisit( ZeroType * ) { 293 mangleName << "Z"; 294 } 295 296 void Mangler::postvisit( OneType * ) { 297 mangleName << "O"; 298 } 299 300 void Mangler::postvisit( TypeDecl * decl ) { 301 static const char *typePrefix[] = { "BT", "BD", "BF" }; 302 mangleName << typePrefix[ decl->get_kind() ] << ( decl->name.length() + 1 ) << decl->name; 303 } 304 305 __attribute__((unused)) void printVarMap( const std::map< std::string, std::pair< int, int > > &varMap, std::ostream &os ) { 306 for ( std::map< std::string, std::pair< int, int > >::const_iterator i = varMap.begin(); i != varMap.end(); ++i ) { 307 os << i->first << "(" << i->second.first << "/" << i->second.second << ")" << std::endl; 287 308 } // for 288 } // for 289 mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; 290 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); 291 mangleName << "_"; 292 } // if 293 if ( type->get_const() ) { 294 mangleName << "C"; 295 } // if 296 if ( type->get_volatile() ) { 297 mangleName << "V"; 298 } // if 299 if ( type->get_mutex() ) { 300 mangleName << "M"; 301 } // if 302 // Removed due to restrict not affecting function compatibility in GCC 303 // if ( type->get_isRestrict() ) { 304 // mangleName << "E"; 305 // } // if 306 if ( type->get_lvalue() ) { 307 // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues 308 mangleName << "L"; 309 } 310 if ( type->get_atomic() ) { 311 mangleName << "A"; 312 } // if 313 } 309 } 310 311 void Mangler::printQualifiers( Type * type ) { 312 // skip if not including qualifiers 313 if ( typeMode ) return; 314 315 if ( ! type->get_forall().empty() ) { 316 std::list< std::string > assertionNames; 317 int tcount = 0, dcount = 0, fcount = 0, vcount = 0; 318 mangleName << "A"; 319 for ( Type::ForallList::iterator i = type->forall.begin(); i != type->forall.end(); ++i ) { 320 switch ( (*i)->get_kind() ) { 321 case TypeDecl::Dtype: 322 dcount++; 323 break; 324 case TypeDecl::Ftype: 325 fcount++; 326 break; 327 case TypeDecl::Ttype: 328 vcount++; 329 break; 330 default: 331 assert( false ); 332 } // switch 333 varNums[ (*i)->name ] = std::pair< int, int >( nextVarNum++, (int)(*i)->get_kind() ); 334 for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { 335 PassVisitor<Mangler> sub_mangler( mangleOverridable, typeMode, mangleGenericParams ); 336 sub_mangler.pass.nextVarNum = nextVarNum; 337 sub_mangler.pass.isTopLevel = false; 338 sub_mangler.pass.varNums = varNums; 339 (*assert)->accept( sub_mangler ); 340 assertionNames.push_back( sub_mangler.pass.mangleName.str() ); 341 } // for 342 } // for 343 mangleName << tcount << "_" << dcount << "_" << fcount << "_" << vcount << "_"; 344 std::copy( assertionNames.begin(), assertionNames.end(), std::ostream_iterator< std::string >( mangleName, "" ) ); 345 mangleName << "_"; 346 } // if 347 if ( type->get_const() ) { 348 mangleName << "C"; 349 } // if 350 if ( type->get_volatile() ) { 351 mangleName << "V"; 352 } // if 353 if ( type->get_mutex() ) { 354 mangleName << "M"; 355 } // if 356 // Removed due to restrict not affecting function compatibility in GCC 357 // if ( type->get_isRestrict() ) { 358 // mangleName << "E"; 359 // } // if 360 if ( type->get_lvalue() ) { 361 // mangle based on whether the type is lvalue, so that the resolver can differentiate lvalues and rvalues 362 mangleName << "L"; 363 } 364 if ( type->get_atomic() ) { 365 mangleName << "A"; 366 } // if 367 } 368 } // namespace 369 } // namespace Mangler 314 370 } // namespace SymTab 315 371 -
src/SymTab/Mangler.h
rad51cc2 rd7d9a60 25 25 26 26 namespace SymTab { 27 /// Mangles names to a unique C identifier 28 class Mangler : public Visitor { 29 public: 27 namespace Mangler { 30 28 /// Mangle syntax tree object; primary interface to clients 31 template< typename SynTreeClass >32 static std::string mangle( SynTreeClass *decl, bool mangleOverridable = true, bool typeMode = false, bool mangleGenericParams = true ); 29 std::string mangle( BaseSyntaxNode * decl, bool mangleOverridable = true, bool typeMode = false, bool mangleGenericParams = true ); 30 33 31 /// Mangle a type name; secondary interface 34 st atic std::string mangleType( Type* ty );32 std::string mangleType( Type* ty ); 35 33 /// Mangle ignoring generic type parameters 36 static std::string mangleConcrete( Type* ty ); 37 38 39 virtual void visit( ObjectDecl *declaration ); 40 virtual void visit( FunctionDecl *declaration ); 41 virtual void visit( TypeDecl *declaration ); 42 43 virtual void visit( VoidType *voidType ); 44 virtual void visit( BasicType *basicType ); 45 virtual void visit( PointerType *pointerType ); 46 virtual void visit( ArrayType *arrayType ); 47 virtual void visit( ReferenceType *refType ); 48 virtual void visit( FunctionType *functionType ); 49 virtual void visit( StructInstType *aggregateUseType ); 50 virtual void visit( UnionInstType *aggregateUseType ); 51 virtual void visit( EnumInstType *aggregateUseType ); 52 virtual void visit( TypeInstType *aggregateUseType ); 53 virtual void visit( TupleType *tupleType ); 54 virtual void visit( VarArgsType *varArgsType ); 55 virtual void visit( ZeroType *zeroType ); 56 virtual void visit( OneType *oneType ); 57 58 std::string get_mangleName() { return mangleName.str(); } 59 private: 60 std::ostringstream mangleName; ///< Mangled name being constructed 61 typedef std::map< std::string, std::pair< int, int > > VarMapType; 62 VarMapType varNums; ///< Map of type variables to indices 63 int nextVarNum; ///< Next type variable index 64 bool isTopLevel; ///< Is the Mangler at the top level 65 bool mangleOverridable; ///< Specially mangle overridable built-in methods 66 bool typeMode; ///< Produce a unique mangled name for a type 67 bool mangleGenericParams; ///< Include generic parameters in name mangling if true 68 69 Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ); 70 Mangler( const Mangler & ); 71 72 void mangleDecl( DeclarationWithType *declaration ); 73 void mangleRef( ReferenceToType *refType, std::string prefix ); 74 75 void printQualifiers( Type *type ); 76 }; // Mangler 77 78 template< typename SynTreeClass > 79 std::string Mangler::mangle( SynTreeClass *decl, bool mangleOverridable, bool typeMode, bool mangleGenericParams ) { 80 Mangler mangler( mangleOverridable, typeMode, mangleGenericParams ); 81 maybeAccept( decl, mangler ); 82 return mangler.get_mangleName(); 83 } 34 std::string mangleConcrete( Type* ty ); 35 } // Mangler 84 36 } // SymTab 85 37
Note: See TracChangeset
for help on using the changeset viewer.