*** empty log message ***
[oota-llvm.git] / support / lib / Support / NameMangling.cpp
1 //===- NameMangling.cpp - Name Mangling for LLVM ----------------------------=//
2 //
3 // This file implements a consistent scheme for name mangling symbols.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/Support/NameMangling.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/GlobalValue.h"
10 using std::string;
11
12 // MangleTypeName - Implement a consistent name-mangling scheme for
13 //                  a given type.
14 // 
15 string MangleTypeName(const Type *Ty) {
16   string mangledName;
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)));
32   }
33   
34   return mangledName;
35 }
36
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.
40 // 
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()
46                    : V->getType());
47 }