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