Render the canonical return type into `out`; returns how many qualifiers and * markers were added around the base type (0 = the base text alone is already * the whole type). The declarator walk is one strict child chain, so it is * O(depth) with no recursion and needs no depth cap. It stops at the first node * that is neither a pointer nor a reference declarator: for a function returning * a
| 5326 | * function_declarator, which leaves the base type as it was. That node is |
| 5327 | * handed back through `rest` when the caller asks for it. */ |
| 5328 | static size_t c_rt_render(c_rt_out_t *out, TSNode func_node, TSNode type_node, TSNode declarator, |
| 5329 | const char *source, TSNode *rest) { |
| 5330 | size_t added = 0; |
| 5331 | uint32_t decl_start = ts_node_start_byte(declarator); |
| 5332 | uint32_t nc = ts_node_named_child_count(func_node); |
| 5333 | for (uint32_t i = 0; i < nc; i++) { |
| 5334 | TSNode ch = ts_node_named_child(func_node, i); |
| 5335 | if (ts_node_start_byte(ch) >= decl_start) { |
| 5336 | break; |
| 5337 | } |
| 5338 | if (is_c_return_cv_qualifier(ch, source)) { |
| 5339 | c_rt_put_node(out, ch, source); |
| 5340 | c_rt_put_str(out, " "); |
| 5341 | added++; |
| 5342 | } |
| 5343 | } |
| 5344 | c_rt_put_node(out, type_node, source); |
| 5345 | |
| 5346 | bool need_space = true; |
| 5347 | TSNode decl = declarator; |
| 5348 | while (!ts_node_is_null(decl)) { |
| 5349 | const char *dk = ts_node_type(decl); |
| 5350 | bool is_ref = strcmp(dk, "reference_declarator") == 0; |
| 5351 | if (!is_ref && strcmp(dk, "pointer_declarator") != 0) { |
| 5352 | break; |
| 5353 | } |
| 5354 | if (need_space) { |
| 5355 | c_rt_put_str(out, " "); |
| 5356 | need_space = false; |
| 5357 | } |
| 5358 | /* A reference_declarator opens with its `&` / `&&` token. */ |
| 5359 | TSNode marker = ts_node_child(decl, 0); |
| 5360 | if (is_ref && !ts_node_is_null(marker) && !ts_node_is_named(marker)) { |
| 5361 | c_rt_put_node(out, marker, source); |
| 5362 | } else { |
| 5363 | c_rt_put_str(out, is_ref ? "&" : "*"); |
| 5364 | } |
| 5365 | added++; |
| 5366 | uint32_t dn = ts_node_named_child_count(decl); |
| 5367 | for (uint32_t i = 0; i < dn; i++) { |
| 5368 | TSNode q = ts_node_named_child(decl, i); |
| 5369 | if (is_c_return_cv_qualifier(q, source)) { |
| 5370 | c_rt_put_node(out, q, source); |
| 5371 | need_space = true; |
| 5372 | added++; |
| 5373 | } |
| 5374 | } |
| 5375 | /* tree-sitter-cpp/-cuda give a reference_declarator's inner declarator no |
| 5376 | * `declarator` field (see find_c_params); it is the one named child. */ |
| 5377 | TSNode inner = ts_node_child_by_field_name(decl, TS_FIELD("declarator")); |
| 5378 | if (ts_node_is_null(inner) && is_ref && dn > 0) { |
| 5379 | inner = ts_node_named_child(decl, 0); |
| 5380 | } |
| 5381 | decl = inner; |
| 5382 | } |
| 5383 | if (rest) { |
| 5384 | *rest = decl; /* the declarator the markers wrap; null when none is left */ |
| 5385 | } |
no test coverage detected