import_cpp.cpp 55 KB

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