capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

schema.capnp (18724B)


      1 # Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
      2 # Licensed under the MIT License:
      3 #
      4 # Permission is hereby granted, free of charge, to any person obtaining a copy
      5 # of this software and associated documentation files (the "Software"), to deal
      6 # in the Software without restriction, including without limitation the rights
      7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      8 # copies of the Software, and to permit persons to whom the Software is
      9 # furnished to do so, subject to the following conditions:
     10 #
     11 # The above copyright notice and this permission notice shall be included in
     12 # all copies or substantial portions of the Software.
     13 #
     14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     20 # THE SOFTWARE.
     21 
     22 using Cxx = import "/capnp/c++.capnp";
     23 
     24 @0xa93fc509624c72d9;
     25 $Cxx.namespace("capnp::schema");
     26 
     27 using Id = UInt64;
     28 # The globally-unique ID of a file, type, or annotation.
     29 
     30 struct Node {
     31   id @0 :Id;
     32 
     33   displayName @1 :Text;
     34   # Name to present to humans to identify this Node.  You should not attempt to parse this.  Its
     35   # format could change.  It is not guaranteed to be unique.
     36   #
     37   # (On Zooko's triangle, this is the node's nickname.)
     38 
     39   displayNamePrefixLength @2 :UInt32;
     40   # If you want a shorter version of `displayName` (just naming this node, without its surrounding
     41   # scope), chop off this many characters from the beginning of `displayName`.
     42 
     43   scopeId @3 :Id;
     44   # ID of the lexical parent node.  Typically, the scope node will have a NestedNode pointing back
     45   # at this node, but robust code should avoid relying on this (and, in fact, group nodes are not
     46   # listed in the outer struct's nestedNodes, since they are listed in the fields).  `scopeId` is
     47   # zero if the node has no parent, which is normally only the case with files, but should be
     48   # allowed for any kind of node (in order to make runtime type generation easier).
     49 
     50   parameters @32 :List(Parameter);
     51   # If this node is parameterized (generic), the list of parameters. Empty for non-generic types.
     52 
     53   isGeneric @33 :Bool;
     54   # True if this node is generic, meaning that it or one of its parent scopes has a non-empty
     55   # `parameters`.
     56 
     57   struct Parameter {
     58     # Information about one of the node's parameters.
     59 
     60     name @0 :Text;
     61   }
     62 
     63   nestedNodes @4 :List(NestedNode);
     64   # List of nodes nested within this node, along with the names under which they were declared.
     65 
     66   struct NestedNode {
     67     name @0 :Text;
     68     # Unqualified symbol name.  Unlike Node.displayName, this *can* be used programmatically.
     69     #
     70     # (On Zooko's triangle, this is the node's petname according to its parent scope.)
     71 
     72     id @1 :Id;
     73     # ID of the nested node.  Typically, the target node's scopeId points back to this node, but
     74     # robust code should avoid relying on this.
     75   }
     76 
     77   annotations @5 :List(Annotation);
     78   # Annotations applied to this node.
     79 
     80   union {
     81     # Info specific to each kind of node.
     82 
     83     file @6 :Void;
     84 
     85     struct :group {
     86       dataWordCount @7 :UInt16;
     87       # Size of the data section, in words.
     88 
     89       pointerCount @8 :UInt16;
     90       # Size of the pointer section, in pointers (which are one word each).
     91 
     92       preferredListEncoding @9 :ElementSize;
     93       # The preferred element size to use when encoding a list of this struct.  If this is anything
     94       # other than `inlineComposite` then the struct is one word or less in size and is a candidate
     95       # for list packing optimization.
     96 
     97       isGroup @10 :Bool;
     98       # If true, then this "struct" node is actually not an independent node, but merely represents
     99       # some named union or group within a particular parent struct.  This node's scopeId refers
    100       # to the parent struct, which may itself be a union/group in yet another struct.
    101       #
    102       # All group nodes share the same dataWordCount and pointerCount as the top-level
    103       # struct, and their fields live in the same ordinal and offset spaces as all other fields in
    104       # the struct.
    105       #
    106       # Note that a named union is considered a special kind of group -- in fact, a named union
    107       # is exactly equivalent to a group that contains nothing but an unnamed union.
    108 
    109       discriminantCount @11 :UInt16;
    110       # Number of fields in this struct which are members of an anonymous union, and thus may
    111       # overlap.  If this is non-zero, then a 16-bit discriminant is present indicating which
    112       # of the overlapping fields is active.  This can never be 1 -- if it is non-zero, it must be
    113       # two or more.
    114       #
    115       # Note that the fields of an unnamed union are considered fields of the scope containing the
    116       # union -- an unnamed union is not its own group.  So, a top-level struct may contain a
    117       # non-zero discriminant count.  Named unions, on the other hand, are equivalent to groups
    118       # containing unnamed unions.  So, a named union has its own independent schema node, with
    119       # `isGroup` = true.
    120 
    121       discriminantOffset @12 :UInt32;
    122       # If `discriminantCount` is non-zero, this is the offset of the union discriminant, in
    123       # multiples of 16 bits.
    124 
    125       fields @13 :List(Field);
    126       # Fields defined within this scope (either the struct's top-level fields, or the fields of
    127       # a particular group; see `isGroup`).
    128       #
    129       # The fields are sorted by ordinal number, but note that because groups share the same
    130       # ordinal space, the field's index in this list is not necessarily exactly its ordinal.
    131       # On the other hand, the field's position in this list does remain the same even as the
    132       # protocol evolves, since it is not possible to insert or remove an earlier ordinal.
    133       # Therefore, for most use cases, if you want to identify a field by number, it may make the
    134       # most sense to use the field's index in this list rather than its ordinal.
    135     }
    136 
    137     enum :group {
    138       enumerants@14 :List(Enumerant);
    139       # Enumerants ordered by numeric value (ordinal).
    140     }
    141 
    142     interface :group {
    143       methods @15 :List(Method);
    144       # Methods ordered by ordinal.
    145 
    146       superclasses @31 :List(Superclass);
    147       # Superclasses of this interface.
    148     }
    149 
    150     const :group {
    151       type @16 :Type;
    152       value @17 :Value;
    153     }
    154 
    155     annotation :group {
    156       type @18 :Type;
    157 
    158       targetsFile @19 :Bool;
    159       targetsConst @20 :Bool;
    160       targetsEnum @21 :Bool;
    161       targetsEnumerant @22 :Bool;
    162       targetsStruct @23 :Bool;
    163       targetsField @24 :Bool;
    164       targetsUnion @25 :Bool;
    165       targetsGroup @26 :Bool;
    166       targetsInterface @27 :Bool;
    167       targetsMethod @28 :Bool;
    168       targetsParam @29 :Bool;
    169       targetsAnnotation @30 :Bool;
    170     }
    171   }
    172 
    173   struct SourceInfo {
    174     # Additional information about a node which is not needed at runtime, but may be useful for
    175     # documentation or debugging purposes. This is kept in a separate struct to make sure it
    176     # doesn't accidentally get included in contexts where it is not needed. The
    177     # `CodeGeneratorRequest` includes this information in a separate array.
    178 
    179     id @0 :Id;
    180     # ID of the Node which this info describes.
    181 
    182     docComment @1 :Text;
    183     # The top-level doc comment for the Node.
    184 
    185     members @2 :List(Member);
    186     # Information about each member -- i.e. fields (for structs), enumerants (for enums), or
    187     # methods (for interfaces).
    188     #
    189     # This list is the same length and order as the corresponding list in the Node, i.e.
    190     # Node.struct.fields, Node.enum.enumerants, or Node.interface.methods.
    191 
    192     struct Member {
    193       docComment @0 :Text;
    194       # Doc comment on the member.
    195     }
    196 
    197     # TODO(someday): Record location of the declaration in the original source code.
    198   }
    199 }
    200 
    201 struct Field {
    202   # Schema for a field of a struct.
    203 
    204   name @0 :Text;
    205 
    206   codeOrder @1 :UInt16;
    207   # Indicates where this member appeared in the code, relative to other members.
    208   # Code ordering may have semantic relevance -- programmers tend to place related fields
    209   # together.  So, using code ordering makes sense in human-readable formats where ordering is
    210   # otherwise irrelevant, like JSON.  The values of codeOrder are tightly-packed, so the maximum
    211   # value is count(members) - 1.  Fields that are members of a union are only ordered relative to
    212   # the other members of that union, so the maximum value there is count(union.members).
    213 
    214   annotations @2 :List(Annotation);
    215 
    216   const noDiscriminant :UInt16 = 0xffff;
    217 
    218   discriminantValue @3 :UInt16 = Field.noDiscriminant;
    219   # If the field is in a union, this is the value which the union's discriminant should take when
    220   # the field is active.  If the field is not in a union, this is 0xffff.
    221 
    222   union {
    223     slot :group {
    224       # A regular, non-group, non-fixed-list field.
    225 
    226       offset @4 :UInt32;
    227       # Offset, in units of the field's size, from the beginning of the section in which the field
    228       # resides.  E.g. for a UInt32 field, multiply this by 4 to get the byte offset from the
    229       # beginning of the data section.
    230 
    231       type @5 :Type;
    232       defaultValue @6 :Value;
    233 
    234       hadExplicitDefault @10 :Bool;
    235       # Whether the default value was specified explicitly.  Non-explicit default values are always
    236       # zero or empty values.  Usually, whether the default value was explicit shouldn't matter.
    237       # The main use case for this flag is for structs representing method parameters:
    238       # explicitly-defaulted parameters may be allowed to be omitted when calling the method.
    239     }
    240 
    241     group :group {
    242       # A group.
    243 
    244       typeId @7 :Id;
    245       # The ID of the group's node.
    246     }
    247   }
    248 
    249   ordinal :union {
    250     implicit @8 :Void;
    251     explicit @9 :UInt16;
    252     # The original ordinal number given to the field.  You probably should NOT use this; if you need
    253     # a numeric identifier for a field, use its position within the field array for its scope.
    254     # The ordinal is given here mainly just so that the original schema text can be reproduced given
    255     # the compiled version -- i.e. so that `capnp compile -ocapnp` can do its job.
    256   }
    257 }
    258 
    259 struct Enumerant {
    260   # Schema for member of an enum.
    261 
    262   name @0 :Text;
    263 
    264   codeOrder @1 :UInt16;
    265   # Specifies order in which the enumerants were declared in the code.
    266   # Like Struct.Field.codeOrder.
    267 
    268   annotations @2 :List(Annotation);
    269 }
    270 
    271 struct Superclass {
    272   id @0 :Id;
    273   brand @1 :Brand;
    274 }
    275 
    276 struct Method {
    277   # Schema for method of an interface.
    278 
    279   name @0 :Text;
    280 
    281   codeOrder @1 :UInt16;
    282   # Specifies order in which the methods were declared in the code.
    283   # Like Struct.Field.codeOrder.
    284 
    285   implicitParameters @7 :List(Node.Parameter);
    286   # The parameters listed in [] (typically, type / generic parameters), whose bindings are intended
    287   # to be inferred rather than specified explicitly, although not all languages support this.
    288 
    289   paramStructType @2 :Id;
    290   # ID of the parameter struct type.  If a named parameter list was specified in the method
    291   # declaration (rather than a single struct parameter type) then a corresponding struct type is
    292   # auto-generated.  Such an auto-generated type will not be listed in the interface's
    293   # `nestedNodes` and its `scopeId` will be zero -- it is completely detached from the namespace.
    294   # (Awkwardly, it does of course inherit generic parameters from the method's scope, which makes
    295   # this a situation where you can't just climb the scope chain to find where a particular
    296   # generic parameter was introduced. Making the `scopeId` zero was a mistake.)
    297 
    298   paramBrand @5 :Brand;
    299   # Brand of param struct type.
    300 
    301   resultStructType @3 :Id;
    302   # ID of the return struct type; similar to `paramStructType`.
    303 
    304   resultBrand @6 :Brand;
    305   # Brand of result struct type.
    306 
    307   annotations @4 :List(Annotation);
    308 }
    309 
    310 struct Type {
    311   # Represents a type expression.
    312 
    313   union {
    314     # The ordinals intentionally match those of Value.
    315 
    316     void @0 :Void;
    317     bool @1 :Void;
    318     int8 @2 :Void;
    319     int16 @3 :Void;
    320     int32 @4 :Void;
    321     int64 @5 :Void;
    322     uint8 @6 :Void;
    323     uint16 @7 :Void;
    324     uint32 @8 :Void;
    325     uint64 @9 :Void;
    326     float32 @10 :Void;
    327     float64 @11 :Void;
    328     text @12 :Void;
    329     data @13 :Void;
    330 
    331     list :group {
    332       elementType @14 :Type;
    333     }
    334 
    335     enum :group {
    336       typeId @15 :Id;
    337       brand @21 :Brand;
    338     }
    339     struct :group {
    340       typeId @16 :Id;
    341       brand @22 :Brand;
    342     }
    343     interface :group {
    344       typeId @17 :Id;
    345       brand @23 :Brand;
    346     }
    347 
    348     anyPointer :union {
    349       unconstrained :union {
    350         # A regular AnyPointer.
    351         #
    352         # The name "unconstrained" means as opposed to constraining it to match a type parameter.
    353         # In retrospect this name is probably a poor choice given that it may still be constrained
    354         # to be a struct, list, or capability.
    355 
    356         anyKind @18 :Void;       # truly AnyPointer
    357         struct @25 :Void;        # AnyStruct
    358         list @26 :Void;          # AnyList
    359         capability @27 :Void;    # Capability
    360       }
    361 
    362       parameter :group {
    363         # This is actually a reference to a type parameter defined within this scope.
    364 
    365         scopeId @19 :Id;
    366         # ID of the generic type whose parameter we're referencing. This should be a parent of the
    367         # current scope.
    368 
    369         parameterIndex @20 :UInt16;
    370         # Index of the parameter within the generic type's parameter list.
    371       }
    372 
    373       implicitMethodParameter :group {
    374         # This is actually a reference to an implicit (generic) parameter of a method. The only
    375         # legal context for this type to appear is inside Method.paramBrand or Method.resultBrand.
    376 
    377         parameterIndex @24 :UInt16;
    378       }
    379     }
    380   }
    381 }
    382 
    383 struct Brand {
    384   # Specifies bindings for parameters of generics. Since these bindings turn a generic into a
    385   # non-generic, we call it the "brand".
    386 
    387   scopes @0 :List(Scope);
    388   # For each of the target type and each of its parent scopes, a parameterization may be included
    389   # in this list. If no parameterization is included for a particular relevant scope, then either
    390   # that scope has no parameters or all parameters should be considered to be `AnyPointer`.
    391 
    392   struct Scope {
    393     scopeId @0 :Id;
    394     # ID of the scope to which these params apply.
    395 
    396     union {
    397       bind @1 :List(Binding);
    398       # List of parameter bindings.
    399 
    400       inherit @2 :Void;
    401       # The place where the Brand appears is within this scope or a sub-scope, and bindings
    402       # for this scope are deferred to later Brand applications. This is equivalent to a
    403       # pass-through binding list, where each of this scope's parameters is bound to itself.
    404       # For example:
    405       #
    406       #   struct Outer(T) {
    407       #     struct Inner {
    408       #       value @0 :T;
    409       #     }
    410       #     innerInherit @0 :Inner;            # Outer Brand.Scope is `inherit`.
    411       #     innerBindSelf @1 :Outer(T).Inner;  # Outer Brand.Scope explicitly binds T to T.
    412       #   }
    413       #
    414       # The innerInherit and innerBindSelf fields have equivalent types, but different Brand
    415       # styles.
    416     }
    417   }
    418 
    419   struct Binding {
    420     union {
    421       unbound @0 :Void;
    422       type @1 :Type;
    423 
    424       # TODO(someday): Allow non-type parameters? Unsure if useful.
    425     }
    426   }
    427 }
    428 
    429 struct Value {
    430   # Represents a value, e.g. a field default value, constant value, or annotation value.
    431 
    432   union {
    433     # The ordinals intentionally match those of Type.
    434 
    435     void @0 :Void;
    436     bool @1 :Bool;
    437     int8 @2 :Int8;
    438     int16 @3 :Int16;
    439     int32 @4 :Int32;
    440     int64 @5 :Int64;
    441     uint8 @6 :UInt8;
    442     uint16 @7 :UInt16;
    443     uint32 @8 :UInt32;
    444     uint64 @9 :UInt64;
    445     float32 @10 :Float32;
    446     float64 @11 :Float64;
    447     text @12 :Text;
    448     data @13 :Data;
    449 
    450     list @14 :AnyPointer;
    451 
    452     enum @15 :UInt16;
    453     struct @16 :AnyPointer;
    454 
    455     interface @17 :Void;
    456     # The only interface value that can be represented statically is "null", whose methods always
    457     # throw exceptions.
    458 
    459     anyPointer @18 :AnyPointer;
    460   }
    461 }
    462 
    463 struct Annotation {
    464   # Describes an annotation applied to a declaration.  Note AnnotationNode describes the
    465   # annotation's declaration, while this describes a use of the annotation.
    466 
    467   id @0 :Id;
    468   # ID of the annotation node.
    469 
    470   brand @2 :Brand;
    471   # Brand of the annotation.
    472   #
    473   # Note that the annotation itself is not allowed to be parameterized, but its scope might be.
    474 
    475   value @1 :Value;
    476 }
    477 
    478 enum ElementSize {
    479   # Possible element sizes for encoded lists.  These correspond exactly to the possible values of
    480   # the 3-bit element size component of a list pointer.
    481 
    482   empty @0;    # aka "void", but that's a keyword.
    483   bit @1;
    484   byte @2;
    485   twoBytes @3;
    486   fourBytes @4;
    487   eightBytes @5;
    488   pointer @6;
    489   inlineComposite @7;
    490 }
    491 
    492 struct CapnpVersion {
    493   major @0 :UInt16;
    494   minor @1 :UInt8;
    495   micro @2 :UInt8;
    496 }
    497 
    498 struct CodeGeneratorRequest {
    499   capnpVersion @2 :CapnpVersion;
    500   # Version of the `capnp` executable. Generally, code generators should ignore this, but the code
    501   # generators that ship with `capnp` itself will print a warning if this mismatches since that
    502   # probably indicates something is misconfigured.
    503   #
    504   # The first version of 'capnp' to set this was 0.6.0. So, if it's missing, the compiler version
    505   # is older than that.
    506 
    507   nodes @0 :List(Node);
    508   # All nodes parsed by the compiler, including for the files on the command line and their
    509   # imports.
    510 
    511   sourceInfo @3 :List(Node.SourceInfo);
    512   # Information about the original source code for each node, where available. This array may be
    513   # omitted or may be missing some nodes if no info is available for them.
    514 
    515   requestedFiles @1 :List(RequestedFile);
    516   # Files which were listed on the command line.
    517 
    518   struct RequestedFile {
    519     id @0 :Id;
    520     # ID of the file.
    521 
    522     filename @1 :Text;
    523     # Name of the file as it appeared on the command-line (minus the src-prefix).  You may use
    524     # this to decide where to write the output.
    525 
    526     imports @2 :List(Import);
    527     # List of all imported paths seen in this file.
    528 
    529     struct Import {
    530       id @0 :Id;
    531       # ID of the imported file.
    532 
    533       name @1 :Text;
    534       # Name which *this* file used to refer to the foreign file.  This may be a relative name.
    535       # This information is provided because it might be useful for code generation, e.g. to
    536       # generate #include directives in C++.  We don't put this in Node.file because this
    537       # information is only meaningful at compile time anyway.
    538       #
    539       # (On Zooko's triangle, this is the import's petname according to the importing file.)
    540     }
    541   }
    542 }