From 4ff620a867b7fcd13fb641c4ea872bd9be4b7b71 Mon Sep 17 00:00:00 2001 From: John Criswell Date: Tue, 1 Jun 2004 14:54:08 +0000 Subject: [PATCH] Modified calcTypeName() so that it does not allocate a std::string for every recursive call. This makes it more robust for deeply nested, unnamed types. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@13915 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/AsmWriter.cpp | 61 ++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 651bf591657..4e4dde7d6fc 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -224,18 +224,26 @@ static void fillTypeNameTable(const Module *M, -static std::string calcTypeName(const Type *Ty, - std::vector &TypeStack, - std::map &TypeNames){ - if (Ty->isPrimitiveType() && !isa(Ty)) - return Ty->getDescription(); // Base case +static void calcTypeName(const Type *Ty, + std::vector &TypeStack, + std::map &TypeNames, + std::string & Result){ + if (Ty->isPrimitiveType() && !isa(Ty)) { + Result += Ty->getDescription(); // Base case + return; + } // Check to see if the type is named. std::map::iterator I = TypeNames.find(Ty); - if (I != TypeNames.end()) return I->second; + if (I != TypeNames.end()) { + Result += I->second; + return; + } - if (isa(Ty)) - return "opaque"; + if (isa(Ty)) { + Result += "opaque"; + return; + } // Check to see if the Type is already on the stack... unsigned Slot = 0, CurSize = TypeStack.size(); @@ -244,21 +252,23 @@ static std::string calcTypeName(const Type *Ty, // This is another base case for the recursion. In this case, we know // that we have looped back to a type that we have previously visited. // Generate the appropriate upreference to handle this. - if (Slot < CurSize) - return "\\" + utostr(CurSize-Slot); // Here's the upreference + if (Slot < CurSize) { + Result += "\\" + utostr(CurSize-Slot); // Here's the upreference + return; + } TypeStack.push_back(Ty); // Recursive case: Add us to the stack.. - std::string Result; switch (Ty->getPrimitiveID()) { case Type::FunctionTyID: { const FunctionType *FTy = cast(Ty); - Result = calcTypeName(FTy->getReturnType(), TypeStack, TypeNames) + " ("; + calcTypeName(FTy->getReturnType(), TypeStack, TypeNames, Result); + Result += " ("; for (FunctionType::param_iterator I = FTy->param_begin(), E = FTy->param_end(); I != E; ++I) { if (I != FTy->param_begin()) Result += ", "; - Result += calcTypeName(*I, TypeStack, TypeNames); + calcTypeName(*I, TypeStack, TypeNames, Result); } if (FTy->isVarArg()) { if (FTy->getNumParams()) Result += ", "; @@ -269,35 +279,37 @@ static std::string calcTypeName(const Type *Ty, } case Type::StructTyID: { const StructType *STy = cast(Ty); - Result = "{ "; + Result += "{ "; for (StructType::element_iterator I = STy->element_begin(), E = STy->element_end(); I != E; ++I) { if (I != STy->element_begin()) Result += ", "; - Result += calcTypeName(*I, TypeStack, TypeNames); + calcTypeName(*I, TypeStack, TypeNames, Result); } Result += " }"; break; } case Type::PointerTyID: - Result = calcTypeName(cast(Ty)->getElementType(), - TypeStack, TypeNames) + "*"; + calcTypeName(cast(Ty)->getElementType(), + TypeStack, TypeNames, Result); + Result += "*"; break; case Type::ArrayTyID: { const ArrayType *ATy = cast(Ty); - Result = "[" + utostr(ATy->getNumElements()) + " x "; - Result += calcTypeName(ATy->getElementType(), TypeStack, TypeNames) + "]"; + Result += "[" + utostr(ATy->getNumElements()) + " x "; + calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result); + Result += "]"; break; } case Type::OpaqueTyID: - Result = "opaque"; + Result += "opaque"; break; default: - Result = ""; + Result += ""; } TypeStack.pop_back(); // Remove self from stack... - return Result; + return; } @@ -321,9 +333,10 @@ static std::ostream &printTypeInt(std::ostream &Out, const Type *Ty, // names. // std::vector TypeStack; - std::string TypeName = calcTypeName(Ty, TypeStack, TypeNames); + std::string TypeName; + calcTypeName(Ty, TypeStack, TypeNames, TypeName); TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use - return Out << TypeName; + return (Out << TypeName); } -- 2.34.1