From: Eli Friedman Date: Thu, 5 Mar 2009 03:16:41 +0000 (+0000) Subject: Initial implementation of CodeGen for incomplete function types; fixes X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b3b6b9b27ab8bdb2a435a5a92ce62e74e3399377;p=clang Initial implementation of CodeGen for incomplete function types; fixes PR3688. (The FIXME is a rather big performance issue, but it only affects code using this feature, which is relatively rare.) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66128 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp index 95b78fe107..5eb925e5e7 100644 --- a/lib/CodeGen/CodeGenTypes.cpp +++ b/lib/CodeGen/CodeGenTypes.cpp @@ -138,6 +138,21 @@ const llvm::Type *CodeGenTypes::ConvertTypeForMem(QualType T) { } +// Code to verify a given function type is complete, i.e. the return type +// and all of the argument types are complete. +static const TagType *VerifyFuncTypeComplete(const Type* T) { + const FunctionType *FT = cast(T); + if (const TagType* TT = FT->getResultType()->getAsTagType()) + if (!TT->getDecl()->isDefinition()) + return TT; + if (const FunctionProtoType *FPT = dyn_cast(T)) + for (unsigned i = 0; i < FPT->getNumArgs(); i++) + if (const TagType* TT = FPT->getArgType(i)->getAsTagType()) + if (!TT->getDecl()->isDefinition()) + return TT; + return 0; +} + /// UpdateCompletedType - When we find the full definition for a TagDecl, /// replace the 'opaque' type we previously made for it if applicable. void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { @@ -160,6 +175,26 @@ void CodeGenTypes::UpdateCompletedType(const TagDecl *TD) { // Refine the old opaque type to its new definition. cast(OpaqueHolder.get())->refineAbstractTypeTo(NT); + + // Since we just completed a tag type, check to see if any function types + // were completed along with the tag type. + // FIXME: This is very inefficient; if we track which function types depend + // on which tag types, though, it should be reasonably efficient. + llvm::DenseMap::iterator i; + for (i = FunctionTypes.begin(); i != FunctionTypes.end(); ++i) { + if (const TagType* TT = VerifyFuncTypeComplete(i->first)) { + // This function type still depends on an incomplete tag type; make sure + // that tag type has an associated opaque type. + ConvertTagDeclType(TT->getDecl()); + } else { + // This function no longer depends on an incomplete tag type; create the + // function type, and refine the opaque type to the new function type. + llvm::PATypeHolder OpaqueHolder = i->second; + const llvm::Type *NFT = ConvertNewType(QualType(i->first, 0)); + cast(OpaqueHolder.get())->refineAbstractTypeTo(NFT); + FunctionTypes.erase(i); + } + } } static const llvm::Type* getTypeForFormat(const llvm::fltSemantics &format) { @@ -273,11 +308,25 @@ const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) { VT.getNumElements()); } case Type::FunctionNoProto: - return GetFunctionType(getFunctionInfo(cast(&Ty)), - true); case Type::FunctionProto: { - const FunctionProtoType *FTP = cast(&Ty); - return GetFunctionType(getFunctionInfo(FTP), FTP->isVariadic()); + // First, check whether we can build the full function type. + if (const TagType* TT = VerifyFuncTypeComplete(&Ty)) { + // This function's type depends on an incomplete tag type; make sure + // we have an opaque type corresponding to the tag type. + ConvertTagDeclType(TT->getDecl()); + // Create an opaque type for this function type, save it, and return it. + llvm::Type *ResultType = llvm::OpaqueType::get(); + FunctionTypes.insert(std::make_pair(&Ty, ResultType)); + return ResultType; + } + // The function type can be built; call the appropriate routines to + // build it. + if (const FunctionProtoType *FPT = dyn_cast(&Ty)) { + return GetFunctionType(getFunctionInfo(FPT), FPT->isVariadic()); + } else { + const FunctionNoProtoType *FNPT = cast(&Ty); + return GetFunctionType(getFunctionInfo(FNPT), true); + } } case Type::ExtQual: diff --git a/lib/CodeGen/CodeGenTypes.h b/lib/CodeGen/CodeGenTypes.h index 186b44373f..bdf0ac6d02 100644 --- a/lib/CodeGen/CodeGenTypes.h +++ b/lib/CodeGen/CodeGenTypes.h @@ -89,6 +89,8 @@ class CodeGenTypes { llvm::DenseMap TagDeclTypes; + llvm::DenseMap FunctionTypes; + /// CGRecordLayouts - This maps llvm struct type with corresponding /// record layout info. /// FIXME : If CGRecordLayout is less than 16 bytes then use diff --git a/test/CodeGen/incomplete-function-type.c b/test/CodeGen/incomplete-function-type.c new file mode 100644 index 0000000000..6bd872b725 --- /dev/null +++ b/test/CodeGen/incomplete-function-type.c @@ -0,0 +1,10 @@ +// RUN: clang -emit-llvm %s -o - | not grep opaque + +enum teste1 (*test1)(void); +struct tests2 (*test2)(); +struct tests3; +void (*test3)(struct tests3); +enum teste1 { TEST1 }; +struct tests2 { int x,y,z,a,b,c,d,e,f,g; }; +struct tests3 { float x; }; +