Commit b69a48e1 authored by Simon Zünd's avatar Simon Zünd Committed by Commit Bot

[torque] Introduce 'import' syntax

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 <szuend@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62080}
parent 0226a008
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#define V8_TORQUE_AST_H_ #define V8_TORQUE_AST_H_
#include <iostream> #include <iostream>
#include <map>
#include <memory> #include <memory>
#include <set>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -198,9 +200,14 @@ class Ast { ...@@ -198,9 +200,14 @@ class Ast {
return result; return result;
} }
void DeclareImportForCurrentFile(SourceId import_id) {
declared_imports_[CurrentSourcePosition::Get().source].insert(import_id);
}
private: private:
std::vector<Declaration*> declarations_; std::vector<Declaration*> declarations_;
std::vector<std::unique_ptr<AstNode>> nodes_; std::vector<std::unique_ptr<AstNode>> nodes_;
std::map<SourceId, std::set<SourceId>> declared_imports_;
}; };
static const char* const kThisParameterName = "this"; static const char* const kThisParameterName = "this";
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "src/torque/source-positions.h" #include "src/torque/source-positions.h"
#include <fstream>
#include "src/torque/utils.h" #include "src/torque/utils.h"
namespace v8 { namespace v8 {
...@@ -63,6 +64,13 @@ std::vector<SourceId> SourceFileMap::AllSources() { ...@@ -63,6 +64,13 @@ std::vector<SourceId> SourceFileMap::AllSources() {
return result; 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 torque
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -80,6 +80,7 @@ class V8_EXPORT_PRIVATE SourceFileMap : public ContextualClass<SourceFileMap> { ...@@ -80,6 +80,7 @@ class V8_EXPORT_PRIVATE SourceFileMap : public ContextualClass<SourceFileMap> {
static SourceId AddSource(std::string path); static SourceId AddSource(std::string path);
static SourceId GetSourceId(const std::string& path); static SourceId GetSourceId(const std::string& path);
static std::vector<SourceId> AllSources(); static std::vector<SourceId> AllSources();
static bool FileRelativeToV8RootExists(const std::string& path);
private: private:
std::vector<std::string> sources_; std::vector<std::string> sources_;
......
...@@ -97,7 +97,7 @@ void CompileCurrentAst(TorqueCompilerOptions options) { ...@@ -97,7 +97,7 @@ void CompileCurrentAst(TorqueCompilerOptions options) {
TorqueCompilerResult CompileTorque(const std::string& source, TorqueCompilerResult CompileTorque(const std::string& source,
TorqueCompilerOptions options) { TorqueCompilerOptions options) {
SourceFileMap::Scope source_map_scope(""); SourceFileMap::Scope source_map_scope(options.v8_root);
CurrentSourceFile::Scope no_file_scope( CurrentSourceFile::Scope no_file_scope(
SourceFileMap::AddSource("dummy-filename.tq")); SourceFileMap::AddSource("dummy-filename.tq"));
CurrentAst::Scope ast_scope; CurrentAst::Scope ast_scope;
......
...@@ -781,6 +781,25 @@ base::Optional<ParseResult> MakeCppIncludeDeclaration( ...@@ -781,6 +781,25 @@ base::Optional<ParseResult> MakeCppIncludeDeclaration(
return ParseResult{result}; return ParseResult{result};
} }
base::Optional<ParseResult> ProcessTorqueImportDeclaration(
ParseResultIterator* child_results) {
auto import_path = child_results->NextAs<std::string>();
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<ParseResult> MakeExternalBuiltin( base::Optional<ParseResult> MakeExternalBuiltin(
ParseResultIterator* child_results) { ParseResultIterator* child_results) {
auto transitioning = child_results->NextAs<bool>(); auto transitioning = child_results->NextAs<bool>();
...@@ -1886,7 +1905,9 @@ struct TorqueGrammar : Grammar { ...@@ -1886,7 +1905,9 @@ struct TorqueGrammar : Grammar {
Token("}")}, Token("}")},
AsSingletonVector<Declaration*, MakeNamespaceDeclaration>())}; AsSingletonVector<Declaration*, MakeNamespaceDeclaration>())};
Symbol file = {Rule({&file, &namespaceDeclaration}, AddGlobalDeclarations), Symbol file = {Rule({&file, Token("import"), &externalString},
ProcessTorqueImportDeclaration),
Rule({&file, &namespaceDeclaration}, AddGlobalDeclarations),
Rule({&file, &declaration}, AddGlobalDeclarations), Rule({})}; Rule({&file, &declaration}, AddGlobalDeclarations), Rule({})};
}; };
......
...@@ -64,6 +64,7 @@ TorqueCompilerResult TestCompileTorque(std::string source) { ...@@ -64,6 +64,7 @@ TorqueCompilerResult TestCompileTorque(std::string source) {
options.output_directory = ""; options.output_directory = "";
options.collect_language_server_data = false; options.collect_language_server_data = false;
options.force_assert_statements = false; options.force_assert_statements = false;
options.v8_root = ".";
source = kTestTorquePrelude + source; source = kTestTorquePrelude + source;
return CompileTorque(source, options); return CompileTorque(source, options);
...@@ -289,6 +290,11 @@ TEST(Torque, NoUnusedWarningForVariablesOnlyUsedInAsserts) { ...@@ -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 torque
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment