1 //===- NameMangling.cpp - Name Mangling for LLVM ----------------------------=//
3 // This file implements a consistent scheme for name mangling symbols.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Support/NameMangling.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/GlobalValue.h"
12 // MangleTypeName - Implement a consistent name-mangling scheme for
15 string MangleTypeName(const Type *Ty) {
17 if (Ty->isPrimitiveType()) {
18 const string &longName = Ty->getDescription();
19 return string(longName.c_str(), (longName.length() < 2) ? 1 : 2);
20 } else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
21 mangledName = string("P_" + MangleTypeName(PTy->getElementType()));
22 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
23 mangledName = string("S_");
24 for (unsigned i=0; i < STy->getNumContainedTypes(); ++i)
25 mangledName += MangleTypeName(STy->getContainedType(i));
26 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
27 mangledName = string("A_" +MangleTypeName(ATy->getElementType()));
28 } else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
29 mangledName = string("M_") + MangleTypeName(FTy->getReturnType());
30 for (unsigned i = 1; i < FTy->getNumContainedTypes(); ++i)
31 mangledName += string(MangleTypeName(FTy->getContainedType(i)));
37 // mangleName - implement a consistent name-mangling scheme for all
38 // externally visible (i.e., global) objects.
39 // privateName should be unique within the module.
41 string MangleName(const string &privateName, const Value *V) {
42 // Lets drop the P_ before every global name since all globals are ptrs
43 return privateName + "_" +
44 MangleTypeName(isa<GlobalValue>(V)
45 ? cast<GlobalValue>(V)->getType()->getElementType()