]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/clang/include/clang/Sema/SemaConcept.h
Merge ^/head r357855 through r357920.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / clang / include / clang / Sema / SemaConcept.h
1 //===-- SemaConcept.h - Semantic Analysis for Constraints and Concepts ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 //  This file provides semantic analysis for C++ constraints and concepts.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CLANG_SEMA_SEMACONCEPT_H
15 #define LLVM_CLANG_SEMA_SEMACONCEPT_H
16 #include "clang/AST/ASTConcept.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/DeclTemplate.h"
20 #include "clang/Basic/SourceLocation.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/Optional.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include <string>
25 #include <utility>
26
27 namespace clang {
28 class Sema;
29
30 struct AtomicConstraint {
31   const Expr *ConstraintExpr;
32   Optional<MutableArrayRef<TemplateArgumentLoc>> ParameterMapping;
33
34   AtomicConstraint(Sema &S, const Expr *ConstraintExpr) :
35       ConstraintExpr(ConstraintExpr) { };
36
37   bool hasMatchingParameterMapping(ASTContext &C,
38                                    const AtomicConstraint &Other) const {
39     if (!ParameterMapping != !Other.ParameterMapping)
40       return false;
41     if (!ParameterMapping)
42       return true;
43     if (ParameterMapping->size() != Other.ParameterMapping->size())
44       return false;
45
46     for (unsigned I = 0, S = ParameterMapping->size(); I < S; ++I)
47       if (!C.getCanonicalTemplateArgument((*ParameterMapping)[I].getArgument())
48                .structurallyEquals(C.getCanonicalTemplateArgument(
49                   (*Other.ParameterMapping)[I].getArgument())))
50         return false;
51     return true;
52   }
53
54   bool subsumes(ASTContext &C, const AtomicConstraint &Other) const {
55     // C++ [temp.constr.order] p2
56     //   - an atomic constraint A subsumes another atomic constraint B
57     //     if and only if the A and B are identical [...]
58     //
59     // C++ [temp.constr.atomic] p2
60     //   Two atomic constraints are identical if they are formed from the
61     //   same expression and the targets of the parameter mappings are
62     //   equivalent according to the rules for expressions [...]
63
64     // We do not actually substitute the parameter mappings into the
65     // constraint expressions, therefore the constraint expressions are
66     // the originals, and comparing them will suffice.
67     if (ConstraintExpr != Other.ConstraintExpr)
68       return false;
69
70     // Check that the parameter lists are identical
71     return hasMatchingParameterMapping(C, Other);
72   }
73 };
74
75 /// \brief A normalized constraint, as defined in C++ [temp.constr.normal], is
76 /// either an atomic constraint, a conjunction of normalized constraints or a
77 /// disjunction of normalized constraints.
78 struct NormalizedConstraint {
79   friend class Sema;
80
81   enum CompoundConstraintKind { CCK_Conjunction, CCK_Disjunction };
82
83   using CompoundConstraint = llvm::PointerIntPair<
84       std::pair<NormalizedConstraint, NormalizedConstraint> *, 1,
85       CompoundConstraintKind>;
86
87   llvm::PointerUnion<AtomicConstraint *, CompoundConstraint> Constraint;
88
89   NormalizedConstraint(AtomicConstraint *C): Constraint{C} { };
90   NormalizedConstraint(ASTContext &C, NormalizedConstraint LHS,
91                        NormalizedConstraint RHS, CompoundConstraintKind Kind)
92       : Constraint{CompoundConstraint{
93             new (C) std::pair<NormalizedConstraint, NormalizedConstraint>{
94                 std::move(LHS), std::move(RHS)}, Kind}} { };
95
96   NormalizedConstraint(ASTContext &C, const NormalizedConstraint &Other) {
97     if (Other.isAtomic()) {
98       Constraint = new (C) AtomicConstraint(*Other.getAtomicConstraint());
99     } else {
100       Constraint = CompoundConstraint(
101           new (C) std::pair<NormalizedConstraint, NormalizedConstraint>{
102               NormalizedConstraint(C, Other.getLHS()),
103               NormalizedConstraint(C, Other.getRHS())},
104               Other.getCompoundKind());
105     }
106   }
107   NormalizedConstraint(NormalizedConstraint &&Other):
108       Constraint(Other.Constraint) {
109     Other.Constraint = nullptr;
110   }
111   NormalizedConstraint &operator=(const NormalizedConstraint &Other) = delete;
112   NormalizedConstraint &operator=(NormalizedConstraint &&Other) {
113     if (&Other != this) {
114       NormalizedConstraint Temp(std::move(Other));
115       std::swap(Constraint, Temp.Constraint);
116     }
117     return *this;
118   }
119
120   CompoundConstraintKind getCompoundKind() const {
121     assert(!isAtomic() && "getCompoundKind called on atomic constraint.");
122     return Constraint.get<CompoundConstraint>().getInt();
123   }
124
125   bool isAtomic() const { return Constraint.is<AtomicConstraint *>(); }
126
127   NormalizedConstraint &getLHS() const {
128     assert(!isAtomic() && "getLHS called on atomic constraint.");
129     return Constraint.get<CompoundConstraint>().getPointer()->first;
130   }
131
132   NormalizedConstraint &getRHS() const {
133     assert(!isAtomic() && "getRHS called on atomic constraint.");
134     return Constraint.get<CompoundConstraint>().getPointer()->second;
135   }
136
137   AtomicConstraint *getAtomicConstraint() const {
138     assert(isAtomic() &&
139            "getAtomicConstraint called on non-atomic constraint.");
140     return Constraint.get<AtomicConstraint *>();
141   }
142
143 private:
144   static Optional<NormalizedConstraint>
145   fromConstraintExprs(Sema &S, NamedDecl *D, ArrayRef<const Expr *> E);
146   static Optional<NormalizedConstraint>
147   fromConstraintExpr(Sema &S, NamedDecl *D, const Expr *E);
148 };
149
150 } // clang
151
152 #endif //LLVM_CLANG_SEMA_SEMACONCEPT_H