Instead of storing an ASTContext* in FunctionProtoTypes with computed noexcept specifiers, unique FunctionProtoTypes with a ContextualFoldingSet, as suggested by John McCall.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@127568 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index c128ce5..0526ebb 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -195,6 +195,7 @@
                        IdentifierTable &idents, SelectorTable &sels,
                        Builtin::Context &builtins,
                        unsigned size_reserve) :
+  FunctionProtoTypes(this_()),
   TemplateSpecializationTypes(this_()),
   DependentTemplateSpecializationTypes(this_()),
   GlobalNestedNameSpecifier(0), IsInt128Installed(false),
@@ -1909,7 +1910,7 @@
   // Unique functions, to guarantee there is only one function of a particular
   // structure.
   llvm::FoldingSetNodeID ID;
-  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, this);
+  FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, EPI, *this);
 
   void *InsertPos = 0;
   if (FunctionProtoType *FTP =
@@ -1960,13 +1961,12 @@
   if (EPI.ExceptionSpecType == EST_Dynamic)
     Size += EPI.NumExceptions * sizeof(QualType);
   else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
-    Size += sizeof(Expr*) + sizeof(ASTContext*);
+    Size += sizeof(Expr*);
   }
   FunctionProtoType *FTP = (FunctionProtoType*) Allocate(Size, TypeAlignment);
   FunctionProtoType::ExtProtoInfo newEPI = EPI;
   newEPI.ExtInfo = EPI.ExtInfo.withCallingConv(CallConv);
-  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI,
-                              this);
+  new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, Canonical, newEPI);
   Types.push_back(FTP);
   FunctionProtoTypes.InsertNode(FTP, InsertPos);
   return QualType(FTP, 0);
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index 1c7807e..9c44442 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -1629,7 +1629,7 @@
   return R;
 }
 
-static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
+static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
                                            bool NullThrows = true) {
   if (!D)
     return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
@@ -1659,7 +1659,7 @@
   if (!FT)
     return Expr::CT_Can;
 
-  return FT->isNothrow() ? Expr::CT_Cannot : Expr::CT_Can;
+  return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
 }
 
 static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
@@ -1723,7 +1723,7 @@
   case CallExprClass:
   case CXXOperatorCallExprClass:
   case CXXMemberCallExprClass: {
-    CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
+    CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
     if (CT == CT_Can)
       return CT;
     return MergeCanThrow(CT, CanSubExprsThrow(C, this));
@@ -1731,7 +1731,7 @@
 
   case CXXConstructExprClass:
   case CXXTemporaryObjectExprClass: {
-    CanThrowResult CT = CanCalleeThrow(
+    CanThrowResult CT = CanCalleeThrow(C,
         cast<CXXConstructExpr>(this)->getConstructor());
     if (CT == CT_Can)
       return CT;
@@ -1740,8 +1740,8 @@
 
   case CXXNewExprClass: {
     CanThrowResult CT = MergeCanThrow(
-        CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
-        CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
+        CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
+        CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
                        /*NullThrows*/false));
     if (CT == CT_Can)
       return CT;
@@ -1749,7 +1749,7 @@
   }
 
   case CXXDeleteExprClass: {
-    CanThrowResult CT = CanCalleeThrow(
+    CanThrowResult CT = CanCalleeThrow(C,
         cast<CXXDeleteExpr>(this)->getOperatorDelete());
     if (CT == CT_Can)
       return CT;
@@ -1759,7 +1759,7 @@
       Arg = Cast->getSubExpr();
     if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
       if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
-        CanThrowResult CT2 = CanCalleeThrow(
+        CanThrowResult CT2 = CanCalleeThrow(C,
             cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
         if (CT2 == CT_Can)
           return CT2;
@@ -1771,7 +1771,7 @@
 
   case CXXBindTemporaryExprClass: {
     // The bound temporary has to be destroyed again, which might throw.
-    CanThrowResult CT = CanCalleeThrow(
+    CanThrowResult CT = CanCalleeThrow(C,
       cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
     if (CT == CT_Can)
       return CT;
diff --git a/lib/AST/ExprCXX.cpp b/lib/AST/ExprCXX.cpp
index fd94dd1..35c2d41 100644
--- a/lib/AST/ExprCXX.cpp
+++ b/lib/AST/ExprCXX.cpp
@@ -100,9 +100,9 @@
   SubExprs = new (C) Stmt*[TotalSize];
 }
 
-bool CXXNewExpr::shouldNullCheckAllocation() const {
+bool CXXNewExpr::shouldNullCheckAllocation(ASTContext &Ctx) const {
   return getOperatorNew()->getType()->
-    castAs<FunctionProtoType>()->isNothrow();
+    castAs<FunctionProtoType>()->isNothrow(Ctx);
 }
 
 // CXXDeleteExpr
diff --git a/lib/AST/Type.cpp b/lib/AST/Type.cpp
index eb5254e..1e2a6c4 100644
--- a/lib/AST/Type.cpp
+++ b/lib/AST/Type.cpp
@@ -1166,8 +1166,7 @@
 
 FunctionProtoType::FunctionProtoType(QualType result, const QualType *args,
                                      unsigned numArgs, QualType canonical,
-                                     const ExtProtoInfo &epi,
-                                     const ASTContext *context)
+                                     const ExtProtoInfo &epi)
   : FunctionType(FunctionProto, result, epi.Variadic, epi.TypeQuals, 
                  epi.RefQualifier, canonical,
                  result->isDependentType(),
@@ -1205,14 +1204,11 @@
     // Store the noexcept expression and context.
     Expr **noexSlot = reinterpret_cast<Expr**>(argSlot + numArgs);
     *noexSlot = epi.NoexceptExpr;
-    const ASTContext **contextSlot = const_cast<const ASTContext**>(
-        reinterpret_cast<ASTContext**>(noexSlot + 1));
-    *contextSlot = context;
   }
 }
 
 FunctionProtoType::NoexceptResult
-FunctionProtoType::getNoexceptSpec() const {
+FunctionProtoType::getNoexceptSpec(ASTContext &ctx) const {
   ExceptionSpecificationType est = getExceptionSpecType();
   if (est == EST_BasicNoexcept)
     return NR_Nothrow;
@@ -1227,7 +1223,6 @@
     return NR_Dependent;
 
   llvm::APSInt value;
-  ASTContext& ctx = const_cast<ASTContext&>(*getContext());
   bool isICE = noexceptExpr->isIntegerConstantExpr(value, ctx, 0,
                                                    /*evaluated*/false);
   (void)isICE;
@@ -1247,7 +1242,7 @@
 void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
                                 const QualType *ArgTys, unsigned NumArgs,
                                 const ExtProtoInfo &epi,
-                                const ASTContext *Context) {
+                                const ASTContext &Context) {
   ID.AddPointer(Result.getAsOpaquePtr());
   for (unsigned i = 0; i != NumArgs; ++i)
     ID.AddPointer(ArgTys[i].getAsOpaquePtr());
@@ -1259,14 +1254,15 @@
     for (unsigned i = 0; i != epi.NumExceptions; ++i)
       ID.AddPointer(epi.Exceptions[i].getAsOpaquePtr());
   } else if (epi.ExceptionSpecType == EST_ComputedNoexcept && epi.NoexceptExpr){
-    epi.NoexceptExpr->Profile(ID, *Context, true);
+    epi.NoexceptExpr->Profile(ID, Context, true);
   }
   epi.ExtInfo.Profile(ID);
 }
 
-void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID) {
+void FunctionProtoType::Profile(llvm::FoldingSetNodeID &ID,
+                                const ASTContext &Ctx) {
   Profile(ID, getResultType(), arg_type_begin(), NumArgs, getExtProtoInfo(),
-          getContext());
+          Ctx);
 }
 
 QualType TypedefType::desugar() const {
diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp
index 8f8c475..4772a69 100644
--- a/lib/Analysis/CFG.cpp
+++ b/lib/Analysis/CFG.cpp
@@ -1118,7 +1118,7 @@
   return Block;
 }
 
-static bool CanThrow(Expr *E) {
+static bool CanThrow(Expr *E, ASTContext &Ctx) {
   QualType Ty = E->getType();
   if (Ty->isFunctionPointerType())
     Ty = Ty->getAs<PointerType>()->getPointeeType();
@@ -1128,7 +1128,7 @@
   const FunctionType *FT = Ty->getAs<FunctionType>();
   if (FT) {
     if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT))
-      if (Proto->isNothrow())
+      if (Proto->isNothrow(Ctx))
         return false;
   }
   return true;
@@ -1156,7 +1156,7 @@
       AddEHEdge = false;
   }
 
-  if (!CanThrow(C->getCallee()))
+  if (!CanThrow(C->getCallee(), *Context))
     AddEHEdge = false;
 
   if (!NoReturn && !AddEHEdge)
diff --git a/lib/CodeGen/CGCall.cpp b/lib/CodeGen/CGCall.cpp
index d72e6dc..15ab6b0 100644
--- a/lib/CodeGen/CGCall.cpp
+++ b/lib/CodeGen/CGCall.cpp
@@ -709,7 +709,7 @@
       FuncAttrs |= llvm::Attribute::NoUnwind;
     else if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
       const FunctionProtoType *FPT = Fn->getType()->getAs<FunctionProtoType>();
-      if (FPT && FPT->isNothrow())
+      if (FPT && FPT->isNothrow(getContext()))
         FuncAttrs |= llvm::Attribute::NoUnwind;
     }
 
diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp
index c9c8a6a..32d1928 100644
--- a/lib/CodeGen/CGExprCXX.cpp
+++ b/lib/CodeGen/CGExprCXX.cpp
@@ -982,7 +982,7 @@
   // exception spec; for this part, we inline
   // CXXNewExpr::shouldNullCheckAllocation()) and we have an
   // interesting initializer.
-  bool nullCheck = allocatorType->isNothrow() &&
+  bool nullCheck = allocatorType->isNothrow(getContext()) &&
     !(allocType->isPODType() && !E->hasInitializer());
 
   llvm::BasicBlock *nullCheckBB = 0;
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index f655105..04c21bc 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -3091,7 +3091,7 @@
 
       // Check out noexcept specs.
       if (EST == EST_ComputedNoexcept) {
-        FunctionProtoType::NoexceptResult NR = Proto->getNoexceptSpec();
+        FunctionProtoType::NoexceptResult NR = Proto->getNoexceptSpec(Context);
         assert(NR != FunctionProtoType::NR_NoNoexcept &&
                "Must have noexcept result for EST_ComputedNoexcept.");
         assert(NR != FunctionProtoType::NR_Dependent &&
diff --git a/lib/Sema/SemaExceptionSpec.cpp b/lib/Sema/SemaExceptionSpec.cpp
index 01ee712..285f400 100644
--- a/lib/Sema/SemaExceptionSpec.cpp
+++ b/lib/Sema/SemaExceptionSpec.cpp
@@ -313,8 +313,8 @@
   if (OldEST == EST_None && NewEST == EST_None)
     return false;
 
-  FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec();
-  FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec();
+  FunctionProtoType::NoexceptResult OldNR = Old->getNoexceptSpec(Context);
+  FunctionProtoType::NoexceptResult NewNR = New->getNoexceptSpec(Context);
   if (OldNR == FunctionProtoType::NR_BadNoexcept ||
       NewNR == FunctionProtoType::NR_BadNoexcept)
     return false;
@@ -460,7 +460,7 @@
   // omissions we make here.
   // We also shortcut checking if a noexcept expression was bad.
 
-  FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec();
+  FunctionProtoType::NoexceptResult SuperNR =Superset->getNoexceptSpec(Context);
   if (SuperNR == FunctionProtoType::NR_BadNoexcept ||
       SuperNR == FunctionProtoType::NR_Dependent)
     return false;
@@ -479,7 +479,7 @@
     return true;
   }
 
-  FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec();
+  FunctionProtoType::NoexceptResult SubNR = Subset->getNoexceptSpec(Context);
   if (SubNR == FunctionProtoType::NR_BadNoexcept ||
       SubNR == FunctionProtoType::NR_Dependent)
     return false;
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index a1cd43a..ea93449 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -2423,7 +2423,7 @@
             FoundAssign = true;
             const FunctionProtoType *CPT
                 = Operator->getType()->getAs<FunctionProtoType>();
-            if (!CPT->isNothrow()) {
+            if (!CPT->isNothrow(Self.Context)) {
               AllNoThrow = false;
               break;
             }
@@ -2465,7 +2465,7 @@
               = Constructor->getType()->getAs<FunctionProtoType>();
           // FIXME: check whether evaluating default arguments can throw.
           // For now, we'll be conservative and assume that they can throw.
-          if (!CPT->isNothrow() || CPT->getNumArgs() > 1) {
+          if (!CPT->isNothrow(Self.Context) || CPT->getNumArgs() > 1) {
             AllNoThrow = false;
             break;
           }
@@ -2500,7 +2500,7 @@
               = Constructor->getType()->getAs<FunctionProtoType>();
           // TODO: check whether evaluating default arguments can throw.
           // For now, we'll be conservative and assume that they can throw.
-          return CPT->isNothrow() && CPT->getNumArgs() == 0;
+          return CPT->isNothrow(Self.Context) && CPT->getNumArgs() == 0;
         }
       }
     }