function.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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/inst.h"
  9. #include "toolchain/check/merge.h"
  10. #include "toolchain/check/pattern.h"
  11. #include "toolchain/check/pattern_match.h"
  12. #include "toolchain/check/type.h"
  13. #include "toolchain/check/type_completion.h"
  14. #include "toolchain/sem_ir/builtin_function_kind.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/pattern.h"
  17. namespace Carbon::Check {
  18. auto FindSelfPattern(Context& context,
  19. SemIR::InstBlockId implicit_param_patterns_id)
  20. -> SemIR::InstId {
  21. auto implicit_param_patterns =
  22. context.inst_blocks().GetOrEmpty(implicit_param_patterns_id);
  23. return FindIfOrNone(implicit_param_patterns, [&](auto implicit_param_id) {
  24. return SemIR::IsSelfPattern(context.sem_ir(), implicit_param_id);
  25. });
  26. }
  27. auto AddReturnPatterns(Context& context, SemIR::LocId loc_id,
  28. Context::FormExpr form_expr) -> SemIR::InstBlockId {
  29. llvm::SmallVector<SemIR::InstId, 1> return_patterns;
  30. auto form_inst = context.insts().Get(form_expr.form_inst_id);
  31. CARBON_KIND_SWITCH(form_inst) {
  32. case SemIR::RefForm::Kind: {
  33. break;
  34. }
  35. case CARBON_KIND(SemIR::InitForm init_form): {
  36. auto pattern_type_id = GetPatternType(context, form_expr.type_id);
  37. auto return_slot_pattern_id = AddPatternInst<SemIR::ReturnSlotPattern>(
  38. context, loc_id,
  39. {.type_id = pattern_type_id,
  40. .type_inst_id = form_expr.type_component_id});
  41. return_patterns.push_back(AddPatternInst<SemIR::OutParamPattern>(
  42. context, SemIR::LocId(form_expr.form_inst_id),
  43. {.type_id = pattern_type_id,
  44. .subpattern_id = return_slot_pattern_id,
  45. .index = init_form.index}));
  46. break;
  47. }
  48. case SemIR::ErrorInst::Kind: {
  49. break;
  50. }
  51. default:
  52. CARBON_FATAL("unexpected inst kind: {0}", form_inst);
  53. }
  54. return context.inst_blocks().AddCanonical(return_patterns);
  55. }
  56. auto IsValidBuiltinDeclaration(Context& context,
  57. const SemIR::Function& function,
  58. SemIR::BuiltinFunctionKind builtin_kind)
  59. -> bool {
  60. if (!function.call_params_id.has_value()) {
  61. // For now, we have no builtins that support positional parameters.
  62. return false;
  63. }
  64. // Find the list of call parameters other than the implicit return slots.
  65. auto call_params = context.inst_blocks()
  66. .Get(function.call_params_id)
  67. .drop_back(context.inst_blocks()
  68. .GetOrEmpty(function.return_patterns_id)
  69. .size());
  70. // Get the return type. This is `()` if none was specified.
  71. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  72. if (!return_type_id.has_value()) {
  73. return_type_id = GetTupleType(context, {});
  74. }
  75. return builtin_kind.IsValidType(context.sem_ir(), call_params,
  76. return_type_id);
  77. }
  78. auto MakeBuiltinFunction(Context& context, SemIR::LocId loc_id,
  79. SemIR::BuiltinFunctionKind builtin_kind,
  80. SemIR::NameScopeId name_scope_id,
  81. SemIR::NameId name_id,
  82. BuiltinFunctionSignature signature) -> SemIR::InstId {
  83. // TODO: Refactor with function construction in thunk.cpp and cpp/import.cpp.
  84. context.scope_stack().PushForDeclName();
  85. context.inst_block_stack().Push();
  86. context.pattern_block_stack().Push();
  87. // Build and add a `[ref self: Self]` parameter if needed.
  88. auto implicit_param_patterns_id = SemIR::InstBlockId::None;
  89. auto self_param_id = SemIR::InstId::None;
  90. if (signature.self_type_id.has_value()) {
  91. context.full_pattern_stack().PushFullPattern(
  92. FullPatternStack::Kind::ImplicitParamList);
  93. BeginSubpattern(context);
  94. auto self_type_region_id = EndSubpatternAsExpr(
  95. context, context.types().GetInstId(signature.self_type_id));
  96. self_param_id = AddParamPattern(context, loc_id, SemIR::NameId::SelfValue,
  97. self_type_region_id, signature.self_type_id,
  98. signature.self_is_ref);
  99. implicit_param_patterns_id = context.inst_blocks().Add({self_param_id});
  100. context.full_pattern_stack().EndImplicitParamList();
  101. } else {
  102. context.full_pattern_stack().PushFullPattern(
  103. FullPatternStack::Kind::ExplicitParamList);
  104. }
  105. // Build and add any explicit parameters. We always use value parameters for
  106. // now.
  107. auto param_patterns_id = SemIR::InstBlockId::Empty;
  108. if (!signature.param_type_ids.empty()) {
  109. context.inst_block_stack().Push();
  110. for (auto param_type_id : signature.param_type_ids) {
  111. BeginSubpattern(context);
  112. auto param_type_region_id = EndSubpatternAsExpr(
  113. context, context.types().GetInstId(param_type_id));
  114. context.inst_block_stack().AddInstId(AddParamPattern(
  115. context, loc_id, SemIR::NameId::Underscore, param_type_region_id,
  116. param_type_id, /*is_ref=*/false));
  117. }
  118. param_patterns_id = context.inst_block_stack().Pop();
  119. }
  120. // Build and add the return type. We always use an initializing form for now.
  121. auto return_patterns_id = SemIR::InstBlockId::None;
  122. Context::FormExpr return_form = {.form_inst_id = SemIR::InstId::None,
  123. .type_component_id = SemIR::TypeInstId::None,
  124. .type_id = SemIR::TypeId::None};
  125. if (signature.return_type_id.has_value()) {
  126. return_form = ExprAsReturnForm(
  127. context, loc_id, context.types().GetInstId(signature.return_type_id));
  128. return_patterns_id = AddReturnPatterns(context, loc_id, return_form);
  129. }
  130. auto [call_param_patterns_id, call_params_id] =
  131. CalleePatternMatch(context, implicit_param_patterns_id, param_patterns_id,
  132. return_patterns_id);
  133. context.full_pattern_stack().PopFullPattern();
  134. auto pattern_block_id = context.pattern_block_stack().Pop();
  135. auto decl_block_id = context.inst_block_stack().Pop();
  136. context.scope_stack().Pop();
  137. // Add the function declaration.
  138. SemIR::FunctionDecl function_decl = {.type_id = SemIR::TypeId::None,
  139. .function_id = SemIR::FunctionId::None,
  140. .decl_block_id = decl_block_id};
  141. auto decl_id = AddPlaceholderInstInNoBlock(
  142. context, SemIR::LocIdAndInst::UncheckedLoc(loc_id, function_decl));
  143. // Build the function entity.
  144. auto function = 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. .first_owning_decl_id = decl_id,
  158. .definition_id = decl_id,
  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_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. CARBON_CHECK(IsValidBuiltinDeclaration(context, function, builtin_kind));
  169. function.SetBuiltinFunction(builtin_kind);
  170. function_decl.function_id = context.functions().Add(function);
  171. function_decl.type_id = GetFunctionType(context, function_decl.function_id,
  172. SemIR::SpecificId::None);
  173. ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
  174. // Add the builtin to the imports block so that it appears in the formatted
  175. // IR.
  176. // TODO: Find a better way to handle this. Ideally we should stop using this
  177. // function entirely and declare builtins in the prelude.
  178. context.imports().push_back(decl_id);
  179. return decl_id;
  180. }
  181. auto CheckFunctionReturnTypeMatches(Context& context,
  182. const SemIR::Function& new_function,
  183. const SemIR::Function& prev_function,
  184. SemIR::SpecificId prev_specific_id,
  185. bool diagnose) -> bool {
  186. // TODO: Pass a specific ID for `prev_function` instead of substitutions and
  187. // use it here.
  188. auto new_return_type_id =
  189. new_function.GetDeclaredReturnType(context.sem_ir());
  190. auto prev_return_type_id =
  191. prev_function.GetDeclaredReturnType(context.sem_ir(), prev_specific_id);
  192. if (new_return_type_id == SemIR::ErrorInst::TypeId ||
  193. prev_return_type_id == SemIR::ErrorInst::TypeId) {
  194. return false;
  195. }
  196. if (!context.types().AreEqualAcrossDeclarations(new_return_type_id,
  197. prev_return_type_id)) {
  198. if (!diagnose) {
  199. return false;
  200. }
  201. CARBON_DIAGNOSTIC(
  202. FunctionRedeclReturnTypeDiffers, Error,
  203. "function redeclaration differs because return type is {0}",
  204. SemIR::TypeId);
  205. CARBON_DIAGNOSTIC(
  206. FunctionRedeclReturnTypeDiffersNoReturn, Error,
  207. "function redeclaration differs because no return type is provided");
  208. auto diag =
  209. new_return_type_id.has_value()
  210. ? context.emitter().Build(new_function.latest_decl_id(),
  211. FunctionRedeclReturnTypeDiffers,
  212. new_return_type_id)
  213. : context.emitter().Build(new_function.latest_decl_id(),
  214. FunctionRedeclReturnTypeDiffersNoReturn);
  215. if (prev_return_type_id.has_value()) {
  216. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePrevious, Note,
  217. "previously declared with return type {0}",
  218. SemIR::TypeId);
  219. diag.Note(prev_function.latest_decl_id(),
  220. FunctionRedeclReturnTypePrevious, prev_return_type_id);
  221. } else {
  222. CARBON_DIAGNOSTIC(FunctionRedeclReturnTypePreviousNoReturn, Note,
  223. "previously declared with no return type");
  224. diag.Note(prev_function.latest_decl_id(),
  225. FunctionRedeclReturnTypePreviousNoReturn);
  226. }
  227. diag.Emit();
  228. return false;
  229. }
  230. return true;
  231. }
  232. auto CheckFunctionTypeMatches(Context& context,
  233. const SemIR::Function& new_function,
  234. const SemIR::Function& prev_function,
  235. SemIR::SpecificId prev_specific_id,
  236. bool check_syntax, bool check_self, bool diagnose)
  237. -> bool {
  238. if (!CheckRedeclParamsMatch(context, DeclParams(new_function),
  239. DeclParams(prev_function), prev_specific_id,
  240. diagnose, check_syntax, check_self)) {
  241. return false;
  242. }
  243. return CheckFunctionReturnTypeMatches(context, new_function, prev_function,
  244. prev_specific_id, diagnose);
  245. }
  246. auto CheckFunctionReturnPatternType(Context& context, SemIR::LocId loc_id,
  247. SemIR::InstId return_pattern_id,
  248. SemIR::SpecificId specific_id)
  249. -> SemIR::TypeId {
  250. auto arg_type_id = SemIR::ExtractScrutineeType(
  251. context.sem_ir(), SemIR::GetTypeOfInstInSpecific(
  252. context.sem_ir(), specific_id, return_pattern_id));
  253. auto init_repr = SemIR::InitRepr::ForType(context.sem_ir(), arg_type_id);
  254. if (!init_repr.is_valid()) {
  255. auto diagnose_incomplete_return_type = [&] {
  256. CARBON_DIAGNOSTIC(IncompleteTypeInFunctionReturnType, Error,
  257. "function returns incomplete type {0}", SemIR::TypeId);
  258. return context.emitter().Build(loc_id, IncompleteTypeInFunctionReturnType,
  259. arg_type_id);
  260. };
  261. auto diagnose_abstract_return_type = [&] {
  262. CARBON_DIAGNOSTIC(AbstractTypeInFunctionReturnType, Error,
  263. "function returns abstract type {0}", SemIR::TypeId);
  264. return context.emitter().Build(loc_id, AbstractTypeInFunctionReturnType,
  265. arg_type_id);
  266. };
  267. // TODO: Consider suppressing the diagnostic if we've already diagnosed a
  268. // definition or call to this function.
  269. if (!RequireConcreteType(
  270. context, arg_type_id, SemIR::LocId(return_pattern_id),
  271. diagnose_incomplete_return_type, diagnose_abstract_return_type)) {
  272. return SemIR::ErrorInst::TypeId;
  273. }
  274. }
  275. return arg_type_id;
  276. }
  277. auto CheckFunctionDefinitionSignature(Context& context,
  278. SemIR::FunctionId function_id) -> void {
  279. auto& function = context.functions().Get(function_id);
  280. auto params_to_complete =
  281. context.inst_blocks().GetOrEmpty(function.call_params_id);
  282. // The return parameter will be diagnosed after and differently from other
  283. // parameters.
  284. auto return_call_param = SemIR::InstId::None;
  285. if (!params_to_complete.empty() && function.return_patterns_id.has_value()) {
  286. return_call_param = params_to_complete.consume_back();
  287. }
  288. // Check the parameter types are complete.
  289. for (auto param_ref_id : params_to_complete) {
  290. if (param_ref_id == SemIR::ErrorInst::InstId) {
  291. continue;
  292. }
  293. // The parameter types need to be complete.
  294. RequireCompleteType(
  295. context, context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
  296. SemIR::LocId(param_ref_id), [&] {
  297. CARBON_DIAGNOSTIC(
  298. IncompleteTypeInFunctionParam, Error,
  299. "parameter has incomplete type {0} in function definition",
  300. TypeOfInstId);
  301. return context.emitter().Build(
  302. param_ref_id, IncompleteTypeInFunctionParam, param_ref_id);
  303. });
  304. }
  305. // Check the return type is complete.
  306. if (function.return_patterns_id.has_value()) {
  307. for (auto return_pattern_id :
  308. context.inst_blocks().Get(function.return_patterns_id)) {
  309. CheckFunctionReturnPatternType(context, SemIR::LocId(return_pattern_id),
  310. return_pattern_id,
  311. SemIR::SpecificId::None);
  312. }
  313. // `CheckFunctionReturnPatternType` should have diagnosed incomplete types,
  314. // so don't `RequireCompleteType` on the return type.
  315. if (return_call_param.has_value()) {
  316. TryToCompleteType(
  317. context,
  318. context.insts().GetAs<SemIR::AnyParam>(return_call_param).type_id,
  319. SemIR::LocId(return_call_param));
  320. }
  321. }
  322. }
  323. } // namespace Carbon::Check