| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099 |
- // 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/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/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/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/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/ids.h"
- #include "toolchain/sem_ir/name_scope.h"
- #include "toolchain/sem_ir/typed_insts.h"
- namespace Carbon::Check {
- // The fake file name to use for the synthesized includes file.
- static constexpr const char IncludesFileName[] = "<carbon Cpp imports>";
- // 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 diagnostics from the Clang driver to Carbon diagnostics.
- class CarbonClangDriverDiagnosticConsumer : public clang::DiagnosticConsumer {
- public:
- // Creates an instance with the location that triggers calling Clang.
- // `context` must not be null.
- explicit CarbonClangDriverDiagnosticConsumer(
- Diagnostics::NoLocEmitter* emitter)
- : emitter_(emitter) {}
- // 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);
- llvm::SmallString<256> message;
- info.FormatDiagnostic(message);
- switch (diag_level) {
- case clang::DiagnosticsEngine::Ignored:
- case clang::DiagnosticsEngine::Note:
- case clang::DiagnosticsEngine::Remark: {
- // TODO: Emit notes and remarks.
- break;
- }
- case clang::DiagnosticsEngine::Warning:
- case clang::DiagnosticsEngine::Error:
- case clang::DiagnosticsEngine::Fatal: {
- CARBON_DIAGNOSTIC(CppInteropDriverWarning, Warning, "{0}", std::string);
- CARBON_DIAGNOSTIC(CppInteropDriverError, Error, "{0}", std::string);
- emitter_->Emit(diag_level == clang::DiagnosticsEngine::Warning
- ? CppInteropDriverWarning
- : CppInteropDriverError,
- message.str().str());
- break;
- }
- }
- }
- private:
- // Diagnostic emitter. Note that driver diagnostics don't have meaningful
- // locations attached.
- Diagnostics::NoLocEmitter* emitter_;
- };
- } // namespace
- // Builds a clang `CompilerInvocation` describing the options to use to build an
- // imported C++ AST.
- // TODO: Cache the compiler invocation created here and reuse it if building
- // multiple AST units. Consider building the `CompilerInvocation` from the
- // driver and passing it into check. This would also allow us to have a shared
- // set of defaults between the Clang invocation we use for imports and the
- // invocation we use for `carbon clang`.
- static auto BuildCompilerInvocation(
- Context& context, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
- const std::string& clang_path, const std::string& target)
- -> std::unique_ptr<clang::CompilerInvocation> {
- Diagnostics::NoLocEmitter emitter(context.emitter());
- CarbonClangDriverDiagnosticConsumer diagnostics_consumer(&emitter);
- const char* driver_args[] = {
- clang_path.c_str(),
- // Propagate the target to Clang.
- "-target",
- target.c_str(),
- // Require PIE. Note its default is configurable in Clang.
- "-fPIE",
- // Parse as a C++ (not C) header.
- "-x",
- "c++",
- IncludesFileName,
- };
- // Build a diagnostics engine. Note that we don't have any diagnostic options
- // yet; they're produced by running the driver.
- clang::DiagnosticOptions driver_diag_opts;
- llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> driver_diags(
- clang::CompilerInstance::createDiagnostics(*fs, driver_diag_opts,
- &diagnostics_consumer,
- /*ShouldOwnClient=*/false));
- // Ask the driver to process the arguments and build a corresponding clang
- // frontend invocation.
- auto invocation =
- clang::createInvocation(driver_args, {.Diags = driver_diags, .VFS = fs});
- // Emit any queued diagnostics from parsing our driver arguments.
- return invocation;
- }
- 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,
- clang::CompilerInvocation* invocation)
- : context_(context), invocation_(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.
- 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,
- const std::string& clang_path,
- const std::string& target)
- -> std::pair<std::unique_ptr<clang::ASTUnit>, bool> {
- // Build the options to use to invoke the Clang frontend.
- std::shared_ptr<clang::CompilerInvocation> invocation =
- BuildCompilerInvocation(context, fs, clang_path, target);
- if (!invocation) {
- return {nullptr, true};
- }
- // Build a diagnostics engine.
- CarbonClangDiagnosticConsumer diagnostics_consumer(&context,
- invocation.get());
- llvm::IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags(
- clang::CompilerInstance::createDiagnostics(
- *fs, invocation->getDiagnosticOpts(), &diagnostics_consumer,
- /*ShouldOwnClient=*/false));
- // Remap the imports file name to the corresponding `#include`s.
- std::string includes = GenerateCppIncludesHeaderCode(context, imports);
- auto includes_buffer =
- llvm::MemoryBuffer::getMemBuffer(includes, IncludesFileName);
- invocation->getPreprocessorOpts().addRemappedFile(IncludesFileName,
- 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 link to the diagnostics consumer before its destruction.
- ast->getDiagnostics().setClient(nullptr);
- // 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();
- return {std::move(ast), !ast || diagnostics_consumer.getNumErrors() > 0};
- }
- // 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,
- llvm::StringRef clang_path, llvm::StringRef target)
- -> 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, clang_path.str(), target.str());
- 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;
- }
- // Imports a namespace declaration from Clang to Carbon. If successful, returns
- // the new Carbon namespace declaration `InstId`.
- static auto ImportNamespaceDecl(Context& context,
- SemIR::NameScopeId parent_scope_id,
- SemIR::NameId name_id,
- clang::NamespaceDecl* clang_decl)
- -> SemIR::InstId {
- auto result = AddImportNamespace(
- context, GetSingletonType(context, SemIR::NamespaceType::TypeInstId),
- name_id, parent_scope_id, /*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, .inst_id = result.inst_id}));
- return result.inst_id;
- }
- // Maps a C++ declaration context to a Carbon namespace.
- static auto AsCarbonNamespace(Context& context,
- clang::DeclContext* decl_context)
- -> SemIR::InstId {
- CARBON_CHECK(decl_context);
- auto& clang_decls = context.sem_ir().clang_decls();
- // Check if the decl context is already mapped to a Carbon namespace.
- if (auto context_clang_decl_id =
- clang_decls.Lookup(clang::dyn_cast<clang::Decl>(decl_context));
- context_clang_decl_id.has_value()) {
- return clang_decls.Get(context_clang_decl_id).inst_id;
- }
- // We know we have at least one context to map, add all decl contexts we need
- // to map.
- llvm::SmallVector<clang::DeclContext*> decl_contexts;
- auto parent_decl_id = SemIR::ClangDeclId::None;
- do {
- decl_contexts.push_back(decl_context);
- decl_context = decl_context->getParent();
- parent_decl_id =
- clang_decls.Lookup(clang::dyn_cast<clang::Decl>(decl_context));
- } while (!parent_decl_id.has_value());
- // We know the parent of the last decl context is mapped, map the rest.
- auto namespace_inst_id = SemIR::InstId::None;
- do {
- decl_context = decl_contexts.pop_back_val();
- auto parent_inst_id = clang_decls.Get(parent_decl_id).inst_id;
- auto parent_namespace =
- context.insts().GetAs<SemIR::Namespace>(parent_inst_id);
- namespace_inst_id = ImportNamespaceDecl(
- context, parent_namespace.name_scope_id,
- AddIdentifierName(
- context, llvm::dyn_cast<clang::NamedDecl>(decl_context)->getName()),
- clang::dyn_cast<clang::NamespaceDecl>(decl_context));
- parent_decl_id = clang_decls.Add({
- .decl = clang::dyn_cast<clang::Decl>(decl_context),
- .inst_id = namespace_inst_id,
- });
- } while (!decl_contexts.empty());
- return namespace_inst_id;
- }
- // 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::NameScopeId parent_scope_id,
- SemIR::NameId name_id)
- -> std::tuple<SemIR::ClassId, SemIR::InstId> {
- // 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};
- // TODO: Consider setting a proper location.
- auto class_decl_id = AddPlaceholderInstInNoBlock(
- context, SemIR::LocIdAndInst::NoLoc(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, class_decl_id};
- }
- // Creates a class definition for the given class name in the given scope based
- // on the information in the given Clang declaration. Returns the `InstId` for
- // the declaration, which is assumed to be for a class definition. Returns the
- // new class id and instruction id.
- static auto BuildClassDefinition(Context& context,
- SemIR::NameScopeId parent_scope_id,
- SemIR::NameId name_id,
- clang::CXXRecordDecl* clang_decl)
- -> std::tuple<SemIR::ClassId, SemIR::InstId> {
- auto [class_id, class_inst_id] =
- BuildClassDecl(context, parent_scope_id, name_id);
- auto& class_info = context.classes().Get(class_id);
- StartClassDefinition(context, class_info, class_inst_id);
- context.name_scopes()
- .Get(class_info.scope_id)
- .set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
- {.decl = clang_decl, .inst_id = class_inst_id}));
- return {class_id, class_inst_id};
- }
- // 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, .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.
- static auto ImportCXXRecordDecl(Context& context, SemIR::LocId loc_id,
- SemIR::NameScopeId parent_scope_id,
- SemIR::NameId name_id,
- clang::CXXRecordDecl* clang_decl)
- -> SemIR::InstId {
- clang::CXXRecordDecl* clang_def = clang_decl->getDefinition();
- if (!clang_def) {
- context.TODO(loc_id,
- "Unsupported: Record declarations without a definition");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- if (clang_def->isDynamicClass()) {
- context.TODO(loc_id, "Unsupported: Dynamic Class");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- if (clang_def->isUnion() && !clang_def->fields().empty()) {
- context.TODO(loc_id, "Unsupported: Non-empty union");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- auto [class_id, class_def_id] =
- BuildClassDefinition(context, parent_scope_id, name_id, clang_def);
- // The class type is now fully defined. Compute its object representation.
- ComputeClassObjectRepr(context,
- // TODO: Consider having a proper location here.
- Parse::ClassDefinitionId::None, class_id,
- // TODO: Set fields.
- /*field_decls=*/{},
- // TODO: Set vtable.
- /*vtable_contents=*/{},
- // TODO: Set block.
- /*body=*/{});
- return class_def_id;
- }
- // Creates an integer type of the given size.
- static auto MakeIntType(Context& context, IntId size_id) -> TypeExpr {
- // TODO: Fill in a location for the type once available.
- auto type_inst_id = MakeIntTypeLiteral(context, Parse::NodeId::None,
- SemIR::IntKind::Signed, 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, const clang::BuiltinType& type)
- -> TypeExpr {
- // TODO: Refactor to avoid duplication.
- switch (type.getKind()) {
- case clang::BuiltinType::Short:
- if (context.ast_context().getTypeSize(&type) == 16) {
- return MakeIntType(context, context.ints().Add(16));
- }
- break;
- case clang::BuiltinType::Int:
- if (context.ast_context().getTypeSize(&type) == 32) {
- return MakeIntType(context, context.ints().Add(32));
- }
- break;
- default:
- break;
- }
- 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.
- static auto MapRecordType(Context& context, SemIR::LocId loc_id,
- 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};
- }
- auto& clang_decls = context.sem_ir().clang_decls();
- SemIR::InstId record_inst_id = SemIR::InstId::None;
- if (auto record_clang_decl_id = clang_decls.Lookup(record_decl);
- record_clang_decl_id.has_value()) {
- record_inst_id = clang_decls.Get(record_clang_decl_id).inst_id;
- } else {
- auto parent_inst_id =
- AsCarbonNamespace(context, record_decl->getDeclContext());
- auto parent_name_scope_id =
- context.insts().GetAs<SemIR::Namespace>(parent_inst_id).name_scope_id;
- SemIR::NameId record_name_id =
- AddIdentifierName(context, record_decl->getName());
- record_inst_id = ImportCXXRecordDecl(context, loc_id, parent_name_scope_id,
- record_name_id, 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.
- static auto MapNonWrapperType(Context& context, SemIR::LocId loc_id,
- clang::QualType type) -> TypeExpr {
- if (const auto* builtin_type = type->getAs<clang::BuiltinType>()) {
- return MapBuiltinType(context, *builtin_type);
- }
- if (const auto* record_type = type->getAs<clang::RecordType>()) {
- return MapRecordType(context, loc_id, *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.
- 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, loc_id, 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 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 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> {
- 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;
- }
- // TODO: Add support for implicit parameters.
- auto call_params_id = CalleePatternMatch(
- context, /*implicit_param_patterns_id=*/SemIR::InstBlockId::None,
- param_patterns_id, return_slot_pattern_id);
- return {{.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`.
- static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
- SemIR::NameScopeId scope_id,
- SemIR::NameId name_id,
- clang::FunctionDecl* clang_decl)
- -> SemIR::InstId {
- if (clang_decl->isVariadic()) {
- context.TODO(loc_id, "Unsupported: Variadic function");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- if (!clang_decl->isGlobal()) {
- context.TODO(loc_id, "Unsupported: Non-global function");
- MarkFailedDecl(context, clang_decl);
- return SemIR::ErrorInst::InstId;
- }
- if (clang_decl->getTemplatedKind() != clang::FunctionDecl::TK_NonTemplate) {
- context.TODO(loc_id, "Unsupported: Template 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 = name_id,
- .parent_scope_id = scope_id,
- .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 = SemIR::InstBlockId::Empty,
- .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 = SemIR::InstId::None,
- .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;
- }
- // Imports a declaration from Clang to Carbon. If successful, returns the
- // instruction for the new Carbon declaration.
- static auto ImportNameDecl(Context& context, SemIR::LocId loc_id,
- SemIR::NameScopeId scope_id, SemIR::NameId name_id,
- clang::NamedDecl* clang_decl) -> SemIR::InstId {
- if (auto* clang_function_decl = clang_decl->getAsFunction()) {
- return ImportFunctionDecl(context, loc_id, scope_id, name_id,
- clang_function_decl);
- }
- if (auto* clang_namespace_decl =
- clang::dyn_cast<clang::NamespaceDecl>(clang_decl)) {
- return ImportNamespaceDecl(context, scope_id, name_id,
- 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;
- }
- context.TODO(loc_id, llvm::formatv("Unsupported: Declaration type {0}",
- clang_decl->getDeclKindName())
- .str());
- return SemIR::InstId::None;
- }
- // 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 =
- ImportNameDecl(context, loc_id, scope_id, name_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
|