import_cpp.cpp 57 KB

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