Preliminary support for function overloading

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@57909 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index f4266fb..cbbfe31 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -182,11 +182,14 @@
   // C++ 3.9.1p5
   InitBuiltinType(WCharTy,             BuiltinType::WChar);
 
+  // Placeholder type for functions.
+  InitBuiltinType(OverloadTy,         BuiltinType::Overload);
+
   // C99 6.2.5p11.
   FloatComplexTy      = getComplexType(FloatTy);
   DoubleComplexTy     = getComplexType(DoubleTy);
   LongDoubleComplexTy = getComplexType(LongDoubleTy);
-  
+
   BuiltinVaListType = QualType();
   ObjCIdType = QualType();
   IdStructType = 0;
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index 75d6bc6..686aba2 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -31,6 +31,7 @@
 static unsigned nEnumConst = 0;
 static unsigned nEnumDecls = 0;
 static unsigned nNamespaces = 0;
+static unsigned nOverFuncs = 0;
 static unsigned nTypedef = 0;
 static unsigned nFieldDecls = 0;
 static unsigned nCXXFieldDecls = 0;
@@ -63,6 +64,7 @@
   switch (DeclKind) {
   default: assert(0 && "Unknown decl kind!");
   case Namespace:           return "Namespace";
+  case OverloadedFunction:  return "OverloadedFunction";
   case Typedef:             return "Typedef";
   case Function:            return "Function";
   case Var:                 return "Var";
@@ -93,10 +95,13 @@
           int(nFuncs+nVars+nParmVars+nFieldDecls+nSUC+nCXXFieldDecls+nCXXSUC+
               nEnumDecls+nEnumConst+nTypedef+nInterfaceDecls+nClassDecls+
               nMethodDecls+nProtocolDecls+nCategoryDecls+nIvarDecls+
-              nAtDefsFieldDecls+nNamespaces));
+              nAtDefsFieldDecls+nNamespaces+nOverFuncs));
   fprintf(stderr, "    %d namespace decls, %d each (%d bytes)\n", 
           nNamespaces, (int)sizeof(NamespaceDecl), 
           int(nNamespaces*sizeof(NamespaceDecl)));
+  fprintf(stderr, "    %d overloaded function decls, %d each (%d bytes)\n", 
+          nOverFuncs, (int)sizeof(OverloadedFunctionDecl), 
+          int(nOverFuncs*sizeof(OverloadedFunctionDecl)));
   fprintf(stderr, "    %d function decls, %d each (%d bytes)\n", 
           nFuncs, (int)sizeof(FunctionDecl), int(nFuncs*sizeof(FunctionDecl)));
   fprintf(stderr, "    %d variable decls, %d each (%d bytes)\n", 
@@ -192,13 +197,15 @@
               nObjCPropertyImplDecl*sizeof(ObjCPropertyImplDecl)+
               nLinkageSpecDecl*sizeof(LinkageSpecDecl)+
               nFileScopeAsmDecl*sizeof(FileScopeAsmDecl)+
-              nNamespaces*sizeof(NamespaceDecl)));
+              nNamespaces*sizeof(NamespaceDecl)+
+              nOverFuncs*sizeof(OverloadedFunctionDecl)));
     
 }
 
 void Decl::addDeclKind(Kind k) {
   switch (k) {
   case Namespace:           nNamespaces++; break;
+  case OverloadedFunction:  nOverFuncs++; break;
   case Typedef:             nTypedef++; break;
   case Function:            nFuncs++; break;
   case Var:                 nVars++; break;
diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp
index af89da4..9fd7ff9 100644
--- a/lib/AST/DeclCXX.cpp
+++ b/lib/AST/DeclCXX.cpp
@@ -59,3 +59,10 @@
   void *Mem = C.getAllocator().Allocate<CXXClassVarDecl>();
   return new (Mem) CXXClassVarDecl(RD, L, Id, T, PrevDecl);
 }
+
+OverloadedFunctionDecl *
+OverloadedFunctionDecl::Create(ASTContext &C, DeclContext *DC,
+                               IdentifierInfo *Id) {
+  void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
+  return new (Mem) OverloadedFunctionDecl(DC, Id);
+}
diff --git a/lib/AST/DeclSerialization.cpp b/lib/AST/DeclSerialization.cpp
index a0befbd..f2aa3f3 100644
--- a/lib/AST/DeclSerialization.cpp
+++ b/lib/AST/DeclSerialization.cpp
@@ -13,6 +13,7 @@
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/Decl.h"
+#include "clang/AST/DeclCXX.h"
 #include "clang/AST/Expr.h"
 #include "llvm/Bitcode/Serialize.h"
 #include "llvm/Bitcode/Deserialize.h"
@@ -74,7 +75,11 @@
     case Function:
       Dcl = FunctionDecl::CreateImpl(D, C);
       break;
-    
+
+    case OverloadedFunction:
+      Dcl = OverloadedFunctionDecl::CreateImpl(D, C);
+      break;
+
     case Record:
       Dcl = RecordDecl::CreateImpl(D, C);
       break;
@@ -455,6 +460,34 @@
 }
 
 //===----------------------------------------------------------------------===//
+//      OverloadedFunctionDecl Serialization.
+//===----------------------------------------------------------------------===//
+
+void OverloadedFunctionDecl::EmitImpl(Serializer& S) const {
+  NamedDecl::EmitInRec(S);
+
+  S.EmitInt(getNumFunctions());
+  for (unsigned func = 0; func < getNumFunctions(); ++func)
+    S.EmitPtr(Functions[func]);
+}
+
+OverloadedFunctionDecl * 
+OverloadedFunctionDecl::CreateImpl(Deserializer& D, ASTContext& C) {
+  void *Mem = C.getAllocator().Allocate<OverloadedFunctionDecl>();
+  OverloadedFunctionDecl* decl = new (Mem)
+    OverloadedFunctionDecl(0, NULL);
+  
+  decl->NamedDecl::ReadInRec(D, C);
+
+  unsigned numFunctions = D.ReadInt();
+  decl->Functions.reserve(numFunctions);
+  for (unsigned func = 0; func < numFunctions; ++func)
+    D.ReadPtr(decl->Functions[func]);
+  
+  return decl;
+}
+
+//===----------------------------------------------------------------------===//
 //      RecordDecl Serialization.
 //===----------------------------------------------------------------------===//
 
diff --git a/lib/AST/StmtSerialization.cpp b/lib/AST/StmtSerialization.cpp
index baee030..decd9a7 100644
--- a/lib/AST/StmtSerialization.cpp
+++ b/lib/AST/StmtSerialization.cpp
@@ -522,12 +522,12 @@
   SourceLocation Loc = SourceLocation::ReadVal(D);
   QualType T = QualType::ReadVal(D);  
   bool OwnsDecl = D.ReadBool();
-  ValueDecl* decl;
+  NamedDecl* decl;
   
   if (!OwnsDecl)
     D.ReadPtr(decl,false); // No backpatching.
   else
-    decl = cast<ValueDecl>(D.ReadOwnedPtr<Decl>(C));
+    decl = cast<NamedDecl>(D.ReadOwnedPtr<Decl>(C));
   
   return new DeclRefExpr(decl,T,Loc);
 }
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index 74e52fe..5de4476 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -450,6 +450,7 @@
            BT->getKind() <= BuiltinType::LongLong;
   if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
     // Incomplete enum types are not treated as integer types.
+    // FIXME: In C++, enum types are never integer types.
     if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
       return true;
   if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
@@ -466,6 +467,7 @@
   if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
     if (TT->getDecl()->isEnum() && TT->getDecl()->isDefinition())
       return true;  // Complete enum types are integral.
+                    // FIXME: In C++, enum types are never integral.
   if (const ASQualType *ASQT = dyn_cast<ASQualType>(CanonicalType))
     return ASQT->getBaseType()->isIntegralType();
   return false;
@@ -697,6 +699,7 @@
   case Double:            return "double";
   case LongDouble:        return "long double";
   case WChar:             return "wchar_t";
+  case Overload:          return "<overloaded function type>";
   }
 }