| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- #include "toolchain/check/import_cpp.h"
- #include <memory>
- #include <optional>
- #include <string>
- #include <tuple>
- #include <utility>
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/RecordLayout.h"
- #include "clang/Basic/FileManager.h"
- #include "clang/Frontend/ASTUnit.h"
- #include "clang/Frontend/CompilerInstance.h"
- #include "clang/Frontend/CompilerInvocation.h"
- #include "clang/Frontend/TextDiagnostic.h"
- #include "clang/Lex/PreprocessorOptions.h"
- #include "clang/Sema/Lookup.h"
- #include "common/check.h"
- #include "common/ostream.h"
- #include "common/raw_string_ostream.h"
- #include "llvm/ADT/IntrusiveRefCntPtr.h"
- #include "llvm/ADT/StringRef.h"
- #include "llvm/Support/raw_ostream.h"
- #include "toolchain/base/kind_switch.h"
- #include "toolchain/check/class.h"
- #include "toolchain/check/context.h"
- #include "toolchain/check/convert.h"
- #include "toolchain/check/diagnostic_helpers.h"
- #include "toolchain/check/eval.h"
- #include "toolchain/check/function.h"
- #include "toolchain/check/import.h"
- #include "toolchain/check/inst.h"
- #include "toolchain/check/literal.h"
- #include "toolchain/check/pattern.h"
- #include "toolchain/check/pattern_match.h"
- #include "toolchain/check/type.h"
- #include "toolchain/diagnostics/diagnostic.h"
- #include "toolchain/diagnostics/diagnostic_emitter.h"
- #include "toolchain/diagnostics/format_providers.h"
- #include "toolchain/parse/node_ids.h"
- #include "toolchain/sem_ir/clang_decl.h"
- #include "toolchain/sem_ir/ids.h"
- #include "toolchain/sem_ir/inst.h"
- #include "toolchain/sem_ir/name_scope.h"
- #include "toolchain/sem_ir/typed_insts.h"
- namespace Carbon::Check {
- // Generates C++ file contents to #include all requested imports.
- static auto GenerateCppIncludesHeaderCode(
- Context& context, llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
- -> std::string {
- std::string code;
- llvm::raw_string_ostream code_stream(code);
- for (const Parse::Tree::PackagingNames& import : imports) {
- // Add a line marker directive pointing at the location of the `import Cpp`
- // declaration in the Carbon source file. This will cause Clang's
- // diagnostics machinery to track and report the location in Carbon code
- // where the import was written.
- auto token = context.parse_tree().node_token(import.node_id);
- code_stream << "# " << context.tokens().GetLineNumber(token) << " \""
- << FormatEscaped(context.tokens().source().filename())
- << "\"\n";
- code_stream << "#include \""
- << FormatEscaped(
- context.string_literal_values().Get(import.library_id))
- << "\"\n";
- }
- return code;
- }
- // Adds the name to the scope with the given `inst_id`, if the `inst_id` is not
- // `None`.
- static auto AddNameToScope(Context& context, SemIR::NameScopeId scope_id,
- SemIR::NameId name_id, SemIR::InstId inst_id)
- -> void {
- if (inst_id.has_value()) {
- context.name_scopes().AddRequiredName(scope_id, name_id, inst_id);
- }
- }
- // Maps a Clang name to a Carbon `NameId`.
- static auto AddIdentifierName(Context& context, llvm::StringRef name)
- -> SemIR::NameId {
- return SemIR::NameId::ForIdentifier(context.identifiers().Add(name));
- }
- // Adds the given source location and an `ImportIRInst` referring to it in
- // `ImportIRId::Cpp`.
- static auto AddImportIRInst(Context& context,
- clang::SourceLocation clang_source_loc)
- -> SemIR::ImportIRInstId {
- SemIR::ClangSourceLocId clang_source_loc_id =
- context.sem_ir().clang_source_locs().Add(clang_source_loc);
- return context.import_ir_insts().Add(
- SemIR::ImportIRInst(clang_source_loc_id));
- }
- namespace {
- // Used to convert Clang diagnostics to Carbon diagnostics.
- class CarbonClangDiagnosticConsumer : public clang::DiagnosticConsumer {
- public:
- // Creates an instance with the location that triggers calling Clang.
- // `context` must not be null.
- explicit CarbonClangDiagnosticConsumer(
- Context* context, std::shared_ptr<clang::CompilerInvocation> invocation)
- : context_(context), invocation_(std::move(invocation)) {}
- // Generates a Carbon warning for each Clang warning and a Carbon error for
- // each Clang error or fatal.
- auto HandleDiagnostic(clang::DiagnosticsEngine::Level diag_level,
- const clang::Diagnostic& info) -> void override {
- DiagnosticConsumer::HandleDiagnostic(diag_level, info);
- SemIR::ImportIRInstId clang_import_ir_inst_id =
- AddImportIRInst(*context_, info.getLocation());
- llvm::SmallString<256> message;
- info.FormatDiagnostic(message);
- if (!info.hasSourceManager()) {
- // If we don't have a source manager, we haven't actually started
- // compiling yet, and this is an error from the driver or early in the
- // frontend. Pass it on directly.
- CARBON_CHECK(info.getLocation().isInvalid());
- diagnostic_infos_.push_back({.level = diag_level,
- .import_ir_inst_id = clang_import_ir_inst_id,
- .message = message.str().str()});
- return;
- }
- RawStringOstream diagnostics_stream;
- clang::TextDiagnostic text_diagnostic(diagnostics_stream,
- invocation_->getLangOpts(),
- invocation_->getDiagnosticOpts());
- text_diagnostic.emitDiagnostic(
- clang::FullSourceLoc(info.getLocation(), info.getSourceManager()),
- diag_level, message, info.getRanges(), info.getFixItHints());
- std::string diagnostics_str = diagnostics_stream.TakeStr();
- diagnostic_infos_.push_back({.level = diag_level,
- .import_ir_inst_id = clang_import_ir_inst_id,
- .message = diagnostics_str});
- }
- // Outputs Carbon diagnostics based on the collected Clang diagnostics. Must
- // be called after the AST is set in the context.
- auto EmitDiagnostics() -> void {
- for (const ClangDiagnosticInfo& info : diagnostic_infos_) {
- switch (info.level) {
- case clang::DiagnosticsEngine::Ignored:
- case clang::DiagnosticsEngine::Note:
- case clang::DiagnosticsEngine::Remark: {
- context_->TODO(
- SemIR::LocId(info.import_ir_inst_id),
- llvm::formatv(
- "Unsupported: C++ diagnostic level for diagnostic\n{0}",
- info.message));
- break;
- }
- case clang::DiagnosticsEngine::Warning:
- case clang::DiagnosticsEngine::Error:
- case clang::DiagnosticsEngine::Fatal: {
- CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning, "{0}",
- std::string);
- CARBON_DIAGNOSTIC(CppInteropParseError, Error, "{0}", std::string);
- context_->emitter().Emit(
- SemIR::LocId(info.import_ir_inst_id),
- info.level == clang::DiagnosticsEngine::Warning
- ? CppInteropParseWarning
- : CppInteropParseError,
- info.message);
- break;
- }
- }
- }
- }
- private:
- // The type-checking context in which we're running Clang.
- Context* context_;
- // The compiler invocation that is producing the diagnostics.
- std::shared_ptr<clang::CompilerInvocation> invocation_;
- // Information on a Clang diagnostic that can be converted to a Carbon
- // diagnostic.
- struct ClangDiagnosticInfo {
- // The Clang diagnostic level.
- clang::DiagnosticsEngine::Level level;
- // The ID of the ImportIR instruction referring to the Clang source
- // location.
- SemIR::ImportIRInstId import_ir_inst_id;
- // The Clang diagnostic textual message.
- std::string message;
- };
- // Collects the information for all Clang diagnostics to be converted to
- // Carbon diagnostics after the context has been initialized with the Clang
- // AST.
- llvm::SmallVector<ClangDiagnosticInfo> diagnostic_infos_;
- };
- } // namespace
- // Returns an AST for the C++ imports and a bool that represents whether
- // compilation errors where encountered or the generated AST is null due to an
- // error. Sets the AST in the context's `sem_ir`.
- // TODO: Consider to always have a (non-null) AST.
- static auto GenerateAst(Context& context,
- llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
- std::shared_ptr<clang::CompilerInvocation> invocation)
- -> std::pair<std::unique_ptr<clang::ASTUnit>, bool> {
- // Build a diagnostics engine.
- auto diagnostics_consumer =
- std::make_unique<CarbonClangDiagnosticConsumer>(&context, invocation);
- llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
- clang::CompilerInstance::createDiagnostics(
- *fs, invocation->getDiagnosticOpts(), diagnostics_consumer.get(),
- /*ShouldOwnClient=*/false));
- // Extract the input from the frontend invocation and make sure it makes
- // sense.
- const auto& inputs = invocation->getFrontendOpts().Inputs;
- CARBON_CHECK(inputs.size() == 1 &&
- inputs[0].getKind().getLanguage() == clang::Language::CXX &&
- inputs[0].getKind().getFormat() == clang::InputKind::Source);
- llvm::StringRef file_name = inputs[0].getFile();
- // Remap the imports file name to the corresponding `#include`s.
- // TODO: Modify the frontend options to specify this memory buffer as input
- // instead of remapping the file.
- std::string includes = GenerateCppIncludesHeaderCode(context, imports);
- auto includes_buffer = llvm::MemoryBuffer::getMemBuffer(includes, file_name);
- invocation->getPreprocessorOpts().addRemappedFile(file_name,
- includes_buffer.get());
- // Create the AST unit.
- auto ast = clang::ASTUnit::LoadFromCompilerInvocation(
- invocation, std::make_shared<clang::PCHContainerOperations>(), nullptr,
- diags, new clang::FileManager(invocation->getFileSystemOpts(), fs));
- // Remove remapped file before its underlying storage is destroyed.
- invocation->getPreprocessorOpts().clearRemappedFiles();
- // Attach the AST to SemIR. This needs to be done before we can emit any
- // diagnostics, so their locations can be properly interpreted by our
- // diagnostics machinery.
- context.sem_ir().set_cpp_ast(ast.get());
- // Emit any diagnostics we queued up while building the AST.
- diagnostics_consumer->EmitDiagnostics();
- bool any_errors = diagnostics_consumer->getNumErrors() > 0;
- // Transfer ownership of the consumer to the AST unit, in case more
- // diagnostics are produced by AST queries.
- ast->getDiagnostics().setClient(diagnostics_consumer.release(),
- /*ShouldOwnClient=*/true);
- return {std::move(ast), !ast || any_errors};
- }
- // Adds a namespace for the `Cpp` import and returns its `NameScopeId`.
- static auto AddNamespace(Context& context, PackageNameId cpp_package_id,
- llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
- -> SemIR::NameScopeId {
- auto& import_cpps = context.sem_ir().import_cpps();
- import_cpps.Reserve(imports.size());
- for (const Parse::Tree::PackagingNames& import : imports) {
- import_cpps.Add({.node_id = context.parse_tree().As<Parse::ImportDeclId>(
- import.node_id),
- .library_id = import.library_id});
- }
- return AddImportNamespaceToScope(
- context,
- GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
- SemIR::NameId::ForPackageName(cpp_package_id),
- SemIR::NameScopeId::Package,
- /*diagnose_duplicate_namespace=*/false,
- [&]() {
- return AddInst<SemIR::ImportCppDecl>(
- context,
- context.parse_tree().As<Parse::ImportDeclId>(
- imports.front().node_id),
- {});
- })
- .add_result.name_scope_id;
- }
- auto ImportCppFiles(Context& context,
- llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
- llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
- std::shared_ptr<clang::CompilerInvocation> invocation)
- -> std::unique_ptr<clang::ASTUnit> {
- if (imports.empty()) {
- return nullptr;
- }
- CARBON_CHECK(!context.sem_ir().cpp_ast());
- PackageNameId package_id = imports.front().package_id;
- CARBON_CHECK(
- llvm::all_of(imports, [&](const Parse::Tree::PackagingNames& import) {
- return import.package_id == package_id;
- }));
- auto name_scope_id = AddNamespace(context, package_id, imports);
- auto [generated_ast, ast_has_error] =
- GenerateAst(context, imports, fs, std::move(invocation));
- SemIR::NameScope& name_scope = context.name_scopes().Get(name_scope_id);
- name_scope.set_is_closed_import(true);
- name_scope.set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
- {.decl = generated_ast->getASTContext().getTranslationUnitDecl(),
- .inst_id = name_scope.inst_id()}));
- if (ast_has_error) {
- name_scope.set_has_error();
- }
- return std::move(generated_ast);
- }
- // Look ups the given name in the Clang AST in a specific scope. Returns the
- // lookup result if lookup was successful.
- static auto ClangLookup(Context& context, SemIR::NameScopeId scope_id,
- SemIR::NameId name_id)
- -> std::optional<clang::LookupResult> {
- std::optional<llvm::StringRef> name =
- context.names().GetAsStringIfIdentifier(name_id);
- if (!name) {
- // Special names never exist in C++ code.
- return std::nullopt;
- }
- clang::ASTUnit* ast = context.sem_ir().cpp_ast();
- CARBON_CHECK(ast);
- clang::Sema& sema = ast->getSema();
- clang::LookupResult lookup(
- sema,
- clang::DeclarationNameInfo(
- clang::DeclarationName(
- sema.getPreprocessor().getIdentifierInfo(*name)),
- clang::SourceLocation()),
- clang::Sema::LookupNameKind::LookupOrdinaryName);
- // TODO: Diagnose on access and return the `AccessKind` for storage. We'll
- // probably need a dedicated `DiagnosticConsumer` because
- // `TextDiagnosticPrinter` assumes we're processing a C++ source file.
- lookup.suppressDiagnostics();
- auto scope_clang_decl_context_id =
- context.name_scopes().Get(scope_id).clang_decl_context_id();
- bool found = sema.LookupQualifiedName(
- lookup,
- clang::dyn_cast<clang::DeclContext>(context.sem_ir()
- .clang_decls()
- .Get(scope_clang_decl_context_id)
- .decl));
- if (!found) {
- return std::nullopt;
- }
- return lookup;
- }
- // Returns whether `decl` already mapped to an instruction.
- static auto IsClangDeclImported(const Context& context, clang::Decl* decl)
- -> bool {
- return context.sem_ir()
- .clang_decls()
- .Lookup(decl->getCanonicalDecl())
- .has_value();
- }
- // If `decl` already mapped to an instruction, returns that instruction.
- // Otherwise returns `None`.
- static auto LookupClangDeclInstId(const Context& context, clang::Decl* decl)
- -> SemIR::InstId {
- const auto& clang_decls = context.sem_ir().clang_decls();
- if (auto context_clang_decl_id = clang_decls.Lookup(decl->getCanonicalDecl());
- context_clang_decl_id.has_value()) {
- return clang_decls.Get(context_clang_decl_id).inst_id;
- }
- return SemIR::InstId::None;
- }
- // Returns the parent of the given declaration. Skips declaration types we
- // ignore.
- static auto GetParentDecl(clang::Decl* clang_decl) -> clang::Decl* {
- clang::DeclContext* decl_context = clang_decl->getDeclContext();
- while (llvm::isa<clang::LinkageSpecDecl>(decl_context)) {
- decl_context = decl_context->getParent();
- }
- return llvm::cast<clang::Decl>(decl_context);
- }
- // Returns the given declaration's parent scope. Assumes the parent declaration
- // was already imported.
- static auto GetParentNameScopeId(Context& context, clang::Decl* clang_decl)
- -> SemIR::NameScopeId {
- SemIR::InstId parent_inst_id =
- LookupClangDeclInstId(context, GetParentDecl(clang_decl));
- CARBON_CHECK(parent_inst_id.has_value());
- CARBON_KIND_SWITCH(context.insts().Get(parent_inst_id)) {
- case CARBON_KIND(SemIR::ClassDecl class_decl): {
- return context.classes().Get(class_decl.class_id).scope_id;
- }
- case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
- return context.interfaces().Get(interface_decl.interface_id).scope_id;
- }
- case CARBON_KIND(SemIR::Namespace namespace_inst): {
- return namespace_inst.name_scope_id;
- }
- default: {
- CARBON_FATAL("Unexpected parent instruction kind");
- }
- }
- }
- // Imports a namespace declaration from Clang to Carbon. If successful, returns
- // the new Carbon namespace declaration `InstId`. If the declaration was already
- // imported, returns the mapped instruction.
- static auto ImportNamespaceDecl(Context& context,
- clang::NamespaceDecl* clang_decl)
- -> SemIR::InstId {
- // Check if the declaration is already mapped.
- if (SemIR::InstId existing_inst_id =
- LookupClangDeclInstId(context, clang_decl);
- existing_inst_id.has_value()) {
- return existing_inst_id;
- }
- auto result = AddImportNamespace(
- context, GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
- AddIdentifierName(context, clang_decl->getName()),
- GetParentNameScopeId(context, clang_decl),
- /*import_id=*/SemIR::InstId::None);
- context.name_scopes()
- .Get(result.name_scope_id)
- .set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
- {.decl = clang_decl->getCanonicalDecl(), .inst_id = result.inst_id}));
- return result.inst_id;
- }
- static auto MapType(Context& context, SemIR::LocId loc_id, clang::QualType type)
- -> TypeExpr;
- // Creates a class declaration for the given class name in the given scope.
- // Returns the `InstId` for the declaration.
- static auto BuildClassDecl(Context& context,
- SemIR::ImportIRInstId import_ir_inst_id,
- SemIR::NameScopeId parent_scope_id,
- SemIR::NameId name_id)
- -> std::tuple<SemIR::ClassId, SemIR::TypeInstId> {
- // Add the class declaration.
- auto class_decl = SemIR::ClassDecl{.type_id = SemIR::TypeType::TypeId,
- .class_id = SemIR::ClassId::None,
- .decl_block_id = SemIR::InstBlockId::None};
- auto class_decl_id = AddPlaceholderInstInNoBlock(
- context,
- SemIR::LocIdAndInst::UncheckedLoc(import_ir_inst_id, class_decl));
- context.imports().push_back(class_decl_id);
- SemIR::Class class_info = {
- {.name_id = name_id,
- .parent_scope_id = parent_scope_id,
- .generic_id = SemIR::GenericId::None,
- .first_param_node_id = Parse::NodeId::None,
- .last_param_node_id = Parse::NodeId::None,
- .pattern_block_id = SemIR::InstBlockId::None,
- .implicit_param_patterns_id = SemIR::InstBlockId::None,
- .param_patterns_id = SemIR::InstBlockId::None,
- .is_extern = false,
- .extern_library_id = SemIR::LibraryNameId::None,
- .non_owning_decl_id = SemIR::InstId::None,
- .first_owning_decl_id = class_decl_id},
- {// `.self_type_id` depends on the ClassType, so is set below.
- .self_type_id = SemIR::TypeId::None,
- // TODO: Support Dynamic classes.
- // TODO: Support Final classes.
- .inheritance_kind = SemIR::Class::Base}};
- class_decl.class_id = context.classes().Add(class_info);
- // Write the class ID into the ClassDecl.
- ReplaceInstBeforeConstantUse(context, class_decl_id, class_decl);
- SetClassSelfType(context, class_decl.class_id);
- return {class_decl.class_id, context.types().GetAsTypeInstId(class_decl_id)};
- }
- // Checks that the specified finished class definition is valid and builds and
- // returns a corresponding complete type witness instruction.
- // TODO: Remove recursion into mapping field types.
- // NOLINTNEXTLINE(misc-no-recursion)
- static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
- SemIR::ImportIRInstId import_ir_inst_id,
- SemIR::TypeInstId class_type_inst_id,
- const clang::CXXRecordDecl* clang_def)
- -> SemIR::TypeInstId {
- // For now, if the class is empty, produce an empty struct as the object
- // representation. This allows our tests to continue to pass while we don't
- // properly support initializing imported C++ classes.
- // TODO: Remove this.
- if (clang_def->isEmpty() && !clang_def->getNumBases()) {
- return context.types().GetAsTypeInstId(AddInst(
- context,
- MakeImportedLocIdAndInst(
- context, import_ir_inst_id,
- SemIR::StructType{.type_id = SemIR::TypeType::TypeId,
- .fields_id = SemIR::StructTypeFieldsId::Empty})));
- }
- const auto& clang_layout =
- context.ast_context().getASTRecordLayout(clang_def);
- llvm::SmallVector<uint64_t> layout;
- llvm::SmallVector<SemIR::StructTypeField> fields;
- static_assert(SemIR::CustomLayoutId::SizeIndex == 0);
- layout.push_back(clang_layout.getSize().getQuantity());
- static_assert(SemIR::CustomLayoutId::AlignIndex == 1);
- layout.push_back(clang_layout.getAlignment().getQuantity());
- static_assert(SemIR::CustomLayoutId::FirstFieldIndex == 2);
- // TODO: Import vptr(s).
- // Import bases.
- for (const auto& base : clang_def->bases()) {
- if (base.isVirtual()) {
- // TODO: Handle virtual bases. We don't actually know where they go in the
- // layout. We may also want to use a different size in the layout for
- // `partial C`, excluding the virtual base. It's also not entirely safe to
- // just skip over the virtual base, as the type we would construct would
- // have a misleading size.
- context.TODO(import_ir_inst_id, "class with virtual bases");
- return SemIR::ErrorInst::TypeInstId;
- }
- auto [base_type_inst_id, base_type_id] =
- MapType(context, import_ir_inst_id, base.getType());
- if (!base_type_id.has_value()) {
- // TODO: If the base class's type can't be mapped, skip it.
- continue;
- }
- auto base_decl_id = AddInst(
- context,
- MakeImportedLocIdAndInst(
- context, import_ir_inst_id,
- SemIR::BaseDecl{.type_id = GetUnboundElementType(
- context, class_type_inst_id, base_type_inst_id),
- .base_type_inst_id = base_type_inst_id,
- .index = SemIR::ElementIndex(fields.size())}));
- // If there's exactly one base class, treat it as a Carbon base class too.
- // TODO: Improve handling for the case where the class has multiple base
- // classes.
- if (clang_def->getNumBases() == 1) {
- auto& class_info = context.classes().Get(class_id);
- CARBON_CHECK(!class_info.base_id.has_value());
- class_info.base_id = base_decl_id;
- }
- auto* base_class = base.getType()->getAsCXXRecordDecl();
- CARBON_CHECK(base_class, "Base class {0} is not a class",
- base.getType().getAsString());
- auto base_offset = base.isVirtual()
- ? clang_layout.getVBaseClassOffset(base_class)
- : clang_layout.getBaseClassOffset(base_class);
- layout.push_back(base_offset.getQuantity());
- fields.push_back(
- {.name_id = SemIR::NameId::Base, .type_inst_id = base_type_inst_id});
- }
- // Import fields.
- for (auto* decl : clang_def->decls()) {
- auto* field = clang::dyn_cast<clang::FieldDecl>(decl);
- // Track the chain of fields from the class to this field. This chain is
- // only one element long unless the field is a member of an anonymous struct
- // or union.
- clang::NamedDecl* single_field_chain[1] = {field};
- llvm::ArrayRef<clang::NamedDecl*> chain = single_field_chain;
- // If this isn't a field, it might be an indirect field in an anonymous
- // struct or union.
- if (!field) {
- auto* indirect_field = clang::dyn_cast<clang::IndirectFieldDecl>(decl);
- if (!indirect_field) {
- continue;
- }
- chain = indirect_field->chain();
- field = indirect_field->getAnonField();
- }
- if (field->isBitField()) {
- // TODO: Add a representation for named bitfield members.
- continue;
- }
- if (field->isAnonymousStructOrUnion()) {
- // Fields within an anonymous structure or union will be added via their
- // IndirectFieldDecls.
- continue;
- }
- auto field_name_id = AddIdentifierName(context, field->getName());
- auto [field_type_inst_id, field_type_id] =
- MapType(context, import_ir_inst_id, field->getType());
- if (!field_type_inst_id.has_value()) {
- // TODO: For now, just skip over fields whose types we can't map.
- continue;
- }
- // Create a field now, as we know the index to use.
- // TODO: Consider doing this lazily instead.
- auto field_decl_id = AddInst(
- context, MakeImportedLocIdAndInst(
- context, import_ir_inst_id,
- SemIR::FieldDecl{
- .type_id = GetUnboundElementType(
- context, class_type_inst_id, field_type_inst_id),
- .name_id = field_name_id,
- .index = SemIR::ElementIndex(fields.size())}));
- context.sem_ir().clang_decls().Add(
- {.decl = decl->getCanonicalDecl(), .inst_id = field_decl_id});
- // Compute the offset to the field that appears directly in the class.
- uint64_t offset = clang_layout.getFieldOffset(
- clang::cast<clang::FieldDecl>(chain.front())->getFieldIndex());
- // If this is an indirect field, walk the path and accumulate the offset to
- // the named field.
- for (auto* inner_decl : chain.drop_front()) {
- auto* inner_field = clang::cast<clang::FieldDecl>(inner_decl);
- const auto& inner_layout =
- context.ast_context().getASTRecordLayout(inner_field->getParent());
- offset += inner_layout.getFieldOffset(inner_field->getFieldIndex());
- }
- layout.push_back(
- context.ast_context().toCharUnitsFromBits(offset).getQuantity());
- fields.push_back(
- {.name_id = field_name_id, .type_inst_id = field_type_inst_id});
- }
- // TODO: Add a field to prevent tail padding reuse if necessary.
- return AddTypeInst<SemIR::CustomLayoutType>(
- context, import_ir_inst_id,
- {.type_id = SemIR::TypeType::TypeId,
- .fields_id = context.struct_type_fields().Add(fields),
- .layout_id = context.custom_layouts().Add(layout)});
- }
- // Creates a class definition based on the information in the given Clang
- // declaration, which is assumed to be for a class definition.
- // TODO: Remove recursion into mapping field types.
- // NOLINTNEXTLINE(misc-no-recursion)
- static auto BuildClassDefinition(Context& context,
- SemIR::ImportIRInstId import_ir_inst_id,
- SemIR::ClassId class_id,
- SemIR::TypeInstId class_inst_id,
- SemIR::ClangDeclId clang_decl_id,
- clang::CXXRecordDecl* clang_def) -> void {
- auto& class_info = context.classes().Get(class_id);
- StartClassDefinition(context, class_info, class_inst_id);
- // Name lookup into the Carbon class looks in the C++ class definition.
- context.name_scopes()
- .Get(class_info.scope_id)
- .set_clang_decl_context_id(clang_decl_id);
- context.inst_block_stack().Push();
- // Compute the class's object representation.
- auto object_repr_id = ImportClassObjectRepr(
- context, class_id, import_ir_inst_id, class_inst_id, clang_def);
- class_info.complete_type_witness_id = AddInst<SemIR::CompleteTypeWitness>(
- context, import_ir_inst_id,
- {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
- .object_repr_type_inst_id = object_repr_id});
- class_info.body_block_id = context.inst_block_stack().Pop();
- }
- // Mark the given `Decl` as failed in `clang_decls`.
- static auto MarkFailedDecl(Context& context, clang::Decl* clang_decl) {
- context.sem_ir().clang_decls().Add({.decl = clang_decl->getCanonicalDecl(),
- .inst_id = SemIR::ErrorInst::InstId});
- }
- // Imports a record declaration from Clang to Carbon. If successful, returns
- // the new Carbon class declaration `InstId`.
- // TODO: Change `clang_decl` to `const &` when lookup is using `clang::DeclID`
- // and we don't need to store the decl for lookup context.
- // TODO: Remove recursion into mapping field types.
- // NOLINTNEXTLINE(misc-no-recursion)
- static auto ImportCXXRecordDecl(Context& context,
- clang::CXXRecordDecl* clang_decl)
- -> SemIR::InstId {
- clang::CXXRecordDecl* clang_def = clang_decl->getDefinition();
- if (clang_def) {
- clang_decl = clang_def;
- }
- auto import_ir_inst_id = AddImportIRInst(context, clang_decl->getLocation());
- auto [class_id, class_inst_id] = BuildClassDecl(
- context, import_ir_inst_id, GetParentNameScopeId(context, clang_decl),
- AddIdentifierName(context, clang_decl->getName()));
- // TODO: The caller does the same lookup. Avoid doing it twice.
- auto clang_decl_id = context.sem_ir().clang_decls().Add(
- {.decl = clang_decl->getCanonicalDecl(), .inst_id = class_inst_id});
- if (clang_def) {
- BuildClassDefinition(context, import_ir_inst_id, class_id, class_inst_id,
- clang_decl_id, clang_def);
- }
- return class_inst_id;
- }
- // Creates an integer type of the given size.
- static auto MakeIntType(Context& context, IntId size_id, bool is_signed)
- -> TypeExpr {
- auto type_inst_id = MakeIntTypeLiteral(
- context, Parse::NodeId::None,
- is_signed ? SemIR::IntKind::Signed : SemIR::IntKind::Unsigned, size_id);
- return ExprAsType(context, Parse::NodeId::None, type_inst_id);
- }
- // Maps a C++ builtin type to a Carbon type.
- // TODO: Support more builtin types.
- static auto MapBuiltinType(Context& context, clang::QualType qual_type,
- const clang::BuiltinType& type) -> TypeExpr {
- if (type.isInteger()) {
- auto width = context.ast_context().getIntWidth(qual_type);
- bool is_signed = type.isSignedInteger();
- auto int_n_type =
- context.ast_context().getIntTypeForBitwidth(width, is_signed);
- if (context.ast_context().hasSameType(qual_type, int_n_type)) {
- return MakeIntType(context, context.ints().Add(width), is_signed);
- }
- // TODO: Handle integer types that map to named aliases.
- }
- return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
- }
- // Maps a C++ record type to a Carbon type.
- // TODO: Support more record types.
- // TODO: Remove recursion mapping fields of class types.
- // NOLINTNEXTLINE(misc-no-recursion)
- static auto MapRecordType(Context& context, const clang::RecordType& type)
- -> TypeExpr {
- auto* record_decl = clang::dyn_cast<clang::CXXRecordDecl>(type.getDecl());
- if (!record_decl) {
- return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
- }
- // Check if the declaration is already mapped.
- SemIR::InstId record_inst_id = LookupClangDeclInstId(context, record_decl);
- if (!record_inst_id.has_value()) {
- record_inst_id = ImportCXXRecordDecl(context, record_decl);
- }
- SemIR::TypeInstId record_type_inst_id =
- context.types().GetAsTypeInstId(record_inst_id);
- return {
- .inst_id = record_type_inst_id,
- .type_id = context.types().GetTypeIdForTypeInstId(record_type_inst_id)};
- }
- // Maps a C++ type that is not a wrapper type such as a pointer to a Carbon
- // type.
- // TODO: Support more types.
- // TODO: Remove recursion mapping fields of class types.
- // NOLINTNEXTLINE(misc-no-recursion)
- static auto MapNonWrapperType(Context& context, clang::QualType type)
- -> TypeExpr {
- if (const auto* builtin_type = type->getAs<clang::BuiltinType>()) {
- return MapBuiltinType(context, type, *builtin_type);
- }
- if (const auto* record_type = type->getAs<clang::RecordType>()) {
- return MapRecordType(context, *record_type);
- }
- CARBON_CHECK(!type.hasQualifiers() && !type->isPointerType(),
- "Should not see wrapper types here");
- return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};
- }
- // Maps a qualified C++ type to a Carbon type.
- static auto MapQualifiedType(Context& context, SemIR::LocId loc_id,
- clang::QualType type, TypeExpr type_expr)
- -> TypeExpr {
- auto quals = type.getQualifiers();
- if (quals.hasConst()) {
- auto type_id = GetConstType(context, type_expr.inst_id);
- type_expr = {.inst_id = context.types().GetInstId(type_id),
- .type_id = type_id};
- quals.removeConst();
- }
- // TODO: Support other qualifiers.
- if (!quals.empty()) {
- context.TODO(loc_id, llvm::formatv("Unsupported: qualified type: {0}",
- type.getAsString()));
- return {.inst_id = SemIR::ErrorInst::TypeInstId,
- .type_id = SemIR::ErrorInst::TypeId};
- }
- return type_expr;
- }
- // Maps a C++ pointer type to a Carbon pointer type.
- static auto MapPointerType(Context& context, SemIR::LocId loc_id,
- clang::QualType type, TypeExpr pointee_type_expr)
- -> TypeExpr {
- CARBON_CHECK(type->isPointerType());
- if (auto nullability = type->getNullability();
- !nullability.has_value() ||
- *nullability != clang::NullabilityKind::NonNull) {
- context.TODO(loc_id, llvm::formatv("Unsupported: nullable pointer: {0}",
- type.getAsString()));
- return {.inst_id = SemIR::ErrorInst::TypeInstId,
- .type_id = SemIR::ErrorInst::TypeId};
- }
- SemIR::TypeId pointer_type_id =
- GetPointerType(context, pointee_type_expr.inst_id);
- return {.inst_id = context.types().GetInstId(pointer_type_id),
- .type_id = pointer_type_id};
- }
- // Maps a C++ type to a Carbon type. `type` should not be canonicalized because
- // we check for pointer nullability and nullability will be lost by
- // canonicalization.
- // TODO: Remove recursion mapping fields of class types.
- // NOLINTNEXTLINE(misc-no-recursion)
- static auto MapType(Context& context, SemIR::LocId loc_id, clang::QualType type)
- -> TypeExpr {
- // Unwrap any type modifiers and wrappers.
- llvm::SmallVector<clang::QualType> wrapper_types;
- while (true) {
- clang::QualType orig_type = type;
- if (type.hasQualifiers()) {
- type = type.getUnqualifiedType();
- } else if (type->isPointerType()) {
- type = type->getPointeeType();
- } else {
- break;
- }
- wrapper_types.push_back(orig_type);
- }
- auto mapped = MapNonWrapperType(context, type);
- for (auto wrapper : llvm::reverse(wrapper_types)) {
- if (!mapped.inst_id.has_value() ||
- mapped.type_id == SemIR::ErrorInst::TypeId) {
- break;
- }
- if (wrapper.hasQualifiers()) {
- mapped = MapQualifiedType(context, loc_id, wrapper, mapped);
- } else if (wrapper->isPointerType()) {
- mapped = MapPointerType(context, loc_id, wrapper, mapped);
- } else {
- CARBON_FATAL("Unexpected wrapper type {0}", wrapper.getAsString());
- }
- }
- return mapped;
- }
- // Returns a block for the implicit parameters of the given function
- // declaration. Because function templates are not yet supported, this currently
- // only contains the `self` parameter. On error, produces a diagnostic and
- // returns None.
- static auto MakeImplicitParamPatternsBlockId(
- Context& context, SemIR::LocId loc_id,
- const clang::FunctionDecl& clang_decl) -> SemIR::InstBlockId {
- const auto* method_decl = dyn_cast<clang::CXXMethodDecl>(&clang_decl);
- if (!method_decl || method_decl->isStatic()) {
- return SemIR::InstBlockId::Empty;
- }
- // Build a `self` parameter from the object parameter.
- BeginSubpattern(context);
- // Perform some special-case mapping for the object parameter:
- //
- // - If it's a const reference to T, produce a by-value `self: T` parameter.
- // - If it's a non-const reference to T, produce an `addr self: T*`
- // parameter.
- // - Otherwise, map it directly, which will currently fail for `&&`-qualified
- // methods.
- //
- // TODO: Some of this mapping should be performed for all parameters.
- clang::QualType param_type =
- method_decl->getFunctionObjectParameterReferenceType();
- bool addr_self = false;
- if (param_type->isLValueReferenceType()) {
- param_type = param_type.getNonReferenceType();
- if (param_type.isConstQualified()) {
- // TODO: Consider only doing this if `const` is the only qualifier. For
- // now, any other qualifier will fail when mapping the type.
- auto split_type = param_type.getSplitUnqualifiedType();
- split_type.Quals.removeConst();
- param_type = method_decl->getASTContext().getQualifiedType(split_type);
- } else {
- addr_self = true;
- }
- }
- auto [type_inst_id, type_id] = MapType(context, loc_id, param_type);
- SemIR::ExprRegionId type_expr_region_id =
- EndSubpatternAsExpr(context, type_inst_id);
- if (!type_id.has_value()) {
- context.TODO(loc_id,
- llvm::formatv("Unsupported: object parameter type: {0}",
- param_type.getAsString()));
- return SemIR::InstBlockId::None;
- }
- if (addr_self) {
- type_id = GetPointerType(context, type_inst_id);
- }
- SemIR::InstId pattern_id =
- // TODO: Fill in a location once available.
- AddBindingPattern(context, SemIR::LocId::None, SemIR::NameId::SelfValue,
- type_id, type_expr_region_id, /*is_generic*/ false,
- /*is_template*/ false)
- .pattern_id;
- // TODO: Fill in a location once available.
- pattern_id = AddPatternInst<SemIR::ValueParamPattern>(
- context, SemIR::LocId::None,
- {.type_id = context.insts().Get(pattern_id).type_id(),
- .subpattern_id = pattern_id,
- .index = SemIR::CallParamIndex::None});
- // If we're building `addr self: Self*`, do that now.
- if (addr_self) {
- // TODO: Fill in a location once available.
- pattern_id = AddPatternInst<SemIR::AddrPattern>(
- context, SemIR::LocId::None,
- {.type_id = GetPatternType(context, SemIR::AutoType::TypeId),
- .inner_id = pattern_id});
- }
- return context.inst_blocks().Add({pattern_id});
- }
- // Returns a block id for the explicit parameters of the given function
- // declaration. If the function declaration has no parameters, it returns
- // `SemIR::InstBlockId::Empty`. In the case of an unsupported parameter type, it
- // produces an error and returns `SemIR::InstBlockId::None`.
- // TODO: Consider refactoring to extract and reuse more logic from
- // `HandleAnyBindingPattern()`.
- static auto MakeParamPatternsBlockId(Context& context, SemIR::LocId loc_id,
- const clang::FunctionDecl& clang_decl)
- -> SemIR::InstBlockId {
- if (clang_decl.parameters().empty()) {
- return SemIR::InstBlockId::Empty;
- }
- llvm::SmallVector<SemIR::InstId> params;
- params.reserve(clang_decl.parameters().size());
- for (const clang::ParmVarDecl* param : clang_decl.parameters()) {
- // TODO: Get the parameter type from the function, not from the
- // `ParmVarDecl`. The type of the `ParmVarDecl` is the type within the
- // function, and isn't in general the same as the type that's exposed to
- // callers. In particular, the parameter type exposed to callers will never
- // be cv-qualified.
- clang::QualType param_type = param->getType();
- // Mark the start of a region of insts, needed for the type expression
- // created later with the call of `EndSubpatternAsExpr()`.
- BeginSubpattern(context);
- auto [type_inst_id, type_id] = MapType(context, loc_id, param_type);
- // Type expression of the binding pattern - a single-entry/single-exit
- // region that allows control flow in the type expression e.g. fn F(x: if C
- // then i32 else i64).
- SemIR::ExprRegionId type_expr_region_id =
- EndSubpatternAsExpr(context, type_inst_id);
- if (!type_id.has_value()) {
- context.TODO(loc_id, llvm::formatv("Unsupported: parameter type: {0}",
- param_type.getAsString()));
- return SemIR::InstBlockId::None;
- }
- llvm::StringRef param_name = param->getName();
- SemIR::NameId name_id =
- param_name.empty()
- // Translate an unnamed parameter to an underscore to
- // match Carbon's naming of unnamed/unused function params.
- ? SemIR::NameId::Underscore
- : AddIdentifierName(context, param_name);
- // TODO: Fix this once templates are supported.
- bool is_template = false;
- // TODO: Fix this once generics are supported.
- bool is_generic = false;
- SemIR::InstId binding_pattern_id =
- // TODO: Fill in a location once available.
- AddBindingPattern(context, SemIR::LocId::None, name_id, type_id,
- type_expr_region_id, is_generic, is_template)
- .pattern_id;
- SemIR::InstId var_pattern_id = AddPatternInst(
- context,
- // TODO: Fill in a location once available.
- SemIR::LocIdAndInst::NoLoc(SemIR::ValueParamPattern(
- {.type_id = context.insts().Get(binding_pattern_id).type_id(),
- .subpattern_id = binding_pattern_id,
- .index = SemIR::CallParamIndex::None})));
- params.push_back(var_pattern_id);
- }
- return context.inst_blocks().Add(params);
- }
- // Returns the return type of the given function declaration. In case of an
- // unsupported return type, it produces a diagnostic and returns
- // `SemIR::ErrorInst::InstId`.
- // TODO: Support more return types.
- static auto GetReturnType(Context& context, SemIR::LocId loc_id,
- const clang::FunctionDecl* clang_decl)
- -> SemIR::InstId {
- clang::QualType ret_type = clang_decl->getReturnType();
- if (ret_type->isVoidType()) {
- return SemIR::InstId::None;
- }
- auto [type_inst_id, type_id] = MapType(context, loc_id, ret_type);
- if (!type_inst_id.has_value()) {
- context.TODO(loc_id, llvm::formatv("Unsupported: return type: {0}",
- ret_type.getAsString()));
- return SemIR::ErrorInst::InstId;
- }
- auto pattern_type_id = GetPatternType(context, type_id);
- SemIR::InstId return_slot_pattern_id = AddPatternInst(
- // TODO: Fill in a location for the return type once available.
- context,
- SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
- {.type_id = pattern_type_id, .type_inst_id = type_inst_id})));
- SemIR::InstId param_pattern_id = AddPatternInst(
- // TODO: Fill in a location for the return type once available.
- context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
- {.type_id = pattern_type_id,
- .subpattern_id = return_slot_pattern_id,
- .index = SemIR::CallParamIndex::None})));
- return param_pattern_id;
- }
- namespace {
- // Represents the parameter patterns block id, the return slot pattern id and
- // the call parameters block id for a function declaration.
- struct FunctionParamsInsts {
- SemIR::InstBlockId implicit_param_patterns_id;
- SemIR::InstBlockId param_patterns_id;
- SemIR::InstId return_slot_pattern_id;
- SemIR::InstBlockId call_params_id;
- };
- } // namespace
- // Creates a block containing the parameter pattern instructions for the
- // explicit parameters, a parameter pattern instruction for the return type and
- // a block containing the call parameters of the function. Emits a callee
- // pattern-match for the explicit parameter patterns and the return slot pattern
- // to create the Call parameters instructions block. Currently the implicit
- // parameter patterns are not taken into account. Returns the parameter patterns
- // block id, the return slot pattern id, and the call parameters block id.
- // Produces a diagnostic and returns `std::nullopt` if the function declaration
- // has an unsupported parameter type.
- static auto CreateFunctionParamsInsts(Context& context, SemIR::LocId loc_id,
- const clang::FunctionDecl* clang_decl)
- -> std::optional<FunctionParamsInsts> {
- if (isa<clang::CXXConstructorDecl, clang::CXXDestructorDecl>(clang_decl)) {
- context.TODO(loc_id, "Unsupported: Constructor/Destructor");
- return std::nullopt;
- }
- auto implicit_param_patterns_id =
- MakeImplicitParamPatternsBlockId(context, loc_id, *clang_decl);
- if (!implicit_param_patterns_id.has_value()) {
- return std::nullopt;
- }
- auto param_patterns_id =
- MakeParamPatternsBlockId(context, loc_id, *clang_decl);
- if (!param_patterns_id.has_value()) {
- return std::nullopt;
- }
- auto return_slot_pattern_id = GetReturnType(context, loc_id, clang_decl);
- if (SemIR::ErrorInst::InstId == return_slot_pattern_id) {
- return std::nullopt;
- }
- auto call_params_id =
- CalleePatternMatch(context, implicit_param_patterns_id, param_patterns_id,
- return_slot_pattern_id);
- return {{.implicit_param_patterns_id = implicit_param_patterns_id,
- .param_patterns_id = param_patterns_id,
- .return_slot_pattern_id = return_slot_pattern_id,
- .call_params_id = call_params_id}};
- }
- // Imports a function declaration from Clang to Carbon. If successful, returns
- // the new Carbon function declaration `InstId`. If the declaration was already
- // imported, returns the mapped instruction.
- static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
- clang::FunctionDecl* clang_decl)
- -> SemIR::InstId {
- // Check if the declaration is already mapped.
- if (SemIR::InstId existing_inst_id =
- LookupClangDeclInstId(context, clang_decl);
- existing_inst_id.has_value()) {
- return existing_inst_id;
- }
- if (clang_decl->isVariadic()) {
- context.TODO(loc_id, "Unsupported: Variadic function");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- if (clang_decl->getTemplatedKind() ==
- clang::FunctionDecl::TK_FunctionTemplate) {
- context.TODO(loc_id, "Unsupported: Template function");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- if (auto* method_decl = dyn_cast<clang::CXXMethodDecl>(clang_decl)) {
- if (method_decl->isVirtual()) {
- context.TODO(loc_id, "Unsupported: Virtual function");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- }
- context.scope_stack().PushForDeclName();
- context.inst_block_stack().Push();
- context.pattern_block_stack().Push();
- auto function_params_insts =
- CreateFunctionParamsInsts(context, loc_id, clang_decl);
- auto pattern_block_id = context.pattern_block_stack().Pop();
- auto decl_block_id = context.inst_block_stack().Pop();
- context.scope_stack().Pop();
- if (!function_params_insts.has_value()) {
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- auto function_decl = SemIR::FunctionDecl{
- SemIR::TypeId::None, SemIR::FunctionId::None, decl_block_id};
- auto decl_id =
- AddPlaceholderInstInNoBlock(context, Parse::NodeId::None, function_decl);
- context.imports().push_back(decl_id);
- auto function_info = SemIR::Function{
- {.name_id = AddIdentifierName(context, clang_decl->getName()),
- .parent_scope_id = GetParentNameScopeId(context, clang_decl),
- .generic_id = SemIR::GenericId::None,
- .first_param_node_id = Parse::NodeId::None,
- .last_param_node_id = Parse::NodeId::None,
- .pattern_block_id = pattern_block_id,
- .implicit_param_patterns_id =
- function_params_insts->implicit_param_patterns_id,
- .param_patterns_id = function_params_insts->param_patterns_id,
- .is_extern = false,
- .extern_library_id = SemIR::LibraryNameId::None,
- .non_owning_decl_id = SemIR::InstId::None,
- .first_owning_decl_id = decl_id,
- .definition_id = SemIR::InstId::None},
- {.call_params_id = function_params_insts->call_params_id,
- .return_slot_pattern_id = function_params_insts->return_slot_pattern_id,
- .virtual_modifier = SemIR::FunctionFields::VirtualModifier::None,
- .self_param_id = FindSelfPattern(
- context, function_params_insts->implicit_param_patterns_id),
- .clang_decl_id = context.sem_ir().clang_decls().Add(
- {.decl = clang_decl, .inst_id = decl_id})}};
- function_decl.function_id = context.functions().Add(function_info);
- function_decl.type_id = GetFunctionType(context, function_decl.function_id,
- SemIR::SpecificId::None);
- ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
- return decl_id;
- }
- // Returns all decls that need to be imported before importing the given type.
- static auto GetDependentUnimportedTypeDecls(const Context& context,
- clang::QualType type)
- -> llvm::SmallVector<clang::Decl*> {
- while (true) {
- type = type.getCanonicalType();
- if (type->isPointerType() || type->isReferenceType()) {
- type = type->getPointeeType();
- } else if (const clang::ArrayType* array_type =
- type->getAsArrayTypeUnsafe()) {
- type = array_type->getElementType();
- } else {
- break;
- }
- }
- type = type.getUnqualifiedType();
- if (const auto* record_type = type->getAs<clang::RecordType>()) {
- if (auto* record_decl =
- clang::dyn_cast<clang::CXXRecordDecl>(record_type->getDecl())) {
- if (!IsClangDeclImported(context, record_decl)) {
- return {record_decl};
- }
- // TODO: Also collect base and field types.
- }
- }
- return {};
- }
- // Returns all decls that need to be imported before importing the given
- // function.
- static auto GetDependentUnimportedFunctionDecls(
- const Context& context, const clang::FunctionDecl& clang_decl)
- -> llvm::SmallVector<clang::Decl*> {
- llvm::SmallVector<clang::Decl*> decls;
- for (const auto* param : clang_decl.parameters()) {
- llvm::append_range(
- decls, GetDependentUnimportedTypeDecls(context, param->getType()));
- }
- llvm::append_range(decls, GetDependentUnimportedTypeDecls(
- context, clang_decl.getReturnType()));
- return decls;
- }
- // Returns all decls that need to be imported before importing the given
- // declaration.
- static auto GetDependentUnimportedDecls(const Context& context,
- clang::Decl* clang_decl)
- -> llvm::SmallVector<clang::Decl*> {
- llvm::SmallVector<clang::Decl*> decls;
- if (auto* parent_decl = GetParentDecl(clang_decl);
- !IsClangDeclImported(context, parent_decl)) {
- decls.push_back(parent_decl);
- }
- if (auto* clang_function_decl = clang_decl->getAsFunction()) {
- llvm::append_range(decls, GetDependentUnimportedFunctionDecls(
- context, *clang_function_decl));
- } else if (auto* type_decl = clang::dyn_cast<clang::TypeDecl>(clang_decl)) {
- llvm::append_range(
- decls,
- GetDependentUnimportedTypeDecls(
- context, type_decl->getASTContext().getTypeDeclType(type_decl)));
- }
- return decls;
- }
- // Imports a declaration from Clang to Carbon. If successful, returns the
- // instruction for the new Carbon declaration. Assumes all dependencies have
- // already been imported.
- static auto ImportDeclAfterDependencies(Context& context, SemIR::LocId loc_id,
- clang::Decl* clang_decl)
- -> SemIR::InstId {
- if (auto* clang_function_decl = clang_decl->getAsFunction()) {
- return ImportFunctionDecl(context, loc_id, clang_function_decl);
- }
- if (auto* clang_namespace_decl =
- clang::dyn_cast<clang::NamespaceDecl>(clang_decl)) {
- return ImportNamespaceDecl(context, clang_namespace_decl);
- }
- if (auto* type_decl = clang::dyn_cast<clang::TypeDecl>(clang_decl)) {
- auto type = type_decl->getASTContext().getTypeDeclType(type_decl);
- auto type_inst_id = MapType(context, loc_id, type).inst_id;
- if (!type_inst_id.has_value()) {
- context.TODO(loc_id, llvm::formatv("Unsupported: Type declaration: {0}",
- type.getAsString()));
- return SemIR::ErrorInst::InstId;
- }
- return type_inst_id;
- }
- if (clang::isa<clang::FieldDecl, clang::IndirectFieldDecl>(clang_decl)) {
- // Usable fields get imported as a side effect of importing the class.
- if (SemIR::InstId existing_inst_id =
- LookupClangDeclInstId(context, clang_decl);
- existing_inst_id.has_value()) {
- return existing_inst_id;
- }
- context.TODO(loc_id, "Unsupported: Unhandled kind of field declaration");
- return SemIR::InstId::None;
- }
- context.TODO(loc_id, llvm::formatv("Unsupported: Declaration type {0}",
- clang_decl->getDeclKindName())
- .str());
- return SemIR::InstId::None;
- }
- // Imports a declaration from Clang to Carbon. If successful, returns the
- // instruction for the new Carbon declaration. All unimported dependencies would
- // be imported first.
- static auto ImportDeclAndDependencies(Context& context, SemIR::LocId loc_id,
- clang::Decl* clang_decl)
- -> SemIR::InstId {
- // Collect dependencies.
- llvm::SetVector<clang::Decl*> clang_decls;
- clang_decls.insert(clang_decl);
- for (size_t i = 0; i < clang_decls.size(); ++i) {
- auto dependent_decls = GetDependentUnimportedDecls(context, clang_decls[i]);
- for (clang::Decl* dependent_decl : dependent_decls) {
- clang_decls.insert(dependent_decl);
- }
- }
- // Import dependencies in reverse order.
- auto inst_id = SemIR::InstId::None;
- for (clang::Decl* clang_decl_to_import : llvm::reverse(clang_decls)) {
- inst_id =
- ImportDeclAfterDependencies(context, loc_id, clang_decl_to_import);
- if (!inst_id.has_value()) {
- break;
- }
- }
- return inst_id;
- }
- // Imports a `clang::NamedDecl` into Carbon and adds that name into the
- // `NameScope`.
- static auto ImportNameDeclIntoScope(Context& context, SemIR::LocId loc_id,
- SemIR::NameScopeId scope_id,
- SemIR::NameId name_id,
- clang::NamedDecl* clang_decl)
- -> SemIR::InstId {
- SemIR::InstId inst_id =
- ImportDeclAndDependencies(context, loc_id, clang_decl);
- AddNameToScope(context, scope_id, name_id, inst_id);
- return inst_id;
- }
- auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
- SemIR::NameScopeId scope_id, SemIR::NameId name_id)
- -> SemIR::InstId {
- Diagnostics::AnnotationScope annotate_diagnostics(
- &context.emitter(), [&](auto& builder) {
- CARBON_DIAGNOSTIC(InCppNameLookup, Note,
- "in `Cpp` name lookup for `{0}`", SemIR::NameId);
- builder.Note(loc_id, InCppNameLookup, name_id);
- });
- auto lookup = ClangLookup(context, scope_id, name_id);
- if (!lookup) {
- return SemIR::InstId::None;
- }
- if (!lookup->isSingleResult()) {
- context.TODO(loc_id,
- llvm::formatv("Unsupported: Lookup succeeded but couldn't "
- "find a single result; LookupResultKind: {0}",
- static_cast<int>(lookup->getResultKind()))
- .str());
- context.name_scopes().AddRequiredName(scope_id, name_id,
- SemIR::ErrorInst::InstId);
- return SemIR::ErrorInst::InstId;
- }
- return ImportNameDeclIntoScope(context, loc_id, scope_id, name_id,
- lookup->getFoundDecl());
- }
- } // namespace Carbon::Check
|