handle_function.cpp 22 KB

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