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 95b78fe..5eb925e 100644
--- a/lib/CodeGen/CodeGenTypes.cpp
+++ b/lib/CodeGen/CodeGenTypes.cpp
@@ -138,6 +138,21 @@
   
 }
 
+// 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<FunctionType>(T);
+  if (const TagType* TT = FT->getResultType()->getAsTagType())
+    if (!TT->getDecl()->isDefinition())
+      return TT;
+  if (const FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(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 @@
 
   // Refine the old opaque type to its new definition.
   cast<llvm::OpaqueType>(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<const Type*, llvm::PATypeHolder>::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<llvm::OpaqueType>(OpaqueHolder.get())->refineAbstractTypeTo(NFT);
+      FunctionTypes.erase(i);
+    }
+  }
 }
 
 static const llvm::Type* getTypeForFormat(const llvm::fltSemantics &format) {
@@ -273,11 +308,25 @@
                                  VT.getNumElements());
   }
   case Type::FunctionNoProto:
-    return GetFunctionType(getFunctionInfo(cast<FunctionNoProtoType>(&Ty)), 
-                           true);
   case Type::FunctionProto: {
-    const FunctionProtoType *FTP = cast<FunctionProtoType>(&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<FunctionProtoType>(&Ty)) {
+      return GetFunctionType(getFunctionInfo(FPT), FPT->isVariadic());
+    } else {
+      const FunctionNoProtoType *FNPT = cast<FunctionNoProtoType>(&Ty);
+      return GetFunctionType(getFunctionInfo(FNPT), true);
+    }
   }
   
   case Type::ExtQual:
diff --git a/lib/CodeGen/CodeGenTypes.h b/lib/CodeGen/CodeGenTypes.h
index 186b443..bdf0ac6 100644
--- a/lib/CodeGen/CodeGenTypes.h
+++ b/lib/CodeGen/CodeGenTypes.h
@@ -89,6 +89,8 @@
 
   llvm::DenseMap<const Type*, llvm::PATypeHolder> TagDeclTypes;
 
+  llvm::DenseMap<const Type*, llvm::PATypeHolder> 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 0000000..6bd872b
--- /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; };
+