1 /*
2  * Copyright (c) 2009-2021, Google LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Google LLC nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "upb/reflection/def_builder_internal.h"
29 #include "upb/reflection/def_type.h"
30 #include "upb/reflection/method_def_internal.h"
31 #include "upb/reflection/service_def.h"
32 
33 // Must be last.
34 #include "upb/port/def.inc"
35 
36 struct upb_MethodDef {
37   const UPB_DESC(MethodOptions) * opts;
38   upb_ServiceDef* service;
39   const char* full_name;
40   const upb_MessageDef* input_type;
41   const upb_MessageDef* output_type;
42   int index;
43   bool client_streaming;
44   bool server_streaming;
45 };
46 
_upb_MethodDef_At(const upb_MethodDef * m,int i)47 upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i) {
48   return (upb_MethodDef*)&m[i];
49 }
50 
upb_MethodDef_Service(const upb_MethodDef * m)51 const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m) {
52   return m->service;
53 }
54 
UPB_DESC(MethodOptions)55 const UPB_DESC(MethodOptions) * upb_MethodDef_Options(const upb_MethodDef* m) {
56   return m->opts;
57 }
58 
upb_MethodDef_HasOptions(const upb_MethodDef * m)59 bool upb_MethodDef_HasOptions(const upb_MethodDef* m) {
60   return m->opts != (void*)kUpbDefOptDefault;
61 }
62 
upb_MethodDef_FullName(const upb_MethodDef * m)63 const char* upb_MethodDef_FullName(const upb_MethodDef* m) {
64   return m->full_name;
65 }
66 
upb_MethodDef_Name(const upb_MethodDef * m)67 const char* upb_MethodDef_Name(const upb_MethodDef* m) {
68   return _upb_DefBuilder_FullToShort(m->full_name);
69 }
70 
upb_MethodDef_Index(const upb_MethodDef * m)71 int upb_MethodDef_Index(const upb_MethodDef* m) { return m->index; }
72 
upb_MethodDef_InputType(const upb_MethodDef * m)73 const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m) {
74   return m->input_type;
75 }
76 
upb_MethodDef_OutputType(const upb_MethodDef * m)77 const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m) {
78   return m->output_type;
79 }
80 
upb_MethodDef_ClientStreaming(const upb_MethodDef * m)81 bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m) {
82   return m->client_streaming;
83 }
84 
upb_MethodDef_ServerStreaming(const upb_MethodDef * m)85 bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m) {
86   return m->server_streaming;
87 }
88 
create_method(upb_DefBuilder * ctx,const UPB_DESC (MethodDescriptorProto)* method_proto,upb_ServiceDef * s,upb_MethodDef * m)89 static void create_method(upb_DefBuilder* ctx,
90                           const UPB_DESC(MethodDescriptorProto) * method_proto,
91                           upb_ServiceDef* s, upb_MethodDef* m) {
92   upb_StringView name = UPB_DESC(MethodDescriptorProto_name)(method_proto);
93 
94   m->service = s;
95   m->full_name =
96       _upb_DefBuilder_MakeFullName(ctx, upb_ServiceDef_FullName(s), name);
97   m->client_streaming =
98       UPB_DESC(MethodDescriptorProto_client_streaming)(method_proto);
99   m->server_streaming =
100       UPB_DESC(MethodDescriptorProto_server_streaming)(method_proto);
101   m->input_type = _upb_DefBuilder_Resolve(
102       ctx, m->full_name, m->full_name,
103       UPB_DESC(MethodDescriptorProto_input_type)(method_proto),
104       UPB_DEFTYPE_MSG);
105   m->output_type = _upb_DefBuilder_Resolve(
106       ctx, m->full_name, m->full_name,
107       UPB_DESC(MethodDescriptorProto_output_type)(method_proto),
108       UPB_DEFTYPE_MSG);
109 
110   UPB_DEF_SET_OPTIONS(m->opts, MethodDescriptorProto, MethodOptions,
111                       method_proto);
112 }
113 
114 // 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,upb_ServiceDef * s)115 upb_MethodDef* _upb_MethodDefs_New(
116     upb_DefBuilder* ctx, int n,
117     const UPB_DESC(MethodDescriptorProto) * const* protos, upb_ServiceDef* s) {
118   upb_MethodDef* m = _upb_DefBuilder_Alloc(ctx, sizeof(upb_MethodDef) * n);
119   for (int i = 0; i < n; i++) {
120     create_method(ctx, protos[i], s, &m[i]);
121     m[i].index = i;
122   }
123   return m;
124 }
125