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 52 : base::Optional<std::string> ReadFile(const std::string& path) {
22 104 : std::ifstream file_stream(path);
23 52 : if (!file_stream.good()) return base::nullopt;
24 :
25 : return std::string{std::istreambuf_iterator<char>(file_stream),
26 52 : std::istreambuf_iterator<char>()};
27 : }
28 :
29 52 : void ReadAndParseTorqueFile(const std::string& path) {
30 104 : SourceId source_id = SourceFileMap::AddSource(path);
31 52 : CurrentSourceFile::Scope source_id_scope(source_id);
32 :
33 : // path might be either a normal file path or an encoded URI.
34 52 : auto maybe_content = ReadFile(path);
35 52 : if (!maybe_content) {
36 0 : if (auto maybe_path = FileUriDecode(path)) {
37 0 : maybe_content = ReadFile(*maybe_path);
38 : }
39 : }
40 :
41 52 : if (!maybe_content) {
42 0 : ReportErrorWithoutPosition("Cannot open file path/uri: ", path);
43 : }
44 :
45 52 : ParseTorque(*maybe_content);
46 52 : }
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 6 : ImplementationVisitor implementation_visitor;
62 3 : for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
63 44 : 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 2 : output_header_path = output_directory + "/class-definitions-from-dsl.h";
75 1 : implementation_visitor.GenerateClassDefinitions(output_header_path);
76 :
77 : std::string output_source_path =
78 1 : output_directory + "/objects-printer-from-dsl.cc";
79 1 : implementation_visitor.GeneratePrintDefinitions(output_source_path);
80 :
81 1 : for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
82 42 : implementation_visitor.EndNamespaceFile(n);
83 42 : implementation_visitor.GenerateImplementation(output_directory, n);
84 : }
85 : }
86 :
87 3 : if (LintErrorStatus::HasLintErrors()) std::abort();
88 3 : }
89 :
90 3 : TorqueCompilerResult CollectResultFromContextuals() {
91 : TorqueCompilerResult result;
92 : result.source_file_map = SourceFileMap::Get();
93 : result.language_server_data = LanguageServerData::Get();
94 3 : return result;
95 : }
96 :
97 0 : TorqueCompilerResult ResultFromError(TorqueError& error) {
98 : TorqueCompilerResult result;
99 : result.source_file_map = SourceFileMap::Get();
100 : result.error = error;
101 0 : return result;
102 : }
103 :
104 : } // namespace
105 :
106 2 : TorqueCompilerResult CompileTorque(const std::string& source,
107 : TorqueCompilerOptions options) {
108 2 : SourceFileMap::Scope source_map_scope;
109 4 : CurrentSourceFile::Scope no_file_scope(SourceFileMap::AddSource("<torque>"));
110 2 : CurrentAst::Scope ast_scope;
111 2 : LintErrorStatus::Scope lint_error_status_scope;
112 4 : LanguageServerData::Scope server_data_scope;
113 :
114 : try {
115 2 : ParseTorque(source);
116 4 : CompileCurrentAst(options);
117 : } catch (TorqueError& error) {
118 : return ResultFromError(error);
119 : }
120 :
121 2 : return CollectResultFromContextuals();
122 : }
123 :
124 1 : TorqueCompilerResult CompileTorque(std::vector<std::string> files,
125 : TorqueCompilerOptions options) {
126 1 : SourceFileMap::Scope source_map_scope;
127 1 : CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
128 1 : CurrentAst::Scope ast_scope;
129 1 : LintErrorStatus::Scope lint_error_status_scope;
130 2 : LanguageServerData::Scope server_data_scope;
131 :
132 : try {
133 53 : for (const auto& path : files) ReadAndParseTorqueFile(path);
134 2 : CompileCurrentAst(options);
135 : } catch (TorqueError& error) {
136 : return ResultFromError(error);
137 : }
138 1 : return CollectResultFromContextuals();
139 : }
140 :
141 : } // namespace torque
142 : } // namespace internal
143 59456 : } // namespace v8
|