17Json python_annotation<Json>::create_name_annotation(
18 const std::string &type_id,
27 {
"ctx", {{
"_type",
"Load"}}},
29 {
"col_offset", col_offset},
30 {
"end_lineno", end_lineno},
31 {
"end_col_offset", end_col_offset}};
35Json python_annotation<Json>::create_subscript_annotation(
37 const std::string &element_type,
42 int base_end_col = col_offset +
base_type.size();
43 int slice_col = base_end_col + 1;
44 int slice_end_col = slice_col + element_type.size();
45 int total_end_col = col_offset +
base_type.size() + 1 + element_type.size() +
51 const std::vector<std::string> params =
58 auto build_elem = [&](
const std::string &t,
int col,
int end_col) -> Json {
59 return t.find(
'[') != std::string::npos
60 ? create_annotation_from_type(t, lineno, col, end_lineno)
61 : create_name_annotation(t,
lineno, col, end_lineno, end_col);
64 Json elts = Json::array();
66 for (
const std::string &t : params)
68 const int end_col = cur +
static_cast<int>(t.size());
69 elts.push_back(build_elem(t, cur, end_col));
75 {
"elts", std::move(elts)},
76 {
"ctx", {{
"_type",
"Load"}}},
78 {
"col_offset", slice_col},
79 {
"end_lineno", end_lineno},
80 {
"end_col_offset", slice_end_col}};
83 {
"_type",
"Subscript"},
85 create_name_annotation(
86 base_type, lineno, col_offset, end_lineno, base_end_col)},
87 {
"slice", tuple_slice},
88 {
"ctx", {{
"_type",
"Load"}}},
90 {
"col_offset", col_offset},
91 {
"end_lineno", end_lineno},
92 {
"end_col_offset", total_end_col}};
103 element_type.find(
'[') != std::string::npos
104 ? create_annotation_from_type(element_type, lineno, slice_col, end_lineno)
105 : create_name_annotation(
106 element_type,
lineno, slice_col, end_lineno, slice_end_col);
109 {
"_type",
"Subscript"},
111 create_name_annotation(
112 base_type, lineno, col_offset, end_lineno, base_end_col)},
113 {
"slice", slice_node},
114 {
"ctx", {{
"_type",
"Load"}}},
116 {
"col_offset", col_offset},
117 {
"end_lineno", end_lineno},
118 {
"end_col_offset", total_end_col}};
125Json python_annotation<Json>::create_tuple_subscript_annotation(
126 const std::vector<std::string> &elem_types,
131 static const std::string
base_type =
"tuple";
132 int base_end_col = col_offset +
base_type.size();
133 int slice_col = base_end_col + 1;
135 Json elts = Json::array();
137 for (
const std::string &t : elem_types)
139 int end_col = cur + t.size();
140 elts.push_back(create_name_annotation(t, lineno, cur, end_lineno, end_col));
143 int slice_end_col = cur - 2;
144 int total_end_col = slice_end_col + 1;
149 {
"ctx", {{
"_type",
"Load"}}},
151 {
"col_offset", slice_col},
152 {
"end_lineno", end_lineno},
153 {
"end_col_offset", slice_end_col}};
156 {
"_type",
"Subscript"},
158 create_name_annotation(
159 base_type, lineno, col_offset, end_lineno, base_end_col)},
161 {
"ctx", {{
"_type",
"Load"}}},
163 {
"col_offset", col_offset},
164 {
"end_lineno", end_lineno},
165 {
"end_col_offset", total_end_col}};
169Json python_annotation<Json>::create_annotation_from_type(
170 const std::string &inferred_type,
176 size_t bracket_pos = inferred_type.find(
'[');
178 if (bracket_pos != std::string::npos)
181 std::string
base_type = inferred_type.substr(0, bracket_pos);
182 std::string element_type = inferred_type.substr(
183 bracket_pos + 1, inferred_type.length() - bracket_pos - 2);
185 return create_subscript_annotation(
186 base_type, element_type, lineno, col_offset, end_lineno);
191 int end_col_offset = col_offset + inferred_type.size();
192 return create_name_annotation(
193 inferred_type, lineno, col_offset, end_lineno, end_col_offset);
198void python_annotation<Json>::update_assignment_node(
200 const std::string &inferred_type)
202 auto target = element[
"targets"][0];
206 if (target.contains(
"id"))
209 else if (target[
"_type"] ==
"Attribute")
215 if (!target[
"value"].is_object() || !target[
"value"].contains(
"id"))
217 id = target[
"value"][
"id"].template get<std::string>() +
"." +
218 target[
"attr"].template get<std::string>();
220 else if (target.contains(
"slice"))
224 element[
"_type"] =
"AnnAssign";
227 element[
"_inferred_annotation"] =
true;
232 int target_col_offset =
233 target.contains(
"col_offset") && !target[
"col_offset"].is_null()
234 ? target[
"col_offset"].template get<int>()
236 int col_offset = target_col_offset +
id.size() + 1;
239 int target_lineno = target.contains(
"lineno") && !target[
"lineno"].is_null()
240 ? target[
"lineno"].template get<int>()
243 int target_end_lineno =
244 target.contains(
"end_lineno") && !target[
"end_lineno"].is_null()
245 ? target[
"end_lineno"].template get<int>()
248 element[
"annotation"] = create_annotation_from_type(
249 inferred_type, target_lineno, col_offset, target_end_lineno);
252 int element_end_col_offset =
253 element.contains(
"end_col_offset") && !element[
"end_col_offset"].is_null()
254 ? element[
"end_col_offset"].template get<int>()
255 : col_offset + inferred_type.size();
257 element[
"end_col_offset"] = element_end_col_offset + inferred_type.size() + 1;
258 element[
"end_lineno"] = target_lineno;
259 element[
"simple"] = 1;
262 element[
"target"] = std::move(target);
263 element.erase(
"targets");
266 element.erase(
"type_comment");
269 auto update_offsets = [&inferred_type](Json &
value) {
270 if (
value.contains(
"col_offset") && !
value[
"col_offset"].is_null())
272 value[
"col_offset"] =
273 value[
"col_offset"].template get<int>() + inferred_type.size() + 1;
275 if (
value.contains(
"end_col_offset") && !
value[
"end_col_offset"].is_null())
277 value[
"end_col_offset"] =
278 value[
"end_col_offset"].template get<int>() + inferred_type.size() + 1;
282 update_offsets(element[
"value"]);
285 if (element[
"value"].contains(
"args"))
286 for (
auto &arg : element[
"value"][
"args"])
290 if (element[
"value"].contains(
"func"))
291 update_offsets(element[
"value"][
"func"]);
295void python_annotation<Json>::add_parameter_annotation(
297 const std::string &type)
299 int col_offset = param[
"col_offset"].template get<int>() +
300 param[
"arg"].template get<std::string>().length() + 1;
302 param[
"annotation"] = create_annotation_from_type(
304 param[
"lineno"].
template get<int>(),
306 param[
"lineno"].
template get<int>());
309 param[
"end_col_offset"] =
310 param[
"end_col_offset"].template get<int>() +
type.size() + 1;
314void python_annotation<Json>::update_end_col_offset(Json &ast)
316 int max_col_offset = ast[
"end_col_offset"];
317 for (
auto &elem : ast[
"body"])
319 if (elem[
"end_col_offset"] > max_col_offset)
320 max_col_offset = elem[
"end_col_offset"];
322 ast[
"end_col_offset"] = max_col_offset;
void base_type(type2tc &type, const namespacet &ns)
Definition base_type.cpp:6
std::vector< std::string > split_top_level_params(const std::string &s)
Split a rendered parameter list on commas that are not inside brackets, trimming each part.
Definition json_utils.h:982
list[float] empty(int shape)
Definition numpy.py:29
lineno
Definition threading_lowering.py:600