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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/tl/tl_writer.h"
#include "td/tl/tl_core.h"
#include <cassert>
#include <cstdio>
namespace td {
namespace tl {
std::string TL_writer::int_to_string(int x) {
char buf[15];
std::snprintf(buf, sizeof(buf), "%d", x);
return buf;
}
bool TL_writer::is_alnum(char c) {
return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
}
char TL_writer::to_lower(char c) {
return 'A' <= c && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c;
}
char TL_writer::to_upper(char c) {
return 'a' <= c && c <= 'z' ? static_cast<char>(c - 'a' + 'A') : c;
}
std::vector<std::string> TL_writer::get_additional_functions() const {
return std::vector<std::string>();
}
bool TL_writer::is_type_supported(const tl_tree_type *tree_type) const {
if (tree_type->type->flags & FLAG_COMPLEX) {
return false;
}
for (std::size_t i = 0; i < tree_type->children.size(); i++) {
const tl_tree *child = tree_type->children[i];
assert(child->get_type() == NODE_TYPE_TYPE || child->get_type() == NODE_TYPE_VAR_TYPE ||
child->get_type() == NODE_TYPE_NAT_CONST || child->get_type() == NODE_TYPE_VAR_NUM);
if (child->get_type() == NODE_TYPE_TYPE) {
if (!is_type_supported(static_cast<const tl_tree_type *>(child))) {
return false;
}
}
if (child->get_type() == NODE_TYPE_VAR_TYPE) {
return false; // TODO
}
}
return true;
}
bool TL_writer::is_combinator_supported(const tl_combinator *constructor) const {
std::vector<bool> is_function_result(constructor->var_count);
for (std::size_t i = 0; i < constructor->args.size(); i++) {
const arg &a = constructor->args[i];
int arg_type = a.type->get_type();
if (arg_type == NODE_TYPE_VAR_TYPE) {
const tl_tree_var_type *t = static_cast<const tl_tree_var_type *>(a.type);
if (a.flags & FLAG_EXCL) {
assert(t->var_num >= 0);
if (is_function_result[t->var_num]) {
return false; // lazy to check that result of two function calls is the same
}
is_function_result[t->var_num] = true;
} else {
return false; // do not support generic types
}
}
}
for (std::size_t i = 0; i < constructor->args.size(); i++) {
const arg &a = constructor->args[i];
int arg_type = a.type->get_type();
if (a.var_num >= 0) {
assert(arg_type == NODE_TYPE_TYPE);
const tl_tree_type *a_type = static_cast<const tl_tree_type *>(a.type);
if (a_type->type->id == ID_VAR_TYPE) {
assert(!(a_type->flags & FLAG_EXCL));
if (!is_function_result[a.var_num]) {
assert(false); // not possible, otherwise type is an argument of a type, but all types with type arguments
// are already marked complex
return false;
} else {
continue;
}
}
}
if (arg_type == NODE_TYPE_VAR_TYPE) {
continue;
} else if (arg_type == NODE_TYPE_TYPE) {
if (!is_type_supported(static_cast<const tl_tree_type *>(a.type))) {
return false;
}
} else {
assert(arg_type == NODE_TYPE_ARRAY);
const tl_tree_array *arr = static_cast<const tl_tree_array *>(a.type);
for (std::size_t j = 0; j < arr->args.size(); j++) {
const arg &b = arr->args[j];
assert(b.type->get_type() == NODE_TYPE_TYPE && b.var_num == -1);
if (!is_type_supported(static_cast<const tl_tree_type *>(b.type))) {
return false;
}
}
}
}
tl_tree *result = constructor->result;
if (result->get_type() == NODE_TYPE_TYPE) {
if (!is_type_supported(static_cast<const tl_tree_type *>(result))) {
return false;
}
} else {
assert(result->get_type() == NODE_TYPE_VAR_TYPE);
const tl_tree_var_type *t = static_cast<const tl_tree_var_type *>(result);
return is_function_result[t->var_num];
}
return true;
}
bool TL_writer::is_documentation_generated() const {
return false;
}
bool TL_writer::is_default_constructor_generated(const tl_combinator *t, bool is_function) const {
return true;
}
std::string TL_writer::gen_main_class_name(const tl_type *t) const {
if (t->simple_constructors == 1) {
for (std::size_t i = 0; i < t->constructors_num; i++) {
if (is_combinator_supported(t->constructors[i])) {
return gen_class_name(t->constructors[i]->name);
}
}
}
return gen_class_name(t->name);
}
int TL_writer::get_parser_type(const tl_combinator *t, const std::string &parser_name) const {
return t->var_count > 0;
}
int TL_writer::get_storer_type(const tl_combinator *t, const std::string &storer_name) const {
return 0;
}
int TL_writer::get_additional_function_type(const std::string &additional_function_name) const {
return 0;
}
TL_writer::Mode TL_writer::get_parser_mode(int type) const {
return All;
}
TL_writer::Mode TL_writer::get_storer_mode(int type) const {
return All;
}
std::string TL_writer::gen_field_type(const arg &a) const {
if (a.flags & FLAG_EXCL) {
assert(a.flags == FLAG_EXCL);
assert(a.type->get_type() == NODE_TYPE_VAR_TYPE);
return gen_var_type_name();
}
assert(a.flags == FLAG_NOVAR || a.flags == 0 || a.flags == (FLAG_OPT_VAR | FLAG_NOVAR | FLAG_BARE));
if (a.type->get_type() == NODE_TYPE_TYPE) {
const tl_tree_type *arg_type = static_cast<const tl_tree_type *>(a.type);
assert(arg_type->children.size() == static_cast<std::size_t>(arg_type->type->arity));
if (arg_type->type->id == ID_VAR_TYPE) {
return std::string();
}
return gen_type_name(arg_type);
} else {
assert(a.flags == FLAG_NOVAR || a.flags == 0);
assert(a.type->get_type() == NODE_TYPE_ARRAY);
const tl_tree_array *arg_array = static_cast<const tl_tree_array *>(a.type);
assert((arg_array->flags & ~FLAG_NOVAR) == 0);
return gen_array_type_name(arg_array, a.name);
}
}
std::string TL_writer::gen_additional_function(const std::string &function_name, const tl_combinator *t,
bool is_function) const {
assert(false);
return "";
}
std::string TL_writer::gen_additional_proxy_function_begin(const std::string &function_name, const tl_type *type,
const std::string &class_name, int arity,
bool is_function) const {
assert(false);
return "";
}
std::string TL_writer::gen_additional_proxy_function_case(const std::string &function_name, const tl_type *type,
const std::string &class_name, int arity) const {
assert(false);
return "";
}
std::string TL_writer::gen_additional_proxy_function_case(const std::string &function_name, const tl_type *type,
const tl_combinator *t, int arity, bool is_function) const {
assert(false);
return "";
}
std::string TL_writer::gen_additional_proxy_function_end(const std::string &function_name, const tl_type *type,
bool is_function) const {
assert(false);
return "";
}
} // namespace tl
} // namespace td
|