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;
31class Expr;
32class NamedDecl;
33struct PrintingPolicy;
34
35/// The result of a constraint satisfaction check, containing the necessary
36/// information to diagnose an unsatisfied constraint.
37class ConstraintSatisfaction : public llvm::FoldingSetNode {
38 // The template-like entity that 'owns' the constraint checked here (can be a
39 // constrained entity or a concept).
40 const NamedDecl *ConstraintOwner = nullptr;
42
43public:
44
46
47 ConstraintSatisfaction(const NamedDecl *ConstraintOwner,
48 ArrayRef<TemplateArgument> TemplateArgs)
49 : ConstraintOwner(ConstraintOwner), TemplateArgs(TemplateArgs) {}
50
51 using SubstitutionDiagnostic = std::pair<SourceLocation, StringRef>;
52 using Detail = llvm::PointerUnion<Expr *, SubstitutionDiagnostic *>;
53
54 bool IsSatisfied = false;
55 bool ContainsErrors = false;
56
57 /// \brief The substituted constraint expr, if the template arguments could be
58 /// substituted into them, or a diagnostic if substitution resulted in an
59 /// invalid expression.
61
62 void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C) {
63 Profile(ID, C, ConstraintOwner, TemplateArgs);
64 }
65
66 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C,
67 const NamedDecl *ConstraintOwner,
68 ArrayRef<TemplateArgument> TemplateArgs);
69
71 for (const auto &Detail : Details)
72 if (Detail.dyn_cast<SubstitutionDiagnostic *>())
73 return true;
74 return false;
75 }
76};
77
78/// Pairs of unsatisfied atomic constraint expressions along with the
79/// substituted constraint expr, if the template arguments could be
80/// substituted into them, or a diagnostic if substitution resulted in
81/// an invalid expression.
83 llvm::PointerUnion<Expr *, std::pair<SourceLocation, StringRef> *>;
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 const ConstraintSatisfaction &Satisfaction);
107 const ASTConstraintSatisfaction &Satisfaction);
108
110 Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction);
112 Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction);
113};
114
115/// A reference to a concept and its template args, as it appears in the code.
116///
117/// Examples:
118/// template <int X> requires is_even<X> int half = X/2;
119/// ~~~~~~~~~~ (in ConceptSpecializationExpr)
120///
121/// std::input_iterator auto I = Container.begin();
122/// ~~~~~~~~~~~~~~~~~~~ (in AutoTypeLoc)
123///
124/// template <std::derives_from<Expr> T> void dump();
125/// ~~~~~~~~~~~~~~~~~~~~~~~ (in TemplateTypeParmDecl)
127protected:
128 // \brief The optional nested name specifier used when naming the concept.
130
131 /// \brief The location of the template keyword, if specified when naming the
132 /// concept.
134
135 /// \brief The concept name used.
137
138 /// \brief The declaration found by name lookup when the expression was
139 /// created.
140 /// Can differ from NamedConcept when, for example, the concept was found
141 /// through a UsingShadowDecl.
143
144 /// \brief The concept named.
146
147 /// \brief The template argument list source info used to specialize the
148 /// concept.
150
152 DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl,
156 ConceptName(ConceptNameInfo), FoundDecl(FoundDecl),
158
159public:
160 static ConceptReference *
165
167 return NestedNameSpec;
168 }
169
171
173 return getConceptNameInfo().getLoc();
174 }
175
177
179
180 SourceLocation getBeginLoc() const LLVM_READONLY;
181
182 SourceLocation getEndLoc() const LLVM_READONLY {
183 return getTemplateArgsAsWritten() &&
187 }
188
189 SourceRange getSourceRange() const LLVM_READONLY {
190 return SourceRange(getBeginLoc(), getEndLoc());
191 }
192
194 return FoundDecl;
195 }
196
198
200 return ArgsAsWritten;
201 }
202
203 /// \brief Whether or not template arguments were explicitly specified in the
204 /// concept reference (they might not be in type constraints, for example)
206 return ArgsAsWritten != nullptr;
207 }
208
209 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
210 void dump() const;
211 void dump(llvm::raw_ostream &) const;
212};
213
214/// Models the abbreviated syntax to constrain a template type parameter:
215/// template <convertible_to<string> T> void print(T object);
216/// ~~~~~~~~~~~~~~~~~~~~~~
217/// Semantically, this adds an "immediately-declared constraint" with extra arg:
218/// requires convertible_to<T, string>
219///
220/// In the C++ grammar, a type-constraint is also used for auto types:
221/// convertible_to<string> auto X = ...;
222/// We do *not* model these as TypeConstraints, but AutoType(Loc) directly.
224 /// \brief The immediately-declared constraint expression introduced by this
225 /// type-constraint.
226 Expr *ImmediatelyDeclaredConstraint = nullptr;
227 ConceptReference *ConceptRef;
228 UnsignedOrNone ArgPackSubstIndex;
229
230public:
232 Expr *ImmediatelyDeclaredConstraint,
233 UnsignedOrNone ArgPackSubstIndex)
234 : ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint),
235 ConceptRef(ConceptRef), ArgPackSubstIndex(ArgPackSubstIndex) {}
236
237 /// \brief Get the immediately-declared constraint expression introduced by
238 /// this type-constraint, that is - the constraint expression that is added to
239 /// the associated constraints of the enclosing declaration in practice.
241 return ImmediatelyDeclaredConstraint;
242 }
243
244 ConceptReference *getConceptReference() const { return ConceptRef; }
245
246 UnsignedOrNone getArgPackSubstIndex() const { return ArgPackSubstIndex; }
247
248 // FIXME: Instead of using these concept related functions the callers should
249 // directly work with the corresponding ConceptReference.
251 return ConceptRef->getNamedConcept();
252 }
253
255 return ConceptRef->getConceptNameLoc();
256 }
257
259 return ConceptRef->hasExplicitTemplateArgs();
260 }
261
263 return ConceptRef->getTemplateArgsAsWritten();
264 }
265
267 return ConceptRef->getTemplateKWLoc();
268 }
269
270 NamedDecl *getFoundDecl() const { return ConceptRef->getFoundDecl(); }
271
273 return ConceptRef->getNestedNameSpecifierLoc();
274 }
275
277 return ConceptRef->getConceptNameInfo();
278 }
279
280 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const {
281 ConceptRef->print(OS, Policy);
282 }
283};
284
285} // clang
286
287#endif // LLVM_CLANG_AST_ASTCONCEPT_H
static char ID
Definition: Arena.cpp:183
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:188
A reference to a concept and its template args, as it appears in the code.
Definition: ASTConcept.h:126
NestedNameSpecifierLoc NestedNameSpec
Definition: ASTConcept.h:129
bool hasExplicitTemplateArgs() const
Whether or not template arguments were explicitly specified in the concept reference (they might not ...
Definition: ASTConcept.h:205
DeclarationNameInfo ConceptName
The concept name used.
Definition: ASTConcept.h:136
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition: ASTConcept.h:166
NamedDecl * getFoundDecl() const
Definition: ASTConcept.h:193
const DeclarationNameInfo & getConceptNameInfo() const
Definition: ASTConcept.h:170
SourceRange getSourceRange() const LLVM_READONLY
Definition: ASTConcept.h:189
SourceLocation getConceptNameLoc() const
Definition: ASTConcept.h:172
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition: ASTConcept.h:133
TemplateDecl * NamedConcept
The concept named.
Definition: ASTConcept.h:145
SourceLocation getLocation() const
Definition: ASTConcept.h:178
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: ASTConcept.cpp:96
const ASTTemplateArgumentListInfo * ArgsAsWritten
The template argument list source info used to specialize the concept.
Definition: ASTConcept.h:149
NamedDecl * FoundDecl
The declaration found by name lookup when the expression was created.
Definition: ASTConcept.h:142
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const
Definition: ASTConcept.cpp:103
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition: ASTConcept.h:199
SourceLocation getEndLoc() const LLVM_READONLY
Definition: ASTConcept.h:182
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition: ASTConcept.h:151
void dump(llvm::raw_ostream &) const
TemplateDecl * getNamedConcept() const
Definition: ASTConcept.h:197
SourceLocation getTemplateKWLoc() const
Definition: ASTConcept.h:176
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:37
llvm::PointerUnion< Expr *, SubstitutionDiagnostic * > Detail
Definition: ASTConcept.h:52
ConstraintSatisfaction(const NamedDecl *ConstraintOwner, ArrayRef< TemplateArgument > TemplateArgs)
Definition: ASTConcept.h:47
std::pair< SourceLocation, StringRef > SubstitutionDiagnostic
Definition: ASTConcept.h:51
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C)
Definition: ASTConcept.h:62
llvm::SmallVector< Detail, 4 > Details
The substituted constraint expr, if the template arguments could be substituted into them,...
Definition: ASTConcept.h:60
This represents one expression.
Definition: Expr.h:112
This represents a decl that may have a name.
Definition: Decl.h:273
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.).
Definition: DeclTemplate.h:396
Models the abbreviated syntax to constrain a template type parameter: template <convertible_to<string...
Definition: ASTConcept.h:223
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition: ASTConcept.h:262
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const
Definition: ASTConcept.h:280
UnsignedOrNone getArgPackSubstIndex() const
Definition: ASTConcept.h:246
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition: ASTConcept.h:240
NamedDecl * getFoundDecl() const
Definition: ASTConcept.h:270
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition: ASTConcept.h:272
TemplateDecl * getNamedConcept() const
Definition: ASTConcept.h:250
TypeConstraint(ConceptReference *ConceptRef, Expr *ImmediatelyDeclaredConstraint, UnsignedOrNone ArgPackSubstIndex)
Definition: ASTConcept.h:231
const DeclarationNameInfo & getConceptNameInfo() const
Definition: ASTConcept.h:276
bool hasExplicitTemplateArgs() const
Definition: ASTConcept.h:258
SourceLocation getConceptNameLoc() const
Definition: ASTConcept.h:254
SourceLocation getTemplateKWLoc() const
Definition: ASTConcept.h:266
ConceptReference * getConceptReference() const
Definition: ASTConcept.h:244
The JSON file list parser is used to communicate input to InstallAPI.
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
llvm::PointerUnion< Expr *, std::pair< SourceLocation, StringRef > * > UnsatisfiedConstraintRecord
Pairs of unsatisfied atomic constraint expressions along with the substituted constraint expr,...
Definition: ASTConcept.h:83
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)
Definition: ASTConcept.cpp:69
const UnsatisfiedConstraintRecord * begin() const
Definition: ASTConcept.h:96
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:678
SourceLocation getRAngleLoc() const
Definition: TemplateBase.h:699
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.
Definition: PrettyPrinter.h:57