clang 24.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
95 DeclarationNameInfo ConceptNameInfo,
98
99 assert(NamedConcept.isConceptName() &&
100 "concept reference does not name a concept");
101
102 return new (C) ConceptReference(NNS, TemplateKWLoc, ConceptNameInfo,
104}
105
107 // Note that if the qualifier is null the template KW must also be null.
108 if (auto QualifierLoc = getNestedNameSpecifierLoc())
109 return QualifierLoc.getBeginLoc();
111}
112
113void ConceptReference::print(llvm::raw_ostream &OS,
114 const PrintingPolicy &Policy) const {
115 NestedNameSpec.getNestedNameSpecifier().print(OS, Policy);
118 OS << "<";
119 llvm::ListSeparator Sep(", ");
120 // FIXME: Find corresponding parameter for argument
121 for (auto &ArgLoc : ArgsAsWritten->arguments()) {
122 OS << Sep;
123 ArgLoc.getArgument().print(Policy, OS, /*IncludeType*/ false);
124 }
125 OS << ">";
126 }
127}
128
130 const ConceptReference *C) {
131 std::string NameStr;
132 llvm::raw_string_ostream OS(NameStr);
133 LangOptions LO;
134 LO.CPlusPlus = true;
135 LO.Bool = true;
136 OS << '\'';
137 C->print(OS, PrintingPolicy(LO));
138 OS << '\'';
139 return DB << NameStr;
140}
141
143 Expr *E, bool IsSimple, SourceLocation NoexceptLoc,
145 ConceptSpecializationExpr *SubstitutedConstraintExpr)
146 : Requirement(IsSimple ? RK_Simple : RK_Compound, Status == SS_Dependent,
147 Status == SS_Dependent &&
150 Status == SS_Satisfied),
151 Value(E), NoexceptLoc(NoexceptLoc), TypeReq(Req),
152 SubstitutedConstraintExpr(SubstitutedConstraintExpr), Status(Status) {
153 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
154 "Simple requirement must not have a return type requirement or a "
155 "noexcept specification");
156 assert((Status > SS_TypeRequirementSubstitutionFailure &&
157 Req.isTypeConstraint()) == (SubstitutedConstraintExpr != nullptr));
158}
159
161 SubstitutionDiagnostic *ExprSubstDiag, bool IsSimple,
162 SourceLocation NoexceptLoc, ReturnTypeRequirement Req)
163 : Requirement(IsSimple ? RK_Simple : RK_Compound, Req.isDependent(),
164 Req.containsUnexpandedParameterPack(), /*IsSatisfied=*/false),
165 Value(ExprSubstDiag), NoexceptLoc(NoexceptLoc), TypeReq(Req),
167 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
168 "Simple requirement must not have a return type requirement or a "
169 "noexcept specification");
170}
171
174 : TypeConstraintInfo(TPL, false) {
175 assert(TPL->size() == 1);
176 const TypeConstraint *TC =
177 cast<TemplateTypeParmDecl>(TPL->getParam(0))->getTypeConstraint();
178 assert(TC &&
179 "TPL must have a template type parameter with a type constraint");
180 auto *Constraint =
182 bool Dependent =
183 Constraint->getTemplateArgsAsWritten() &&
184 TemplateSpecializationType::anyInstantiationDependentTemplateArguments(
185 Constraint->getTemplateArgsAsWritten()->arguments().drop_front(1));
186 TypeConstraintInfo.setInt(Dependent ? true : false);
187}
188
190 TemplateParameterList *TPL, bool IsDependent)
191 : TypeConstraintInfo(TPL, IsDependent) {}
192
194 : Requirement(RK_Type, T->getType()->isInstantiationDependentType(),
196 // We reach this ctor with either dependent types (in which
197 // IsSatisfied doesn't matter) or with non-dependent type in
198 // which the existence of the type indicates satisfaction.
199 /*IsSatisfied=*/true),
200 Value(T),
201 Status(T->getType()->isInstantiationDependentType() ? SS_Dependent
202 : 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:223
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
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateName NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition ASTConcept.h:155
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition ASTConcept.h:170
const DeclarationNameInfo & getConceptNameInfo() const
Definition ASTConcept.h:174
TemplateName NamedConcept
The concept named.
Definition ASTConcept.h:149
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition ASTConcept.h:137
static ConceptReference * Create(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, TemplateName NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
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
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:113
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:275
A C++ nested-name-specifier augmented with source location information.
Encodes a location in the source.
The streaming interface shared between DiagnosticBuilder and PartialDiagnostic.
Represents a C++ template name within the type system.
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:8472
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.
Top level wrappers for InstallAPI frontend operations.
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:585
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.