[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: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#62080}
This commit is contained in:
Simon Zünd 2019-06-11 10:57:18 +02:00 committed by Commit Bot
parent 0226a00880
commit b69a48e1fe
6 changed files with 45 additions and 2 deletions

View File

@ -6,7 +6,9 @@
#define V8_TORQUE_AST_H_
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
@ -198,9 +200,14 @@ class Ast {
return result;
}
void DeclareImportForCurrentFile(SourceId import_id) {
declared_imports_[CurrentSourcePosition::Get().source].insert(import_id);
}
private:
std::vector<Declaration*> declarations_;
std::vector<std::unique_ptr<AstNode>> nodes_;
std::map<SourceId, std::set<SourceId>> declared_imports_;
};
static const char* const kThisParameterName = "this";

View File

@ -4,6 +4,7 @@
#include "src/torque/source-positions.h"
#include <fstream>
#include "src/torque/utils.h"
namespace v8 {
@ -63,6 +64,13 @@ std::vector<SourceId> 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

View File

@ -80,6 +80,7 @@ class V8_EXPORT_PRIVATE SourceFileMap : public ContextualClass<SourceFileMap> {
static SourceId AddSource(std::string path);
static SourceId GetSourceId(const std::string& path);
static std::vector<SourceId> AllSources();
static bool FileRelativeToV8RootExists(const std::string& path);
private:
std::vector<std::string> sources_;

View File

@ -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;

View File

@ -781,6 +781,25 @@ base::Optional<ParseResult> MakeCppIncludeDeclaration(
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(
ParseResultIterator* child_results) {
auto transitioning = child_results->NextAs<bool>();
@ -1886,7 +1905,9 @@ struct TorqueGrammar : Grammar {
Token("}")},
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({})};
};

View File

@ -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