• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 #include "upb/reflection/internal/method_def.h"
9 
10 #include "upb/reflection/def_type.h"
11 #include "upb/reflection/internal/def_builder.h"
12 #include "upb/reflection/service_def.h"
13 
14 // Must be last.
15 #include "upb/port/def.inc"
16 
17 struct upb_MethodDef {
18   const UPB_DESC(MethodOptions*) opts;
19   const UPB_DESC(FeatureSet*) resolved_features;
20   upb_ServiceDef* service;
21   const char* full_name;
22   const upb_MessageDef* input_type;
23   const upb_MessageDef* output_type;
24   int index;
25   bool client_streaming;
26   bool server_streaming;
27 };
28 
_upb_MethodDef_At(const upb_MethodDef * m,int i)29 upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i) {
30   return (upb_MethodDef*)&m[i];
31 }
32 
upb_MethodDef_Service(const upb_MethodDef * m)33 const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m) {
34   return m->service;
35 }
36 
UPB_DESC(MethodOptions)37 const UPB_DESC(MethodOptions) * upb_MethodDef_Options(const upb_MethodDef* m) {
38   return m->opts;
39 }
40 
upb_MethodDef_HasOptions(const upb_MethodDef * m)41 bool upb_MethodDef_HasOptions(const upb_MethodDef* m) {
42   return m->opts != (void*)kUpbDefOptDefault;
43 }
44 
UPB_DESC(FeatureSet)45 const UPB_DESC(FeatureSet) *
46     upb_MethodDef_ResolvedFeatures(const upb_MethodDef* m) {
47   return m->resolved_features;
48 }
49 
upb_MethodDef_FullName(const upb_MethodDef * m)50 const char* upb_MethodDef_FullName(const upb_MethodDef* m) {
51   return m->full_name;
52 }
53 
upb_MethodDef_Name(const upb_MethodDef * m)54 const char* upb_MethodDef_Name(const upb_MethodDef* m) {
55   return _upb_DefBuilder_FullToShort(m->full_name);
56 }
57 
upb_MethodDef_Index(const upb_MethodDef * m)58 int upb_MethodDef_Index(const upb_MethodDef* m) { return m->index; }
59 
upb_MethodDef_InputType(const upb_MethodDef * m)60 const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m) {
61   return m->input_type;
62 }
63 
upb_MethodDef_OutputType(const upb_MethodDef * m)64 const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m) {
65   return m->output_type;
66 }
67 
upb_MethodDef_ClientStreaming(const upb_MethodDef * m)68 bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m) {
69   return m->client_streaming;
70 }
71 
upb_MethodDef_ServerStreaming(const upb_MethodDef * m)72 bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m) {
73   return m->server_streaming;
74 }
75 
create_method(upb_DefBuilder * ctx,const UPB_DESC (MethodDescriptorProto *)method_proto,const UPB_DESC (FeatureSet *)parent_features,upb_ServiceDef * s,upb_MethodDef * m)76 static void create_method(upb_DefBuilder* ctx,
77                           const UPB_DESC(MethodDescriptorProto*) method_proto,
78                           const UPB_DESC(FeatureSet*) parent_features,
79                           upb_ServiceDef* s, upb_MethodDef* m) {
80   UPB_DEF_SET_OPTIONS(m->opts, MethodDescriptorProto, MethodOptions,
81                       method_proto);
82   m->resolved_features = _upb_DefBuilder_ResolveFeatures(
83       ctx, parent_features, UPB_DESC(MethodOptions_features)(m->opts));
84 
85   upb_StringView name = UPB_DESC(MethodDescriptorProto_name)(method_proto);
86 
87   m->service = s;
88   m->full_name =
89       _upb_DefBuilder_MakeFullName(ctx, upb_ServiceDef_FullName(s), name);
90   m->client_streaming =
91       UPB_DESC(MethodDescriptorProto_client_streaming)(method_proto);
92   m->server_streaming =
93       UPB_DESC(MethodDescriptorProto_server_streaming)(method_proto);
94   m->input_type = _upb_DefBuilder_Resolve(
95       ctx, m->full_name, m->full_name,
96       UPB_DESC(MethodDescriptorProto_input_type)(method_proto),
97       UPB_DEFTYPE_MSG);
98   m->output_type = _upb_DefBuilder_Resolve(
99       ctx, m->full_name, m->full_name,
100       UPB_DESC(MethodDescriptorProto_output_type)(method_proto),
101       UPB_DEFTYPE_MSG);
102 }
103 
104 // Allocate and initialize an array of |n| method defs belonging to |s|.
_upb_MethodDefs_New(upb_DefBuilder * ctx,int n,const UPB_DESC (MethodDescriptorProto *)const * protos,const UPB_DESC (FeatureSet *)parent_features,upb_ServiceDef * s)105 upb_MethodDef* _upb_MethodDefs_New(upb_DefBuilder* ctx, int n,
106                                    const UPB_DESC(MethodDescriptorProto*)
107                                        const* protos,
108                                    const UPB_DESC(FeatureSet*) parent_features,
109                                    upb_ServiceDef* s) {
110   upb_MethodDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MethodDef) * n);
111   for (int i = 0; i < n; i++) {
112     create_method(ctx, protos[i], parent_features, s, &m[i]);
113     m[i].index = i;
114   }
115   return m;
116 }
117