semantics_context.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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_ir,
  19. llvm::raw_ostream* vlog_stream)
  20. : tokens_(&tokens),
  21. emitter_(&emitter),
  22. parse_tree_(&parse_tree),
  23. semantics_ir_(&semantics_ir),
  24. vlog_stream_(vlog_stream),
  25. node_stack_(parse_tree, vlog_stream),
  26. node_block_stack_("node_block_stack_", semantics_ir.node_blocks(),
  27. vlog_stream),
  28. params_or_args_stack_("params_or_args_stack_", semantics_ir.node_blocks(),
  29. vlog_stream),
  30. args_type_info_stack_("args_type_info_stack_", semantics_ir.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_ir_->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 (current_scope().names.insert(name_id).second) {
  69. name_lookup_[name_id].push_back(target_id);
  70. } else {
  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_ir_->GetNode(prev_def_id);
  76. emitter_->Build(name_node, NameRedefined, semantics_ir_->GetString(name_id))
  77. .Note(prev_def.parse_node(), PreviousDefinition)
  78. .Emit();
  79. }
  80. }
  81. auto SemanticsContext::LookupName(ParseTree::Node parse_node,
  82. llvm::StringRef name) -> SemanticsNodeId {
  83. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name {0} not found", llvm::StringRef);
  84. auto name_id = semantics_ir_->GetStringID(name);
  85. if (!name_id) {
  86. emitter_->Emit(parse_node, NameNotFound, name);
  87. return SemanticsNodeId::BuiltinInvalidType;
  88. }
  89. auto it = name_lookup_.find(*name_id);
  90. if (it == name_lookup_.end()) {
  91. emitter_->Emit(parse_node, NameNotFound, name);
  92. return SemanticsNodeId::BuiltinInvalidType;
  93. }
  94. CARBON_CHECK(!it->second.empty()) << "Should have been erased: " << name;
  95. // TODO: Check for ambiguous lookups.
  96. return it->second.back();
  97. }
  98. auto SemanticsContext::PushScope() -> void { scope_stack_.push_back({}); }
  99. auto SemanticsContext::PopScope() -> void {
  100. auto scope = scope_stack_.pop_back_val();
  101. for (const auto& str_id : scope.names) {
  102. auto it = name_lookup_.find(str_id);
  103. if (it->second.size() == 1) {
  104. // Erase names that no longer resolve.
  105. name_lookup_.erase(it);
  106. } else {
  107. it->second.pop_back();
  108. }
  109. }
  110. }
  111. auto SemanticsContext::ImplicitAsForArgs(
  112. SemanticsNodeBlockId arg_refs_id, ParseTree::Node param_parse_node,
  113. SemanticsNodeBlockId param_refs_id,
  114. DiagnosticEmitter<ParseTree::Node>::DiagnosticBuilder* diagnostic) -> bool {
  115. // If both arguments and parameters are empty, return quickly. Otherwise,
  116. // we'll fetch both so that errors are consistent.
  117. if (arg_refs_id == SemanticsNodeBlockId::Empty &&
  118. param_refs_id == SemanticsNodeBlockId::Empty) {
  119. return true;
  120. }
  121. auto arg_refs = semantics_ir_->GetNodeBlock(arg_refs_id);
  122. auto param_refs = semantics_ir_->GetNodeBlock(param_refs_id);
  123. // If sizes mismatch, fail early.
  124. if (arg_refs.size() != param_refs.size()) {
  125. CARBON_CHECK(diagnostic != nullptr) << "Should have validated first";
  126. CARBON_DIAGNOSTIC(CallArgCountMismatch, Note,
  127. "Function cannot be used: Received {0} argument(s), but "
  128. "require {1} argument(s).",
  129. int, int);
  130. diagnostic->Note(param_parse_node, CallArgCountMismatch, arg_refs.size(),
  131. param_refs.size());
  132. return false;
  133. }
  134. // Check type conversions per-element.
  135. // TODO: arg_ir_id is passed so that implicit conversions can be inserted.
  136. // It's currently not supported, but will be needed.
  137. for (size_t i = 0; i < arg_refs.size(); ++i) {
  138. auto value_id = arg_refs[i];
  139. auto as_type_id = semantics_ir_->GetNode(param_refs[i]).type_id();
  140. if (ImplicitAsImpl(value_id, as_type_id,
  141. diagnostic == nullptr ? &value_id : nullptr) ==
  142. ImplicitAsKind::Incompatible) {
  143. CARBON_CHECK(diagnostic != nullptr) << "Should have validated first";
  144. CARBON_DIAGNOSTIC(CallArgTypeMismatch, Note,
  145. "Function cannot be used: Cannot implicityly convert "
  146. "argument {0} from `{1}` to `{2}`.",
  147. size_t, std::string, std::string);
  148. diagnostic->Note(param_parse_node, CallArgTypeMismatch, i,
  149. semantics_ir_->StringifyType(
  150. semantics_ir_->GetNode(value_id).type_id()),
  151. semantics_ir_->StringifyType(as_type_id));
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. auto SemanticsContext::ImplicitAsRequired(ParseTree::Node parse_node,
  158. SemanticsNodeId value_id,
  159. SemanticsTypeId as_type_id)
  160. -> SemanticsNodeId {
  161. SemanticsNodeId output_value_id = value_id;
  162. if (ImplicitAsImpl(value_id, as_type_id, &output_value_id) ==
  163. ImplicitAsKind::Incompatible) {
  164. // Only error when the system is trying to use the result.
  165. CARBON_DIAGNOSTIC(ImplicitAsConversionFailure, Error,
  166. "Cannot implicitly convert from `{0}` to `{1}`.",
  167. std::string, std::string);
  168. emitter_
  169. ->Build(parse_node, ImplicitAsConversionFailure,
  170. semantics_ir_->StringifyType(
  171. semantics_ir_->GetNode(value_id).type_id()),
  172. semantics_ir_->StringifyType(as_type_id))
  173. .Emit();
  174. }
  175. return output_value_id;
  176. }
  177. auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
  178. SemanticsTypeId as_type_id,
  179. SemanticsNodeId* output_value_id)
  180. -> ImplicitAsKind {
  181. // Start by making sure both sides are valid. If any part is invalid, the
  182. // result is invalid and we shouldn't error.
  183. if (value_id == SemanticsNodeId::BuiltinInvalidType) {
  184. // If the value is invalid, we can't do much, but do "succeed".
  185. return ImplicitAsKind::Identical;
  186. }
  187. auto value = semantics_ir_->GetNode(value_id);
  188. auto value_type_id = value.type_id();
  189. if (value_type_id == SemanticsTypeId::InvalidType) {
  190. return ImplicitAsKind::Identical;
  191. }
  192. if (as_type_id == SemanticsTypeId::InvalidType) {
  193. // Although the target type is invalid, this still changes the value.
  194. if (output_value_id != nullptr) {
  195. *output_value_id = SemanticsNodeId::BuiltinInvalidType;
  196. }
  197. return ImplicitAsKind::Compatible;
  198. }
  199. if (value_type_id == as_type_id) {
  200. // Type doesn't need to change.
  201. return ImplicitAsKind::Identical;
  202. }
  203. if (as_type_id == SemanticsTypeId::TypeType) {
  204. // TODO: When converting `()` to a type, the result is `() as Type`.
  205. // Right now there is no tuple value support.
  206. // When converting `{}` to a type, the result is `{} as Type`.
  207. if (value.kind() == SemanticsNodeKind::StructValue &&
  208. value.GetAsStructValue() == SemanticsNodeBlockId::Empty) {
  209. if (output_value_id != nullptr) {
  210. *output_value_id = semantics_ir_->GetType(value_type_id);
  211. }
  212. return ImplicitAsKind::Compatible;
  213. }
  214. }
  215. // TODO: Handle ImplicitAs for compatible structs and tuples.
  216. if (output_value_id != nullptr) {
  217. *output_value_id = SemanticsNodeId::BuiltinInvalidType;
  218. }
  219. return ImplicitAsKind::Incompatible;
  220. }
  221. auto SemanticsContext::ParamOrArgStart() -> void {
  222. params_or_args_stack_.Push();
  223. }
  224. auto SemanticsContext::ParamOrArgComma(bool for_args) -> void {
  225. ParamOrArgSave(for_args);
  226. }
  227. auto SemanticsContext::ParamOrArgEnd(bool for_args, ParseNodeKind start_kind)
  228. -> SemanticsNodeBlockId {
  229. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  230. ParamOrArgSave(for_args);
  231. }
  232. return params_or_args_stack_.Pop();
  233. }
  234. auto SemanticsContext::ParamOrArgSave(bool for_args) -> void {
  235. SemanticsNodeId param_or_arg_id = SemanticsNodeId::Invalid;
  236. if (for_args) {
  237. // For an argument, we add a stub reference to the expression on the top of
  238. // the stack. There may not be anything on the IR prior to this.
  239. auto [entry_parse_node, entry_node_id] =
  240. node_stack_.PopWithParseNode<SemanticsNodeId>();
  241. param_or_arg_id = AddNode(SemanticsNode::StubReference::Make(
  242. entry_parse_node, semantics_ir_->GetNode(entry_node_id).type_id(),
  243. entry_node_id));
  244. } else {
  245. // For a parameter, there should always be something in the IR.
  246. node_stack_.PopAndIgnore();
  247. auto ir_id = node_block_stack_.Peek();
  248. CARBON_CHECK(ir_id.is_valid());
  249. auto& ir = semantics_ir_->GetNodeBlock(ir_id);
  250. CARBON_CHECK(!ir.empty()) << "Should have had a param";
  251. param_or_arg_id = ir.back();
  252. }
  253. // Save the param or arg ID.
  254. auto& params_or_args =
  255. semantics_ir_->GetNodeBlock(params_or_args_stack_.PeekForAdd());
  256. params_or_args.push_back(param_or_arg_id);
  257. }
  258. auto SemanticsContext::CanonicalizeType(SemanticsNodeId node_id)
  259. -> SemanticsTypeId {
  260. auto it = canonical_types_.find(node_id);
  261. if (it != canonical_types_.end()) {
  262. return it->second;
  263. }
  264. auto type_id = semantics_ir_->AddType(node_id);
  265. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  266. return type_id;
  267. }
  268. auto SemanticsContext::CanonicalizeStructType(ParseTree::Node parse_node,
  269. SemanticsNodeBlockId refs_id)
  270. -> SemanticsTypeId {
  271. // Construct the field structure for lookup.
  272. auto refs = semantics_ir_->GetNodeBlock(refs_id);
  273. llvm::FoldingSetNodeID canonical_id;
  274. for (const auto& ref_id : refs) {
  275. auto ref = semantics_ir_->GetNode(ref_id);
  276. canonical_id.AddInteger(ref.GetAsStructTypeField().index);
  277. canonical_id.AddInteger(ref.type_id().index);
  278. }
  279. // If a struct with matching fields was already created, reuse it.
  280. void* insert_pos;
  281. auto* node =
  282. canonical_struct_types_.FindNodeOrInsertPos(canonical_id, insert_pos);
  283. if (node != nullptr) {
  284. return node->type_id();
  285. }
  286. // The struct doesn't already exist, so create and store it as canonical.
  287. auto node_id = AddNode(SemanticsNode::StructType::Make(
  288. parse_node, SemanticsTypeId::TypeType, refs_id));
  289. auto type_id = semantics_ir_->AddType(node_id);
  290. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  291. canonical_struct_types_nodes_.push_back(
  292. std::make_unique<StructTypeNode>(canonical_id, type_id));
  293. canonical_struct_types_.InsertNode(canonical_struct_types_nodes_.back().get(),
  294. insert_pos);
  295. return type_id;
  296. }
  297. auto SemanticsContext::PrintForStackDump(llvm::raw_ostream& output) const
  298. -> void {
  299. node_stack_.PrintForStackDump(output);
  300. node_block_stack_.PrintForStackDump(output);
  301. params_or_args_stack_.PrintForStackDump(output);
  302. args_type_info_stack_.PrintForStackDump(output);
  303. }
  304. } // namespace Carbon