name_lookup.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. // Information about a scope in which we can perform name lookup.
  12. struct LookupScope {
  13. // The name scope in which names are searched.
  14. SemIR::NameScopeId name_scope_id;
  15. // The specific for the name scope, or `None` if the name scope is not
  16. // defined by a generic or we should perform lookup into the generic itself.
  17. SemIR::SpecificId specific_id;
  18. };
  19. // A result produced by name lookup.
  20. struct LookupResult {
  21. // The specific in which the lookup result was found. `None` if the result
  22. // was not found in a specific.
  23. SemIR::SpecificId specific_id;
  24. // The result from the lookup in the scope.
  25. SemIR::ScopeLookupResult scope_result;
  26. };
  27. // Information about an access.
  28. struct AccessInfo {
  29. // The constant being accessed.
  30. SemIR::ConstantId constant_id;
  31. // The highest allowed access for a lookup. For example, `Protected` allows
  32. // access to `Public` and `Protected` names, but not `Private`.
  33. SemIR::AccessKind highest_allowed_access;
  34. };
  35. // Adds a name to name lookup. Prints a diagnostic for name conflicts. If
  36. // specified, `scope_index` specifies which lexical scope the name is inserted
  37. // into, otherwise the name is inserted into the current scope.
  38. auto AddNameToLookup(Context& context, SemIR::NameId name_id,
  39. SemIR::InstId target_id,
  40. ScopeIndex scope_index = ScopeIndex::None) -> void;
  41. // Performs name lookup in a specified scope for a name appearing in a
  42. // declaration. If scope_id is `None`, performs lookup into the lexical scope
  43. // specified by scope_index instead.
  44. auto LookupNameInDecl(Context& context, SemIR::LocId loc_id,
  45. SemIR::NameId name_id, SemIR::NameScopeId scope_id,
  46. ScopeIndex scope_index) -> SemIR::ScopeLookupResult;
  47. // Performs an unqualified name lookup, returning the referenced `InstId`.
  48. auto LookupUnqualifiedName(Context& context, Parse::NodeId node_id,
  49. SemIR::NameId name_id, bool required = true)
  50. -> LookupResult;
  51. // Performs a name lookup in a specified scope, returning the referenced
  52. // `InstId`. Does not look into extended scopes. Returns `InstId::None` if the
  53. // name is not found.
  54. //
  55. // If `is_being_declared` is false, then this is a regular name lookup, and
  56. // the name will be poisoned if not found so that later lookups will fail; a
  57. // poisoned name will be treated as if it is not declared. Otherwise, this is
  58. // a lookup for a name being declared, so the name will not be poisoned, but
  59. // poison will be returned if it's already been looked up.
  60. //
  61. // If `name_id` is not an identifier, the name will not be poisoned.
  62. auto LookupNameInExactScope(Context& context, SemIR::LocId loc_id,
  63. SemIR::NameId name_id, SemIR::NameScopeId scope_id,
  64. SemIR::NameScope& scope,
  65. bool is_being_declared = false)
  66. -> SemIR::ScopeLookupResult;
  67. // Appends the lookup scopes corresponding to `base_const_id` to `*scopes`.
  68. // Returns `false` if not a scope. On invalid scopes, prints a diagnostic, but
  69. // still updates `*scopes` and returns `true`.
  70. auto AppendLookupScopesForConstant(Context& context, SemIR::LocId loc_id,
  71. SemIR::ConstantId base_const_id,
  72. llvm::SmallVector<LookupScope>* scopes)
  73. -> bool;
  74. // Performs a qualified name lookup in a specified scopes and in scopes that
  75. // they extend, returning the referenced `InstId`.
  76. auto LookupQualifiedName(Context& context, SemIR::LocId loc_id,
  77. SemIR::NameId name_id,
  78. llvm::ArrayRef<LookupScope> lookup_scopes,
  79. bool required = true,
  80. std::optional<AccessInfo> access_info = std::nullopt)
  81. -> LookupResult;
  82. // Returns the `InstId` corresponding to a name in the core package, or
  83. // BuiltinErrorInst if not found.
  84. auto LookupNameInCore(Context& context, SemIR::LocId loc_id,
  85. llvm::StringRef name) -> SemIR::InstId;
  86. // Prints a diagnostic for a duplicate name.
  87. auto DiagnoseDuplicateName(Context& context, SemIR::NameId name_id,
  88. SemIRLoc dup_def, SemIRLoc prev_def) -> void;
  89. // Prints a diagnostic for a poisoned name when it's later declared.
  90. auto DiagnosePoisonedName(Context& context, SemIR::NameId name_id,
  91. SemIR::LocId poisoning_loc_id,
  92. SemIR::LocId decl_name_loc_id) -> void;
  93. // Prints a diagnostic for a missing name.
  94. auto DiagnoseNameNotFound(Context& context, SemIRLoc loc, SemIR::NameId name_id)
  95. -> void;
  96. } // namespace Carbon::Check
  97. #endif // CARBON_TOOLCHAIN_CHECK_NAME_LOOKUP_H_