handle_function.cpp 22 KB

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