saar.raz updated this revision to Diff 195010.
saar.raz added a comment.
Herald added a reviewer: martong.
Herald added a reviewer: shafik.

Fixed bug in normalization substitution into CSEs
Rebase onto trunk


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D41910/new/

https://reviews.llvm.org/D41910

Files:
  include/clang/AST/DeclTemplate.h
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  lib/AST/ASTImporter.cpp
  lib/AST/DeclTemplate.cpp
  lib/Sema/SemaConcept.cpp
  lib/Sema/SemaTemplate.cpp
  lib/Sema/SemaTemplateDeduction.cpp
  lib/Sema/SemaTemplateInstantiateDecl.cpp
  lib/Serialization/ASTReaderDecl.cpp
  lib/Serialization/ASTWriterDecl.cpp
  test/CXX/concepts-ts/temp/temp.constr/temp.constr.decl/func-template-decl.cpp
  test/CXX/concepts-ts/temp/temp.constr/temp.constr.normal/p1.cpp
  
test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/class-template-partial-specializations.cpp
  test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/function-templates.cpp
  
test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/var-template-partial-specializations.cpp

Index: test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/var-template-partial-specializations.cpp
===================================================================
--- /dev/null
+++ test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/var-template-partial-specializations.cpp
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -std=c++2a -fconcepts-ts -x c++ -verify %s
+
+template<typename T> requires sizeof(T) >= 4
+bool a = false; // expected-note{{template is declared here}}
+
+template<typename T> requires sizeof(T) >= 4 && sizeof(T) <= 10
+bool a<T> = true; // expected-error{{variable template partial specialization is not more specialized than the primary template}}
+
+template<typename T>
+concept C1 = sizeof(T) >= 4;
+
+template<typename T> requires C1<T>
+bool b = false;
+
+template<typename T> requires C1<T> && sizeof(T) <= 10
+bool b<T> = true;
+
+template<typename T>
+concept C2 = sizeof(T) > 1 && sizeof(T) <= 8;
+
+template<typename T>
+bool c = false;
+
+template<typename T> requires C1<T>
+bool c<T> = true;
+
+template<typename T>
+bool d = false;
+
+template<typename T>
+bool d<T> = true; // expected-error{{variable template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list}}
+
+template<typename T> requires C1<T>
+bool e = false;
+
+template<typename T>
+bool e<T> = true; // expected-error{{variable template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list}}
+
+template<typename T>
+constexpr int f = 1;
+
+template<typename T> requires C1<T> && C2<T>
+constexpr int f<T> = 2;
+
+template<typename T> requires C1<T> || C2<T>
+constexpr int f<T> = 3;
+
+static_assert(f<unsigned> == 2);
+static_assert(f<char[10]> == 3);
+static_assert(f<char> == 1);
+
+
+
Index: test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/function-templates.cpp
===================================================================
--- /dev/null
+++ test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/function-templates.cpp
@@ -0,0 +1,82 @@
+// RUN: %clang_cc1 -std=c++2a -fconcepts-ts -x c++ -verify %s
+
+template<typename T> requires sizeof(T) >= 4
+bool a() { return false; } // expected-note {{candidate function [with T = unsigned int]}}
+
+template<typename T> requires sizeof(T) >= 4 && sizeof(T) <= 10
+bool a() { return true; } // expected-note {{candidate function [with T = unsigned int]}}
+
+bool av = a<unsigned>(); // expected-error {{call to 'a' is ambiguous}}
+
+template<typename T>
+concept C1 = sizeof(T) >= 4;
+
+template<typename T> requires C1<T>
+constexpr bool b() { return false; }
+
+template<typename T> requires C1<T> && sizeof(T) <= 10
+constexpr bool b() { return true; }
+
+static_assert(b<int>());
+static_assert(!b<int[10]>());
+
+template<typename T>
+concept C2 = sizeof(T) > 1 && sizeof(T) <= 8;
+
+template<typename T>
+bool c() { return false; }
+
+template<typename T> requires C1<T>
+bool c() { return true; }
+
+template<typename T> requires C1<T>
+constexpr bool d() { return false; }
+
+template<typename T>
+constexpr bool d() { return true; }
+
+static_assert(!d<int>());
+
+template<typename T>
+constexpr int e() { return 1; }
+
+template<typename T> requires C1<T> && C2<T>
+constexpr int e() { return 2; }
+
+template<typename T> requires C1<T> || C2<T>
+constexpr int e() { return 3; }
+
+static_assert(e<unsigned>() == 2);
+static_assert(e<char[10]>() == 3);
+static_assert(e<char>() == 1);
+
+template<class T, class U>
+concept BiggerThan = sizeof(T) > sizeof(U);
+
+template<class T>
+concept BiggerThanInt = BiggerThan<T, int>;
+
+template<class T, class U> requires BiggerThan<T, U>
+void f() { }
+// expected-note@-1 {{candidate function [with T = long long, U = int]}}
+
+template<class T, class U> requires BiggerThanInt<T>
+void f() { }
+// expected-note@-1 {{candidate function [with T = long long, U = int]}}
+
+static_assert(sizeof(f<long long, int>()));
+// expected-error@-1 {{call to 'f' is ambiguous}}
+
+template<typename T>
+concept C3 = true;
+
+template<typename T>
+concept C4 = true && C3<T>;
+
+template<typename T> requires C3<void>
+int g() { }
+
+template<typename T> requires C4<void>
+int g() { }
+
+static_assert(sizeof(g<int>()));
\ No newline at end of file
Index: test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/class-template-partial-specializations.cpp
===================================================================
--- /dev/null
+++ test/CXX/concepts-ts/temp/temp.constr/temp.constr.order/class-template-partial-specializations.cpp
@@ -0,0 +1,84 @@
+// RUN: %clang_cc1 -std=c++2a -fconcepts-ts -x c++ -verify %s
+
+template<typename T> requires sizeof(T) >= 4
+class A{}; // expected-note{{template is declared here}}
+
+template<typename T> requires sizeof(T) >= 4 && sizeof(T) <= 10
+class A<T>{}; // expected-error{{class template partial specialization is not more specialized than the primary template}}
+
+template<typename T>
+concept C1 = sizeof(T) >= 4;
+
+template<typename T> requires C1<T>
+class B{};
+
+template<typename T> requires C1<T> && sizeof(T) <= 10
+class B<T>{};
+
+template<typename T>
+concept C2 = sizeof(T) > 1 && sizeof(T) <= 8;
+
+template<typename T>
+class C{};
+
+template<typename T> requires C1<T>
+class C<T>{};
+
+template<typename T>
+class D{}; // expected-note{{previous definition is here}}
+
+template<typename T>
+class D<T>{}; // expected-error{{class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list}} expected-error{{redefinition of 'D'}}
+
+template<typename T> requires C1<T> // expected-note{{previous template declaration is here}}
+class E{};
+
+template<typename T> // expected-error{{associated constraints differ in template redeclaration}}
+class E<T>{}; // expected-error{{class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list}}
+
+template<typename T>
+struct F{ enum{ value = 1 }; };
+
+template<typename T> requires C1<T> && C2<T>
+struct F<T>{ enum{ value = 2 }; };
+
+template<typename T> requires C1<T> || C2<T>
+struct F<T>{ enum{ value = 3 }; };
+
+static_assert(F<unsigned>::value == 2);
+static_assert(F<char[10]>::value == 3);
+static_assert(F<char>::value == 1);
+
+template<typename T1, typename T2>
+concept C3 = true;
+
+template<typename... Ts>
+concept C4 = C3<Ts...>;
+
+// C4 is normalized to C3<Ts...>@<C4's definition> because there is a pack
+// expansion into a non-pack parameter. therefore the two C4<T...> subsume each
+// other and the following is non-ambiguous.
+
+template<typename... T> requires C4<T...>
+struct G { };
+
+template<typename... T> requires C4<T...> && C4<int, short>
+struct G<T...> { };
+
+// Here the two C3s cannot be normalized further, and do not subsume each other
+// because they originate in two different locations in code.
+
+template<typename... T> requires C3<T...>
+struct H { }; // expected-note {{template is declared here}}
+
+template<typename... T> requires C3<T...> && C4<int, short>
+struct H<T...> { }; // expected-error {{class template partial specialization is not more specialized than the primary template}}
+
+// Make sure atomic constraints subsume each other only if their parameter
+// mappings are identical.
+
+template<typename T, typename U> requires C2<T>
+struct I { }; // expected-note {{template is declared here}}
+
+template<typename T, typename U> requires C2<U>
+struct I<T, U> { }; // expected-error {{class template partial specialization is not more specialized than the primary template}}
\ No newline at end of file
Index: test/CXX/concepts-ts/temp/temp.constr/temp.constr.normal/p1.cpp
===================================================================
--- /dev/null
+++ test/CXX/concepts-ts/temp/temp.constr/temp.constr.normal/p1.cpp
@@ -0,0 +1,68 @@
+// RUN: %clang_cc1 -std=c++2a -fconcepts-ts -x c++ -verify %s
+
+namespace maybe_incorrect_args {
+  template<typename U, typename T>
+  concept C = true;
+
+  // diagnostic expected here - the C<Ts...>s are treated as atomic, and since
+  // they originate from different source-level constructs, they do not subsume
+  // each other.
+  template<typename... Ts> requires C<Ts...>
+  struct A {}; // expected-note{{template is declared here}}
+
+  template<typename... Ts> requires C<Ts...> && true
+  struct A<Ts...> {}; // expected-error{{class template partial specialization is not more specialized than the primary template}}
+}
+
+namespace ill_formed_subst {
+
+  template<typename T>
+  struct B; // expected-note 2{{template is declared here}}
+
+  template<typename T>
+  concept C1 = true;
+
+  template<typename T, typename U>
+  concept C2 = C1<typename B<U>::foo>;
+  // expected-error@-1 2{{implicit instantiation of undefined template 'ill_formed_subst::B<int>'}}
+  // expected-note@-2 2{{when substituting into C1<typename B<U>::foo> [with T = type-parameter-0-0, U = int]. Make sure concept arguments are valid for any substitution}}
+
+  template<typename T> requires C2<T, int>
+  struct A {}; // expected-note {{template is declared here}}
+
+  template<typename T> requires C2<T, int> && true
+  struct A<T> {}; // expected-error {{class template partial specialization is not more specialized than the primary template}}
+}
+
+namespace incorrect_args_after_subst {
+  template<typename T>
+  concept C1 = true; // expected-note 2{{template is declared here}}
+
+  template<typename... Ts>
+  concept C2 = C1<Ts...>;
+  // expected-error@-1 2{{too many template arguments for concept 'C1'}}
+  // expected-note@-2 2{{when substituting into C1<Ts...> [with Ts = <type-parameter-0-0, type-parameter-0-0>]. Make sure concept arguments are valid for any substitution}}
+
+  template<typename T> requires C2<T, T>
+  struct A {}; // expected-note{{template is declared here}}
+
+  template<typename T> requires C2<T, T> && true
+  struct A<T> {}; // expected-error{{class template partial specialization is not more specialized than the primary template}}
+}
+
+namespace maybe_incorrect_args_after_subst {
+  template<typename T, typename U>
+  concept C1 = true;
+
+  template<typename... Us>
+  concept C2 = C1<Us...>;
+
+  // no diagnostic expected here - C1<Us...> is treated as atomic, and since it
+  // originates at the same source level construct, the specialized subsumes the
+  // primary.
+  template<typename... Ts> requires C2<Ts...>
+  struct A {};
+
+  template<typename... Ts> requires C2<Ts...> && true
+  struct A<Ts...> {};
+}
Index: test/CXX/concepts-ts/temp/temp.constr/temp.constr.decl/func-template-decl.cpp
===================================================================
--- test/CXX/concepts-ts/temp/temp.constr/temp.constr.decl/func-template-decl.cpp
+++ test/CXX/concepts-ts/temp/temp.constr/temp.constr.decl/func-template-decl.cpp
@@ -9,23 +9,6 @@
 
 } // end namespace nodiag
 
-namespace diag {
-
-template <typename T> requires true // expected-note{{previous template declaration is here}}
-int A();
-template <typename T> int A(); // expected-error{{associated constraints differ in template redeclaration}}
-
-template <typename T> int B(); // expected-note{{previous template declaration is here}}
-template <typename T> requires true // expected-error{{associated constraints differ in template redeclaration}}
-int B();
-
-template <typename T> requires true // expected-note{{previous template declaration is here}}
-int C();
-template <typename T> requires !0 // expected-error{{associated constraints differ in template redeclaration}}
-int C();
-
-} // end namespace diag
-
 namespace nodiag {
 
 struct AA {
@@ -42,11 +25,11 @@
 
 template <unsigned N>
 struct TA {
-  template <template <unsigned> class TT> requires TT<N>::happy // expected-note{{previous template declaration is here}}
+  template <template <unsigned> class TT> requires TT<N>::happy
   int A();
 };
 
 template <unsigned N>
-template <template <unsigned> class TT> int TA<N>::A() { return sizeof(TT<N>); } // expected-error{{associated constraints differ in template redeclaration}}
+template <template <unsigned> class TT> int TA<N>::A() { return sizeof(TT<N>); } // expected-error{{out-of-line definition of 'A' does not match any declaration in 'TA<N>'}}
 
 } // end namespace diag
Index: lib/Serialization/ASTWriterDecl.cpp
===================================================================
--- lib/Serialization/ASTWriterDecl.cpp
+++ lib/Serialization/ASTWriterDecl.cpp
@@ -1484,11 +1484,11 @@
 
 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
                                     ClassTemplatePartialSpecializationDecl *D) {
-  VisitClassTemplateSpecializationDecl(D);
-
   Record.AddTemplateParameterList(D->getTemplateParameters());
   Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
 
+  VisitClassTemplateSpecializationDecl(D);
+
   // These are read/set from/to the first declaration.
   if (D->getPreviousDecl() == nullptr) {
     Record.AddDeclRef(D->getInstantiatedFromMember());
@@ -1544,11 +1544,11 @@
 
 void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
     VarTemplatePartialSpecializationDecl *D) {
-  VisitVarTemplateSpecializationDecl(D);
-
   Record.AddTemplateParameterList(D->getTemplateParameters());
   Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
 
+  VisitVarTemplateSpecializationDecl(D);
+
   // These are read/set from/to the first declaration.
   if (D->getPreviousDecl() == nullptr) {
     Record.AddDeclRef(D->getInstantiatedFromMember());
Index: lib/Serialization/ASTReaderDecl.cpp
===================================================================
--- lib/Serialization/ASTReaderDecl.cpp
+++ lib/Serialization/ASTReaderDecl.cpp
@@ -2232,12 +2232,14 @@
 
 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
                                     ClassTemplatePartialSpecializationDecl *D) {
-  RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
-
+  // We need to read the template params first because redeclarable is going to
+  // need them for profiling
   TemplateParameterList *Params = Record.readTemplateParameterList();
   D->TemplateParams = Params;
   D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
 
+  RedeclarableResult Redecl = VisitClassTemplateSpecializationDeclImpl(D);
+
   // These are read/set from/to the first declaration.
   if (ThisDeclID == Redecl.getFirstID()) {
     D->InstantiatedFromMember.setPointer(
@@ -2333,12 +2335,12 @@
 ///        using Template(Partial)SpecializationDecl as input type.
 void ASTDeclReader::VisitVarTemplatePartialSpecializationDecl(
     VarTemplatePartialSpecializationDecl *D) {
-  RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
-
   TemplateParameterList *Params = Record.readTemplateParameterList();
   D->TemplateParams = Params;
   D->ArgsAsWritten = Record.readASTTemplateArgumentListInfo();
 
+  RedeclarableResult Redecl = VisitVarTemplateSpecializationDeclImpl(D);
+
   // These are read/set from/to the first declaration.
   if (ThisDeclID == Redecl.getFirstID()) {
     D->InstantiatedFromMember.setPointer(
Index: lib/Sema/SemaTemplateInstantiateDecl.cpp
===================================================================
--- lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -3503,7 +3503,8 @@
   // in the member template's set of class template partial specializations.
   void *InsertPos = nullptr;
   ClassTemplateSpecializationDecl *PrevDecl
-    = ClassTemplate->findPartialSpecialization(Converted, InsertPos);
+    = ClassTemplate->findPartialSpecialization(Converted,
+          InstParams->getAssociatedConstraints(), InsertPos);
 
   // Build the canonical type that describes the converted template
   // arguments of the class template partial specialization.
@@ -3627,7 +3628,8 @@
   // in the member template's set of variable template partial specializations.
   void *InsertPos = nullptr;
   VarTemplateSpecializationDecl *PrevDecl =
-      VarTemplate->findPartialSpecialization(Converted, InsertPos);
+      VarTemplate->findPartialSpecialization(Converted,
+          InstParams->getAssociatedConstraints(), InsertPos);
 
   // Build the canonical type that describes the converted template
   // arguments of the variable template partial specialization.
Index: lib/Sema/SemaTemplateDeduction.cpp
===================================================================
--- lib/Sema/SemaTemplateDeduction.cpp
+++ lib/Sema/SemaTemplateDeduction.cpp
@@ -2709,6 +2709,22 @@
   static constexpr bool value = true;
 };
 
+template<typename TemplateDeclT>
+static Sema::TemplateDeductionResult
+CheckDeducedArgumentConstraints(Sema& S, TemplateDeclT *Template,
+                                ArrayRef<TemplateArgument> DeducedArgs,
+                                TemplateDeductionInfo& Info) {
+  if (S.CheckConstraintSatisfaction(Template,
+                                    Template->getAssociatedConstraints(),
+                                    DeducedArgs, Info.getLocation(),
+                                    Info.AssociatedConstraintsSatisfaction)
+      || !Info.AssociatedConstraintsSatisfaction.IsSatisfied) {
+    Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs));
+    return Sema::TDK_ConstraintsNotSatisfied;
+  }
+  return Sema::TDK_Success;
+}
+
 /// Complete template argument deduction for a partial specialization.
 template <typename T>
 static typename std::enable_if<IsPartialSpecialization<T>::value,
@@ -2786,6 +2802,9 @@
   if (Trap.hasErrorOccurred())
     return Sema::TDK_SubstitutionFailure;
 
+  if (auto Result = CheckDeducedArgumentConstraints(S, Partial, Builder, Info))
+    return Result;
+
   return Sema::TDK_Success;
 }
 
@@ -2828,22 +2847,10 @@
   if (Trap.hasErrorOccurred())
     return Sema::TDK_SubstitutionFailure;
 
-  return Sema::TDK_Success;
-}
+  if (auto Result = CheckDeducedArgumentConstraints(S, Template, Builder,
+                                                    Info))
+    return Result;
 
-template<typename TemplateDeclT>
-static Sema::TemplateDeductionResult
-CheckDeducedArgumentConstraints(Sema& S, TemplateDeclT *Template,
-                                ArrayRef<TemplateArgument> DeducedArgs,
-                                TemplateDeductionInfo& Info) {
-  if (S.CheckConstraintSatisfaction(Template,
-                                    Template->getAssociatedConstraints(),
-                                    DeducedArgs, Info.getLocation(),
-                                    Info.AssociatedConstraintsSatisfaction)
-      || !Info.AssociatedConstraintsSatisfaction.IsSatisfied) {
-    Info.reset(TemplateArgumentList::CreateCopy(S.Context, DeducedArgs));
-    return Sema::TDK_ConstraintsNotSatisfied;
-  }
   return Sema::TDK_Success;
 }
 
@@ -2886,10 +2893,6 @@
   if (Trap.hasErrorOccurred())
     return Sema::TDK_SubstitutionFailure;
 
-  if (TemplateDeductionResult Result
-        = CheckDeducedArgumentConstraints(*this, Partial, DeducedArgs, Info))
-    return Result;
-
   return ::FinishTemplateArgumentDeduction(
       *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info);
 }
@@ -2931,10 +2934,6 @@
   if (Trap.hasErrorOccurred())
     return Sema::TDK_SubstitutionFailure;
 
-  if (TemplateDeductionResult Result
-        = CheckDeducedArgumentConstraints(*this, Partial, DeducedArgs, Info))
-    return Result;
-
   return ::FinishTemplateArgumentDeduction(
       *this, Partial, /*PartialOrdering=*/false, TemplateArgs, Deduced, Info);
 }
@@ -3044,12 +3043,6 @@
     = TemplateArgumentList::CreateCopy(Context, Builder);
   Info.setExplicitArgs(ExplicitArgumentList);
 
-  if (TemplateDeductionResult Result
-        = CheckDeducedArgumentConstraints(*this, FunctionTemplate,
-                                          ExplicitArgumentList->asArray(),
-                                          Info))
-    return Result;
-
   // Template argument deduction and the final substitution should be
   // done in the context of the templated declaration.  Explicit
   // argument substitution, on the other hand, needs to happen in the
@@ -3363,7 +3356,7 @@
     return Result;
 
   if (TemplateDeductionResult Result
-        = CheckDeducedArgumentConstraints(*this, FunctionTemplate, DeducedArgs,
+        = CheckDeducedArgumentConstraints(*this, FunctionTemplate, Builder,
                                           Info))
     return Result;
 
@@ -4899,6 +4892,21 @@
                                  TemplatePartialOrderingContext TPOC,
                                  unsigned NumCallArguments1,
                                  unsigned NumCallArguments2) {
+
+  auto JudgeByConstraints = [&] () -> FunctionTemplateDecl * {
+    bool MoreConstrained1 = IsMoreConstrained(FT1,
+                                              FT1->getAssociatedConstraints(),
+                                              FT2,
+                                              FT2->getAssociatedConstraints());
+    bool MoreConstrained2 = IsMoreConstrained(FT2,
+                                              FT2->getAssociatedConstraints(),
+                                              FT1,
+                                              FT1->getAssociatedConstraints());
+    if (MoreConstrained1 == MoreConstrained2)
+      return nullptr;
+    return MoreConstrained1 ? FT1 : FT2;
+  };
+
   bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
                                           NumCallArguments1);
   bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
@@ -4908,7 +4916,7 @@
     return Better1 ? FT1 : FT2;
 
   if (!Better1 && !Better2) // Neither is better than the other
-    return nullptr;
+    return JudgeByConstraints();
 
   // FIXME: This mimics what GCC implements, but doesn't match up with the
   // proposed resolution for core issue 692. This area needs to be sorted out,
@@ -4918,7 +4926,7 @@
   if (Variadic1 != Variadic2)
     return Variadic1? FT2 : FT1;
 
-  return nullptr;
+  return JudgeByConstraints();
 }
 
 /// Determine if the two templates are equivalent.
@@ -5043,7 +5051,6 @@
 static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2,
                                      TemplateLikeDecl *P2,
                                      TemplateDeductionInfo &Info) {
-  // TODO: Concepts: Regard constraints
   // C++ [temp.class.order]p1:
   //   For two class template partial specializations, the first is at least as
   //   specialized as the second if, given the following rewrite to two
@@ -5114,8 +5121,19 @@
   bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info);
   bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info);
 
-  if (Better1 == Better2)
-    return nullptr;
+  if (Better1 == Better2) {
+    bool MoreConstrained1 = IsMoreConstrained(PS1,
+                                              PS1->getAssociatedConstraints(),
+                                              PS2,
+                                              PS2->getAssociatedConstraints());
+    bool MoreConstrained2 = IsMoreConstrained(PS2,
+                                              PS2->getAssociatedConstraints(),
+                                              PS1,
+                                              PS1->getAssociatedConstraints());
+    if (MoreConstrained1 == MoreConstrained2)
+      return nullptr;
+    return MoreConstrained1 ? PS1 : PS2;
+  }
 
   return Better1 ? PS1 : PS2;
 }
@@ -5125,11 +5143,24 @@
   ClassTemplateDecl *Primary = Spec->getSpecializedTemplate();
   QualType PrimaryT = Primary->getInjectedClassNameSpecialization();
   QualType PartialT = Spec->getInjectedSpecializationType();
+  auto JudgeByConstraints = [&] {
+    bool MoreConstrainedPrimary = IsMoreConstrained(Primary,
+                                            Primary->getAssociatedConstraints(),
+                                            Spec,
+                                            Spec->getAssociatedConstraints());
+    bool MoreConstrainedSpec = IsMoreConstrained(Spec,
+                                           Spec->getAssociatedConstraints(),
+                                           Primary,
+                                           Primary->getAssociatedConstraints());
+    if (MoreConstrainedPrimary == MoreConstrainedSpec)
+      return false;
+    return MoreConstrainedSpec;
+  };
   if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info))
-    return false;
-  if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) {
+    return JudgeByConstraints();
+  if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)){
     Info.clearSFINAEDiagnostic();
-    return false;
+    return JudgeByConstraints();
   }
   return true;
 }
@@ -5154,8 +5185,20 @@
   bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info);
   bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info);
 
-  if (Better1 == Better2)
-    return nullptr;
+  if (Better1 == Better2) {
+    bool MoreConstrained1 = IsMoreConstrained(PS1,
+                                              PS1->getAssociatedConstraints(),
+                                              PS2,
+                                              PS2->getAssociatedConstraints());
+    bool MoreConstrained2 = IsMoreConstrained(PS2,
+                                              PS2->getAssociatedConstraints(),
+                                              PS1,
+                                              PS1->getAssociatedConstraints());
+    if (MoreConstrained1 == MoreConstrained2) {
+      return nullptr;
+    }
+    return MoreConstrained1 ? PS1 : PS2;
+  }
 
   return Better1 ? PS1 : PS2;
 }
@@ -5175,11 +5218,26 @@
       CanonTemplate, PrimaryArgs);
   QualType PartialT = Context.getTemplateSpecializationType(
       CanonTemplate, Spec->getTemplateArgs().asArray());
+
+  auto JudgeByConstraints = [&] {
+      bool MoreConstrainedPrimary = IsMoreConstrained(Primary,
+                                            Primary->getAssociatedConstraints(),
+                                            Spec,
+                                            Spec->getAssociatedConstraints());
+      bool MoreConstrainedSpec = IsMoreConstrained(Spec,
+                                           Spec->getAssociatedConstraints(),
+                                           Primary,
+                                           Primary->getAssociatedConstraints());
+      if (MoreConstrainedPrimary == MoreConstrainedSpec)
+        return false;
+      return MoreConstrainedSpec;
+  };
+
   if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info))
-    return false;
-  if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) {
+    return JudgeByConstraints();
+  if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)){
     Info.clearSFINAEDiagnostic();
-    return false;
+    return JudgeByConstraints();
   }
   return true;
 }
Index: lib/Sema/SemaTemplate.cpp
===================================================================
--- lib/Sema/SemaTemplate.cpp
+++ lib/Sema/SemaTemplate.cpp
@@ -1049,11 +1049,11 @@
   // Check that we have valid decl-specifiers specified.
   auto CheckValidDeclSpecifiers = [this, &D] {
     // C++ [temp.param]
-    // p1
+    // p1 
     //   template-parameter:
     //     ...
     //     parameter-declaration
-    // p2
+    // p2 
     //   ... A storage class shall not be specified in a template-parameter
     //   declaration.
     // [dcl.typedef]p1:
@@ -3778,7 +3778,9 @@
     }
 
     if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(),
-                                Converted)) {
+                                Converted)
+        && (!Context.getLangOpts().ConceptsTS
+            || !TemplateParams->hasAssociatedConstraints())) {
       // C++ [temp.class.spec]p9b3:
       //
       //   -- The argument list of the specialization shall not be identical
@@ -3798,7 +3800,8 @@
 
   if (IsPartialSpecialization)
     // FIXME: Template parameter list matters too
-    PrevDecl = VarTemplate->findPartialSpecialization(Converted, InsertPos);
+    PrevDecl = VarTemplate->findPartialSpecialization(Converted,
+                   TemplateParams->getAssociatedConstraints(), InsertPos);
   else
     PrevDecl = VarTemplate->findSpecialization(Converted, InsertPos);
 
@@ -6990,6 +6993,7 @@
                                        bool Complain,
                                      Sema::TemplateParameterListEqualKind Kind,
                                        SourceLocation TemplateArgLoc) {
+  // TODO: Concepts: Check constrained-parameter constraints here.
   // Check the actual kind (type, non-type, template).
   if (Old->getKind() != New->getKind()) {
     if (Complain) {
@@ -7199,6 +7203,13 @@
     return false;
   }
 
+  if (!CheckRedeclarationConstraintMatch(Old->getAssociatedConstraints(),
+                                         New->getAssociatedConstraints())) {
+    if (Complain)
+      DiagnoseRedeclarationConstraintMismatch(Old, New);
+    return false;
+  }
+
   return true;
 }
 
@@ -7666,7 +7677,8 @@
 
   if (isPartialSpecialization)
     // FIXME: Template parameter list matters, too
-    PrevDecl = ClassTemplate->findPartialSpecialization(Converted, InsertPos);
+    PrevDecl = ClassTemplate->findPartialSpecialization(Converted,
+        TemplateParams->getAssociatedConstraints(), InsertPos);
   else
     PrevDecl = ClassTemplate->findSpecialization(Converted, InsertPos);
 
@@ -7690,7 +7702,9 @@
                                                       Converted);
 
     if (Context.hasSameType(CanonType,
-                        ClassTemplate->getInjectedClassNameSpecialization())) {
+                        ClassTemplate->getInjectedClassNameSpecialization())
+        && (!Context.getLangOpts().ConceptsTS
+            || !TemplateParams->hasAssociatedConstraints())) {
       // C++ [temp.class.spec]p9b3:
       //
       //   -- The argument list of the specialization shall not be identical
@@ -10301,3 +10315,9 @@
                           MissingImportKind::PartialSpecialization,
                           /*Recover*/true);
 }
+
+    // p1
+    //   template-parameter:
+    //     ...
+    //     parameter-declaration
+    // p2
\ No newline at end of file
Index: lib/Sema/SemaConcept.cpp
===================================================================
--- lib/Sema/SemaConcept.cpp
+++ lib/Sema/SemaConcept.cpp
@@ -60,6 +60,16 @@
   return NewACInfo == OldACInfo;
 }
 
+void
+Sema::DiagnoseRedeclarationConstraintMismatch(const TemplateParameterList *Old,
+                                              const TemplateParameterList *New){
+  Diag(New->getTemplateLoc(),
+       diag::err_template_different_associated_constraints);
+
+  Diag(Old->getTemplateLoc(), diag::note_template_prev_declaration)
+        << /*declaration*/0;
+}
+
 template <typename AtomicEvaluator>
 static bool
 calculateConstraintSatisfaction(Sema &S, const Expr *ConstraintExpr,
@@ -404,4 +414,450 @@
     diagnoseUnsatisfiedConstraintExpr(*this, Pair.first, Pair.second, First);
     First = false;
   }
+}
+namespace {
+struct AtomicConstraint {
+  AtomicConstraint(const Expr *ConstraintExpr,
+      const ASTTemplateArgumentListInfo *ParameterMapping = nullptr) :
+      ConstraintExpr{ConstraintExpr}, ParameterMapping{ParameterMapping} {}
+
+  bool subsumes(ASTContext &C, const AtomicConstraint &Other) const {
+    // C++ [temp.constr.order] p2
+    //   - an atomic constraint A subsumes another atomic constraint B
+    //     if and only if the A and B are identical [...]
+    //
+    // C++ [temp.constr.atomic] p2
+    //   Two atomic constraints are identical if they are formed from the
+    //   same expression and the targets of the parameter mappings are
+    //   equivalent according to the rules for expressions [...]
+
+    // We do not actually substitute the parameter mappings, therefore the
+    // constraint expressions are the originals, and comparing them will
+    // suffice.
+    if (ConstraintExpr != Other.ConstraintExpr)
+      return false;
+
+    // Check that the parameter lists are identical
+    if ((!ParameterMapping) != (!Other.ParameterMapping))
+      return false;
+    if (!ParameterMapping)
+      return true;
+    if (ParameterMapping->NumTemplateArgs !=
+        Other.ParameterMapping->NumTemplateArgs)
+      return false;
+
+    for (unsigned I = 0, S = ParameterMapping->NumTemplateArgs; I < S; ++I)
+      if (!C.getCanonicalTemplateArgument(
+                ParameterMapping->arguments()[I].getArgument())
+               .structurallyEquals(C.getCanonicalTemplateArgument(
+                   Other.ParameterMapping->arguments()[I].getArgument())))
+        return false;
+
+
+    return true;
+  }
+
+  const Expr *ConstraintExpr;
+  const ASTTemplateArgumentListInfo *ParameterMapping;
+};
+
+/// \brief A normalized constraint, as defined in C++ [temp.constr.normal], is
+/// either an atomic constraint, a conjunction of normalized constraints or a
+/// disjunction of normalized constraints.
+struct NormalizedConstraint {
+  enum CompoundConstraintKind { CCK_Conjunction, CCK_Disjunction };
+
+  using CompoundConstraint = llvm::PointerIntPair<
+      std::pair<NormalizedConstraint, NormalizedConstraint> *, 1,
+      CompoundConstraintKind>;
+
+  llvm::PointerUnion<AtomicConstraint *, CompoundConstraint> Constraint;
+
+  NormalizedConstraint(AtomicConstraint *C) : Constraint{C} {};
+  NormalizedConstraint(ASTContext &C, NormalizedConstraint LHS,
+                       NormalizedConstraint RHS, CompoundConstraintKind Kind)
+      : Constraint{CompoundConstraint{
+            new (C) std::pair<NormalizedConstraint, NormalizedConstraint>{LHS,
+                                                                          RHS},
+            Kind}} {};
+
+  CompoundConstraintKind getCompoundKind() const {
+    assert(!isAtomic() && "getCompoundKind called on atomic constraint.");
+    return Constraint.get<CompoundConstraint>().getInt();
+  }
+
+  bool isAtomic() const { return Constraint.is<AtomicConstraint *>(); }
+
+  NormalizedConstraint &getLHS() const {
+    assert(!isAtomic() && "getLHS called on atomic constraint.");
+    return Constraint.get<CompoundConstraint>().getPointer()->first;
+  }
+
+  NormalizedConstraint &getRHS() const {
+    assert(!isAtomic() && "getRHS called on atomic constraint.");
+    return Constraint.get<CompoundConstraint>().getPointer()->second;
+  }
+
+  AtomicConstraint *getAtomicConstraint() const {
+    assert(isAtomic() &&
+           "getAtomicConstraint called on non-atomic constraint.");
+    return Constraint.get<AtomicConstraint *>();
+  }
+  static llvm::Optional<NormalizedConstraint> fromConstraintExpr(
+      Sema &S, const Expr *E, TemplateDecl *TD = nullptr,
+      const ASTTemplateArgumentListInfo *ParameterMapping = nullptr) {
+    assert(E != nullptr);
+
+    // C++ [temp.constr.normal]p1.1
+    // [...]
+    // - The normal form of an expression (E) is the normal form of E.
+    // [...]
+    if (auto *P = dyn_cast<const ParenExpr>(E))
+      return fromConstraintExpr(S, P->getSubExpr(), TD, ParameterMapping);
+    if (auto *BO = dyn_cast<const BinaryOperator>(E)) {
+      if (BO->getOpcode() == BO_LAnd || BO->getOpcode() == BO_LOr) {
+        auto LHS = fromConstraintExpr(S, BO->getLHS(), TD, ParameterMapping);
+        if (!LHS)
+          return llvm::Optional<NormalizedConstraint>{};
+        auto RHS = fromConstraintExpr(S, BO->getRHS(), TD, ParameterMapping);
+        if (!RHS)
+          return llvm::Optional<NormalizedConstraint>{};
+
+        return NormalizedConstraint(
+            S.Context, std::move(*LHS), std::move(*RHS),
+            BO->getOpcode() == BO_LAnd ? CCK_Conjunction : CCK_Disjunction);
+      }
+    } else if (auto *CSE = dyn_cast<const ConceptSpecializationExpr>(E)) {
+      // C++ [temp.constr.normal]p1.1
+      // [...]
+      // The normal form of an id-expression of the form C<A1, A2, ..., AN>,
+      // where C names a concept, is the normal form of the
+      // constraint-expression of C, after substituting A1, A2, ..., AN for C’s
+      // respective template parameters in the parameter mappings in each atomic
+      // constraint. If any such substitution results in an invalid type or
+      // expression, the program is ill-formed; no diagnostic is required.
+      // [...]
+      const ASTTemplateArgumentListInfo *Mapping =
+          CSE->getTemplateArgsAsWritten();
+      if (!ParameterMapping) {
+        // This is a top level CSE.
+        //
+        // template<typename T>
+        // concept C = true;
+        //
+        // template<typename U>
+        // void foo() requires C<U> {} -> Mapping is <U>
+        //
+        llvm::SmallVector<TemplateArgument, 4> TempList;
+        bool InstantiationDependent = false;
+        TemplateArgumentListInfo TALI(Mapping->LAngleLoc, Mapping->RAngleLoc);
+        for (auto &Arg : Mapping->arguments())
+          TALI.addArgument(Arg);
+        bool Failed = S.CheckTemplateArgumentList(CSE->getNamedConcept(),
+            E->getBeginLoc(), TALI, /*PartialTemplateArgs=*/false, TempList,
+            /*UpdateArgsWithConversions=*/false, &InstantiationDependent);
+        // The potential failure case here is this:
+        //
+        // template<typename U>
+        // concept C = true;
+        //
+        // template<typename T>
+        // void foo() requires C<T, T> // The immediate constraint expr
+        //                             // contains a CSE with incorrect no.
+        //                             // of arguments.
+        // {}
+        // This case should have been handled when C<T, T> was parsed.
+        assert(
+            !Failed &&
+            "Unmatched arguments in top level concept specialization "
+            "expression should've been caught while it was being constructed");
+
+        if (InstantiationDependent)
+          // The case is this:
+          //
+          // template<typename U, typename T>
+          // concept C = true;
+          //
+          // template<typename... Ts>
+          // void foo() requires C<Ts...> // The immediate constraint expr
+          //                              // contains a CSE whose parameters
+          //                              // are not mappable to arguments
+          //                              // without concrete values.
+          // {}
+          //
+          // Just treat C<Ts...> as an atomic constraint.
+          return NormalizedConstraint{new (S.Context)
+                                          AtomicConstraint(E, Mapping)};
+
+        return fromConstraintExpr(S,
+                                  CSE->getNamedConcept()->getConstraintExpr(),
+                                  CSE->getNamedConcept(), Mapping);
+      }
+
+      // This is not a top level CSE.
+      //
+      // template<typename T1, typename T2>
+      // concept C1 = true;
+      //
+      // template<typename T, typename U>
+      // concept C2 = C1<U, T>; -> We are here.
+      //                           Mapping is {T1=U, T2=T}
+      //                           ParameterMapping is {T=X, U=Y}, TD is C2
+      //
+      // template<typename X, typename Y>
+      // void foo() requires C2<X, Y> {}
+      //
+      // We would like to substitute ParameterMapping into Mapping, to get
+      // ParameterMapping={T1=Y, T2=X} for the next level down.
+      // Instead of doing the direct substitution of ParameterMapping into
+      // Mapping, we instead substitute ParameterMapping into C1<U, T> and take
+      // the substituted argument list as the ParameterMapping for the next
+      // level down.
+      assert(TD && "ParameterMapping provided without TemplateDecl");
+
+      TemplateArgumentListInfo TALI(ParameterMapping->LAngleLoc,
+                                    ParameterMapping->RAngleLoc);
+      for (auto &Arg : ParameterMapping->arguments())
+        TALI.addArgument(Arg);
+      llvm::SmallVector<TemplateArgument, 4> TempList;
+      bool InstantiationDependent = false;
+      bool Success =
+          !S.CheckTemplateArgumentList(TD, ParameterMapping->LAngleLoc,
+                                       TALI, /*PartialTemplateArgs=*/false,
+                                       TempList,
+                                       /*UpdateArgsWithConversions=*/false,
+                                       &InstantiationDependent) &&
+          !InstantiationDependent;
+      assert(Success && "ParameterMapping should have already been cheked "
+                        "against template argument list earlier.");
+
+      auto DiagnoseSubstitutionError = [&](unsigned int Diag) {
+        std::string TemplateArgString = S.getTemplateArgumentBindingsText(
+            TD->getTemplateParameters(), TempList.data(), TempList.size());
+        S.Diag(CSE->getBeginLoc(), Diag)
+            << const_cast<ConceptSpecializationExpr *>(CSE)
+            << TemplateArgString;
+      };
+
+      MultiLevelTemplateArgumentList MLTAL;
+      MLTAL.addOuterTemplateArguments(TempList);
+
+      ExprResult Result = S.SubstExpr(
+          const_cast<ConceptSpecializationExpr *>(CSE), MLTAL);
+      if (!Result.isUsable() || Result.isInvalid()) {
+        // C++ [temp.constr.normal]
+        // If any such substitution results in an invalid type or
+        // expression, the program is ill-formed; no diagnostic is required.
+
+        // A diagnostic was already emitted from the substitution , but
+        // we'll let the user know why it's not SFINAEd from them.
+        DiagnoseSubstitutionError(
+            diag::note_could_not_normalize_argument_substitution_failed);
+        return llvm::Optional<NormalizedConstraint>{};
+      }
+      ParameterMapping = cast<ConceptSpecializationExpr>(Result.get())
+                           ->getTemplateArgsAsWritten();
+
+      TemplateArgumentListInfo SubstTALI(ParameterMapping->LAngleLoc,
+                                         ParameterMapping->RAngleLoc);
+      for (auto &Arg : ParameterMapping->arguments())
+        SubstTALI.addArgument(Arg);
+      llvm::SmallVector<TemplateArgument, 4> Converted;
+      bool Failure = S.CheckTemplateArgumentList(
+          CSE->getNamedConcept(), CSE->getBeginLoc(), SubstTALI,
+          /*PartialTemplateArgs=*/false, Converted,
+          /*UpdateArgsWithConversions=*/true, &InstantiationDependent);
+      // The case is this:
+      //
+      // template<typename T, typename U>
+      // concept C1 = true;
+      //
+      // template<typename... Ts>
+      // concept C2 = C1<Ts...>; // After substituting Ts = {T}, the
+      //                         // resulting argument list does not match
+      //                         // the parameter list.
+      //
+      // template<typename T>
+      // void foo() requires C2<T> {}
+      //
+      // This case should be checked when substituting into C1<Ts...>, and will
+      // be caught by the if above.
+      assert(!Failure &&
+             "Template argument list match should have been checked during "
+             "substitution.");
+      if (InstantiationDependent)
+        // The case is this:
+        //
+        // template<typename T, typename U>
+        // concept C1 = true;
+        //
+        // template<typename... Us>
+        // concept C2 = C1<Us...>; // After substituting Us = {Ts}, we cannot
+        //                         // match arguments to parameters.
+        //
+        // template<typename... Ts>
+        // void foo() requires C2<T...> {}
+        //
+        // Treat the CSE as an atomic expression.
+        return NormalizedConstraint{new (S.Context)
+                                        AtomicConstraint(E, ParameterMapping)};
+
+      return fromConstraintExpr(S, CSE->getNamedConcept()->getConstraintExpr(),
+                                CSE->getNamedConcept(), ParameterMapping);
+    }
+    return NormalizedConstraint{new (S.Context)
+                                    AtomicConstraint(E, ParameterMapping)};
+  }
+
+  static llvm::Optional<NormalizedConstraint> fromConstraintExprs(Sema &S,
+      ArrayRef<const Expr *> E) {
+    assert(E.size() != 0);
+    auto First = fromConstraintExpr(S, E[0]);
+    if (E.size() == 1)
+      return First;
+    auto Second = fromConstraintExpr(S, E[1]);
+    if (!Second)
+      return llvm::Optional<NormalizedConstraint>{};
+    llvm::Optional<NormalizedConstraint> Conjunction;
+    Conjunction.emplace(S.Context, std::move(*First), std::move(*Second),
+                        CCK_Conjunction);
+    for (unsigned I = 2; I < E.size(); ++I) {
+      auto Next = fromConstraintExpr(S, E[I]);
+      if (!Next)
+        return llvm::Optional<NormalizedConstraint>{};
+      NormalizedConstraint NewConjunction(S.Context, std::move(*Conjunction),
+                                          std::move(*Next), CCK_Conjunction);
+      *Conjunction = std::move(NewConjunction);
+    }
+    return Conjunction;
+  }
+};
+} // namespace
+
+using NormalForm =
+    llvm::SmallVector<llvm::SmallVector<AtomicConstraint *, 2>, 4>;
+
+static NormalForm makeCNF(const NormalizedConstraint &Normalized) {
+  if (Normalized.isAtomic())
+    return {{Normalized.getAtomicConstraint()}};
+
+  NormalForm LCNF = makeCNF(Normalized.getLHS());
+  NormalForm RCNF = makeCNF(Normalized.getRHS());
+  if (Normalized.getCompoundKind() == NormalizedConstraint::CCK_Conjunction) {
+    LCNF.reserve(LCNF.size() + RCNF.size());
+    while (!RCNF.empty())
+      LCNF.push_back(std::move(RCNF.pop_back_val()));
+    return LCNF;
+  }
+
+  // Disjunction
+  NormalForm Res;
+  Res.reserve(LCNF.size() * RCNF.size());
+  for (auto &LDisjunction : LCNF)
+    for (auto &RDisjunction : RCNF) {
+      NormalForm::value_type Combined;
+      Combined.reserve(LDisjunction.size() + RDisjunction.size());
+      std::copy(LDisjunction.begin(), LDisjunction.end(),
+                std::back_inserter(Combined));
+      std::copy(RDisjunction.begin(), RDisjunction.end(),
+                std::back_inserter(Combined));
+      Res.emplace_back(Combined);
+    }
+  return Res;
+}
+
+static NormalForm makeDNF(const NormalizedConstraint &Normalized) {
+  if (Normalized.isAtomic())
+    return {{Normalized.getAtomicConstraint()}};
+
+  NormalForm LDNF = makeDNF(Normalized.getLHS());
+  NormalForm RDNF = makeDNF(Normalized.getRHS());
+  if (Normalized.getCompoundKind() == NormalizedConstraint::CCK_Disjunction) {
+    LDNF.reserve(LDNF.size() + RDNF.size());
+    while (!RDNF.empty())
+      LDNF.push_back(std::move(RDNF.pop_back_val()));
+    return LDNF;
+  }
+
+  // Conjunction
+  NormalForm Res;
+  Res.reserve(LDNF.size() * RDNF.size());
+  for (auto &LConjunction : LDNF) {
+    for (auto &RConjunction : RDNF) {
+      NormalForm::value_type Combined;
+      Combined.reserve(LConjunction.size() + RConjunction.size());
+      std::copy(LConjunction.begin(), LConjunction.end(),
+                std::back_inserter(Combined));
+      std::copy(RConjunction.begin(), RConjunction.end(),
+                std::back_inserter(Combined));
+      Res.emplace_back(Combined);
+    }
+  }
+  return Res;
+}
+
+static bool subsumes(Sema &S, ArrayRef<const Expr *> P,
+                     ArrayRef<const Expr *> Q) {
+  // C++ [temp.constr.order] p2
+  //   In order to determine if a constraint P subsumes a constraint Q, P is
+  //   transformed into disjunctive normal form, and Q is transformed into
+  //   conjunctive normal form. [...]
+  auto PNormalized = NormalizedConstraint::fromConstraintExprs(S, P);
+  if (!PNormalized)
+    // Program is ill formed at this point.
+    return false;
+  const NormalForm PDNF = makeDNF(*PNormalized);
+
+  auto QNormalized = NormalizedConstraint::fromConstraintExprs(S, Q);
+  if (!QNormalized)
+    // Program is ill formed at this point.
+    return false;
+  const NormalForm QCNF = makeCNF(*QNormalized);
+
+  // C++ [temp.constr.order] p2
+  //   Then, P subsumes Q if and only if, for every disjunctive clause Pi in the
+  //   disjunctive normal form of P, Pi subsumes every conjunctive clause Qj in
+  //   the conjuctive normal form of Q, where [...]
+  for (const auto &Pi : PDNF) {
+    for (const auto &Qj : QCNF) {
+      // C++ [temp.constr.order] p2
+      //   - [...] a disjunctive clause Pi subsumes a conjunctive clause Qj if
+      //     and only if there exists an atomic constraint Pia in Pi for which
+      //     there exists an atomic constraint, Qjb, in Qj such that Pia
+      //     subsumes Qjb.
+      bool Found = false;
+      for (const AtomicConstraint *Pia : Pi) {
+        for (const AtomicConstraint *Qjb : Qj) {
+          if (Pia->subsumes(S.Context, *Qjb)) {
+            Found = true;
+            break;
+          }
+        }
+        if (Found)
+          break;
+      }
+      if (!Found) {
+        return false;
+      }
+    }
+  }
+  return true;
+}
+
+bool Sema::IsMoreConstrained(NamedDecl *D1, ArrayRef<const Expr *> AC1,
+                             NamedDecl *D2, ArrayRef<const Expr *> AC2) {
+  if (AC1.empty())
+    return AC2.empty();
+  if (AC2.empty())
+    // TD1 has associated constraints and TD2 does not.
+    return true;
+
+  std::pair<NamedDecl *, NamedDecl *> Key{D1, D2};
+  auto CacheEntry = SubsumptionCache.find(Key);
+  if (CacheEntry != SubsumptionCache.end()) {
+    return CacheEntry->second;
+  }
+  bool Subsumes = subsumes(*this, AC1, AC2);
+  SubsumptionCache.try_emplace(Key, Subsumes);
+  return Subsumes;
 }
\ No newline at end of file
Index: lib/AST/DeclTemplate.cpp
===================================================================
--- lib/AST/DeclTemplate.cpp
+++ lib/AST/DeclTemplate.cpp
@@ -233,15 +233,15 @@
   }
 }
 
-template<class EntryType>
+template<class EntryType, typename... ProfileArguments>
 typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
 RedeclarableTemplateDecl::findSpecializationImpl(
-    llvm::FoldingSetVector<EntryType> &Specs, ArrayRef<TemplateArgument> Args,
-    void *&InsertPos) {
+    llvm::FoldingSetVector<EntryType> &Specs, void *&InsertPos,
+    ProfileArguments... ProfileArgs) {
   using SETraits = SpecEntryTraits<EntryType>;
 
   llvm::FoldingSetNodeID ID;
-  EntryType::Profile(ID, Args, getASTContext());
+  EntryType::Profile(ID, ProfileArgs..., getASTContext());
   EntryType *Entry = Specs.FindNodeOrInsertPos(ID, InsertPos);
   return Entry ? SETraits::getDecl(Entry)->getMostRecentDecl() : nullptr;
 }
@@ -256,8 +256,8 @@
 #ifndef NDEBUG
     void *CorrectInsertPos;
     assert(!findSpecializationImpl(Specializations,
-                                   SETraits::getTemplateArgs(Entry),
-                                   CorrectInsertPos) &&
+                                   CorrectInsertPos,
+                                   SETraits::getTemplateArgs(Entry)) &&
            InsertPos == CorrectInsertPos &&
            "given incorrect InsertPos for specialization");
 #endif
@@ -314,7 +314,7 @@
 FunctionDecl *
 FunctionTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
                                          void *&InsertPos) {
-  return findSpecializationImpl(getSpecializations(), Args, InsertPos);
+  return findSpecializationImpl(getSpecializations(), InsertPos, Args);
 }
 
 void FunctionTemplateDecl::addSpecialization(
@@ -420,7 +420,7 @@
 ClassTemplateSpecializationDecl *
 ClassTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
                                       void *&InsertPos) {
-  return findSpecializationImpl(getSpecializations(), Args, InsertPos);
+  return findSpecializationImpl(getSpecializations(), InsertPos, Args);
 }
 
 void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
@@ -430,8 +430,9 @@
 
 ClassTemplatePartialSpecializationDecl *
 ClassTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
-                                             void *&InsertPos) {
-  return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
+    ArrayRef<const Expr *> AssociatedConstraints, void *&InsertPos) {
+  return findSpecializationImpl(getPartialSpecializations(), InsertPos,
+                                Args, AssociatedConstraints);
 }
 
 void ClassTemplateDecl::AddPartialSpecialization(
@@ -1035,7 +1036,7 @@
 VarTemplateSpecializationDecl *
 VarTemplateDecl::findSpecialization(ArrayRef<TemplateArgument> Args,
                                     void *&InsertPos) {
-  return findSpecializationImpl(getSpecializations(), Args, InsertPos);
+  return findSpecializationImpl(getSpecializations(), InsertPos, Args);
 }
 
 void VarTemplateDecl::AddSpecialization(VarTemplateSpecializationDecl *D,
@@ -1045,8 +1046,9 @@
 
 VarTemplatePartialSpecializationDecl *
 VarTemplateDecl::findPartialSpecialization(ArrayRef<TemplateArgument> Args,
-                                           void *&InsertPos) {
-  return findSpecializationImpl(getPartialSpecializations(), Args, InsertPos);
+     ArrayRef<const Expr *> AssociatedConstraints, void *&InsertPos) {
+  return findSpecializationImpl(getPartialSpecializations(), InsertPos, Args,
+                                AssociatedConstraints);
 }
 
 void VarTemplateDecl::AddPartialSpecialization(
Index: lib/AST/ASTImporter.cpp
===================================================================
--- lib/AST/ASTImporter.cpp
+++ lib/AST/ASTImporter.cpp
@@ -5014,14 +5014,16 @@
       D->getTemplateArgs().data(), D->getTemplateArgs().size(), TemplateArgs))
     return std::move(Err);
 
-  // Try to find an existing specialization with these template arguments.
+  // Try to find an existing specialization with these template arguments and
+  // constraints.
   void *InsertPos = nullptr;
   ClassTemplateSpecializationDecl *PrevDecl = nullptr;
   ClassTemplatePartialSpecializationDecl *PartialSpec =
             dyn_cast<ClassTemplatePartialSpecializationDecl>(D);
   if (PartialSpec)
     PrevDecl =
-        ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos);
+        ClassTemplate->findPartialSpecialization(TemplateArgs,
+            PartialSpec->getAssociatedConstraints(), InsertPos);
   else
     PrevDecl = ClassTemplate->findSpecialization(TemplateArgs, InsertPos);
 
@@ -5089,10 +5091,11 @@
 
     // Update InsertPos, because preceding import calls may have invalidated
     // it by adding new specializations.
-    if (!ClassTemplate->findPartialSpecialization(TemplateArgs, InsertPos))
+    auto *PartSpec = cast<ClassTemplatePartialSpecializationDecl>(D2);
+    if (!ClassTemplate->findPartialSpecialization(TemplateArgs,
+            PartSpec->getAssociatedConstraints(), InsertPos))
       // Add this partial specialization to the class template.
-      ClassTemplate->AddPartialSpecialization(
-          cast<ClassTemplatePartialSpecializationDecl>(D2), InsertPos);
+      ClassTemplate->AddPartialSpecialization(PartSpec, InsertPos);
 
   } else { // Not a partial specialization.
     if (GetImportedOrCreateDecl(
Index: include/clang/Sema/Sema.h
===================================================================
--- include/clang/Sema/Sema.h
+++ include/clang/Sema/Sema.h
@@ -5765,6 +5765,19 @@
   /// A diagnostic is emitted if it is not, and false is returned.
   bool CheckConstraintExpression(Expr *CE);
 
+private:
+  /// \brief Caches pairs of template-like decls whose associated constraints
+  /// were checked for subsumption and whether or not the first's constraints
+  /// did in fact subsume the second's.
+  llvm::DenseMap<std::pair<NamedDecl *, NamedDecl *>, bool> SubsumptionCache;
+
+public:
+  /// \brief Returns whether the given declaration's associated constraints are
+  /// more constrained than another declaration's according to the partial
+  /// ordering of constraints.
+  bool IsMoreConstrained(NamedDecl *D1, ArrayRef<const Expr *> AC1,
+                         NamedDecl *D2, ArrayRef<const Expr *> AC2);
+
   /// \brief Check whether the given list of constraint expressions are
   /// satisfied (as if in a 'conjunction') given template arguments.
   /// \param ConstraintExprs a list of constraint expressions, treated as if
@@ -5838,6 +5851,9 @@
   void DiagnoseUnsatisfiedIllFormedConstraint(SourceLocation DiagnosticLocation,
                                               StringRef Diagnostic);
 
+  void DiagnoseRedeclarationConstraintMismatch(const TemplateParameterList *Old,
+                                              const TemplateParameterList *New);
+
   // ParseObjCStringLiteral - Parse Objective-C string literals.
   ExprResult ParseObjCStringLiteral(SourceLocation *AtLocs,
                                     ArrayRef<Expr *> Strings);
Index: include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- include/clang/Basic/DiagnosticSemaKinds.td
+++ include/clang/Basic/DiagnosticSemaKinds.td
@@ -2465,11 +2465,9 @@
   "%select{and |because }0%1 does not satisfy %2">;
 def note_atomic_constraint_evaluated_to_false_elaborated : Note<
   "%select{and |because }0'%1' (%2 %3 %4) evaluated to false">;
-def err_could_not_normalize_ill_formed_constraint : Error<
-  "required expansion of concept specialization %0 failed, substituted "
-  "expression would be illegal">;
-def note_could_not_normalize_ill_formed_constraint_reason : Note<
-  "because: %0">;
+def note_could_not_normalize_argument_substitution_failed : Note<
+  "when substituting into %0 %1. Make sure concept arguments are "
+  "valid for any substitution">;
 
 def err_template_different_associated_constraints : Error<
   "associated constraints differ in template redeclaration">;
Index: include/clang/AST/DeclTemplate.h
===================================================================
--- include/clang/AST/DeclTemplate.h
+++ include/clang/AST/DeclTemplate.h
@@ -736,9 +736,10 @@
 
   void loadLazySpecializationsImpl() const;
 
-  template <class EntryType> typename SpecEntryTraits<EntryType>::DeclType*
+  template <class EntryType, typename... ProfileArguments>
+  typename SpecEntryTraits<EntryType>::DeclType*
   findSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
-                         ArrayRef<TemplateArgument> Args, void *&InsertPos);
+                         void *&InsertPos, ProfileArguments... ProfileArgs);
 
   template <class Derived, class EntryType>
   void addSpecializationImpl(llvm::FoldingSetVector<EntryType> &Specs,
@@ -1985,7 +1986,21 @@
              ->getInjectedSpecializationType();
   }
 
-  // FIXME: Add Profile support!
+  void Profile(llvm::FoldingSetNodeID &ID) const {
+    Profile(ID, getTemplateArgs().asArray(), getAssociatedConstraints(),
+            getASTContext());
+  }
+
+  static void
+  Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
+          ArrayRef<const Expr *> AssociatedConstraints, ASTContext &Context) {
+    ID.AddInteger(TemplateArgs.size());
+    for (const TemplateArgument &TemplateArg : TemplateArgs)
+      TemplateArg.Profile(ID, Context);
+    ID.AddInteger(AssociatedConstraints.size());
+    for (const Expr *Constraint : AssociatedConstraints)
+      Constraint->Profile(ID, Context, /*Canonical=*/true);
+  }
 
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
 
@@ -2109,7 +2124,9 @@
   /// Return the partial specialization with the provided arguments if it
   /// exists, otherwise return the insertion point.
   ClassTemplatePartialSpecializationDecl *
-  findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
+  findPartialSpecialization(ArrayRef<TemplateArgument> Args,
+                            ArrayRef<const Expr *> AssociatedConstraints,
+                            void *&InsertPos);
 
   /// Insert the specified partial specialization knowing that it is not
   /// already in. InsertPos must be obtained from findPartialSpecialization.
@@ -2806,6 +2823,22 @@
     return First->InstantiatedFromMember.setInt(true);
   }
 
+  void Profile(llvm::FoldingSetNodeID &ID) const {
+    Profile(ID, getTemplateArgs().asArray(), getAssociatedConstraints(),
+            getASTContext());
+  }
+
+  static void
+  Profile(llvm::FoldingSetNodeID &ID, ArrayRef<TemplateArgument> TemplateArgs,
+          ArrayRef<const Expr *> AssociatedConstraints, ASTContext &Context) {
+    ID.AddInteger(TemplateArgs.size());
+    for (const TemplateArgument &TemplateArg : TemplateArgs)
+      TemplateArg.Profile(ID, Context);
+    ID.AddInteger(AssociatedConstraints.size());
+    for (const Expr *Constraint : AssociatedConstraints)
+      Constraint->Profile(ID, Context, /*Canonical=*/true);
+  }
+
   static bool classof(const Decl *D) { return classofKind(D->getKind()); }
 
   static bool classofKind(Kind K) {
@@ -2924,7 +2957,9 @@
   /// Return the partial specialization with the provided arguments if it
   /// exists, otherwise return the insertion point.
   VarTemplatePartialSpecializationDecl *
-  findPartialSpecialization(ArrayRef<TemplateArgument> Args, void *&InsertPos);
+  findPartialSpecialization(ArrayRef<TemplateArgument> Args,
+                            ArrayRef<const Expr *> AssociatedConstraints,
+                            void *&InsertPos);
 
   /// Insert the specified partial specialization knowing that it is not
   /// already in. InsertPos must be obtained from findPartialSpecialization.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to