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
14#include "clang/AST/ASTConcept.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/ExprConcepts.h"
17#include "clang/AST/NestedNameSpecifier.h"
18#include "clang/AST/PrettyPrinter.h"
19#include "llvm/ADT/StringExtras.h"
20
21using namespace clang;
22
23static void
24CreateUnsatisfiedConstraintRecord(const ASTContext &C,
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 *>(Val: Detail))
30 new (TrailingObject) UnsatisfiedConstraintRecord(E);
31 else if (const auto *Concept =
32 llvm::dyn_cast<const ConceptReference *>(Val: Detail))
33 new (TrailingObject) UnsatisfiedConstraintRecord(Concept);
34 else {
35 auto &SubstitutionDiagnostic =
36 *cast<const clang::ConstraintSubstitutionDiagnostic *>(Val: Detail);
37 StringRef Message = C.backupStr(S: SubstitutionDiagnostic.second);
38 auto *NewSubstDiag = new (C) clang::ConstraintSubstitutionDiagnostic(
39 SubstitutionDiagnostic.first, Message);
40 new (TrailingObject) UnsatisfiedConstraintRecord(NewSubstDiag);
41 }
42}
43
44ASTConstraintSatisfaction::ASTConstraintSatisfaction(
45 const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
46 : NumRecords{Satisfaction.Details.size()},
47 IsSatisfied{Satisfaction.IsSatisfied}, ContainsErrors{
48 Satisfaction.ContainsErrors} {
49 for (unsigned I = 0; I < NumRecords; ++I)
50 CreateUnsatisfiedConstraintRecord(C, Detail: Satisfaction.Details[I],
51 TrailingObject: getTrailingObjects() + I);
52}
53
54ASTConstraintSatisfaction::ASTConstraintSatisfaction(
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, Detail: *(Satisfaction.begin() + I),
61 TrailingObject: getTrailingObjects() + I);
62}
63
64ASTConstraintSatisfaction *
65ASTConstraintSatisfaction::Create(const ASTContext &C,
66 const ConstraintSatisfaction &Satisfaction) {
67 std::size_t size =
68 totalSizeToAlloc<UnsatisfiedConstraintRecord>(
69 Counts: Satisfaction.Details.size());
70 void *Mem = C.Allocate(Size: size, Align: alignof(ASTConstraintSatisfaction));
71 return new (Mem) ASTConstraintSatisfaction(C, Satisfaction);
72}
73
74ASTConstraintSatisfaction *ASTConstraintSatisfaction::Rebuild(
75 const ASTContext &C, const ASTConstraintSatisfaction &Satisfaction) {
76 std::size_t size =
77 totalSizeToAlloc<UnsatisfiedConstraintRecord>(Counts: Satisfaction.NumRecords);
78 void *Mem = C.Allocate(Size: size, Align: 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(Ptr: ConstraintOwner);
87 ID.AddInteger(I: TemplateArgs.size());
88 for (auto &Arg : TemplateArgs)
89 Arg.Profile(ID, Context: C);
90}
91
92ConceptReference *
93ConceptReference::Create(const ASTContext &C, NestedNameSpecifierLoc NNS,
94 SourceLocation TemplateKWLoc,
95 DeclarationNameInfo ConceptNameInfo,
96 NamedDecl *FoundDecl, TemplateName NamedConcept,
97 const ASTTemplateArgumentListInfo *ArgsAsWritten) {
98
99 assert(NamedConcept.isConceptName() &&
100 "concept reference does not name a concept");
101
102 return new (C) ConceptReference(NNS, TemplateKWLoc, ConceptNameInfo,
103 FoundDecl, NamedConcept, ArgsAsWritten);
104}
105
106SourceLocation ConceptReference::getBeginLoc() const {
107 // Note that if the qualifier is null the template KW must also be null.
108 if (auto QualifierLoc = getNestedNameSpecifierLoc())
109 return QualifierLoc.getBeginLoc();
110 return getConceptNameInfo().getBeginLoc();
111}
112
113void ConceptReference::print(llvm::raw_ostream &OS,
114 const PrintingPolicy &Policy) const {
115 NestedNameSpec.getNestedNameSpecifier().print(OS, Policy);
116 NamedConcept.print(OS, Policy, Qual: TemplateName::Qualified::None);
117 if (hasExplicitTemplateArgs()) {
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, Out&: OS, /*IncludeType*/ false);
124 }
125 OS << ">";
126 }
127}
128
129const StreamingDiagnostic &clang::operator<<(const StreamingDiagnostic &DB,
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, Policy: PrintingPolicy(LO));
138 OS << '\'';
139 return DB << NameStr;
140}
141
142concepts::ExprRequirement::ExprRequirement(
143 Expr *E, bool IsSimple, SourceLocation NoexceptLoc,
144 ReturnTypeRequirement Req, SatisfactionStatus Status,
145 ConceptSpecializationExpr *SubstitutedConstraintExpr)
146 : Requirement(IsSimple ? RK_Simple : RK_Compound, Status == SS_Dependent,
147 Status == SS_Dependent &&
148 (E->containsUnexpandedParameterPack() ||
149 Req.containsUnexpandedParameterPack()),
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
160concepts::ExprRequirement::ExprRequirement(
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),
166 Status(SS_ExprSubstitutionFailure) {
167 assert((!IsSimple || (Req.isEmpty() && NoexceptLoc.isInvalid())) &&
168 "Simple requirement must not have a return type requirement or a "
169 "noexcept specification");
170}
171
172concepts::ExprRequirement::ReturnTypeRequirement::ReturnTypeRequirement(
173 TemplateParameterList *TPL)
174 : TypeConstraintInfo(TPL, false) {
175 assert(TPL->size() == 1);
176 const TypeConstraint *TC =
177 cast<TemplateTypeParmDecl>(Val: TPL->getParam(Idx: 0))->getTypeConstraint();
178 assert(TC &&
179 "TPL must have a template type parameter with a type constraint");
180 auto *Constraint =
181 cast<ConceptSpecializationExpr>(Val: TC->getImmediatelyDeclaredConstraint());
182 bool Dependent =
183 Constraint->getTemplateArgsAsWritten() &&
184 TemplateSpecializationType::anyInstantiationDependentTemplateArguments(
185 Args: Constraint->getTemplateArgsAsWritten()->arguments().drop_front(N: 1));
186 TypeConstraintInfo.setInt(Dependent ? true : false);
187}
188
189concepts::ExprRequirement::ReturnTypeRequirement::ReturnTypeRequirement(
190 TemplateParameterList *TPL, bool IsDependent)
191 : TypeConstraintInfo(TPL, IsDependent) {}
192
193concepts::TypeRequirement::TypeRequirement(TypeSourceInfo *T)
194 : Requirement(RK_Type, T->getType()->isInstantiationDependentType(),
195 T->getType()->containsUnexpandedParameterPack(),
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) {}
203