cannam@147: # Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors cannam@147: # Licensed under the MIT License: cannam@147: # cannam@147: # Permission is hereby granted, free of charge, to any person obtaining a copy cannam@147: # of this software and associated documentation files (the "Software"), to deal cannam@147: # in the Software without restriction, including without limitation the rights cannam@147: # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell cannam@147: # copies of the Software, and to permit persons to whom the Software is cannam@147: # furnished to do so, subject to the following conditions: cannam@147: # cannam@147: # The above copyright notice and this permission notice shall be included in cannam@147: # all copies or substantial portions of the Software. cannam@147: # cannam@147: # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR cannam@147: # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, cannam@147: # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE cannam@147: # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER cannam@147: # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, cannam@147: # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN cannam@147: # THE SOFTWARE. cannam@147: cannam@147: using Cxx = import "/capnp/c++.capnp"; cannam@147: cannam@147: @0xa93fc509624c72d9; cannam@147: $Cxx.namespace("capnp::schema"); cannam@147: cannam@147: using Id = UInt64; cannam@147: # The globally-unique ID of a file, type, or annotation. cannam@147: cannam@147: struct Node { cannam@147: id @0 :Id; cannam@147: cannam@147: displayName @1 :Text; cannam@147: # Name to present to humans to identify this Node. You should not attempt to parse this. Its cannam@147: # format could change. It is not guaranteed to be unique. cannam@147: # cannam@147: # (On Zooko's triangle, this is the node's nickname.) cannam@147: cannam@147: displayNamePrefixLength @2 :UInt32; cannam@147: # If you want a shorter version of `displayName` (just naming this node, without its surrounding cannam@147: # scope), chop off this many characters from the beginning of `displayName`. cannam@147: cannam@147: scopeId @3 :Id; cannam@147: # ID of the lexical parent node. Typically, the scope node will have a NestedNode pointing back cannam@147: # at this node, but robust code should avoid relying on this (and, in fact, group nodes are not cannam@147: # listed in the outer struct's nestedNodes, since they are listed in the fields). `scopeId` is cannam@147: # zero if the node has no parent, which is normally only the case with files, but should be cannam@147: # allowed for any kind of node (in order to make runtime type generation easier). cannam@147: cannam@147: parameters @32 :List(Parameter); cannam@147: # If this node is parameterized (generic), the list of parameters. Empty for non-generic types. cannam@147: cannam@147: isGeneric @33 :Bool; cannam@147: # True if this node is generic, meaning that it or one of its parent scopes has a non-empty cannam@147: # `parameters`. cannam@147: cannam@147: struct Parameter { cannam@147: # Information about one of the node's parameters. cannam@147: cannam@147: name @0 :Text; cannam@147: } cannam@147: cannam@147: nestedNodes @4 :List(NestedNode); cannam@147: # List of nodes nested within this node, along with the names under which they were declared. cannam@147: cannam@147: struct NestedNode { cannam@147: name @0 :Text; cannam@147: # Unqualified symbol name. Unlike Node.displayName, this *can* be used programmatically. cannam@147: # cannam@147: # (On Zooko's triangle, this is the node's petname according to its parent scope.) cannam@147: cannam@147: id @1 :Id; cannam@147: # ID of the nested node. Typically, the target node's scopeId points back to this node, but cannam@147: # robust code should avoid relying on this. cannam@147: } cannam@147: cannam@147: annotations @5 :List(Annotation); cannam@147: # Annotations applied to this node. cannam@147: cannam@147: union { cannam@147: # Info specific to each kind of node. cannam@147: cannam@147: file @6 :Void; cannam@147: cannam@147: struct :group { cannam@147: dataWordCount @7 :UInt16; cannam@147: # Size of the data section, in words. cannam@147: cannam@147: pointerCount @8 :UInt16; cannam@147: # Size of the pointer section, in pointers (which are one word each). cannam@147: cannam@147: preferredListEncoding @9 :ElementSize; cannam@147: # The preferred element size to use when encoding a list of this struct. If this is anything cannam@147: # other than `inlineComposite` then the struct is one word or less in size and is a candidate cannam@147: # for list packing optimization. cannam@147: cannam@147: isGroup @10 :Bool; cannam@147: # If true, then this "struct" node is actually not an independent node, but merely represents cannam@147: # some named union or group within a particular parent struct. This node's scopeId refers cannam@147: # to the parent struct, which may itself be a union/group in yet another struct. cannam@147: # cannam@147: # All group nodes share the same dataWordCount and pointerCount as the top-level cannam@147: # struct, and their fields live in the same ordinal and offset spaces as all other fields in cannam@147: # the struct. cannam@147: # cannam@147: # Note that a named union is considered a special kind of group -- in fact, a named union cannam@147: # is exactly equivalent to a group that contains nothing but an unnamed union. cannam@147: cannam@147: discriminantCount @11 :UInt16; cannam@147: # Number of fields in this struct which are members of an anonymous union, and thus may cannam@147: # overlap. If this is non-zero, then a 16-bit discriminant is present indicating which cannam@147: # of the overlapping fields is active. This can never be 1 -- if it is non-zero, it must be cannam@147: # two or more. cannam@147: # cannam@147: # Note that the fields of an unnamed union are considered fields of the scope containing the cannam@147: # union -- an unnamed union is not its own group. So, a top-level struct may contain a cannam@147: # non-zero discriminant count. Named unions, on the other hand, are equivalent to groups cannam@147: # containing unnamed unions. So, a named union has its own independent schema node, with cannam@147: # `isGroup` = true. cannam@147: cannam@147: discriminantOffset @12 :UInt32; cannam@147: # If `discriminantCount` is non-zero, this is the offset of the union discriminant, in cannam@147: # multiples of 16 bits. cannam@147: cannam@147: fields @13 :List(Field); cannam@147: # Fields defined within this scope (either the struct's top-level fields, or the fields of cannam@147: # a particular group; see `isGroup`). cannam@147: # cannam@147: # The fields are sorted by ordinal number, but note that because groups share the same cannam@147: # ordinal space, the field's index in this list is not necessarily exactly its ordinal. cannam@147: # On the other hand, the field's position in this list does remain the same even as the cannam@147: # protocol evolves, since it is not possible to insert or remove an earlier ordinal. cannam@147: # Therefore, for most use cases, if you want to identify a field by number, it may make the cannam@147: # most sense to use the field's index in this list rather than its ordinal. cannam@147: } cannam@147: cannam@147: enum :group { cannam@147: enumerants@14 :List(Enumerant); cannam@147: # Enumerants ordered by numeric value (ordinal). cannam@147: } cannam@147: cannam@147: interface :group { cannam@147: methods @15 :List(Method); cannam@147: # Methods ordered by ordinal. cannam@147: cannam@147: superclasses @31 :List(Superclass); cannam@147: # Superclasses of this interface. cannam@147: } cannam@147: cannam@147: const :group { cannam@147: type @16 :Type; cannam@147: value @17 :Value; cannam@147: } cannam@147: cannam@147: annotation :group { cannam@147: type @18 :Type; cannam@147: cannam@147: targetsFile @19 :Bool; cannam@147: targetsConst @20 :Bool; cannam@147: targetsEnum @21 :Bool; cannam@147: targetsEnumerant @22 :Bool; cannam@147: targetsStruct @23 :Bool; cannam@147: targetsField @24 :Bool; cannam@147: targetsUnion @25 :Bool; cannam@147: targetsGroup @26 :Bool; cannam@147: targetsInterface @27 :Bool; cannam@147: targetsMethod @28 :Bool; cannam@147: targetsParam @29 :Bool; cannam@147: targetsAnnotation @30 :Bool; cannam@147: } cannam@147: } cannam@147: } cannam@147: cannam@147: struct Field { cannam@147: # Schema for a field of a struct. cannam@147: cannam@147: name @0 :Text; cannam@147: cannam@147: codeOrder @1 :UInt16; cannam@147: # Indicates where this member appeared in the code, relative to other members. cannam@147: # Code ordering may have semantic relevance -- programmers tend to place related fields cannam@147: # together. So, using code ordering makes sense in human-readable formats where ordering is cannam@147: # otherwise irrelevant, like JSON. The values of codeOrder are tightly-packed, so the maximum cannam@147: # value is count(members) - 1. Fields that are members of a union are only ordered relative to cannam@147: # the other members of that union, so the maximum value there is count(union.members). cannam@147: cannam@147: annotations @2 :List(Annotation); cannam@147: cannam@147: const noDiscriminant :UInt16 = 0xffff; cannam@147: cannam@147: discriminantValue @3 :UInt16 = Field.noDiscriminant; cannam@147: # If the field is in a union, this is the value which the union's discriminant should take when cannam@147: # the field is active. If the field is not in a union, this is 0xffff. cannam@147: cannam@147: union { cannam@147: slot :group { cannam@147: # A regular, non-group, non-fixed-list field. cannam@147: cannam@147: offset @4 :UInt32; cannam@147: # Offset, in units of the field's size, from the beginning of the section in which the field cannam@147: # resides. E.g. for a UInt32 field, multiply this by 4 to get the byte offset from the cannam@147: # beginning of the data section. cannam@147: cannam@147: type @5 :Type; cannam@147: defaultValue @6 :Value; cannam@147: cannam@147: hadExplicitDefault @10 :Bool; cannam@147: # Whether the default value was specified explicitly. Non-explicit default values are always cannam@147: # zero or empty values. Usually, whether the default value was explicit shouldn't matter. cannam@147: # The main use case for this flag is for structs representing method parameters: cannam@147: # explicitly-defaulted parameters may be allowed to be omitted when calling the method. cannam@147: } cannam@147: cannam@147: group :group { cannam@147: # A group. cannam@147: cannam@147: typeId @7 :Id; cannam@147: # The ID of the group's node. cannam@147: } cannam@147: } cannam@147: cannam@147: ordinal :union { cannam@147: implicit @8 :Void; cannam@147: explicit @9 :UInt16; cannam@147: # The original ordinal number given to the field. You probably should NOT use this; if you need cannam@147: # a numeric identifier for a field, use its position within the field array for its scope. cannam@147: # The ordinal is given here mainly just so that the original schema text can be reproduced given cannam@147: # the compiled version -- i.e. so that `capnp compile -ocapnp` can do its job. cannam@147: } cannam@147: } cannam@147: cannam@147: struct Enumerant { cannam@147: # Schema for member of an enum. cannam@147: cannam@147: name @0 :Text; cannam@147: cannam@147: codeOrder @1 :UInt16; cannam@147: # Specifies order in which the enumerants were declared in the code. cannam@147: # Like Struct.Field.codeOrder. cannam@147: cannam@147: annotations @2 :List(Annotation); cannam@147: } cannam@147: cannam@147: struct Superclass { cannam@147: id @0 :Id; cannam@147: brand @1 :Brand; cannam@147: } cannam@147: cannam@147: struct Method { cannam@147: # Schema for method of an interface. cannam@147: cannam@147: name @0 :Text; cannam@147: cannam@147: codeOrder @1 :UInt16; cannam@147: # Specifies order in which the methods were declared in the code. cannam@147: # Like Struct.Field.codeOrder. cannam@147: cannam@147: implicitParameters @7 :List(Node.Parameter); cannam@147: # The parameters listed in [] (typically, type / generic parameters), whose bindings are intended cannam@147: # to be inferred rather than specified explicitly, although not all languages support this. cannam@147: cannam@147: paramStructType @2 :Id; cannam@147: # ID of the parameter struct type. If a named parameter list was specified in the method cannam@147: # declaration (rather than a single struct parameter type) then a corresponding struct type is cannam@147: # auto-generated. Such an auto-generated type will not be listed in the interface's cannam@147: # `nestedNodes` and its `scopeId` will be zero -- it is completely detached from the namespace. cannam@147: # (Awkwardly, it does of course inherit generic parameters from the method's scope, which makes cannam@147: # this a situation where you can't just climb the scope chain to find where a particular cannam@147: # generic parameter was introduced. Making the `scopeId` zero was a mistake.) cannam@147: cannam@147: paramBrand @5 :Brand; cannam@147: # Brand of param struct type. cannam@147: cannam@147: resultStructType @3 :Id; cannam@147: # ID of the return struct type; similar to `paramStructType`. cannam@147: cannam@147: resultBrand @6 :Brand; cannam@147: # Brand of result struct type. cannam@147: cannam@147: annotations @4 :List(Annotation); cannam@147: } cannam@147: cannam@147: struct Type { cannam@147: # Represents a type expression. cannam@147: cannam@147: union { cannam@147: # The ordinals intentionally match those of Value. cannam@147: cannam@147: void @0 :Void; cannam@147: bool @1 :Void; cannam@147: int8 @2 :Void; cannam@147: int16 @3 :Void; cannam@147: int32 @4 :Void; cannam@147: int64 @5 :Void; cannam@147: uint8 @6 :Void; cannam@147: uint16 @7 :Void; cannam@147: uint32 @8 :Void; cannam@147: uint64 @9 :Void; cannam@147: float32 @10 :Void; cannam@147: float64 @11 :Void; cannam@147: text @12 :Void; cannam@147: data @13 :Void; cannam@147: cannam@147: list :group { cannam@147: elementType @14 :Type; cannam@147: } cannam@147: cannam@147: enum :group { cannam@147: typeId @15 :Id; cannam@147: brand @21 :Brand; cannam@147: } cannam@147: struct :group { cannam@147: typeId @16 :Id; cannam@147: brand @22 :Brand; cannam@147: } cannam@147: interface :group { cannam@147: typeId @17 :Id; cannam@147: brand @23 :Brand; cannam@147: } cannam@147: cannam@147: anyPointer :union { cannam@147: unconstrained :union { cannam@147: # A regular AnyPointer. cannam@147: # cannam@147: # The name "unconstrained" means as opposed to constraining it to match a type parameter. cannam@147: # In retrospect this name is probably a poor choice given that it may still be constrained cannam@147: # to be a struct, list, or capability. cannam@147: cannam@147: anyKind @18 :Void; # truly AnyPointer cannam@147: struct @25 :Void; # AnyStruct cannam@147: list @26 :Void; # AnyList cannam@147: capability @27 :Void; # Capability cannam@147: } cannam@147: cannam@147: parameter :group { cannam@147: # This is actually a reference to a type parameter defined within this scope. cannam@147: cannam@147: scopeId @19 :Id; cannam@147: # ID of the generic type whose parameter we're referencing. This should be a parent of the cannam@147: # current scope. cannam@147: cannam@147: parameterIndex @20 :UInt16; cannam@147: # Index of the parameter within the generic type's parameter list. cannam@147: } cannam@147: cannam@147: implicitMethodParameter :group { cannam@147: # This is actually a reference to an implicit (generic) parameter of a method. The only cannam@147: # legal context for this type to appear is inside Method.paramBrand or Method.resultBrand. cannam@147: cannam@147: parameterIndex @24 :UInt16; cannam@147: } cannam@147: } cannam@147: } cannam@147: } cannam@147: cannam@147: struct Brand { cannam@147: # Specifies bindings for parameters of generics. Since these bindings turn a generic into a cannam@147: # non-generic, we call it the "brand". cannam@147: cannam@147: scopes @0 :List(Scope); cannam@147: # For each of the target type and each of its parent scopes, a parameterization may be included cannam@147: # in this list. If no parameterization is included for a particular relevant scope, then either cannam@147: # that scope has no parameters or all parameters should be considered to be `AnyPointer`. cannam@147: cannam@147: struct Scope { cannam@147: scopeId @0 :Id; cannam@147: # ID of the scope to which these params apply. cannam@147: cannam@147: union { cannam@147: bind @1 :List(Binding); cannam@147: # List of parameter bindings. cannam@147: cannam@147: inherit @2 :Void; cannam@147: # The place where this Brand appears is actually within this scope or a sub-scope, cannam@147: # and the bindings for this scope should be inherited from the reference point. cannam@147: } cannam@147: } cannam@147: cannam@147: struct Binding { cannam@147: union { cannam@147: unbound @0 :Void; cannam@147: type @1 :Type; cannam@147: cannam@147: # TODO(someday): Allow non-type parameters? Unsure if useful. cannam@147: } cannam@147: } cannam@147: } cannam@147: cannam@147: struct Value { cannam@147: # Represents a value, e.g. a field default value, constant value, or annotation value. cannam@147: cannam@147: union { cannam@147: # The ordinals intentionally match those of Type. cannam@147: cannam@147: void @0 :Void; cannam@147: bool @1 :Bool; cannam@147: int8 @2 :Int8; cannam@147: int16 @3 :Int16; cannam@147: int32 @4 :Int32; cannam@147: int64 @5 :Int64; cannam@147: uint8 @6 :UInt8; cannam@147: uint16 @7 :UInt16; cannam@147: uint32 @8 :UInt32; cannam@147: uint64 @9 :UInt64; cannam@147: float32 @10 :Float32; cannam@147: float64 @11 :Float64; cannam@147: text @12 :Text; cannam@147: data @13 :Data; cannam@147: cannam@147: list @14 :AnyPointer; cannam@147: cannam@147: enum @15 :UInt16; cannam@147: struct @16 :AnyPointer; cannam@147: cannam@147: interface @17 :Void; cannam@147: # The only interface value that can be represented statically is "null", whose methods always cannam@147: # throw exceptions. cannam@147: cannam@147: anyPointer @18 :AnyPointer; cannam@147: } cannam@147: } cannam@147: cannam@147: struct Annotation { cannam@147: # Describes an annotation applied to a declaration. Note AnnotationNode describes the cannam@147: # annotation's declaration, while this describes a use of the annotation. cannam@147: cannam@147: id @0 :Id; cannam@147: # ID of the annotation node. cannam@147: cannam@147: brand @2 :Brand; cannam@147: # Brand of the annotation. cannam@147: # cannam@147: # Note that the annotation itself is not allowed to be parameterized, but its scope might be. cannam@147: cannam@147: value @1 :Value; cannam@147: } cannam@147: cannam@147: enum ElementSize { cannam@147: # Possible element sizes for encoded lists. These correspond exactly to the possible values of cannam@147: # the 3-bit element size component of a list pointer. cannam@147: cannam@147: empty @0; # aka "void", but that's a keyword. cannam@147: bit @1; cannam@147: byte @2; cannam@147: twoBytes @3; cannam@147: fourBytes @4; cannam@147: eightBytes @5; cannam@147: pointer @6; cannam@147: inlineComposite @7; cannam@147: } cannam@147: cannam@147: struct CapnpVersion { cannam@147: major @0 :UInt16; cannam@147: minor @1 :UInt8; cannam@147: micro @2 :UInt8; cannam@147: } cannam@147: cannam@147: struct CodeGeneratorRequest { cannam@147: capnpVersion @2 :CapnpVersion; cannam@147: # Version of the `capnp` executable. Generally, code generators should ignore this, but the code cannam@147: # generators that ship with `capnp` itself will print a warning if this mismatches since that cannam@147: # probably indicates something is misconfigured. cannam@147: # cannam@147: # The first version of 'capnp' to set this was 0.6.0. So, if it's missing, the compiler version cannam@147: # is older than that. cannam@147: cannam@147: nodes @0 :List(Node); cannam@147: # All nodes parsed by the compiler, including for the files on the command line and their cannam@147: # imports. cannam@147: cannam@147: requestedFiles @1 :List(RequestedFile); cannam@147: # Files which were listed on the command line. cannam@147: cannam@147: struct RequestedFile { cannam@147: id @0 :Id; cannam@147: # ID of the file. cannam@147: cannam@147: filename @1 :Text; cannam@147: # Name of the file as it appeared on the command-line (minus the src-prefix). You may use cannam@147: # this to decide where to write the output. cannam@147: cannam@147: imports @2 :List(Import); cannam@147: # List of all imported paths seen in this file. cannam@147: cannam@147: struct Import { cannam@147: id @0 :Id; cannam@147: # ID of the imported file. cannam@147: cannam@147: name @1 :Text; cannam@147: # Name which *this* file used to refer to the foreign file. This may be a relative name. cannam@147: # This information is provided because it might be useful for code generation, e.g. to cannam@147: # generate #include directives in C++. We don't put this in Node.file because this cannam@147: # information is only meaningful at compile time anyway. cannam@147: # cannam@147: # (On Zooko's triangle, this is the import's petname according to the importing file.) cannam@147: } cannam@147: } cannam@147: }