clang  14.0.0git
SemaConcept.h
Go to the documentation of this file.
1 //===-- SemaConcept.h - Semantic Analysis for Constraints and Concepts ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 // This file provides semantic analysis for C++ constraints and concepts.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_SEMA_SEMACONCEPT_H
14 #define LLVM_CLANG_SEMA_SEMACONCEPT_H
15 #include "clang/AST/ASTConcept.h"
16 #include "clang/AST/ASTContext.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/DeclTemplate.h"
20 #include "llvm/ADT/PointerUnion.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include <string>
24 #include <utility>
25 
26 namespace clang {
27 class Sema;
28 
32 
35 
37  const AtomicConstraint &Other) const {
38  if (!ParameterMapping != !Other.ParameterMapping)
39  return false;
40  if (!ParameterMapping)
41  return true;
42  if (ParameterMapping->size() != Other.ParameterMapping->size())
43  return false;
44 
45  for (unsigned I = 0, S = ParameterMapping->size(); I < S; ++I) {
46  llvm::FoldingSetNodeID IDA, IDB;
47  C.getCanonicalTemplateArgument((*ParameterMapping)[I].getArgument())
48  .Profile(IDA, C);
49  C.getCanonicalTemplateArgument((*Other.ParameterMapping)[I].getArgument())
50  .Profile(IDB, C);
51  if (IDA != IDB)
52  return false;
53  }
54  return true;
55  }
56 
57  bool subsumes(ASTContext &C, const AtomicConstraint &Other) const {
58  // C++ [temp.constr.order] p2
59  // - an atomic constraint A subsumes another atomic constraint B
60  // if and only if the A and B are identical [...]
61  //
62  // C++ [temp.constr.atomic] p2
63  // Two atomic constraints are identical if they are formed from the
64  // same expression and the targets of the parameter mappings are
65  // equivalent according to the rules for expressions [...]
66 
67  // We do not actually substitute the parameter mappings into the
68  // constraint expressions, therefore the constraint expressions are
69  // the originals, and comparing them will suffice.
70  if (ConstraintExpr != Other.ConstraintExpr)
71  return false;
72 
73  // Check that the parameter lists are identical
74  return hasMatchingParameterMapping(C, Other);
75  }
76 };
77 
78 /// \brief A normalized constraint, as defined in C++ [temp.constr.normal], is
79 /// either an atomic constraint, a conjunction of normalized constraints or a
80 /// disjunction of normalized constraints.
82  friend class Sema;
83 
85 
86  using CompoundConstraint = llvm::PointerIntPair<
87  std::pair<NormalizedConstraint, NormalizedConstraint> *, 1,
89 
90  llvm::PointerUnion<AtomicConstraint *, CompoundConstraint> Constraint;
91 
96  new (C) std::pair<NormalizedConstraint, NormalizedConstraint>{
97  std::move(LHS), std::move(RHS)}, Kind}} { };
98 
100  if (Other.isAtomic()) {
102  } else {
104  new (C) std::pair<NormalizedConstraint, NormalizedConstraint>{
105  NormalizedConstraint(C, Other.getLHS()),
106  NormalizedConstraint(C, Other.getRHS())},
107  Other.getCompoundKind());
108  }
109  }
111  Constraint(Other.Constraint) {
112  Other.Constraint = nullptr;
113  }
114  NormalizedConstraint &operator=(const NormalizedConstraint &Other) = delete;
116  if (&Other != this) {
117  NormalizedConstraint Temp(std::move(Other));
118  std::swap(Constraint, Temp.Constraint);
119  }
120  return *this;
121  }
122 
124  assert(!isAtomic() && "getCompoundKind called on atomic constraint.");
125  return Constraint.get<CompoundConstraint>().getInt();
126  }
127 
128  bool isAtomic() const { return Constraint.is<AtomicConstraint *>(); }
129 
131  assert(!isAtomic() && "getLHS called on atomic constraint.");
132  return Constraint.get<CompoundConstraint>().getPointer()->first;
133  }
134 
136  assert(!isAtomic() && "getRHS called on atomic constraint.");
137  return Constraint.get<CompoundConstraint>().getPointer()->second;
138  }
139 
141  assert(isAtomic() &&
142  "getAtomicConstraint called on non-atomic constraint.");
143  return Constraint.get<AtomicConstraint *>();
144  }
145 
146 private:
148  fromConstraintExprs(Sema &S, NamedDecl *D, ArrayRef<const Expr *> E);
150  fromConstraintExpr(Sema &S, NamedDecl *D, const Expr *E);
151 };
152 
153 } // clang
154 
155 #endif //LLVM_CLANG_SEMA_SEMACONCEPT_H
clang::NormalizedConstraint
A normalized constraint, as defined in C++ [temp.constr.normal], is either an atomic constraint,...
Definition: SemaConcept.h:81
clang::NormalizedConstraint::NormalizedConstraint
NormalizedConstraint(ASTContext &C, NormalizedConstraint LHS, NormalizedConstraint RHS, CompoundConstraintKind Kind)
Definition: SemaConcept.h:93
clang::NormalizedConstraint::getCompoundKind
CompoundConstraintKind getCompoundKind() const
Definition: SemaConcept.h:123
clang::AtomicConstraint
Definition: SemaConcept.h:29
clang::NormalizedConstraint::CompoundConstraintKind
CompoundConstraintKind
Definition: SemaConcept.h:84
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:249
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:54
llvm::Optional
Definition: LLVM.h:40
getArgument
static const TemplateArgument & getArgument(const TemplateArgument &A)
Definition: TypePrinter.cpp:1849
DeclTemplate.h
clang::NormalizedConstraint::getLHS
NormalizedConstraint & getLHS() const
Definition: SemaConcept.h:130
clang::NormalizedConstraint::NormalizedConstraint
NormalizedConstraint(NormalizedConstraint &&Other)
Definition: SemaConcept.h:110
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::NormalizedConstraint::getAtomicConstraint
AtomicConstraint * getAtomicConstraint() const
Definition: SemaConcept.h:140
Expr.h
ASTContext.h
clang::NormalizedConstraint::operator=
NormalizedConstraint & operator=(const NormalizedConstraint &Other)=delete
ASTConcept.h
This file provides AST data structures related to concepts.
clang::NormalizedConstraint::CCK_Disjunction
@ CCK_Disjunction
Definition: SemaConcept.h:84
SourceLocation.h
llvm::ArrayRef
Definition: LLVM.h:34
clang::AtomicConstraint::ParameterMapping
Optional< MutableArrayRef< TemplateArgumentLoc > > ParameterMapping
Definition: SemaConcept.h:31
clang::Sema
Sema - This implements semantic analysis and AST building for C.
Definition: Sema.h:355
clang::NormalizedConstraint::NormalizedConstraint
NormalizedConstraint(AtomicConstraint *C)
Definition: SemaConcept.h:92
clang::NormalizedConstraint::Constraint
llvm::PointerUnion< AtomicConstraint *, CompoundConstraint > Constraint
Definition: SemaConcept.h:90
clang::AtomicConstraint::ConstraintExpr
const Expr * ConstraintExpr
Definition: SemaConcept.h:30
clang::AtomicConstraint::AtomicConstraint
AtomicConstraint(Sema &S, const Expr *ConstraintExpr)
Definition: SemaConcept.h:33
clang::NormalizedConstraint::CCK_Conjunction
@ CCK_Conjunction
Definition: SemaConcept.h:84
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang
Definition: CalledOnceCheck.h:17
clang::NormalizedConstraint::CompoundConstraint
llvm::PointerIntPair< std::pair< NormalizedConstraint, NormalizedConstraint > *, 1, CompoundConstraintKind > CompoundConstraint
Definition: SemaConcept.h:88
clang::AtomicConstraint::subsumes
bool subsumes(ASTContext &C, const AtomicConstraint &Other) const
Definition: SemaConcept.h:57
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::NormalizedConstraint::NormalizedConstraint
NormalizedConstraint(ASTContext &C, const NormalizedConstraint &Other)
Definition: SemaConcept.h:99
clang::NormalizedConstraint::isAtomic
bool isAtomic() const
Definition: SemaConcept.h:128
clang::NormalizedConstraint::operator=
NormalizedConstraint & operator=(NormalizedConstraint &&Other)
Definition: SemaConcept.h:115
clang::NormalizedConstraint::getRHS
NormalizedConstraint & getRHS() const
Definition: SemaConcept.h:135
clang::AtomicConstraint::hasMatchingParameterMapping
bool hasMatchingParameterMapping(ASTContext &C, const AtomicConstraint &Other) const
Definition: SemaConcept.h:36