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

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 (Detail.isNull())
28 new (TrailingObject) UnsatisfiedConstraintRecord(nullptr);
29 else if (const auto *E = llvm::dyn_cast<const Expr *>(Detail))
30 new (TrailingObject) UnsatisfiedConstraintRecord(E);
31 else if (const auto *Concept =
32 llvm::dyn_cast<const ConceptReference *>(Detail))
33 new (TrailingObject) UnsatisfiedConstraintRecord(Concept);
34 else {
35 auto &SubstitutionDiagnostic =
37 StringRef Message = C.backupStr(SubstitutionDiagnostic.second);
38 auto *NewSubstDiag = new (C) clang::ConstraintSubstitutionDiagnostic(
39 SubstitutionDiagnostic.first, Message);
40 new (TrailingObject) UnsatisfiedConstraintRecord(NewSubstDiag);
41 }
42}
43
45 const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
46 : NumRecords{Satisfaction.Details.size()},
48 Satisfaction.ContainsErrors} {
49 for (unsigned I = 0; I < NumRecords; ++I)
51 getTrailingObjects() + I);
52}
53
55 const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction)
56 : NumRecords{Satisfaction.NumRecords},
57 IsSatisfied{Satisfaction.IsSatisfied},
58 ContainsErrors{Satisfaction.ContainsErrors} {
59 for (unsigned I = 0; I < NumRecords; ++I)
60 CreateUnsatisfiedConstraintRecord(C, *(Satisfaction.begin() + I),
61 getTrailingObjects() + I);
62}
63
66 const ConstraintSatisfaction &Satisfaction) {
67 std::size_t size =
68 totalSizeToAlloc<UnsatisfiedConstraintRecord>(
69 Satisfaction.Details.size());
70 void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
71 return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
72}
73
75 const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction) {
76 std::size_t size =
77 totalSizeToAlloc<UnsatisfiedConstraintRecord>(Satisfaction.NumRecords);
78 void *Mem = C.Allocate(size, alignof(ASTConstraintSatisfaction));
79 return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
80}
81
82void ConstraintSatisfaction::Profile(llvm::FoldingSetNodeID &ID,
83 const ASTContext &C,
84 const NamedDecl *ConstraintOwner,
85 ArrayRef<TemplateArgument> TemplateArgs) {
86 ID.AddPointer(ConstraintOwner);
87 ID.AddInteger(TemplateArgs.size());
88 for (auto &Arg : TemplateArgs)
89 Arg.Profile(ID, C);
90}
91
101
103 // Note that if the qualifier is null the template KW must also be null.
104 if (auto QualifierLoc = getNestedNameSpecifierLoc())
105 return QualifierLoc.getBeginLoc();
107}
108
109void ConceptReference::print(llvm::raw_ostream &OS,
110 const PrintingPolicy &Policy) const {
111 NestedNameSpec.getNestedNameSpecifier().print(OS, Policy);
112 ConceptName.printName(OS, Policy);
114 OS << "<";
115 llvm::ListSeparator Sep(", ");
116 // FIXME: Find corresponding parameter for argument
117 for (auto &ArgLoc : ArgsAsWritten->arguments()) {
118 OS << Sep;
119 ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
120 }
121 OS << ">";
122 }
123}
124
126 const ConceptReference *C) {
127 std::string NameStr;
128 llvm::raw_string_ostream OS(NameStr);
129 LangOptions LO;
130 LO.CPlusPlus = true;
131 LO.Bool = true;
132 OS << '\'';
133 C->print(OS, PrintingPolicy(LO));
134 OS << '\'';
135 return DB << NameStr;
136}
137
139 Expr *E, bool IsSimple, SourceLocation NoexceptLoc,
141 ConceptSpecializationExpr *SubstitutedConstraintExpr)
142 : Requirement(IsSimple ? RK_Simple : RK_Compound, Status == SS_Dependent,
143 Status == SS_Dependent &&
146 Status == SS_Satisfied),
147 Value(E), NoexceptLoc(NoexceptLoc), TypeReq(Req),
148 SubstitutedConstraintExpr(SubstitutedConstraintExpr), Status(Status) {
149 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
150 "Simple requirement must not have a return type requirement or a "
151 "noexcept specification");
152 assert((Status > SS_TypeRequirementSubstitutionFailure &&
153 Req.isTypeConstraint()) == (SubstitutedConstraintExpr != nullptr));
154}
155
157 SubstitutionDiagnostic *ExprSubstDiag, bool IsSimple,
158 SourceLocation NoexceptLoc, ReturnTypeRequirement Req)
159 : Requirement(IsSimple ? RK_Simple : RK_Compound, Req.isDependent(),
160 Req.containsUnexpandedParameterPack(), /*IsSatisfied=*/false),
161 Value(ExprSubstDiag), NoexceptLoc(NoexceptLoc), TypeReq(Req),
163 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
164 "Simple requirement must not have a return type requirement or a "
165 "noexcept specification");
166}
167
170 : TypeConstraintInfo(TPL, false) {
171 assert(TPL->size() == 1);
172 const TypeConstraint *TC =
173 cast<TemplateTypeParmDecl>(TPL->getParam(0))->getTypeConstraint();
174 assert(TC &&
175 "TPL must have a template type parameter with a type constraint");
176 auto *Constraint =
178 bool Dependent =
179 Constraint->getTemplateArgsAsWritten() &&
180 TemplateSpecializationType::anyInstantiationDependentTemplateArguments(
181 Constraint->getTemplateArgsAsWritten()->arguments().drop_front(1));
182 TypeConstraintInfo.setInt(Dependent ? true : false);
183}
184
186 TemplateParameterList *TPL, bool IsDependent)
187 : TypeConstraintInfo(TPL, IsDependent) {}
188
190 : Requirement(RK_Type, T->getType()->isInstantiationDependentType(),
192 // We reach this ctor with either dependent types (in which
193 // IsSatisfied doesn't matter) or with non-dependent type in
194 // which the existence of the type indicates satisfaction.
195 /*IsSatisfied=*/true),
196 Value(T),
197 Status(T->getType()->isInstantiationDependentType() ? SS_Dependent
198 : SS_Satisfied) {}
static void CreateUnsatisfiedConstraintRecord(const ASTContext &C, const UnsatisfiedConstraintRecord &Detail, UnsatisfiedConstraintRecord *TrailingObject)
This file provides AST data structures related to concepts.
Defines the clang::ASTContext interface.
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:220
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
const DeclarationNameInfo & getConceptNameInfo() const
Definition ASTConcept.h:174
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 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
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition ASTConcept.h:155
static ConceptReference * Create(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Represents the specialization of a concept - evaluates to a prvalue of type bool.
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition ASTConcept.h:47
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
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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.
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
The base class of all kinds of template declarations (e.g., class, function, etc.).
Stores a list of template parameters for a TemplateDecl and its derived classes.
NamedDecl * getParam(unsigned Idx)
Models the abbreviated syntax to constrain a template type parameter: template <convertible_to<string...
Definition ASTConcept.h:227
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition ASTConcept.h:244
A container of type source information.
Definition TypeBase.h:8265
ReturnTypeRequirement()
No return type requirement was specified.
ExprRequirement(Expr *E, bool IsSimple, SourceLocation NoexceptLoc, ReturnTypeRequirement Req, SatisfactionStatus Status, ConceptSpecializationExpr *SubstitutedConstraintExpr=nullptr)
Construct a compound requirement.
bool containsUnexpandedParameterPack() const
Requirement(RequirementKind Kind, bool IsDependent, bool ContainsUnexpandedParameterPack, bool IsSatisfied=true)
TypeSourceInfo * getType() const
TypeRequirement(TypeSourceInfo *T)
Construct a type requirement from a type.
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
const FunctionProtoType * T
@ Concept
The name was classified as a concept name.
Definition Sema.h:589
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.
U cast(CodeGen::Address addr)
Definition Address.h:327
#define false
Definition stdbool.h:26
#define true
Definition stdbool.h:25
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)
ASTConstraintSatisfaction(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
const UnsatisfiedConstraintRecord * begin() const
Definition ASTConcept.h:96
static ASTConstraintSatisfaction * Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspon...
SourceLocation getBeginLoc() const
getBeginLoc - Retrieve the location of the first token.
Describes how types, statements, expressions, and declarations should be printed.