function.cpp 19 KB

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