After type-system-rewrite branch update the Cpp backend to not use OpaqueType.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135186 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nicolas Geoffray 2011-07-14 21:04:35 +00:00
parent 1134be2428
commit 5cf9fcdad1

View File

@ -101,8 +101,6 @@ namespace {
uint64_t uniqueNum; uint64_t uniqueNum;
TypeMap TypeNames; TypeMap TypeNames;
ValueMap ValueNames; ValueMap ValueNames;
TypeMap UnresolvedTypes;
TypeList TypeStack;
NameSet UsedNames; NameSet UsedNames;
TypeSet DefinedTypes; TypeSet DefinedTypes;
ValueSet DefinedValues; ValueSet DefinedValues;
@ -149,8 +147,7 @@ namespace {
inline void printCppName(const Value* val); inline void printCppName(const Value* val);
void printAttributes(const AttrListPtr &PAL, const std::string &name); void printAttributes(const AttrListPtr &PAL, const std::string &name);
bool printTypeInternal(const Type* Ty); void printType(const Type* Ty);
inline void printType(const Type* Ty);
void printTypes(const Module* M); void printTypes(const Module* M);
void printConstant(const Constant *CPV); void printConstant(const Constant *CPV);
@ -499,65 +496,38 @@ void CppWriter::printAttributes(const AttrListPtr &PAL,
} }
} }
bool CppWriter::printTypeInternal(const Type* Ty) { void CppWriter::printType(const Type* Ty) {
// We don't print definitions for primitive types // We don't print definitions for primitive types
if (Ty->isPrimitiveType() || Ty->isIntegerTy()) if (Ty->isPrimitiveType() || Ty->isIntegerTy())
return false; return;
// If we already defined this type, we don't need to define it again. // If we already defined this type, we don't need to define it again.
if (DefinedTypes.find(Ty) != DefinedTypes.end()) if (DefinedTypes.find(Ty) != DefinedTypes.end())
return false; return;
// Everything below needs the name for the type so get it now. // Everything below needs the name for the type so get it now.
std::string typeName(getCppName(Ty)); std::string typeName(getCppName(Ty));
// Search the type stack for recursion. If we find it, then generate this
// as an OpaqueType, but make sure not to do this multiple times because
// the type could appear in multiple places on the stack. Once the opaque
// definition is issued, it must not be re-issued. Consequently we have to
// check the UnresolvedTypes list as well.
TypeList::const_iterator TI = std::find(TypeStack.begin(), TypeStack.end(),
Ty);
if (TI != TypeStack.end()) {
TypeMap::const_iterator I = UnresolvedTypes.find(Ty);
if (I == UnresolvedTypes.end()) {
Out << "PATypeHolder " << typeName;
Out << "_fwd = OpaqueType::get(mod->getContext());";
nl(Out);
UnresolvedTypes[Ty] = typeName;
}
return true;
}
// We're going to print a derived type which, by definition, contains other
// types. So, push this one we're printing onto the type stack to assist with
// recursive definitions.
TypeStack.push_back(Ty);
// Print the type definition // Print the type definition
switch (Ty->getTypeID()) { switch (Ty->getTypeID()) {
case Type::FunctionTyID: { case Type::FunctionTyID: {
const FunctionType* FT = cast<FunctionType>(Ty); const FunctionType* FT = cast<FunctionType>(Ty);
Out << "std::vector<const Type*>" << typeName << "_args;"; Out << "std::vector<Type*>" << typeName << "_args;";
nl(Out); nl(Out);
FunctionType::param_iterator PI = FT->param_begin(); FunctionType::param_iterator PI = FT->param_begin();
FunctionType::param_iterator PE = FT->param_end(); FunctionType::param_iterator PE = FT->param_end();
for (; PI != PE; ++PI) { for (; PI != PE; ++PI) {
const Type* argTy = static_cast<const Type*>(*PI); const Type* argTy = static_cast<const Type*>(*PI);
bool isForward = printTypeInternal(argTy); printType(argTy);
std::string argName(getCppName(argTy)); std::string argName(getCppName(argTy));
Out << typeName << "_args.push_back(" << argName; Out << typeName << "_args.push_back(" << argName;
if (isForward)
Out << "_fwd";
Out << ");"; Out << ");";
nl(Out); nl(Out);
} }
bool isForward = printTypeInternal(FT->getReturnType()); printType(FT->getReturnType());
std::string retTypeName(getCppName(FT->getReturnType())); std::string retTypeName(getCppName(FT->getReturnType()));
Out << "FunctionType* " << typeName << " = FunctionType::get("; Out << "FunctionType* " << typeName << " = FunctionType::get(";
in(); nl(Out) << "/*Result=*/" << retTypeName; in(); nl(Out) << "/*Result=*/" << retTypeName;
if (isForward)
Out << "_fwd";
Out << ","; Out << ",";
nl(Out) << "/*Params=*/" << typeName << "_args,"; nl(Out) << "/*Params=*/" << typeName << "_args,";
nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");"; nl(Out) << "/*isVarArg=*/" << (FT->isVarArg() ? "true" : "false") << ");";
@ -567,31 +537,36 @@ bool CppWriter::printTypeInternal(const Type* Ty) {
} }
case Type::StructTyID: { case Type::StructTyID: {
const StructType* ST = cast<StructType>(Ty); const StructType* ST = cast<StructType>(Ty);
Out << "std::vector<const Type*>" << typeName << "_fields;"; if (!ST->isAnonymous()) {
Out << "StructType *" << typeName << " = ";
Out << "StructType::createNamed(mod->getContext(), \"";
printEscapedString(ST->getName());
Out << "\");";
nl(Out);
// Indicate that this type is now defined.
DefinedTypes.insert(Ty);
}
Out << "std::vector<Type*>" << typeName << "_fields;";
nl(Out); nl(Out);
StructType::element_iterator EI = ST->element_begin(); StructType::element_iterator EI = ST->element_begin();
StructType::element_iterator EE = ST->element_end(); StructType::element_iterator EE = ST->element_end();
for (; EI != EE; ++EI) { for (; EI != EE; ++EI) {
const Type* fieldTy = static_cast<const Type*>(*EI); const Type* fieldTy = static_cast<const Type*>(*EI);
bool isForward = printTypeInternal(fieldTy); printType(fieldTy);
std::string fieldName(getCppName(fieldTy)); std::string fieldName(getCppName(fieldTy));
Out << typeName << "_fields.push_back(" << fieldName; Out << typeName << "_fields.push_back(" << fieldName;
if (isForward)
Out << "_fwd";
Out << ");"; Out << ");";
nl(Out); nl(Out);
} }
Out << "StructType *" << typeName << " = ";
if (ST->isAnonymous()) { if (ST->isAnonymous()) {
Out << "StructType *" << typeName << " = ";
Out << "StructType::get(" << "mod->getContext(), "; Out << "StructType::get(" << "mod->getContext(), ";
} else { } else {
Out << "StructType::createNamed(mod->getContext(), \"";
printEscapedString(ST->getName());
Out << "\");";
nl(Out);
Out << typeName << "->setBody("; Out << typeName << "->setBody(";
} }
Out << typeName << "_fields, /*isPacked=*/" Out << typeName << "_fields, /*isPacked=*/"
<< (ST->isPacked() ? "true" : "false") << ");"; << (ST->isPacked() ? "true" : "false") << ");";
nl(Out); nl(Out);
@ -600,83 +575,51 @@ bool CppWriter::printTypeInternal(const Type* Ty) {
case Type::ArrayTyID: { case Type::ArrayTyID: {
const ArrayType* AT = cast<ArrayType>(Ty); const ArrayType* AT = cast<ArrayType>(Ty);
const Type* ET = AT->getElementType(); const Type* ET = AT->getElementType();
bool isForward = printTypeInternal(ET); printType(ET);
std::string elemName(getCppName(ET)); if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
Out << "ArrayType* " << typeName << " = ArrayType::get(" std::string elemName(getCppName(ET));
<< elemName << (isForward ? "_fwd" : "") Out << "ArrayType* " << typeName << " = ArrayType::get("
<< ", " << utostr(AT->getNumElements()) << ");"; << elemName
nl(Out); << ", " << utostr(AT->getNumElements()) << ");";
nl(Out);
}
break; break;
} }
case Type::PointerTyID: { case Type::PointerTyID: {
const PointerType* PT = cast<PointerType>(Ty); const PointerType* PT = cast<PointerType>(Ty);
const Type* ET = PT->getElementType(); const Type* ET = PT->getElementType();
bool isForward = printTypeInternal(ET); printType(ET);
std::string elemName(getCppName(ET)); if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
Out << "PointerType* " << typeName << " = PointerType::get(" std::string elemName(getCppName(ET));
<< elemName << (isForward ? "_fwd" : "") Out << "PointerType* " << typeName << " = PointerType::get("
<< ", " << utostr(PT->getAddressSpace()) << ");"; << elemName
nl(Out); << ", " << utostr(PT->getAddressSpace()) << ");";
nl(Out);
}
break; break;
} }
case Type::VectorTyID: { case Type::VectorTyID: {
const VectorType* PT = cast<VectorType>(Ty); const VectorType* PT = cast<VectorType>(Ty);
const Type* ET = PT->getElementType(); const Type* ET = PT->getElementType();
bool isForward = printTypeInternal(ET); printType(ET);
std::string elemName(getCppName(ET)); if (DefinedTypes.find(Ty) == DefinedTypes.end()) {
Out << "VectorType* " << typeName << " = VectorType::get(" std::string elemName(getCppName(ET));
<< elemName << (isForward ? "_fwd" : "") Out << "VectorType* " << typeName << " = VectorType::get("
<< ", " << utostr(PT->getNumElements()) << ");"; << elemName
nl(Out); << ", " << utostr(PT->getNumElements()) << ");";
nl(Out);
}
break; break;
} }
default: default:
error("Invalid TypeID"); error("Invalid TypeID");
} }
// Pop us off the type stack
TypeStack.pop_back();
// Indicate that this type is now defined. // Indicate that this type is now defined.
DefinedTypes.insert(Ty); DefinedTypes.insert(Ty);
// Early resolve as many unresolved types as possible. Search the unresolved
// types map for the type we just printed. Now that its definition is complete
// we can resolve any previous references to it. This prevents a cascade of
// unresolved types.
TypeMap::iterator I = UnresolvedTypes.find(Ty);
if (I != UnresolvedTypes.end()) {
Out << "cast<OpaqueType>(" << I->second
<< "_fwd.get())->refineAbstractTypeTo(" << I->second << ");";
nl(Out);
Out << I->second << " = cast<";
switch (Ty->getTypeID()) {
case Type::FunctionTyID: Out << "FunctionType"; break;
case Type::ArrayTyID: Out << "ArrayType"; break;
case Type::StructTyID: Out << "StructType"; break;
case Type::VectorTyID: Out << "VectorType"; break;
case Type::PointerTyID: Out << "PointerType"; break;
default: Out << "NoSuchDerivedType"; break;
}
Out << ">(" << I->second << "_fwd.get());";
nl(Out); nl(Out);
UnresolvedTypes.erase(I);
}
// Finally, separate the type definition from other with a newline. // Finally, separate the type definition from other with a newline.
nl(Out); nl(Out);
// We weren't a recursive type
return false;
}
// Prints a type definition. Returns true if it could not resolve all the
// types in the definition but had to use a forward reference.
void CppWriter::printType(const Type* Ty) {
assert(TypeStack.empty());
TypeStack.clear();
printTypeInternal(Ty);
assert(TypeStack.empty());
} }
void CppWriter::printTypes(const Module* M) { void CppWriter::printTypes(const Module* M) {