semantics_context.cpp 15 KB

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