clang  16.0.0git
ASTConcept.h
Go to the documentation of this file.
1 //===--- ASTConcept.h - 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 provides AST data structures related to concepts.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_ASTCONCEPT_H
15 #define LLVM_CLANG_AST_ASTCONCEPT_H
16 
17 #include "clang/AST/Expr.h"
19 #include "llvm/ADT/PointerUnion.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include <utility>
22 
23 namespace clang {
24 class ConceptDecl;
25 
26 /// The result of a constraint satisfaction check, containing the necessary
27 /// information to diagnose an unsatisfied constraint.
28 class ConstraintSatisfaction : public llvm::FoldingSetNode {
29  // The template-like entity that 'owns' the constraint checked here (can be a
30  // constrained entity or a concept).
31  const NamedDecl *ConstraintOwner = nullptr;
33 
34 public:
35 
36  ConstraintSatisfaction() = default;
37 
38  ConstraintSatisfaction(const NamedDecl *ConstraintOwner,
39  ArrayRef<TemplateArgument> TemplateArgs) :
40  ConstraintOwner(ConstraintOwner), TemplateArgs(TemplateArgs.begin(),
41  TemplateArgs.end()) { }
42 
43  using SubstitutionDiagnostic = std::pair<SourceLocation, StringRef>;
44  using Detail = llvm::PointerUnion<Expr *, SubstitutionDiagnostic *>;
45 
46  bool IsSatisfied = false;
47  bool ContainsErrors = false;
48 
49  /// \brief Pairs of unsatisfied atomic constraint expressions along with the
50  /// substituted constraint expr, if the template arguments could be
51  /// substituted into them, or a diagnostic if substitution resulted in an
52  /// invalid expression.
54 
55  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C) {
56  Profile(ID, C, ConstraintOwner, TemplateArgs);
57  }
58 
59  static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C,
60  const NamedDecl *ConstraintOwner,
61  ArrayRef<TemplateArgument> TemplateArgs);
62 };
63 
64 /// Pairs of unsatisfied atomic constraint expressions along with the
65 /// substituted constraint expr, if the template arguments could be
66 /// substituted into them, or a diagnostic if substitution resulted in
67 /// an invalid expression.
69  std::pair<const Expr *,
70  llvm::PointerUnion<Expr *,
71  std::pair<SourceLocation, StringRef> *>>;
72 
73 /// \brief The result of a constraint satisfaction check, containing the
74 /// necessary information to diagnose an unsatisfied constraint.
75 ///
76 /// This is safe to store in an AST node, as opposed to ConstraintSatisfaction.
78  llvm::TrailingObjects<ASTConstraintSatisfaction,
79  UnsatisfiedConstraintRecord> {
81  bool IsSatisfied : 1;
82  bool ContainsErrors : 1;
83 
85  return getTrailingObjects<UnsatisfiedConstraintRecord>();
86  }
87 
89  return getTrailingObjects<UnsatisfiedConstraintRecord>() + NumRecords;
90  }
91 
93  const ConstraintSatisfaction &Satisfaction);
94 
96  Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction);
97 };
98 
99 /// \brief Common data class for constructs that reference concepts with
100 /// template arguments.
102 protected:
103  // \brief The optional nested name specifier used when naming the concept.
105 
106  /// \brief The location of the template keyword, if specified when naming the
107  /// concept.
109 
110  /// \brief The concept name used.
112 
113  /// \brief The declaration found by name lookup when the expression was
114  /// created.
115  /// Can differ from NamedConcept when, for example, the concept was found
116  /// through a UsingShadowDecl.
118 
119  /// \brief The concept named.
121 
122  /// \brief The template argument list source info used to specialize the
123  /// concept.
125 
126 public:
128  DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl,
132  ConceptName(ConceptNameInfo), FoundDecl(FoundDecl),
134 
136  : FoundDecl(nullptr), NamedConcept(nullptr), ArgsAsWritten(nullptr) {}
137 
139  return NestedNameSpec;
140  }
141 
143 
145  return getConceptNameInfo().getLoc();
146  }
147 
149 
151  return FoundDecl;
152  }
153 
155  return NamedConcept;
156  }
157 
159  return ArgsAsWritten;
160  }
161 
162  /// \brief Whether or not template arguments were explicitly specified in the
163  /// concept reference (they might not be in type constraints, for example)
164  bool hasExplicitTemplateArgs() const {
165  return ArgsAsWritten != nullptr;
166  }
167 };
168 
170  /// \brief The immediately-declared constraint expression introduced by this
171  /// type-constraint.
172  Expr *ImmediatelyDeclaredConstraint = nullptr;
173 
174 public:
176  DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl,
179  Expr *ImmediatelyDeclaredConstraint) :
180  ConceptReference(NNS, /*TemplateKWLoc=*/SourceLocation(), ConceptNameInfo,
182  ImmediatelyDeclaredConstraint(ImmediatelyDeclaredConstraint) {}
183 
184  /// \brief Get the immediately-declared constraint expression introduced by
185  /// this type-constraint, that is - the constraint expression that is added to
186  /// the associated constraints of the enclosing declaration in practice.
188  return ImmediatelyDeclaredConstraint;
189  }
190 
191  void print(llvm::raw_ostream &OS, PrintingPolicy Policy) const;
192 };
193 
194 } // clang
195 
196 #endif // LLVM_CLANG_AST_ASTCONCEPT_H
clang::ASTConstraintSatisfaction
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:77
clang::ASTConstraintSatisfaction::ContainsErrors
bool ContainsErrors
Definition: ASTConcept.h:82
clang::ConceptReference::ConceptReference
ConceptReference()
Definition: ASTConcept.h:135
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:247
clang::ConceptReference::getNamedConcept
ConceptDecl * getNamedConcept() const
Definition: ASTConcept.h:154
clang::TypeConstraint::getImmediatelyDeclaredConstraint
Expr * getImmediatelyDeclaredConstraint() const
Get the immediately-declared constraint expression introduced by this type-constraint,...
Definition: ASTConcept.h:187
clang::PrintingPolicy
Describes how types, statements, expressions, and declarations should be printed.
Definition: PrettyPrinter.h:57
clang::ConstraintSatisfaction::Details
llvm::SmallVector< std::pair< const Expr *, Detail >, 4 > Details
Pairs of unsatisfied atomic constraint expressions along with the substituted constraint expr,...
Definition: ASTConcept.h:53
size_t
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c-base.h:118
clang::UnsatisfiedConstraintRecord
std::pair< const Expr *, llvm::PointerUnion< Expr *, std::pair< SourceLocation, StringRef > * > > UnsatisfiedConstraintRecord
Pairs of unsatisfied atomic constraint expressions along with the substituted constraint expr,...
Definition: ASTConcept.h:71
clang::ConceptReference::getTemplateArgsAsWritten
const ASTTemplateArgumentListInfo * getTemplateArgsAsWritten() const
Definition: ASTConcept.h:158
clang::ConceptDecl
Declaration of a C++20 concept.
Definition: DeclTemplate.h:3262
clang::ConstraintSatisfaction::ConstraintSatisfaction
ConstraintSatisfaction()=default
clang::ConceptReference::ConceptName
DeclarationNameInfo ConceptName
The concept name used.
Definition: ASTConcept.h:111
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:209
clang::ConceptReference::NamedConcept
ConceptDecl * NamedConcept
The concept named.
Definition: ASTConcept.h:120
clang::ConceptReference::getTemplateKWLoc
SourceLocation getTemplateKWLoc() const
Definition: ASTConcept.h:148
clang::RISCV::Policy
Policy
Definition: RISCVVIntrinsicUtils.h:96
Expr.h
clang::ConceptReference::hasExplicitTemplateArgs
bool hasExplicitTemplateArgs() const
Whether or not template arguments were explicitly specified in the concept reference (they might not ...
Definition: ASTConcept.h:164
clang::ConceptReference::getFoundDecl
NamedDecl * getFoundDecl() const
Definition: ASTConcept.h:150
clang::TypeConstraint::print
void print(llvm::raw_ostream &OS, PrintingPolicy Policy) const
Definition: DeclTemplate.cpp:1544
clang::NestedNameSpecifierLoc
A C++ nested-name-specifier augmented with source location information.
Definition: NestedNameSpecifier.h:243
clang::ASTConstraintSatisfaction::begin
const UnsatisfiedConstraintRecord * begin() const
Definition: ASTConcept.h:84
clang::ASTConstraintSatisfaction::end
const UnsatisfiedConstraintRecord * end() const
Definition: ASTConcept.h:88
clang::ConstraintSatisfaction::IsSatisfied
bool IsSatisfied
Definition: ASTConcept.h:46
clang::ConceptReference::ArgsAsWritten
const ASTTemplateArgumentListInfo * ArgsAsWritten
The template argument list source info used to specialize the concept.
Definition: ASTConcept.h:124
SourceLocation.h
clang::ConceptReference::NestedNameSpec
NestedNameSpecifierLoc NestedNameSpec
Definition: ASTConcept.h:104
clang::ConstraintSatisfaction::Profile
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &C)
Definition: ASTConcept.h:55
clang::ConceptReference
Common data class for constructs that reference concepts with template arguments.
Definition: ASTConcept.h:101
clang::ASTConstraintSatisfaction::IsSatisfied
bool IsSatisfied
Definition: ASTConcept.h:81
llvm::ArrayRef< TemplateArgument >
clang::ConceptReference::ConceptReference
ConceptReference(NestedNameSpecifierLoc NNS, SourceLocation TemplateKWLoc, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten)
Definition: ASTConcept.h:127
clang::Builtin::ID
ID
Definition: Builtins.h:52
clang::ConceptReference::getConceptNameLoc
SourceLocation getConceptNameLoc() const
Definition: ASTConcept.h:144
clang
Definition: CalledOnceCheck.h:17
clang::TypeConstraint::TypeConstraint
TypeConstraint(NestedNameSpecifierLoc NNS, DeclarationNameInfo ConceptNameInfo, NamedDecl *FoundDecl, ConceptDecl *NamedConcept, const ASTTemplateArgumentListInfo *ArgsAsWritten, Expr *ImmediatelyDeclaredConstraint)
Definition: ASTConcept.h:175
clang::ConstraintSatisfaction::Detail
llvm::PointerUnion< Expr *, SubstitutionDiagnostic * > Detail
Definition: ASTConcept.h:44
clang::ConstraintSatisfaction::ContainsErrors
bool ContainsErrors
Definition: ASTConcept.h:47
clang::ASTConstraintSatisfaction::ASTConstraintSatisfaction
ASTConstraintSatisfaction(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:22
clang::ConstraintSatisfaction::ConstraintSatisfaction
ConstraintSatisfaction(const NamedDecl *ConstraintOwner, ArrayRef< TemplateArgument > TemplateArgs)
Definition: ASTConcept.h:38
clang::ConstraintSatisfaction::SubstitutionDiagnostic
std::pair< SourceLocation, StringRef > SubstitutionDiagnostic
Definition: ASTConcept.h:43
clang::ConstraintSatisfaction
The result of a constraint satisfaction check, containing the necessary information to diagnose an un...
Definition: ASTConcept.h:28
clang::ConceptReference::getNestedNameSpecifierLoc
const NestedNameSpecifierLoc & getNestedNameSpecifierLoc() const
Definition: ASTConcept.h:138
clang::ConceptReference::getConceptNameInfo
const DeclarationNameInfo & getConceptNameInfo() const
Definition: ASTConcept.h:142
clang::ASTTemplateArgumentListInfo
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Definition: TemplateBase.h:612
clang::ConceptReference::FoundDecl
NamedDecl * FoundDecl
The declaration found by name lookup when the expression was created.
Definition: ASTConcept.h:117
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::DeclarationNameInfo::getLoc
SourceLocation getLoc() const
getLoc - Returns the main location of the declaration name.
Definition: DeclarationName.h:796
clang::DeclarationNameInfo
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspnd...
Definition: DeclarationName.h:767
clang::ASTConstraintSatisfaction::NumRecords
std::size_t NumRecords
Definition: ASTConcept.h:80
clang::TypeConstraint
Definition: ASTConcept.h:169
clang::ConceptReference::TemplateKWLoc
SourceLocation TemplateKWLoc
The location of the template keyword, if specified when naming the concept.
Definition: ASTConcept.h:108
clang::ASTConstraintSatisfaction::Create
static ASTConstraintSatisfaction * Create(const ASTContext &C, const ConstraintSatisfaction &Satisfaction)
Definition: ASTConcept.cpp:51