Line data Source code
1 : // Copyright 2019 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/torque/torque-compiler.h"
6 :
7 : #include <fstream>
8 : #include "src/torque/declarable.h"
9 : #include "src/torque/declaration-visitor.h"
10 : #include "src/torque/global-context.h"
11 : #include "src/torque/implementation-visitor.h"
12 : #include "src/torque/torque-parser.h"
13 : #include "src/torque/type-oracle.h"
14 :
15 : namespace v8 {
16 : namespace internal {
17 : namespace torque {
18 :
19 : namespace {
20 :
21 47 : base::Optional<std::string> ReadFile(const std::string& path) {
22 94 : std::ifstream file_stream(path);
23 47 : if (!file_stream.good()) return base::nullopt;
24 :
25 : return std::string{std::istreambuf_iterator<char>(file_stream),
26 47 : std::istreambuf_iterator<char>()};
27 : }
28 :
29 47 : void ReadAndParseTorqueFile(const std::string& path) {
30 94 : SourceId source_id = SourceFileMap::AddSource(path);
31 47 : CurrentSourceFile::Scope source_id_scope(source_id);
32 :
33 : // path might be either a normal file path or an encoded URI.
34 47 : auto maybe_content = ReadFile(path);
35 47 : if (!maybe_content) {
36 0 : if (auto maybe_path = FileUriDecode(path)) {
37 0 : maybe_content = ReadFile(*maybe_path);
38 : }
39 : }
40 :
41 47 : if (!maybe_content) {
42 0 : ReportErrorWithoutPosition("Cannot open file path/uri: ", path);
43 : }
44 :
45 47 : ParseTorque(*maybe_content);
46 47 : }
47 :
48 3 : void CompileCurrentAst(TorqueCompilerOptions options) {
49 3 : GlobalContext::Scope global_context(std::move(CurrentAst::Get()));
50 3 : if (options.verbose) GlobalContext::SetVerbose();
51 3 : if (options.collect_language_server_data) {
52 : GlobalContext::SetCollectLanguageServerData();
53 : }
54 3 : TypeOracle::Scope type_oracle;
55 :
56 3 : DeclarationVisitor declaration_visitor;
57 :
58 3 : declaration_visitor.Visit(GlobalContext::Get().ast());
59 3 : declaration_visitor.FinalizeStructsAndClasses();
60 :
61 3 : ImplementationVisitor implementation_visitor;
62 3 : for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
63 42 : implementation_visitor.BeginNamespaceFile(n);
64 : }
65 :
66 3 : implementation_visitor.VisitAllDeclarables();
67 :
68 : std::string output_directory = options.output_directory;
69 3 : if (output_directory.length() != 0) {
70 : std::string output_header_path = output_directory;
71 : output_header_path += "/builtin-definitions-from-dsl.h";
72 1 : implementation_visitor.GenerateBuiltinDefinitions(output_header_path);
73 :
74 1 : output_header_path = output_directory + "/class-definitions-from-dsl.h";
75 1 : implementation_visitor.GenerateClassDefinitions(output_header_path);
76 :
77 1 : for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
78 40 : implementation_visitor.EndNamespaceFile(n);
79 40 : implementation_visitor.GenerateImplementation(output_directory, n);
80 : }
81 : }
82 :
83 3 : if (LintErrorStatus::HasLintErrors()) std::abort();
84 3 : }
85 :
86 : } // namespace
87 :
88 2 : void CompileTorque(const std::string& source, TorqueCompilerOptions options) {
89 4 : CurrentSourceFile::Scope no_file_scope(SourceFileMap::AddSource("<torque>"));
90 2 : CurrentAst::Scope ast_scope_;
91 2 : LintErrorStatus::Scope lint_error_status_scope_;
92 :
93 2 : ParseTorque(source);
94 4 : CompileCurrentAst(options);
95 2 : }
96 :
97 1 : void CompileTorque(std::vector<std::string> files,
98 : TorqueCompilerOptions options) {
99 1 : CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
100 1 : CurrentAst::Scope ast_scope_;
101 1 : LintErrorStatus::Scope lint_error_status_scope_;
102 :
103 48 : for (const auto& path : files) ReadAndParseTorqueFile(path);
104 :
105 2 : CompileCurrentAst(options);
106 1 : }
107 :
108 : } // namespace torque
109 : } // namespace internal
110 6150 : } // namespace v8
|