function.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/function.h"
  5. #include "common/find.h"
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/check/inst.h"
  10. #include "toolchain/check/merge.h"
  11. #include "toolchain/check/pattern.h"
  12. #include "toolchain/check/pattern_match.h"
  13. #include "toolchain/check/scope_stack.h"
  14. #include "toolchain/check/type.h"
  15. #include "toolchain/check/type_completion.h"
  16. #include "toolchain/diagnostics/format_providers.h"
  17. #include "toolchain/sem_ir/builtin_function_kind.h"
  18. #include "toolchain/sem_ir/ids.h"
  19. #include "toolchain/sem_ir/pattern.h"
  20. namespace Carbon::Check {
  21. auto FindSelfPattern(Context& context,
  22. SemIR::InstBlockId implicit_param_patterns_id)
  23. -> SemIR::InstId {
  24. auto implicit_param_patterns =
  25. context.inst_blocks().GetOrEmpty(implicit_param_patterns_id);
  26. return FindIfOrNone(implicit_param_patterns, [&](auto implicit_param_id) {
  27. return SemIR::IsSelfPattern(context.sem_ir(), implicit_param_id);
  28. });
  29. }
  30. auto AddReturnPattern(Context& context, SemIR::LocId loc_id,
  31. Context::FormExpr form_expr) -> SemIR::InstId {
  32. auto result_id = SemIR::InstId::None;
  33. auto result_type_id = SemIR::TypeId::None;
  34. auto form_inst = context.insts().Get(
  35. context.constant_values().GetConstantInstId(form_expr.form_inst_id));
  36. CARBON_KIND_SWITCH(form_inst) {
  37. case SemIR::RefForm::Kind: {
  38. result_type_id = GetPatternType(context, form_expr.type_component_id);
  39. result_id = AddInst<SemIR::RefReturnPattern>(context, loc_id,
  40. {.type_id = result_type_id});
  41. break;
  42. }
  43. case SemIR::ValueForm::Kind: {
  44. result_type_id = GetPatternType(context, form_expr.type_component_id);
  45. result_id = AddInst<SemIR::ValueReturnPattern>(
  46. context, loc_id, {.type_id = result_type_id});
  47. break;
  48. }
  49. case CARBON_KIND(SemIR::InitForm _): {
  50. result_type_id = GetPatternType(context, form_expr.type_component_id);
  51. result_id = AddInst<SemIR::OutParamPattern>(
  52. context, SemIR::LocId(form_expr.form_inst_id),
  53. {.type_id = result_type_id,
  54. .pretty_name_id = SemIR::NameId::ReturnSlot});
  55. break;
  56. }
  57. case SemIR::SymbolicBinding::Kind:
  58. CARBON_CHECK(
  59. context.constant_values().Get(form_expr.form_inst_id).is_symbolic());
  60. context.TODO(loc_id, "Support symbolic return forms");
  61. result_type_id = SemIR::ErrorInst::TypeId;
  62. result_id = SemIR::ErrorInst::InstId;
  63. break;
  64. case SemIR::ErrorInst::Kind:
  65. result_type_id = SemIR::ErrorInst::TypeId;
  66. result_id = SemIR::ErrorInst::InstId;
  67. break;
  68. default:
  69. CARBON_FATAL("unexpected inst kind: {0}", form_inst);
  70. }
  71. return AddInst<SemIR::ReturnSlotPattern>(
  72. context, SemIR::LocId(form_expr.form_inst_id),
  73. {.type_id = result_type_id,
  74. .subpattern_id = result_id,
  75. .type_inst_id = form_expr.type_component_inst_id});
  76. }
  77. auto IsValidBuiltinDeclaration(Context& context,
  78. const SemIR::Function& function,
  79. SemIR::BuiltinFunctionKind builtin_kind)
  80. -> bool {
  81. if (!function.call_params_id.has_value()) {
  82. // For now, we have no builtins that support positional parameters.
  83. return false;
  84. }
  85. // Find the list of call parameters other than the implicit return slots.
  86. auto call_params =
  87. context.inst_blocks()
  88. .Get(function.call_params_id)
  89. .take_front(function.call_param_ranges.explicit_end().index);
  90. // Get the return type. This is `()` if none was specified.
  91. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  92. if (!return_type_id.has_value()) {
  93. return_type_id = GetTupleType(context, {});
  94. }
  95. return builtin_kind.IsValidType(context.sem_ir(), call_params,
  96. return_type_id);
  97. }
  98. namespace {
  99. // Function signature fields for `MakeFunctionSignature`.
  100. struct FunctionSignatureInsts {
  101. SemIR::InstBlockId decl_block_id = SemIR::InstBlockId::None;
  102. SemIR::InstBlockId pattern_block_id = SemIR::InstBlockId::None;
  103. SemIR::InstBlockId implicit_param_patterns_id = SemIR::InstBlockId::None;
  104. SemIR::InstBlockId param_patterns_id = SemIR::InstBlockId::None;
  105. SemIR::InstBlockId call_param_patterns_id = SemIR::InstBlockId::None;
  106. SemIR::InstBlockId call_params_id = SemIR::InstBlockId::None;
  107. SemIR::Function::CallParamIndexRanges call_param_ranges =
  108. SemIR::Function::CallParamIndexRanges::Empty;
  109. SemIR::TypeInstId return_type_inst_id = SemIR::TypeInstId::None;
  110. SemIR::InstId return_form_inst_id = SemIR::InstId::None;
  111. SemIR::InstId return_pattern_id = SemIR::InstId::None;
  112. SemIR::InstId self_param_id = SemIR::InstId::None;
  113. };
  114. } // namespace
  115. // Handles construction of the signature's parameter and return types.
  116. static auto MakeFunctionSignature(Context& context, SemIR::LocId loc_id,
  117. const FunctionDeclArgs& args)
  118. -> FunctionSignatureInsts {
  119. FunctionSignatureInsts insts;
  120. StartFunctionSignature(context);
  121. // Build and add a `self: Self` or `ref self: Self` parameter if needed.
  122. if (args.self_type_id.has_value()) {
  123. context.full_pattern_stack().StartImplicitParamList();
  124. BeginSubpattern(context);
  125. auto self_type_region_id = ConsumeSubpatternExpr(
  126. context, context.types().GetTypeInstId(args.self_type_id));
  127. EndEmptySubpattern(context);
  128. insts.self_param_id = AddParamPattern(
  129. context, loc_id, SemIR::NameId::SelfValue, self_type_region_id,
  130. args.self_type_id, args.self_is_ref);
  131. insts.implicit_param_patterns_id =
  132. context.inst_blocks().Add({insts.self_param_id});
  133. context.full_pattern_stack().EndImplicitParamList();
  134. }
  135. // Build and add any explicit parameters. Whether these are references
  136. // or not is controlled by `args.params_are_refs`.
  137. context.full_pattern_stack().StartExplicitParamList();
  138. if (args.param_type_ids.empty()) {
  139. insts.param_patterns_id = SemIR::InstBlockId::Empty;
  140. } else {
  141. context.inst_block_stack().Push();
  142. for (auto param_type_id : args.param_type_ids) {
  143. BeginSubpattern(context);
  144. auto param_type_region_id = ConsumeSubpatternExpr(
  145. context, context.types().GetTypeInstId(param_type_id));
  146. EndEmptySubpattern(context);
  147. context.inst_block_stack().AddInstId(AddParamPattern(
  148. context, loc_id, SemIR::NameId::Underscore, param_type_region_id,
  149. param_type_id, /*is_ref=*/args.params_are_refs));
  150. }
  151. insts.param_patterns_id = context.inst_block_stack().Pop();
  152. }
  153. context.full_pattern_stack().EndExplicitParamList();
  154. // Build and add the return type. We always use an initializing form for now.
  155. if (args.return_type_id.has_value()) {
  156. auto return_form = ReturnExprAsForm(
  157. context, loc_id, context.types().GetTypeInstId(args.return_type_id));
  158. insts.return_type_inst_id = return_form.type_component_inst_id;
  159. insts.return_form_inst_id = return_form.form_inst_id;
  160. insts.return_pattern_id = AddReturnPattern(context, loc_id, return_form);
  161. }
  162. auto match_results =
  163. CalleePatternMatch(context, insts.implicit_param_patterns_id,
  164. insts.param_patterns_id, insts.return_pattern_id);
  165. insts.call_param_patterns_id = match_results.call_param_patterns_id;
  166. insts.call_params_id = match_results.call_params_id;
  167. insts.call_param_ranges = match_results.param_ranges;
  168. auto [pattern_block_id, decl_block_id] =
  169. FinishFunctionSignature(context, /*check_unused=*/false);
  170. insts.pattern_block_id = pattern_block_id;
  171. insts.decl_block_id = decl_block_id;
  172. return insts;
  173. }
  174. auto MakeGeneratedFunctionDecl(Context& context, SemIR::LocId loc_id,
  175. const FunctionDeclArgs& args)
  176. -> std::pair<SemIR::InstId, SemIR::FunctionId> {
  177. auto insts = MakeFunctionSignature(context, loc_id, args);
  178. // Add the function declaration.
  179. auto [decl_id, function_id] = MakeFunctionDecl(
  180. context, loc_id, insts.decl_block_id, /*build_generic=*/false,
  181. /*is_definition=*/true,
  182. SemIR::Function{
  183. {
  184. .name_id = args.name_id,
  185. .parent_scope_id = args.parent_scope_id,
  186. .generic_id = SemIR::GenericId::None,
  187. .first_param_node_id = Parse::NodeId::None,
  188. .last_param_node_id = Parse::NodeId::None,
  189. .pattern_block_id = insts.pattern_block_id,
  190. .implicit_param_patterns_id = insts.implicit_param_patterns_id,
  191. .param_patterns_id = insts.param_patterns_id,
  192. .is_extern = false,
  193. .extern_library_id = SemIR::LibraryNameId::None,
  194. .non_owning_decl_id = SemIR::InstId::None,
  195. // Set by `MakeFunctionDecl`.
  196. .first_owning_decl_id = SemIR::InstId::None,
  197. },
  198. {
  199. .call_param_patterns_id = insts.call_param_patterns_id,
  200. .call_params_id = insts.call_params_id,
  201. .call_param_ranges = insts.call_param_ranges,
  202. .return_type_inst_id = insts.return_type_inst_id,
  203. .return_form_inst_id = insts.return_form_inst_id,
  204. .return_pattern_id = insts.return_pattern_id,
  205. .self_param_id = insts.self_param_id,
  206. }});
  207. context.generated().push_back(decl_id);
  208. return {decl_id, function_id};
  209. }
  210. auto CheckFunctionReturnTypeMatches(Context& context,
  211. const SemIR::Function& new_function,
  212. const SemIR::Function& prev_function,
  213. SemIR::SpecificId prev_specific_id,
  214. bool diagnose) -> bool {
  215. // TODO: Pass a specific ID for `prev_function` instead of substitutions and
  216. // use it here.
  217. auto new_return_type_id =
  218. new_function.GetDeclaredReturnType(context.sem_ir());
  219. auto prev_return_type_id =
  220. prev_function.GetDeclaredReturnType(context.sem_ir(), prev_specific_id);
  221. if (new_return_type_id == SemIR::ErrorInst::TypeId ||
  222. prev_return_type_id == SemIR::ErrorInst::TypeId) {
  223. return false;
  224. }
  225. if (!context.types().AreEqualAcrossDeclarations(new_return_type_id,
  226. prev_return_type_id)) {
  227. if (!diagnose) {
  228. return false;
  229. }
  230. CARBON_DIAGNOSTIC(
  231. FunctionRedeclReturnTypeDiffers, Error,
  232. "function redeclaration differs because return type is {0}",
  233. SemIR::TypeId);
  234. CARBON_DIAGNOSTIC(
  235. FunctionRedeclReturnTypeDiffersNoReturn, Error,
  236. "function redeclaration differs because no return type is provided");
  237. auto diag =
  238. new_return_type_id.has_value()
  239. ? context.emitter().Build(new_function.latest_decl_id(),
  240. FunctionRedeclReturnTypeDiffers,
  241. new_return_type_id)
  242. : context.emitter().Build(new_function.latest_decl_id(),
  243. FunctionRedeclReturnTypeDiffersNoReturn);
  244. if (prev_return_type_id.has_value()) {
  245. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePrevious, Note,
  246. "previously declared with return type {0}",
  247. SemIR::TypeId);
  248. diag.Note(prev_function.latest_decl_id(),
  249. FunctionRedeclReturnTypePrevious, prev_return_type_id);
  250. } else {
  251. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePreviousNoReturn, Note,
  252. "previously declared with no return type");
  253. diag.Note(prev_function.latest_decl_id(),
  254. FunctionRedeclReturnTypePreviousNoReturn);
  255. }
  256. diag.Emit();
  257. return false;
  258. }
  259. return true;
  260. }
  261. // Checks that a function declaration's evaluation mode matches the previous
  262. // declaration's evaluation mode. Returns `false` and optionally produces a
  263. // diagnostic on mismatch.
  264. static auto CheckFunctionEvaluationModeMatches(
  265. Context& context, const SemIR::Function& new_function,
  266. const SemIR::Function& prev_function, bool diagnose) -> bool {
  267. if (prev_function.evaluation_mode == new_function.evaluation_mode) {
  268. return true;
  269. }
  270. if (!diagnose) {
  271. return false;
  272. }
  273. auto eval_mode_index = [](SemIR::Function::EvaluationMode mode) {
  274. switch (mode) {
  275. case SemIR::Function::EvaluationMode::None:
  276. return 0;
  277. case SemIR::Function::EvaluationMode::Eval:
  278. return 1;
  279. case SemIR::Function::EvaluationMode::MustEval:
  280. return 2;
  281. }
  282. };
  283. auto prev_eval_mode_index = eval_mode_index(prev_function.evaluation_mode);
  284. auto new_eval_mode_index = eval_mode_index(new_function.evaluation_mode);
  285. CARBON_DIAGNOSTIC(
  286. FunctionRedeclEvaluationModeDiffers, Error,
  287. "function redeclaration differs because new function is "
  288. "{0:=-1:not `eval`|=-2:not `musteval`|=1:`eval`|=2:`musteval`}",
  289. Diagnostics::IntAsSelect);
  290. CARBON_DIAGNOSTIC(FunctionRedeclEvaluationModePrevious, Note,
  291. "previously {0:<0:not |:}declared as "
  292. "{0:=-1:`eval`|=-2:`musteval`|=1:`eval`|=2:`musteval`}",
  293. Diagnostics::IntAsSelect);
  294. context.emitter()
  295. .Build(new_function.latest_decl_id(), FunctionRedeclEvaluationModeDiffers,
  296. new_eval_mode_index ? new_eval_mode_index : -prev_eval_mode_index)
  297. .Note(prev_function.latest_decl_id(),
  298. FunctionRedeclEvaluationModePrevious,
  299. prev_eval_mode_index ? prev_eval_mode_index : -new_eval_mode_index)
  300. .Emit();
  301. return false;
  302. }
  303. auto CheckFunctionTypeMatches(Context& context,
  304. const SemIR::Function& new_function,
  305. const SemIR::Function& prev_function,
  306. SemIR::SpecificId prev_specific_id,
  307. bool check_syntax, bool check_self, bool diagnose)
  308. -> bool {
  309. if (!CheckRedeclParamsMatch(context, DeclParams(new_function),
  310. DeclParams(prev_function), prev_specific_id,
  311. diagnose, check_syntax, check_self)) {
  312. return false;
  313. }
  314. if (!CheckFunctionReturnTypeMatches(context, new_function, prev_function,
  315. prev_specific_id, diagnose)) {
  316. return false;
  317. }
  318. if (!CheckFunctionEvaluationModeMatches(context, new_function, prev_function,
  319. diagnose)) {
  320. return false;
  321. }
  322. return true;
  323. }
  324. auto CheckFunctionReturnPatternType(Context& context, SemIR::LocId loc_id,
  325. SemIR::InstId return_pattern_id,
  326. SemIR::SpecificId specific_id)
  327. -> SemIR::TypeId {
  328. auto arg_type_id = SemIR::ExtractScrutineeType(
  329. context.sem_ir(), SemIR::GetTypeOfInstInSpecific(
  330. context.sem_ir(), specific_id, return_pattern_id));
  331. auto init_repr = SemIR::InitRepr::ForType(context.sem_ir(), arg_type_id);
  332. if (!init_repr.is_valid()) {
  333. // TODO: Consider suppressing the diagnostics if we've already diagnosed a
  334. // definition or call to this function.
  335. if (!RequireConcreteType(
  336. context, arg_type_id, SemIR::LocId(return_pattern_id),
  337. [&](auto& builder) {
  338. CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Context,
  339. "function returns incomplete type {0}",
  340. SemIR::TypeId);
  341. builder.Context(loc_id, IncompleteTypeInFunctionReturnType,
  342. arg_type_id);
  343. },
  344. [&](auto& builder) {
  345. CARBON_DIAGNOSTIC(AbstractTypeInFunctionReturnType, Context,
  346. "function returns abstract type {0}",
  347. SemIR::TypeId);
  348. builder.Context(loc_id, AbstractTypeInFunctionReturnType,
  349. arg_type_id);
  350. })) {
  351. return SemIR::ErrorInst::TypeId;
  352. }
  353. }
  354. return arg_type_id;
  355. }
  356. auto CheckFunctionDefinitionSignature(Context& context,
  357. SemIR::FunctionId function_id) -> void {
  358. auto& function = context.functions().Get(function_id);
  359. auto params_to_complete =
  360. context.inst_blocks().GetOrEmpty(function.call_params_id);
  361. // The return parameter will be diagnosed after and differently from other
  362. // parameters.
  363. auto return_call_param = SemIR::InstId::None;
  364. if (!params_to_complete.empty() && function.return_pattern_id.has_value()) {
  365. return_call_param = params_to_complete.consume_back();
  366. }
  367. // Check the parameter types are complete.
  368. for (auto param_ref_id : params_to_complete) {
  369. if (param_ref_id == SemIR::ErrorInst::InstId) {
  370. continue;
  371. }
  372. // The parameter types need to be complete.
  373. RequireCompleteType(
  374. context, context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
  375. SemIR::LocId(param_ref_id), [&](auto& builder) {
  376. CARBON_DIAGNOSTIC(
  377. IncompleteTypeInFunctionParam, Context,
  378. "parameter has incomplete type {0} in function definition",
  379. TypeOfInstId);
  380. builder.Context(param_ref_id, IncompleteTypeInFunctionParam,
  381. param_ref_id);
  382. });
  383. }
  384. // Check the return type is complete.
  385. if (function.return_pattern_id.has_value()) {
  386. CheckFunctionReturnPatternType(
  387. context, SemIR::LocId(function.return_pattern_id),
  388. function.return_pattern_id, SemIR::SpecificId::None);
  389. // `CheckFunctionReturnPatternType` should have diagnosed incomplete types,
  390. // so don't `RequireCompleteType` on the return type.
  391. if (return_call_param.has_value()) {
  392. // TODO: If the types are already checked for completeness then this does
  393. // nothing?
  394. TryToCompleteType(
  395. context,
  396. context.insts().GetAs<SemIR::AnyParam>(return_call_param).type_id,
  397. SemIR::LocId(return_call_param));
  398. }
  399. }
  400. }
  401. auto StartFunctionSignature(Context& context) -> void {
  402. context.scope_stack().PushForDeclName();
  403. context.inst_block_stack().Push();
  404. context.pattern_block_stack().Push();
  405. context.full_pattern_stack().PushParameterizedDecl();
  406. }
  407. auto FinishFunctionSignature(Context& context, bool check_unused)
  408. -> FinishFunctionSignatureResult {
  409. context.full_pattern_stack().PopFullPattern();
  410. auto pattern_block_id = context.pattern_block_stack().Pop();
  411. auto decl_block_id = context.inst_block_stack().Pop();
  412. context.scope_stack().Pop(check_unused);
  413. return {.pattern_block_id = pattern_block_id, .decl_block_id = decl_block_id};
  414. }
  415. auto MakeFunctionDecl(Context& context, SemIR::LocId loc_id,
  416. SemIR::InstBlockId decl_block_id, bool build_generic,
  417. bool is_definition, SemIR::Function function)
  418. -> std::pair<SemIR::InstId, SemIR::FunctionId> {
  419. CARBON_CHECK(!function.first_owning_decl_id.has_value());
  420. SemIR::FunctionDecl function_decl = {SemIR::TypeId::None,
  421. SemIR::FunctionId::None, decl_block_id};
  422. auto decl_id = AddPlaceholderInstInNoBlock(
  423. context, SemIR::LocIdAndInst::RuntimeVerified(context.sem_ir(), loc_id,
  424. function_decl));
  425. function.first_owning_decl_id = decl_id;
  426. if (is_definition) {
  427. function.definition_id = decl_id;
  428. }
  429. if (build_generic) {
  430. function.generic_id = BuildGenericDecl(context, decl_id);
  431. }
  432. // Create the `Function` object.
  433. function_decl.function_id = context.functions().Add(std::move(function));
  434. function_decl.type_id =
  435. GetFunctionType(context, function_decl.function_id,
  436. build_generic ? context.scope_stack().PeekSpecificId()
  437. : SemIR::SpecificId::None);
  438. ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
  439. return {decl_id, function_decl.function_id};
  440. }
  441. auto StartFunctionDefinition(Context& context, SemIR::InstId decl_id,
  442. SemIR::FunctionId function_id) -> void {
  443. // Create the function scope and the entry block.
  444. context.scope_stack().PushForFunctionBody(decl_id);
  445. context.inst_block_stack().Push();
  446. context.region_stack().PushRegion(context.inst_block_stack().PeekOrAdd());
  447. StartGenericDefinition(context,
  448. context.functions().Get(function_id).generic_id);
  449. CheckFunctionDefinitionSignature(context, function_id);
  450. }
  451. auto FinishFunctionDefinition(Context& context, SemIR::FunctionId function_id)
  452. -> void {
  453. context.inst_block_stack().Pop();
  454. context.scope_stack().Pop(/*check_unused=*/true);
  455. auto& function = context.functions().Get(function_id);
  456. function.body_block_ids = context.region_stack().PopRegion();
  457. // If this is a generic function, collect information about the definition.
  458. FinishGenericDefinition(context, function.generic_id);
  459. }
  460. } // namespace Carbon::Check