name_lookup.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_NAME_LOOKUP_H_
  5. #define CARBON_TOOLCHAIN_CHECK_NAME_LOOKUP_H_
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/check/context.h"
  9. #include "toolchain/check/core_identifier.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. namespace Carbon::Check {
  12. // Information about a scope in which we can perform name lookup.
  13. struct LookupScope {
  14. // The name scope in which names are searched.
  15. SemIR::NameScopeId name_scope_id;
  16. // The specific for the name scope, or `None` if the name scope is not
  17. // defined by a generic or we should perform lookup into the generic itself.
  18. SemIR::SpecificId specific_id;
  19. // The self-type where lookup is happening when the lookup is for a member
  20. // access.
  21. SemIR::ConstantId self_const_id;
  22. };
  23. // A result produced by name lookup.
  24. struct LookupResult {
  25. // The specific in which the lookup result was found. `None` if the result
  26. // was not found in a specific.
  27. SemIR::SpecificId specific_id;
  28. // The result from the lookup in the scope.
  29. SemIR::ScopeLookupResult scope_result;
  30. };
  31. // Information about an access.
  32. struct AccessInfo {
  33. // The constant being accessed.
  34. SemIR::ConstantId constant_id;
  35. // The highest allowed access for a lookup. For example, `Protected` allows
  36. // access to `Public` and `Protected` names, but not `Private`.
  37. SemIR::AccessKind highest_allowed_access;
  38. };
  39. // Adds a name to name lookup. Prints a diagnostic for name conflicts. If
  40. // specified, `scope_index` specifies which lexical scope the name is inserted
  41. // into, otherwise the name is inserted into the current scope.
  42. auto AddNameToLookup(Context& context, SemIR::NameId name_id,
  43. SemIR::InstId target_id,
  44. ScopeIndex scope_index = ScopeIndex::None) -> void;
  45. // Performs name lookup in a specified scope for a name appearing in a
  46. // declaration. If scope_id is `None`, performs lookup into the lexical scope
  47. // specified by scope_index instead.
  48. auto LookupNameInDecl(Context& context, SemIR::LocId loc_id,
  49. SemIR::NameId name_id, SemIR::NameScopeId scope_id,
  50. ScopeIndex scope_index) -> SemIR::ScopeLookupResult;
  51. // Performs an unqualified name lookup, returning the referenced `InstId`.
  52. auto LookupUnqualifiedName(Context& context, SemIR::LocId loc_id,
  53. SemIR::NameId name_id, bool required = true)
  54. -> LookupResult;
  55. // Performs a name lookup in a specified scope, returning the referenced
  56. // `InstId`. Does not look into extended scopes. Returns `InstId::None` if the
  57. // name is not found.
  58. //
  59. // If `is_being_declared` is false, then this is a regular name lookup, and
  60. // the name will be poisoned if not found so that later lookups will fail; a
  61. // poisoned name will be treated as if it is not declared. Otherwise, this is
  62. // a lookup for a name being declared, so the name will not be poisoned, but
  63. // poison will be returned if it's already been looked up.
  64. //
  65. // If `name_id` is not an identifier, the name will not be poisoned.
  66. auto LookupNameInExactScope(Context& context, SemIR::LocId loc_id,
  67. SemIR::NameId name_id, SemIR::NameScopeId scope_id,
  68. SemIR::NameScope& scope,
  69. bool is_being_declared = false)
  70. -> SemIR::ScopeLookupResult;
  71. // Appends the lookup scopes corresponding to `lookup_const_id` to `*scopes`.
  72. //
  73. // The `self_type_const_id` is the self-type that we are looking for a name in,
  74. // and which is passed through to extended scopes. It may be a facet or a value
  75. // of type `type`. Some extended scopes have a symbolic `Self` internally which
  76. // needs to know the self-type in order to produce a correct specific scope in
  77. // the result.
  78. //
  79. // Returns `false` if not a scope. On invalid scopes, prints a diagnostic, but
  80. // still updates `*scopes` and returns `true`.
  81. auto AppendLookupScopesForConstant(Context& context, SemIR::LocId loc_id,
  82. SemIR::ConstantId lookup_const_id,
  83. SemIR::ConstantId self_type_const_id,
  84. llvm::SmallVector<LookupScope>* scopes)
  85. -> bool;
  86. // Performs a qualified name lookup in a specified scopes and in scopes that
  87. // they extend, returning the referenced `InstId`.
  88. auto LookupQualifiedName(Context& context, SemIR::LocId loc_id,
  89. SemIR::NameId name_id,
  90. llvm::ArrayRef<LookupScope> lookup_scopes,
  91. bool required = true,
  92. std::optional<AccessInfo> access_info = std::nullopt)
  93. -> LookupResult;
  94. // Returns the `InstId` corresponding to a qualified name in the `Core` package,
  95. // or BuiltinErrorInst if not found.
  96. auto LookupNameInCore(Context& context, SemIR::LocId loc_id,
  97. llvm::ArrayRef<CoreIdentifier> qualifiers)
  98. -> SemIR::InstId;
  99. // Returns the `InstId` corresponding to a name in the `Core`, or
  100. // BuiltinErrorInst if not found.
  101. inline auto LookupNameInCore(Context& context, SemIR::LocId loc_id,
  102. CoreIdentifier identifier) -> SemIR::InstId {
  103. return LookupNameInCore(context, loc_id, llvm::ArrayRef{identifier});
  104. }
  105. // Checks whether a name is accessible in the given access context. Produces a
  106. // diagnostic if not.
  107. auto CheckAccess(Context& context, SemIR::LocId loc_id,
  108. SemIR::LocId member_loc_id, SemIR::NameId name_id,
  109. SemIR::AccessKind access_kind, bool is_parent_access,
  110. AccessInfo access_info) -> void;
  111. // Prints a diagnostic for a duplicate name.
  112. auto DiagnoseDuplicateName(Context& context, SemIR::NameId name_id,
  113. SemIR::LocId dup_def, SemIR::LocId prev_def) -> void;
  114. // Prints a diagnostic for a poisoned name when it's later declared.
  115. auto DiagnosePoisonedName(Context& context, SemIR::NameId name_id,
  116. SemIR::LocId poisoning_loc_id,
  117. SemIR::LocId decl_name_loc_id) -> void;
  118. // Prints a diagnostic for a missing name.
  119. auto DiagnoseNameNotFound(Context& context, SemIR::LocId loc_id,
  120. SemIR::NameId name_id) -> void;
  121. } // namespace Carbon::Check
  122. #endif // CARBON_TOOLCHAIN_CHECK_NAME_LOOKUP_H_