diff --git a/parser/shapeinfer.py b/parser/shapeinfer.py index 2c436c6..22ebed2 100644 --- a/parser/shapeinfer.py +++ b/parser/shapeinfer.py @@ -108,6 +108,16 @@ def _bare(ctype: str) -> str: return " ".join((ctype or "").replace("const ", "").split()) +def _is_array_param(prm: dict) -> bool: + """Whether a parameter is an array by its type alone, as #_input_arrays reads a run: a + pointer to pointers other than a string array or an untyped one, or a pointer to a + scalar element.""" + ctype = _bare(prm.get("cType")) + if ctype.endswith("**"): + return ctype not in ("char **", "void **") + return ctype.endswith("*") and ctype[:-1].strip() in _ELEMENT_SCALARS + + def _input_arrays(func: dict) -> list: """The array ARGUMENTS a function reads, with the parameter each takes its length from. @@ -133,13 +143,7 @@ def _input_arrays(func: dict) -> list: keeps ``arr1`` on ``count1``. """ params = func.get("params", []) - - def is_array(prm) -> bool: - ctype = _bare(prm.get("cType")) - if ctype.endswith("**"): - return ctype not in ("char **", "void **") - return ctype.endswith("*") and ctype[:-1].strip() in _ELEMENT_SCALARS - + is_array = _is_array_param out = [] start = 0 while start < len(params): @@ -191,7 +195,19 @@ def attach_struct_input_arrays(idl: dict, docs: dict) -> tuple[dict, int]: shape = func.get("shape") or {} known = {a["param"] for a in shape.get("inputArrays") or ()} found = [] - for prm, nxt in zip(params, params[1:]): + + def array(prm, count): + return { + "param": prm["name"], + "lengthFrom": {"kind": "param", "name": count}, + "element": { + "c": _strip_one_ptr(_bare(prm.get("cType"))), + "canonical": _strip_one_ptr( + _bare(prm.get("canonical") or prm.get("cType"))), + }, + } + + for i, (prm, nxt) in enumerate(zip(params, params[1:])): ctype = _bare(prm.get("cType")) if (prm["name"] in known or not ctype.endswith("*") or ctype.endswith("**") or _bare(nxt.get("cType")) not in _LENGTH_TYPES): @@ -199,15 +215,17 @@ def attach_struct_input_arrays(idl: dict, docs: dict) -> tuple[dict, int]: if not (_ARRAY_DOC.match(fdocs.get(prm["name"], "")) and _COUNT_DOC.match(fdocs.get(nxt["name"], ""))): continue - found.append({ - "param": prm["name"], - "lengthFrom": {"kind": "param", "name": nxt["name"]}, - "element": { - "c": _strip_one_ptr(ctype), - "canonical": _strip_one_ptr( - _bare(prm.get("canonical") or prm.get("cType"))), - }, - }) + # The arrays documented as such just before it are of the same run, which + # #_input_arrays ends at the array of structs: raster_value_series(traj, + # rasts, spans, count) reads count rasters and count spans. + run = [] + for before in reversed(params[:i]): + if (before["name"] in known or not _is_array_param(before) + or not _ARRAY_DOC.match(fdocs.get(before["name"], ""))): + break + run.insert(0, array(before, nxt["name"])) + found.extend(run) + found.append(array(prm, nxt["name"])) if found: func.setdefault("shape", {}).setdefault("inputArrays", []).extend(found) n += len(found) diff --git a/tests/test_shapeinfer.py b/tests/test_shapeinfer.py index 0762884..6bc17cb 100644 --- a/tests/test_shapeinfer.py +++ b/tests/test_shapeinfer.py @@ -261,6 +261,40 @@ def test_one_struct_and_an_integer_is_no_array(self): self.assertEqual(n, 0) self.assertNotIn("shape", idl["functions"][0]) + def test_the_arrays_documented_before_an_array_of_structs_share_its_count(self): + """As #test_a_documented_array_of_structs_is_an_input_array, with the run of + arrays #_input_arrays ends at the array of structs before it.""" + idl = {"functions": [_fn("raster_value_series", "Temporal *", + [("traj", "const Temporal *"), + ("rasts", "const struct Raster **"), + ("spans", "const Span *"), ("count", "int"), + ("band", "int")])]} + docs = {"raster_value_series": { + "traj": "Trajectory (temporal geometry point)", + "rasts": "Array of the rasters of the series", + "spans": "Array of the validity periods of the rasters, one per raster", + "count": "Number of elements in the arrays"}} + idl, n = attach_struct_input_arrays(idl, docs) + self.assertEqual(n, 2) + self.assertEqual( + [(a["param"], a["lengthFrom"]["name"], a["element"]["canonical"]) + for a in idl["functions"][0]["shape"]["inputArrays"]], + [("rasts", "count", "struct Raster *"), ("spans", "count", "Span")]) + + def test_a_pointer_before_an_array_of_structs_documented_otherwise_is_no_array(self): + """As #test_one_struct_and_an_integer_is_no_array: the documentation, not the + type alone, makes the pointer before the array of structs one of its run.""" + idl = {"functions": [_fn("f", "Temporal *", + [("rasts", "const struct Raster **"), + ("spans", "const Span *"), ("count", "int")])]} + docs = {"f": {"rasts": "Rasters of the series", + "spans": "Array of spans", + "count": "Number of elements in the array"}} + idl, n = attach_struct_input_arrays(idl, docs) + self.assertEqual(n, 1) + self.assertEqual([a["param"] for a in idl["functions"][0]["shape"]["inputArrays"]], + ["spans"]) + def test_an_array_read_at_an_index_is_no_input_array(self): idl = {"functions": [_fn("meos_array_get", "void *", [("array", "MeosArray *"), ("n", "int")])]}