Skip to content

Specify pointer attributes in api.json #117

@Parzival-3141

Description

@Parzival-3141

Extracted from #116.
See also: orca-app/orca-zig#7.

Add an attributes field to pointer types, containing an array of strings signalling how the pointer is to be used:

"name": "str",
"doc": "A null-terminated string.",
"type": {
    "kind": "pointer",
    "attributes": ["many-item", "const", "null-terminated"],
    "type": {
        "kind": "char"
    }
}

Pointer attributes are treated like flags. The lack of an attribute in the array is the same as saying it's opposite is true (e.g. if nullable is not present, then the pointer must not be null). To simplify parsing, the attributes field must always be present, even if empty. This behaviour is similar to function parameters.

Existing attr fields will be converted to the new attribute format as well.

Here are the proposed pointer attributes:

  • many-item:
    • Enabled: points to an unknown number of items.
    • Disabled: points to a single item.
    • Use case: arrays, strings, buffers.
  • const:
    • Enabled: points to an immutable item.
    • Disabled: points to a mutable item.
    • Use case: signaling that functions will or will not modify parameters passed by reference.
  • nullable:
    • Enabled: pointer may be null.
    • Disabled: pointer cannot be null.
    • Use case: signaling what input is valid to a function or how to deal with it's output. Functions should assert non-nullable parameters are not null.
  • null-terminated:
    • Enabled: pointer's length is determined by a null sentinel value. many-item must be present as well.
    • Disabled: pointer has no sentinel value.
    • Use case: C strings.

The naming for const is a little confusing, since the subject is the child type rather than the pointer itself (unlike the other attributes). However I don't think it'll matter as long as the documentation is clear.

Effects

If implemented, the orca-zig project can make use of this feature to drastically improve type safety in it's bindings. I'm not as familiar with Odin, but I assume it's bindings will see similar benefits. Orca can also use this metadata to annotate the C headers, improving API clarity for C users too.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions