import_cpp.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "clang/Frontend/TextDiagnosticPrinter.h"
  9. #include "clang/Sema/Lookup.h"
  10. #include "clang/Tooling/Tooling.h"
  11. #include "common/raw_string_ostream.h"
  12. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. #include "toolchain/check/context.h"
  16. #include "toolchain/check/diagnostic_helpers.h"
  17. #include "toolchain/check/import.h"
  18. #include "toolchain/check/inst.h"
  19. #include "toolchain/check/literal.h"
  20. #include "toolchain/check/type.h"
  21. #include "toolchain/diagnostics/diagnostic.h"
  22. #include "toolchain/diagnostics/format_providers.h"
  23. #include "toolchain/parse/node_ids.h"
  24. #include "toolchain/sem_ir/ids.h"
  25. #include "toolchain/sem_ir/name_scope.h"
  26. namespace Carbon::Check {
  27. // Generates C++ file contents to #include all requested imports.
  28. static auto GenerateCppIncludesHeaderCode(
  29. Context& context, llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
  30. -> std::string {
  31. std::string code;
  32. llvm::raw_string_ostream code_stream(code);
  33. for (const Parse::Tree::PackagingNames& import : imports) {
  34. code_stream << "#include \""
  35. << FormatEscaped(
  36. context.string_literal_values().Get(import.library_id))
  37. << "\"\n";
  38. }
  39. return code;
  40. }
  41. // Returns an AST for the C++ imports and a bool that represents whether
  42. // compilation errors where encountered or the generated AST is null due to an
  43. // error.
  44. // TODO: Consider to always have a (non-null) AST.
  45. static auto GenerateAst(Context& context, llvm::StringRef importing_file_path,
  46. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  47. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
  48. -> std::pair<std::unique_ptr<clang::ASTUnit>, bool> {
  49. // TODO: Use all import locations by referring each Clang diagnostic to the
  50. // relevant import.
  51. SemIRLoc loc = imports.back().node_id;
  52. std::string diagnostics_str;
  53. llvm::raw_string_ostream diagnostics_stream(diagnostics_str);
  54. llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> diagnostic_options(
  55. new clang::DiagnosticOptions());
  56. clang::TextDiagnosticPrinter diagnostics_consumer(diagnostics_stream,
  57. diagnostic_options.get());
  58. // TODO: Share compilation flags with ClangRunner.
  59. auto ast = clang::tooling::buildASTFromCodeWithArgs(
  60. GenerateCppIncludesHeaderCode(context, imports),
  61. // Parse C++ (and not C)
  62. {"-x", "c++"}, (importing_file_path + ".generated.cpp_imports.h").str(),
  63. "clang-tool", std::make_shared<clang::PCHContainerOperations>(),
  64. clang::tooling::getClangStripDependencyFileAdjuster(),
  65. clang::tooling::FileContentMappings(), &diagnostics_consumer, fs);
  66. // Remove link to the diagnostics consumer before its deletion.
  67. ast->getDiagnostics().setClient(nullptr);
  68. // TODO: Implement and use a DynamicRecursiveASTVisitor to traverse the AST.
  69. int num_errors = diagnostics_consumer.getNumErrors();
  70. int num_warnings = diagnostics_consumer.getNumWarnings();
  71. int num_imports = imports.size();
  72. if (num_errors > 0) {
  73. // TODO: Remove the warnings part when there are no warnings.
  74. CARBON_DIAGNOSTIC(
  75. CppInteropParseError, Error,
  76. "{0} error{0:s} and {1} warning{1:s} in {2} `Cpp` import{2:s}:\n{3}",
  77. IntAsSelect, IntAsSelect, IntAsSelect, std::string);
  78. context.emitter().Emit(loc, CppInteropParseError, num_errors, num_warnings,
  79. num_imports, diagnostics_str);
  80. } else if (num_warnings > 0) {
  81. CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning,
  82. "{0} warning{0:s} in `Cpp` {1} import{1:s}:\n{2}",
  83. IntAsSelect, IntAsSelect, std::string);
  84. context.emitter().Emit(loc, CppInteropParseWarning, num_warnings,
  85. num_imports, diagnostics_str);
  86. }
  87. return {std::move(ast), !ast || num_errors > 0};
  88. }
  89. // Adds a namespace for the `Cpp` import and returns its `NameScopeId`.
  90. static auto AddNamespace(Context& context, PackageNameId cpp_package_id,
  91. llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
  92. -> SemIR::NameScopeId {
  93. auto& import_cpps = context.sem_ir().import_cpps();
  94. import_cpps.Reserve(imports.size());
  95. for (const Parse::Tree::PackagingNames& import : imports) {
  96. import_cpps.Add({.node_id = context.parse_tree().As<Parse::ImportDeclId>(
  97. import.node_id),
  98. .library_id = import.library_id});
  99. }
  100. return AddImportNamespaceToScope(
  101. context,
  102. GetSingletonType(context, SemIR::NamespaceType::SingletonInstId),
  103. SemIR::NameId::ForPackageName(cpp_package_id),
  104. SemIR::NameScopeId::Package,
  105. /*diagnose_duplicate_namespace=*/false,
  106. [&]() {
  107. return AddInst<SemIR::ImportCppDecl>(
  108. context,
  109. context.parse_tree().As<Parse::ImportDeclId>(
  110. imports.front().node_id),
  111. {});
  112. })
  113. .add_result.name_scope_id;
  114. }
  115. auto ImportCppFiles(Context& context, llvm::StringRef importing_file_path,
  116. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  117. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
  118. -> std::unique_ptr<clang::ASTUnit> {
  119. if (imports.empty()) {
  120. return nullptr;
  121. }
  122. CARBON_CHECK(!context.sem_ir().cpp_ast());
  123. auto [generated_ast, ast_has_error] =
  124. GenerateAst(context, importing_file_path, imports, fs);
  125. PackageNameId package_id = imports.front().package_id;
  126. CARBON_CHECK(
  127. llvm::all_of(imports, [&](const Parse::Tree::PackagingNames& import) {
  128. return import.package_id == package_id;
  129. }));
  130. auto name_scope_id = AddNamespace(context, package_id, imports);
  131. SemIR::NameScope& name_scope = context.name_scopes().Get(name_scope_id);
  132. name_scope.set_is_closed_import(true);
  133. name_scope.set_cpp_decl_context(
  134. generated_ast->getASTContext().getTranslationUnitDecl());
  135. context.sem_ir().set_cpp_ast(generated_ast.get());
  136. if (ast_has_error) {
  137. name_scope.set_has_error();
  138. }
  139. return std::move(generated_ast);
  140. }
  141. // Look ups the given name in the Clang AST in a specific scope. Returns the
  142. // lookup result if lookup was successful.
  143. static auto ClangLookup(Context& context, SemIR::LocId loc_id,
  144. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  145. -> std::optional<clang::LookupResult> {
  146. std::optional<llvm::StringRef> name =
  147. context.names().GetAsStringIfIdentifier(name_id);
  148. if (!name) {
  149. // Special names never exist in C++ code.
  150. return std::nullopt;
  151. }
  152. clang::ASTUnit* ast = context.sem_ir().cpp_ast();
  153. CARBON_CHECK(ast);
  154. clang::Sema& sema = ast->getSema();
  155. clang::LookupResult lookup(
  156. sema,
  157. clang::DeclarationNameInfo(
  158. clang::DeclarationName(
  159. sema.getPreprocessor().getIdentifierInfo(*name)),
  160. clang::SourceLocation()),
  161. clang::Sema::LookupNameKind::LookupOrdinaryName);
  162. bool found = sema.LookupQualifiedName(
  163. lookup, context.name_scopes().Get(scope_id).cpp_decl_context());
  164. if (lookup.isClassLookup()) {
  165. // TODO: To support class lookup, also return the AccessKind for storage.
  166. context.TODO(loc_id, "Unsupported: Lookup in Class");
  167. return std::nullopt;
  168. }
  169. if (!found) {
  170. return std::nullopt;
  171. }
  172. return lookup;
  173. }
  174. // Returns the return type of the given function declaration.
  175. // Currently only void and 32-bit int are supported.
  176. // TODO: Support more return types.
  177. static auto GetReturnType(Context& context, SemIRLoc loc_id,
  178. const clang::FunctionDecl* clang_decl)
  179. -> SemIR::InstId {
  180. clang::QualType ret_type = clang_decl->getReturnType().getCanonicalType();
  181. if (ret_type->isVoidType()) {
  182. return SemIR::InstId::None;
  183. }
  184. if (const auto* builtin_type = dyn_cast<clang::BuiltinType>(ret_type);
  185. builtin_type && builtin_type->getKind() == clang::BuiltinType::Int) {
  186. constexpr int SupportedIntWidth = 32;
  187. uint64_t int_size = context.ast_context().getTypeSize(ret_type);
  188. if (int_size != SupportedIntWidth) {
  189. // TODO: Add tests for this case.
  190. context.TODO(loc_id,
  191. llvm::formatv("Unsupported: return type: {0}, size: {1}",
  192. ret_type.getAsString(), int_size));
  193. return SemIR::ErrorInst::SingletonInstId;
  194. }
  195. IntId size_id = context.ints().Add(int_size);
  196. // TODO: Fill in a location for the type once available.
  197. SemIR::TypeId type_id = MakeIntType(context, Parse::NodeId::None,
  198. SemIR::IntKind::Signed, size_id);
  199. // TODO: Fill in a location for the type once available.
  200. SemIR::InstId type_inst_id = MakeIntTypeLiteral(
  201. context, Parse::NodeId::None, SemIR::IntKind::Signed, size_id);
  202. SemIR::InstId return_slot_pattern_id = AddInstInNoBlock(
  203. // TODO: Fill in a location for the return type once available.
  204. context, SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
  205. {.type_id = type_id, .type_inst_id = type_inst_id})));
  206. SemIR::InstId param_pattern_id = AddInstInNoBlock(
  207. // TODO: Fill in a location for the return type once available.
  208. context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
  209. {.type_id = type_id,
  210. .subpattern_id = return_slot_pattern_id,
  211. .index = SemIR::CallParamIndex::None})));
  212. return param_pattern_id;
  213. }
  214. context.TODO(loc_id, llvm::formatv("Unsupported: return type: {0}",
  215. ret_type.getAsString()));
  216. return SemIR::ErrorInst::SingletonInstId;
  217. }
  218. // Imports a function declaration from Clang to Carbon. If successful, returns
  219. // the new Carbon function declaration `InstId`.
  220. static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
  221. SemIR::NameScopeId scope_id,
  222. SemIR::NameId name_id,
  223. const clang::FunctionDecl* clang_decl)
  224. -> SemIR::InstId {
  225. if (clang_decl->isVariadic()) {
  226. context.TODO(loc_id, "Unsupported: Variadic function");
  227. return SemIR::ErrorInst::SingletonInstId;
  228. }
  229. if (!clang_decl->isGlobal()) {
  230. context.TODO(loc_id, "Unsupported: Non-global function");
  231. return SemIR::ErrorInst::SingletonInstId;
  232. }
  233. if (clang_decl->getTemplatedKind() != clang::FunctionDecl::TK_NonTemplate) {
  234. context.TODO(loc_id, "Unsupported: Template function");
  235. return SemIR::ErrorInst::SingletonInstId;
  236. }
  237. if (!clang_decl->param_empty()) {
  238. context.TODO(loc_id, "Unsupported: Function with parameters");
  239. return SemIR::ErrorInst::SingletonInstId;
  240. }
  241. auto return_slot_pattern_id = GetReturnType(context, loc_id, clang_decl);
  242. if (SemIR::ErrorInst::SingletonInstId == return_slot_pattern_id) {
  243. return SemIR::ErrorInst::SingletonInstId;
  244. }
  245. auto function_decl = SemIR::FunctionDecl{
  246. SemIR::TypeId::None, SemIR::FunctionId::None, SemIR::InstBlockId::Empty};
  247. auto decl_id =
  248. AddPlaceholderInst(context, Parse::NodeId::None, function_decl);
  249. auto function_info = SemIR::Function{
  250. {.name_id = name_id,
  251. .parent_scope_id = scope_id,
  252. .generic_id = SemIR::GenericId::None,
  253. .first_param_node_id = Parse::NodeId::None,
  254. .last_param_node_id = Parse::NodeId::None,
  255. .pattern_block_id = SemIR::InstBlockId::Empty,
  256. .implicit_param_patterns_id = SemIR::InstBlockId::Empty,
  257. .param_patterns_id = SemIR::InstBlockId::Empty,
  258. .is_extern = false,
  259. .extern_library_id = SemIR::LibraryNameId::None,
  260. .non_owning_decl_id = SemIR::InstId::None,
  261. .first_owning_decl_id = decl_id,
  262. .definition_id = SemIR::InstId::None},
  263. {.call_params_id = SemIR::InstBlockId::Empty,
  264. .return_slot_pattern_id = return_slot_pattern_id,
  265. .virtual_modifier = SemIR::FunctionFields::VirtualModifier::None,
  266. .self_param_id = SemIR::InstId::None,
  267. .cpp_decl = clang_decl}};
  268. function_decl.function_id = context.functions().Add(function_info);
  269. function_decl.type_id = GetFunctionType(context, function_decl.function_id,
  270. SemIR::SpecificId::None);
  271. ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
  272. return decl_id;
  273. }
  274. // Imports a namespace declaration from Clang to Carbon. If successful, returns
  275. // the new Carbon namespace declaration `InstId`.
  276. static auto ImportNamespaceDecl(Context& context,
  277. SemIR::NameScopeId parent_scope_id,
  278. SemIR::NameId name_id,
  279. clang::NamespaceDecl* clang_decl)
  280. -> SemIR::InstId {
  281. auto result = AddImportNamespace(
  282. context, GetSingletonType(context, SemIR::NamespaceType::SingletonInstId),
  283. name_id, parent_scope_id, /*import_id=*/SemIR::InstId::None);
  284. context.name_scopes()
  285. .Get(result.name_scope_id)
  286. .set_cpp_decl_context(clang_decl);
  287. return result.inst_id;
  288. }
  289. // Imports a declaration from Clang to Carbon. If successful, returns the
  290. // instruction for the new Carbon declaration.
  291. static auto ImportNameDecl(Context& context, SemIR::LocId loc_id,
  292. SemIR::NameScopeId scope_id, SemIR::NameId name_id,
  293. clang::NamedDecl* clang_decl) -> SemIR::InstId {
  294. if (const auto* clang_function_decl =
  295. clang::dyn_cast<clang::FunctionDecl>(clang_decl)) {
  296. return ImportFunctionDecl(context, loc_id, scope_id, name_id,
  297. clang_function_decl);
  298. }
  299. if (auto* clang_namespace_decl =
  300. clang::dyn_cast<clang::NamespaceDecl>(clang_decl)) {
  301. return ImportNamespaceDecl(context, scope_id, name_id,
  302. clang_namespace_decl);
  303. }
  304. context.TODO(loc_id, llvm::formatv("Unsupported: Declaration type {0}",
  305. clang_decl->getDeclKindName())
  306. .str());
  307. return SemIR::InstId::None;
  308. }
  309. auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
  310. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  311. -> SemIR::InstId {
  312. auto lookup = ClangLookup(context, loc_id, scope_id, name_id);
  313. if (!lookup) {
  314. return SemIR::InstId::None;
  315. }
  316. DiagnosticAnnotationScope annotate_diagnostics(
  317. &context.emitter(), [&](auto& builder) {
  318. CARBON_DIAGNOSTIC(InCppNameLookup, Note,
  319. "in `Cpp` name lookup for `{0}`", SemIR::NameId);
  320. builder.Note(loc_id, InCppNameLookup, name_id);
  321. });
  322. if (!lookup->isSingleResult()) {
  323. context.TODO(loc_id,
  324. llvm::formatv("Unsupported: Lookup succeeded but couldn't "
  325. "find a single result; LookupResultKind: {0}",
  326. lookup->getResultKind())
  327. .str());
  328. return SemIR::ErrorInst::SingletonInstId;
  329. }
  330. return ImportNameDecl(context, loc_id, scope_id, name_id,
  331. lookup->getFoundDecl());
  332. }
  333. } // namespace Carbon::Check