handle_name.cpp 6.9 KB

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