kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/common/indexing/KytheGraphRecorder.h (about)

     1  /*
     2   * Copyright 2014 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #ifndef KYTHE_CXX_COMMON_INDEXING_KYTHE_GRAPH_RECORDER_H_
    18  #define KYTHE_CXX_COMMON_INDEXING_KYTHE_GRAPH_RECORDER_H_
    19  
    20  #include "KytheOutputStream.h"
    21  #include "absl/strings/string_view.h"
    22  
    23  namespace kythe {
    24  
    25  /// \brief Known node kinds. See the schema for details.
    26  enum class NodeKindID {
    27    kAnchor,
    28    kFile,
    29    kVariable,
    30    kTAlias,
    31    kTApp,
    32    kTNominal,
    33    kRecord,
    34    kSum,
    35    kConstant,
    36    kFunction,
    37    kLookup,
    38    kMacro,
    39    kInterface,
    40    kPackage,
    41    kTSigma,
    42    kDoc,
    43    kTBuiltin,
    44    kMeta,
    45    kDiagnostic,
    46    kClangUsr,
    47    kTVar,
    48    kName
    49  };
    50  
    51  /// \brief Known properties of nodes. See the schema for details.
    52  enum class PropertyID {
    53    kLocation,
    54    kLocationUri,
    55    kLocationStart,
    56    kLocationStartRow,
    57    kLocationStartOffset,
    58    kLocationEnd,
    59    kLocationEndRow,
    60    kLocationEndOffset,
    61    kText,
    62    kComplete,
    63    kSubkind,
    64    kNodeKind,
    65    kCode,
    66    kVariance,
    67    kParamDefault,
    68    kTagStatic,
    69    kTagDeprecated,
    70    kDiagnosticMessage,
    71    kDiagnosticDetails,
    72    kDiagnosticContextOrUrl,
    73    kDocUri,
    74    kBuildConfig,
    75    kVisibility,
    76    kFlatCode,
    77  };
    78  
    79  /// \brief Known edge kinds. See the schema for details.
    80  enum class EdgeKindID {
    81    kDefinesFull,
    82    kHasType,
    83    kRef,
    84    kRefImplicit,
    85    kRefImports,
    86    kParam,
    87    kAliases,
    88    kAliasesRoot,
    89    kChildOf,
    90    kSpecializes,
    91    kRefCall,
    92    kRefCallImplicit,
    93    kRefExpands,
    94    kUndefines,
    95    kRefIncludes,
    96    kRefQueries,
    97    kInstantiates,
    98    kRefExpandsTransitive,
    99    kExtendsPublic,
   100    kExtendsProtected,
   101    kExtendsPrivate,
   102    kExtends,
   103    kExtendsPublicVirtual,
   104    kExtendsProtectedVirtual,
   105    kExtendsPrivateVirtual,
   106    kExtendsVirtual,
   107    kExtendsCategory,
   108    kSpecializesSpeculative,
   109    kInstantiatesSpeculative,
   110    kDocuments,
   111    kRefDoc,
   112    kGenerates,
   113    kDefinesBinding,
   114    kOverrides,
   115    kOverridesRoot,
   116    kChildOfContext,
   117    kBoundedUpper,
   118    kRefInit,
   119    kRefInitImplicit,
   120    kImputes,
   121    kTagged,
   122    kPropertyReads,
   123    kPropertyWrites,
   124    kClangUsr,
   125    kRefId,
   126    kRefWrites,
   127    kRefWritesImplicit,
   128    kInfluences,
   129    kRefFile,
   130    kTParam,
   131    kCompletedby,
   132    kRefCallDirect,
   133    kRefCallDirectImplicit,
   134    kDenotes,
   135    kNamed,
   136    kTypedInit
   137  };
   138  
   139  /// \brief Returns the Kythe spelling of `node_kind_id`
   140  ///
   141  /// ~~~
   142  /// spelling_of(kAnchor) == "/kythe/anchor"
   143  /// ~~~
   144  absl::string_view spelling_of(NodeKindID node_kind_id);
   145  
   146  /// \brief Returns the Kythe spelling of `property_id`
   147  ///
   148  /// ~~~
   149  /// spelling_of(kLocationUri) == "/kythe/loc/uri"
   150  /// ~~~
   151  absl::string_view spelling_of(PropertyID property_id);
   152  
   153  /// \brief Returns the Kythe spelling of `edge_kind_id`
   154  ///
   155  /// ~~~
   156  /// spelling_of(kDefines) == "/kythe/defines"
   157  /// ~~~
   158  absl::string_view spelling_of(EdgeKindID edge_kind_id);
   159  
   160  /// Returns true and sets `out_edge` to the enumerator corresponding to
   161  /// `spelling` (or returns false if there is no such correspondence).
   162  bool of_spelling(absl::string_view str, EdgeKindID* edge_id);
   163  
   164  /// \brief Records Kythe nodes and edges to a provided `KytheOutputStream`.
   165  class KytheGraphRecorder {
   166   public:
   167    /// \brief Renders nodes and edges to the provided `KytheOutputStream`.
   168    /// \param stream The stream into which nodes and edges should be emitted.
   169    explicit KytheGraphRecorder(KytheOutputStream* stream) : stream_(stream) {
   170      assert(stream_ != nullptr);
   171    }
   172  
   173    /// \brief Record a property about a node.
   174    ///
   175    /// \param node_vname The vname of the node to modify.
   176    /// \param property_id The `PropertyID` of the property to record.
   177    /// \param property_value The value of the property to set.
   178    void AddProperty(const VNameRef& node_vname, PropertyID property_id,
   179                     absl::string_view property_value);
   180  
   181    /// \brief Record a node's marked source.
   182    ///
   183    /// \param node_vname The vname of the node to modify.
   184    /// \param marked_source The marked source to set.
   185    void AddMarkedSource(const VNameRef& node_vname,
   186                         const MarkedSource& marked_source);
   187  
   188    /// \brief Record a node's flat source.
   189    ///
   190    /// \param node_vname The vname of the node to modify.
   191    /// \param flat_source The flat source to set.
   192    void AddFlatSource(const VNameRef& node_vname, std::string_view source);
   193  
   194    /// \copydoc KytheGraphRecorder::AddProperty(const
   195    /// VNameRef&,PropertyID,std::string&)
   196    void AddProperty(const VNameRef& node_vname, PropertyID property_id,
   197                     size_t property_value);
   198  
   199    /// \copydoc KytheGraphRecorder::AddProperty(const
   200    /// VNameRef&,PropertyID,std::string&)
   201    void AddProperty(const VNameRef& node_vname, NodeKindID node_kind_value) {
   202      AddProperty(node_vname, PropertyID::kNodeKind,
   203                  spelling_of(node_kind_value));
   204    }
   205  
   206    /// \brief Records an edge between nodes.
   207    ///
   208    /// \param edge_from The `VNameRef` of the node at which the edge starts.
   209    /// \param edge_kind_id The `EdgeKindID` of the edge.
   210    /// \param edge_to The `VNameRef` of the node at which the edge terminates.
   211    void AddEdge(const VNameRef& edge_from, EdgeKindID edge_kind_id,
   212                 const VNameRef& edge_to);
   213  
   214    /// \brief Records an edge between nodes with an associated ordinal.
   215    ///
   216    /// \param edge_from The `VNameRef` of the node at which the edge starts.
   217    /// \param edge_kind_id The `EdgeKindID` of the edge.
   218    /// \param edge_to The `VNameRef` of the node at which the edge terminates.
   219    /// \param edge_ordinal The edge's associated ordinal.
   220    void AddEdge(const VNameRef& edge_from, EdgeKindID edge_kind_id,
   221                 const VNameRef& edge_to, uint32_t edge_ordinal);
   222  
   223    /// \brief Records the content of a file that was visited during compilation.
   224    /// \param file_vname The file's vname.
   225    /// \param file_content The buffer of this file's content.
   226    void AddFileContent(const VNameRef& file_vname,
   227                        absl::string_view file_content);
   228  
   229    /// \brief Stop using the last entry group pushed to the stack.
   230    void PopEntryGroup() { stream_->PopBuffer(); }
   231  
   232    /// \brief Push a new entry group to the group stack.
   233    ///
   234    /// Subsequent entries and groups will be attributed to this group.
   235    /// Various output stream policies determine when a group is ready to be
   236    /// released. Every PushEntryGroup should be paired with a PopEntryGroup.
   237    void PushEntryGroup() { stream_->PushBuffer(); }
   238  
   239   private:
   240    /// The `KytheOutputStream` to which new graph elements are written.
   241    KytheOutputStream* stream_;
   242  };
   243  
   244  }  // namespace kythe
   245  
   246  #endif  // KYTHE_CXX_COMMON_INDEXING_KYTHE_GRAPH_RECORDER_H_