[cfe-commits] r66128 - in /cfe/trunk: lib/CodeGen/CodeGenTypes.cpp lib/CodeGen/CodeGenTypes.h test/CodeGen/incomplete-function-type.c

Eli Friedman eli.friedman at gmail.com
Wed Mar 4 19:16:42 PST 2009


Author: efriedma
Date: Wed Mar  4 21:16:41 2009
New Revision: 66128

URL: http://llvm.org/viewvc/llvm-project?rev=66128&view=rev
Log:
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.)


Added:
    cfe/trunk/test/CodeGen/incomplete-function-type.c
Modified:
    cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
    cfe/trunk/lib/CodeGen/CodeGenTypes.h

Modified: cfe/trunk/lib/CodeGen/CodeGenTypes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenTypes.cpp?rev=66128&r1=66127&r2=66128&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenTypes.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenTypes.cpp Wed Mar  4 21:16:41 2009
@@ -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:

Modified: cfe/trunk/lib/CodeGen/CodeGenTypes.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenTypes.h?rev=66128&r1=66127&r2=66128&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenTypes.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenTypes.h Wed Mar  4 21:16:41 2009
@@ -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 

Added: cfe/trunk/test/CodeGen/incomplete-function-type.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/incomplete-function-type.c?rev=66128&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/incomplete-function-type.c (added)
+++ cfe/trunk/test/CodeGen/incomplete-function-type.c Wed Mar  4 21:16:41 2009
@@ -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; };
+





More information about the cfe-commits mailing list