clang  10.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 
13 #include "clang/AST/ExprConcepts.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/ASTConcept.h"
16 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/Expr.h"
21 #include "clang/AST/TemplateBase.h"
22 #include "clang/AST/Type.h"
24 #include "llvm/Support/TrailingObjects.h"
25 #include <algorithm>
26 #include <utility>
27 #include <string>
28 
29 using namespace clang;
30 
32  NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc,
33  DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl,
34  ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten,
35  ArrayRef<TemplateArgument> ConvertedArgs,
36  const ConstraintSatisfaction *Satisfaction)
37  : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_RValue, OK_Ordinary,
38  /*TypeDependent=*/false,
39  // All the flags below are set in setTemplateArguments.
40  /*ValueDependent=*/!Satisfaction, /*InstantiationDependent=*/false,
41  /*ContainsUnexpandedParameterPacks=*/false),
42  ConceptReference(NNS, TemplateKWLoc, ConceptNameInfo, FoundDecl,
43  NamedConcept, ArgsAsWritten),
44  NumTemplateArgs(ConvertedArgs.size()),
45  Satisfaction(Satisfaction ?
46  ASTConstraintSatisfaction::Create(C, *Satisfaction) :
47  nullptr) {
48  setTemplateArguments(ConvertedArgs);
49  bool IsInstantiationDependent = false;
50  bool ContainsUnexpandedParameterPack = false;
51  for (const TemplateArgumentLoc& ArgLoc : ArgsAsWritten->arguments()) {
52  if (ArgLoc.getArgument().isInstantiationDependent())
53  IsInstantiationDependent = true;
54  if (ArgLoc.getArgument().containsUnexpandedParameterPack())
55  ContainsUnexpandedParameterPack = true;
56  if (ContainsUnexpandedParameterPack && IsInstantiationDependent)
57  break;
58  }
59 
60  // Currently guaranteed by the fact concepts can only be at namespace-scope.
61  assert(!NestedNameSpec ||
65  setInstantiationDependent(IsInstantiationDependent);
66  setContainsUnexpandedParameterPack(ContainsUnexpandedParameterPack);
67  assert((!isValueDependent() || isInstantiationDependent()) &&
68  "should not be value-dependent");
69 }
70 
72  unsigned NumTemplateArgs)
73  : Expr(ConceptSpecializationExprClass, Empty), ConceptReference(),
74  NumTemplateArgs(NumTemplateArgs) { }
75 
77  ArrayRef<TemplateArgument> Converted) {
78  assert(Converted.size() == NumTemplateArgs);
79  std::uninitialized_copy(Converted.begin(), Converted.end(),
80  getTrailingObjects<TemplateArgument>());
81 }
82 
87  DeclarationNameInfo ConceptNameInfo,
91  ArrayRef<TemplateArgument> ConvertedArgs,
93  void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
94  ConvertedArgs.size()));
95  return new (Buffer) ConceptSpecializationExpr(C, NNS, TemplateKWLoc,
96  ConceptNameInfo, FoundDecl,
97  NamedConcept, ArgsAsWritten,
98  ConvertedArgs, Satisfaction);
99 }
100 
103  ArrayRef<TemplateArgument> ConvertedArgs,
104  const ConstraintSatisfaction *Satisfaction, bool Dependent,
105  bool ContainsUnexpandedParameterPack)
106  : Expr(ConceptSpecializationExprClass, C.BoolTy, VK_RValue, OK_Ordinary,
107  /*TypeDependent=*/false,
108  /*ValueDependent=*/!Satisfaction, Dependent,
109  ContainsUnexpandedParameterPack),
111  DeclarationNameInfo(), NamedConcept,
112  NamedConcept, nullptr),
113  NumTemplateArgs(ConvertedArgs.size()),
114  Satisfaction(Satisfaction ?
115  ASTConstraintSatisfaction::Create(C, *Satisfaction) :
116  nullptr) {
117  setTemplateArguments(ConvertedArgs);
118 }
119 
123  ArrayRef<TemplateArgument> ConvertedArgs,
125  bool Dependent,
126  bool ContainsUnexpandedParameterPack) {
127  void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
128  ConvertedArgs.size()));
129  return new (Buffer) ConceptSpecializationExpr(
130  C, NamedConcept, ConvertedArgs, Satisfaction, Dependent,
131  ContainsUnexpandedParameterPack);
132 }
133 
136  unsigned NumTemplateArgs) {
137  void *Buffer = C.Allocate(totalSizeToAlloc<TemplateArgument>(
138  NumTemplateArgs));
139  return new (Buffer) ConceptSpecializationExpr(Empty, NumTemplateArgs);
140 }
141 
142 const TypeConstraint *
144  assert(isTypeConstraint());
145  auto TPL =
146  TypeConstraintInfo.getPointer().get<TemplateParameterList *>();
147  return cast<TemplateTypeParmDecl>(TPL->getParam(0))
148  ->getTypeConstraint();
149 }
150 
151 RequiresExpr::RequiresExpr(ASTContext &C, SourceLocation RequiresKWLoc,
152  RequiresExprBodyDecl *Body,
153  ArrayRef<ParmVarDecl *> LocalParameters,
155  SourceLocation RBraceLoc)
156  : Expr(RequiresExprClass, C.BoolTy, VK_RValue, OK_Ordinary,
157  /*TD=*/false, /*VD=*/false, /*ID=*/false,
158  /*ContainsUnexpandedParameterPack=*/false),
159  NumLocalParameters(LocalParameters.size()),
160  NumRequirements(Requirements.size()), Body(Body), RBraceLoc(RBraceLoc) {
161  RequiresExprBits.IsSatisfied = false;
162  RequiresExprBits.RequiresKWLoc = RequiresKWLoc;
163  bool Dependent = false;
164  bool ContainsUnexpandedParameterPack = false;
165  for (ParmVarDecl *P : LocalParameters) {
166  Dependent |= P->getType()->isInstantiationDependentType();
167  ContainsUnexpandedParameterPack |=
168  P->getType()->containsUnexpandedParameterPack();
169  }
170  RequiresExprBits.IsSatisfied = true;
171  for (concepts::Requirement *R : Requirements) {
172  Dependent |= R->isDependent();
173  ContainsUnexpandedParameterPack |= R->containsUnexpandedParameterPack();
174  if (!Dependent) {
175  RequiresExprBits.IsSatisfied = R->isSatisfied();
176  if (!RequiresExprBits.IsSatisfied)
177  break;
178  }
179  }
180  std::copy(LocalParameters.begin(), LocalParameters.end(),
181  getTrailingObjects<ParmVarDecl *>());
182  std::copy(Requirements.begin(), Requirements.end(),
183  getTrailingObjects<concepts::Requirement *>());
184  RequiresExprBits.IsSatisfied |= Dependent;
185  setValueDependent(Dependent);
186  setInstantiationDependent(Dependent);
187  setContainsUnexpandedParameterPack(ContainsUnexpandedParameterPack);
188 }
189 
190 RequiresExpr::RequiresExpr(ASTContext &C, EmptyShell Empty,
191  unsigned NumLocalParameters,
192  unsigned NumRequirements)
193  : Expr(RequiresExprClass, Empty), NumLocalParameters(NumLocalParameters),
194  NumRequirements(NumRequirements) { }
195 
196 RequiresExpr *
198  RequiresExprBodyDecl *Body,
199  ArrayRef<ParmVarDecl *> LocalParameters,
201  SourceLocation RBraceLoc) {
202  void *Mem =
203  C.Allocate(totalSizeToAlloc<ParmVarDecl *, concepts::Requirement *>(
204  LocalParameters.size(), Requirements.size()),
205  alignof(RequiresExpr));
206  return new (Mem) RequiresExpr(C, RequiresKWLoc, Body, LocalParameters,
207  Requirements, RBraceLoc);
208 }
209 
210 RequiresExpr *
212  unsigned NumLocalParameters, unsigned NumRequirements) {
213  void *Mem =
214  C.Allocate(totalSizeToAlloc<ParmVarDecl *, concepts::Requirement *>(
215  NumLocalParameters, NumRequirements),
216  alignof(RequiresExpr));
217  return new (Mem) RequiresExpr(C, Empty, NumLocalParameters, NumRequirements);
218 }
ConceptSpecializationExpr(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten, ArrayRef< TemplateArgument > ConvertedArgs, const ConstraintSatisfaction *Satisfaction)
void setValueDependent(bool VD)
Set whether this expression is value-dependent or not.
Definition: Expr.h:161
Defines the clang::ASTContext interface.
C Language Family Type Representation.
Defines the C++ template declaration subclasses.
StringRef P
static RequiresExpr * Create(ASTContext &C, SourceLocation RequiresKWLoc, RequiresExprBodyDecl *Body, ArrayRef< ParmVarDecl *> LocalParameters, ArrayRef< concepts::Requirement *> Requirements, SourceLocation RBraceLoc)
ASTConstraintSatisfaction * Satisfaction
Information about the satisfaction of the named concept with the given arguments. ...
Definition: ExprConcepts.h:56
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:603
Stores a list of template parameters for a TemplateDecl and its derived classes.
Definition: DeclTemplate.h:69
void setContainsUnexpandedParameterPack(bool PP=true)
Set the bit that describes whether this expression contains an unexpanded parameter pack...
Definition: Expr.h:229
llvm::ArrayRef< TemplateArgumentLoc > arguments() const
Definition: TemplateBase.h:631
Represents a parameter to a function.
Definition: Decl.h:1595
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
Definition: ExprConcepts.h:470
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:168
A C++ nested-name-specifier augmented with source location information.
An r-value expression (a pr-value in the C++11 taxonomy) produces a temporary value.
Definition: Specifiers.h:125
unsigned NumTemplateArgs
The number of template arguments in the tail-allocated list of converted template arguments...
Definition: ExprConcepts.h:51
An ordinary object is located at an address in memory.
Definition: Specifiers.h:141
bool isInstantiationDependent() const
Whether this nested name specifier involves a template parameter.
Expr()=delete
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:29
This represents one expression.
Definition: Expr.h:108
Represents the body of a requires-expression.
Definition: DeclCXX.h:1909
NestedNameSpecifierLoc NestedNameSpec
Definition: ASTConcept.h:103
NamedDecl * FoundDecl
The declaration found by name lookup when the expression was created.
Definition: ASTConcept.h:116
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition: ASTConcept.h:107
bool containsUnexpandedParameterPack() const
Whether this nested-name-specifier contains an unexpanded parameter pack (for C++11 variadic template...
#define false
Definition: stdbool.h:17
bool isInstantiationDependent() const
Whether this expression is instantiation-dependent, meaning that it depends in some way on a template...
Definition: Expr.h:200
Encodes a location in the source.
Common data class for constructs that reference concepts with template arguments. ...
Definition: ASTConcept.h:100
bool isValueDependent() const
isValueDependent - Determines whether this expression is value-dependent (C++ [temp.dep.constexpr]).
Definition: Expr.h:158
A static requirement that can be used in a requires-expression to check properties of types and expre...
Definition: ExprConcepts.h:145
static ConceptSpecializationExpr * Create(const ASTContext &C, NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten, ArrayRef< TemplateArgument > ConvertedArgs, const ConstraintSatisfaction *Satisfaction)
A placeholder type used to construct an empty shell of a type, that will be filled in later (e...
Definition: Stmt.h:1056
void * Allocate(size_t Size, unsigned Align=8) const
Definition: ASTContext.h:686
Dataflow Directional Tag Classes.
NestedNameSpecifier * getNestedNameSpecifier() const
Retrieve the nested-name-specifier to which this instance refers.
void setInstantiationDependent(bool ID)
Set whether this expression is instantiation-dependent or not.
Definition: Expr.h:205
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspnd...
RequiresExprBitfields RequiresExprBits
Definition: Stmt.h:1022
Location wrapper for a TemplateArgument.
Definition: TemplateBase.h:449
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:77
ConceptDecl * NamedConcept
The concept named.
Definition: ASTConcept.h:119
void setTemplateArguments(ArrayRef< TemplateArgument > Converted)
Set new template arguments for this concept specialization.
Defines the clang::SourceLocation class and associated facilities.
Defines Expressions and AST nodes for C++2a concepts.
Represents the specialization of a concept - evaluates to a prvalue of type bool. ...
Definition: ExprConcepts.h:40
const ASTTemplateArgumentListInfo * ArgsAsWritten
The template argument list source info used to specialize the concept.
Definition: ASTConcept.h:123
This represents a decl that may have a name.
Definition: Decl.h:223
CanQualType BoolTy
Definition: ASTContext.h:1017
This file provides AST data structures related to concepts.
static OMPLinearClause * Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation LParenLoc, OpenMPLinearClauseKind Modifier, SourceLocation ModifierLoc, SourceLocation ColonLoc, SourceLocation EndLoc, ArrayRef< Expr *> VL, ArrayRef< Expr *> PL, ArrayRef< Expr *> IL, Expr *Step, Expr *CalcStep, Stmt *PreInit, Expr *PostUpdate)
Creates clause with a list of variables VL and a linear step Step.