handle_name.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/inst.h"
  8. #include "toolchain/check/member_access.h"
  9. #include "toolchain/check/name_component.h"
  10. #include "toolchain/check/name_lookup.h"
  11. #include "toolchain/check/pointer_dereference.h"
  12. #include "toolchain/check/type.h"
  13. #include "toolchain/lex/token_kind.h"
  14. #include "toolchain/sem_ir/inst.h"
  15. #include "toolchain/sem_ir/typed_insts.h"
  16. namespace Carbon::Check {
  17. auto HandleParseNode(Context& context, Parse::MemberAccessExprId node_id)
  18. -> bool {
  19. auto node_kind = context.node_stack().PeekNodeKind();
  20. if (node_kind == Parse::NodeKind::ParenExpr) {
  21. auto member_expr_id = context.node_stack().PopExpr();
  22. auto base_id = context.node_stack().PopExpr();
  23. auto member_id =
  24. PerformCompoundMemberAccess(context, node_id, base_id, member_expr_id);
  25. context.node_stack().Push(node_id, member_id);
  26. } else if (node_kind == Parse::NodeKind::IntLiteral) {
  27. auto index_inst_id = context.node_stack().PopExpr();
  28. auto tuple_inst_id = context.node_stack().PopExpr();
  29. auto tuple_value_inst_id =
  30. PerformTupleAccess(context, node_id, tuple_inst_id, index_inst_id);
  31. context.node_stack().Push(node_id, tuple_value_inst_id);
  32. } else {
  33. SemIR::NameId name_id = context.node_stack().PopName();
  34. auto base_id = context.node_stack().PopExpr();
  35. auto member_id = PerformMemberAccess(context, node_id, base_id, name_id);
  36. context.node_stack().Push(node_id, member_id);
  37. }
  38. return true;
  39. }
  40. auto HandleParseNode(Context& context, Parse::PointerMemberAccessExprId node_id)
  41. -> bool {
  42. auto diagnose_not_pointer = [&context,
  43. &node_id](SemIR::TypeId not_pointer_type_id) {
  44. // TODO: Pass in the expression we're trying to dereference to produce a
  45. // better diagnostic.
  46. CARBON_DIAGNOSTIC(ArrowOperatorOfNonPointer, Error,
  47. "cannot apply `->` operator to non-pointer type {0}",
  48. SemIR::TypeId);
  49. auto builder = context.emitter().Build(
  50. TokenOnly(node_id), ArrowOperatorOfNonPointer, not_pointer_type_id);
  51. builder.Emit();
  52. };
  53. auto node_kind = context.node_stack().PeekNodeKind();
  54. if (node_kind == Parse::NodeKind::ParenExpr) {
  55. auto member_expr_id = context.node_stack().PopExpr();
  56. auto base_id = context.node_stack().PopExpr();
  57. auto deref_base_id = PerformPointerDereference(context, node_id, base_id,
  58. diagnose_not_pointer);
  59. auto member_id = PerformCompoundMemberAccess(context, node_id,
  60. deref_base_id, member_expr_id);
  61. context.node_stack().Push(node_id, member_id);
  62. } else if (node_kind == Parse::NodeKind::IntLiteral) {
  63. auto index_inst_id = context.node_stack().PopExpr();
  64. auto tuple_pointer_inst_id = context.node_stack().PopExpr();
  65. auto tuple_inst_id = PerformPointerDereference(
  66. context, node_id, tuple_pointer_inst_id, diagnose_not_pointer);
  67. auto tuple_value_inst_id =
  68. PerformTupleAccess(context, node_id, tuple_inst_id, index_inst_id);
  69. context.node_stack().Push(node_id, tuple_value_inst_id);
  70. } else {
  71. SemIR::NameId name_id = context.node_stack().PopName();
  72. auto base_id = context.node_stack().PopExpr();
  73. auto deref_base_id = PerformPointerDereference(context, node_id, base_id,
  74. diagnose_not_pointer);
  75. auto member_id =
  76. PerformMemberAccess(context, node_id, deref_base_id, name_id);
  77. context.node_stack().Push(node_id, member_id);
  78. }
  79. return true;
  80. }
  81. static auto GetIdentifierAsName(Context& context, Parse::NodeId node_id)
  82. -> SemIR::NameId {
  83. CARBON_CHECK(!context.parse_tree().node_has_error(node_id),
  84. "TODO: Support checking error parse nodes");
  85. auto token = context.parse_tree().node_token(node_id);
  86. return SemIR::NameId::ForIdentifier(context.tokens().GetIdentifier(token));
  87. }
  88. // Handle a name that is used as an expression by performing unqualified name
  89. // lookup.
  90. static auto HandleNameAsExpr(Context& context, Parse::NodeId node_id,
  91. SemIR::NameId name_id) -> SemIR::InstId {
  92. auto result = LookupUnqualifiedName(context, node_id, name_id);
  93. SemIR::InstId inst_id = result.scope_result.target_inst_id();
  94. auto value = context.insts().Get(inst_id);
  95. auto type_id = SemIR::GetTypeInSpecific(context.sem_ir(), result.specific_id,
  96. value.type_id());
  97. CARBON_CHECK(type_id.has_value(), "Missing type for {0}", value);
  98. // If the named entity has a constant value that depends on its specific,
  99. // store the specific too.
  100. if (result.specific_id.has_value() &&
  101. context.constant_values().Get(inst_id).is_symbolic()) {
  102. inst_id =
  103. AddInst<SemIR::SpecificConstant>(context, node_id,
  104. {.type_id = type_id,
  105. .inst_id = inst_id,
  106. .specific_id = result.specific_id});
  107. }
  108. return AddInst<SemIR::NameRef>(
  109. context, node_id,
  110. {.type_id = type_id, .name_id = name_id, .value_id = inst_id});
  111. }
  112. auto HandleParseNode(Context& context,
  113. Parse::IdentifierNameNotBeforeParamsId node_id) -> bool {
  114. // The parent is responsible for binding the name.
  115. context.node_stack().Push(node_id, GetIdentifierAsName(context, node_id));
  116. return true;
  117. }
  118. auto HandleParseNode(Context& context,
  119. Parse::IdentifierNameBeforeParamsId node_id) -> bool {
  120. // Push a pattern block stack entry to handle the parameter pattern.
  121. context.pattern_block_stack().Push();
  122. context.full_pattern_stack().PushFullPattern(
  123. FullPatternStack::Kind::ImplicitParamList);
  124. // The parent is responsible for binding the name.
  125. context.node_stack().Push(node_id, GetIdentifierAsName(context, node_id));
  126. return true;
  127. }
  128. auto HandleParseNode(Context& context, Parse::IdentifierNameExprId node_id)
  129. -> bool {
  130. auto name_id = GetIdentifierAsName(context, node_id);
  131. context.node_stack().Push(node_id,
  132. HandleNameAsExpr(context, node_id, name_id));
  133. return true;
  134. }
  135. auto HandleParseNode(Context& context, Parse::BaseNameId node_id) -> bool {
  136. context.node_stack().Push(node_id, SemIR::NameId::Base);
  137. return true;
  138. }
  139. auto HandleParseNode(Context& context, Parse::SelfTypeNameId node_id) -> bool {
  140. context.node_stack().Push(node_id, SemIR::NameId::SelfType);
  141. return true;
  142. }
  143. auto HandleParseNode(Context& context, Parse::SelfTypeNameExprId node_id)
  144. -> bool {
  145. context.node_stack().Push(
  146. node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::SelfType));
  147. return true;
  148. }
  149. auto HandleParseNode(Context& context, Parse::SelfValueNameId node_id) -> bool {
  150. context.node_stack().Push(node_id, SemIR::NameId::SelfValue);
  151. return true;
  152. }
  153. auto HandleParseNode(Context& context, Parse::SelfValueNameExprId node_id)
  154. -> bool {
  155. context.node_stack().Push(
  156. node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::SelfValue));
  157. return true;
  158. }
  159. auto HandleParseNode(Context& context,
  160. Parse::NameQualifierWithParamsId /*node_id*/) -> bool {
  161. context.decl_name_stack().ApplyNameQualifier(PopNameComponent(context));
  162. return true;
  163. }
  164. auto HandleParseNode(Context& context,
  165. Parse::NameQualifierWithoutParamsId /*node_id*/) -> bool {
  166. context.decl_name_stack().ApplyNameQualifier(PopNameComponent(context));
  167. return true;
  168. }
  169. auto HandleParseNode(Context& context, Parse::DesignatorExprId node_id)
  170. -> bool {
  171. SemIR::NameId name_id = context.node_stack().PopName();
  172. if (name_id == SemIR::NameId::SelfType) {
  173. // Look up `.Self`.
  174. SemIR::InstId period_self_id =
  175. HandleNameAsExpr(context, node_id, SemIR::NameId::PeriodSelf);
  176. context.node_stack().Push(node_id, period_self_id);
  177. } else {
  178. // Otherwise this is `.Member`, so look up `.Self` and then `Member` in
  179. // `.Self`.
  180. SemIR::InstId period_self_id = SemIR::InstId::None;
  181. {
  182. // TODO: Instead of annotating the diagnostic, should change
  183. // `HandleNameAsExpr` to optionally allow us to produce the diagnostic
  184. // instead so we can generate a "name `.Self` implicitly referenced by
  185. // designated expression, but not found" diagnostic instead of adding a
  186. // note to the current "name `.Self` not found" message.
  187. DiagnosticAnnotationScope annotate_diagnostics(
  188. &context.emitter(), [&](auto& builder) {
  189. CARBON_DIAGNOSTIC(
  190. NoPeriodSelfForDesignator, Note,
  191. "designator may only be used when `.Self` is in scope");
  192. builder.Note(SemIR::LocId::None, NoPeriodSelfForDesignator);
  193. });
  194. period_self_id =
  195. HandleNameAsExpr(context, node_id, SemIR::NameId::PeriodSelf);
  196. }
  197. auto member_id =
  198. PerformMemberAccess(context, node_id, period_self_id, name_id);
  199. context.node_stack().Push(node_id, member_id);
  200. }
  201. return true;
  202. }
  203. auto HandleParseNode(Context& context, Parse::PackageExprId node_id) -> bool {
  204. AddInstAndPush<SemIR::NameRef>(
  205. context, node_id,
  206. {.type_id =
  207. GetSingletonType(context, SemIR::NamespaceType::SingletonInstId),
  208. .name_id = SemIR::NameId::PackageNamespace,
  209. .value_id = SemIR::Namespace::PackageInstId});
  210. return true;
  211. }
  212. auto HandleParseNode(Context& context, Parse::CoreNameExprId node_id) -> bool {
  213. // TODO: Unqualified lookup will never find anything; perform lookup directly
  214. // into file scope.
  215. context.node_stack().Push(
  216. node_id, HandleNameAsExpr(context, node_id, SemIR::NameId::Core));
  217. return true;
  218. }
  219. } // namespace Carbon::Check