clang 24.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.
48public:
49 bool IsSatisfied = false;
50 bool ContainsErrors = false;
51
52 /// \brief The substituted constraint expr, if the template arguments could be
53 /// substituted into them, or a diagnostic if substitution resulted in an
54 /// invalid expression.
56
57 static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C,
58 const NamedDecl *ConstraintOwner,
59 ArrayRef<TemplateArgument> TemplateArgs);
60
62 for (const auto &Detail : Details)
63 if (dyn_cast<const ConstraintSubstitutionDiagnostic *>(Detail))
64 return true;
65 return false;
66 }
67};
68
69/// \brief The result of a constraint satisfaction check, containing the
70/// necessary information to diagnose an unsatisfied constraint.
71///
72/// This is safe to store in an AST node, as opposed to ConstraintSatisfaction.
73class ASTConstraintSatisfaction final
74 : llvm::TrailingObjects<ASTConstraintSatisfaction,
75 UnsatisfiedConstraintRecord> {
76 friend TrailingObjects;
77 friend class ASTStmtWriter;
78 friend class ASTStmtReader;
79
80 unsigned NumRecords : 30;
81 bool IsSatisfied : 1;
82 bool ContainsErrors : 1;
83
84 ASTConstraintSatisfaction(const ASTContext &C,
85 const ConstraintSatisfaction &Satisfaction);
86
87 ASTConstraintSatisfaction(const ASTContext &C,
88 const ASTConstraintSatisfaction &Satisfaction);
89
90public:
92 return getTrailingObjects();
93 }
94
96 return getTrailingObjects() + NumRecords;
97 }
98
100 return {begin(), end()};
101 }
102
103 bool isSatisfied() const { return IsSatisfied; }
104
105 bool containsErrors() const { return ContainsErrors; }
106
107 unsigned record_size() const { return NumRecords; }
108
110 Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction);
111
113 Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction);
114};
115
116/// A reference to a concept and its template args, as it appears in the code.
117///
118/// Examples:
119/// template <int X> requires is_even<X> int half = X/2;
120/// ~~~~~~~~~~ (in ConceptSpecializationExpr)
121///
122/// std::input_iterator auto I = Container.begin();
123/// ~~~~~~~~~~~~~~~~~~~ (in AutoTypeLoc)
124///
125/// template <std::derives_from<Expr> T> void dump();
126/// ~~~~~~~~~~~~~~~~~~~~~~~ (in TemplateTypeParmDecl)
128protected:
129 // \brief The optional nested name specifier used when naming the concept.
131
132 /// \brief The location of the template keyword, if specified when naming the
133 /// concept.
135
136 /// \brief The concept name used.
138
139 /// \brief The declaration found by name lookup when the expression was
140 /// created.
141 /// Can differ from NamedConcept when, for example, the concept was found
142 /// through a UsingShadowDecl.
144
145 /// \brief The concept named.
147
148 /// \brief The template argument list source info used to specialize the
149 /// concept.
151
159
160public:
161 static ConceptReference *
166
170
172
176
178
180
181 SourceLocation getBeginLoc() const LLVM_READONLY;
182
189
190 SourceRange getSourceRange() const LLVM_READONLY {
191 return SourceRange(getBeginLoc(), getEndLoc());
192 }
193
195 return FoundDecl;
196 }
197
199
203
204 /// \brief Whether or not template arguments were explicitly specified in the
205 /// concept reference (they might not be in type constraints, for example)
207 return ArgsAsWritten != nullptr;
208 }
209
210 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const;
211 void dump() const;
212 void dump(llvm::raw_ostream &OS, const ASTContext &Context) const;
213};
214
215/// Models the abbreviated syntax to constrain a template type parameter:
216/// template <convertible_to<string> T> void print(T object);
217/// ~~~~~~~~~~~~~~~~~~~~~~
218/// Semantically, this adds an "immediately-declared constraint" with extra arg:
219/// requires convertible_to<T, string>
220///
221/// In the C++ grammar, a type-constraint is also used for auto types:
222/// convertible_to<string> auto X = ...;
223/// We do *not* model these as TypeConstraints, but AutoType(Loc) directly.
225 /// \brief The immediately-declared constraint expression introduced by this
226 /// type-constraint.
227 Expr *ImmediatelyDeclaredConstraint = nullptr;
228 ConceptReference *ConceptRef;
229 UnsignedOrNone ArgPackSubstIndex;
230
231public:
233 Expr *ImmediatelyDeclaredConstraint,
234 UnsignedOrNone ArgPackSubstIndex)
235 : ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint),
236 ConceptRef(ConceptRef), ArgPackSubstIndex(ArgPackSubstIndex) {}
237
238 /// \brief Get the immediately-declared constraint expression introduced by
239 /// this type-constraint, that is - the constraint expression that is added to
240 /// the associated constraints of the enclosing declaration in practice.
242 return ImmediatelyDeclaredConstraint;
243 }
244
245 ConceptReference *getConceptReference() const { return ConceptRef; }
246
247 UnsignedOrNone getArgPackSubstIndex() const { return ArgPackSubstIndex; }
248
249 // FIXME: Instead of using these concept related functions the callers should
250 // directly work with the corresponding ConceptReference.
251 TemplateName getNamedConcept() const { return ConceptRef->getNamedConcept(); }
252
254 return ConceptRef->getConceptNameLoc();
255 }
256
258 return ConceptRef->hasExplicitTemplateArgs();
259 }
260
262 return ConceptRef->getTemplateArgsAsWritten();
263 }
264
266 return ConceptRef->getTemplateKWLoc();
267 }
268
269 NamedDecl *getFoundDecl() const { return ConceptRef->getFoundDecl(); }
270
272 return ConceptRef->getNestedNameSpecifierLoc();
273 }
274
276 return ConceptRef->getConceptNameInfo();
277 }
278
279 void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const {
280 ConceptRef->print(OS, Policy);
281 }
282};
283
284/// Insertion operator for diagnostics. This allows sending ConceptReferences's
285/// into a diagnostic with <<.
286const StreamingDiagnostic &operator<<(const StreamingDiagnostic &DB,
287 const ConceptReference *C);
288
289} // clang
290
291#endif // LLVM_CLANG_AST_ASTCONCEPT_H
Defines clang::OptionalUnsigned.
Defines the clang::SourceLocation class and associated facilities.
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition ASTConcept.h:75
const UnsatisfiedConstraintRecord * end() const
Definition ASTConcept.h:95
static ASTConstraintSatisfaction * Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
const UnsatisfiedConstraintRecord * begin() const
Definition ASTConcept.h:91
ArrayRef< UnsatisfiedConstraintRecord > records() const
Definition ASTConcept.h:99
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
Declaration of a C++20 concept.
A reference to a concept and its template args, as it appears in the code.
Definition ASTConcept.h:127
NestedNameSpecifierLoc NestedNameSpec
Definition ASTConcept.h:130
bool hasExplicitTemplateArgs() const
Whether or not template arguments were explicitly specified in the concept reference (they might not ...
Definition ASTConcept.h:206
DeclarationNameInfo ConceptName
The concept name used.
Definition ASTConcept.h:137
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateName NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition ASTConcept.h:152
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition ASTConcept.h:167
NamedDecl * getFoundDecl() const
Definition ASTConcept.h:194
const DeclarationNameInfo & getConceptNameInfo() const
Definition ASTConcept.h:171
SourceRange getSourceRange() const LLVM_READONLY
Definition ASTConcept.h:190
TemplateName NamedConcept
The concept named.
Definition ASTConcept.h:146
SourceLocation getConceptNameLoc() const
Definition ASTConcept.h:173
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition ASTConcept.h:134
void dump(llvm::raw_ostream &OS, const ASTContext &Context) const
SourceLocation getLocation() const
Definition ASTConcept.h:179
SourceLocation getBeginLoc() const LLVM_READONLY
const ASTTemplateArgumentListInfo * ArgsAsWritten
The template argument list source info used to specialize the concept.
Definition ASTConcept.h:150
NamedDecl * FoundDecl
The declaration found by name lookup when the expression was created.
Definition ASTConcept.h:143
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition ASTConcept.h:200
SourceLocation getEndLoc() const LLVM_READONLY
Definition ASTConcept.h:183
TemplateName getNamedConcept() const
Definition ASTConcept.h:198
SourceLocation getTemplateKWLoc() const
Definition ASTConcept.h:177
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition ASTConcept.h:47
static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C, const NamedDecl *ConstraintOwner, ArrayRef< TemplateArgument > TemplateArgs)
llvm::SmallVector< UnsatisfiedConstraintRecord, 1 > Details
The substituted constraint expr, if the template arguments could be substituted into them,...
Definition ASTConcept.h:55
This represents one expression.
Definition Expr.h:114
This represents a decl that may have a name.
Definition Decl.h:276
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.).
Represents a C++ template name within the type system.
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition ASTConcept.h:261
void print(llvm::raw_ostream &OS, const PrintingPolicy &Policy) const
Definition ASTConcept.h:279
UnsignedOrNone getArgPackSubstIndex() const
Definition ASTConcept.h:247
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition ASTConcept.h:241
NamedDecl * getFoundDecl() const
Definition ASTConcept.h:269
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition ASTConcept.h:271
TypeConstraint(ConceptReference *ConceptRef, Expr *ImmediatelyDeclaredConstraint, UnsignedOrNone ArgPackSubstIndex)
Definition ASTConcept.h:232
const DeclarationNameInfo & getConceptNameInfo() const
Definition ASTConcept.h:275
bool hasExplicitTemplateArgs() const
Definition ASTConcept.h:257
TemplateName getNamedConcept() const
Definition ASTConcept.h:251
SourceLocation getConceptNameLoc() const
Definition ASTConcept.h:253
SourceLocation getTemplateKWLoc() const
Definition ASTConcept.h:265
ConceptReference * getConceptReference() const
Definition ASTConcept.h:245
Top level wrappers for InstallAPI frontend operations.
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',...
OptionalUnsigned< unsigned > UnsignedOrNone
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.
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.