import_cpp.cpp 60 KB

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