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

resolver.h (6031B)


      1 // Copyright (c) 2013-2020 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 #pragma once
     23 
     24 #include <capnp/compiler/grammar.capnp.h>
     25 #include <capnp/schema.capnp.h>
     26 #include <capnp/schema.h>
     27 #include <kj/one-of.h>
     28 
     29 CAPNP_BEGIN_HEADER
     30 
     31 namespace capnp {
     32 namespace compiler {
     33 
     34 class Resolver {
     35   // Callback class used to find other nodes relative to some existing node.
     36   //
     37   // `Resolver` is used when compiling one declaration requires inspecting the compiled versions
     38   // of other declarations it depends on. For example, if struct type Foo contains a field of type
     39   // Bar, and specifies a default value for that field, then to parse that default value we need
     40   // the compiled version of `Bar`. Or, more commonly, if a struct type Foo refers to some other
     41   // type `Bar.Baz`, this requires doing a lookup that depends on at least partial compilation of
     42   // `Bar`, in order to discover its nested type `Baz`.
     43   //
     44   // Note that declarations are often compiled just-in-time the first time they are resolved. So,
     45   // the methods of Resolver may recurse back into other parts of the compiler. It must detect when
     46   // a dependency cycle occurs and report an error in order to prevent an infinite loop.
     47 
     48 public:
     49   struct ResolvedDecl {
     50     // Information about a resolved declaration.
     51 
     52     uint64_t id;
     53     // Type ID / node ID of the resolved declaration.
     54 
     55     uint genericParamCount;
     56     // If non-zero, the declaration is a generic with the given number of parameters.
     57 
     58     uint64_t scopeId;
     59     // The ID of the parent scope of this declaration.
     60 
     61     Declaration::Which kind;
     62     // What basic kind of declaration is this? E.g. struct, interface, const, etc.
     63 
     64     Resolver* resolver;
     65     // `Resolver` instance that can be used to further resolve other declarations relative to this
     66     // one.
     67 
     68     kj::Maybe<schema::Brand::Reader> brand;
     69     // If present, then it is necessary to replace the brand scope with the given brand before
     70     // using the target type. This happens when the decl resolved to an alias; all other fields
     71     // of `ResolvedDecl` refer to the target of the alias, except for `scopeId` which is the
     72     // scope that contained the alias.
     73   };
     74 
     75   struct ResolvedParameter {
     76     uint64_t id;  // ID of the node declaring the parameter.
     77     uint index;   // Index of the parameter.
     78   };
     79 
     80   typedef kj::OneOf<ResolvedDecl, ResolvedParameter> ResolveResult;
     81 
     82   virtual kj::Maybe<ResolveResult> resolve(kj::StringPtr name) = 0;
     83   // Look up the given name, relative to this node, and return basic information about the
     84   // target.
     85 
     86   virtual kj::Maybe<ResolveResult> resolveMember(kj::StringPtr name) = 0;
     87   // Look up a member of this node.
     88 
     89   virtual ResolvedDecl resolveBuiltin(Declaration::Which which) = 0;
     90   virtual ResolvedDecl resolveId(uint64_t id) = 0;
     91 
     92   virtual kj::Maybe<ResolvedDecl> getParent() = 0;
     93   // Returns the parent of this scope, or null if this is the top scope.
     94 
     95   virtual ResolvedDecl getTopScope() = 0;
     96   // Get the top-level scope containing this node.
     97 
     98   virtual kj::Maybe<Schema> resolveBootstrapSchema(uint64_t id, schema::Brand::Reader brand) = 0;
     99   // Get the schema for the given ID.  If a schema is returned, it must be safe to traverse its
    100   // dependencies via the Schema API.  A schema that is only at the bootstrap stage is
    101   // acceptable.
    102   //
    103   // Throws an exception if the id is not one that was found by calling resolve() or by
    104   // traversing other schemas.  Returns null if the ID is recognized, but the corresponding
    105   // schema node failed to be built for reasons that were already reported.
    106 
    107   virtual kj::Maybe<schema::Node::Reader> resolveFinalSchema(uint64_t id) = 0;
    108   // Get the final schema for the given ID.  A bootstrap schema is not acceptable.  A raw
    109   // node reader is returned rather than a Schema object because using a Schema object built
    110   // by the final schema loader could trigger lazy initialization of dependencies which could
    111   // lead to a cycle and deadlock.
    112   //
    113   // Throws an exception if the id is not one that was found by calling resolve() or by
    114   // traversing other schemas.  Returns null if the ID is recognized, but the corresponding
    115   // schema node failed to be built for reasons that were already reported.
    116 
    117   virtual kj::Maybe<ResolvedDecl> resolveImport(kj::StringPtr name) = 0;
    118   // Get the ID of an imported file given the import path.
    119 
    120   virtual kj::Maybe<kj::Array<const byte>> readEmbed(kj::StringPtr name) = 0;
    121   // Read and return the contents of a file for an `embed` expression.
    122 
    123   virtual kj::Maybe<Type> resolveBootstrapType(schema::Type::Reader type, Schema scope) = 0;
    124   // Compile a schema::Type into a Type whose dependencies may safely be traversed via the schema
    125   // API. These dependencies may have only bootstrap schemas. Returns null if the type could not
    126   // be constructed due to already-reported errors.
    127 };
    128 
    129 }  // namespace compiler
    130 }  // namespace capnp
    131 
    132 CAPNP_END_HEADER