import_cpp.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. Diagnostics::IntAsSelect, Diagnostics::IntAsSelect,
  78. Diagnostics::IntAsSelect, std::string);
  79. context.emitter().Emit(loc, CppInteropParseError, num_errors, num_warnings,
  80. num_imports, diagnostics_str);
  81. } else if (num_warnings > 0) {
  82. CARBON_DIAGNOSTIC(CppInteropParseWarning, Warning,
  83. "{0} warning{0:s} in `Cpp` {1} import{1:s}:\n{2}",
  84. Diagnostics::IntAsSelect, Diagnostics::IntAsSelect,
  85. std::string);
  86. context.emitter().Emit(loc, CppInteropParseWarning, num_warnings,
  87. num_imports, diagnostics_str);
  88. }
  89. return {std::move(ast), !ast || num_errors > 0};
  90. }
  91. // Adds a namespace for the `Cpp` import and returns its `NameScopeId`.
  92. static auto AddNamespace(Context& context, PackageNameId cpp_package_id,
  93. llvm::ArrayRef<Parse::Tree::PackagingNames> imports)
  94. -> SemIR::NameScopeId {
  95. auto& import_cpps = context.sem_ir().import_cpps();
  96. import_cpps.Reserve(imports.size());
  97. for (const Parse::Tree::PackagingNames& import : imports) {
  98. import_cpps.Add({.node_id = context.parse_tree().As<Parse::ImportDeclId>(
  99. import.node_id),
  100. .library_id = import.library_id});
  101. }
  102. return AddImportNamespaceToScope(
  103. context,
  104. GetSingletonType(context, SemIR::NamespaceType::SingletonInstId),
  105. SemIR::NameId::ForPackageName(cpp_package_id),
  106. SemIR::NameScopeId::Package,
  107. /*diagnose_duplicate_namespace=*/false,
  108. [&]() {
  109. return AddInst<SemIR::ImportCppDecl>(
  110. context,
  111. context.parse_tree().As<Parse::ImportDeclId>(
  112. imports.front().node_id),
  113. {});
  114. })
  115. .add_result.name_scope_id;
  116. }
  117. auto ImportCppFiles(Context& context, llvm::StringRef importing_file_path,
  118. llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
  119. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
  120. -> std::unique_ptr<clang::ASTUnit> {
  121. if (imports.empty()) {
  122. return nullptr;
  123. }
  124. CARBON_CHECK(!context.sem_ir().cpp_ast());
  125. auto [generated_ast, ast_has_error] =
  126. GenerateAst(context, importing_file_path, imports, fs);
  127. PackageNameId package_id = imports.front().package_id;
  128. CARBON_CHECK(
  129. llvm::all_of(imports, [&](const Parse::Tree::PackagingNames& import) {
  130. return import.package_id == package_id;
  131. }));
  132. auto name_scope_id = AddNamespace(context, package_id, imports);
  133. SemIR::NameScope& name_scope = context.name_scopes().Get(name_scope_id);
  134. name_scope.set_is_closed_import(true);
  135. name_scope.set_cpp_decl_context(
  136. generated_ast->getASTContext().getTranslationUnitDecl());
  137. context.sem_ir().set_cpp_ast(generated_ast.get());
  138. if (ast_has_error) {
  139. name_scope.set_has_error();
  140. }
  141. return std::move(generated_ast);
  142. }
  143. // Look ups the given name in the Clang AST in a specific scope. Returns the
  144. // lookup result if lookup was successful.
  145. static auto ClangLookup(Context& context, SemIR::LocId loc_id,
  146. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  147. -> std::optional<clang::LookupResult> {
  148. std::optional<llvm::StringRef> name =
  149. context.names().GetAsStringIfIdentifier(name_id);
  150. if (!name) {
  151. // Special names never exist in C++ code.
  152. return std::nullopt;
  153. }
  154. clang::ASTUnit* ast = context.sem_ir().cpp_ast();
  155. CARBON_CHECK(ast);
  156. clang::Sema& sema = ast->getSema();
  157. clang::LookupResult lookup(
  158. sema,
  159. clang::DeclarationNameInfo(
  160. clang::DeclarationName(
  161. sema.getPreprocessor().getIdentifierInfo(*name)),
  162. clang::SourceLocation()),
  163. clang::Sema::LookupNameKind::LookupOrdinaryName);
  164. bool found = sema.LookupQualifiedName(
  165. lookup, context.name_scopes().Get(scope_id).cpp_decl_context());
  166. if (lookup.isClassLookup()) {
  167. // TODO: To support class lookup, also return the AccessKind for storage.
  168. context.TODO(loc_id, "Unsupported: Lookup in Class");
  169. return std::nullopt;
  170. }
  171. if (!found) {
  172. return std::nullopt;
  173. }
  174. return lookup;
  175. }
  176. // Returns the return type of the given function declaration.
  177. // Currently only void and 32-bit int are supported.
  178. // TODO: Support more return types.
  179. static auto GetReturnType(Context& context, SemIRLoc loc_id,
  180. const clang::FunctionDecl* clang_decl)
  181. -> SemIR::InstId {
  182. clang::QualType ret_type = clang_decl->getReturnType().getCanonicalType();
  183. if (ret_type->isVoidType()) {
  184. return SemIR::InstId::None;
  185. }
  186. if (const auto* builtin_type = dyn_cast<clang::BuiltinType>(ret_type);
  187. builtin_type && builtin_type->getKind() == clang::BuiltinType::Int) {
  188. constexpr int SupportedIntWidth = 32;
  189. uint64_t int_size = context.ast_context().getTypeSize(ret_type);
  190. if (int_size != SupportedIntWidth) {
  191. // TODO: Add tests for this case.
  192. context.TODO(loc_id,
  193. llvm::formatv("Unsupported: return type: {0}, size: {1}",
  194. ret_type.getAsString(), int_size));
  195. return SemIR::ErrorInst::SingletonInstId;
  196. }
  197. IntId size_id = context.ints().Add(int_size);
  198. // TODO: Fill in a location for the type once available.
  199. SemIR::TypeId type_id = MakeIntType(context, Parse::NodeId::None,
  200. SemIR::IntKind::Signed, size_id);
  201. // TODO: Fill in a location for the type once available.
  202. SemIR::InstId type_inst_id = MakeIntTypeLiteral(
  203. context, Parse::NodeId::None, SemIR::IntKind::Signed, size_id);
  204. SemIR::InstId return_slot_pattern_id = AddInstInNoBlock(
  205. // TODO: Fill in a location for the return type once available.
  206. context, SemIR::LocIdAndInst::NoLoc(SemIR::ReturnSlotPattern(
  207. {.type_id = type_id, .type_inst_id = type_inst_id})));
  208. SemIR::InstId param_pattern_id = AddInstInNoBlock(
  209. // TODO: Fill in a location for the return type once available.
  210. context, SemIR::LocIdAndInst::NoLoc(SemIR::OutParamPattern(
  211. {.type_id = type_id,
  212. .subpattern_id = return_slot_pattern_id,
  213. .index = SemIR::CallParamIndex::None})));
  214. return param_pattern_id;
  215. }
  216. context.TODO(loc_id, llvm::formatv("Unsupported: return type: {0}",
  217. ret_type.getAsString()));
  218. return SemIR::ErrorInst::SingletonInstId;
  219. }
  220. // Imports a function declaration from Clang to Carbon. If successful, returns
  221. // the new Carbon function declaration `InstId`.
  222. static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
  223. SemIR::NameScopeId scope_id,
  224. SemIR::NameId name_id,
  225. const clang::FunctionDecl* clang_decl)
  226. -> SemIR::InstId {
  227. if (clang_decl->isVariadic()) {
  228. context.TODO(loc_id, "Unsupported: Variadic function");
  229. return SemIR::ErrorInst::SingletonInstId;
  230. }
  231. if (!clang_decl->isGlobal()) {
  232. context.TODO(loc_id, "Unsupported: Non-global function");
  233. return SemIR::ErrorInst::SingletonInstId;
  234. }
  235. if (clang_decl->getTemplatedKind() != clang::FunctionDecl::TK_NonTemplate) {
  236. context.TODO(loc_id, "Unsupported: Template function");
  237. return SemIR::ErrorInst::SingletonInstId;
  238. }
  239. if (!clang_decl->param_empty()) {
  240. context.TODO(loc_id, "Unsupported: Function with parameters");
  241. return SemIR::ErrorInst::SingletonInstId;
  242. }
  243. auto return_slot_pattern_id = GetReturnType(context, loc_id, clang_decl);
  244. if (SemIR::ErrorInst::SingletonInstId == return_slot_pattern_id) {
  245. return SemIR::ErrorInst::SingletonInstId;
  246. }
  247. auto function_decl = SemIR::FunctionDecl{
  248. SemIR::TypeId::None, SemIR::FunctionId::None, SemIR::InstBlockId::Empty};
  249. auto decl_id =
  250. AddPlaceholderInst(context, Parse::NodeId::None, function_decl);
  251. auto function_info = SemIR::Function{
  252. {.name_id = name_id,
  253. .parent_scope_id = scope_id,
  254. .generic_id = SemIR::GenericId::None,
  255. .first_param_node_id = Parse::NodeId::None,
  256. .last_param_node_id = Parse::NodeId::None,
  257. .pattern_block_id = SemIR::InstBlockId::Empty,
  258. .implicit_param_patterns_id = SemIR::InstBlockId::Empty,
  259. .param_patterns_id = SemIR::InstBlockId::Empty,
  260. .is_extern = false,
  261. .extern_library_id = SemIR::LibraryNameId::None,
  262. .non_owning_decl_id = SemIR::InstId::None,
  263. .first_owning_decl_id = decl_id,
  264. .definition_id = SemIR::InstId::None},
  265. {.call_params_id = SemIR::InstBlockId::Empty,
  266. .return_slot_pattern_id = return_slot_pattern_id,
  267. .virtual_modifier = SemIR::FunctionFields::VirtualModifier::None,
  268. .self_param_id = SemIR::InstId::None,
  269. .cpp_decl = clang_decl}};
  270. function_decl.function_id = context.functions().Add(function_info);
  271. function_decl.type_id = GetFunctionType(context, function_decl.function_id,
  272. SemIR::SpecificId::None);
  273. ReplaceInstBeforeConstantUse(context, decl_id, function_decl);
  274. return decl_id;
  275. }
  276. // Imports a namespace declaration from Clang to Carbon. If successful, returns
  277. // the new Carbon namespace declaration `InstId`.
  278. static auto ImportNamespaceDecl(Context& context,
  279. SemIR::NameScopeId parent_scope_id,
  280. SemIR::NameId name_id,
  281. clang::NamespaceDecl* clang_decl)
  282. -> SemIR::InstId {
  283. auto result = AddImportNamespace(
  284. context, GetSingletonType(context, SemIR::NamespaceType::SingletonInstId),
  285. name_id, parent_scope_id, /*import_id=*/SemIR::InstId::None);
  286. context.name_scopes()
  287. .Get(result.name_scope_id)
  288. .set_cpp_decl_context(clang_decl);
  289. return result.inst_id;
  290. }
  291. // Imports a declaration from Clang to Carbon. If successful, returns the
  292. // instruction for the new Carbon declaration.
  293. static auto ImportNameDecl(Context& context, SemIR::LocId loc_id,
  294. SemIR::NameScopeId scope_id, SemIR::NameId name_id,
  295. clang::NamedDecl* clang_decl) -> SemIR::InstId {
  296. if (const auto* clang_function_decl =
  297. clang::dyn_cast<clang::FunctionDecl>(clang_decl)) {
  298. return ImportFunctionDecl(context, loc_id, scope_id, name_id,
  299. clang_function_decl);
  300. }
  301. if (auto* clang_namespace_decl =
  302. clang::dyn_cast<clang::NamespaceDecl>(clang_decl)) {
  303. return ImportNamespaceDecl(context, scope_id, name_id,
  304. clang_namespace_decl);
  305. }
  306. context.TODO(loc_id, llvm::formatv("Unsupported: Declaration type {0}",
  307. clang_decl->getDeclKindName())
  308. .str());
  309. return SemIR::InstId::None;
  310. }
  311. auto ImportNameFromCpp(Context& context, SemIR::LocId loc_id,
  312. SemIR::NameScopeId scope_id, SemIR::NameId name_id)
  313. -> SemIR::InstId {
  314. Diagnostics::AnnotationScope annotate_diagnostics(
  315. &context.emitter(), [&](auto& builder) {
  316. CARBON_DIAGNOSTIC(InCppNameLookup, Note,
  317. "in `Cpp` name lookup for `{0}`", SemIR::NameId);
  318. builder.Note(loc_id, InCppNameLookup, name_id);
  319. });
  320. auto lookup = ClangLookup(context, loc_id, scope_id, name_id);
  321. if (!lookup) {
  322. return SemIR::InstId::None;
  323. }
  324. if (!lookup->isSingleResult()) {
  325. context.TODO(loc_id,
  326. llvm::formatv("Unsupported: Lookup succeeded but couldn't "
  327. "find a single result; LookupResultKind: {0}",
  328. lookup->getResultKind())
  329. .str());
  330. return SemIR::ErrorInst::SingletonInstId;
  331. }
  332. return ImportNameDecl(context, loc_id, scope_id, name_id,
  333. lookup->getFoundDecl());
  334. }
  335. } // namespace Carbon::Check