merge.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // Diagnoses an `extern` declaration that was not preceded by a declaration in
  11. // the API file.
  12. auto DiagnoseExternRequiresDeclInApiFile(Context& context, SemIR::LocId loc_id)
  13. -> void;
  14. // Information on new and previous declarations for DiagnoseIfInvalidRedecl.
  15. struct RedeclInfo {
  16. explicit RedeclInfo(const SemIR::EntityWithParamsBase& params,
  17. SemIR::LocId loc_id, bool is_definition)
  18. : loc_id(loc_id),
  19. is_definition(is_definition),
  20. is_extern(params.is_extern),
  21. extern_library_id(params.extern_library_id) {}
  22. // The associated diagnostic location.
  23. SemIR::LocId loc_id;
  24. // True if a definition.
  25. bool is_definition;
  26. // True if an `extern` declaration.
  27. bool is_extern;
  28. // The library name in `extern library`, or `None` if not present.
  29. SemIR::LibraryNameId extern_library_id;
  30. };
  31. // Checks for various invalid redeclarations. This can emit diagnostics.
  32. // However, merging is still often appropriate for error recovery, so this
  33. // doesn't return whether a diagnostic occurred.
  34. //
  35. // The kinds of things this verifies are:
  36. // - A declaration is not redundant.
  37. // - A definition doesn't redefine a prior definition.
  38. // - The use of `extern` is consistent within a library.
  39. // - Multiple libraries do not declare non-`extern`.
  40. auto DiagnoseIfInvalidRedecl(Context& context, Lex::TokenKind decl_kind,
  41. SemIR::NameId name_id, RedeclInfo new_decl,
  42. RedeclInfo prev_decl,
  43. SemIR::ImportIRId prev_import_ir_id) -> void;
  44. // When the prior name lookup result is an import and we are successfully
  45. // merging, replace the name lookup result with the reference in the current
  46. // file.
  47. auto ReplacePrevInstForMerge(Context& context, SemIR::NameScopeId scope_id,
  48. SemIR::NameId name_id, SemIR::InstId new_inst_id)
  49. -> void;
  50. // Information about the parameters of a declaration, which is common across
  51. // different kinds of entity such as classes and functions.
  52. struct DeclParams {
  53. explicit DeclParams(const SemIR::EntityWithParamsBase& base)
  54. : loc_id(base.latest_decl_id()),
  55. first_param_node_id(base.first_param_node_id),
  56. last_param_node_id(base.last_param_node_id),
  57. implicit_param_patterns_id(base.implicit_param_patterns_id),
  58. param_patterns_id(base.param_patterns_id) {}
  59. DeclParams(SemIR::LocId loc_id, Parse::NodeId first_param_node_id,
  60. Parse::NodeId last_param_node_id,
  61. SemIR::InstBlockId implicit_param_patterns_id,
  62. SemIR::InstBlockId param_patterns_id)
  63. : loc_id(loc_id),
  64. first_param_node_id(first_param_node_id),
  65. last_param_node_id(last_param_node_id),
  66. implicit_param_patterns_id(implicit_param_patterns_id),
  67. param_patterns_id(param_patterns_id) {}
  68. // The location of the declaration of the entity.
  69. SemIR::LocId loc_id;
  70. // Parse tree bounds for the parameters, including both implicit and explicit
  71. // parameters. These will be compared to match between declaration and
  72. // definition.
  73. Parse::NodeId first_param_node_id;
  74. Parse::NodeId last_param_node_id;
  75. // The implicit parameters of the entity. Can be `None` if there is no
  76. // implicit parameter list.
  77. SemIR::InstBlockId implicit_param_patterns_id;
  78. // The explicit parameters of the entity. Can be `None` if there is no
  79. // explicit parameter list.
  80. SemIR::InstBlockId param_patterns_id;
  81. };
  82. // Checks that the parameters in a redeclaration of an entity match the
  83. // parameters in the prior declaration. If not, produces a diagnostic if
  84. // `diagnose` is true, and returns false. If `check_self` is false,
  85. // type and name mismatches will not be diagnosed for the `self` parameter
  86. // (if any), but form mismatches will still be diagnosed.
  87. auto CheckRedeclParamsMatch(Context& context, const DeclParams& new_entity,
  88. const DeclParams& prev_entity,
  89. SemIR::SpecificId prev_specific_id, bool diagnose,
  90. bool check_syntax, bool check_self) -> bool;
  91. inline auto CheckRedeclParamsMatch(Context& context,
  92. const DeclParams& new_entity,
  93. const DeclParams& prev_entity) -> bool {
  94. return CheckRedeclParamsMatch(context, new_entity, prev_entity,
  95. SemIR::SpecificId::None, /*diagnose=*/true,
  96. /*check_syntax=*/true, /*check_self=*/true);
  97. }
  98. } // namespace Carbon::Check
  99. #endif // CARBON_TOOLCHAIN_CHECK_MERGE_H_