import_cpp.cpp 60 KB

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