make CalcTypeName take a stream instead of a string to concat onto,

eliminate redundant opaque handling code.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@65716 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-02-28 20:49:40 +00:00
parent a32c3e4b1d
commit 534361e7e1

View File

@ -149,8 +149,8 @@ namespace {
void printAtLeastOneLevel(const Type *Ty); void printAtLeastOneLevel(const Type *Ty);
private: private:
void calcTypeName(const Type *Ty, SmallVectorImpl<const Type *> &TypeStack, void CalcTypeName(const Type *Ty, SmallVectorImpl<const Type *> &TypeStack,
std::string &Result); raw_ostream &Result);
}; };
} // end anonymous namespace. } // end anonymous namespace.
@ -181,11 +181,13 @@ TypePrinting::TypePrinting(const Module *M, raw_ostream &os) : OS(os) {
} }
} }
void TypePrinting::calcTypeName(const Type *Ty, /// CalcTypeName - Write the specified type to the specified raw_ostream, making
/// use of type names or up references to shorten the type name where possible.
void TypePrinting::CalcTypeName(const Type *Ty,
SmallVectorImpl<const Type *> &TypeStack, SmallVectorImpl<const Type *> &TypeStack,
std::string &Result) { raw_ostream &Result) {
if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) { if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
Result += Ty->getDescription(); // Base case Result << Ty->getDescription(); // Base case
return; return;
} }
@ -194,12 +196,7 @@ void TypePrinting::calcTypeName(const Type *Ty,
if (I != TypeNames.end() && if (I != TypeNames.end() &&
// If the name wasn't temporarily removed use it. // If the name wasn't temporarily removed use it.
!I->second.empty()) { !I->second.empty()) {
Result += I->second; Result << I->second;
return;
}
if (isa<OpaqueType>(Ty)) {
Result += "opaque";
return; return;
} }
@ -211,7 +208,7 @@ void TypePrinting::calcTypeName(const Type *Ty,
// that we have looped back to a type that we have previously visited. // that we have looped back to a type that we have previously visited.
// Generate the appropriate upreference to handle this. // Generate the appropriate upreference to handle this.
if (Slot < CurSize) { if (Slot < CurSize) {
Result += "\\" + utostr(CurSize-Slot); // Here's the upreference Result << '\\' << unsigned(CurSize-Slot); // Here's the upreference
return; return;
} }
@ -220,69 +217,69 @@ void TypePrinting::calcTypeName(const Type *Ty,
switch (Ty->getTypeID()) { switch (Ty->getTypeID()) {
case Type::FunctionTyID: { case Type::FunctionTyID: {
const FunctionType *FTy = cast<FunctionType>(Ty); const FunctionType *FTy = cast<FunctionType>(Ty);
calcTypeName(FTy->getReturnType(), TypeStack, Result); CalcTypeName(FTy->getReturnType(), TypeStack, Result);
Result += " ("; Result << " (";
for (FunctionType::param_iterator I = FTy->param_begin(), for (FunctionType::param_iterator I = FTy->param_begin(),
E = FTy->param_end(); I != E; ++I) { E = FTy->param_end(); I != E; ++I) {
if (I != FTy->param_begin()) if (I != FTy->param_begin())
Result += ", "; Result << ", ";
calcTypeName(*I, TypeStack, Result); CalcTypeName(*I, TypeStack, Result);
} }
if (FTy->isVarArg()) { if (FTy->isVarArg()) {
if (FTy->getNumParams()) Result += ", "; if (FTy->getNumParams()) Result << ", ";
Result += "..."; Result << "...";
} }
Result += ")"; Result << ')';
break; break;
} }
case Type::StructTyID: { case Type::StructTyID: {
const StructType *STy = cast<StructType>(Ty); const StructType *STy = cast<StructType>(Ty);
if (STy->isPacked()) if (STy->isPacked())
Result += '<'; Result << '<';
Result += "{ "; Result << "{ ";
for (StructType::element_iterator I = STy->element_begin(), for (StructType::element_iterator I = STy->element_begin(),
E = STy->element_end(); I != E; ++I) { E = STy->element_end(); I != E; ++I) {
calcTypeName(*I, TypeStack, Result); CalcTypeName(*I, TypeStack, Result);
if (next(I) != STy->element_end()) if (next(I) != STy->element_end())
Result += ','; Result << ',';
Result += ' '; Result << ' ';
} }
Result += '}'; Result << '}';
if (STy->isPacked()) if (STy->isPacked())
Result += '>'; Result << '>';
break; break;
} }
case Type::PointerTyID: { case Type::PointerTyID: {
const PointerType *PTy = cast<PointerType>(Ty); const PointerType *PTy = cast<PointerType>(Ty);
calcTypeName(PTy->getElementType(), TypeStack, Result); CalcTypeName(PTy->getElementType(), TypeStack, Result);
if (unsigned AddressSpace = PTy->getAddressSpace()) if (unsigned AddressSpace = PTy->getAddressSpace())
Result += " addrspace(" + utostr(AddressSpace) + ")"; Result << " addrspace(" << AddressSpace << ')';
Result += "*"; Result << '*';
break; break;
} }
case Type::ArrayTyID: { case Type::ArrayTyID: {
const ArrayType *ATy = cast<ArrayType>(Ty); const ArrayType *ATy = cast<ArrayType>(Ty);
Result += "[" + utostr(ATy->getNumElements()) + " x "; Result << "[" << ATy->getNumElements() << " x ";
calcTypeName(ATy->getElementType(), TypeStack, Result); CalcTypeName(ATy->getElementType(), TypeStack, Result);
Result += "]"; Result << ']';
break; break;
} }
case Type::VectorTyID: { case Type::VectorTyID: {
const VectorType *PTy = cast<VectorType>(Ty); const VectorType *PTy = cast<VectorType>(Ty);
Result += "<" + utostr(PTy->getNumElements()) + " x "; Result << "<" << PTy->getNumElements() << " x ";
calcTypeName(PTy->getElementType(), TypeStack, Result); CalcTypeName(PTy->getElementType(), TypeStack, Result);
Result += ">"; Result << '>';
break; break;
} }
case Type::OpaqueTyID: case Type::OpaqueTyID:
Result += "opaque"; Result << "opaque";
break; break;
default: default:
Result += "<unrecognized-type>"; Result << "<unrecognized-type>";
break; break;
} }
TypeStack.pop_back(); // Remove self from stack... TypeStack.pop_back(); // Remove self from stack.
} }
/// printTypeInt - The internal guts of printing out a type that has a /// printTypeInt - The internal guts of printing out a type that has a
@ -308,9 +305,15 @@ void TypePrinting::print(const Type *Ty) {
// names. // names.
SmallVector<const Type *, 16> TypeStack; SmallVector<const Type *, 16> TypeStack;
std::string TypeName; std::string TypeName;
calcTypeName(Ty, TypeStack, TypeName);
TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use raw_string_ostream TypeOS(TypeName);
OS << TypeName;
CalcTypeName(Ty, TypeStack, TypeOS);
OS << TypeOS.str();
// Cache type name for later use.
TypeNames.insert(std::make_pair(Ty, TypeOS.str()));
} }
/// printAtLeastOneLevel - Print out one level of the possibly complex type /// printAtLeastOneLevel - Print out one level of the possibly complex type
@ -318,7 +321,7 @@ void TypePrinting::print(const Type *Ty) {
void TypePrinting::printAtLeastOneLevel(const Type *Ty) { void TypePrinting::printAtLeastOneLevel(const Type *Ty) {
// If the type does not have a name, then it is already guaranteed to print at // If the type does not have a name, then it is already guaranteed to print at
// least one level. // least one level.
std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty); std::map<const Type*, std::string>::iterator I = TypeNames.find(Ty);
if (I == TypeNames.end()) if (I == TypeNames.end())
return print(Ty); return print(Ty);
@ -326,18 +329,15 @@ void TypePrinting::printAtLeastOneLevel(const Type *Ty) {
std::string OldName; std::string OldName;
std::swap(OldName, I->second); std::swap(OldName, I->second);
// Print the type without the name.
SmallVector<const Type *, 16> TypeStack; SmallVector<const Type *, 16> TypeStack;
std::string TypeName; CalcTypeName(Ty, TypeStack, OS);
calcTypeName(Ty, TypeStack, TypeName);
OS << TypeName;
// Restore the name. // Restore the name.
std::swap(OldName, I->second); std::swap(OldName, I->second);
} }
/// WriteTypeSymbolic - This attempts to write the specified type as a symbolic /// WriteTypeSymbolic - This attempts to write the specified type as a symbolic
/// type, iff there is an entry in the modules symbol table for the specified /// type, iff there is an entry in the modules symbol table for the specified
/// type or one of it's component types. This is slower than a simple x << Type /// type or one of it's component types. This is slower than a simple x << Type
@ -346,13 +346,7 @@ void llvm::WriteTypeSymbolic(raw_ostream &Out, const Type *Ty, const Module *M){
// FIXME: Remove this space. // FIXME: Remove this space.
Out << ' '; Out << ' ';
// If they want us to print out a type, but there is no context, we can't
// print it symbolically.
if (!M) {
Out << Ty->getDescription();
} else {
TypePrinting(M, Out).print(Ty); TypePrinting(M, Out).print(Ty);
}
} }
// std::ostream adaptor. // std::ostream adaptor.