clang 17.0.0git
ASTConcept.h
Go to the documentation of this file.
1//===--- ASTConcept.h - Concepts Related AST Data Structures ----*- C++ -*-===//
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/// \file
10/// \brief This file provides AST data structures related to concepts.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_AST_ASTCONCEPT_H
15#define LLVM_CLANG_AST_ASTCONCEPT_H
16
17#include "clang/AST/Expr.h"
19#include "llvm/ADT/PointerUnion.h"
20#include "llvm/ADT/SmallVector.h"
21#include <utility>
22
23namespace clang {
24class ConceptDecl;
25
26/// The result of a constraint satisfaction check, containing the necessary
27/// information to diagnose an unsatisfied constraint.
28class ConstraintSatisfaction : public llvm::FoldingSetNode {
29 // The template-like entity that 'owns' the constraint checked here (can be a
30 // constrained entity or a concept).
31 const NamedDecl *ConstraintOwner = nullptr;
33
34public:
35
37
38 ConstraintSatisfaction(const NamedDecl *ConstraintOwner,
39 ArrayRef<TemplateArgument> TemplateArgs) :
40 ConstraintOwner(ConstraintOwner), TemplateArgs(TemplateArgs.begin(),
41 TemplateArgs.end()) { }
42
43 using SubstitutionDiagnostic = std::pair<SourceLocation, StringRef>;
44 using Detail = llvm::PointerUnion<Expr *, SubstitutionDiagnostic *>;
45
46 bool IsSatisfied = false;
47 bool ContainsErrors = false;
48
49 /// \brief Pairs of unsatisfied atomic constraint expressions along with the
50 /// substituted constraint expr, if the template arguments could be
51 /// substituted into them, or a diagnostic if substitution resulted in an
52 /// invalid expression.
54
55 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C) {
56 Profile(ID, C, ConstraintOwner, TemplateArgs);
57 }
58
59 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C,
60 const NamedDecl *ConstraintOwner,
61 ArrayRef<TemplateArgument> TemplateArgs);
62
64 for (const auto &Detail : Details)
65 if (Detail.second.dyn_cast<SubstitutionDiagnostic *>())
66 return true;
67 return false;
68 }
69};
70
71/// Pairs of unsatisfied atomic constraint expressions along with the
72/// substituted constraint expr, if the template arguments could be
73/// substituted into them, or a diagnostic if substitution resulted in
74/// an invalid expression.
76 std::pair<const Expr *,
77 llvm::PointerUnion<Expr *,
78 std::pair<SourceLocation, StringRef> *>>;
79
80/// \brief The result of a constraint satisfaction check, containing the
81/// necessary information to diagnose an unsatisfied constraint.
82///
83/// This is safe to store in an AST node, as opposed to ConstraintSatisfaction.
85 llvm::TrailingObjects<ASTConstraintSatisfaction,
86 UnsatisfiedConstraintRecord> {
87 std::size_t NumRecords;
88 bool IsSatisfied : 1;
90
92 return getTrailingObjects<UnsatisfiedConstraintRecord>();
93 }
94
96 return getTrailingObjects<UnsatisfiedConstraintRecord>() + NumRecords;
97 }
98
100 const ConstraintSatisfaction &Satisfaction);
102 const ASTConstraintSatisfaction &Satisfaction);
103
105 Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction);
107 Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction);
108};
109
110/// \brief Common data class for constructs that reference concepts with
111/// template arguments.
113protected:
114 // \brief The optional nested name specifier used when naming the concept.
116
117 /// \brief The location of the template keyword, if specified when naming the
118 /// concept.
120
121 /// \brief The concept name used.
123
124 /// \brief The declaration found by name lookup when the expression was
125 /// created.
126 /// Can differ from NamedConcept when, for example, the concept was found
127 /// through a UsingShadowDecl.
129
130 /// \brief The concept named.
132
133 /// \brief The template argument list source info used to specialize the
134 /// concept.
136
137public:
139 DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl,
143 ConceptName(ConceptNameInfo), FoundDecl(FoundDecl),
145
147 : FoundDecl(nullptr), NamedConcept(nullptr), ArgsAsWritten(nullptr) {}
148
150 return NestedNameSpec;
151 }
152
154
156 return getConceptNameInfo().getLoc();
157 }
158
160
162 return FoundDecl;
163 }
164
166 return NamedConcept;
167 }
168
170 return ArgsAsWritten;
171 }
172
173 /// \brief Whether or not template arguments were explicitly specified in the
174 /// concept reference (they might not be in type constraints, for example)
176 return ArgsAsWritten != nullptr;
177 }
178};
179
181 /// \brief The immediately-declared constraint expression introduced by this
182 /// type-constraint.
183 Expr *ImmediatelyDeclaredConstraint = nullptr;
184
185public:
187 DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl,
190 Expr *ImmediatelyDeclaredConstraint) :
191 ConceptReference(NNS, /*TemplateKWLoc=*/SourceLocation(), ConceptNameInfo,
193 ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint) {}
194
195 /// \brief Get the immediately-declared constraint expression introduced by
196 /// this type-constraint, that is - the constraint expression that is added to
197 /// the associated constraints of the enclosing declaration in practice.
199 return ImmediatelyDeclaredConstraint;
200 }
201
202 void print(llvm::raw_ostream &OS, PrintingPolicy Policy) const;
203};
204
205} // clang
206
207#endif // LLVM_CLANG_AST_ASTCONCEPT_H
Defines the clang::SourceLocation class and associated facilities.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:182
Declaration of a C++20 concept.
Common data class for constructs that reference concepts with template arguments.
Definition: ASTConcept.h:112
NestedNameSpecifierLoc NestedNameSpec
Definition: ASTConcept.h:115
bool hasExplicitTemplateArgs() const
Whether or not template arguments were explicitly specified in the concept reference (they might not ...
Definition: ASTConcept.h:175
DeclarationNameInfo ConceptName
The concept name used.
Definition: ASTConcept.h:122
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition: ASTConcept.h:138
ConceptDecl * NamedConcept
The concept named.
Definition: ASTConcept.h:131
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition: ASTConcept.h:149
NamedDecl * getFoundDecl() const
Definition: ASTConcept.h:161
const DeclarationNameInfo & getConceptNameInfo() const
Definition: ASTConcept.h:153
SourceLocation getConceptNameLoc() const
Definition: ASTConcept.h:155
ConceptDecl * getNamedConcept() const
Definition: ASTConcept.h:165
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition: ASTConcept.h:119
const ASTTemplateArgumentListInfo * ArgsAsWritten
The template argument list source info used to specialize the concept.
Definition: ASTConcept.h:135
NamedDecl * FoundDecl
The declaration found by name lookup when the expression was created.
Definition: ASTConcept.h:128
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition: ASTConcept.h:169
SourceLocation getTemplateKWLoc() const
Definition: ASTConcept.h:159
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:28
llvm::SmallVector< std::pair< const Expr *, Detail >, 4 > Details
Pairs of unsatisfied atomic constraint expressions along with the substituted constraint expr,...
Definition: ASTConcept.h:53
llvm::PointerUnion< Expr *, SubstitutionDiagnostic * > Detail
Definition: ASTConcept.h:44
ConstraintSatisfaction(const NamedDecl *ConstraintOwner, ArrayRef< TemplateArgument > TemplateArgs)
Definition: ASTConcept.h:38
std::pair< SourceLocation, StringRef > SubstitutionDiagnostic
Definition: ASTConcept.h:43
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C)
Definition: ASTConcept.h:55
This represents one expression.
Definition: Expr.h:110
This represents a decl that may have a name.
Definition: Decl.h:247
A C++ nested-name-specifier augmented with source location information.
Encodes a location in the source.
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition: ASTConcept.h:198
void print(llvm::raw_ostream &OS, PrintingPolicy Policy) const
TypeConstraint(NestedNameSpecifierLoc NNS, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten, Expr *ImmediatelyDeclaredConstraint)
Definition: ASTConcept.h:186
@ C
Languages that the frontend can parse and compile.
std::pair< const Expr *, llvm::PointerUnion< Expr *, std::pair< SourceLocation, StringRef > * > > UnsatisfiedConstraintRecord
Pairs of unsatisfied atomic constraint expressions along with the substituted constraint expr,...
Definition: ASTConcept.h:78
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:86
const UnsatisfiedConstraintRecord * end() const
Definition: ASTConcept.h:95
static ASTConstraintSatisfaction * Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:76
const UnsatisfiedConstraintRecord * begin() const
Definition: ASTConcept.h:91
static ASTConstraintSatisfaction * Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:67
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:641
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspnd...
SourceLocation getLoc() const
getLoc - Returns the main location of the declaration name.
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57