v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
torque-compiler.cc
Go to the documentation of this file.
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
6
7#include <fstream>
8#include <optional>
9
16
17namespace v8::internal::torque {
18
19namespace {
20
21std::optional<std::string> ReadFile(const std::string& path) {
22 std::ifstream file_stream(path);
23 if (!file_stream.good()) return std::nullopt;
24
25 return std::string{std::istreambuf_iterator<char>(file_stream),
26 std::istreambuf_iterator<char>()};
27}
28
29void ReadAndParseTorqueFile(const std::string& path) {
30 SourceId source_id = SourceFileMap::AddSource(path);
31 CurrentSourceFile::Scope source_id_scope(source_id);
32
33 // path might be either a normal file path or an encoded URI.
34 auto maybe_content = ReadFile(SourceFileMap::AbsolutePath(source_id));
35 if (!maybe_content) {
36 if (auto maybe_path = FileUriDecode(path)) {
37 maybe_content = ReadFile(*maybe_path);
38 }
39 }
40
41 if (!maybe_content) {
42 Error("Cannot open file path/uri: ", path).Throw();
43 }
44
45 ParseTorque(*maybe_content);
46}
47
48void CompileCurrentAst(TorqueCompilerOptions options) {
49 GlobalContext::Scope global_context(std::move(CurrentAst::Get()));
50 if (options.collect_language_server_data) {
52 }
53 if (options.collect_kythe_data) {
55 }
56 if (options.force_assert_statements) {
58 }
59 if (options.annotate_ir) {
61 }
62 TypeOracle::Scope type_oracle;
63 CurrentScope::Scope current_namespace(GlobalContext::GetDefaultNamespace());
64
65 // Two-step process of predeclaration + resolution allows to resolve type
66 // declarations independent of the order they are given.
69
70 // Process other declarations.
72
73 // A class types' fields are resolved here, which allows two class fields to
74 // mutually refer to each others.
76
77 std::string output_directory = options.output_directory;
78
79 ImplementationVisitor implementation_visitor;
80 implementation_visitor.SetDryRun(output_directory.empty());
81
82 implementation_visitor.GenerateInstanceTypes(output_directory);
83 implementation_visitor.BeginGeneratedFiles();
84 implementation_visitor.BeginDebugMacrosFile();
85
86 implementation_visitor.VisitAllDeclarables();
87
89
90 implementation_visitor.GenerateBuiltinDefinitionsAndInterfaceDescriptors(
91 output_directory);
92 implementation_visitor.GenerateVisitorLists(output_directory);
93 implementation_visitor.GenerateBitFields(output_directory);
94 implementation_visitor.GeneratePrintDefinitions(output_directory);
95 implementation_visitor.GenerateClassDefinitions(output_directory);
96 implementation_visitor.GenerateClassVerifiers(output_directory);
97 implementation_visitor.GenerateClassDebugReaders(output_directory);
98 implementation_visitor.GenerateEnumVerifiers(output_directory);
99 implementation_visitor.GenerateBodyDescriptors(output_directory);
100 implementation_visitor.GenerateExportedMacrosAssembler(output_directory);
101 implementation_visitor.GenerateCSATypes(output_directory);
102
103 implementation_visitor.EndGeneratedFiles();
104 implementation_visitor.EndDebugMacrosFile();
105 implementation_visitor.GenerateImplementation(output_directory);
106
110 }
111}
112
113} // namespace
114
115TorqueCompilerResult CompileTorque(const std::string& source,
116 TorqueCompilerOptions options) {
117 TargetArchitecture::Scope target_architecture(options.force_32bit_output);
118 SourceFileMap::Scope source_map_scope(options.v8_root);
119 CurrentSourceFile::Scope no_file_scope(
120 SourceFileMap::AddSource("dummy-filename.tq"));
121 CurrentAst::Scope ast_scope;
122 TorqueMessages::Scope messages_scope;
123 LanguageServerData::Scope server_data_scope;
124
126 try {
127 ParseTorque(source);
128 CompileCurrentAst(options);
129 } catch (TorqueAbortCompilation&) {
130 // Do nothing. The relevant TorqueMessage is part of the
131 // TorqueMessages contextual.
132 }
133
135 result.language_server_data = std::move(LanguageServerData::Get());
136 result.messages = std::move(TorqueMessages::Get());
137
138 return result;
139}
140
141TorqueCompilerResult CompileTorque(const std::vector<std::string>& files,
142 TorqueCompilerOptions options) {
143 TargetArchitecture::Scope target_architecture(options.force_32bit_output);
144 SourceFileMap::Scope source_map_scope(options.v8_root);
145 CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
146 CurrentAst::Scope ast_scope;
147 TorqueMessages::Scope messages_scope;
148 LanguageServerData::Scope server_data_scope;
149
151 try {
152 for (const auto& path : files) {
153 ReadAndParseTorqueFile(path);
154 }
155 CompileCurrentAst(options);
156 } catch (TorqueAbortCompilation&) {
157 // Do nothing. The relevant TorqueMessage is part of the
158 // TorqueMessages contextual.
159 }
160
162 result.language_server_data = std::move(LanguageServerData::Get());
163 result.messages = std::move(TorqueMessages::Get());
164
165 return result;
166}
167
169 std::vector<TorqueCompilationUnit> units, TorqueCompilerOptions options,
170 KytheConsumer* consumer) {
171 TargetArchitecture::Scope target_architecture(options.force_32bit_output);
172 SourceFileMap::Scope source_map_scope(options.v8_root);
173 CurrentSourceFile::Scope unknown_source_file_scope(SourceId::Invalid());
174 CurrentAst::Scope ast_scope;
175 TorqueMessages::Scope messages_scope;
176 LanguageServerData::Scope server_data_scope;
177 KytheData::Scope kythe_scope;
178
179 KytheData::Get().SetConsumer(consumer);
180
182 try {
183 for (const auto& unit : units) {
184 SourceId source_id = SourceFileMap::AddSource(unit.source_file_path);
185 CurrentSourceFile::Scope source_id_scope(source_id);
186 ParseTorque(unit.file_content);
187 }
188 CompileCurrentAst(options);
189 } catch (TorqueAbortCompilation&) {
190 // Do nothing. The relevant TorqueMessage is part of the
191 // TorqueMessages contextual.
192 }
193
195 result.language_server_data = std::move(LanguageServerData::Get());
196 result.messages = std::move(TorqueMessages::Get());
197
198 return result;
199}
200
201} // namespace v8::internal::torque
static VarType & Get()
Definition contextual.h:64
static Namespace * GetDefaultNamespace()
static void SetGlobalContext(GlobalContext global_context)
Definition server-data.h:47
static void SetTypeOracle(TypeOracle type_oracle)
Definition server-data.h:53
static SourceId AddSource(std::string path)
static std::string AbsolutePath(SourceId file)
ZoneVector< RpoNumber > & result
std::priority_queue< BigUnit > units[CompilationTier::kNumTiers]
std::optional< std::string > FileUriDecode(const std::string &uri)
Definition utils.cc:94
void ParseTorque(const std::string &input)
TorqueCompilerResult CompileTorque(const std::string &source, TorqueCompilerOptions options)
TorqueCompilerResult CompileTorqueForKythe(std::vector< TorqueCompilationUnit > units, TorqueCompilerOptions options, KytheConsumer *consumer)
MessageBuilder Error(Args &&... args)
Definition utils.h:81
std::string ReadFile(const char *filename, bool *exists, bool verbose)
Definition utils.cc:178
std::optional< SourceFileMap > source_file_map