Line data Source code
1 : // Copyright 2017 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 <fstream>
6 : #include <iostream>
7 :
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 : #include "src/torque/types.h"
15 : #include "src/torque/utils.h"
16 :
17 : namespace v8 {
18 : namespace internal {
19 : namespace torque {
20 :
21 1 : int WrappedMain(int argc, const char** argv) {
22 : std::string output_directory;
23 : bool verbose = false;
24 2 : SourceFileMap::Scope source_file_map_scope;
25 : CurrentSourceFile::Scope unknown_sourcefile_scope(
26 2 : SourceFileMap::AddSource("<unknown>"));
27 2 : CurrentAst::Scope ast_scope;
28 1 : LintErrorStatus::Scope lint_error_status_scope;
29 :
30 30 : for (int i = 1; i < argc; ++i) {
31 : // Check for options
32 29 : if (!strcmp("-o", argv[i])) {
33 1 : output_directory = argv[++i];
34 1 : continue;
35 : }
36 28 : if (!strcmp("-v", argv[i])) {
37 : verbose = true;
38 : continue;
39 : }
40 :
41 : // Otherwise it's a .tq
42 : // file, parse it and
43 : // remember the syntax tree
44 28 : std::string path = argv[i];
45 56 : SourceId source_id = SourceFileMap::AddSource(path);
46 28 : CurrentSourceFile::Scope source_id_scope(source_id);
47 56 : std::ifstream file_stream(path);
48 : std::string file_content = {std::istreambuf_iterator<char>(file_stream),
49 28 : std::istreambuf_iterator<char>()};
50 28 : ParseTorque(file_content);
51 : }
52 :
53 2 : GlobalContext::Scope global_context(std::move(CurrentAst::Get()));
54 1 : if (verbose) GlobalContext::SetVerbose();
55 2 : TypeOracle::Scope type_oracle;
56 :
57 1 : if (output_directory.length() != 0) {
58 : DeclarationVisitor declaration_visitor;
59 :
60 1 : declaration_visitor.Visit(GlobalContext::Get().ast());
61 1 : declaration_visitor.FinalizeStructsAndClasses();
62 :
63 2 : ImplementationVisitor implementation_visitor;
64 25 : for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
65 23 : implementation_visitor.BeginNamespaceFile(n);
66 : }
67 :
68 1 : implementation_visitor.VisitAllDeclarables();
69 :
70 1 : 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 25 : for (Namespace* n : GlobalContext::Get().GetNamespaces()) {
78 23 : implementation_visitor.EndNamespaceFile(n);
79 23 : implementation_visitor.GenerateImplementation(output_directory, n);
80 1 : }
81 : }
82 :
83 1 : if (LintErrorStatus::HasLintErrors()) std::abort();
84 :
85 1 : return 0;
86 : }
87 :
88 : } // namespace torque
89 : } // namespace internal
90 : } // namespace v8
91 :
92 1 : int main(int argc, const char** argv) {
93 1 : return v8::internal::torque::WrappedMain(argc, argv);
94 3 : }
|