import_cpp.cpp 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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/import_cpp.h"
  5. #include <memory>
  6. #include <optional>
  7. #include <string>
  8. #include <tuple>
  9. #include <utility>
  10. #include "clang/AST/ASTContext.h"
  11. #include "clang/AST/RecordLayout.h"
  12. #include "clang/Basic/FileManager.h"
  13. #include "clang/Frontend/ASTUnit.h"
  14. #include "clang/Frontend/CompilerInstance.h"
  15. #include "clang/Frontend/CompilerInvocation.h"
  16. #include "clang/Frontend/TextDiagnostic.h"
  17. #include "clang/Lex/PreprocessorOptions.h"
  18. #include "clang/Sema/Lookup.h"
  19. #include "common/check.h"
  20. #include "common/ostream.h"
  21. #include "common/raw_string_ostream.h"
  22. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  23. #include "llvm/ADT/StringRef.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "toolchain/base/kind_switch.h"
  26. #include "toolchain/check/class.h"
  27. #include "toolchain/check/context.h"
  28. #include "toolchain/check/convert.h"
  29. #include "toolchain/check/diagnostic_helpers.h"
  30. #include "toolchain/check/eval.h"
  31. #include "toolchain/check/function.h"
  32. #include "toolchain/check/import.h"
  33. #include "toolchain/check/inst.h"
  34. #include "toolchain/check/literal.h"
  35. #include "toolchain/check/pattern.h"
  36. #include "toolchain/check/pattern_match.h"
  37. #include "toolchain/check/type.h"
  38. #include "toolchain/diagnostics/diagnostic.h"
  39. #include "toolchain/diagnostics/diagnostic_emitter.h"
  40. #include "toolchain/diagnostics/format_providers.h"
  41. #include "toolchain/parse/node_ids.h"
  42. #include "toolchain/sem_ir/clang_decl.h"
  43. #include "toolchain/sem_ir/ids.h"
  44. #include "toolchain/sem_ir/inst.h"
  45. #include "toolchain/sem_ir/name_scope.h"
  46. #include "toolchain/sem_ir/typed_insts.h"
  47. namespace Carbon::Check {
  48. // Generates C++ file contents to #include all requested imports.
  49. static auto GenerateCppIncludesHeaderCode(
  50. Context& context, llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
  51. -> std::string {
  52. std::string code;
  53. llvm::raw_string_ostream code_stream(code);
  54. for (const Parse::Tree::PackagingNames& import : imports) {
  55. // Add a line marker directive pointing at the location of the `import Cpp`
  56. // declaration in the Carbon source file. This will cause Clang's
  57. // diagnostics machinery to track and report the location in Carbon code
  58. // where the import was written.
  59. auto token = context.parse_tree().node_token(import.node_id);
  60. code_stream << "# " << context.tokens().GetLineNumber(token) << " \""
  61. << FormatEscaped(context.tokens().source().filename())
  62. << "\"\n";
  63. code_stream << "#include \""
  64. << FormatEscaped(
  65. context.string_literal_values().Get(import.library_id))
  66. << "\"\n";
  67. }
  68. return code;
  69. }
  70. // Adds the name to the scope with the given `access_kind` and `inst_id`.
  71. // `inst_id` must have a value.
  72. static auto AddNameToScope(Context& context, SemIR::NameScopeId scope_id,
  73. SemIR::NameId name_id, SemIR::AccessKind access_kind,
  74. SemIR::InstId inst_id) -> void {
  75. CARBON_CHECK(inst_id.has_value());
  76. context.name_scopes().Get(scope_id).AddRequired(
  77. {.name_id = name_id,
  78. .result = SemIR::ScopeLookupResult::MakeFound(inst_id, access_kind)});
  79. }
  80. // Maps a Clang name to a Carbon `NameId`.
  81. static auto AddIdentifierName(Context& context, llvm::StringRef name)
  82. -> SemIR::NameId {
  83. return SemIR::NameId::ForIdentifier(context.identifiers().Add(name));
  84. }
  85. // Adds the given source location and an `ImportIRInst` referring to it in
  86. // `ImportIRId::Cpp`.
  87. static auto AddImportIRInst(Context& context,
  88. clang::SourceLocation clang_source_loc)
  89. -> SemIR::ImportIRInstId {
  90. SemIR::ClangSourceLocId clang_source_loc_id =
  91. context.sem_ir().clang_source_locs().Add(clang_source_loc);
  92. return context.import_ir_insts().Add(
  93. SemIR::ImportIRInst(clang_source_loc_id));
  94. }
  95. namespace {
  96. // Used to convert Clang diagnostics to Carbon diagnostics.
  97. class CarbonClangDiagnosticConsumer : public clang::DiagnosticConsumer {
  98. public:
  99. // Creates an instance with the location that triggers calling Clang.
  100. // `context` must not be null.
  101. explicit CarbonClangDiagnosticConsumer(
  102. Context* context, std::shared_ptr<clang::CompilerInvocation> invocation)
  103. : context_(context), invocation_(std::move(invocation)) {}
  104. // Generates a Carbon warning for each Clang warning and a Carbon error for
  105. // each Clang error or fatal.
  106. auto HandleDiagnostic(clang::DiagnosticsEngine::Level diag_level,
  107. const clang::Diagnostic& info) -> void override {
  108. DiagnosticConsumer::HandleDiagnostic(diag_level, info);
  109. SemIR::ImportIRInstId clang_import_ir_inst_id =
  110. AddImportIRInst(*context_, info.getLocation());
  111. llvm::SmallString<256> message;
  112. info.FormatDiagnostic(message);
  113. if (!info.hasSourceManager()) {
  114. // If we don't have a source manager, we haven't actually started
  115. // compiling yet, and this is an error from the driver or early in the
  116. // frontend. Pass it on directly.
  117. CARBON_CHECK(info.getLocation().isInvalid());
  118. diagnostic_infos_.push_back({.level = diag_level,
  119. .import_ir_inst_id = clang_import_ir_inst_id,
  120. .message = message.str().str()});
  121. return;
  122. }
  123. RawStringOstream diagnostics_stream;
  124. clang::TextDiagnostic text_diagnostic(diagnostics_stream,
  125. invocation_->getLangOpts(),
  126. invocation_->getDiagnosticOpts());
  127. text_diagnostic.emitDiagnostic(
  128. clang::FullSourceLoc(info.getLocation(), info.getSourceManager()),
  129. diag_level, message, info.getRanges(), info.getFixItHints());
  130. std::string diagnostics_str = diagnostics_stream.TakeStr();
  131. diagnostic_infos_.push_back({.level = diag_level,
  132. .import_ir_inst_id = clang_import_ir_inst_id,
  133. .message = diagnostics_str});
  134. }
  135. // Outputs Carbon diagnostics based on the collected Clang diagnostics. Must
  136. // be called after the AST is set in the context.
  137. auto EmitDiagnostics() -> void {
  138. for (const ClangDiagnosticInfo& info : diagnostic_infos_) {
  139. switch (info.level) {
  140. case clang::DiagnosticsEngine::Ignored:
  141. case clang::DiagnosticsEngine::Note:
  142. case clang::DiagnosticsEngine::Remark: {
  143. context_->TODO(
  144. SemIR::LocId(info.import_ir_inst_id),
  145. llvm::formatv(
  146. "Unsupported: C++ diagnostic level for diagnostic\n{0}",
  147. info.message));
  148. break;
  149. }
  150. case clang::DiagnosticsEngine::Warning:
  151. case clang::DiagnosticsEngine::Error:
  152. case clang::DiagnosticsEngine::Fatal: {
  153. CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning, "{0}",
  154. std::string);
  155. CARBON_DIAGNOSTIC(CppInteropParseError, Error, "{0}", std::string);
  156. context_->emitter().Emit(
  157. SemIR::LocId(info.import_ir_inst_id),
  158. info.level == clang::DiagnosticsEngine::Warning
  159. ? CppInteropParseWarning
  160. : CppInteropParseError,
  161. info.message);
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. private:
  168. // The type-checking context in which we're running Clang.
  169. Context* context_;
  170. // The compiler invocation that is producing the diagnostics.
  171. std::shared_ptr<clang::CompilerInvocation> invocation_;
  172. // Information on a Clang diagnostic that can be converted to a Carbon
  173. // diagnostic.
  174. struct ClangDiagnosticInfo {
  175. // The Clang diagnostic level.
  176. clang::DiagnosticsEngine::Level level;
  177. // The ID of the ImportIR instruction referring to the Clang source
  178. // location.
  179. SemIR::ImportIRInstId import_ir_inst_id;
  180. // The Clang diagnostic textual message.
  181. std::string message;
  182. };
  183. // Collects the information for all Clang diagnostics to be converted to
  184. // Carbon diagnostics after the context has been initialized with the Clang
  185. // AST.
  186. llvm::SmallVector<ClangDiagnosticInfo> diagnostic_infos_;
  187. };
  188. } // namespace
  189. // Returns an AST for the C++ imports and a bool that represents whether
  190. // compilation errors where encountered or the generated AST is null due to an
  191. // error. Sets the AST in the context's `sem_ir`.
  192. // TODO: Consider to always have a (non-null) AST.
  193. static auto GenerateAst(Context& context,
  194. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  195. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  196. std::shared_ptr<clang::CompilerInvocation> invocation)
  197. -> std::pair<std::unique_ptr<clang::ASTUnit>, bool> {
  198. // Build a diagnostics engine.
  199. auto diagnostics_consumer =
  200. std::make_unique<CarbonClangDiagnosticConsumer>(&context, invocation);
  201. llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
  202. clang::CompilerInstance::createDiagnostics(
  203. *fs, invocation->getDiagnosticOpts(), diagnostics_consumer.get(),
  204. /*ShouldOwnClient=*/false));
  205. // Extract the input from the frontend invocation and make sure it makes
  206. // sense.
  207. const auto& inputs = invocation->getFrontendOpts().Inputs;
  208. CARBON_CHECK(inputs.size() == 1 &&
  209. inputs[0].getKind().getLanguage() == clang::Language::CXX &&
  210. inputs[0].getKind().getFormat() == clang::InputKind::Source);
  211. llvm::StringRef file_name = inputs[0].getFile();
  212. // Remap the imports file name to the corresponding `#include`s.
  213. // TODO: Modify the frontend options to specify this memory buffer as input
  214. // instead of remapping the file.
  215. std::string includes = GenerateCppIncludesHeaderCode(context, imports);
  216. auto includes_buffer = llvm::MemoryBuffer::getMemBuffer(includes, file_name);
  217. invocation->getPreprocessorOpts().addRemappedFile(file_name,
  218. includes_buffer.get());
  219. // Create the AST unit.
  220. auto ast = clang::ASTUnit::LoadFromCompilerInvocation(
  221. invocation, std::make_shared<clang::PCHContainerOperations>(), nullptr,
  222. diags, new clang::FileManager(invocation->getFileSystemOpts(), fs));
  223. // Remove remapped file before its underlying storage is destroyed.
  224. invocation->getPreprocessorOpts().clearRemappedFiles();
  225. // Attach the AST to SemIR. This needs to be done before we can emit any
  226. // diagnostics, so their locations can be properly interpreted by our
  227. // diagnostics machinery.
  228. context.sem_ir().set_cpp_ast(ast.get());
  229. // Emit any diagnostics we queued up while building the AST.
  230. diagnostics_consumer->EmitDiagnostics();
  231. bool any_errors = diagnostics_consumer->getNumErrors() > 0;
  232. // Transfer ownership of the consumer to the AST unit, in case more
  233. // diagnostics are produced by AST queries.
  234. ast->getDiagnostics().setClient(diagnostics_consumer.release(),
  235. /*ShouldOwnClient=*/true);
  236. return {std::move(ast), !ast || any_errors};
  237. }
  238. // Adds a namespace for the `Cpp` import and returns its `NameScopeId`.
  239. static auto AddNamespace(Context& context, PackageNameId cpp_package_id,
  240. llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
  241. -> SemIR::NameScopeId {
  242. auto& import_cpps = context.sem_ir().import_cpps();
  243. import_cpps.Reserve(imports.size());
  244. for (const Parse::Tree::PackagingNames& import : imports) {
  245. import_cpps.Add({.node_id = context.parse_tree().As<Parse::ImportDeclId>(
  246. import.node_id),
  247. .library_id = import.library_id});
  248. }
  249. return AddImportNamespaceToScope(
  250. context,
  251. GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
  252. SemIR::NameId::ForPackageName(cpp_package_id),
  253. SemIR::NameScopeId::Package,
  254. /*diagnose_duplicate_namespace=*/false,
  255. [&]() {
  256. return AddInst<SemIR::ImportCppDecl>(
  257. context,
  258. context.parse_tree().As<Parse::ImportDeclId>(
  259. imports.front().node_id),
  260. {});
  261. })
  262. .add_result.name_scope_id;
  263. }
  264. auto ImportCppFiles(Context& context,
  265. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  266. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  267. std::shared_ptr<clang::CompilerInvocation> invocation)
  268. -> std::unique_ptr<clang::ASTUnit> {
  269. if (imports.empty()) {
  270. return nullptr;
  271. }
  272. CARBON_CHECK(!context.sem_ir().cpp_ast());
  273. PackageNameId package_id = imports.front().package_id;
  274. CARBON_CHECK(
  275. llvm::all_of(imports, [&](const Parse::Tree::PackagingNames& import) {
  276. return import.package_id == package_id;
  277. }));
  278. auto name_scope_id = AddNamespace(context, package_id, imports);
  279. auto [generated_ast, ast_has_error] =
  280. GenerateAst(context, imports, fs, std::move(invocation));
  281. SemIR::NameScope& name_scope = context.name_scopes().Get(name_scope_id);
  282. name_scope.set_is_closed_import(true);
  283. name_scope.set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
  284. {.decl = generated_ast->getASTContext().getTranslationUnitDecl(),
  285. .inst_id = name_scope.inst_id()}));
  286. if (ast_has_error) {
  287. name_scope.set_has_error();
  288. }
  289. return std::move(generated_ast);
  290. }
  291. // Look ups the given name in the Clang AST in a specific scope. Returns the
  292. // lookup result if lookup was successful.
  293. static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
  294. SemIR::NameId name_id)
  295. -> std::optional<clang::LookupResult> {
  296. std::optional<llvm::StringRef> name =
  297. context.names().GetAsStringIfIdentifier(name_id);
  298. if (!name) {
  299. // Special names never exist in C++ code.
  300. return std::nullopt;
  301. }
  302. clang::ASTUnit* ast = context.sem_ir().cpp_ast();
  303. CARBON_CHECK(ast);
  304. clang::Sema& sema = ast->getSema();
  305. clang::LookupResult lookup(
  306. sema,
  307. clang::DeclarationNameInfo(
  308. clang::DeclarationName(
  309. sema.getPreprocessor().getIdentifierInfo(*name)),
  310. clang::SourceLocation()),
  311. clang::Sema::LookupNameKind::LookupOrdinaryName);
  312. auto scope_clang_decl_context_id =
  313. context.name_scopes().Get(scope_id).clang_decl_context_id();
  314. bool found = sema.LookupQualifiedName(
  315. lookup,
  316. clang::dyn_cast<clang::DeclContext>(context.sem_ir()
  317. .clang_decls()
  318. .Get(scope_clang_decl_context_id)
  319. .decl));
  320. if (!found) {
  321. return std::nullopt;
  322. }
  323. return lookup;
  324. }
  325. // Returns whether `decl` already mapped to an instruction.
  326. static auto IsClangDeclImported(const Context& context, clang::Decl* decl)
  327. -> bool {
  328. return context.sem_ir()
  329. .clang_decls()
  330. .Lookup(decl->getCanonicalDecl())
  331. .has_value();
  332. }
  333. // If `decl` already mapped to an instruction, returns that instruction.
  334. // Otherwise returns `None`.
  335. static auto LookupClangDeclInstId(const Context& context, clang::Decl* decl)
  336. -> SemIR::InstId {
  337. const auto& clang_decls = context.sem_ir().clang_decls();
  338. if (auto context_clang_decl_id = clang_decls.Lookup(decl->getCanonicalDecl());
  339. context_clang_decl_id.has_value()) {
  340. return clang_decls.Get(context_clang_decl_id).inst_id;
  341. }
  342. return SemIR::InstId::None;
  343. }
  344. // Returns the parent of the given declaration. Skips declaration types we
  345. // ignore.
  346. static auto GetParentDecl(clang::Decl* clang_decl) -> clang::Decl* {
  347. clang::DeclContext* decl_context = clang_decl->getDeclContext();
  348. while (llvm::isa<clang::LinkageSpecDecl>(decl_context)) {
  349. decl_context = decl_context->getParent();
  350. }
  351. return llvm::cast<clang::Decl>(decl_context);
  352. }
  353. // Returns the given declaration's parent scope. Assumes the parent declaration
  354. // was already imported.
  355. static auto GetParentNameScopeId(Context& context, clang::Decl* clang_decl)
  356. -> SemIR::NameScopeId {
  357. SemIR::InstId parent_inst_id =
  358. LookupClangDeclInstId(context, GetParentDecl(clang_decl));
  359. CARBON_CHECK(parent_inst_id.has_value());
  360. CARBON_KIND_SWITCH(context.insts().Get(parent_inst_id)) {
  361. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  362. return context.classes().Get(class_decl.class_id).scope_id;
  363. }
  364. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  365. return context.interfaces().Get(interface_decl.interface_id).scope_id;
  366. }
  367. case CARBON_KIND(SemIR::Namespace namespace_inst): {
  368. return namespace_inst.name_scope_id;
  369. }
  370. default: {
  371. CARBON_FATAL("Unexpected parent instruction kind");
  372. }
  373. }
  374. }
  375. // Imports a namespace declaration from Clang to Carbon. If successful, returns
  376. // the new Carbon namespace declaration `InstId`. If the declaration was already
  377. // imported, returns the mapped instruction.
  378. static auto ImportNamespaceDecl(Context& context,
  379. clang::NamespaceDecl* clang_decl)
  380. -> SemIR::InstId {
  381. // Check if the declaration is already mapped.
  382. if (SemIR::InstId existing_inst_id =
  383. LookupClangDeclInstId(context, clang_decl);
  384. existing_inst_id.has_value()) {
  385. return existing_inst_id;
  386. }
  387. auto result = AddImportNamespace(
  388. context, GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
  389. AddIdentifierName(context, clang_decl->getName()),
  390. GetParentNameScopeId(context, clang_decl),
  391. /*import_id=*/SemIR::InstId::None);
  392. context.name_scopes()
  393. .Get(result.name_scope_id)
  394. .set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
  395. {.decl = clang_decl->getCanonicalDecl(), .inst_id = result.inst_id}));
  396. return result.inst_id;
  397. }
  398. static auto MapType(Context& context, SemIR::LocId loc_id, clang::QualType type)
  399. -> TypeExpr;
  400. // Creates a class declaration for the given class name in the given scope.
  401. // Returns the `InstId` for the declaration.
  402. static auto BuildClassDecl(Context& context,
  403. SemIR::ImportIRInstId import_ir_inst_id,
  404. SemIR::NameScopeId parent_scope_id,
  405. SemIR::NameId name_id)
  406. -> std::tuple<SemIR::ClassId, SemIR::TypeInstId> {
  407. // Add the class declaration.
  408. auto class_decl = SemIR::ClassDecl{.type_id = SemIR::TypeType::TypeId,
  409. .class_id = SemIR::ClassId::None,
  410. .decl_block_id = SemIR::InstBlockId::None};
  411. auto class_decl_id = AddPlaceholderInstInNoBlock(
  412. context,
  413. SemIR::LocIdAndInst::UncheckedLoc(import_ir_inst_id, class_decl));
  414. context.imports().push_back(class_decl_id);
  415. SemIR::Class class_info = {
  416. {.name_id = name_id,
  417. .parent_scope_id = parent_scope_id,
  418. .generic_id = SemIR::GenericId::None,
  419. .first_param_node_id = Parse::NodeId::None,
  420. .last_param_node_id = Parse::NodeId::None,
  421. .pattern_block_id = SemIR::InstBlockId::None,
  422. .implicit_param_patterns_id = SemIR::InstBlockId::None,
  423. .param_patterns_id = SemIR::InstBlockId::None,
  424. .is_extern = false,
  425. .extern_library_id = SemIR::LibraryNameId::None,
  426. .non_owning_decl_id = SemIR::InstId::None,
  427. .first_owning_decl_id = class_decl_id},
  428. {// `.self_type_id` depends on the ClassType, so is set below.
  429. .self_type_id = SemIR::TypeId::None,
  430. // TODO: Support Dynamic classes.
  431. // TODO: Support Final classes.
  432. .inheritance_kind = SemIR::Class::Base}};
  433. class_decl.class_id = context.classes().Add(class_info);
  434. // Write the class ID into the ClassDecl.
  435. ReplaceInstBeforeConstantUse(context, class_decl_id, class_decl);
  436. SetClassSelfType(context, class_decl.class_id);
  437. return {class_decl.class_id, context.types().GetAsTypeInstId(class_decl_id)};
  438. }
  439. // Checks that the specified finished class definition is valid and builds and
  440. // returns a corresponding complete type witness instruction.
  441. // TODO: Remove recursion into mapping field types.
  442. // NOLINTNEXTLINE(misc-no-recursion)
  443. static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
  444. SemIR::ImportIRInstId import_ir_inst_id,
  445. SemIR::TypeInstId class_type_inst_id,
  446. const clang::CXXRecordDecl* clang_def)
  447. -> SemIR::TypeInstId {
  448. // For now, if the class is empty, produce an empty struct as the object
  449. // representation. This allows our tests to continue to pass while we don't
  450. // properly support initializing imported C++ classes.
  451. // TODO: Remove this.
  452. if (clang_def->isEmpty() && !clang_def->getNumBases()) {
  453. return context.types().GetAsTypeInstId(AddInst(
  454. context,
  455. MakeImportedLocIdAndInst(
  456. context, import_ir_inst_id,
  457. SemIR::StructType{.type_id = SemIR::TypeType::TypeId,
  458. .fields_id = SemIR::StructTypeFieldsId::Empty})));
  459. }
  460. const auto& clang_layout =
  461. context.ast_context().getASTRecordLayout(clang_def);
  462. llvm::SmallVector<uint64_t> layout;
  463. llvm::SmallVector<SemIR::StructTypeField> fields;
  464. static_assert(SemIR::CustomLayoutId::SizeIndex == 0);
  465. layout.push_back(clang_layout.getSize().getQuantity());
  466. static_assert(SemIR::CustomLayoutId::AlignIndex == 1);
  467. layout.push_back(clang_layout.getAlignment().getQuantity());
  468. static_assert(SemIR::CustomLayoutId::FirstFieldIndex == 2);
  469. // TODO: Import vptr(s).
  470. // Import bases.
  471. for (const auto& base : clang_def->bases()) {
  472. if (base.isVirtual()) {
  473. // TODO: Handle virtual bases. We don't actually know where they go in the
  474. // layout. We may also want to use a different size in the layout for
  475. // `partial C`, excluding the virtual base. It's also not entirely safe to
  476. // just skip over the virtual base, as the type we would construct would
  477. // have a misleading size.
  478. context.TODO(import_ir_inst_id, "class with virtual bases");
  479. return SemIR::ErrorInst::TypeInstId;
  480. }
  481. auto [base_type_inst_id, base_type_id] =
  482. MapType(context, import_ir_inst_id, base.getType());
  483. if (!base_type_id.has_value()) {
  484. // TODO: If the base class's type can't be mapped, skip it.
  485. continue;
  486. }
  487. auto base_decl_id = AddInst(
  488. context,
  489. MakeImportedLocIdAndInst(
  490. context, import_ir_inst_id,
  491. SemIR::BaseDecl{.type_id = GetUnboundElementType(
  492. context, class_type_inst_id, base_type_inst_id),
  493. .base_type_inst_id = base_type_inst_id,
  494. .index = SemIR::ElementIndex(fields.size())}));
  495. // If there's exactly one base class, treat it as a Carbon base class too.
  496. // TODO: Improve handling for the case where the class has multiple base
  497. // classes.
  498. if (clang_def->getNumBases() == 1) {
  499. auto& class_info = context.classes().Get(class_id);
  500. CARBON_CHECK(!class_info.base_id.has_value());
  501. class_info.base_id = base_decl_id;
  502. }
  503. auto* base_class = base.getType()->getAsCXXRecordDecl();
  504. CARBON_CHECK(base_class, "Base class {0} is not a class",
  505. base.getType().getAsString());
  506. auto base_offset = base.isVirtual()
  507. ? clang_layout.getVBaseClassOffset(base_class)
  508. : clang_layout.getBaseClassOffset(base_class);
  509. layout.push_back(base_offset.getQuantity());
  510. fields.push_back(
  511. {.name_id = SemIR::NameId::Base, .type_inst_id = base_type_inst_id});
  512. }
  513. // Import fields.
  514. for (auto* decl : clang_def->decls()) {
  515. auto* field = clang::dyn_cast<clang::FieldDecl>(decl);
  516. // Track the chain of fields from the class to this field. This chain is
  517. // only one element long unless the field is a member of an anonymous struct
  518. // or union.
  519. clang::NamedDecl* single_field_chain[1] = {field};
  520. llvm::ArrayRef<clang::NamedDecl*> chain = single_field_chain;
  521. // If this isn't a field, it might be an indirect field in an anonymous
  522. // struct or union.
  523. if (!field) {
  524. auto* indirect_field = clang::dyn_cast<clang::IndirectFieldDecl>(decl);
  525. if (!indirect_field) {
  526. continue;
  527. }
  528. chain = indirect_field->chain();
  529. field = indirect_field->getAnonField();
  530. }
  531. if (field->isBitField()) {
  532. // TODO: Add a representation for named bitfield members.
  533. continue;
  534. }
  535. if (field->isAnonymousStructOrUnion()) {
  536. // Fields within an anonymous structure or union will be added via their
  537. // IndirectFieldDecls.
  538. continue;
  539. }
  540. auto field_name_id = AddIdentifierName(context, field->getName());
  541. auto [field_type_inst_id, field_type_id] =
  542. MapType(context, import_ir_inst_id, field->getType());
  543. if (!field_type_inst_id.has_value()) {
  544. // TODO: For now, just skip over fields whose types we can't map.
  545. continue;
  546. }
  547. // Create a field now, as we know the index to use.
  548. // TODO: Consider doing this lazily instead.
  549. auto field_decl_id = AddInst(
  550. context, MakeImportedLocIdAndInst(
  551. context, import_ir_inst_id,
  552. SemIR::FieldDecl{
  553. .type_id = GetUnboundElementType(
  554. context, class_type_inst_id, field_type_inst_id),
  555. .name_id = field_name_id,
  556. .index = SemIR::ElementIndex(fields.size())}));
  557. context.sem_ir().clang_decls().Add(
  558. {.decl = decl->getCanonicalDecl(), .inst_id = field_decl_id});
  559. // Compute the offset to the field that appears directly in the class.
  560. uint64_t offset = clang_layout.getFieldOffset(
  561. clang::cast<clang::FieldDecl>(chain.front())->getFieldIndex());
  562. // If this is an indirect field, walk the path and accumulate the offset to
  563. // the named field.
  564. for (auto* inner_decl : chain.drop_front()) {
  565. auto* inner_field = clang::cast<clang::FieldDecl>(inner_decl);
  566. const auto& inner_layout =
  567. context.ast_context().getASTRecordLayout(inner_field->getParent());
  568. offset += inner_layout.getFieldOffset(inner_field->getFieldIndex());
  569. }
  570. layout.push_back(
  571. context.ast_context().toCharUnitsFromBits(offset).getQuantity());
  572. fields.push_back(
  573. {.name_id = field_name_id, .type_inst_id = field_type_inst_id});
  574. }
  575. // TODO: Add a field to prevent tail padding reuse if necessary.
  576. return AddTypeInst<SemIR::CustomLayoutType>(
  577. context, import_ir_inst_id,
  578. {.type_id = SemIR::TypeType::TypeId,
  579. .fields_id = context.struct_type_fields().Add(fields),
  580. .layout_id = context.custom_layouts().Add(layout)});
  581. }
  582. // Creates a class definition based on the information in the given Clang
  583. // declaration, which is assumed to be for a class definition.
  584. // TODO: Remove recursion into mapping field types.
  585. // NOLINTNEXTLINE(misc-no-recursion)
  586. static auto BuildClassDefinition(Context& context,
  587. SemIR::ImportIRInstId import_ir_inst_id,
  588. SemIR::ClassId class_id,
  589. SemIR::TypeInstId class_inst_id,
  590. SemIR::ClangDeclId clang_decl_id,
  591. clang::CXXRecordDecl* clang_def) -> void {
  592. auto& class_info = context.classes().Get(class_id);
  593. StartClassDefinition(context, class_info, class_inst_id);
  594. // Name lookup into the Carbon class looks in the C++ class definition.
  595. context.name_scopes()
  596. .Get(class_info.scope_id)
  597. .set_clang_decl_context_id(clang_decl_id);
  598. context.inst_block_stack().Push();
  599. // Compute the class's object representation.
  600. auto object_repr_id = ImportClassObjectRepr(
  601. context, class_id, import_ir_inst_id, class_inst_id, clang_def);
  602. class_info.complete_type_witness_id = AddInst<SemIR::CompleteTypeWitness>(
  603. context, import_ir_inst_id,
  604. {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  605. .object_repr_type_inst_id = object_repr_id});
  606. class_info.body_block_id = context.inst_block_stack().Pop();
  607. }
  608. // Mark the given `Decl` as failed in `clang_decls`.
  609. static auto MarkFailedDecl(Context& context, clang::Decl* clang_decl) {
  610. context.sem_ir().clang_decls().Add({.decl = clang_decl->getCanonicalDecl(),
  611. .inst_id = SemIR::ErrorInst::InstId});
  612. }
  613. // Imports a record declaration from Clang to Carbon. If successful, returns
  614. // the new Carbon class declaration `InstId`.
  615. // TODO: Change `clang_decl` to `const &` when lookup is using `clang::DeclID`
  616. // and we don't need to store the decl for lookup context.
  617. // TODO: Remove recursion into mapping field types.
  618. // NOLINTNEXTLINE(misc-no-recursion)
  619. static auto ImportCXXRecordDecl(Context& context,
  620. clang::CXXRecordDecl* clang_decl)
  621. -> SemIR::InstId {
  622. clang::CXXRecordDecl* clang_def = clang_decl->getDefinition();
  623. if (clang_def) {
  624. clang_decl = clang_def;
  625. }
  626. auto import_ir_inst_id = AddImportIRInst(context, clang_decl->getLocation());
  627. auto [class_id, class_inst_id] = BuildClassDecl(
  628. context, import_ir_inst_id, GetParentNameScopeId(context, clang_decl),
  629. AddIdentifierName(context, clang_decl->getName()));
  630. // TODO: The caller does the same lookup. Avoid doing it twice.
  631. auto clang_decl_id = context.sem_ir().clang_decls().Add(
  632. {.decl = clang_decl->getCanonicalDecl(), .inst_id = class_inst_id});
  633. if (clang_def) {
  634. BuildClassDefinition(context, import_ir_inst_id, class_id, class_inst_id,
  635. clang_decl_id, clang_def);
  636. }
  637. return class_inst_id;
  638. }
  639. // Creates an integer type of the given size.
  640. static auto MakeIntType(Context& context, IntId size_id, bool is_signed)
  641. -> TypeExpr {
  642. auto type_inst_id = MakeIntTypeLiteral(
  643. context, Parse::NodeId::None,
  644. is_signed ? SemIR::IntKind::Signed : SemIR::IntKind::Unsigned, size_id);
  645. return ExprAsType(context, Parse::NodeId::None, type_inst_id);
  646. }
  647. // Maps a C++ builtin type to a Carbon type.
  648. // TODO: Support more builtin types.
  649. static auto MapBuiltinType(Context& context, clang::QualType qual_type,
  650. const clang::BuiltinType& type) -> TypeExpr {
  651. clang::ASTContext& ast_context = context.ast_context();
  652. if (type.isBooleanType()) {
  653. CARBON_CHECK(ast_context.hasSameType(qual_type, ast_context.BoolTy));
  654. return ExprAsType(context, Parse::NodeId::None,
  655. context.types().GetInstId(GetSingletonType(
  656. context, SemIR::BoolType::TypeInstId)));
  657. }
  658. if (type.isInteger()) {
  659. auto width = ast_context.getIntWidth(qual_type);
  660. bool is_signed = type.isSignedInteger();
  661. auto int_n_type = ast_context.getIntTypeForBitwidth(width, is_signed);
  662. if (ast_context.hasSameType(qual_type, int_n_type)) {
  663. return MakeIntType(context, context.ints().Add(width), is_signed);
  664. }
  665. // TODO: Handle integer types that map to named aliases.
  666. }
  667. return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
  668. }
  669. // Maps a C++ record type to a Carbon type.
  670. // TODO: Support more record types.
  671. // TODO: Remove recursion mapping fields of class types.
  672. // NOLINTNEXTLINE(misc-no-recursion)
  673. static auto MapRecordType(Context& context, const clang::RecordType& type)
  674. -> TypeExpr {
  675. auto* record_decl = clang::dyn_cast<clang::CXXRecordDecl>(type.getDecl());
  676. if (!record_decl) {
  677. return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
  678. }
  679. // Check if the declaration is already mapped.
  680. SemIR::InstId record_inst_id = LookupClangDeclInstId(context, record_decl);
  681. if (!record_inst_id.has_value()) {
  682. record_inst_id = ImportCXXRecordDecl(context, record_decl);
  683. }
  684. SemIR::TypeInstId record_type_inst_id =
  685. context.types().GetAsTypeInstId(record_inst_id);
  686. return {
  687. .inst_id = record_type_inst_id,
  688. .type_id = context.types().GetTypeIdForTypeInstId(record_type_inst_id)};
  689. }
  690. // Maps a C++ type that is not a wrapper type such as a pointer to a Carbon
  691. // type.
  692. // TODO: Support more types.
  693. // TODO: Remove recursion mapping fields of class types.
  694. // NOLINTNEXTLINE(misc-no-recursion)
  695. static auto MapNonWrapperType(Context& context, clang::QualType type)
  696. -> TypeExpr {
  697. if (const auto* builtin_type = type->getAs<clang::BuiltinType>()) {
  698. return MapBuiltinType(context, type, *builtin_type);
  699. }
  700. if (const auto* record_type = type->getAs<clang::RecordType>()) {
  701. return MapRecordType(context, *record_type);
  702. }
  703. CARBON_CHECK(!type.hasQualifiers() && !type->isPointerType(),
  704. "Should not see wrapper types here");
  705. return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
  706. }
  707. // Maps a qualified C++ type to a Carbon type.
  708. static auto MapQualifiedType(Context& context, SemIR::LocId loc_id,
  709. clang::QualType type, TypeExpr type_expr)
  710. -> TypeExpr {
  711. auto quals = type.getQualifiers();
  712. if (quals.hasConst()) {
  713. auto type_id = GetConstType(context, type_expr.inst_id);
  714. type_expr = {.inst_id = context.types().GetInstId(type_id),
  715. .type_id = type_id};
  716. quals.removeConst();
  717. }
  718. // TODO: Support other qualifiers.
  719. if (!quals.empty()) {
  720. context.TODO(loc_id, llvm::formatv("Unsupported: qualified type: {0}",
  721. type.getAsString()));
  722. return {.inst_id = SemIR::ErrorInst::TypeInstId,
  723. .type_id = SemIR::ErrorInst::TypeId};
  724. }
  725. return type_expr;
  726. }
  727. // Maps a C++ pointer type to a Carbon pointer type.
  728. static auto MapPointerType(Context& context, SemIR::LocId loc_id,
  729. clang::QualType type, TypeExpr pointee_type_expr)
  730. -> TypeExpr {
  731. CARBON_CHECK(type->isPointerType());
  732. if (auto nullability = type->getNullability();
  733. !nullability.has_value() ||
  734. *nullability != clang::NullabilityKind::NonNull) {
  735. context.TODO(loc_id, llvm::formatv("Unsupported: nullable pointer: {0}",
  736. type.getAsString()));
  737. return {.inst_id = SemIR::ErrorInst::TypeInstId,
  738. .type_id = SemIR::ErrorInst::TypeId};
  739. }
  740. SemIR::TypeId pointer_type_id =
  741. GetPointerType(context, pointee_type_expr.inst_id);
  742. return {.inst_id = context.types().GetInstId(pointer_type_id),
  743. .type_id = pointer_type_id};
  744. }
  745. // Maps a C++ type to a Carbon type. `type` should not be canonicalized because
  746. // we check for pointer nullability and nullability will be lost by
  747. // canonicalization.
  748. // TODO: Remove recursion mapping fields of class types.
  749. // NOLINTNEXTLINE(misc-no-recursion)
  750. static auto MapType(Context& context, SemIR::LocId loc_id, clang::QualType type)
  751. -> TypeExpr {
  752. // Unwrap any type modifiers and wrappers.
  753. llvm::SmallVector<clang::QualType> wrapper_types;
  754. while (true) {
  755. clang::QualType orig_type = type;
  756. if (type.hasQualifiers()) {
  757. type = type.getUnqualifiedType();
  758. } else if (type->isPointerType()) {
  759. type = type->getPointeeType();
  760. } else {
  761. break;
  762. }
  763. wrapper_types.push_back(orig_type);
  764. }
  765. auto mapped = MapNonWrapperType(context, type);
  766. for (auto wrapper : llvm::reverse(wrapper_types)) {
  767. if (!mapped.inst_id.has_value() ||
  768. mapped.type_id == SemIR::ErrorInst::TypeId) {
  769. break;
  770. }
  771. if (wrapper.hasQualifiers()) {
  772. mapped = MapQualifiedType(context, loc_id, wrapper, mapped);
  773. } else if (wrapper->isPointerType()) {
  774. mapped = MapPointerType(context, loc_id, wrapper, mapped);
  775. } else {
  776. CARBON_FATAL("Unexpected wrapper type {0}", wrapper.getAsString());
  777. }
  778. }
  779. return mapped;
  780. }
  781. // Returns a block for the implicit parameters of the given function
  782. // declaration. Because function templates are not yet supported, this currently
  783. // only contains the `self` parameter. On error, produces a diagnostic and
  784. // returns None.
  785. static auto MakeImplicitParamPatternsBlockId(
  786. Context& context, SemIR::LocId loc_id,
  787. const clang::FunctionDecl& clang_decl) -> SemIR::InstBlockId {
  788. const auto* method_decl = dyn_cast<clang::CXXMethodDecl>(&clang_decl);
  789. if (!method_decl || method_decl->isStatic()) {
  790. return SemIR::InstBlockId::Empty;
  791. }
  792. // Build a `self` parameter from the object parameter.
  793. BeginSubpattern(context);
  794. // Perform some special-case mapping for the object parameter:
  795. //
  796. // - If it's a const reference to T, produce a by-value `self: T` parameter.
  797. // - If it's a non-const reference to T, produce an `addr self: T*`
  798. // parameter.
  799. // - Otherwise, map it directly, which will currently fail for `&&`-qualified
  800. // methods.
  801. //
  802. // TODO: Some of this mapping should be performed for all parameters.
  803. clang::QualType param_type =
  804. method_decl->getFunctionObjectParameterReferenceType();
  805. bool addr_self = false;
  806. if (param_type->isLValueReferenceType()) {
  807. param_type = param_type.getNonReferenceType();
  808. if (param_type.isConstQualified()) {
  809. // TODO: Consider only doing this if `const` is the only qualifier. For
  810. // now, any other qualifier will fail when mapping the type.
  811. auto split_type = param_type.getSplitUnqualifiedType();
  812. split_type.Quals.removeConst();
  813. param_type = method_decl->getASTContext().getQualifiedType(split_type);
  814. } else {
  815. addr_self = true;
  816. }
  817. }
  818. auto [type_inst_id, type_id] = MapType(context, loc_id, param_type);
  819. SemIR::ExprRegionId type_expr_region_id =
  820. EndSubpatternAsExpr(context, type_inst_id);
  821. if (!type_id.has_value()) {
  822. context.TODO(loc_id,
  823. llvm::formatv("Unsupported: object parameter type: {0}",
  824. param_type.getAsString()));
  825. return SemIR::InstBlockId::None;
  826. }
  827. if (addr_self) {
  828. type_id = GetPointerType(context, type_inst_id);
  829. }
  830. SemIR::InstId pattern_id =
  831. // TODO: Fill in a location once available.
  832. AddBindingPattern(context, SemIR::LocId::None, SemIR::NameId::SelfValue,
  833. type_id, type_expr_region_id, /*is_generic*/ false,
  834. /*is_template*/ false)
  835. .pattern_id;
  836. // TODO: Fill in a location once available.
  837. pattern_id = AddPatternInst<SemIR::ValueParamPattern>(
  838. context, SemIR::LocId::None,
  839. {.type_id = context.insts().Get(pattern_id).type_id(),
  840. .subpattern_id = pattern_id,
  841. .index = SemIR::CallParamIndex::None});
  842. // If we're building `addr self: Self*`, do that now.
  843. if (addr_self) {
  844. // TODO: Fill in a location once available.
  845. pattern_id = AddPatternInst<SemIR::AddrPattern>(
  846. context, SemIR::LocId::None,
  847. {.type_id = GetPatternType(context, SemIR::AutoType::TypeId),
  848. .inner_id = pattern_id});
  849. }
  850. return context.inst_blocks().Add({pattern_id});
  851. }
  852. // Returns a block id for the explicit parameters of the given function
  853. // declaration. If the function declaration has no parameters, it returns
  854. // `SemIR::InstBlockId::Empty`. In the case of an unsupported parameter type, it
  855. // produces an error and returns `SemIR::InstBlockId::None`.
  856. // TODO: Consider refactoring to extract and reuse more logic from
  857. // `HandleAnyBindingPattern()`.
  858. static auto MakeParamPatternsBlockId(Context& context, SemIR::LocId loc_id,
  859. const clang::FunctionDecl& clang_decl)
  860. -> SemIR::InstBlockId {
  861. if (clang_decl.parameters().empty()) {
  862. return SemIR::InstBlockId::Empty;
  863. }
  864. llvm::SmallVector<SemIR::InstId> params;
  865. params.reserve(clang_decl.parameters().size());
  866. for (const clang::ParmVarDecl* param : clang_decl.parameters()) {
  867. // TODO: Get the parameter type from the function, not from the
  868. // `ParmVarDecl`. The type of the `ParmVarDecl` is the type within the
  869. // function, and isn't in general the same as the type that's exposed to
  870. // callers. In particular, the parameter type exposed to callers will never
  871. // be cv-qualified.
  872. clang::QualType param_type = param->getType();
  873. // Mark the start of a region of insts, needed for the type expression
  874. // created later with the call of `EndSubpatternAsExpr()`.
  875. BeginSubpattern(context);
  876. auto [type_inst_id, type_id] = MapType(context, loc_id, param_type);
  877. // Type expression of the binding pattern - a single-entry/single-exit
  878. // region that allows control flow in the type expression e.g. fn F(x: if C
  879. // then i32 else i64).
  880. SemIR::ExprRegionId type_expr_region_id =
  881. EndSubpatternAsExpr(context, type_inst_id);
  882. if (!type_id.has_value()) {
  883. context.TODO(loc_id, llvm::formatv("Unsupported: parameter type: {0}",
  884. param_type.getAsString()));
  885. return SemIR::InstBlockId::None;
  886. }
  887. llvm::StringRef param_name = param->getName();
  888. SemIR::NameId name_id =
  889. param_name.empty()
  890. // Translate an unnamed parameter to an underscore to
  891. // match Carbon's naming of unnamed/unused function params.
  892. ? SemIR::NameId::Underscore
  893. : AddIdentifierName(context, param_name);
  894. // TODO: Fix this once templates are supported.
  895. bool is_template = false;
  896. // TODO: Fix this once generics are supported.
  897. bool is_generic = false;
  898. SemIR::InstId binding_pattern_id =
  899. // TODO: Fill in a location once available.
  900. AddBindingPattern(context, SemIR::LocId::None, name_id, type_id,
  901. type_expr_region_id, is_generic, is_template)
  902. .pattern_id;
  903. SemIR::InstId var_pattern_id = AddPatternInst(
  904. context,
  905. // TODO: Fill in a location once available.
  906. SemIR::LocIdAndInst::NoLoc(SemIR::ValueParamPattern(
  907. {.type_id = context.insts().Get(binding_pattern_id).type_id(),
  908. .subpattern_id = binding_pattern_id,
  909. .index = SemIR::CallParamIndex::None})));
  910. params.push_back(var_pattern_id);
  911. }
  912. return context.inst_blocks().Add(params);
  913. }
  914. // Returns the return type of the given function declaration. In case of an
  915. // unsupported return type, it produces a diagnostic and returns
  916. // `SemIR::ErrorInst::InstId`.
  917. // TODO: Support more return types.
  918. static auto GetReturnType(Context& context, SemIR::LocId loc_id,
  919. const clang::FunctionDecl* clang_decl)
  920. -> SemIR::InstId {
  921. clang::QualType ret_type = clang_decl->getReturnType();
  922. if (ret_type->isVoidType()) {
  923. return SemIR::InstId::None;
  924. }
  925. auto [type_inst_id, type_id] = MapType(context, loc_id, ret_type);
  926. if (!type_inst_id.has_value()) {
  927. context.TODO(loc_id, llvm::formatv("Unsupported: return type: {0}",
  928. ret_type.getAsString()));
  929. return SemIR::ErrorInst::InstId;
  930. }
  931. auto pattern_type_id = GetPatternType(context, type_id);
  932. SemIR::InstId return_slot_pattern_id = AddPatternInst(
  933. // TODO: Fill in a location for the return type once available.
  934. context,
  935. SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
  936. {.type_id = pattern_type_id, .type_inst_id = type_inst_id})));
  937. SemIR::InstId param_pattern_id = AddPatternInst(
  938. // TODO: Fill in a location for the return type once available.
  939. context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
  940. {.type_id = pattern_type_id,
  941. .subpattern_id = return_slot_pattern_id,
  942. .index = SemIR::CallParamIndex::None})));
  943. return param_pattern_id;
  944. }
  945. namespace {
  946. // Represents the parameter patterns block id, the return slot pattern id and
  947. // the call parameters block id for a function declaration.
  948. struct FunctionParamsInsts {
  949. SemIR::InstBlockId implicit_param_patterns_id;
  950. SemIR::InstBlockId param_patterns_id;
  951. SemIR::InstId return_slot_pattern_id;
  952. SemIR::InstBlockId call_params_id;
  953. };
  954. } // namespace
  955. // Creates a block containing the parameter pattern instructions for the
  956. // explicit parameters, a parameter pattern instruction for the return type and
  957. // a block containing the call parameters of the function. Emits a callee
  958. // pattern-match for the explicit parameter patterns and the return slot pattern
  959. // to create the Call parameters instructions block. Currently the implicit
  960. // parameter patterns are not taken into account. Returns the parameter patterns
  961. // block id, the return slot pattern id, and the call parameters block id.
  962. // Produces a diagnostic and returns `std::nullopt` if the function declaration
  963. // has an unsupported parameter type.
  964. static auto CreateFunctionParamsInsts(Context& context, SemIR::LocId loc_id,
  965. const clang::FunctionDecl* clang_decl)
  966. -> std::optional<FunctionParamsInsts> {
  967. if (isa<clang::CXXConstructorDecl, clang::CXXDestructorDecl>(clang_decl)) {
  968. context.TODO(loc_id, "Unsupported: Constructor/Destructor");
  969. return std::nullopt;
  970. }
  971. auto implicit_param_patterns_id =
  972. MakeImplicitParamPatternsBlockId(context, loc_id, *clang_decl);
  973. if (!implicit_param_patterns_id.has_value()) {
  974. return std::nullopt;
  975. }
  976. auto param_patterns_id =
  977. MakeParamPatternsBlockId(context, loc_id, *clang_decl);
  978. if (!param_patterns_id.has_value()) {
  979. return std::nullopt;
  980. }
  981. auto return_slot_pattern_id = GetReturnType(context, loc_id, clang_decl);
  982. if (SemIR::ErrorInst::InstId == return_slot_pattern_id) {
  983. return std::nullopt;
  984. }
  985. auto call_params_id =
  986. CalleePatternMatch(context, implicit_param_patterns_id, param_patterns_id,
  987. return_slot_pattern_id);
  988. return {{.implicit_param_patterns_id = implicit_param_patterns_id,
  989. .param_patterns_id = param_patterns_id,
  990. .return_slot_pattern_id = return_slot_pattern_id,
  991. .call_params_id = call_params_id}};
  992. }
  993. // Imports a function declaration from Clang to Carbon. If successful, returns
  994. // the new Carbon function declaration `InstId`. If the declaration was already
  995. // imported, returns the mapped instruction.
  996. static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
  997. clang::FunctionDecl* clang_decl)
  998. -> SemIR::InstId {
  999. // Check if the declaration is already mapped.
  1000. if (SemIR::InstId existing_inst_id =
  1001. LookupClangDeclInstId(context, clang_decl);
  1002. existing_inst_id.has_value()) {
  1003. return existing_inst_id;
  1004. }
  1005. if (clang_decl->isVariadic()) {
  1006. context.TODO(loc_id, "Unsupported: Variadic function");
  1007. MarkFailedDecl(context, clang_decl);
  1008. return SemIR::ErrorInst::InstId;
  1009. }
  1010. if (clang_decl->getTemplatedKind() ==
  1011. clang::FunctionDecl::TK_FunctionTemplate) {
  1012. context.TODO(loc_id, "Unsupported: Template function");
  1013. MarkFailedDecl(context, clang_decl);
  1014. return SemIR::ErrorInst::InstId;
  1015. }
  1016. if (auto* method_decl = dyn_cast<clang::CXXMethodDecl>(clang_decl)) {
  1017. if (method_decl->isVirtual()) {
  1018. context.TODO(loc_id, "Unsupported: Virtual function");
  1019. MarkFailedDecl(context, clang_decl);
  1020. return SemIR::ErrorInst::InstId;
  1021. }
  1022. }
  1023. context.scope_stack().PushForDeclName();
  1024. context.inst_block_stack().Push();
  1025. context.pattern_block_stack().Push();
  1026. auto function_params_insts =
  1027. CreateFunctionParamsInsts(context, loc_id, clang_decl);
  1028. auto pattern_block_id = context.pattern_block_stack().Pop();
  1029. auto decl_block_id = context.inst_block_stack().Pop();
  1030. context.scope_stack().Pop();
  1031. if (!function_params_insts.has_value()) {
  1032. MarkFailedDecl(context, clang_decl);
  1033. return SemIR::ErrorInst::InstId;
  1034. }
  1035. auto function_decl = SemIR::FunctionDecl{
  1036. SemIR::TypeId::None, SemIR::FunctionId::None, decl_block_id};
  1037. auto decl_id =
  1038. AddPlaceholderInstInNoBlock(context, Parse::NodeId::None, function_decl);
  1039. context.imports().push_back(decl_id);
  1040. auto function_info = SemIR::Function{
  1041. {.name_id = AddIdentifierName(context, clang_decl->getName()),
  1042. .parent_scope_id = GetParentNameScopeId(context, clang_decl),
  1043. .generic_id = SemIR::GenericId::None,
  1044. .first_param_node_id = Parse::NodeId::None,
  1045. .last_param_node_id = Parse::NodeId::None,
  1046. .pattern_block_id = pattern_block_id,
  1047. .implicit_param_patterns_id =
  1048. function_params_insts->implicit_param_patterns_id,
  1049. .param_patterns_id = function_params_insts->param_patterns_id,
  1050. .is_extern = false,
  1051. .extern_library_id = SemIR::LibraryNameId::None,
  1052. .non_owning_decl_id = SemIR::InstId::None,
  1053. .first_owning_decl_id = decl_id,
  1054. .definition_id = SemIR::InstId::None},
  1055. {.call_params_id = function_params_insts->call_params_id,
  1056. .return_slot_pattern_id = function_params_insts->return_slot_pattern_id,
  1057. .virtual_modifier = SemIR::FunctionFields::VirtualModifier::None,
  1058. .self_param_id = FindSelfPattern(
  1059. context, function_params_insts->implicit_param_patterns_id),
  1060. .clang_decl_id = context.sem_ir().clang_decls().Add(
  1061. {.decl = clang_decl, .inst_id = decl_id})}};
  1062. function_decl.function_id = context.functions().Add(function_info);
  1063. function_decl.type_id = GetFunctionType(context, function_decl.function_id,
  1064. SemIR::SpecificId::None);
  1065. ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
  1066. return decl_id;
  1067. }
  1068. // Returns all decls that need to be imported before importing the given type.
  1069. static auto GetDependentUnimportedTypeDecls(const Context& context,
  1070. clang::QualType type)
  1071. -> llvm::SmallVector<clang::Decl*> {
  1072. while (true) {
  1073. type = type.getCanonicalType();
  1074. if (type->isPointerType() || type->isReferenceType()) {
  1075. type = type->getPointeeType();
  1076. } else if (const clang::ArrayType* array_type =
  1077. type->getAsArrayTypeUnsafe()) {
  1078. type = array_type->getElementType();
  1079. } else {
  1080. break;
  1081. }
  1082. }
  1083. type = type.getUnqualifiedType();
  1084. if (const auto* record_type = type->getAs<clang::RecordType>()) {
  1085. if (auto* record_decl =
  1086. clang::dyn_cast<clang::CXXRecordDecl>(record_type->getDecl())) {
  1087. if (!IsClangDeclImported(context, record_decl)) {
  1088. return {record_decl};
  1089. }
  1090. // TODO: Also collect base and field types.
  1091. }
  1092. }
  1093. return {};
  1094. }
  1095. // Returns all decls that need to be imported before importing the given
  1096. // function.
  1097. static auto GetDependentUnimportedFunctionDecls(
  1098. const Context& context, const clang::FunctionDecl& clang_decl)
  1099. -> llvm::SmallVector<clang::Decl*> {
  1100. llvm::SmallVector<clang::Decl*> decls;
  1101. for (const auto* param : clang_decl.parameters()) {
  1102. llvm::append_range(
  1103. decls, GetDependentUnimportedTypeDecls(context, param->getType()));
  1104. }
  1105. llvm::append_range(decls, GetDependentUnimportedTypeDecls(
  1106. context, clang_decl.getReturnType()));
  1107. return decls;
  1108. }
  1109. // Returns all decls that need to be imported before importing the given
  1110. // declaration.
  1111. static auto GetDependentUnimportedDecls(const Context& context,
  1112. clang::Decl* clang_decl)
  1113. -> llvm::SmallVector<clang::Decl*> {
  1114. llvm::SmallVector<clang::Decl*> decls;
  1115. if (auto* parent_decl = GetParentDecl(clang_decl);
  1116. !IsClangDeclImported(context, parent_decl)) {
  1117. decls.push_back(parent_decl);
  1118. }
  1119. if (auto* clang_function_decl = clang_decl->getAsFunction()) {
  1120. llvm::append_range(decls, GetDependentUnimportedFunctionDecls(
  1121. context, *clang_function_decl));
  1122. } else if (auto* type_decl = clang::dyn_cast<clang::TypeDecl>(clang_decl)) {
  1123. llvm::append_range(
  1124. decls,
  1125. GetDependentUnimportedTypeDecls(
  1126. context, type_decl->getASTContext().getTypeDeclType(type_decl)));
  1127. }
  1128. return decls;
  1129. }
  1130. // Imports a declaration from Clang to Carbon. If successful, returns the
  1131. // instruction for the new Carbon declaration. Assumes all dependencies have
  1132. // already been imported.
  1133. static auto ImportDeclAfterDependencies(Context& context, SemIR::LocId loc_id,
  1134. clang::Decl* clang_decl)
  1135. -> SemIR::InstId {
  1136. if (auto* clang_function_decl = clang_decl->getAsFunction()) {
  1137. return ImportFunctionDecl(context, loc_id, clang_function_decl);
  1138. }
  1139. if (auto* clang_namespace_decl =
  1140. clang::dyn_cast<clang::NamespaceDecl>(clang_decl)) {
  1141. return ImportNamespaceDecl(context, clang_namespace_decl);
  1142. }
  1143. if (auto* type_decl = clang::dyn_cast<clang::TypeDecl>(clang_decl)) {
  1144. auto type = type_decl->getASTContext().getTypeDeclType(type_decl);
  1145. auto type_inst_id = MapType(context, loc_id, type).inst_id;
  1146. if (!type_inst_id.has_value()) {
  1147. context.TODO(loc_id, llvm::formatv("Unsupported: Type declaration: {0}",
  1148. type.getAsString()));
  1149. return SemIR::ErrorInst::InstId;
  1150. }
  1151. return type_inst_id;
  1152. }
  1153. if (clang::isa<clang::FieldDecl, clang::IndirectFieldDecl>(clang_decl)) {
  1154. // Usable fields get imported as a side effect of importing the class.
  1155. if (SemIR::InstId existing_inst_id =
  1156. LookupClangDeclInstId(context, clang_decl);
  1157. existing_inst_id.has_value()) {
  1158. return existing_inst_id;
  1159. }
  1160. context.TODO(loc_id, "Unsupported: Unhandled kind of field declaration");
  1161. return SemIR::InstId::None;
  1162. }
  1163. context.TODO(loc_id, llvm::formatv("Unsupported: Declaration type {0}",
  1164. clang_decl->getDeclKindName())
  1165. .str());
  1166. return SemIR::InstId::None;
  1167. }
  1168. // Imports a declaration from Clang to Carbon. If successful, returns the
  1169. // instruction for the new Carbon declaration. All unimported dependencies would
  1170. // be imported first.
  1171. static auto ImportDeclAndDependencies(Context& context, SemIR::LocId loc_id,
  1172. clang::Decl* clang_decl)
  1173. -> SemIR::InstId {
  1174. // Collect dependencies.
  1175. llvm::SetVector<clang::Decl*> clang_decls;
  1176. clang_decls.insert(clang_decl);
  1177. for (size_t i = 0; i < clang_decls.size(); ++i) {
  1178. auto dependent_decls = GetDependentUnimportedDecls(context, clang_decls[i]);
  1179. for (clang::Decl* dependent_decl : dependent_decls) {
  1180. clang_decls.insert(dependent_decl);
  1181. }
  1182. }
  1183. // Import dependencies in reverse order.
  1184. auto inst_id = SemIR::InstId::None;
  1185. for (clang::Decl* clang_decl_to_import : llvm::reverse(clang_decls)) {
  1186. inst_id =
  1187. ImportDeclAfterDependencies(context, loc_id, clang_decl_to_import);
  1188. if (!inst_id.has_value()) {
  1189. break;
  1190. }
  1191. }
  1192. return inst_id;
  1193. }
  1194. // Maps `clang::AccessSpecifier` to `SemIR::AccessKind`.
  1195. static auto MapAccess(clang::AccessSpecifier access_specifier)
  1196. -> SemIR::AccessKind {
  1197. switch (access_specifier) {
  1198. case clang::AS_public:
  1199. case clang::AS_none:
  1200. return SemIR::AccessKind::Public;
  1201. case clang::AS_protected:
  1202. return SemIR::AccessKind::Protected;
  1203. case clang::AS_private:
  1204. return SemIR::AccessKind::Private;
  1205. }
  1206. }
  1207. // Imports a `clang::NamedDecl` into Carbon and adds that name into the
  1208. // `NameScope`.
  1209. static auto ImportNameDeclIntoScope(Context& context, SemIR::LocId loc_id,
  1210. SemIR::NameScopeId scope_id,
  1211. SemIR::NameId name_id,
  1212. clang::NamedDecl* clang_decl)
  1213. -> SemIR::ScopeLookupResult {
  1214. SemIR::InstId inst_id =
  1215. ImportDeclAndDependencies(context, loc_id, clang_decl);
  1216. if (!inst_id.has_value()) {
  1217. return SemIR::ScopeLookupResult::MakeNotFound();
  1218. }
  1219. SemIR::AccessKind access_kind = MapAccess(clang_decl->getAccess());
  1220. AddNameToScope(context, scope_id, name_id, access_kind, inst_id);
  1221. return SemIR::ScopeLookupResult::MakeWrappedLookupResult(inst_id,
  1222. access_kind);
  1223. }
  1224. auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
  1225. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  1226. -> SemIR::ScopeLookupResult {
  1227. Diagnostics::AnnotationScope annotate_diagnostics(
  1228. &context.emitter(), [&](auto& builder) {
  1229. CARBON_DIAGNOSTIC(InCppNameLookup, Note,
  1230. "in `Cpp` name lookup for `{0}`", SemIR::NameId);
  1231. builder.Note(loc_id, InCppNameLookup, name_id);
  1232. });
  1233. auto lookup = ClangLookup(context, scope_id, name_id);
  1234. if (!lookup) {
  1235. return SemIR::ScopeLookupResult::MakeNotFound();
  1236. }
  1237. if (!lookup->isSingleResult()) {
  1238. context.TODO(loc_id,
  1239. llvm::formatv("Unsupported: Lookup succeeded but couldn't "
  1240. "find a single result; LookupResultKind: {0}",
  1241. static_cast<int>(lookup->getResultKind()))
  1242. .str());
  1243. context.name_scopes().AddRequiredName(scope_id, name_id,
  1244. SemIR::ErrorInst::InstId);
  1245. return SemIR::ScopeLookupResult::MakeError();
  1246. }
  1247. return ImportNameDeclIntoScope(context, loc_id, scope_id, name_id,
  1248. lookup->getFoundDecl());
  1249. }
  1250. } // namespace Carbon::Check