clang 22.0.0git
ExprConcepts.cpp
Go to the documentation of this file.
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
17#include "clang/AST/Decl.h"
21#include "clang/AST/Expr.h"
24#include "clang/AST/Type.h"
26
27using namespace clang;
28
29ConceptSpecializationExpr::ConceptSpecializationExpr(
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)
37 : nullptr) {
38 setDependence(computeDependence(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
58 const ConstraintSatisfaction *Satisfaction) {
59 return new (C) ConceptSpecializationExpr(C, Loc, SpecDecl, Satisfaction);
60}
61
62ConceptSpecializationExpr::ConceptSpecializationExpr(
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)
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;
80}
81
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 *
95 assert(isTypeConstraint());
96 auto TPL = cast<TemplateParameterList *>(TypeConstraintInfo.getPointer());
97 return cast<TemplateTypeParmDecl>(TPL->getParam(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.
104 if (auto *ExprReq = dyn_cast<concepts::ExprRequirement>(R))
105 return ExprReq->getExpr() && ExprReq->getExpr()->containsErrors();
106
107 if (auto *NestedReq = dyn_cast<concepts::NestedRequirement>(R))
108 return !NestedReq->hasInvalidConstraint() &&
109 NestedReq->getConstraintExpr() &&
110 NestedReq->getConstraintExpr()->containsErrors();
111 return false;
112}
113
114RequiresExpr::RequiresExpr(ASTContext &C, SourceLocation RequiresKWLoc,
115 RequiresExprBodyDecl *Body, SourceLocation LParenLoc,
116 ArrayRef<ParmVarDecl *> LocalParameters,
117 SourceLocation RParenLoc,
119 SourceLocation RBraceLoc)
120 : Expr(RequiresExprClass, C.BoolTy, VK_PRValue, OK_Ordinary),
121 NumLocalParameters(LocalParameters.size()),
122 NumRequirements(Requirements.size()), Body(Body), LParenLoc(LParenLoc),
123 RParenLoc(RParenLoc), RBraceLoc(RBraceLoc) {
124 RequiresExprBits.IsSatisfied = false;
125 RequiresExprBits.RequiresKWLoc = RequiresKWLoc;
126 bool Dependent = false;
127 bool ContainsUnexpandedParameterPack = false;
128 for (ParmVarDecl *P : LocalParameters) {
129 Dependent |= P->getType()->isInstantiationDependentType();
130 ContainsUnexpandedParameterPack |=
131 P->getType()->containsUnexpandedParameterPack();
132 }
133 RequiresExprBits.IsSatisfied = true;
134 for (concepts::Requirement *R : Requirements) {
135 Dependent |= R->isDependent();
136 ContainsUnexpandedParameterPack |= R->containsUnexpandedParameterPack();
137 if (!Dependent) {
138 RequiresExprBits.IsSatisfied = R->isSatisfied();
139 if (!RequiresExprBits.IsSatisfied)
140 break;
141 }
142
144 setDependence(getDependence() | ExprDependence::Error);
145 }
146 llvm::copy(LocalParameters, getTrailingObjects<ParmVarDecl *>());
147 llvm::copy(Requirements, getTrailingObjects<concepts::Requirement *>());
148 RequiresExprBits.IsSatisfied |= Dependent;
149 // FIXME: move the computing dependency logic to ComputeDependence.h
150 if (ContainsUnexpandedParameterPack)
151 setDependence(getDependence() | ExprDependence::UnexpandedPack);
152 // FIXME: this is incorrect for cases where we have a non-dependent
153 // requirement, but its parameters are instantiation-dependent. RequiresExpr
154 // should be instantiation-dependent if it has instantiation-dependent
155 // parameters.
156 if (Dependent)
157 setDependence(getDependence() | ExprDependence::ValueInstantiation);
158}
159
160RequiresExpr::RequiresExpr(ASTContext &C, EmptyShell Empty,
161 unsigned NumLocalParameters,
162 unsigned NumRequirements)
163 : Expr(RequiresExprClass, Empty), NumLocalParameters(NumLocalParameters),
164 NumRequirements(NumRequirements) { }
165
167 ASTContext &C, SourceLocation RequiresKWLoc, RequiresExprBodyDecl *Body,
168 SourceLocation LParenLoc, ArrayRef<ParmVarDecl *> LocalParameters,
170 SourceLocation RBraceLoc) {
171 void *Mem =
172 C.Allocate(totalSizeToAlloc<ParmVarDecl *, concepts::Requirement *>(
173 LocalParameters.size(), Requirements.size()),
174 alignof(RequiresExpr));
175 return new (Mem)
176 RequiresExpr(C, RequiresKWLoc, Body, LParenLoc, LocalParameters,
177 RParenLoc, Requirements, RBraceLoc);
178}
179
182 unsigned NumLocalParameters, unsigned NumRequirements) {
183 void *Mem =
184 C.Allocate(totalSizeToAlloc<ParmVarDecl *, concepts::Requirement *>(
185 NumLocalParameters, NumRequirements),
186 alignof(RequiresExpr));
187 return new (Mem) RequiresExpr(C, Empty, NumLocalParameters, NumRequirements);
188}
This file provides AST data structures related to concepts.
Defines the clang::ASTContext interface.
StringRef P
const Decl * D
Defines the C++ template declaration subclasses.
static bool RequirementContainsError(concepts::Requirement *R)
Defines Expressions and AST nodes for C++2a concepts.
SourceLocation Loc
Definition: SemaObjC.cpp:754
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
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
Represents the specialization of a concept - evaluates to a prvalue of type bool.
Definition: ExprConcepts.h:42
static ConceptSpecializationExpr * Create(const ASTContext &C, ConceptReference *ConceptRef, ImplicitConceptSpecializationDecl *SpecDecl, const ConstraintSatisfaction *Satisfaction)
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:37
This represents one expression.
Definition: Expr.h:112
void setDependence(ExprDependence Deps)
Each concrete expr subclass is expected to compute its dependence and call this in the constructor.
Definition: Expr.h:137
Represents a parameter to a function.
Definition: Decl.h:1789
Represents the body of a requires-expression.
Definition: DeclCXX.h:2098
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Definition: ExprConcepts.h:505
static RequiresExpr * Create(ASTContext &C, SourceLocation RequiresKWLoc, RequiresExprBodyDecl *Body, SourceLocation LParenLoc, ArrayRef< ParmVarDecl * > LocalParameters, SourceLocation RParenLoc, ArrayRef< concepts::Requirement * > Requirements, SourceLocation RBraceLoc)
Encodes a location in the source.
Models the abbreviated syntax to constrain a template type parameter: template <convertible_to<string...
Definition: ASTConcept.h:223
A static requirement that can be used in a requires-expression to check properties of types and expre...
Definition: ExprConcepts.h:170
The JSON file list parser is used to communicate input to InstallAPI.
@ OK_Ordinary
An ordinary object is located at an address in memory.
Definition: Specifiers.h:151
ExprDependence computeDependence(FullExpr *E)
@ Create
'create' clause, allowed on Compute and Combined constructs, plus 'data', 'enter data',...
@ Dependent
Parse the block as a dependent block, which may be used in some template instantiations but not other...
@ VK_PRValue
A pr-value expression (in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:135
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:91
A placeholder type used to construct an empty shell of a type, that will be filled in later (e....
Definition: Stmt.h:1412