operators.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/check/cpp/operators.h"
  5. #include "clang/Sema/Overload.h"
  6. #include "clang/Sema/Sema.h"
  7. #include "toolchain/check/core_identifier.h"
  8. #include "toolchain/check/cpp/import.h"
  9. #include "toolchain/check/cpp/location.h"
  10. #include "toolchain/check/cpp/overload_resolution.h"
  11. #include "toolchain/check/cpp/type_mapping.h"
  12. #include "toolchain/check/inst.h"
  13. #include "toolchain/check/type.h"
  14. #include "toolchain/check/type_completion.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::Check {
  18. // Maps Carbon operator interface and operator names to Clang operator kinds.
  19. static auto GetClangOperatorKind(Context& context, SemIR::LocId loc_id,
  20. CoreIdentifier interface_name,
  21. CoreIdentifier op_name)
  22. -> std::optional<clang::OverloadedOperatorKind> {
  23. switch (interface_name) {
  24. // Unary operators.
  25. case CoreIdentifier::Destroy:
  26. case CoreIdentifier::As:
  27. case CoreIdentifier::ImplicitAs:
  28. case CoreIdentifier::Copy: {
  29. // TODO: Support destructors and conversions.
  30. return std::nullopt;
  31. }
  32. // Increment and decrement.
  33. case CoreIdentifier::Inc: {
  34. CARBON_CHECK(op_name == CoreIdentifier::Op);
  35. return clang::OO_PlusPlus;
  36. }
  37. case CoreIdentifier::Dec: {
  38. CARBON_CHECK(op_name == CoreIdentifier::Op);
  39. return clang::OO_MinusMinus;
  40. }
  41. // Arithmetic.
  42. case CoreIdentifier::Negate: {
  43. CARBON_CHECK(op_name == CoreIdentifier::Op);
  44. return clang::OO_Minus;
  45. }
  46. // Binary operators.
  47. // Arithmetic operators.
  48. case CoreIdentifier::AddWith: {
  49. CARBON_CHECK(op_name == CoreIdentifier::Op);
  50. return clang::OO_Plus;
  51. }
  52. case CoreIdentifier::SubWith: {
  53. CARBON_CHECK(op_name == CoreIdentifier::Op);
  54. return clang::OO_Minus;
  55. }
  56. case CoreIdentifier::MulWith: {
  57. CARBON_CHECK(op_name == CoreIdentifier::Op);
  58. return clang::OO_Star;
  59. }
  60. case CoreIdentifier::DivWith: {
  61. CARBON_CHECK(op_name == CoreIdentifier::Op);
  62. return clang::OO_Slash;
  63. }
  64. case CoreIdentifier::ModWith: {
  65. CARBON_CHECK(op_name == CoreIdentifier::Op);
  66. return clang::OO_Percent;
  67. }
  68. // Bitwise operators.
  69. case CoreIdentifier::BitAndWith: {
  70. CARBON_CHECK(op_name == CoreIdentifier::Op);
  71. return clang::OO_Amp;
  72. }
  73. case CoreIdentifier::BitOrWith: {
  74. CARBON_CHECK(op_name == CoreIdentifier::Op);
  75. return clang::OO_Pipe;
  76. }
  77. case CoreIdentifier::BitXorWith: {
  78. CARBON_CHECK(op_name == CoreIdentifier::Op);
  79. return clang::OO_Caret;
  80. }
  81. case CoreIdentifier::LeftShiftWith: {
  82. CARBON_CHECK(op_name == CoreIdentifier::Op);
  83. return clang::OO_LessLess;
  84. }
  85. case CoreIdentifier::RightShiftWith: {
  86. CARBON_CHECK(op_name == CoreIdentifier::Op);
  87. return clang::OO_GreaterGreater;
  88. }
  89. // Compound assignment arithmetic operators.
  90. case CoreIdentifier::AddAssignWith: {
  91. CARBON_CHECK(op_name == CoreIdentifier::Op);
  92. return clang::OO_PlusEqual;
  93. }
  94. case CoreIdentifier::SubAssignWith: {
  95. CARBON_CHECK(op_name == CoreIdentifier::Op);
  96. return clang::OO_MinusEqual;
  97. }
  98. case CoreIdentifier::MulAssignWith: {
  99. CARBON_CHECK(op_name == CoreIdentifier::Op);
  100. return clang::OO_StarEqual;
  101. }
  102. case CoreIdentifier::DivAssignWith: {
  103. CARBON_CHECK(op_name == CoreIdentifier::Op);
  104. return clang::OO_SlashEqual;
  105. }
  106. case CoreIdentifier::ModAssignWith: {
  107. CARBON_CHECK(op_name == CoreIdentifier::Op);
  108. return clang::OO_PercentEqual;
  109. }
  110. // Compound assignment bitwise operators.
  111. case CoreIdentifier::BitAndAssignWith: {
  112. CARBON_CHECK(op_name == CoreIdentifier::Op);
  113. return clang::OO_AmpEqual;
  114. }
  115. case CoreIdentifier::BitOrAssignWith: {
  116. CARBON_CHECK(op_name == CoreIdentifier::Op);
  117. return clang::OO_PipeEqual;
  118. }
  119. case CoreIdentifier::BitXorAssignWith: {
  120. CARBON_CHECK(op_name == CoreIdentifier::Op);
  121. return clang::OO_CaretEqual;
  122. }
  123. case CoreIdentifier::LeftShiftAssignWith: {
  124. CARBON_CHECK(op_name == CoreIdentifier::Op);
  125. return clang::OO_LessLessEqual;
  126. }
  127. case CoreIdentifier::RightShiftAssignWith: {
  128. CARBON_CHECK(op_name == CoreIdentifier::Op);
  129. return clang::OO_GreaterGreaterEqual;
  130. }
  131. // Relational operators.
  132. case CoreIdentifier::EqWith: {
  133. if (op_name == CoreIdentifier::Equal) {
  134. return clang::OO_EqualEqual;
  135. }
  136. CARBON_CHECK(op_name == CoreIdentifier::NotEqual);
  137. return clang::OO_ExclaimEqual;
  138. }
  139. case CoreIdentifier::OrderedWith: {
  140. switch (op_name) {
  141. case CoreIdentifier::Less:
  142. return clang::OO_Less;
  143. case CoreIdentifier::Greater:
  144. return clang::OO_Greater;
  145. case CoreIdentifier::LessOrEquivalent:
  146. return clang::OO_LessEqual;
  147. case CoreIdentifier::GreaterOrEquivalent:
  148. return clang::OO_GreaterEqual;
  149. default:
  150. CARBON_FATAL("Unexpected OrderedWith op `{0}`", op_name);
  151. }
  152. }
  153. default:
  154. context.TODO(loc_id, llvm::formatv("Unsupported operator interface `{0}`",
  155. interface_name));
  156. return std::nullopt;
  157. }
  158. }
  159. auto LookupCppOperator(Context& context, SemIR::LocId loc_id, Operator op,
  160. llvm::ArrayRef<SemIR::InstId> arg_ids) -> SemIR::InstId {
  161. // Register an annotation scope to flush any Clang diagnostics when we return.
  162. // This is important to ensure that Clang diagnostics are properly interleaved
  163. // with Carbon diagnostics.
  164. Diagnostics::AnnotationScope annotate_diagnostics(&context.emitter(),
  165. [](auto& /*builder*/) {});
  166. auto op_kind =
  167. GetClangOperatorKind(context, loc_id, op.interface_name, op.op_name);
  168. if (!op_kind) {
  169. return SemIR::InstId::None;
  170. }
  171. // Make sure all operands are complete before lookup.
  172. for (SemIR::InstId arg_id : arg_ids) {
  173. SemIR::TypeId arg_type_id = context.insts().Get(arg_id).type_id();
  174. if (!RequireCompleteType(context, arg_type_id, loc_id, [&] {
  175. CARBON_DIAGNOSTIC(
  176. IncompleteOperandTypeInCppOperatorLookup, Error,
  177. "looking up a C++ operator with incomplete operand type {0}",
  178. SemIR::TypeId);
  179. return context.emitter().Build(
  180. loc_id, IncompleteOperandTypeInCppOperatorLookup, arg_type_id);
  181. })) {
  182. return SemIR::ErrorInst::InstId;
  183. }
  184. }
  185. auto maybe_arg_exprs = InventClangArgs(context, arg_ids);
  186. if (!maybe_arg_exprs.has_value()) {
  187. return SemIR::ErrorInst::InstId;
  188. }
  189. auto& arg_exprs = *maybe_arg_exprs;
  190. clang::SourceLocation loc = GetCppLocation(context, loc_id);
  191. clang::OverloadCandidateSet::OperatorRewriteInfo operator_rewrite_info(
  192. *op_kind, loc, /*AllowRewritten=*/true);
  193. clang::OverloadCandidateSet candidate_set(
  194. loc, clang::OverloadCandidateSet::CSK_Operator, operator_rewrite_info);
  195. clang::Sema& sema = context.clang_sema();
  196. // This works for both unary and binary operators.
  197. sema.LookupOverloadedBinOp(candidate_set, *op_kind, clang::UnresolvedSet<0>{},
  198. arg_exprs);
  199. clang::OverloadCandidateSet::iterator best_viable_fn;
  200. switch (candidate_set.BestViableFunction(sema, loc, best_viable_fn)) {
  201. case clang::OverloadingResult::OR_Success: {
  202. if (!best_viable_fn->Function) {
  203. // The best viable candidate was a builtin. Let the Carbon operator
  204. // machinery handle that.
  205. return SemIR::InstId::None;
  206. }
  207. if (best_viable_fn->RewriteKind) {
  208. context.TODO(
  209. loc_id,
  210. llvm::formatv("Rewriting operator{0} using {1} is not supported",
  211. clang::getOperatorSpelling(
  212. candidate_set.getRewriteInfo().OriginalOperator),
  213. best_viable_fn->Function->getNameAsString()));
  214. return SemIR::ErrorInst::InstId;
  215. }
  216. sema.MarkFunctionReferenced(loc, best_viable_fn->Function);
  217. auto result_id = ImportCppFunctionDecl(
  218. context, loc_id, best_viable_fn->Function,
  219. // If this is an operator method, the first arg will be used as self.
  220. arg_ids.size() -
  221. (isa<clang::CXXMethodDecl>(best_viable_fn->Function) ? 1 : 0));
  222. if (auto fn_decl =
  223. context.insts().TryGetAsWithId<SemIR::FunctionDecl>(result_id)) {
  224. CheckCppOverloadAccess(context, loc_id, best_viable_fn->FoundDecl,
  225. fn_decl->inst_id);
  226. } else {
  227. CARBON_CHECK(result_id == SemIR::ErrorInst::InstId);
  228. }
  229. return result_id;
  230. }
  231. case clang::OverloadingResult::OR_No_Viable_Function: {
  232. // OK, didn't find a viable C++ candidate, but this is not an error, as
  233. // there might be a Carbon candidate.
  234. return SemIR::InstId::None;
  235. }
  236. case clang::OverloadingResult::OR_Ambiguous: {
  237. const char* spelling = clang::getOperatorSpelling(*op_kind);
  238. candidate_set.NoteCandidates(
  239. clang::PartialDiagnosticAt(
  240. loc, sema.PDiag(clang::diag::err_ovl_ambiguous_oper_binary)
  241. << spelling << arg_exprs[0]->getType()
  242. << arg_exprs[1]->getType()),
  243. sema, clang::OCD_AmbiguousCandidates, arg_exprs, spelling, loc);
  244. return SemIR::ErrorInst::InstId;
  245. }
  246. case clang::OverloadingResult::OR_Deleted:
  247. const char* spelling = clang::getOperatorSpelling(*op_kind);
  248. auto* message = best_viable_fn->Function->getDeletedMessage();
  249. // The best viable function might be a different operator if the best
  250. // candidate is a rewritten candidate, so use the operator kind of the
  251. // candidate itself in the diagnostic.
  252. candidate_set.NoteCandidates(
  253. clang::PartialDiagnosticAt(
  254. loc, sema.PDiag(clang::diag::err_ovl_deleted_oper)
  255. << clang::getOperatorSpelling(
  256. best_viable_fn->Function->getOverloadedOperator())
  257. << (message != nullptr)
  258. << (message ? message->getString() : llvm::StringRef())),
  259. sema, clang::OCD_AllCandidates, arg_exprs, spelling, loc);
  260. return SemIR::ErrorInst::InstId;
  261. }
  262. }
  263. auto IsCppOperatorMethodDecl(clang::Decl* decl) -> bool {
  264. auto* clang_method_decl = dyn_cast<clang::CXXMethodDecl>(decl);
  265. return clang_method_decl && clang_method_decl->isOverloadedOperator();
  266. }
  267. static auto GetAsCppFunctionDecl(Context& context, SemIR::InstId inst_id)
  268. -> clang::FunctionDecl* {
  269. auto function_type = context.types().TryGetAs<SemIR::FunctionType>(
  270. context.insts().Get(inst_id).type_id());
  271. if (!function_type) {
  272. return nullptr;
  273. }
  274. SemIR::ClangDeclId clang_decl_id =
  275. context.functions().Get(function_type->function_id).clang_decl_id;
  276. return clang_decl_id.has_value()
  277. ? dyn_cast<clang::FunctionDecl>(
  278. context.clang_decls().Get(clang_decl_id).key.decl)
  279. : nullptr;
  280. }
  281. auto IsCppOperatorMethod(Context& context, SemIR::InstId inst_id) -> bool {
  282. auto* function_decl = GetAsCppFunctionDecl(context, inst_id);
  283. return function_decl && IsCppOperatorMethodDecl(function_decl);
  284. }
  285. auto IsCppConstructorOrNonMethodOperator(Context& context,
  286. SemIR::InstId inst_id) -> bool {
  287. auto* function_decl = GetAsCppFunctionDecl(context, inst_id);
  288. if (!function_decl) {
  289. return false;
  290. }
  291. if (isa<clang::CXXConstructorDecl>(function_decl)) {
  292. return true;
  293. }
  294. return !isa<clang::CXXMethodDecl>(function_decl) &&
  295. function_decl->isOverloadedOperator();
  296. }
  297. } // namespace Carbon::Check