function.cpp 19 KB

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