semantics_context.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/semantics/semantics_context.h"
  5. #include <utility>
  6. #include "common/vlog.h"
  7. #include "toolchain/diagnostics/diagnostic_kind.h"
  8. #include "toolchain/lexer/token_kind.h"
  9. #include "toolchain/lexer/tokenized_buffer.h"
  10. #include "toolchain/parser/parse_node_kind.h"
  11. #include "toolchain/semantics/semantics_ir.h"
  12. #include "toolchain/semantics/semantics_node.h"
  13. #include "toolchain/semantics/semantics_node_block_stack.h"
  14. namespace Carbon {
  15. SemanticsContext::SemanticsContext(const TokenizedBuffer& tokens,
  16. DiagnosticEmitter<ParseTree::Node>& emitter,
  17. const ParseTree& parse_tree,
  18. SemanticsIR& semantics,
  19. llvm::raw_ostream* vlog_stream)
  20. : tokens_(&tokens),
  21. emitter_(&emitter),
  22. parse_tree_(&parse_tree),
  23. semantics_(&semantics),
  24. vlog_stream_(vlog_stream),
  25. node_stack_(parse_tree, vlog_stream),
  26. node_block_stack_("node_block_stack_", semantics.node_blocks(),
  27. vlog_stream),
  28. params_or_args_stack_("params_or_args_stack_", semantics.node_blocks(),
  29. vlog_stream),
  30. args_type_info_stack_("args_type_info_stack_", semantics.node_blocks(),
  31. vlog_stream) {
  32. // Inserts the "Invalid" and "Type" types as "used types" so that
  33. // canonicalization can skip them. We don't emit either for lowering.
  34. canonical_types_.insert(SemanticsNodeId::BuiltinInvalidType);
  35. canonical_types_.insert(SemanticsNodeId::BuiltinTypeType);
  36. }
  37. auto SemanticsContext::TODO(ParseTree::Node parse_node, std::string label)
  38. -> bool {
  39. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: {0}", std::string);
  40. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  41. return false;
  42. }
  43. auto SemanticsContext::VerifyOnFinish() -> void {
  44. // Information in all the various context objects should be cleaned up as
  45. // various pieces of context go out of scope. At this point, nothing should
  46. // remain.
  47. // node_stack_ will still contain top-level entities.
  48. CARBON_CHECK(name_lookup_.empty()) << name_lookup_.size();
  49. CARBON_CHECK(scope_stack_.empty()) << scope_stack_.size();
  50. CARBON_CHECK(node_block_stack_.empty()) << node_block_stack_.size();
  51. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  52. }
  53. auto SemanticsContext::AddNode(SemanticsNode node) -> SemanticsNodeId {
  54. CARBON_CHECK(!node.type_id().is_valid() ||
  55. node.type_id() == SemanticsNodeId::BuiltinInvalidType ||
  56. canonical_types_.contains(node.type_id()))
  57. << "Added node without canonicalizing its type: " << node;
  58. auto block = node_block_stack_.PeekForAdd();
  59. CARBON_VLOG() << "AddNode " << block << ": " << node << "\n";
  60. return semantics_->AddNode(block, node);
  61. }
  62. auto SemanticsContext::AddNodeAndPush(ParseTree::Node parse_node,
  63. SemanticsNode node) -> void {
  64. auto node_id = AddNode(node);
  65. node_stack_.Push(parse_node, node_id);
  66. }
  67. auto SemanticsContext::AddNameToLookup(ParseTree::Node name_node,
  68. SemanticsStringId name_id,
  69. SemanticsNodeId target_id) -> void {
  70. if (!AddNameToLookupImpl(name_id, target_id)) {
  71. CARBON_DIAGNOSTIC(NameRedefined, Error, "Redefining {0} in the same scope.",
  72. llvm::StringRef);
  73. CARBON_DIAGNOSTIC(PreviousDefinition, Note, "Previous definition is here.");
  74. auto prev_def_id = name_lookup_[name_id].back();
  75. auto prev_def = semantics_->GetNode(prev_def_id);
  76. emitter_->Build(name_node, NameRedefined, semantics_->GetString(name_id))
  77. .Note(prev_def.parse_node(), PreviousDefinition)
  78. .Emit();
  79. }
  80. }
  81. auto SemanticsContext::AddNameToLookupImpl(SemanticsStringId name_id,
  82. SemanticsNodeId target_id) -> bool {
  83. if (current_scope().names.insert(name_id).second) {
  84. name_lookup_[name_id].push_back(target_id);
  85. return true;
  86. } else {
  87. return false;
  88. }
  89. }
  90. auto SemanticsContext::BindName(ParseTree::Node name_node,
  91. SemanticsNodeId type_id,
  92. SemanticsNodeId target_id)
  93. -> SemanticsStringId {
  94. CARBON_CHECK(parse_tree_->node_kind(name_node) == ParseNodeKind::DeclaredName)
  95. << parse_tree_->node_kind(name_node);
  96. auto name_str = parse_tree_->GetNodeText(name_node);
  97. auto name_id = semantics_->AddString(name_str);
  98. AddNode(
  99. SemanticsNode::BindName::Make(name_node, type_id, name_id, target_id));
  100. AddNameToLookup(name_node, name_id, target_id);
  101. return name_id;
  102. }
  103. auto SemanticsContext::TempRemoveLatestNameFromLookup() -> SemanticsNodeId {
  104. // Save the storage ID.
  105. auto it = name_lookup_.find(
  106. node_stack_.PeekForNameId(ParseNodeKind::PatternBinding));
  107. CARBON_CHECK(it != name_lookup_.end());
  108. CARBON_CHECK(!it->second.empty());
  109. auto storage_id = it->second.back();
  110. // Pop the name from lookup.
  111. if (it->second.size() == 1) {
  112. // Erase names that no longer resolve.
  113. name_lookup_.erase(it);
  114. } else {
  115. it->second.pop_back();
  116. }
  117. return storage_id;
  118. }
  119. auto SemanticsContext::LookupName(ParseTree::Node parse_node,
  120. llvm::StringRef name) -> SemanticsNodeId {
  121. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name {0} not found", llvm::StringRef);
  122. auto name_id = semantics_->GetStringID(name);
  123. if (!name_id) {
  124. emitter_->Emit(parse_node, NameNotFound, name);
  125. return SemanticsNodeId::BuiltinInvalidType;
  126. }
  127. auto it = name_lookup_.find(*name_id);
  128. if (it == name_lookup_.end()) {
  129. emitter_->Emit(parse_node, NameNotFound, name);
  130. return SemanticsNodeId::BuiltinInvalidType;
  131. }
  132. CARBON_CHECK(!it->second.empty()) << "Should have been erased: " << name;
  133. // TODO: Check for ambiguous lookups.
  134. return it->second.back();
  135. }
  136. auto SemanticsContext::PushScope() -> void { scope_stack_.push_back({}); }
  137. auto SemanticsContext::PopScope() -> void {
  138. auto scope = scope_stack_.pop_back_val();
  139. for (const auto& str_id : scope.names) {
  140. auto it = name_lookup_.find(str_id);
  141. if (it->second.size() == 1) {
  142. // Erase names that no longer resolve.
  143. name_lookup_.erase(it);
  144. } else {
  145. it->second.pop_back();
  146. }
  147. }
  148. }
  149. auto SemanticsContext::ImplicitAsForArgs(
  150. SemanticsNodeBlockId arg_refs_id, ParseTree::Node param_parse_node,
  151. SemanticsNodeBlockId param_refs_id,
  152. DiagnosticEmitter<ParseTree::Node>::DiagnosticBuilder* diagnostic) -> bool {
  153. // If both arguments and parameters are empty, return quickly. Otherwise,
  154. // we'll fetch both so that errors are consistent.
  155. if (arg_refs_id == SemanticsNodeBlockId::Empty &&
  156. param_refs_id == SemanticsNodeBlockId::Empty) {
  157. return true;
  158. }
  159. auto arg_refs = semantics_->GetNodeBlock(arg_refs_id);
  160. auto param_refs = semantics_->GetNodeBlock(param_refs_id);
  161. // If sizes mismatch, fail early.
  162. if (arg_refs.size() != param_refs.size()) {
  163. CARBON_CHECK(diagnostic != nullptr) << "Should have validated first";
  164. CARBON_DIAGNOSTIC(CallArgCountMismatch, Note,
  165. "Callable cannot be used: Received {0} argument(s), but "
  166. "require {1} argument(s).",
  167. int, int);
  168. diagnostic->Note(param_parse_node, CallArgCountMismatch, arg_refs.size(),
  169. param_refs.size());
  170. return false;
  171. }
  172. // Check type conversions per-element.
  173. // TODO: arg_ir_id is passed so that implicit conversions can be inserted.
  174. // It's currently not supported, but will be needed.
  175. for (size_t i = 0; i < arg_refs.size(); ++i) {
  176. auto value_id = arg_refs[i];
  177. auto as_type_id = semantics_->GetNode(param_refs[i]).type_id();
  178. if (ImplicitAsImpl(value_id, as_type_id,
  179. diagnostic == nullptr ? &value_id : nullptr) ==
  180. ImplicitAsKind::Incompatible) {
  181. CARBON_CHECK(diagnostic != nullptr) << "Should have validated first";
  182. CARBON_DIAGNOSTIC(CallArgTypeMismatch, Note,
  183. "Callable cannot be used: Cannot implicityly convert "
  184. "argument {0} from `{1}` to `{2}`.",
  185. size_t, std::string, std::string);
  186. diagnostic->Note(
  187. param_parse_node, CallArgTypeMismatch, i,
  188. semantics_->StringifyNode(semantics_->GetNode(value_id).type_id()),
  189. semantics_->StringifyNode(as_type_id));
  190. return false;
  191. }
  192. }
  193. return true;
  194. }
  195. auto SemanticsContext::ImplicitAsRequired(ParseTree::Node parse_node,
  196. SemanticsNodeId value_id,
  197. SemanticsNodeId as_type_id)
  198. -> SemanticsNodeId {
  199. SemanticsNodeId output_value_id = value_id;
  200. if (ImplicitAsImpl(value_id, as_type_id, &output_value_id) ==
  201. ImplicitAsKind::Incompatible) {
  202. // Only error when the system is trying to use the result.
  203. CARBON_DIAGNOSTIC(ImplicitAsConversionFailure, Error,
  204. "Cannot implicitly convert from `{0}` to `{1}`.",
  205. std::string, std::string);
  206. emitter_
  207. ->Build(
  208. parse_node, ImplicitAsConversionFailure,
  209. semantics_->StringifyNode(semantics_->GetNode(value_id).type_id()),
  210. semantics_->StringifyNode(as_type_id))
  211. .Emit();
  212. }
  213. return output_value_id;
  214. }
  215. auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
  216. SemanticsNodeId as_type_id,
  217. SemanticsNodeId* output_value_id)
  218. -> ImplicitAsKind {
  219. // Start by making sure both sides are valid. If any part is invalid, the
  220. // result is invalid and we shouldn't error.
  221. if (value_id == SemanticsNodeId::BuiltinInvalidType) {
  222. // If the value is invalid, we can't do much, but do "succeed".
  223. return ImplicitAsKind::Identical;
  224. }
  225. auto value = semantics_->GetNode(value_id);
  226. auto value_type_id = value.type_id();
  227. if (value_type_id == SemanticsNodeId::BuiltinInvalidType) {
  228. return ImplicitAsKind::Identical;
  229. }
  230. if (as_type_id == SemanticsNodeId::BuiltinInvalidType) {
  231. // Although the target type is invalid, this still changes the value.
  232. if (output_value_id != nullptr) {
  233. *output_value_id = SemanticsNodeId::BuiltinInvalidType;
  234. }
  235. return ImplicitAsKind::Compatible;
  236. }
  237. if (value_type_id == as_type_id) {
  238. // Type doesn't need to change.
  239. return ImplicitAsKind::Identical;
  240. }
  241. if (as_type_id == SemanticsNodeId::BuiltinTypeType) {
  242. // When converting `()` to a type, the result is `() as Type`.
  243. // TODO: This might switch to be closer to the struct conversion below.
  244. if (value_id == SemanticsNodeId::BuiltinEmptyTuple) {
  245. if (output_value_id != nullptr) {
  246. *output_value_id = SemanticsNodeId::BuiltinEmptyTupleType;
  247. }
  248. return ImplicitAsKind::Compatible;
  249. }
  250. // When converting `{}` to a type, the result is `{} as Type`.
  251. if (value.kind() == SemanticsNodeKind::StructValue &&
  252. value.GetAsStructValue() == SemanticsNodeBlockId::Empty) {
  253. if (output_value_id != nullptr) {
  254. *output_value_id = value_type_id;
  255. }
  256. return ImplicitAsKind::Compatible;
  257. }
  258. }
  259. auto value_type = semantics_->GetNode(value_type_id);
  260. auto as_type = semantics_->GetNode(as_type_id);
  261. if (CanImplicitAsStruct(value_type, as_type)) {
  262. // Under the current implementation, struct types are only allowed to
  263. // ImplicitAs when they're equivalent. What's really missing is type
  264. // consolidation such that this would fall under the above `value_type_id ==
  265. // as_type_id` case. In the future, this will need to handle actual
  266. // conversions.
  267. return ImplicitAsKind::Identical;
  268. }
  269. if (output_value_id != nullptr) {
  270. *output_value_id = SemanticsNodeId::BuiltinInvalidType;
  271. }
  272. return ImplicitAsKind::Incompatible;
  273. }
  274. auto SemanticsContext::CanImplicitAsStruct(SemanticsNode value_type,
  275. SemanticsNode as_type) -> bool {
  276. if (value_type.kind() != SemanticsNodeKind::StructType ||
  277. as_type.kind() != SemanticsNodeKind::StructType) {
  278. return false;
  279. }
  280. auto value_type_refs = semantics_->GetNodeBlock(value_type.GetAsStructType());
  281. auto as_type_refs = semantics_->GetNodeBlock(as_type.GetAsStructType());
  282. if (value_type_refs.size() != as_type_refs.size()) {
  283. return false;
  284. }
  285. for (int i = 0; i < static_cast<int>(value_type_refs.size()); ++i) {
  286. auto value_type_field = semantics_->GetNode(value_type_refs[i]);
  287. auto as_type_field = semantics_->GetNode(as_type_refs[i]);
  288. if (value_type_field.type_id() != as_type_field.type_id() ||
  289. value_type_field.GetAsStructTypeField() !=
  290. as_type_field.GetAsStructTypeField()) {
  291. return false;
  292. }
  293. }
  294. return true;
  295. }
  296. auto SemanticsContext::ParamOrArgStart() -> void {
  297. params_or_args_stack_.Push();
  298. }
  299. auto SemanticsContext::ParamOrArgComma(bool for_args) -> void {
  300. ParamOrArgSave(for_args);
  301. }
  302. auto SemanticsContext::ParamOrArgEnd(bool for_args, ParseNodeKind start_kind)
  303. -> SemanticsNodeBlockId {
  304. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  305. ParamOrArgSave(for_args);
  306. }
  307. return params_or_args_stack_.Pop();
  308. }
  309. auto SemanticsContext::ParamOrArgSave(bool for_args) -> void {
  310. SemanticsNodeId param_or_arg_id = SemanticsNodeId::Invalid;
  311. if (for_args) {
  312. // For an argument, we add a stub reference to the expression on the top of
  313. // the stack. There may not be anything on the IR prior to this.
  314. auto [entry_parse_node, entry_node_id] =
  315. node_stack_.PopForParseNodeAndNodeId();
  316. param_or_arg_id = AddNode(SemanticsNode::StubReference::Make(
  317. entry_parse_node, semantics_->GetNode(entry_node_id).type_id(),
  318. entry_node_id));
  319. } else {
  320. // For a parameter, there should always be something in the IR.
  321. node_stack_.PopAndIgnore();
  322. auto ir_id = node_block_stack_.Peek();
  323. CARBON_CHECK(ir_id.is_valid());
  324. auto& ir = semantics_->GetNodeBlock(ir_id);
  325. CARBON_CHECK(!ir.empty()) << "Should have had a param";
  326. param_or_arg_id = ir.back();
  327. }
  328. // Save the param or arg ID.
  329. auto& params_or_args =
  330. semantics_->GetNodeBlock(params_or_args_stack_.PeekForAdd());
  331. params_or_args.push_back(param_or_arg_id);
  332. }
  333. auto SemanticsContext::CanonicalizeType(SemanticsNodeId node_id)
  334. -> SemanticsNodeId {
  335. if (canonical_types_.insert(node_id).second) {
  336. semantics_->AddType(node_id);
  337. }
  338. return node_id;
  339. }
  340. auto SemanticsContext::PrintForStackDump(llvm::raw_ostream& output) const
  341. -> void {
  342. node_stack_.PrintForStackDump(output);
  343. node_block_stack_.PrintForStackDump(output);
  344. params_or_args_stack_.PrintForStackDump(output);
  345. args_type_info_stack_.PrintForStackDump(output);
  346. }
  347. } // namespace Carbon