context.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 <string>
  6. #include <utility>
  7. #include "common/check.h"
  8. #include "common/vlog.h"
  9. #include "llvm/ADT/Sequence.h"
  10. #include "toolchain/check/declaration_name_stack.h"
  11. #include "toolchain/check/node_block_stack.h"
  12. #include "toolchain/diagnostics/diagnostic_kind.h"
  13. #include "toolchain/lex/tokenized_buffer.h"
  14. #include "toolchain/parse/node_kind.h"
  15. #include "toolchain/sem_ir/file.h"
  16. #include "toolchain/sem_ir/node.h"
  17. #include "toolchain/sem_ir/node_kind.h"
  18. namespace Carbon::Check {
  19. Context::Context(const Lex::TokenizedBuffer& tokens,
  20. DiagnosticEmitter<Parse::Node>& emitter,
  21. const Parse::Tree& parse_tree, SemIR::File& semantics_ir,
  22. llvm::raw_ostream* vlog_stream)
  23. : tokens_(&tokens),
  24. emitter_(&emitter),
  25. parse_tree_(&parse_tree),
  26. semantics_ir_(&semantics_ir),
  27. vlog_stream_(vlog_stream),
  28. node_stack_(parse_tree, vlog_stream),
  29. node_block_stack_("node_block_stack_", semantics_ir, vlog_stream),
  30. params_or_args_stack_("params_or_args_stack_", semantics_ir, vlog_stream),
  31. args_type_info_stack_("args_type_info_stack_", semantics_ir, vlog_stream),
  32. declaration_name_stack_(this) {
  33. // Inserts the "Error" and "Type" types as "used types" so that
  34. // canonicalization can skip them. We don't emit either for lowering.
  35. canonical_types_.insert({SemIR::NodeId::BuiltinError, SemIR::TypeId::Error});
  36. canonical_types_.insert(
  37. {SemIR::NodeId::BuiltinTypeType, SemIR::TypeId::TypeType});
  38. }
  39. auto Context::TODO(Parse::Node parse_node, std::string label) -> bool {
  40. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
  41. std::string);
  42. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  43. return false;
  44. }
  45. auto Context::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 Context::AddNode(SemIR::Node node) -> SemIR::NodeId {
  56. auto node_id = node_block_stack_.AddNode(node);
  57. CARBON_VLOG() << "AddNode: " << node << "\n";
  58. return node_id;
  59. }
  60. auto Context::AddNodeAndPush(Parse::Node parse_node, SemIR::Node node) -> void {
  61. auto node_id = AddNode(node);
  62. node_stack_.Push(parse_node, node_id);
  63. }
  64. auto Context::DiagnoseDuplicateName(Parse::Node parse_node,
  65. SemIR::NodeId prev_def_id) -> void {
  66. CARBON_DIAGNOSTIC(NameDeclarationDuplicate, Error,
  67. "Duplicate name being declared in the same scope.");
  68. CARBON_DIAGNOSTIC(NameDeclarationPrevious, Note,
  69. "Name is previously declared here.");
  70. auto prev_def = semantics_ir_->GetNode(prev_def_id);
  71. emitter_->Build(parse_node, NameDeclarationDuplicate)
  72. .Note(prev_def.parse_node(), NameDeclarationPrevious)
  73. .Emit();
  74. }
  75. auto Context::DiagnoseNameNotFound(Parse::Node parse_node,
  76. SemIR::StringId name_id) -> void {
  77. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
  78. llvm::StringRef);
  79. emitter_->Emit(parse_node, NameNotFound, semantics_ir_->GetString(name_id));
  80. }
  81. auto Context::AddNameToLookup(Parse::Node name_node, SemIR::StringId name_id,
  82. SemIR::NodeId target_id) -> void {
  83. if (current_scope().names.insert(name_id).second) {
  84. name_lookup_[name_id].push_back(target_id);
  85. } else {
  86. DiagnoseDuplicateName(name_node, name_lookup_[name_id].back());
  87. }
  88. }
  89. auto Context::LookupName(Parse::Node parse_node, SemIR::StringId name_id,
  90. SemIR::NameScopeId scope_id, bool print_diagnostics)
  91. -> SemIR::NodeId {
  92. if (scope_id == SemIR::NameScopeId::Invalid) {
  93. auto it = name_lookup_.find(name_id);
  94. if (it == name_lookup_.end()) {
  95. if (print_diagnostics) {
  96. DiagnoseNameNotFound(parse_node, name_id);
  97. }
  98. return SemIR::NodeId::BuiltinError;
  99. }
  100. CARBON_CHECK(!it->second.empty())
  101. << "Should have been erased: " << semantics_ir_->GetString(name_id);
  102. // TODO: Check for ambiguous lookups.
  103. return it->second.back();
  104. } else {
  105. const auto& scope = semantics_ir_->GetNameScope(scope_id);
  106. auto it = scope.find(name_id);
  107. if (it == scope.end()) {
  108. if (print_diagnostics) {
  109. DiagnoseNameNotFound(parse_node, name_id);
  110. }
  111. return SemIR::NodeId::BuiltinError;
  112. }
  113. return it->second;
  114. }
  115. }
  116. auto Context::PushScope() -> void { scope_stack_.push_back({}); }
  117. auto Context::PopScope() -> void {
  118. auto scope = scope_stack_.pop_back_val();
  119. for (const auto& str_id : scope.names) {
  120. auto it = name_lookup_.find(str_id);
  121. if (it->second.size() == 1) {
  122. // Erase names that no longer resolve.
  123. name_lookup_.erase(it);
  124. } else {
  125. it->second.pop_back();
  126. }
  127. }
  128. }
  129. auto Context::FollowNameReferences(SemIR::NodeId node_id) -> SemIR::NodeId {
  130. while (auto name_ref =
  131. semantics_ir().GetNode(node_id).TryAs<SemIR::NameReference>()) {
  132. node_id = name_ref->value_id;
  133. }
  134. return node_id;
  135. }
  136. template <typename BranchNode, typename... Args>
  137. static auto AddDominatedBlockAndBranchImpl(Context& context,
  138. Parse::Node parse_node, Args... args)
  139. -> SemIR::NodeBlockId {
  140. if (!context.node_block_stack().is_current_block_reachable()) {
  141. return SemIR::NodeBlockId::Unreachable;
  142. }
  143. auto block_id = context.semantics_ir().AddNodeBlockId();
  144. context.AddNode(BranchNode(parse_node, block_id, args...));
  145. return block_id;
  146. }
  147. auto Context::AddDominatedBlockAndBranch(Parse::Node parse_node)
  148. -> SemIR::NodeBlockId {
  149. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, parse_node);
  150. }
  151. auto Context::AddDominatedBlockAndBranchWithArg(Parse::Node parse_node,
  152. SemIR::NodeId arg_id)
  153. -> SemIR::NodeBlockId {
  154. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, parse_node,
  155. arg_id);
  156. }
  157. auto Context::AddDominatedBlockAndBranchIf(Parse::Node parse_node,
  158. SemIR::NodeId cond_id)
  159. -> SemIR::NodeBlockId {
  160. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, parse_node,
  161. cond_id);
  162. }
  163. auto Context::AddConvergenceBlockAndPush(Parse::Node parse_node, int num_blocks)
  164. -> void {
  165. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  166. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  167. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  168. if (node_block_stack().is_current_block_reachable()) {
  169. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  170. new_block_id = semantics_ir().AddNodeBlockId();
  171. }
  172. AddNode(SemIR::Branch(parse_node, new_block_id));
  173. }
  174. node_block_stack().Pop();
  175. }
  176. node_block_stack().Push(new_block_id);
  177. }
  178. auto Context::AddConvergenceBlockWithArgAndPush(
  179. Parse::Node parse_node, std::initializer_list<SemIR::NodeId> block_args)
  180. -> SemIR::NodeId {
  181. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  182. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  183. for (auto arg_id : block_args) {
  184. if (node_block_stack().is_current_block_reachable()) {
  185. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  186. new_block_id = semantics_ir().AddNodeBlockId();
  187. }
  188. AddNode(SemIR::BranchWithArg(parse_node, new_block_id, arg_id));
  189. }
  190. node_block_stack().Pop();
  191. }
  192. node_block_stack().Push(new_block_id);
  193. // Acquire the result value.
  194. SemIR::TypeId result_type_id =
  195. semantics_ir().GetNode(*block_args.begin()).type_id();
  196. return AddNode(SemIR::BlockArg(parse_node, result_type_id, new_block_id));
  197. }
  198. // Add the current code block to the enclosing function.
  199. auto Context::AddCurrentCodeBlockToFunction() -> void {
  200. CARBON_CHECK(!node_block_stack().empty()) << "no current code block";
  201. CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
  202. if (!node_block_stack().is_current_block_reachable()) {
  203. // Don't include unreachable blocks in the function.
  204. return;
  205. }
  206. auto function_id =
  207. semantics_ir()
  208. .GetNodeAs<SemIR::FunctionDeclaration>(return_scope_stack().back())
  209. .function_id;
  210. semantics_ir()
  211. .GetFunction(function_id)
  212. .body_block_ids.push_back(node_block_stack().PeekOrAdd());
  213. }
  214. auto Context::is_current_position_reachable() -> bool {
  215. if (!node_block_stack().is_current_block_reachable()) {
  216. return false;
  217. }
  218. // Our current position is at the end of a reachable block. That position is
  219. // reachable unless the previous instruction is a terminator instruction.
  220. auto block_contents = node_block_stack().PeekCurrentBlockContents();
  221. if (block_contents.empty()) {
  222. return true;
  223. }
  224. const auto& last_node = semantics_ir().GetNode(block_contents.back());
  225. return last_node.kind().terminator_kind() !=
  226. SemIR::TerminatorKind::Terminator;
  227. }
  228. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  229. auto Context::ParamOrArgComma() -> void {
  230. ParamOrArgSave(node_stack_.PopExpression());
  231. }
  232. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  233. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  234. ParamOrArgSave(node_stack_.PopExpression());
  235. }
  236. }
  237. auto Context::ParamOrArgPop() -> SemIR::NodeBlockId {
  238. return params_or_args_stack_.Pop();
  239. }
  240. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::NodeBlockId {
  241. ParamOrArgEndNoPop(start_kind);
  242. return ParamOrArgPop();
  243. }
  244. auto Context::CanonicalizeTypeImpl(
  245. SemIR::NodeKind kind,
  246. llvm::function_ref<void(llvm::FoldingSetNodeID& canonical_id)> profile_type,
  247. llvm::function_ref<SemIR::NodeId()> make_node) -> SemIR::TypeId {
  248. llvm::FoldingSetNodeID canonical_id;
  249. kind.Profile(canonical_id);
  250. profile_type(canonical_id);
  251. void* insert_pos;
  252. auto* node =
  253. canonical_type_nodes_.FindNodeOrInsertPos(canonical_id, insert_pos);
  254. if (node != nullptr) {
  255. return node->type_id();
  256. }
  257. auto node_id = make_node();
  258. auto type_id = semantics_ir_->AddType(node_id);
  259. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  260. type_node_storage_.push_back(
  261. std::make_unique<TypeNode>(canonical_id, type_id));
  262. // In a debug build, check that our insertion position is still valid. It
  263. // could have been invalidated by a misbehaving `make_node`.
  264. CARBON_DCHECK([&] {
  265. void* check_insert_pos;
  266. auto* check_node = canonical_type_nodes_.FindNodeOrInsertPos(
  267. canonical_id, check_insert_pos);
  268. return !check_node && insert_pos == check_insert_pos;
  269. }()) << "Type was created recursively during canonicalization";
  270. canonical_type_nodes_.InsertNode(type_node_storage_.back().get(), insert_pos);
  271. return type_id;
  272. }
  273. // Compute a fingerprint for a tuple type, for use as a key in a folding set.
  274. static auto ProfileTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids,
  275. llvm::FoldingSetNodeID& canonical_id) -> void {
  276. for (auto type_id : type_ids) {
  277. canonical_id.AddInteger(type_id.index);
  278. }
  279. }
  280. // Compute a fingerprint for a type, for use as a key in a folding set.
  281. static auto ProfileType(Context& semantics_context, SemIR::Node node,
  282. llvm::FoldingSetNodeID& canonical_id) -> void {
  283. switch (node.kind()) {
  284. case SemIR::ArrayType::Kind: {
  285. auto array_type = node.As<SemIR::ArrayType>();
  286. canonical_id.AddInteger(
  287. semantics_context.semantics_ir().GetArrayBoundValue(
  288. array_type.bound_id));
  289. canonical_id.AddInteger(array_type.element_type_id.index);
  290. break;
  291. }
  292. case SemIR::Builtin::Kind:
  293. canonical_id.AddInteger(node.As<SemIR::Builtin>().builtin_kind.AsInt());
  294. break;
  295. case SemIR::ClassDeclaration::Kind:
  296. canonical_id.AddInteger(
  297. node.As<SemIR::ClassDeclaration>().class_id.index);
  298. break;
  299. case SemIR::CrossReference::Kind: {
  300. // TODO: Cross-references should be canonicalized by looking at their
  301. // target rather than treating them as new unique types.
  302. auto xref = node.As<SemIR::CrossReference>();
  303. canonical_id.AddInteger(xref.ir_id.index);
  304. canonical_id.AddInteger(xref.node_id.index);
  305. break;
  306. }
  307. case SemIR::ConstType::Kind:
  308. canonical_id.AddInteger(
  309. semantics_context
  310. .GetUnqualifiedType(node.As<SemIR::ConstType>().inner_id)
  311. .index);
  312. break;
  313. case SemIR::PointerType::Kind:
  314. canonical_id.AddInteger(node.As<SemIR::PointerType>().pointee_id.index);
  315. break;
  316. case SemIR::StructType::Kind: {
  317. auto fields = semantics_context.semantics_ir().GetNodeBlock(
  318. node.As<SemIR::StructType>().fields_id);
  319. for (const auto& field_id : fields) {
  320. auto field =
  321. semantics_context.semantics_ir().GetNodeAs<SemIR::StructTypeField>(
  322. field_id);
  323. canonical_id.AddInteger(field.name_id.index);
  324. canonical_id.AddInteger(field.type_id.index);
  325. }
  326. break;
  327. }
  328. case SemIR::TupleType::Kind:
  329. ProfileTupleType(semantics_context.semantics_ir().GetTypeBlock(
  330. node.As<SemIR::TupleType>().elements_id),
  331. canonical_id);
  332. break;
  333. default:
  334. CARBON_FATAL() << "Unexpected type node " << node;
  335. }
  336. }
  337. auto Context::CanonicalizeTypeAndAddNodeIfNew(SemIR::Node node)
  338. -> SemIR::TypeId {
  339. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  340. ProfileType(*this, node, canonical_id);
  341. };
  342. auto make_node = [&] { return AddNode(node); };
  343. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  344. }
  345. auto Context::CanonicalizeType(SemIR::NodeId node_id) -> SemIR::TypeId {
  346. node_id = FollowNameReferences(node_id);
  347. auto it = canonical_types_.find(node_id);
  348. if (it != canonical_types_.end()) {
  349. return it->second;
  350. }
  351. auto node = semantics_ir_->GetNode(node_id);
  352. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  353. ProfileType(*this, node, canonical_id);
  354. };
  355. auto make_node = [&] { return node_id; };
  356. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  357. }
  358. auto Context::CanonicalizeStructType(Parse::Node parse_node,
  359. SemIR::NodeBlockId refs_id)
  360. -> SemIR::TypeId {
  361. return CanonicalizeTypeAndAddNodeIfNew(
  362. SemIR::StructType(parse_node, SemIR::TypeId::TypeType, refs_id));
  363. }
  364. auto Context::CanonicalizeTupleType(Parse::Node parse_node,
  365. llvm::ArrayRef<SemIR::TypeId> type_ids)
  366. -> SemIR::TypeId {
  367. // Defer allocating a SemIR::TypeBlockId until we know this is a new type.
  368. auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
  369. ProfileTupleType(type_ids, canonical_id);
  370. };
  371. auto make_tuple_node = [&] {
  372. return AddNode(SemIR::TupleType(parse_node, SemIR::TypeId::TypeType,
  373. semantics_ir_->AddTypeBlock(type_ids)));
  374. };
  375. return CanonicalizeTypeImpl(SemIR::TupleType::Kind, profile_tuple,
  376. make_tuple_node);
  377. }
  378. auto Context::GetPointerType(Parse::Node parse_node,
  379. SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  380. return CanonicalizeTypeAndAddNodeIfNew(
  381. SemIR::PointerType(parse_node, SemIR::TypeId::TypeType, pointee_type_id));
  382. }
  383. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  384. SemIR::Node type_node =
  385. semantics_ir_->GetNode(semantics_ir_->GetTypeAllowBuiltinTypes(type_id));
  386. if (auto const_type = type_node.TryAs<SemIR::ConstType>()) {
  387. return const_type->inner_id;
  388. }
  389. return type_id;
  390. }
  391. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  392. node_stack_.PrintForStackDump(output);
  393. node_block_stack_.PrintForStackDump(output);
  394. params_or_args_stack_.PrintForStackDump(output);
  395. args_type_info_stack_.PrintForStackDump(output);
  396. }
  397. } // namespace Carbon::Check