call.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. // Returns true if the given instruction can only be a template argument, and
  20. // not a function argument. We classify arguments as definitely being template
  21. // arguments if they are types or the name of a template or generic.
  22. // TODO: We should also have a way to specify that an argument is a non-type
  23. // template argument.
  24. static auto IsTemplateArg(Context& context, SemIR::InstId arg_id) -> bool {
  25. auto arg_type_id = context.insts().Get(arg_id).type_id();
  26. auto arg_type = context.types().GetAsInst(arg_type_id);
  27. return arg_type
  28. .IsOneOf<SemIR::TypeType, SemIR::FacetType, SemIR::CppTemplateNameType,
  29. SemIR::GenericClassType, SemIR::GenericInterfaceType,
  30. SemIR::GenericNamedConstraintType>();
  31. }
  32. // Splits a call argument list into a list of template arguments followed by a
  33. // list of function arguments. We split the argument list as early as possible,
  34. // subject to the constraint that if an argument is a template argument, it goes
  35. // in the template argument list.
  36. static auto SplitCallArgumentList(Context& context,
  37. llvm::ArrayRef<SemIR::InstId> arg_ids)
  38. -> std::pair<llvm::ArrayRef<SemIR::InstId>, llvm::ArrayRef<SemIR::InstId>> {
  39. for (auto [n, arg_id] : llvm::enumerate(llvm::reverse(arg_ids))) {
  40. if (IsTemplateArg(context, arg_id)) {
  41. return {arg_ids.drop_back(n), arg_ids.take_back(n)};
  42. }
  43. }
  44. // No template arguments found.
  45. return {{}, arg_ids};
  46. }
  47. auto PerformCallToCppFunction(Context& context, SemIR::LocId loc_id,
  48. SemIR::CppOverloadSetId overload_set_id,
  49. SemIR::InstId self_id,
  50. llvm::ArrayRef<SemIR::InstId> arg_ids,
  51. bool is_operator_syntax) -> SemIR::InstId {
  52. auto [template_arg_ids, function_arg_ids] =
  53. SplitCallArgumentList(context, arg_ids);
  54. auto callee_id = PerformCppOverloadResolution(
  55. context, loc_id, context.cpp_overload_sets().Get(overload_set_id),
  56. template_arg_ids, self_id, function_arg_ids);
  57. SemIR::Callee callee = GetCallee(context.sem_ir(), callee_id);
  58. CARBON_KIND_SWITCH(callee) {
  59. case CARBON_KIND(SemIR::CalleeError _): {
  60. return SemIR::ErrorInst::InstId;
  61. }
  62. case CARBON_KIND(SemIR::CalleeFunction fn): {
  63. CARBON_CHECK(!fn.self_id.has_value());
  64. if (self_id.has_value()) {
  65. // Preserve the `self` argument from the original callee.
  66. fn.self_id = self_id;
  67. }
  68. return PerformCallToFunction(context, loc_id, callee_id, fn,
  69. function_arg_ids, is_operator_syntax);
  70. }
  71. case CARBON_KIND(SemIR::CalleeCppOverloadSet _): {
  72. CARBON_FATAL("overloads can't be recursive");
  73. }
  74. case CARBON_KIND(SemIR::CalleeNonFunction _): {
  75. CARBON_FATAL("overloads should produce functions");
  76. }
  77. }
  78. }
  79. // Synthesize a placeholder `void{}` template argument, that will never be a
  80. // valid argument for any template parameter. This is used in order to get Clang
  81. // to diagnose invalid template argument errors for us. The location of the
  82. // Carbon expression is used as the location of the C++ expression, so
  83. // Clang's diagnostics will point into the Carbon code.
  84. //
  85. // TODO: If Clang ever tries to print the type of the expression or to
  86. // pretty-print the expression itself, it would print the wrong thing. Currently
  87. // this doesn't appear to happen, but in principle it could. Ideally we'd add an
  88. // extension point to Clang to represent a "foreign expression" and use it here
  89. // instead of creating a bogus placeholder expression.
  90. static auto MakePlaceholderTemplateArg(Context& context, SemIR::InstId arg_id)
  91. -> clang::TemplateArgumentLoc {
  92. auto arg_loc = GetCppLocation(context, SemIR::LocId(arg_id));
  93. auto void_type = context.ast_context().VoidTy;
  94. auto* arg = new (context.ast_context()) clang::CXXScalarValueInitExpr(
  95. void_type,
  96. context.ast_context().getTrivialTypeSourceInfo(void_type, arg_loc),
  97. arg_loc);
  98. return clang::TemplateArgumentLoc(
  99. clang::TemplateArgument(arg, /*IsCanonical=*/false), arg);
  100. }
  101. // Converts an argument in a call to a C++ template name into a corresponding
  102. // clang template argument, given the template parameter it will be matched
  103. // against.
  104. static auto ConvertArgToTemplateArg(
  105. Context& context, clang::TemplateDecl* template_decl,
  106. clang::NamedDecl* param_decl, SemIR::InstId arg_id,
  107. clang::SmallVector<clang::TemplateArgument>* template_args, bool diagnose)
  108. -> std::optional<clang::TemplateArgumentLoc> {
  109. if (isa<clang::TemplateTypeParmDecl>(param_decl)) {
  110. auto type = ExprAsType(context, SemIR::LocId(arg_id), arg_id, diagnose);
  111. if (type.type_id == SemIR::ErrorInst::TypeId) {
  112. return std::nullopt;
  113. }
  114. auto clang_type = MapToCppType(context, type.type_id);
  115. if (clang_type.isNull()) {
  116. if (diagnose) {
  117. context.TODO(arg_id, "unsupported type used as template argument");
  118. }
  119. return std::nullopt;
  120. }
  121. return clang::TemplateArgumentLoc(
  122. clang_type,
  123. context.ast_context().getTrivialTypeSourceInfo(
  124. clang_type, GetCppLocation(context, SemIR::LocId(arg_id))));
  125. }
  126. if (isa<clang::TemplateTemplateParmDecl>(param_decl)) {
  127. auto inst = context.sem_ir().insts().Get(arg_id);
  128. if (auto template_name_type =
  129. context.types().TryGetAs<SemIR::CppTemplateNameType>(
  130. inst.type_id())) {
  131. clang::TemplateName name(cast<clang::TemplateDecl>(
  132. context.clang_decls().Get(template_name_type->decl_id).key.decl));
  133. return clang::TemplateArgumentLoc(
  134. context.ast_context(), clang::TemplateArgument(name),
  135. /*TemplateKWLoc=*/clang::SourceLocation(),
  136. clang::NestedNameSpecifierLoc(),
  137. GetCppLocation(context, SemIR::LocId(arg_id)));
  138. }
  139. // TODO: Eventually we should also support passing Carbon generics as
  140. // template template arguments.
  141. return MakePlaceholderTemplateArg(context, arg_id);
  142. }
  143. if (auto* non_type = dyn_cast<clang::NonTypeTemplateParmDecl>(param_decl)) {
  144. auto param_type = non_type->getType();
  145. // Handle non-type parameters with a dependent type. For example:
  146. //
  147. // C++: template<typename T, T N> struct S{};
  148. // Carbon: Cpp.S(i32, 42)
  149. //
  150. // When evaluating the second template argument, the generic type of
  151. // `T` should be substituted with `i32`.
  152. if (param_type->isInstantiationDependentType()) {
  153. // If we don't want to diagnose errors, create a SFINAE context so that
  154. // Clang knows to suppress error messages.
  155. std::optional<clang::Sema::SFINAETrap> sfinae;
  156. if (!diagnose) {
  157. sfinae.emplace(context.clang_sema());
  158. }
  159. clang::Sema::InstantiatingTemplate inst(
  160. context.clang_sema(), clang::SourceLocation(), param_decl, non_type,
  161. *template_args, clang::SourceRange());
  162. if (inst.isInvalid()) {
  163. return std::nullopt;
  164. }
  165. clang::MultiLevelTemplateArgumentList mltal(template_decl, *template_args,
  166. /*Final=*/true);
  167. mltal.addOuterRetainedLevels(non_type->getDepth());
  168. // TODO: handle pack expansion by passing in the pack index from
  169. // `ConvertArgsToTemplateArgs`.
  170. if (!param_type->getAs<clang::PackExpansionType>()) {
  171. param_type = context.clang_sema().SubstType(param_type, mltal,
  172. non_type->getLocation(),
  173. non_type->getDeclName());
  174. }
  175. if (!param_type.isNull()) {
  176. param_type = context.clang_sema().CheckNonTypeTemplateParameterType(
  177. param_type, non_type->getLocation());
  178. }
  179. if (param_type.isNull() || (sfinae && sfinae->hasErrorOccurred())) {
  180. return std::nullopt;
  181. }
  182. }
  183. // Get the Carbon type corresponding to the parameter's Clang type.
  184. const auto type_expr =
  185. ImportCppType(context, SemIR::LocId(arg_id), param_type);
  186. // Try to convert the argument to the parameter type.
  187. const auto converted_inst_id =
  188. Convert(context, SemIR::LocId(arg_id), arg_id,
  189. {
  190. .kind = ConversionTarget::Value,
  191. .type_id = type_expr.type_id,
  192. .diagnose = diagnose,
  193. });
  194. if (converted_inst_id == SemIR::ErrorInst::InstId) {
  195. return std::nullopt;
  196. }
  197. // TODO: provide a better location.
  198. auto template_loc = clang::TemplateArgumentLocInfo();
  199. auto const_inst_id =
  200. context.constant_values().GetConstantInstId(converted_inst_id);
  201. if (const_inst_id.has_value()) {
  202. if (param_type->isIntegerType()) {
  203. const bool is_signed = param_type->isSignedIntegerOrEnumerationType();
  204. if (auto int_value =
  205. context.insts().TryGetAs<SemIR::IntValue>(const_inst_id)) {
  206. const auto& ap_int = context.ints().Get(int_value->int_id);
  207. auto aps_int =
  208. llvm::APSInt(ap_int, !is_signed)
  209. .extOrTrunc(context.ast_context().getIntWidth(param_type));
  210. clang::TemplateArgument template_arg(context.ast_context(), aps_int,
  211. param_type);
  212. return clang::TemplateArgumentLoc(template_arg, template_loc);
  213. } else if (auto bool_value =
  214. context.insts().TryGetAs<SemIR::BoolLiteral>(
  215. const_inst_id)) {
  216. llvm::APInt ap_int(context.ast_context().getIntWidth(param_type),
  217. bool_value->value.ToBool(), is_signed);
  218. auto aps_int =
  219. llvm::APSInt(ap_int, !is_signed)
  220. .extOrTrunc(context.ast_context().getIntWidth(param_type));
  221. auto template_arg = clang::TemplateArgument(context.ast_context(),
  222. aps_int, param_type);
  223. return clang::TemplateArgumentLoc(template_arg, template_loc);
  224. }
  225. } else if (param_type->isFloatingType()) {
  226. if (auto float_value =
  227. context.insts().TryGetAs<SemIR::FloatValue>(const_inst_id)) {
  228. const auto& ap_float = context.floats().Get(float_value->float_id);
  229. clang::TemplateArgument template_arg(
  230. context.ast_context(), param_type, clang::APValue(ap_float));
  231. return clang::TemplateArgumentLoc(template_arg, template_loc);
  232. }
  233. } else if (param_type->isPointerType()) {
  234. if (auto addr_of =
  235. context.insts().TryGetAs<SemIR::AddrOf>(const_inst_id)) {
  236. if (auto* var_decl = GetAsClangVarDecl(context, addr_of->lvalue_id)) {
  237. clang::TemplateArgument template_arg(var_decl, param_type);
  238. return clang::TemplateArgumentLoc(template_arg, template_loc);
  239. }
  240. // TODO: support pointers to variables declared in Carbon.
  241. }
  242. }
  243. }
  244. // TODO: Support other types.
  245. if (diagnose) {
  246. context.TODO(arg_id,
  247. "unsupported argument type for non-type template parameter");
  248. }
  249. return std::nullopt;
  250. }
  251. CARBON_FATAL("Unknown declaration kind for template parameter");
  252. }
  253. auto ConvertArgsToTemplateArgs(Context& context,
  254. clang::TemplateDecl* template_decl,
  255. llvm::ArrayRef<SemIR::InstId> arg_ids,
  256. clang::TemplateArgumentListInfo& arg_list,
  257. bool diagnose) -> bool {
  258. clang::SmallVector<clang::TemplateArgument> template_args;
  259. for (auto* param_decl : template_decl->getTemplateParameters()->asArray()) {
  260. if (arg_ids.empty()) {
  261. return true;
  262. }
  263. // A parameter pack consumes all remaining arguments; otherwise, it consumes
  264. // a single argument.
  265. // TODO: Handle expanded template parameter packs, which have a known, fixed
  266. // arity.
  267. llvm::ArrayRef<SemIR::InstId> args_for_param =
  268. param_decl->isTemplateParameterPack() ? std::exchange(arg_ids, {})
  269. : arg_ids.consume_front();
  270. for (auto arg_id : args_for_param) {
  271. if (auto arg =
  272. ConvertArgToTemplateArg(context, template_decl, param_decl,
  273. arg_id, &template_args, diagnose)) {
  274. arg_list.addArgument(*arg);
  275. template_args.push_back(arg->getArgument());
  276. } else {
  277. return false;
  278. }
  279. }
  280. }
  281. // If there are any remaining arguments, that's an error; convert them to
  282. // placeholder template arguments so that Clang will diagnose it for us.
  283. for (auto arg_id : arg_ids) {
  284. // Synthesize a placeholder `void{}` template argument.
  285. arg_list.addArgument(MakePlaceholderTemplateArg(context, arg_id));
  286. }
  287. return true;
  288. }
  289. // Given a template and an template argument list, builds a Carbon value
  290. // describing the corresponding C++ template-id.
  291. static auto BuildTemplateId(Context& context, SemIR::LocId loc_id,
  292. clang::SourceLocation loc,
  293. clang::TemplateDecl* template_decl,
  294. clang::TemplateArgumentListInfo& arg_list)
  295. -> SemIR::InstId {
  296. if (auto* var_template_decl =
  297. dyn_cast<clang::VarTemplateDecl>(template_decl)) {
  298. auto decl_result = context.clang_sema().CheckVarTemplateId(
  299. var_template_decl, /*TemplateLoc=*/clang::SourceLocation(), loc,
  300. arg_list, /*SetWrittenArgs=*/false);
  301. return decl_result.isInvalid()
  302. ? SemIR::ErrorInst::InstId
  303. : ImportCppDecl(context, loc_id,
  304. SemIR::ClangDeclKey::ForNonFunctionDecl(
  305. decl_result.get()));
  306. }
  307. if (auto* concept_decl = dyn_cast<clang::ConceptDecl>(template_decl)) {
  308. auto expr_result = context.clang_sema().CheckConceptTemplateId(
  309. clang::CXXScopeSpec(), /*TemplateKWLoc=*/clang::SourceLocation(),
  310. clang::DeclarationNameInfo(concept_decl->getDeclName(), loc),
  311. concept_decl, concept_decl, &arg_list);
  312. if (expr_result.isInvalid()) {
  313. return SemIR::ErrorInst::InstId;
  314. }
  315. auto* expr = expr_result.getAs<clang::ConceptSpecializationExpr>();
  316. return MakeBoolLiteral(context, loc_id,
  317. SemIR::BoolValue::From(expr->isSatisfied()));
  318. }
  319. clang::TemplateName template_name(template_decl);
  320. auto clang_type = context.clang_sema().CheckTemplateIdType(
  321. clang::ElaboratedTypeKeyword::None, template_name, loc, arg_list,
  322. /*Scope=*/nullptr, /*ForNestedNameSpecifier=*/false);
  323. if (clang_type.isNull()) {
  324. return SemIR::ErrorInst::InstId;
  325. }
  326. return ImportCppType(context, loc_id, clang_type).inst_id;
  327. }
  328. auto PerformCallToCppTemplateName(Context& context, SemIR::LocId loc_id,
  329. SemIR::ClangDeclId template_decl_id,
  330. llvm::ArrayRef<SemIR::InstId> arg_ids)
  331. -> SemIR::InstId {
  332. auto* template_decl = dyn_cast<clang::TemplateDecl>(
  333. context.clang_decls().Get(template_decl_id).key.decl);
  334. auto loc = GetCppLocation(context, loc_id);
  335. // Form a template argument list for this template.
  336. clang::TemplateArgumentListInfo arg_list(loc, loc);
  337. if (!ConvertArgsToTemplateArgs(context, template_decl, arg_ids, arg_list)) {
  338. return SemIR::ErrorInst::InstId;
  339. }
  340. return BuildTemplateId(context, loc_id, loc, template_decl, arg_list);
  341. }
  342. } // namespace Carbon::Check