forked from v8/v8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtorque-compiler.cc
More file actions
201 lines (164 loc) · 6.79 KB
/
torque-compiler.cc
File metadata and controls
201 lines (164 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/torque/torque-compiler.h"
#include <fstream>
#include "src/torque/declarable.h"
#include "src/torque/declaration-visitor.h"
#include "src/torque/global-context.h"
#include "src/torque/implementation-visitor.h"
#include "src/torque/torque-parser.h"
#include "src/torque/type-oracle.h"
namespace v8 {
namespace internal {
namespace torque {
namespace {
base::Optional<std::string> ReadFile(const std::string& path) {
std::ifstream file_stream(path);
if (!file_stream.good()) return base::nullopt;
return std::string{std::istreambuf_iterator<char>(file_stream),
std::istreambuf_iterator<char>()};
}
void ReadAndParseTorqueFile(const std::string& path) {
SourceId source_id = SourceFileMap::AddSource(path);
CurrentSourceFile::Scope source_id_scope(source_id);
// path might be either a normal file path or an encoded URI.
auto maybe_content = ReadFile(SourceFileMap::AbsolutePath(source_id));
if (!maybe_content) {
if (auto maybe_path = FileUriDecode(path)) {
maybe_content = ReadFile(*maybe_path);
}
}
if (!maybe_content) {
Error("Cannot open file path/uri: ", path).Throw();
}
ParseTorque(*maybe_content);
}
void CompileCurrentAst(TorqueCompilerOptions options) {
GlobalContext::Scope global_context(std::move(CurrentAst::Get()));
if (options.collect_language_server_data) {
GlobalContext::SetCollectLanguageServerData();
}
if (options.collect_kythe_data) {
GlobalContext::SetCollectKytheData();
}
if (options.force_assert_statements) {
GlobalContext::SetForceAssertStatements();
}
if (options.annotate_ir) {
GlobalContext::SetAnnotateIR();
}
TargetArchitecture::Scope target_architecture(options.force_32bit_output);
TypeOracle::Scope type_oracle;
CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace());
// Two-step process of predeclaration + resolution allows to resolve type
// declarations independent of the order they are given.
PredeclarationVisitor::Predeclare(GlobalContext::ast());
PredeclarationVisitor::ResolvePredeclarations();
// Process other declarations.
DeclarationVisitor::Visit(GlobalContext::ast());
// A class types' fields are resolved here, which allows two class fields to
// mutually refer to each others.
TypeOracle::FinalizeAggregateTypes();
std::string output_directory = options.output_directory;
ImplementationVisitor implementation_visitor;
implementation_visitor.SetDryRun(output_directory.length() == 0);
implementation_visitor.GenerateInstanceTypes(output_directory);
implementation_visitor.BeginGeneratedFiles();
implementation_visitor.BeginDebugMacrosFile();
implementation_visitor.VisitAllDeclarables();
ReportAllUnusedMacros();
implementation_visitor.GenerateBuiltinDefinitionsAndInterfaceDescriptors(
output_directory);
implementation_visitor.GenerateVisitorLists(output_directory);
implementation_visitor.GenerateBitFields(output_directory);
implementation_visitor.GeneratePrintDefinitions(output_directory);
implementation_visitor.GenerateClassDefinitions(output_directory);
implementation_visitor.GenerateClassVerifiers(output_directory);
implementation_visitor.GenerateClassDebugReaders(output_directory);
implementation_visitor.GenerateEnumVerifiers(output_directory);
implementation_visitor.GenerateBodyDescriptors(output_directory);
implementation_visitor.GenerateExportedMacrosAssembler(output_directory);
implementation_visitor.GenerateCSATypes(output_directory);
implementation_visitor.EndGeneratedFiles();
implementation_visitor.EndDebugMacrosFile();
implementation_visitor.GenerateImplementation(output_directory);
if (GlobalContext::collect_language_server_data()) {
LanguageServerData::SetGlobalContext(std::move(GlobalContext::Get()));
LanguageServerData::SetTypeOracle(std::move(TypeOracle::Get()));
}
}
} // namespace
TorqueCompilerResult CompileTorque(const std::string& source,
TorqueCompilerOptions options) {
SourceFileMap::Scope source_map_scope(options.v8_root);
CurrentSourceFile::Scope no_file_scope(
SourceFileMap::AddSource("dummy-filename.tq"));
CurrentAst::Scope ast_scope;
TorqueMessages::Scope messages_scope;
LanguageServerData::Scope server_data_scope;
TorqueCompilerResult result;
try {
ParseTorque(source);
CompileCurrentAst(options);
} catch (TorqueAbortCompilation&) {
// Do nothing. The relevant TorqueMessage is part of the
// TorqueMessages contextual.
}
result.source_file_map = SourceFileMap::Get();
result.language_server_data = std::move(LanguageServerData::Get());
result.messages = std::move(TorqueMessages::Get());
return result;
}
TorqueCompilerResult CompileTorque(std::vector<std::string> files,
TorqueCompilerOptions options) {
SourceFileMap::Scope source_map_scope(options.v8_root);
CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
CurrentAst::Scope ast_scope;
TorqueMessages::Scope messages_scope;
LanguageServerData::Scope server_data_scope;
TorqueCompilerResult result;
try {
for (const auto& path : files) {
ReadAndParseTorqueFile(path);
}
CompileCurrentAst(options);
} catch (TorqueAbortCompilation&) {
// Do nothing. The relevant TorqueMessage is part of the
// TorqueMessages contextual.
}
result.source_file_map = SourceFileMap::Get();
result.language_server_data = std::move(LanguageServerData::Get());
result.messages = std::move(TorqueMessages::Get());
return result;
}
TorqueCompilerResult CompileTorqueForKythe(
std::vector<TorqueCompilationUnit> units, TorqueCompilerOptions options,
KytheConsumer* consumer) {
SourceFileMap::Scope source_map_scope(options.v8_root);
CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
CurrentAst::Scope ast_scope;
TorqueMessages::Scope messages_scope;
LanguageServerData::Scope server_data_scope;
KytheData::Scope kythe_scope;
KytheData::Get().SetConsumer(consumer);
TorqueCompilerResult result;
try {
for (const auto& unit : units) {
SourceId source_id = SourceFileMap::AddSource(unit.source_file_path);
CurrentSourceFile::Scope source_id_scope(source_id);
ParseTorque(unit.file_content);
}
CompileCurrentAst(options);
} catch (TorqueAbortCompilation&) {
// Do nothing. The relevant TorqueMessage is part of the
// TorqueMessages contextual.
}
result.source_file_map = SourceFileMap::Get();
result.language_server_data = std::move(LanguageServerData::Get());
result.messages = std::move(TorqueMessages::Get());
return result;
}
} // namespace torque
} // namespace internal
} // namespace v8