call.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/call.h"
  5. #include "clang/Sema/Sema.h"
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/call.h"
  8. #include "toolchain/check/cpp/import.h"
  9. #include "toolchain/check/cpp/location.h"
  10. #include "toolchain/check/cpp/operators.h"
  11. #include "toolchain/check/cpp/overload_resolution.h"
  12. #include "toolchain/check/cpp/type_mapping.h"
  13. #include "toolchain/check/literal.h"
  14. #include "toolchain/sem_ir/function.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::Check {
  18. auto PerformCallToCppFunction(Context& context, SemIR::LocId loc_id,
  19. SemIR::CppOverloadSetId overload_set_id,
  20. SemIR::InstId self_id,
  21. llvm::ArrayRef<SemIR::InstId> arg_ids)
  22. -> SemIR::InstId {
  23. SemIR::InstId callee_id = PerformCppOverloadResolution(
  24. context, loc_id, overload_set_id, self_id, arg_ids);
  25. SemIR::Callee callee = GetCallee(context.sem_ir(), callee_id);
  26. CARBON_KIND_SWITCH(callee) {
  27. case CARBON_KIND(SemIR::CalleeError _): {
  28. return SemIR::ErrorInst::InstId;
  29. }
  30. case CARBON_KIND(SemIR::CalleeFunction fn): {
  31. CARBON_CHECK(!fn.self_id.has_value());
  32. if (self_id.has_value()) {
  33. // Preserve the `self` argument from the original callee.
  34. fn.self_id = self_id;
  35. }
  36. return PerformCallToFunction(context, loc_id, callee_id, fn, arg_ids);
  37. }
  38. case CARBON_KIND(SemIR::CalleeCppOverloadSet _): {
  39. CARBON_FATAL("overloads can't be recursive");
  40. }
  41. case CARBON_KIND(SemIR::CalleeNonFunction _): {
  42. CARBON_FATAL("overloads should produce functions");
  43. }
  44. }
  45. }
  46. // Converts an argument in a call to a C++ template name into a corresponding
  47. // clang template argument, given the template parameter it will be matched
  48. // against.
  49. static auto ConvertArgToTemplateArg(Context& context,
  50. const clang::NamedDecl* param_decl,
  51. SemIR::InstId arg_id)
  52. -> std::optional<clang::TemplateArgumentLoc> {
  53. if (isa<clang::TemplateTypeParmDecl>(param_decl)) {
  54. auto type = ExprAsType(context, SemIR::LocId(arg_id), arg_id);
  55. if (type.type_id == SemIR::ErrorInst::TypeId) {
  56. return std::nullopt;
  57. }
  58. auto clang_type = MapToCppType(context, type.type_id);
  59. if (clang_type.isNull()) {
  60. context.TODO(arg_id, "unsupported type used as template argument");
  61. return std::nullopt;
  62. }
  63. return clang::TemplateArgumentLoc(
  64. clang_type,
  65. context.ast_context().getTrivialTypeSourceInfo(
  66. clang_type, GetCppLocation(context, SemIR::LocId(arg_id))));
  67. }
  68. if (isa<clang::TemplateTemplateParmDecl>(param_decl)) {
  69. // TODO: Check the type of the argument `CppTemplateNameType` and
  70. // convert it to a `clang::TemplateName`.
  71. context.TODO(arg_id, "argument for template template parameter");
  72. return std::nullopt;
  73. }
  74. if (isa<clang::NonTypeTemplateParmDecl>(param_decl)) {
  75. // TODO: Check the argument has a concrete constant value, and convert it to
  76. // a Clang constant value.
  77. context.TODO(arg_id, "argument for non-type template parameter");
  78. return std::nullopt;
  79. }
  80. CARBON_FATAL("Unknown declaration kind for template parameter");
  81. }
  82. // Converts a call argument list into a Clang template argument list for a given
  83. // template. Returns true on success, or false if an error was diagnosed.
  84. static auto ConvertArgsToTemplateArgs(Context& context,
  85. clang::TemplateDecl* template_decl,
  86. llvm::ArrayRef<SemIR::InstId> arg_ids,
  87. clang::TemplateArgumentListInfo& arg_list)
  88. -> bool {
  89. for (auto* param_decl : template_decl->getTemplateParameters()->asArray()) {
  90. if (arg_ids.empty()) {
  91. return true;
  92. }
  93. // A parameter pack consumes all remaining arguments; otherwise, it consumes
  94. // a single argument.
  95. // TODO: Handle expanded template parameter packs, which have a known, fixed
  96. // arity.
  97. llvm::ArrayRef<SemIR::InstId> args_for_param =
  98. param_decl->isTemplateParameterPack() ? std::exchange(arg_ids, {})
  99. : arg_ids.consume_front();
  100. for (auto arg_id : args_for_param) {
  101. if (auto arg = ConvertArgToTemplateArg(context, param_decl, arg_id)) {
  102. arg_list.addArgument(*arg);
  103. } else {
  104. return false;
  105. }
  106. }
  107. }
  108. // If there are any remaining arguments, that's an error; convert them to
  109. // placeholder template arguments so that Clang will diagnose it for us.
  110. for (auto arg_id : arg_ids) {
  111. // Synthesize a placeholder `void{}` template argument.
  112. auto arg_loc = GetCppLocation(context, SemIR::LocId(arg_id));
  113. auto void_type = context.ast_context().VoidTy;
  114. auto* arg = new (context.ast_context()) clang::CXXScalarValueInitExpr(
  115. void_type,
  116. context.ast_context().getTrivialTypeSourceInfo(void_type, arg_loc),
  117. arg_loc);
  118. arg_list.addArgument(clang::TemplateArgumentLoc(
  119. clang::TemplateArgument(arg, /*IsCanonical=*/false), arg));
  120. }
  121. return true;
  122. }
  123. // Given a template and an template argument list, builds a Carbon value
  124. // describing the corresponding C++ template-id.
  125. static auto BuildTemplateId(Context& context, SemIR::LocId loc_id,
  126. clang::SourceLocation loc,
  127. clang::TemplateDecl* template_decl,
  128. clang::TemplateArgumentListInfo& arg_list)
  129. -> SemIR::InstId {
  130. if (auto* var_template_decl =
  131. dyn_cast<clang::VarTemplateDecl>(template_decl)) {
  132. auto decl_result = context.clang_sema().CheckVarTemplateId(
  133. var_template_decl, /*TemplateLoc=*/clang::SourceLocation(), loc,
  134. arg_list, /*SetWrittenArgs=*/false);
  135. return decl_result.isInvalid()
  136. ? SemIR::ErrorInst::InstId
  137. : ImportCppDecl(context, loc_id,
  138. SemIR::ClangDeclKey::ForNonFunctionDecl(
  139. decl_result.get()));
  140. }
  141. if (auto* concept_decl = dyn_cast<clang::ConceptDecl>(template_decl)) {
  142. auto expr_result = context.clang_sema().CheckConceptTemplateId(
  143. clang::CXXScopeSpec(), /*TemplateKWLoc=*/clang::SourceLocation(),
  144. clang::DeclarationNameInfo(concept_decl->getDeclName(), loc),
  145. concept_decl, concept_decl, &arg_list);
  146. if (expr_result.isInvalid()) {
  147. return SemIR::ErrorInst::InstId;
  148. }
  149. auto* expr = expr_result.getAs<clang::ConceptSpecializationExpr>();
  150. return MakeBoolLiteral(context, loc_id,
  151. SemIR::BoolValue::From(expr->isSatisfied()));
  152. }
  153. clang::TemplateName template_name(template_decl);
  154. auto clang_type = context.clang_sema().CheckTemplateIdType(
  155. clang::ElaboratedTypeKeyword::None, template_name, loc, arg_list,
  156. /*Scope=*/nullptr, /*ForNestedNameSpecifier=*/false);
  157. if (clang_type.isNull()) {
  158. return SemIR::ErrorInst::InstId;
  159. }
  160. return ImportCppType(context, loc_id, clang_type).inst_id;
  161. }
  162. auto PerformCallToCppTemplateName(Context& context, SemIR::LocId loc_id,
  163. SemIR::ClangDeclId template_decl_id,
  164. llvm::ArrayRef<SemIR::InstId> arg_ids)
  165. -> SemIR::InstId {
  166. auto* template_decl = dyn_cast<clang::TemplateDecl>(
  167. context.clang_decls().Get(template_decl_id).key.decl);
  168. auto loc = GetCppLocation(context, loc_id);
  169. // Form a template argument list for this template.
  170. clang::TemplateArgumentListInfo arg_list(loc, loc);
  171. if (!ConvertArgsToTemplateArgs(context, template_decl, arg_ids, arg_list)) {
  172. return SemIR::ErrorInst::InstId;
  173. }
  174. return BuildTemplateId(context, loc_id, loc, template_decl, arg_list);
  175. }
  176. } // namespace Carbon::Check