From b69a48e1fee159e989cae1ebf5b010b7a7a6f786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Z=C3=BCnd?= Date: Tue, 11 Jun 2019 10:57:18 +0200 Subject: [PATCH] [torque] Introduce 'import' syntax MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This CL introduces an 'import' statement. It does not produce any AST node. The AST contextual directly collects what source id imports what other source id. Currently the import map is unused. In the future, import syntax will be used to implement partial compilation. Bug: v8:7793 Change-Id: I5f09e6254d7ca2e7bc1a93d2e2d82e202cafc8ef Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1649357 Commit-Queue: Simon Zünd Reviewed-by: Tobias Tebbi Cr-Commit-Position: refs/heads/master@{#62080} --- src/torque/ast.h | 7 +++++++ src/torque/source-positions.cc | 8 ++++++++ src/torque/source-positions.h | 1 + src/torque/torque-compiler.cc | 2 +- src/torque/torque-parser.cc | 23 ++++++++++++++++++++++- test/unittests/torque/torque-unittest.cc | 6 ++++++ 6 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/torque/ast.h b/src/torque/ast.h index 3826bd2e35..0fdb4bc65d 100644 --- a/src/torque/ast.h +++ b/src/torque/ast.h @@ -6,7 +6,9 @@ #define V8_TORQUE_AST_H_ #include +#include #include +#include #include #include @@ -198,9 +200,14 @@ class Ast { return result; } + void DeclareImportForCurrentFile(SourceId import_id) { + declared_imports_[CurrentSourcePosition::Get().source].insert(import_id); + } + private: std::vector declarations_; std::vector> nodes_; + std::map> declared_imports_; }; static const char* const kThisParameterName = "this"; diff --git a/src/torque/source-positions.cc b/src/torque/source-positions.cc index e704fe0aab..69be0e0911 100644 --- a/src/torque/source-positions.cc +++ b/src/torque/source-positions.cc @@ -4,6 +4,7 @@ #include "src/torque/source-positions.h" +#include #include "src/torque/utils.h" namespace v8 { @@ -63,6 +64,13 @@ std::vector SourceFileMap::AllSources() { return result; } +// static +bool SourceFileMap::FileRelativeToV8RootExists(const std::string& path) { + const std::string file = Get().v8_root_ + "/" + path; + std::ifstream stream(file); + return stream.good(); +} + } // namespace torque } // namespace internal } // namespace v8 diff --git a/src/torque/source-positions.h b/src/torque/source-positions.h index 34fc4caa31..69ce78cf7c 100644 --- a/src/torque/source-positions.h +++ b/src/torque/source-positions.h @@ -80,6 +80,7 @@ class V8_EXPORT_PRIVATE SourceFileMap : public ContextualClass { static SourceId AddSource(std::string path); static SourceId GetSourceId(const std::string& path); static std::vector AllSources(); + static bool FileRelativeToV8RootExists(const std::string& path); private: std::vector sources_; diff --git a/src/torque/torque-compiler.cc b/src/torque/torque-compiler.cc index 7644185f53..58437fa49c 100644 --- a/src/torque/torque-compiler.cc +++ b/src/torque/torque-compiler.cc @@ -97,7 +97,7 @@ void CompileCurrentAst(TorqueCompilerOptions options) { TorqueCompilerResult CompileTorque(const std::string& source, TorqueCompilerOptions options) { - SourceFileMap::Scope source_map_scope(""); + SourceFileMap::Scope source_map_scope(options.v8_root); CurrentSourceFile::Scope no_file_scope( SourceFileMap::AddSource("dummy-filename.tq")); CurrentAst::Scope ast_scope; diff --git a/src/torque/torque-parser.cc b/src/torque/torque-parser.cc index 90c986e7f3..9ad5748630 100644 --- a/src/torque/torque-parser.cc +++ b/src/torque/torque-parser.cc @@ -781,6 +781,25 @@ base::Optional MakeCppIncludeDeclaration( return ParseResult{result}; } +base::Optional ProcessTorqueImportDeclaration( + ParseResultIterator* child_results) { + auto import_path = child_results->NextAs(); + if (!SourceFileMap::FileRelativeToV8RootExists(import_path)) { + Error("File '", import_path, "' not found."); + } + + auto import_id = SourceFileMap::GetSourceId(import_path); + if (!import_id.IsValid()) { + // TODO(szuend): Instead of reporting and error. Queue the file up + // for compilation. + Error("File '", import_path, "'is not part of the source set.").Throw(); + } + + CurrentAst::Get().DeclareImportForCurrentFile(import_id); + + return base::nullopt; +} + base::Optional MakeExternalBuiltin( ParseResultIterator* child_results) { auto transitioning = child_results->NextAs(); @@ -1886,7 +1905,9 @@ struct TorqueGrammar : Grammar { Token("}")}, AsSingletonVector())}; - Symbol file = {Rule({&file, &namespaceDeclaration}, AddGlobalDeclarations), + Symbol file = {Rule({&file, Token("import"), &externalString}, + ProcessTorqueImportDeclaration), + Rule({&file, &namespaceDeclaration}, AddGlobalDeclarations), Rule({&file, &declaration}, AddGlobalDeclarations), Rule({})}; }; diff --git a/test/unittests/torque/torque-unittest.cc b/test/unittests/torque/torque-unittest.cc index b12a281f41..f2689fd7fc 100644 --- a/test/unittests/torque/torque-unittest.cc +++ b/test/unittests/torque/torque-unittest.cc @@ -64,6 +64,7 @@ TorqueCompilerResult TestCompileTorque(std::string source) { options.output_directory = ""; options.collect_language_server_data = false; options.force_assert_statements = false; + options.v8_root = "."; source = kTestTorquePrelude + source; return CompileTorque(source, options); @@ -289,6 +290,11 @@ TEST(Torque, NoUnusedWarningForVariablesOnlyUsedInAsserts) { )"); } +TEST(Torque, ImportNonExistentFile) { + ExpectFailingCompilation(R"(import "foo/bar.tq")", + HasSubstr("File 'foo/bar.tq' not found.")); +} + } // namespace torque } // namespace internal } // namespace v8