handle_function.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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/base/kind_switch.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/decl_introducer_state.h"
  8. #include "toolchain/check/decl_name_stack.h"
  9. #include "toolchain/check/function.h"
  10. #include "toolchain/check/generic.h"
  11. #include "toolchain/check/handle.h"
  12. #include "toolchain/check/import_ref.h"
  13. #include "toolchain/check/interface.h"
  14. #include "toolchain/check/literal.h"
  15. #include "toolchain/check/merge.h"
  16. #include "toolchain/check/modifiers.h"
  17. #include "toolchain/check/name_component.h"
  18. #include "toolchain/sem_ir/builtin_function_kind.h"
  19. #include "toolchain/sem_ir/entry_point.h"
  20. #include "toolchain/sem_ir/function.h"
  21. #include "toolchain/sem_ir/ids.h"
  22. #include "toolchain/sem_ir/typed_insts.h"
  23. namespace Carbon::Check {
  24. auto HandleParseNode(Context& context, Parse::FunctionIntroducerId node_id)
  25. -> bool {
  26. // Create an instruction block to hold the instructions created as part of the
  27. // function signature, such as parameter and return types.
  28. context.inst_block_stack().Push();
  29. // Push the bracketing node.
  30. context.node_stack().Push(node_id);
  31. // Optional modifiers and the name follow.
  32. context.decl_introducer_state_stack().Push<Lex::TokenKind::Fn>();
  33. context.decl_name_stack().PushScopeAndStartName();
  34. // The function is potentially generic.
  35. StartGenericDecl(context);
  36. return true;
  37. }
  38. auto HandleParseNode(Context& context, Parse::ReturnTypeId node_id) -> bool {
  39. // Propagate the type expression.
  40. auto [type_node_id, type_inst_id] = context.node_stack().PopExprWithNodeId();
  41. auto type_id = ExprAsType(context, type_node_id, type_inst_id).type_id;
  42. auto return_slot_pattern_id =
  43. context.AddPatternInst<SemIR::ReturnSlotPattern>(
  44. node_id, {.type_id = type_id, .type_inst_id = type_inst_id});
  45. auto param_pattern_id = context.AddPatternInst<SemIR::OutParamPattern>(
  46. node_id, {.type_id = type_id,
  47. .subpattern_id = return_slot_pattern_id,
  48. .runtime_index = SemIR::RuntimeParamIndex::Unknown});
  49. context.node_stack().Push(node_id, param_pattern_id);
  50. return true;
  51. }
  52. static auto DiagnoseModifiers(Context& context, DeclIntroducerState& introducer,
  53. bool is_definition,
  54. SemIR::InstId parent_scope_inst_id,
  55. std::optional<SemIR::Inst> parent_scope_inst)
  56. -> void {
  57. CheckAccessModifiersOnDecl(context, introducer, parent_scope_inst);
  58. LimitModifiersOnDecl(context, introducer,
  59. KeywordModifierSet::Access | KeywordModifierSet::Extern |
  60. KeywordModifierSet::Method |
  61. KeywordModifierSet::Interface);
  62. RestrictExternModifierOnDecl(context, introducer, parent_scope_inst,
  63. is_definition);
  64. CheckMethodModifiersOnFunction(context, introducer, parent_scope_inst_id,
  65. parent_scope_inst);
  66. RequireDefaultFinalOnlyInInterfaces(context, introducer, parent_scope_inst);
  67. }
  68. // Tries to merge new_function into prev_function_id. Since new_function won't
  69. // have a definition even if one is upcoming, set is_definition to indicate the
  70. // planned result.
  71. //
  72. // If merging is successful, returns true and may update the previous function.
  73. // Otherwise, returns false. Prints a diagnostic when appropriate.
  74. static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
  75. SemIR::Function& new_function,
  76. bool new_is_import, bool new_is_definition,
  77. SemIR::FunctionId prev_function_id,
  78. SemIR::ImportIRId prev_import_ir_id) -> bool {
  79. auto& prev_function = context.functions().Get(prev_function_id);
  80. if (!CheckFunctionTypeMatches(context, new_function, prev_function)) {
  81. return false;
  82. }
  83. DiagnoseIfInvalidRedecl(
  84. context, Lex::TokenKind::Fn, prev_function.name_id,
  85. RedeclInfo(new_function, new_loc, new_is_definition),
  86. RedeclInfo(prev_function, prev_function.latest_decl_id(),
  87. prev_function.has_definition_started()),
  88. prev_import_ir_id);
  89. if (new_is_definition && prev_function.has_definition_started()) {
  90. return false;
  91. }
  92. if (!prev_function.first_owning_decl_id.is_valid()) {
  93. prev_function.first_owning_decl_id = new_function.first_owning_decl_id;
  94. }
  95. if (new_is_definition) {
  96. // Track the signature from the definition, so that IDs in the body
  97. // match IDs in the signature.
  98. prev_function.MergeDefinition(new_function);
  99. prev_function.return_slot_pattern_id = new_function.return_slot_pattern_id;
  100. }
  101. if ((prev_import_ir_id.is_valid() && !new_is_import)) {
  102. ReplacePrevInstForMerge(context, new_function.parent_scope_id,
  103. prev_function.name_id,
  104. new_function.first_owning_decl_id);
  105. }
  106. return true;
  107. }
  108. // Check whether this is a redeclaration, merging if needed.
  109. static auto TryMergeRedecl(Context& context, Parse::AnyFunctionDeclId node_id,
  110. SemIR::InstId prev_id,
  111. SemIR::FunctionDecl& function_decl,
  112. SemIR::Function& function_info, bool is_definition)
  113. -> void {
  114. if (!prev_id.is_valid()) {
  115. return;
  116. }
  117. auto prev_function_id = SemIR::FunctionId::Invalid;
  118. auto prev_import_ir_id = SemIR::ImportIRId::Invalid;
  119. CARBON_KIND_SWITCH(context.insts().Get(prev_id)) {
  120. case CARBON_KIND(SemIR::FunctionDecl function_decl): {
  121. prev_function_id = function_decl.function_id;
  122. break;
  123. }
  124. case SemIR::ImportRefLoaded::Kind: {
  125. auto import_ir_inst = GetCanonicalImportIRInst(context, prev_id);
  126. // Verify the decl so that things like aliases are name conflicts.
  127. const auto* import_ir =
  128. context.import_irs().Get(import_ir_inst.ir_id).sem_ir;
  129. if (!import_ir->insts().Is<SemIR::FunctionDecl>(import_ir_inst.inst_id)) {
  130. break;
  131. }
  132. // Use the type to get the ID.
  133. if (auto struct_value = context.insts().TryGetAs<SemIR::StructValue>(
  134. context.constant_values().GetConstantInstId(prev_id))) {
  135. if (auto function_type = context.types().TryGetAs<SemIR::FunctionType>(
  136. struct_value->type_id)) {
  137. prev_function_id = function_type->function_id;
  138. prev_import_ir_id = import_ir_inst.ir_id;
  139. }
  140. }
  141. break;
  142. }
  143. default:
  144. break;
  145. }
  146. if (!prev_function_id.is_valid()) {
  147. context.DiagnoseDuplicateName(function_info.latest_decl_id(), prev_id);
  148. return;
  149. }
  150. if (MergeFunctionRedecl(context, node_id, function_info,
  151. /*new_is_import=*/false, is_definition,
  152. prev_function_id, prev_import_ir_id)) {
  153. // When merging, use the existing function rather than adding a new one.
  154. function_decl.function_id = prev_function_id;
  155. }
  156. }
  157. // Build a FunctionDecl describing the signature of a function. This
  158. // handles the common logic shared by function declaration syntax and function
  159. // definition syntax.
  160. static auto BuildFunctionDecl(Context& context,
  161. Parse::AnyFunctionDeclId node_id,
  162. bool is_definition)
  163. -> std::pair<SemIR::FunctionId, SemIR::InstId> {
  164. auto return_slot_pattern_id = SemIR::InstId::Invalid;
  165. if (auto [return_node, maybe_return_slot_pattern_id] =
  166. context.node_stack().PopWithNodeIdIf<Parse::NodeKind::ReturnType>();
  167. maybe_return_slot_pattern_id) {
  168. return_slot_pattern_id = *maybe_return_slot_pattern_id;
  169. }
  170. auto name = PopNameComponent(context, return_slot_pattern_id);
  171. if (!name.param_patterns_id.is_valid()) {
  172. context.TODO(node_id, "function with positional parameters");
  173. name.param_patterns_id = SemIR::InstBlockId::Empty;
  174. }
  175. auto name_context = context.decl_name_stack().FinishName(name);
  176. context.node_stack()
  177. .PopAndDiscardSoloNodeId<Parse::NodeKind::FunctionIntroducer>();
  178. // Process modifiers.
  179. auto [parent_scope_inst_id, parent_scope_inst] =
  180. context.name_scopes().GetInstIfValid(name_context.parent_scope_id);
  181. auto introducer =
  182. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Fn>();
  183. DiagnoseModifiers(context, introducer, is_definition, parent_scope_inst_id,
  184. parent_scope_inst);
  185. bool is_extern = introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extern);
  186. auto virtual_modifier =
  187. introducer.modifier_set.ToEnum<SemIR::Function::VirtualModifier>()
  188. .Case(KeywordModifierSet::Virtual,
  189. SemIR::Function::VirtualModifier::Virtual)
  190. .Case(KeywordModifierSet::Abstract,
  191. SemIR::Function::VirtualModifier::Abstract)
  192. .Case(KeywordModifierSet::Impl,
  193. SemIR::Function::VirtualModifier::Impl)
  194. .Default(SemIR::Function::VirtualModifier::None);
  195. SemIR::Class* virtual_class_info = nullptr;
  196. if (virtual_modifier != SemIR::Function::VirtualModifier::None &&
  197. parent_scope_inst) {
  198. if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
  199. virtual_class_info = &context.classes().Get(class_decl->class_id);
  200. if (virtual_modifier == SemIR::Function::VirtualModifier::Impl &&
  201. !virtual_class_info->base_id.is_valid()) {
  202. CARBON_DIAGNOSTIC(ImplWithoutBase, Error, "impl without base class");
  203. context.emitter().Build(node_id, ImplWithoutBase).Emit();
  204. }
  205. // TODO: If this is an `impl` function, check there's a matching base
  206. // function that's impl or virtual.
  207. virtual_class_info->is_dynamic = true;
  208. }
  209. }
  210. if (introducer.modifier_set.HasAnyOf(KeywordModifierSet::Interface)) {
  211. // TODO: Once we are saving the modifiers for a function, add check that
  212. // the function may only be defined if it is marked `default` or `final`.
  213. context.TODO(introducer.modifier_node_id(ModifierOrder::Decl),
  214. "interface modifier");
  215. }
  216. // Add the function declaration.
  217. auto decl_block_id = context.inst_block_stack().Pop();
  218. auto function_decl = SemIR::FunctionDecl{
  219. SemIR::TypeId::Invalid, SemIR::FunctionId::Invalid, decl_block_id};
  220. auto decl_id =
  221. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, function_decl));
  222. // Build the function entity. This will be merged into an existing function if
  223. // there is one, or otherwise added to the function store.
  224. auto function_info =
  225. SemIR::Function{{name_context.MakeEntityWithParamsBase(
  226. name, decl_id, is_extern, introducer.extern_library)},
  227. {.return_slot_pattern_id = name.return_slot_pattern_id,
  228. .virtual_modifier = virtual_modifier}};
  229. if (is_definition) {
  230. function_info.definition_id = decl_id;
  231. }
  232. if (virtual_class_info) {
  233. context.vtable_stack().AddInstId(decl_id);
  234. }
  235. if (name_context.state == DeclNameStack::NameContext::State::Poisoned) {
  236. context.DiagnosePoisonedName(function_info.latest_decl_id());
  237. } else {
  238. TryMergeRedecl(context, node_id, name_context.prev_inst_id(), function_decl,
  239. function_info, is_definition);
  240. }
  241. // Create a new function if this isn't a valid redeclaration.
  242. if (!function_decl.function_id.is_valid()) {
  243. if (function_info.is_extern && context.IsImplFile()) {
  244. DiagnoseExternRequiresDeclInApiFile(context, node_id);
  245. }
  246. function_info.generic_id = BuildGenericDecl(context, decl_id);
  247. function_decl.function_id = context.functions().Add(function_info);
  248. } else {
  249. FinishGenericRedecl(context, decl_id, function_info.generic_id);
  250. // TODO: Validate that the redeclaration doesn't set an access modifier.
  251. }
  252. function_decl.type_id = context.GetFunctionType(
  253. function_decl.function_id, context.scope_stack().PeekSpecificId());
  254. // Write the function ID into the FunctionDecl.
  255. context.ReplaceInstBeforeConstantUse(decl_id, function_decl);
  256. // Diagnose 'definition of `abstract` function' using the canonical Function's
  257. // modifiers.
  258. if (is_definition &&
  259. context.functions().Get(function_decl.function_id).virtual_modifier ==
  260. SemIR::Function::VirtualModifier::Abstract) {
  261. CARBON_DIAGNOSTIC(DefinedAbstractFunction, Error,
  262. "definition of `abstract` function");
  263. context.emitter().Emit(TokenOnly(node_id), DefinedAbstractFunction);
  264. }
  265. // Check if we need to add this to name lookup, now that the function decl is
  266. // done.
  267. if (name_context.state != DeclNameStack::NameContext::State::Poisoned &&
  268. !name_context.prev_inst_id().is_valid()) {
  269. // At interface scope, a function declaration introduces an associated
  270. // function.
  271. auto lookup_result_id = decl_id;
  272. if (parent_scope_inst && !name_context.has_qualifiers) {
  273. if (auto interface_scope =
  274. parent_scope_inst->TryAs<SemIR::InterfaceDecl>()) {
  275. lookup_result_id = BuildAssociatedEntity(
  276. context, interface_scope->interface_id, decl_id);
  277. }
  278. }
  279. context.decl_name_stack().AddName(name_context, lookup_result_id,
  280. introducer.modifier_set.GetAccessKind());
  281. }
  282. if (SemIR::IsEntryPoint(context.sem_ir(), function_decl.function_id)) {
  283. auto return_type_id = function_info.GetDeclaredReturnType(context.sem_ir());
  284. // TODO: Update this once valid signatures for the entry point are decided.
  285. if (function_info.implicit_param_patterns_id.is_valid() ||
  286. !function_info.param_patterns_id.is_valid() ||
  287. !context.inst_blocks().Get(function_info.param_patterns_id).empty() ||
  288. (return_type_id.is_valid() &&
  289. return_type_id != context.GetTupleType({}) &&
  290. // TODO: Decide on valid return types for `Main.Run`. Perhaps we should
  291. // have an interface for this.
  292. return_type_id != MakeIntType(context, node_id, SemIR::IntKind::Signed,
  293. context.ints().Add(32)))) {
  294. CARBON_DIAGNOSTIC(InvalidMainRunSignature, Error,
  295. "invalid signature for `Main.Run` function; expected "
  296. "`fn ()` or `fn () -> i32`");
  297. context.emitter().Emit(node_id, InvalidMainRunSignature);
  298. }
  299. }
  300. if (!is_definition && context.IsImplFile() && !is_extern) {
  301. context.definitions_required().push_back(decl_id);
  302. }
  303. return {function_decl.function_id, decl_id};
  304. }
  305. auto HandleParseNode(Context& context, Parse::FunctionDeclId node_id) -> bool {
  306. BuildFunctionDecl(context, node_id, /*is_definition=*/false);
  307. context.decl_name_stack().PopScope();
  308. return true;
  309. }
  310. static auto CheckFunctionDefinitionSignature(Context& context,
  311. SemIR::Function& function)
  312. -> void {
  313. auto params_to_complete =
  314. context.inst_blocks().GetOrEmpty(function.call_params_id);
  315. // Check the return type is complete.
  316. if (function.return_slot_pattern_id.is_valid()) {
  317. CheckFunctionReturnType(
  318. context, context.insts().GetLocId(function.return_slot_pattern_id),
  319. function, SemIR::SpecificId::Invalid);
  320. params_to_complete = params_to_complete.drop_back();
  321. }
  322. // Check the parameter types are complete.
  323. for (auto param_ref_id : params_to_complete) {
  324. if (param_ref_id == SemIR::ErrorInst::SingletonInstId) {
  325. continue;
  326. }
  327. // The parameter types need to be complete.
  328. context.RequireCompleteType(
  329. context.insts().GetAs<SemIR::AnyParam>(param_ref_id).type_id,
  330. context.insts().GetLocId(param_ref_id), [&] {
  331. CARBON_DIAGNOSTIC(
  332. IncompleteTypeInFunctionParam, Error,
  333. "parameter has incomplete type {0} in function definition",
  334. TypeOfInstId);
  335. return context.emitter().Build(
  336. param_ref_id, IncompleteTypeInFunctionParam, param_ref_id);
  337. });
  338. }
  339. }
  340. // Processes a function definition after a signature for which we have already
  341. // built a function ID. This logic is shared between processing regular function
  342. // definitions and delayed parsing of inline method definitions.
  343. static auto HandleFunctionDefinitionAfterSignature(
  344. Context& context, Parse::FunctionDefinitionStartId node_id,
  345. SemIR::FunctionId function_id, SemIR::InstId decl_id) -> void {
  346. auto& function = context.functions().Get(function_id);
  347. // Create the function scope and the entry block.
  348. context.return_scope_stack().push_back({.decl_id = decl_id});
  349. context.inst_block_stack().Push();
  350. context.PushRegion(context.inst_block_stack().PeekOrAdd());
  351. context.scope_stack().Push(decl_id);
  352. StartGenericDefinition(context);
  353. CheckFunctionDefinitionSignature(context, function);
  354. context.node_stack().Push(node_id, function_id);
  355. }
  356. auto HandleFunctionDefinitionSuspend(Context& context,
  357. Parse::FunctionDefinitionStartId node_id)
  358. -> SuspendedFunction {
  359. // Process the declaration portion of the function.
  360. auto [function_id, decl_id] =
  361. BuildFunctionDecl(context, node_id, /*is_definition=*/true);
  362. return {.function_id = function_id,
  363. .decl_id = decl_id,
  364. .saved_name_state = context.decl_name_stack().Suspend()};
  365. }
  366. auto HandleFunctionDefinitionResume(Context& context,
  367. Parse::FunctionDefinitionStartId node_id,
  368. SuspendedFunction suspended_fn) -> void {
  369. context.decl_name_stack().Restore(suspended_fn.saved_name_state);
  370. HandleFunctionDefinitionAfterSignature(
  371. context, node_id, suspended_fn.function_id, suspended_fn.decl_id);
  372. }
  373. auto HandleParseNode(Context& context, Parse::FunctionDefinitionStartId node_id)
  374. -> bool {
  375. // Process the declaration portion of the function.
  376. auto [function_id, decl_id] =
  377. BuildFunctionDecl(context, node_id, /*is_definition=*/true);
  378. HandleFunctionDefinitionAfterSignature(context, node_id, function_id,
  379. decl_id);
  380. return true;
  381. }
  382. auto HandleParseNode(Context& context, Parse::FunctionDefinitionId node_id)
  383. -> bool {
  384. SemIR::FunctionId function_id =
  385. context.node_stack().Pop<Parse::NodeKind::FunctionDefinitionStart>();
  386. // If the `}` of the function is reachable, reject if we need a return value
  387. // and otherwise add an implicit `return;`.
  388. if (context.is_current_position_reachable()) {
  389. if (context.functions()
  390. .Get(function_id)
  391. .return_slot_pattern_id.is_valid()) {
  392. CARBON_DIAGNOSTIC(
  393. MissingReturnStatement, Error,
  394. "missing `return` at end of function with declared return type");
  395. context.emitter().Emit(TokenOnly(node_id), MissingReturnStatement);
  396. } else {
  397. context.AddInst<SemIR::Return>(node_id, {});
  398. }
  399. }
  400. context.scope_stack().Pop();
  401. context.inst_block_stack().Pop();
  402. context.return_scope_stack().pop_back();
  403. context.decl_name_stack().PopScope();
  404. auto& function = context.functions().Get(function_id);
  405. function.body_block_ids = context.PopRegion();
  406. // If this is a generic function, collect information about the definition.
  407. FinishGenericDefinition(context, function.generic_id);
  408. return true;
  409. }
  410. auto HandleParseNode(Context& context,
  411. Parse::BuiltinFunctionDefinitionStartId node_id) -> bool {
  412. // Process the declaration portion of the function.
  413. auto [function_id, _] =
  414. BuildFunctionDecl(context, node_id, /*is_definition=*/true);
  415. context.node_stack().Push(node_id, function_id);
  416. return true;
  417. }
  418. auto HandleParseNode(Context& context, Parse::BuiltinNameId node_id) -> bool {
  419. context.node_stack().Push(node_id);
  420. return true;
  421. }
  422. // Looks up a builtin function kind given its name as a string.
  423. // TODO: Move this out to another file.
  424. static auto LookupBuiltinFunctionKind(Context& context,
  425. Parse::BuiltinNameId name_id)
  426. -> SemIR::BuiltinFunctionKind {
  427. auto builtin_name = context.string_literal_values().Get(
  428. context.tokens().GetStringLiteralValue(
  429. context.parse_tree().node_token(name_id)));
  430. auto kind = SemIR::BuiltinFunctionKind::ForBuiltinName(builtin_name);
  431. if (kind == SemIR::BuiltinFunctionKind::None) {
  432. CARBON_DIAGNOSTIC(UnknownBuiltinFunctionName, Error,
  433. "unknown builtin function name \"{0}\"", std::string);
  434. context.emitter().Emit(name_id, UnknownBuiltinFunctionName,
  435. builtin_name.str());
  436. }
  437. return kind;
  438. }
  439. // Returns whether `function` is a valid declaration of `builtin_kind`.
  440. static auto IsValidBuiltinDeclaration(Context& context,
  441. const SemIR::Function& function,
  442. SemIR::BuiltinFunctionKind builtin_kind)
  443. -> bool {
  444. // Form the list of parameter types for the declaration.
  445. llvm::SmallVector<SemIR::TypeId> param_type_ids;
  446. auto implicit_param_patterns =
  447. context.inst_blocks().GetOrEmpty(function.implicit_param_patterns_id);
  448. auto param_patterns =
  449. context.inst_blocks().GetOrEmpty(function.param_patterns_id);
  450. param_type_ids.reserve(implicit_param_patterns.size() +
  451. param_patterns.size());
  452. for (auto param_id : llvm::concat<const SemIR::InstId>(
  453. implicit_param_patterns, param_patterns)) {
  454. // TODO: We also need to track whether the parameter is declared with
  455. // `var`.
  456. param_type_ids.push_back(context.insts().Get(param_id).type_id());
  457. }
  458. // Get the return type. This is `()` if none was specified.
  459. auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
  460. if (!return_type_id.is_valid()) {
  461. return_type_id = context.GetTupleType({});
  462. }
  463. return builtin_kind.IsValidType(context.sem_ir(), param_type_ids,
  464. return_type_id);
  465. }
  466. auto HandleParseNode(Context& context,
  467. Parse::BuiltinFunctionDefinitionId /*node_id*/) -> bool {
  468. auto name_id =
  469. context.node_stack().PopForSoloNodeId<Parse::NodeKind::BuiltinName>();
  470. auto [fn_node_id, function_id] =
  471. context.node_stack()
  472. .PopWithNodeId<Parse::NodeKind::BuiltinFunctionDefinitionStart>();
  473. auto builtin_kind = LookupBuiltinFunctionKind(context, name_id);
  474. if (builtin_kind != SemIR::BuiltinFunctionKind::None) {
  475. auto& function = context.functions().Get(function_id);
  476. CheckFunctionDefinitionSignature(context, function);
  477. if (IsValidBuiltinDeclaration(context, function, builtin_kind)) {
  478. function.builtin_function_kind = builtin_kind;
  479. // Build an empty generic definition if this is a generic builtin.
  480. StartGenericDefinition(context);
  481. FinishGenericDefinition(context, function.generic_id);
  482. } else {
  483. CARBON_DIAGNOSTIC(InvalidBuiltinSignature, Error,
  484. "invalid signature for builtin function \"{0}\"",
  485. std::string);
  486. context.emitter().Emit(fn_node_id, InvalidBuiltinSignature,
  487. builtin_kind.name().str());
  488. }
  489. }
  490. context.decl_name_stack().PopScope();
  491. return true;
  492. }
  493. } // namespace Carbon::Check