handle_function.cpp 22 KB

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