clang 22.0.0git
ASTConcept.cpp
Go to the documentation of this file.
1//===--- ASTConcept.cpp - 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 defines AST data structures related to concepts.
11///
12//===----------------------------------------------------------------------===//
13
19#include "llvm/ADT/StringExtras.h"
20
21using namespace clang;
22
23static void
25 const UnsatisfiedConstraintRecord &Detail,
26 UnsatisfiedConstraintRecord *TrailingObject) {
27 if (auto *E = dyn_cast<Expr *>(Detail))
28 new (TrailingObject) UnsatisfiedConstraintRecord(E);
29 else {
30 auto &SubstitutionDiagnostic =
31 *cast<std::pair<SourceLocation, StringRef> *>(Detail);
32 StringRef Message = C.backupStr(SubstitutionDiagnostic.second);
33 auto *NewSubstDiag = new (C) std::pair<SourceLocation, StringRef>(
34 SubstitutionDiagnostic.first, Message);
35 new (TrailingObject) UnsatisfiedConstraintRecord(NewSubstDiag);
36 }
37}
38
40 const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
41 : NumRecords{Satisfaction.Details.size()},
42 IsSatisfied{Satisfaction.IsSatisfied}, ContainsErrors{
43 Satisfaction.ContainsErrors} {
44 for (unsigned I = 0; I < NumRecords; ++I)
46 getTrailingObjects() + I);
47}
48
50 const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
51 : NumRecords{Satisfaction.NumRecords},
52 IsSatisfied{Satisfaction.IsSatisfied},
53 ContainsErrors{Satisfaction.ContainsErrors} {
54 for (unsigned I = 0; I < NumRecords; ++I)
55 CreateUnsatisfiedConstraintRecord(C, *(Satisfaction.begin() + I),
56 getTrailingObjects() + I);
57}
58
61 const ConstraintSatisfaction &Satisfaction) {
62 std::size_t size =
63 totalSizeToAlloc<UnsatisfiedConstraintRecord>(
64 Satisfaction.Details.size());
65 void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
66 return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
67}
68
70 const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction) {
71 std::size_t size =
72 totalSizeToAlloc<UnsatisfiedConstraintRecord>(Satisfaction.NumRecords);
73 void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
74 return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
75}
76
78 llvm::FoldingSetNodeID &ID, const ASTContext &C,
79 const NamedDecl *ConstraintOwner, ArrayRef<TemplateArgument> TemplateArgs) {
80 ID.AddPointer(ConstraintOwner);
81 ID.AddInteger(TemplateArgs.size());
82 for (auto &Arg : TemplateArgs)
83 Arg.Profile(ID, C);
84}
85
88 SourceLocation TemplateKWLoc,
89 DeclarationNameInfo ConceptNameInfo,
90 NamedDecl *FoundDecl, TemplateDecl *NamedConcept,
91 const ASTTemplateArgumentListInfo *ArgsAsWritten) {
92 return new (C) ConceptReference(NNS, TemplateKWLoc, ConceptNameInfo,
94}
95
97 // Note that if the qualifier is null the template KW must also be null.
98 if (auto QualifierLoc = getNestedNameSpecifierLoc())
99 return QualifierLoc.getBeginLoc();
101}
102
103void ConceptReference::print(llvm::raw_ostream &OS,
104 const PrintingPolicy &Policy) const {
106 ConceptName.printName(OS, Policy);
108 OS << "<";
109 llvm::ListSeparator Sep(", ");
110 // FIXME: Find corresponding parameter for argument
111 for (auto &ArgLoc : ArgsAsWritten->arguments()) {
112 OS << Sep;
113 ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
114 }
115 OS << ">";
116 }
117}
118
120 Expr *E, bool IsSimple, SourceLocation NoexceptLoc,
122 ConceptSpecializationExpr *SubstitutedConstraintExpr)
123 : Requirement(IsSimple ? RK_Simple : RK_Compound, Status == SS_Dependent,
124 Status == SS_Dependent &&
125 (E->containsUnexpandedParameterPack() ||
126 Req.containsUnexpandedParameterPack()),
127 Status == SS_Satisfied),
128 Value(E), NoexceptLoc(NoexceptLoc), TypeReq(Req),
129 SubstitutedConstraintExpr(SubstitutedConstraintExpr), Status(Status) {
130 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
131 "Simple requirement must not have a return type requirement or a "
132 "noexcept specification");
133 assert((Status > SS_TypeRequirementSubstitutionFailure &&
134 Req.isTypeConstraint()) == (SubstitutedConstraintExpr != nullptr));
135}
136
138 SubstitutionDiagnostic *ExprSubstDiag, bool IsSimple,
139 SourceLocation NoexceptLoc, ReturnTypeRequirement Req)
140 : Requirement(IsSimple ? RK_Simple : RK_Compound, Req.isDependent(),
141 Req.containsUnexpandedParameterPack(), /*IsSatisfied=*/false),
142 Value(ExprSubstDiag), NoexceptLoc(NoexceptLoc), TypeReq(Req),
143 Status(SS_ExprSubstitutionFailure) {
144 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
145 "Simple requirement must not have a return type requirement or a "
146 "noexcept specification");
147}
148
151 : TypeConstraintInfo(TPL, false) {
152 assert(TPL->size() == 1);
153 const TypeConstraint *TC =
154 cast<TemplateTypeParmDecl>(TPL->getParam(0))->getTypeConstraint();
155 assert(TC &&
156 "TPL must have a template type parameter with a type constraint");
157 auto *Constraint =
158 cast<ConceptSpecializationExpr>(TC->getImmediatelyDeclaredConstraint());
159 bool Dependent =
160 Constraint->getTemplateArgsAsWritten() &&
162 Constraint->getTemplateArgsAsWritten()->arguments().drop_front(1));
163 TypeConstraintInfo.setInt(Dependent ? true : false);
164}
165
167 TemplateParameterList *TPL, bool IsDependent)
168 : TypeConstraintInfo(TPL, IsDependent) {}
169
171 : Requirement(RK_Type, T->getType()->isInstantiationDependentType(),
173 // We reach this ctor with either dependent types (in which
174 // IsSatisfied doesn't matter) or with non-dependent type in
175 // which the existence of the type indicates satisfaction.
176 /*IsSatisfied=*/true),
177 Value(T),
178 Status(T->getType()->isInstantiationDependentType() ? SS_Dependent
179 : SS_Satisfied) {}
static void CreateUnsatisfiedConstraintRecord(const ASTContext &C, const UnsatisfiedConstraintRecord &Detail, UnsatisfiedConstraintRecord *TrailingObject)
Definition: ASTConcept.cpp:24
This file provides AST data structures related to concepts.
Defines the clang::ASTContext interface.
Expr * E
Defines Expressions and AST nodes for C++2a concepts.
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
const DeclarationNameInfo & getConceptNameInfo() const
Definition: ASTConcept.h:170
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 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
static ConceptReference * Create(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition: ASTConcept.cpp:87
Represents the specialization of a concept - evaluates to a prvalue of type bool.
Definition: ExprConcepts.h:42
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:37
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.
NestedNameSpecifier getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
void print(raw_ostream &OS, const PrintingPolicy &Policy, bool ResolveTemplateArguments=false, bool PrintFinalScopeResOp=true) const
Print this nested name specifier to the given output stream.
Encodes a location in the source.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Definition: DeclTemplate.h:396
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:74
NamedDecl * getParam(unsigned Idx)
Definition: DeclTemplate.h:146
static bool anyInstantiationDependentTemplateArguments(ArrayRef< TemplateArgumentLoc > Args)
Definition: Type.cpp:4609
Models the abbreviated syntax to constrain a template type parameter: template <convertible_to<string...
Definition: ASTConcept.h:223
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition: ASTConcept.h:240
A container of type source information.
Definition: TypeBase.h:8314
ReturnTypeRequirement()
No return type requirement was specified.
Definition: ExprConcepts.h:302
ExprRequirement(Expr *E, bool IsSimple, SourceLocation NoexceptLoc, ReturnTypeRequirement Req, SatisfactionStatus Status, ConceptSpecializationExpr *SubstitutedConstraintExpr=nullptr)
Construct a compound requirement.
Definition: ASTConcept.cpp:119
A static requirement that can be used in a requires-expression to check properties of types and expre...
Definition: ExprConcepts.h:170
bool containsUnexpandedParameterPack() const
Definition: ExprConcepts.h:220
TypeRequirement(TypeSourceInfo *T)
Construct a type requirement from a type.
Definition: ASTConcept.cpp:170
The JSON file list parser is used to communicate input to InstallAPI.
@ Dependent
Parse the block as a dependent block, which may be used in some template instantiations but not other...
llvm::PointerUnion< Expr *, std::pair< SourceLocation, StringRef > * > UnsatisfiedConstraintRecord
Pairs of unsatisfied atomic constraint expressions along with the substituted constraint expr,...
Definition: ASTConcept.h:83
const FunctionProtoType * T
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:91
static ASTConstraintSatisfaction * Rebuild(const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:69
ASTConstraintSatisfaction(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:39
const UnsatisfiedConstraintRecord * begin() const
Definition: ASTConcept.h:96
static ASTConstraintSatisfaction * Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:60
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:678
ArrayRef< TemplateArgumentLoc > arguments() const
Definition: TemplateBase.h:707
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
void printName(raw_ostream &OS, PrintingPolicy Policy) const
printName - Print the human-readable name to a stream.
SourceLocation getBeginLoc() const
getBeginLoc - Retrieve the location of the first token.
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57