merge.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef CARBON_TOOLCHAIN_CHECK_MERGE_H_
  5. #define CARBON_TOOLCHAIN_CHECK_MERGE_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/subst.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Information on new and previous declarations for CheckIsAllowedRedecl.
  11. struct RedeclInfo {
  12. // The associated diagnostic location.
  13. SemIRLoc loc;
  14. // True if a definition.
  15. bool is_definition;
  16. // True if an `extern` declaration.
  17. bool is_extern;
  18. };
  19. // Checks if a redeclaration is allowed prior to merging. This may emit a
  20. // diagnostic, but diagnostics do not prevent merging.
  21. //
  22. // The kinds of things this verifies are:
  23. // - A declaration is not redundant.
  24. // - A definition doesn't redefine a prior definition.
  25. // - The use of `extern` is consistent within a library.
  26. // - Multiple libraries do not declare non-`extern`.
  27. auto CheckIsAllowedRedecl(Context& context, Lex::TokenKind decl_kind,
  28. SemIR::NameId name_id, RedeclInfo new_decl,
  29. RedeclInfo prev_decl,
  30. SemIR::ImportIRId prev_import_ir_id) -> void;
  31. // When the prior name lookup result is an import and we are successfully
  32. // merging, replace the name lookup result with the reference in the current
  33. // file.
  34. auto ReplacePrevInstForMerge(Context& context, SemIR::NameScopeId scope_id,
  35. SemIR::NameId name_id, SemIR::InstId new_inst_id)
  36. -> void;
  37. // Information about the parameters of a declaration, which is common across
  38. // different kinds of entity such as classes and functions.
  39. struct DeclParams {
  40. explicit DeclParams(const SemIR::EntityWithParamsBase& base)
  41. : loc(base.decl_id),
  42. first_param_node_id(base.first_param_node_id),
  43. last_param_node_id(base.last_param_node_id),
  44. implicit_param_refs_id(base.implicit_param_refs_id),
  45. param_refs_id(base.param_refs_id) {}
  46. DeclParams(SemIRLoc loc, Parse::NodeId first_param_node_id,
  47. Parse::NodeId last_param_node_id,
  48. SemIR::InstBlockId implicit_params_id,
  49. SemIR::InstBlockId params_id)
  50. : loc(loc),
  51. first_param_node_id(first_param_node_id),
  52. last_param_node_id(last_param_node_id),
  53. implicit_param_refs_id(implicit_params_id),
  54. param_refs_id(params_id) {}
  55. // The location of the declaration of the entity.
  56. SemIRLoc loc;
  57. // Parse tree bounds for the parameters, including both implicit and explicit
  58. // parameters. These will be compared to match between declaration and
  59. // definition.
  60. Parse::NodeId first_param_node_id;
  61. Parse::NodeId last_param_node_id;
  62. // The implicit parameters of the entity. Can be Invalid if there is no
  63. // implicit parameter list.
  64. SemIR::InstBlockId implicit_param_refs_id;
  65. // The explicit parameters of the entity. Can be Invalid if there is no
  66. // explicit parameter list.
  67. SemIR::InstBlockId param_refs_id;
  68. };
  69. // Checks that the parameters in a redeclaration of an entity match the
  70. // parameters in the prior declaration. If not, produces a diagnostic and
  71. // returns false.
  72. auto CheckRedeclParamsMatch(
  73. Context& context, const DeclParams& new_entity,
  74. const DeclParams& prev_entity,
  75. SemIR::SpecificId prev_specific_id = SemIR::SpecificId::Invalid,
  76. bool check_syntax = true) -> bool;
  77. } // namespace Carbon::Check
  78. #endif // CARBON_TOOLCHAIN_CHECK_MERGE_H_