1//===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
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// This file implements the subclesses of Expr class declared in ExprCXX.h
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/ExprConcepts.h"
14#include "clang/AST/ASTConcept.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/ComputeDependence.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclarationName.h"
20#include "clang/AST/DependenceFlags.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/NestedNameSpecifier.h"
23#include "clang/AST/TemplateBase.h"
24#include "clang/AST/Type.h"
25#include "clang/Basic/SourceLocation.h"
26
27using namespace clang;
28
29ConceptSpecializationExpr::ConceptSpecializationExpr(
30 const ASTContext &C, ConceptReference *Loc,
31 ImplicitConceptSpecializationDecl *SpecDecl,
32 const ConstraintSatisfaction *Satisfaction)
33 : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_PRValue, OK_Ordinary),
34 ConceptRef(Loc), SpecDecl(SpecDecl),
35 Satisfaction(Satisfaction
36 ? ASTConstraintSatisfaction::Create(C, Satisfaction: *Satisfaction)
37 : nullptr) {
38 setDependence(computeDependence(E: this, /*ValueDependent=*/!Satisfaction));
39
40 // Currently guaranteed by the fact concepts can only be at namespace-scope.
41 assert(!Loc->getNestedNameSpecifierLoc() ||
42 (!Loc->getNestedNameSpecifierLoc()
43 .getNestedNameSpecifier()
44 .isInstantiationDependent() &&
45 !Loc->getNestedNameSpecifierLoc()
46 .getNestedNameSpecifier()
47 .containsUnexpandedParameterPack()));
48 assert((!isValueDependent() || isInstantiationDependent()) &&
49 "should not be value-dependent");
50}
51
52ConceptSpecializationExpr::ConceptSpecializationExpr(EmptyShell Empty)
53 : Expr(ConceptSpecializationExprClass, Empty) {}
54
55ConceptSpecializationExpr *
56ConceptSpecializationExpr::Create(const ASTContext &C, ConceptReference *Loc,
57 ImplicitConceptSpecializationDecl *SpecDecl,
58 const ConstraintSatisfaction *Satisfaction) {
59 return new (C) ConceptSpecializationExpr(C, Loc, SpecDecl, Satisfaction);
60}
61
62ConceptSpecializationExpr::ConceptSpecializationExpr(
63 const ASTContext &C, ConceptReference *Loc,
64 ImplicitConceptSpecializationDecl *SpecDecl,
65 const ConstraintSatisfaction *Satisfaction, bool Dependent,
66 bool ContainsUnexpandedParameterPack)
67 : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_PRValue, OK_Ordinary),
68 ConceptRef(Loc), SpecDecl(SpecDecl),
69 Satisfaction(Satisfaction
70 ? ASTConstraintSatisfaction::Create(C, Satisfaction: *Satisfaction)
71 : nullptr) {
72 ExprDependence D = ExprDependence::None;
73 if (!Satisfaction)
74 D |= ExprDependence::Value;
75 if (Dependent)
76 D |= ExprDependence::Instantiation;
77 if (ContainsUnexpandedParameterPack)
78 D |= ExprDependence::UnexpandedPack;
79 setDependence(D);
80}
81
82ConceptSpecializationExpr *
83ConceptSpecializationExpr::Create(const ASTContext &C, ConceptReference *Loc,
84 ImplicitConceptSpecializationDecl *SpecDecl,
85 const ConstraintSatisfaction *Satisfaction,
86 bool Dependent,
87 bool ContainsUnexpandedParameterPack) {
88 return new (C)
89 ConceptSpecializationExpr(C, Loc, SpecDecl, Satisfaction, Dependent,
90 ContainsUnexpandedParameterPack);
91}
92
93const TypeConstraint *
94concepts::ExprRequirement::ReturnTypeRequirement::getTypeConstraint() const {
95 assert(isTypeConstraint());
96 auto TPL = cast<TemplateParameterList *>(Val: TypeConstraintInfo.getPointer());
97 return cast<TemplateTypeParmDecl>(Val: TPL->getParam(Idx: 0))
98 ->getTypeConstraint();
99}
100
101// Search through the requirements, and see if any have a RecoveryExpr in it,
102// which means this RequiresExpr ALSO needs to be invalid.
103static bool RequirementContainsError(concepts::Requirement *R) {
104 if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(Val: R)) {
105 if (ExprReq->isExprSubstitutionFailure())
106 return true;
107 if (auto *E = ExprReq->getExpr())
108 return E->containsErrors();
109 return false;
110 }
111
112 if (auto *NestedReq = dyn_cast<concepts::NestedRequirement>(Val: R))
113 return !NestedReq->hasInvalidConstraint() &&
114 NestedReq->getConstraintExpr() &&
115 NestedReq->getConstraintExpr()->containsErrors();
116 return false;
117}
118
119RequiresExpr::RequiresExpr(ASTContext &C, SourceLocation RequiresKWLoc,
120 RequiresExprBodyDecl *Body, SourceLocation LParenLoc,
121 ArrayRef<ParmVarDecl *> LocalParameters,
122 SourceLocation RParenLoc,
123 ArrayRef<concepts::Requirement *> Requirements,
124 SourceLocation RBraceLoc)
125 : Expr(RequiresExprClass, C.BoolTy, VK_PRValue, OK_Ordinary),
126 NumLocalParameters(LocalParameters.size()),
127 NumRequirements(Requirements.size()), Body(Body), LParenLoc(LParenLoc),
128 RParenLoc(RParenLoc), RBraceLoc(RBraceLoc) {
129 RequiresExprBits.IsSatisfied = false;
130 RequiresExprBits.RequiresKWLoc = RequiresKWLoc;
131 bool Dependent = false;
132 bool ContainsUnexpandedParameterPack = false;
133 for (ParmVarDecl *P : LocalParameters) {
134 Dependent |= P->getType()->isInstantiationDependentType();
135 ContainsUnexpandedParameterPack |=
136 P->getType()->containsUnexpandedParameterPack();
137 }
138 RequiresExprBits.IsSatisfied = true;
139 for (concepts::Requirement *R : Requirements) {
140 Dependent |= R->isDependent();
141 ContainsUnexpandedParameterPack |= R->containsUnexpandedParameterPack();
142 if (!Dependent) {
143 RequiresExprBits.IsSatisfied = R->isSatisfied();
144 if (!RequiresExprBits.IsSatisfied)
145 break;
146 }
147
148 if (RequirementContainsError(R))
149 setDependence(getDependence() | ExprDependence::Error);
150 }
151 llvm::copy(Range&: LocalParameters, Out: getTrailingObjects<ParmVarDecl *>());
152 llvm::copy(Range&: Requirements, Out: getTrailingObjects<concepts::Requirement *>());
153 RequiresExprBits.IsSatisfied |= Dependent;
154 // FIXME: move the computing dependency logic to ComputeDependence.h
155 if (ContainsUnexpandedParameterPack)
156 setDependence(getDependence() | ExprDependence::UnexpandedPack);
157 // FIXME: this is incorrect for cases where we have a non-dependent
158 // requirement, but its parameters are instantiation-dependent. RequiresExpr
159 // should be instantiation-dependent if it has instantiation-dependent
160 // parameters.
161 if (Dependent)
162 setDependence(getDependence() | ExprDependence::ValueInstantiation);
163}
164
165RequiresExpr::RequiresExpr(ASTContext &C, EmptyShell Empty,
166 unsigned NumLocalParameters,
167 unsigned NumRequirements)
168 : Expr(RequiresExprClass, Empty), NumLocalParameters(NumLocalParameters),
169 NumRequirements(NumRequirements) { }
170
171RequiresExpr *RequiresExpr::Create(
172 ASTContext &C, SourceLocation RequiresKWLoc, RequiresExprBodyDecl *Body,
173 SourceLocation LParenLoc, ArrayRef<ParmVarDecl *> LocalParameters,
174 SourceLocation RParenLoc, ArrayRef<concepts::Requirement *> Requirements,
175 SourceLocation RBraceLoc) {
176 void *Mem =
177 C.Allocate(Size: totalSizeToAlloc<ParmVarDecl *, concepts::Requirement *>(
178 Counts: LocalParameters.size(), Counts: Requirements.size()),
179 Align: alignof(RequiresExpr));
180 return new (Mem)
181 RequiresExpr(C, RequiresKWLoc, Body, LParenLoc, LocalParameters,
182 RParenLoc, Requirements, RBraceLoc);
183}
184
185RequiresExpr *
186RequiresExpr::Create(ASTContext &C, EmptyShell Empty,
187 unsigned NumLocalParameters, unsigned NumRequirements) {
188 void *Mem =
189 C.Allocate(Size: totalSizeToAlloc<ParmVarDecl *, concepts::Requirement *>(
190 Counts: NumLocalParameters, Counts: NumRequirements),
191 Align: alignof(RequiresExpr));
192 return new (Mem) RequiresExpr(C, Empty, NumLocalParameters, NumRequirements);
193}
194