Thanks to visit codestin.com
Credit goes to clang.llvm.org

clang 22.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
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/PointerUnion.h"
24#include "llvm/ADT/SmallVector.h"
25#include <utility>
26
27namespace clang {
28
29class ConceptDecl;
30class TemplateDecl;
32class Expr;
33class NamedDecl;
34struct PrintingPolicy;
35
36/// Unsatisfied constraint expressions if the template arguments could be
37/// substituted into them, or a diagnostic if substitution resulted in
38/// an invalid expression.
39///
40using ConstraintSubstitutionDiagnostic = std::pair<SourceLocation, StringRef>;
42 llvm::PointerUnion<const Expr *, const ConceptReference *,
44
45/// The result of a constraint satisfaction check, containing the necessary
46/// information to diagnose an unsatisfied constraint.
47class ConstraintSatisfaction : public llvm::FoldingSetNode {
48 // The template-like entity that 'owns' the constraint checked here (can be a
49 // constrained entity or a concept).
50 const NamedDecl *ConstraintOwner = nullptr;
52
53public:
54
56
57 ConstraintSatisfaction(const NamedDecl *ConstraintOwner,
58 ArrayRef<TemplateArgument> TemplateArgs)
59 : ConstraintOwner(ConstraintOwner), TemplateArgs(TemplateArgs) {}
60
61 bool IsSatisfied = false;
62 bool ContainsErrors = false;
63
64 /// \brief The substituted constraint expr, if the template arguments could be
65 /// substituted into them, or a diagnostic if substitution resulted in an
66 /// invalid expression.
68
69 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C) {
70 Profile(ID, C, ConstraintOwner, TemplateArgs);
71 }
72
73 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C,
74 const NamedDecl *ConstraintOwner,
75 ArrayRef<TemplateArgument> TemplateArgs);
76
78 for (const auto &Detail : Details)
79 if (Detail.dyn_cast<const ConstraintSubstitutionDiagnostic *>())
80 return true;
81 return false;
82 }
83};
84
85/// \brief The result of a constraint satisfaction check, containing the
86/// necessary information to diagnose an unsatisfied constraint.
87///
88/// This is safe to store in an AST node, as opposed to ConstraintSatisfaction.
90 llvm::TrailingObjects<ASTConstraintSatisfaction,
91 UnsatisfiedConstraintRecord> {
92 std::size_t NumRecords;
93 bool IsSatisfied : 1;
95
97 return getTrailingObjects();
98 }
99
101 return getTrailingObjects() + NumRecords;
102 }
103
105 return {begin(), end()};
106 }
107
109 const ConstraintSatisfaction &Satisfaction);
111 const ASTConstraintSatisfaction &Satisfaction);
112
114 Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction);
116 Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction);
117};
118
119/// A reference to a concept and its template args, as it appears in the code.
120///
121/// Examples:
122/// template <int X> requires is_even<X> int half = X/2;
123/// ~~~~~~~~~~ (in ConceptSpecializationExpr)
124///
125/// std::input_iterator auto I = Container.begin();
126/// ~~~~~~~~~~~~~~~~~~~ (in AutoTypeLoc)
127///
128/// template <std::derives_from<Expr> T> void dump();
129/// ~~~~~~~~~~~~~~~~~~~~~~~ (in TemplateTypeParmDecl)
131protected:
132 // \brief The optional nested name specifier used when naming the concept.
134
135 /// \brief The location of the template keyword, if specified when naming the
136 /// concept.
138
139 /// \brief The concept name used.
141
142 /// \brief The declaration found by name lookup when the expression was
143 /// created.
144 /// Can differ from NamedConcept when, for example, the concept was found
145 /// through a UsingShadowDecl.
147
148 /// \brief The concept named.
150
151 /// \brief The template argument list source info used to specialize the
152 /// concept.
154
162
163public:
164 static ConceptReference *
169
173
175
179
181
183
184 SourceLocation getBeginLoc() const LLVM_READONLY;
185
192
193 SourceRange getSourceRange() const LLVM_READONLY {
194 return SourceRange(getBeginLoc(), getEndLoc());
195 }
196
198 return FoundDecl;
199 }
200
202
206
207 /// \brief Whether or not template arguments were explicitly specified in the
208 /// concept reference (they might not be in type constraints, for example)
210 return ArgsAsWritten != nullptr;
211 }
212
213 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
214 void dump() const;
215 void dump(llvm::raw_ostream &) const;
216};
217
218/// Models the abbreviated syntax to constrain a template type parameter:
219/// template <convertible_to<string> T> void print(T object);
220/// ~~~~~~~~~~~~~~~~~~~~~~
221/// Semantically, this adds an "immediately-declared constraint" with extra arg:
222/// requires convertible_to<T, string>
223///
224/// In the C++ grammar, a type-constraint is also used for auto types:
225/// convertible_to<string> auto X = ...;
226/// We do *not* model these as TypeConstraints, but AutoType(Loc) directly.
228 /// \brief The immediately-declared constraint expression introduced by this
229 /// type-constraint.
230 Expr *ImmediatelyDeclaredConstraint = nullptr;
231 ConceptReference *ConceptRef;
232 UnsignedOrNone ArgPackSubstIndex;
233
234public:
236 Expr *ImmediatelyDeclaredConstraint,
237 UnsignedOrNone ArgPackSubstIndex)
238 : ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint),
239 ConceptRef(ConceptRef), ArgPackSubstIndex(ArgPackSubstIndex) {}
240
241 /// \brief Get the immediately-declared constraint expression introduced by
242 /// this type-constraint, that is - the constraint expression that is added to
243 /// the associated constraints of the enclosing declaration in practice.
245 return ImmediatelyDeclaredConstraint;
246 }
247
248 ConceptReference *getConceptReference() const { return ConceptRef; }
249
250 UnsignedOrNone getArgPackSubstIndex() const { return ArgPackSubstIndex; }
251
252 // FIXME: Instead of using these concept related functions the callers should
253 // directly work with the corresponding ConceptReference.
255 return ConceptRef->getNamedConcept();
256 }
257
259 return ConceptRef->getConceptNameLoc();
260 }
261
263 return ConceptRef->hasExplicitTemplateArgs();
264 }
265
267 return ConceptRef->getTemplateArgsAsWritten();
268 }
269
271 return ConceptRef->getTemplateKWLoc();
272 }
273
274 NamedDecl *getFoundDecl() const { return ConceptRef->getFoundDecl(); }
275
277 return ConceptRef->getNestedNameSpecifierLoc();
278 }
279
281 return ConceptRef->getConceptNameInfo();
282 }
283
284 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const {
285 ConceptRef->print(OS, Policy);
286 }
287};
288
289/// Insertion operator for diagnostics. This allows sending ConceptReferences's
290/// into a diagnostic with <<.
291const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
292 const ConceptReference *C);
293
294} // clang
295
296#endif // LLVM_CLANG_AST_ASTCONCEPT_H
Defines the clang::SourceLocation class and associated facilities.
Defines clang::UnsignedOrNone.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
Declaration of a C++20 concept.
A reference to a concept and its template args, as it appears in the code.
Definition ASTConcept.h:130
NestedNameSpecifierLoc NestedNameSpec
Definition ASTConcept.h:133
bool hasExplicitTemplateArgs() const
Whether or not template arguments were explicitly specified in the concept reference (they might not ...
Definition ASTConcept.h:209
DeclarationNameInfo ConceptName
The concept name used.
Definition ASTConcept.h:140
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition ASTConcept.h:170
NamedDecl * getFoundDecl() const
Definition ASTConcept.h:197
const DeclarationNameInfo & getConceptNameInfo() const
Definition ASTConcept.h:174
SourceRange getSourceRange() const LLVM_READONLY
Definition ASTConcept.h:193
SourceLocation getConceptNameLoc() const
Definition ASTConcept.h:176
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition ASTConcept.h:137
TemplateDecl * NamedConcept
The concept named.
Definition ASTConcept.h:149
SourceLocation getLocation() const
Definition ASTConcept.h:182
SourceLocation getBeginLoc() const LLVM_READONLY
const ASTTemplateArgumentListInfo * ArgsAsWritten
The template argument list source info used to specialize the concept.
Definition ASTConcept.h:153
NamedDecl * FoundDecl
The declaration found by name lookup when the expression was created.
Definition ASTConcept.h:146
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition ASTConcept.h:203
SourceLocation getEndLoc() const LLVM_READONLY
Definition ASTConcept.h:186
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition ASTConcept.h:155
void dump(llvm::raw_ostream &) const
TemplateDecl * getNamedConcept() const
Definition ASTConcept.h:201
SourceLocation getTemplateKWLoc() const
Definition ASTConcept.h:180
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition ASTConcept.h:47
ConstraintSatisfaction(const NamedDecl *ConstraintOwner, ArrayRef< TemplateArgument > TemplateArgs)
Definition ASTConcept.h:57
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C)
Definition ASTConcept.h:69
llvm::SmallVector< UnsatisfiedConstraintRecord, 4 > Details
The substituted constraint expr, if the template arguments could be substituted into them,...
Definition ASTConcept.h:67
This represents one expression.
Definition Expr.h:112
This represents a decl that may have a name.
Definition Decl.h:274
A C++ nested-name-specifier augmented with source location information.
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
A trivial tuple used to represent a source range.
The base class of all kinds of template declarations (e.g., class, function, etc.).
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition ASTConcept.h:266
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const
Definition ASTConcept.h:284
UnsignedOrNone getArgPackSubstIndex() const
Definition ASTConcept.h:250
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition ASTConcept.h:244
NamedDecl * getFoundDecl() const
Definition ASTConcept.h:274
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition ASTConcept.h:276
TemplateDecl * getNamedConcept() const
Definition ASTConcept.h:254
TypeConstraint(ConceptReference *ConceptRef, Expr *ImmediatelyDeclaredConstraint, UnsignedOrNone ArgPackSubstIndex)
Definition ASTConcept.h:235
const DeclarationNameInfo & getConceptNameInfo() const
Definition ASTConcept.h:280
bool hasExplicitTemplateArgs() const
Definition ASTConcept.h:262
SourceLocation getConceptNameLoc() const
Definition ASTConcept.h:258
SourceLocation getTemplateKWLoc() const
Definition ASTConcept.h:270
ConceptReference * getConceptReference() const
Definition ASTConcept.h:248
The JSON file list parser is used to communicate input to InstallAPI.
llvm::PointerUnion< const Expr *, const ConceptReference *, const ConstraintSubstitutionDiagnostic * > UnsatisfiedConstraintRecord
Definition ASTConcept.h:41
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
std::pair< SourceLocation, StringRef > ConstraintSubstitutionDiagnostic
Unsatisfied constraint expressions if the template arguments could be substituted into them,...
Definition ASTConcept.h:40
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ConceptReference *C)
Insertion operator for diagnostics.
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition ASTConcept.h:91
const UnsatisfiedConstraintRecord * end() const
Definition ASTConcept.h:100
static ASTConstraintSatisfaction * Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
ASTConstraintSatisfaction(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
const UnsatisfiedConstraintRecord * begin() const
Definition ASTConcept.h:96
ArrayRef< UnsatisfiedConstraintRecord > records() const
Definition ASTConcept.h:104
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
SourceLocation getRAngleLoc() const
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
SourceLocation getLoc() const
getLoc - Returns the main location of the declaration name.
SourceLocation getEndLoc() const LLVM_READONLY
Describes how types, statements, expressions, and declarations should be printed.