ComparisonCategories.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //===- ComparisonCategories.cpp - Three Way Comparison Data -----*- 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. // This file defines the Comparison Category enum and data types, which
  10. // store the types and expressions needed to support operator<=>
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/ComparisonCategories.h"
  14. #include "clang/AST/Decl.h"
  15. #include "clang/AST/DeclCXX.h"
  16. #include "clang/AST/Type.h"
  17. #include "llvm/ADT/SmallVector.h"
  18. using namespace clang;
  19. bool ComparisonCategoryInfo::ValueInfo::hasValidIntValue() const {
  20. assert(VD && "must have var decl");
  21. if (!VD->checkInitIsICE())
  22. return false;
  23. // Before we attempt to get the value of the first field, ensure that we
  24. // actually have one (and only one) field.
  25. auto *Record = VD->getType()->getAsCXXRecordDecl();
  26. if (std::distance(Record->field_begin(), Record->field_end()) != 1 ||
  27. !Record->field_begin()->getType()->isIntegralOrEnumerationType())
  28. return false;
  29. return true;
  30. }
  31. /// Attempt to determine the integer value used to represent the comparison
  32. /// category result by evaluating the initializer for the specified VarDecl as
  33. /// a constant expression and retreiving the value of the class's first
  34. /// (and only) field.
  35. ///
  36. /// Note: The STL types are expected to have the form:
  37. /// struct X { T value; };
  38. /// where T is an integral or enumeration type.
  39. llvm::APSInt ComparisonCategoryInfo::ValueInfo::getIntValue() const {
  40. assert(hasValidIntValue() && "must have a valid value");
  41. return VD->evaluateValue()->getStructField(0).getInt();
  42. }
  43. ComparisonCategoryInfo::ValueInfo *ComparisonCategoryInfo::lookupValueInfo(
  44. ComparisonCategoryResult ValueKind) const {
  45. // Check if we already have a cache entry for this value.
  46. auto It = llvm::find_if(
  47. Objects, [&](ValueInfo const &Info) { return Info.Kind == ValueKind; });
  48. if (It != Objects.end())
  49. return &(*It);
  50. // We don't have a cached result. Lookup the variable declaration and create
  51. // a new entry representing it.
  52. DeclContextLookupResult Lookup = Record->getCanonicalDecl()->lookup(
  53. &Ctx.Idents.get(ComparisonCategories::getResultString(ValueKind)));
  54. if (Lookup.size() != 1 || !isa<VarDecl>(Lookup.front()))
  55. return nullptr;
  56. Objects.emplace_back(ValueKind, cast<VarDecl>(Lookup.front()));
  57. return &Objects.back();
  58. }
  59. static const NamespaceDecl *lookupStdNamespace(const ASTContext &Ctx,
  60. NamespaceDecl *&StdNS) {
  61. if (!StdNS) {
  62. DeclContextLookupResult Lookup =
  63. Ctx.getTranslationUnitDecl()->lookup(&Ctx.Idents.get("std"));
  64. if (Lookup.size() == 1)
  65. StdNS = dyn_cast<NamespaceDecl>(Lookup.front());
  66. }
  67. return StdNS;
  68. }
  69. static CXXRecordDecl *lookupCXXRecordDecl(const ASTContext &Ctx,
  70. const NamespaceDecl *StdNS,
  71. ComparisonCategoryType Kind) {
  72. StringRef Name = ComparisonCategories::getCategoryString(Kind);
  73. DeclContextLookupResult Lookup = StdNS->lookup(&Ctx.Idents.get(Name));
  74. if (Lookup.size() == 1)
  75. if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Lookup.front()))
  76. return RD;
  77. return nullptr;
  78. }
  79. const ComparisonCategoryInfo *
  80. ComparisonCategories::lookupInfo(ComparisonCategoryType Kind) const {
  81. auto It = Data.find(static_cast<char>(Kind));
  82. if (It != Data.end())
  83. return &It->second;
  84. if (const NamespaceDecl *NS = lookupStdNamespace(Ctx, StdNS))
  85. if (CXXRecordDecl *RD = lookupCXXRecordDecl(Ctx, NS, Kind))
  86. return &Data.try_emplace((char)Kind, Ctx, RD, Kind).first->second;
  87. return nullptr;
  88. }
  89. const ComparisonCategoryInfo *
  90. ComparisonCategories::lookupInfoForType(QualType Ty) const {
  91. assert(!Ty.isNull() && "type must be non-null");
  92. using CCT = ComparisonCategoryType;
  93. auto *RD = Ty->getAsCXXRecordDecl();
  94. if (!RD)
  95. return nullptr;
  96. // Check to see if we have information for the specified type cached.
  97. const auto *CanonRD = RD->getCanonicalDecl();
  98. for (auto &KV : Data) {
  99. const ComparisonCategoryInfo &Info = KV.second;
  100. if (CanonRD == Info.Record->getCanonicalDecl())
  101. return &Info;
  102. }
  103. if (!RD->getEnclosingNamespaceContext()->isStdNamespace())
  104. return nullptr;
  105. // If not, check to see if the decl names a type in namespace std with a name
  106. // matching one of the comparison category types.
  107. for (unsigned I = static_cast<unsigned>(CCT::First),
  108. End = static_cast<unsigned>(CCT::Last);
  109. I <= End; ++I) {
  110. CCT Kind = static_cast<CCT>(I);
  111. // We've found the comparison category type. Build a new cache entry for
  112. // it.
  113. if (getCategoryString(Kind) == RD->getName())
  114. return &Data.try_emplace((char)Kind, Ctx, RD, Kind).first->second;
  115. }
  116. // We've found nothing. This isn't a comparison category type.
  117. return nullptr;
  118. }
  119. const ComparisonCategoryInfo &ComparisonCategories::getInfoForType(QualType Ty) const {
  120. const ComparisonCategoryInfo *Info = lookupInfoForType(Ty);
  121. assert(Info && "info for comparison category not found");
  122. return *Info;
  123. }
  124. QualType ComparisonCategoryInfo::getType() const {
  125. assert(Record);
  126. return QualType(Record->getTypeForDecl(), 0);
  127. }
  128. StringRef ComparisonCategories::getCategoryString(ComparisonCategoryType Kind) {
  129. using CCKT = ComparisonCategoryType;
  130. switch (Kind) {
  131. case CCKT::WeakEquality:
  132. return "weak_equality";
  133. case CCKT::StrongEquality:
  134. return "strong_equality";
  135. case CCKT::PartialOrdering:
  136. return "partial_ordering";
  137. case CCKT::WeakOrdering:
  138. return "weak_ordering";
  139. case CCKT::StrongOrdering:
  140. return "strong_ordering";
  141. }
  142. llvm_unreachable("unhandled cases in switch");
  143. }
  144. StringRef ComparisonCategories::getResultString(ComparisonCategoryResult Kind) {
  145. using CCVT = ComparisonCategoryResult;
  146. switch (Kind) {
  147. case CCVT::Equal:
  148. return "equal";
  149. case CCVT::Nonequal:
  150. return "nonequal";
  151. case CCVT::Equivalent:
  152. return "equivalent";
  153. case CCVT::Nonequivalent:
  154. return "nonequivalent";
  155. case CCVT::Less:
  156. return "less";
  157. case CCVT::Greater:
  158. return "greater";
  159. case CCVT::Unordered:
  160. return "unordered";
  161. }
  162. llvm_unreachable("unhandled case in switch");
  163. }
  164. std::vector<ComparisonCategoryResult>
  165. ComparisonCategories::getPossibleResultsForType(ComparisonCategoryType Type) {
  166. using CCT = ComparisonCategoryType;
  167. using CCR = ComparisonCategoryResult;
  168. std::vector<CCR> Values;
  169. Values.reserve(6);
  170. Values.push_back(CCR::Equivalent);
  171. bool IsStrong = (Type == CCT::StrongEquality || Type == CCT::StrongOrdering);
  172. if (IsStrong)
  173. Values.push_back(CCR::Equal);
  174. if (Type == CCT::StrongOrdering || Type == CCT::WeakOrdering ||
  175. Type == CCT::PartialOrdering) {
  176. Values.push_back(CCR::Less);
  177. Values.push_back(CCR::Greater);
  178. } else {
  179. Values.push_back(CCR::Nonequivalent);
  180. if (IsStrong)
  181. Values.push_back(CCR::Nonequal);
  182. }
  183. if (Type == CCT::PartialOrdering)
  184. Values.push_back(CCR::Unordered);
  185. return Values;
  186. }