Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two extra annotations: donotinclude and typedefto #42

Merged
merged 3 commits into from
May 10, 2021

Conversation

detly
Copy link
Collaborator

@detly detly commented Jan 2, 2021

This adds two extra annotations:

donotinclude

Currently the code generator assumes that every schema import statement requires a #include directive in the corresponding generated code. However this is not always the case eg. a schema that contains only annotation declarations doesn't generate any code. The best example of this is c.capnp.h itself (see #22) but also other language bindings eg. Rust's rust.capnp contains only annotations too.

Ideally the generator could detect this and suppress #include directives for any such schema file, however I couldn't figure out how to do this without multiple passes over input files and massive refactoring. The next best solution I could come up with was to have an annotation at the file level that declares that a particular ID should not result in a #include statement. For example, in the user's schema, they would have:

using C = import "/capnp/c.capnp";

$C.donotinclude(0xc0183dd65ffef0f3);

Note that 0xc0183dd65ffef0f3 is the file ID for c.capnp. It does not matter whether the donotinclude annotation comes before or after the import statement. Multiple annotations are fine:

using C = import "/capnp/c.capnp";
using Rust = import "rust.capnp";

$C.donotinclude(0xc0183dd65ffef0f3);
$C.donotinclude(0x83b3c14c3c8dd083);

typedefto

This is purely cosmetic. This annotation lets you declare that a struct or enum must have a typedef generated for it with a given name. For example:

enum FanSpeed $C.typedefto("fan_speed_t") {
    high @0;
    low @1;
}

...will result in the following typedef:

typedef enum FanSpeed fan_speed_t;

I did this so that code can look a little more consistent with local style guides at the calling sites. Eventually it might be nice to implement a full camelCase/snake_case conversion, but that is a great deal more work.

It does not support doing this for the "which" enums created for unions.

schema files.

Some schema files (eg. those that only decalare annotations) do not actually
result in any generated C code. They do not need to have a corresponding
include directive for C files generated from schemas that include them. This
introduces a "donotinclude" annotation that takes the Cap'n Proto ID (a
UInt64) of any such files and skips generating the include directive for them.
A new annotation 'typedefto' allows you to make a typedef in the generated
code for structs and enums (but not union 'which' enums).
@detly detly merged commit 024dca6 into opensourcerouting:master May 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant