forked from free1139/ziron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_generator.cpp
125 lines (113 loc) · 3.68 KB
/
json_generator.cpp
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
// Copyright 2018 The Fuchsia 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 "generator.h"
bool JsonGenerator::header(std::ofstream& os) {
os << "{\n";
os << " \"syscalls\": [\n";
return os.good();
}
bool JsonGenerator::footer(std::ofstream& os) {
os << "\n";
os << " ]\n";
os << "}\n";
return os.good();
}
bool JsonGenerator::syscall(std::ofstream& os, const Syscall& sc) {
if (first_syscall_) {
first_syscall_ = false;
} else {
os << ",\n";
}
os << " {\n";
os << " \"name\": \"" << sc.name << "\",\n";
// Attributes.
os << " \"attributes\": [\n";
for (std::vector<std::string>::size_type index = 0;
index != sc.attributes.size(); ++index) {
os << " \"" << sc.attributes[index] << "\"";
if (index < sc.attributes.size() - 1) {
os << ",";
}
os << "\n";
}
os << " ],\n";
// Top description.
os << " \"top_description\": [\n";
os << " ";
for (size_t i = 0; i < sc.top_description.size(); ++i) {
os << "\"" << sc.top_description[i] << "\"";
if (i < sc.top_description.size() - 1) {
os << ", ";
}
}
os << "\n ],\n";
// Requirements.
os << " \"requirements\": [\n";
for (size_t i = 0; i < sc.requirements.size(); ++i) {
os << " ";
for (size_t j = 0; j < sc.requirements[i].size(); ++j) {
os << "\"" << sc.requirements[i][j] << "\"";
if (j < sc.requirements[i].size() - 1) {
os << ", ";
}
}
if (i < sc.requirements.size() - 1) {
os << ",";
}
os << "\n";
}
os << " ],\n";
// Arguments.
os << " \"arguments\": [\n";
bool first_arg = true;
bool has_args = false;
sc.for_each_kernel_arg([&](const TypeSpec& arg) {
has_args = true;
if (first_arg) {
first_arg = false;
} else {
os << ",\n";
}
os << " {\n";
os << " \"name\": \"" << arg.name << "\",\n";
os << " \"type\": \"" << arg.type << "\",\n";
// Array spec.
os << " \"is_array\": " << (arg.arr_spec ? "true" : "false") << ",\n";
if (arg.arr_spec) {
if (arg.arr_spec->count) {
os << " \"array_count\": " << arg.arr_spec->count << ",\n";
} else {
os << " \"array_multipliers\": [\n";
for (std::vector<std::string>::size_type index = 0;
index != arg.arr_spec->multipliers.size(); ++index) {
os << " \"" << arg.arr_spec->multipliers[index] << "\"";
if (index < arg.arr_spec->multipliers.size() - 1) {
os << ",";
}
os << "\n";
}
os << " ],\n";
}
}
// Attributes.
os << " \"attributes\": [\n";
for (std::vector<std::string>::size_type index = 0;
index != arg.attributes.size(); ++index) {
os << " \"" << arg.attributes[index] << "\"";
if (index < arg.attributes.size() - 1) {
os << ",";
}
os << "\n";
}
os << " ]\n";
os << " }";
});
if (has_args) {
os << "\n";
}
os << " ],\n";
os << " \"return_type\": \"" << sc.return_type() << "\"\n";
os << " }";
return os.good();
}