ComparisonCategories.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ComparisonCategories.h - Three Way Comparison Data -------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the Comparison Category enum and data types, which
  15. // store the types and expressions needed to support operator<=>
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_COMPARISONCATEGORIES_H
  19. #define LLVM_CLANG_AST_COMPARISONCATEGORIES_H
  20. #include "clang/Basic/LLVM.h"
  21. #include "llvm/ADT/APSInt.h"
  22. #include "llvm/ADT/DenseMap.h"
  23. #include <array>
  24. #include <cassert>
  25. #include <optional>
  26. #include <vector>
  27. namespace llvm {
  28. class StringRef;
  29. class APSInt;
  30. }
  31. namespace clang {
  32. class ASTContext;
  33. class VarDecl;
  34. class CXXRecordDecl;
  35. class Sema;
  36. class QualType;
  37. class NamespaceDecl;
  38. /// An enumeration representing the different comparison categories
  39. /// types.
  40. ///
  41. /// C++2a [cmp.categories.pre] The types weak_equality, strong_equality,
  42. /// partial_ordering, weak_ordering, and strong_ordering are collectively
  43. /// termed the comparison category types.
  44. enum class ComparisonCategoryType : unsigned char {
  45. PartialOrdering,
  46. WeakOrdering,
  47. StrongOrdering,
  48. First = PartialOrdering,
  49. Last = StrongOrdering
  50. };
  51. /// Determine the common comparison type, as defined in C++2a
  52. /// [class.spaceship]p4.
  53. inline ComparisonCategoryType commonComparisonType(ComparisonCategoryType A,
  54. ComparisonCategoryType B) {
  55. return A < B ? A : B;
  56. }
  57. /// Get the comparison category that should be used when comparing values of
  58. /// type \c T.
  59. std::optional<ComparisonCategoryType>
  60. getComparisonCategoryForBuiltinCmp(QualType T);
  61. /// An enumeration representing the possible results of a three-way
  62. /// comparison. These values map onto instances of comparison category types
  63. /// defined in the standard library. e.g. 'std::strong_ordering::less'.
  64. enum class ComparisonCategoryResult : unsigned char {
  65. Equal,
  66. Equivalent,
  67. Less,
  68. Greater,
  69. Unordered,
  70. Last = Unordered
  71. };
  72. class ComparisonCategoryInfo {
  73. friend class ComparisonCategories;
  74. friend class Sema;
  75. public:
  76. ComparisonCategoryInfo(const ASTContext &Ctx, CXXRecordDecl *RD,
  77. ComparisonCategoryType Kind)
  78. : Ctx(Ctx), Record(RD), Kind(Kind) {}
  79. struct ValueInfo {
  80. ComparisonCategoryResult Kind;
  81. VarDecl *VD;
  82. ValueInfo(ComparisonCategoryResult Kind, VarDecl *VD)
  83. : Kind(Kind), VD(VD) {}
  84. /// True iff we've successfully evaluated the variable as a constant
  85. /// expression and extracted its integer value.
  86. bool hasValidIntValue() const;
  87. /// Get the constant integer value used by this variable to represent
  88. /// the comparison category result type.
  89. llvm::APSInt getIntValue() const;
  90. };
  91. private:
  92. const ASTContext &Ctx;
  93. /// A map containing the comparison category result decls from the
  94. /// standard library. The key is a value of ComparisonCategoryResult.
  95. mutable llvm::SmallVector<
  96. ValueInfo, static_cast<unsigned>(ComparisonCategoryResult::Last) + 1>
  97. Objects;
  98. /// Lookup the ValueInfo struct for the specified ValueKind. If the
  99. /// VarDecl for the value cannot be found, nullptr is returned.
  100. ///
  101. /// If the ValueInfo does not have a valid integer value the variable
  102. /// is evaluated as a constant expression to determine that value.
  103. ValueInfo *lookupValueInfo(ComparisonCategoryResult ValueKind) const;
  104. public:
  105. /// The declaration for the comparison category type from the
  106. /// standard library.
  107. const CXXRecordDecl *Record = nullptr;
  108. /// The Kind of the comparison category type
  109. ComparisonCategoryType Kind;
  110. public:
  111. QualType getType() const;
  112. const ValueInfo *getValueInfo(ComparisonCategoryResult ValueKind) const {
  113. ValueInfo *Info = lookupValueInfo(ValueKind);
  114. assert(Info &&
  115. "comparison category does not contain the specified result kind");
  116. assert(Info->hasValidIntValue() &&
  117. "couldn't determine the integer constant for this value");
  118. return Info;
  119. }
  120. /// True iff the comparison is "strong". i.e. it checks equality and
  121. /// not equivalence.
  122. bool isStrong() const {
  123. using CCK = ComparisonCategoryType;
  124. return Kind == CCK::StrongOrdering;
  125. }
  126. /// True iff the comparison is not totally ordered.
  127. bool isPartial() const {
  128. using CCK = ComparisonCategoryType;
  129. return Kind == CCK::PartialOrdering;
  130. }
  131. /// Converts the specified result kind into the correct result kind
  132. /// for this category. Specifically it lowers strong equality results to
  133. /// weak equivalence if needed.
  134. ComparisonCategoryResult makeWeakResult(ComparisonCategoryResult Res) const {
  135. using CCR = ComparisonCategoryResult;
  136. if (!isStrong() && Res == CCR::Equal)
  137. return CCR::Equivalent;
  138. return Res;
  139. }
  140. const ValueInfo *getEqualOrEquiv() const {
  141. return getValueInfo(makeWeakResult(ComparisonCategoryResult::Equal));
  142. }
  143. const ValueInfo *getLess() const {
  144. return getValueInfo(ComparisonCategoryResult::Less);
  145. }
  146. const ValueInfo *getGreater() const {
  147. return getValueInfo(ComparisonCategoryResult::Greater);
  148. }
  149. const ValueInfo *getUnordered() const {
  150. assert(isPartial());
  151. return getValueInfo(ComparisonCategoryResult::Unordered);
  152. }
  153. };
  154. class ComparisonCategories {
  155. public:
  156. static StringRef getCategoryString(ComparisonCategoryType Kind);
  157. static StringRef getResultString(ComparisonCategoryResult Kind);
  158. /// Return the list of results which are valid for the specified
  159. /// comparison category type.
  160. static std::vector<ComparisonCategoryResult>
  161. getPossibleResultsForType(ComparisonCategoryType Type);
  162. /// Return the comparison category information for the category
  163. /// specified by 'Kind'.
  164. const ComparisonCategoryInfo &getInfo(ComparisonCategoryType Kind) const {
  165. const ComparisonCategoryInfo *Result = lookupInfo(Kind);
  166. assert(Result != nullptr &&
  167. "information for specified comparison category has not been built");
  168. return *Result;
  169. }
  170. /// Return the comparison category information as specified by
  171. /// `getCategoryForType(Ty)`. If the information is not already cached,
  172. /// the declaration is looked up and a cache entry is created.
  173. /// NOTE: Lookup is expected to succeed. Use lookupInfo if failure is
  174. /// possible.
  175. const ComparisonCategoryInfo &getInfoForType(QualType Ty) const;
  176. public:
  177. /// Return the cached comparison category information for the
  178. /// specified 'Kind'. If no cache entry is present the comparison category
  179. /// type is looked up. If lookup fails nullptr is returned. Otherwise, a
  180. /// new cache entry is created and returned
  181. const ComparisonCategoryInfo *lookupInfo(ComparisonCategoryType Kind) const;
  182. ComparisonCategoryInfo *lookupInfo(ComparisonCategoryType Kind) {
  183. const auto &This = *this;
  184. return const_cast<ComparisonCategoryInfo *>(This.lookupInfo(Kind));
  185. }
  186. const ComparisonCategoryInfo *lookupInfoForType(QualType Ty) const;
  187. private:
  188. friend class ASTContext;
  189. explicit ComparisonCategories(const ASTContext &Ctx) : Ctx(Ctx) {}
  190. const ASTContext &Ctx;
  191. /// A map from the ComparisonCategoryType (represented as 'char') to the
  192. /// cached information for the specified category.
  193. mutable llvm::DenseMap<char, ComparisonCategoryInfo> Data;
  194. mutable NamespaceDecl *StdNS = nullptr;
  195. };
  196. } // namespace clang
  197. #endif
  198. #ifdef __GNUC__
  199. #pragma GCC diagnostic pop
  200. #endif