call.cpp 15 KB

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