-/// fillTypeNameTable - If the module has a symbol table, take all global types
-/// and stuff their names into the TypeNames map.
-///
-static void fillTypeNameTable(const Module *M,
- std::map<const Type *, std::string> &TypeNames) {
- if (!M) return;
- const TypeSymbolTable &ST = M->getTypeSymbolTable();
- TypeSymbolTable::const_iterator TI = ST.begin();
- for (; TI != ST.end(); ++TI) {
- // As a heuristic, don't insert pointer to primitive types, because
- // they are used too often to have a single useful name.
- //
- const Type *Ty = cast<Type>(TI->second);
- if (!isa<PointerType>(Ty) ||
- !cast<PointerType>(Ty)->getElementType()->isPrimitiveType() ||
- !cast<PointerType>(Ty)->getElementType()->isInteger() ||
- isa<OpaqueType>(cast<PointerType>(Ty)->getElementType()))
- TypeNames.insert(std::make_pair(Ty, '%' + getLLVMName(TI->first)));
- }
-}
-
-
-
-static void calcTypeName(const Type *Ty,
- std::vector<const Type *> &TypeStack,
- std::map<const Type *, std::string> &TypeNames,
- std::string &Result) {
- if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
- Result += Ty->getDescription(); // Base case
- return;
- }
-
- // Check to see if the type is named.
- std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
- if (I != TypeNames.end()) {
- Result += I->second;
- return;
- }
-
- if (isa<OpaqueType>(Ty)) {
- Result += "opaque";
- return;
- }
-
- // Check to see if the Type is already on the stack...
- unsigned Slot = 0, CurSize = TypeStack.size();
- while (Slot < CurSize && TypeStack[Slot] != Ty) ++Slot; // Scan for type
-
- // 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) {
- Result += "\\" + utostr(CurSize-Slot); // Here's the upreference
- return;
- }
-
- TypeStack.push_back(Ty); // Recursive case: Add us to the stack..
-
- switch (Ty->getTypeID()) {
- case Type::IntegerTyID: {
- unsigned BitWidth = cast<IntegerType>(Ty)->getBitWidth();
- Result += "i" + utostr(BitWidth);
- break;
- }
- case Type::FunctionTyID: {
- const FunctionType *FTy = cast<FunctionType>(Ty);
- 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 += ", ";
- calcTypeName(*I, TypeStack, TypeNames, Result);
- }
- if (FTy->isVarArg()) {
- if (FTy->getNumParams()) Result += ", ";
- Result += "...";
- }
- Result += ")";
- break;
- }
- case Type::StructTyID: {
- const StructType *STy = cast<StructType>(Ty);
- if (STy->isPacked())
- Result += '<';
- Result += "{ ";
- for (StructType::element_iterator I = STy->element_begin(),
- E = STy->element_end(); I != E; ++I) {
- calcTypeName(*I, TypeStack, TypeNames, Result);
- if (next(I) != STy->element_end())
- Result += ',';
- Result += ' ';
- }
- Result += '}';
- if (STy->isPacked())
- Result += '>';
- break;
- }
- case Type::PointerTyID: {
- const PointerType *PTy = cast<PointerType>(Ty);
- calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
- if (unsigned AddressSpace = PTy->getAddressSpace())
- Result += " addrspace(" + utostr(AddressSpace) + ")";
- Result += "*";
- break;
- }
- case Type::ArrayTyID: {
- const ArrayType *ATy = cast<ArrayType>(Ty);
- Result += "[" + utostr(ATy->getNumElements()) + " x ";
- calcTypeName(ATy->getElementType(), TypeStack, TypeNames, Result);
- Result += "]";
- break;
- }
- case Type::VectorTyID: {
- const VectorType *PTy = cast<VectorType>(Ty);
- Result += "<" + utostr(PTy->getNumElements()) + " x ";
- calcTypeName(PTy->getElementType(), TypeStack, TypeNames, Result);
- Result += ">";
- break;
- }
- case Type::OpaqueTyID:
- Result += "opaque";
- break;
- default:
- Result += "<unrecognized-type>";
- break;
- }
-
- TypeStack.pop_back(); // Remove self from stack...
-}
-
-
-/// printTypeInt - The internal guts of printing out a type that has a
-/// potentially named portion.
-///
-static void printTypeInt(raw_ostream &Out, const Type *Ty,
- std::map<const Type *, std::string> &TypeNames) {
- // Primitive types always print out their description, regardless of whether
- // they have been named or not.
- //
- if (Ty->isInteger() || (Ty->isPrimitiveType() && !isa<OpaqueType>(Ty))) {
- Out << Ty->getDescription();
- return;
- }
-
- // Check to see if the type is named.
- std::map<const Type *, std::string>::iterator I = TypeNames.find(Ty);
- if (I != TypeNames.end()) {
- Out << I->second;
- return;
- }
-
- // Otherwise we have a type that has not been named but is a derived type.
- // Carefully recurse the type hierarchy to print out any contained symbolic
- // names.
- //
- std::vector<const Type *> TypeStack;
- std::string TypeName;
- calcTypeName(Ty, TypeStack, TypeNames, TypeName);
- TypeNames.insert(std::make_pair(Ty, TypeName));//Cache type name for later use
- Out << TypeName;
-}
-
-
-/// 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 or one of it's component types. This is slower than a simple x << Type
-///
-void llvm::WriteTypeSymbolic(std::ostream &Out, const Type *Ty,
- const Module *M) {
- raw_os_ostream RO(Out);
- WriteTypeSymbolic(RO, Ty, M);
-}
-
-void llvm::WriteTypeSymbolic(raw_ostream &Out, const Type *Ty, const Module *M){
- 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 {
- std::map<const Type *, std::string> TypeNames;
- fillTypeNameTable(M, TypeNames);
- printTypeInt(Out, Ty, TypeNames);
- }
-}
-