handle_name.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/check/context.h"
  5. #include "toolchain/check/generic.h"
  6. #include "toolchain/check/handle.h"
  7. #include "toolchain/check/member_access.h"
  8. #include "toolchain/check/name_component.h"
  9. #include "toolchain/check/pointer_dereference.h"
  10. #include "toolchain/lex/token_kind.h"
  11. #include "toolchain/sem_ir/inst.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::Check {
  14. auto HandleParseNode(Context& context, Parse::MemberAccessExprId node_id)
  15. -> bool {
  16. if (context.node_stack().PeekIs<Parse::NodeKind::ParenExpr>()) {
  17. auto member_expr_id = context.node_stack().PopExpr();
  18. auto base_id = context.node_stack().PopExpr();
  19. auto member_id =
  20. PerformCompoundMemberAccess(context, node_id, base_id, member_expr_id);
  21. context.node_stack().Push(node_id, member_id);
  22. } else {
  23. SemIR::NameId name_id = context.node_stack().PopName();
  24. auto base_id = context.node_stack().PopExpr();
  25. auto member_id = PerformMemberAccess(context, node_id, base_id, name_id);
  26. context.node_stack().Push(node_id, member_id);
  27. }
  28. return true;
  29. }
  30. auto HandleParseNode(Context& context, Parse::PointerMemberAccessExprId node_id)
  31. -> bool {
  32. auto diagnose_not_pointer = [&context,
  33. &node_id](SemIR::TypeId not_pointer_type_id) {
  34. CARBON_DIAGNOSTIC(ArrowOperatorOfNonPointer, Error,
  35. "Cannot apply `->` operator to non-pointer type `{0}`.",
  36. SemIR::TypeId);
  37. auto builder = context.emitter().Build(
  38. TokenOnly(node_id), ArrowOperatorOfNonPointer, not_pointer_type_id);
  39. builder.Emit();
  40. };
  41. if (context.node_stack().PeekIs<Parse::NodeKind::ParenExpr>()) {
  42. auto member_expr_id = context.node_stack().PopExpr();
  43. auto base_id = context.node_stack().PopExpr();
  44. auto deref_base_id = PerformPointerDereference(context, node_id, base_id,
  45. diagnose_not_pointer);
  46. auto member_id = PerformCompoundMemberAccess(context, node_id,
  47. deref_base_id, member_expr_id);
  48. context.node_stack().Push(node_id, member_id);
  49. } else {
  50. SemIR::NameId name_id = context.node_stack().PopName();
  51. auto base_id = context.node_stack().PopExpr();
  52. auto deref_base_id = PerformPointerDereference(context, node_id, base_id,
  53. diagnose_not_pointer);
  54. auto member_id =
  55. PerformMemberAccess(context, node_id, deref_base_id, name_id);
  56. context.node_stack().Push(node_id, member_id);
  57. }
  58. return true;
  59. }
  60. static auto GetIdentifierAsName(Context& context, Parse::NodeId node_id)
  61. -> std::optional<SemIR::NameId> {
  62. auto token = context.parse_tree().node_token(node_id);
  63. if (context.tokens().GetKind(token) != Lex::TokenKind::Identifier) {
  64. CARBON_CHECK(context.parse_tree().node_has_error(node_id));
  65. return std::nullopt;
  66. }
  67. return SemIR::NameId::ForIdentifier(context.tokens().GetIdentifier(token));
  68. }
  69. // Handle a name that is used as an expression by performing unqualified name
  70. // lookup.
  71. static auto HandleNameAsExpr(Context& context, Parse::NodeId node_id,
  72. SemIR::NameId name_id) -> bool {
  73. auto result = context.LookupUnqualifiedName(node_id, name_id);
  74. auto value = context.insts().Get(result.inst_id);
  75. auto type_id = SemIR::GetTypeInSpecific(context.sem_ir(), result.specific_id,
  76. value.type_id());
  77. CARBON_CHECK(type_id.is_valid()) << "Missing type for " << value;
  78. // If the named entity has a constant value that depends on its specific,
  79. // store the specific too.
  80. if (result.specific_id.is_valid() &&
  81. context.constant_values().Get(result.inst_id).is_symbolic()) {
  82. result.inst_id = context.AddInst<SemIR::SpecificConstant>(
  83. node_id, {.type_id = type_id,
  84. .inst_id = result.inst_id,
  85. .specific_id = result.specific_id});
  86. }
  87. context.AddInstAndPush<SemIR::NameRef>(
  88. node_id,
  89. {.type_id = type_id, .name_id = name_id, .value_id = result.inst_id});
  90. return true;
  91. }
  92. auto HandleParseNode(Context& context, Parse::IdentifierNameId node_id)
  93. -> bool {
  94. // The parent is responsible for binding the name.
  95. auto name_id = GetIdentifierAsName(context, node_id);
  96. if (!name_id) {
  97. return context.TODO(node_id, "Error recovery from keyword name.");
  98. }
  99. context.node_stack().Push(node_id, *name_id);
  100. return true;
  101. }
  102. auto HandleParseNode(Context& context, Parse::IdentifierNameExprId node_id)
  103. -> bool {
  104. auto name_id = GetIdentifierAsName(context, node_id);
  105. if (!name_id) {
  106. return context.TODO(node_id, "Error recovery from keyword name.");
  107. }
  108. return HandleNameAsExpr(context, node_id, *name_id);
  109. }
  110. auto HandleParseNode(Context& context, Parse::BaseNameId node_id) -> bool {
  111. context.node_stack().Push(node_id, SemIR::NameId::Base);
  112. return true;
  113. }
  114. auto HandleParseNode(Context& context, Parse::SelfTypeNameExprId node_id)
  115. -> bool {
  116. return HandleNameAsExpr(context, node_id, SemIR::NameId::SelfType);
  117. }
  118. auto HandleParseNode(Context& context, Parse::SelfValueNameId node_id) -> bool {
  119. context.node_stack().Push(node_id, SemIR::NameId::SelfValue);
  120. return true;
  121. }
  122. auto HandleParseNode(Context& context, Parse::SelfValueNameExprId node_id)
  123. -> bool {
  124. return HandleNameAsExpr(context, node_id, SemIR::NameId::SelfValue);
  125. }
  126. auto HandleParseNode(Context& context, Parse::NameQualifierId /*node_id*/)
  127. -> bool {
  128. context.decl_name_stack().ApplyNameQualifier(PopNameComponent(context));
  129. return true;
  130. }
  131. auto HandleParseNode(Context& context, Parse::PackageExprId node_id) -> bool {
  132. context.AddInstAndPush<SemIR::NameRef>(
  133. node_id,
  134. {.type_id = context.GetBuiltinType(SemIR::BuiltinInstKind::NamespaceType),
  135. .name_id = SemIR::NameId::PackageNamespace,
  136. .value_id = SemIR::InstId::PackageNamespace});
  137. return true;
  138. }
  139. } // namespace Carbon::Check