2001-10-29 00:44:03 +01:00
|
|
|
//===- NameMangling.cpp - Name Mangling for LLVM ----------------------------=//
|
|
|
|
//
|
|
|
|
// This file implements a consistent scheme for name mangling symbols.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/NameMangling.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/GlobalValue.h"
|
2002-01-20 23:54:45 +01:00
|
|
|
using std::string;
|
2001-10-29 00:44:03 +01:00
|
|
|
|
|
|
|
// MangleTypeName - Implement a consistent name-mangling scheme for
|
|
|
|
// a given type.
|
|
|
|
//
|
|
|
|
string MangleTypeName(const Type *Ty) {
|
|
|
|
string mangledName;
|
|
|
|
if (Ty->isPrimitiveType()) {
|
|
|
|
const string &longName = Ty->getDescription();
|
|
|
|
return string(longName.c_str(), (longName.length() < 2) ? 1 : 2);
|
2002-06-05 19:55:27 +02:00
|
|
|
} else if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) {
|
2001-12-04 01:03:30 +01:00
|
|
|
mangledName = string("P_" + MangleTypeName(PTy->getElementType()));
|
2002-06-05 19:55:27 +02:00
|
|
|
} else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
|
2001-10-29 00:44:03 +01:00
|
|
|
mangledName = string("S_");
|
|
|
|
for (unsigned i=0; i < STy->getNumContainedTypes(); ++i)
|
|
|
|
mangledName += MangleTypeName(STy->getContainedType(i));
|
2002-06-05 19:55:27 +02:00
|
|
|
} else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
|
2001-10-29 00:44:03 +01:00
|
|
|
mangledName = string("A_" +MangleTypeName(ATy->getElementType()));
|
2002-06-05 19:55:27 +02:00
|
|
|
} else if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) {
|
2002-04-05 00:19:18 +02:00
|
|
|
mangledName = string("M_") + MangleTypeName(FTy->getReturnType());
|
|
|
|
for (unsigned i = 1; i < FTy->getNumContainedTypes(); ++i)
|
|
|
|
mangledName += string(MangleTypeName(FTy->getContainedType(i)));
|
2001-10-29 00:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return mangledName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mangleName - implement a consistent name-mangling scheme for all
|
|
|
|
// externally visible (i.e., global) objects.
|
|
|
|
// privateName should be unique within the module.
|
|
|
|
//
|
|
|
|
string MangleName(const string &privateName, const Value *V) {
|
|
|
|
// Lets drop the P_ before every global name since all globals are ptrs
|
|
|
|
return privateName + "_" +
|
|
|
|
MangleTypeName(isa<GlobalValue>(V)
|
2001-12-04 01:03:30 +01:00
|
|
|
? cast<GlobalValue>(V)->getType()->getElementType()
|
2001-10-29 00:44:03 +01:00
|
|
|
: V->getType());
|
|
|
|
}
|