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