Skip to content
This repository was archived by the owner on Jun 16, 2021. It is now read-only.

Conversation

uucidl
Copy link

@uucidl uucidl commented Oct 1, 2018

Merge branch 'add-enum-typeinfos'

This introduces type information for enum types.

The ion program can now know that a type is an enum, and what is its base
type and size as well as the values of available constants in the enum
(see enum_items and num_enum_items)

This allows user programs to print the friendly enumeration constant
associated with a value, or validate that a value is a legal one.

The name of the constant is the external C name, i.e. as mangled into
the C symbols, similarly to how type names are also introspected. This
guarantees unicity, however means that a ion program looking to generate
new ion programs would have to reverse the manging somehow. The same issue
is true for existing type names however.

Note that this creates a distinction between enum constants and plain
constants: enum constants are known to the program and can be shared
outside. This matches my experience of using enumerations in applications.

Initially I wanted to supply sorted arrays for an enumeration type by
ame and value, to make lookups more efficient. However to do this at
compile time would necessitate the ion compiler to evaluate the enum
expression. Right now the value of the expression is only known to the ion program
(after the C compiler has done its job)

So fast lookups (sub-O(n)) for large enumerations will have to be done by hand in
the ion program using raw type infos for now.

uucidl added 2 commits October 1, 2018 09:12
We populate the `base` field with the type of the enum.

I.e.

```ion
enum Foo = int8 { A, B, C }
#assert(get_typeinfo(typeof(Foo)).base == typeof(int8))
```
This allows printing the constant corresponding to the value,
or simply enumerating the constants.

Note however that the name corresponds to the externally
known name, (i.e. C name) rather than the ion name.
@uucidl
Copy link
Author

uucidl commented Oct 1, 2018

@pervognsen this PR is more of an example of how this could be done for followers of this repository. I'm also curious how you would export this information compared to this solution.

I can create this as an issue instead, maybe it fits the question format better. Let me know.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant