call.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "clang/Sema/Template.h"
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/check/call.h"
  9. #include "toolchain/check/cpp/import.h"
  10. #include "toolchain/check/cpp/location.h"
  11. #include "toolchain/check/cpp/operators.h"
  12. #include "toolchain/check/cpp/overload_resolution.h"
  13. #include "toolchain/check/cpp/type_mapping.h"
  14. #include "toolchain/check/literal.h"
  15. #include "toolchain/sem_ir/function.h"
  16. #include "toolchain/sem_ir/ids.h"
  17. #include "toolchain/sem_ir/typed_insts.h"
  18. namespace Carbon::Check {
  19. auto PerformCallToCppFunction(Context& context, SemIR::LocId loc_id,
  20. SemIR::CppOverloadSetId overload_set_id,
  21. SemIR::InstId self_id,
  22. llvm::ArrayRef<SemIR::InstId> arg_ids,
  23. bool is_operator_syntax) -> SemIR::InstId {
  24. SemIR::InstId callee_id = PerformCppOverloadResolution(
  25. context, loc_id, overload_set_id, self_id, arg_ids);
  26. SemIR::Callee callee = GetCallee(context.sem_ir(), callee_id);
  27. CARBON_KIND_SWITCH(callee) {
  28. case CARBON_KIND(SemIR::CalleeError _): {
  29. return SemIR::ErrorInst::InstId;
  30. }
  31. case CARBON_KIND(SemIR::CalleeFunction fn): {
  32. CARBON_CHECK(!fn.self_id.has_value());
  33. if (self_id.has_value()) {
  34. // Preserve the `self` argument from the original callee.
  35. fn.self_id = self_id;
  36. }
  37. return PerformCallToFunction(context, loc_id, callee_id, fn, arg_ids,
  38. is_operator_syntax);
  39. }
  40. case CARBON_KIND(SemIR::CalleeCppOverloadSet _): {
  41. CARBON_FATAL("overloads can't be recursive");
  42. }
  43. case CARBON_KIND(SemIR::CalleeNonFunction _): {
  44. CARBON_FATAL("overloads should produce functions");
  45. }
  46. }
  47. }
  48. // Synthesize a placeholder `void{}` template argument, that will never be a
  49. // valid argument for any template parameter. This is used in order to get Clang
  50. // to diagnose invalid template argument errors for us. The location of the
  51. // Carbon expression is used as the location of the C++ expression, so
  52. // Clang's diagnostics will point into the Carbon code.
  53. //
  54. // TODO: If Clang ever tries to print the type of the expression or to
  55. // pretty-print the expression itself, it would print the wrong thing. Currently
  56. // this doesn't appear to happen, but in principle it could. Ideally we'd add an
  57. // extension point to Clang to represent a "foreign expression" and use it here
  58. // instead of creating a bogus placeholder expression.
  59. static auto MakePlaceholderTemplateArg(Context& context, SemIR::InstId arg_id)
  60. -> clang::TemplateArgumentLoc {
  61. auto arg_loc = GetCppLocation(context, SemIR::LocId(arg_id));
  62. auto void_type = context.ast_context().VoidTy;
  63. auto* arg = new (context.ast_context()) clang::CXXScalarValueInitExpr(
  64. void_type,
  65. context.ast_context().getTrivialTypeSourceInfo(void_type, arg_loc),
  66. arg_loc);
  67. return clang::TemplateArgumentLoc(
  68. clang::TemplateArgument(arg, /*IsCanonical=*/false), arg);
  69. }
  70. // Converts an argument in a call to a C++ template name into a corresponding
  71. // clang template argument, given the template parameter it will be matched
  72. // against.
  73. static auto ConvertArgToTemplateArg(
  74. Context& context, clang::TemplateDecl* template_decl,
  75. clang::NamedDecl* param_decl, SemIR::InstId arg_id,
  76. clang::SmallVector<clang::TemplateArgument>* template_args)
  77. -> std::optional<clang::TemplateArgumentLoc> {
  78. if (isa<clang::TemplateTypeParmDecl>(param_decl)) {
  79. auto type = ExprAsType(context, SemIR::LocId(arg_id), arg_id);
  80. if (type.type_id == SemIR::ErrorInst::TypeId) {
  81. return std::nullopt;
  82. }
  83. auto clang_type = MapToCppType(context, type.type_id);
  84. if (clang_type.isNull()) {
  85. context.TODO(arg_id, "unsupported type used as template argument");
  86. return std::nullopt;
  87. }
  88. return clang::TemplateArgumentLoc(
  89. clang_type,
  90. context.ast_context().getTrivialTypeSourceInfo(
  91. clang_type, GetCppLocation(context, SemIR::LocId(arg_id))));
  92. }
  93. if (isa<clang::TemplateTemplateParmDecl>(param_decl)) {
  94. auto inst = context.sem_ir().insts().Get(arg_id);
  95. if (auto template_name_type =
  96. context.types().TryGetAs<SemIR::CppTemplateNameType>(
  97. inst.type_id())) {
  98. clang::TemplateName name(cast<clang::TemplateDecl>(
  99. context.clang_decls().Get(template_name_type->decl_id).key.decl));
  100. return clang::TemplateArgumentLoc(
  101. context.ast_context(), clang::TemplateArgument(name),
  102. /*TemplateKWLoc=*/clang::SourceLocation(),
  103. clang::NestedNameSpecifierLoc(),
  104. GetCppLocation(context, SemIR::LocId(arg_id)));
  105. }
  106. // TODO: Eventually we should also support passing Carbon generics as
  107. // template template arguments.
  108. return MakePlaceholderTemplateArg(context, arg_id);
  109. }
  110. if (auto* non_type = dyn_cast<clang::NonTypeTemplateParmDecl>(param_decl)) {
  111. auto param_type = non_type->getType();
  112. // Handle non-type parameters with a dependent type. For example:
  113. //
  114. // C++: template<typename T, T N> struct S{};
  115. // Carbon: Cpp.S(i32, 42)
  116. //
  117. // When evaluating the second template argument, the generic type of
  118. // `T` should be substituted with `i32`.
  119. if (param_type->isInstantiationDependentType()) {
  120. clang::Sema::InstantiatingTemplate inst(
  121. context.clang_sema(), clang::SourceLocation(), param_decl, non_type,
  122. *template_args, clang::SourceRange());
  123. if (inst.isInvalid()) {
  124. return std::nullopt;
  125. }
  126. clang::MultiLevelTemplateArgumentList mltal(template_decl, *template_args,
  127. /*Final=*/true);
  128. mltal.addOuterRetainedLevels(non_type->getDepth());
  129. // TODO: handle pack expansion by passing in the pack index from
  130. // `ConvertArgsToTemplateArgs`.
  131. if (!param_type->getAs<clang::PackExpansionType>()) {
  132. param_type = context.clang_sema().SubstType(param_type, mltal,
  133. non_type->getLocation(),
  134. non_type->getDeclName());
  135. }
  136. if (!param_type.isNull()) {
  137. param_type = context.clang_sema().CheckNonTypeTemplateParameterType(
  138. param_type, non_type->getLocation());
  139. }
  140. if (param_type.isNull()) {
  141. return std::nullopt;
  142. }
  143. }
  144. // Get the Carbon type corresponding to the parameter's Clang type.
  145. const auto type_expr =
  146. ImportCppType(context, SemIR::LocId(arg_id), param_type);
  147. // Try to convert the argument to the parameter type.
  148. const auto converted_inst_id =
  149. Convert(context, SemIR::LocId(arg_id), arg_id,
  150. {
  151. .kind = ConversionTarget::Value,
  152. .type_id = type_expr.type_id,
  153. });
  154. if (converted_inst_id == SemIR::ErrorInst::InstId) {
  155. return std::nullopt;
  156. }
  157. // TODO: provide a better location.
  158. auto template_loc = clang::TemplateArgumentLocInfo();
  159. auto const_inst_id =
  160. context.constant_values().GetConstantInstId(converted_inst_id);
  161. if (const_inst_id.has_value()) {
  162. if (param_type->isIntegerType()) {
  163. if (auto int_value =
  164. context.insts().TryGetAs<SemIR::IntValue>(const_inst_id)) {
  165. const auto& ap_int = context.ints().Get(int_value->int_id);
  166. const bool is_unsigned =
  167. !param_type->isSignedIntegerOrEnumerationType();
  168. auto aps_int =
  169. llvm::APSInt(ap_int, is_unsigned)
  170. .extOrTrunc(context.ast_context().getIntWidth(param_type));
  171. clang::TemplateArgument template_arg(context.ast_context(), aps_int,
  172. param_type);
  173. return clang::TemplateArgumentLoc(template_arg, template_loc);
  174. }
  175. } else if (param_type->isFloatingType()) {
  176. if (auto float_value =
  177. context.insts().TryGetAs<SemIR::FloatValue>(const_inst_id)) {
  178. const auto& ap_float = context.floats().Get(float_value->float_id);
  179. clang::TemplateArgument template_arg(
  180. context.ast_context(), param_type, clang::APValue(ap_float));
  181. return clang::TemplateArgumentLoc(template_arg, template_loc);
  182. }
  183. }
  184. }
  185. // TODO: Support other types.
  186. context.TODO(arg_id,
  187. "unsupported argument type for non-type template parameter");
  188. return std::nullopt;
  189. }
  190. CARBON_FATAL("Unknown declaration kind for template parameter");
  191. }
  192. // Converts a call argument list into a Clang template argument list for a given
  193. // template. Returns true on success, or false if an error was diagnosed.
  194. static auto ConvertArgsToTemplateArgs(Context& context,
  195. clang::TemplateDecl* template_decl,
  196. llvm::ArrayRef<SemIR::InstId> arg_ids,
  197. clang::TemplateArgumentListInfo& arg_list)
  198. -> bool {
  199. clang::SmallVector<clang::TemplateArgument> template_args;
  200. for (auto* param_decl : template_decl->getTemplateParameters()->asArray()) {
  201. if (arg_ids.empty()) {
  202. return true;
  203. }
  204. // A parameter pack consumes all remaining arguments; otherwise, it consumes
  205. // a single argument.
  206. // TODO: Handle expanded template parameter packs, which have a known, fixed
  207. // arity.
  208. llvm::ArrayRef<SemIR::InstId> args_for_param =
  209. param_decl->isTemplateParameterPack() ? std::exchange(arg_ids, {})
  210. : arg_ids.consume_front();
  211. for (auto arg_id : args_for_param) {
  212. if (auto arg = ConvertArgToTemplateArg(context, template_decl, param_decl,
  213. arg_id, &template_args)) {
  214. arg_list.addArgument(*arg);
  215. template_args.push_back(arg->getArgument());
  216. } else {
  217. return false;
  218. }
  219. }
  220. }
  221. // If there are any remaining arguments, that's an error; convert them to
  222. // placeholder template arguments so that Clang will diagnose it for us.
  223. for (auto arg_id : arg_ids) {
  224. // Synthesize a placeholder `void{}` template argument.
  225. arg_list.addArgument(MakePlaceholderTemplateArg(context, arg_id));
  226. }
  227. return true;
  228. }
  229. // Given a template and an template argument list, builds a Carbon value
  230. // describing the corresponding C++ template-id.
  231. static auto BuildTemplateId(Context& context, SemIR::LocId loc_id,
  232. clang::SourceLocation loc,
  233. clang::TemplateDecl* template_decl,
  234. clang::TemplateArgumentListInfo& arg_list)
  235. -> SemIR::InstId {
  236. if (auto* var_template_decl =
  237. dyn_cast<clang::VarTemplateDecl>(template_decl)) {
  238. auto decl_result = context.clang_sema().CheckVarTemplateId(
  239. var_template_decl, /*TemplateLoc=*/clang::SourceLocation(), loc,
  240. arg_list, /*SetWrittenArgs=*/false);
  241. return decl_result.isInvalid()
  242. ? SemIR::ErrorInst::InstId
  243. : ImportCppDecl(context, loc_id,
  244. SemIR::ClangDeclKey::ForNonFunctionDecl(
  245. decl_result.get()));
  246. }
  247. if (auto* concept_decl = dyn_cast<clang::ConceptDecl>(template_decl)) {
  248. auto expr_result = context.clang_sema().CheckConceptTemplateId(
  249. clang::CXXScopeSpec(), /*TemplateKWLoc=*/clang::SourceLocation(),
  250. clang::DeclarationNameInfo(concept_decl->getDeclName(), loc),
  251. concept_decl, concept_decl, &arg_list);
  252. if (expr_result.isInvalid()) {
  253. return SemIR::ErrorInst::InstId;
  254. }
  255. auto* expr = expr_result.getAs<clang::ConceptSpecializationExpr>();
  256. return MakeBoolLiteral(context, loc_id,
  257. SemIR::BoolValue::From(expr->isSatisfied()));
  258. }
  259. clang::TemplateName template_name(template_decl);
  260. auto clang_type = context.clang_sema().CheckTemplateIdType(
  261. clang::ElaboratedTypeKeyword::None, template_name, loc, arg_list,
  262. /*Scope=*/nullptr, /*ForNestedNameSpecifier=*/false);
  263. if (clang_type.isNull()) {
  264. return SemIR::ErrorInst::InstId;
  265. }
  266. return ImportCppType(context, loc_id, clang_type).inst_id;
  267. }
  268. auto PerformCallToCppTemplateName(Context& context, SemIR::LocId loc_id,
  269. SemIR::ClangDeclId template_decl_id,
  270. llvm::ArrayRef<SemIR::InstId> arg_ids)
  271. -> SemIR::InstId {
  272. auto* template_decl = dyn_cast<clang::TemplateDecl>(
  273. context.clang_decls().Get(template_decl_id).key.decl);
  274. auto loc = GetCppLocation(context, loc_id);
  275. // Form a template argument list for this template.
  276. clang::TemplateArgumentListInfo arg_list(loc, loc);
  277. if (!ConvertArgsToTemplateArgs(context, template_decl, arg_ids, arg_list)) {
  278. return SemIR::ErrorInst::InstId;
  279. }
  280. return BuildTemplateId(context, loc_id, loc, template_decl, arg_list);
  281. }
  282. } // namespace Carbon::Check