Debian Codegraph @

third_party/upb/upbc/code_generator_request.c

raw
1 /*
2 * Copyright (c) 2009-2022, 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 "upbc/code_generator_request.h"
29
30 #include <inttypes.h>
31 #include <setjmp.h>
32 #include <stdio.h>
33
34 #include "google/protobuf/compiler/plugin.upb.h"
35 #include "upb/mini_descriptor.h"
36 #include "upb/mini_table.h"
37
38 // Must be last.
39 #include "upb/port_def.inc"
40
41 /******************************************************************************/
42
43 // Kitchen sink storage for all of our state as we build the mini descriptors.
44
45 typedef struct {
46 upb_Arena* arena;
47 upb_Status* status;
48 upb_DefPool* symtab;
49
50 upbc_CodeGeneratorRequest* out;
51
52 jmp_buf jmp;
53 } upbc_State;
54
55 static void upbc_State_Fini(upbc_State* s) {
56 if (s->symtab) upb_DefPool_Free(s->symtab);
57 }
58
59 static void upbc_Error(upbc_State* s, const char* fn, const char* msg) {
60 upb_Status_SetErrorFormat(s->status, "%s(): %s", fn, msg);
61 upbc_State_Fini(s);
62 UPB_LONGJMP(s->jmp, -1);
63 }
64
65 static void upbc_State_Init(upbc_State* s) {
66 s->symtab = upb_DefPool_New();
67 if (!s->symtab) upbc_Error(s, __func__, "could not allocate def pool");
68
69 s->out = upbc_CodeGeneratorRequest_new(s->arena);
70 if (!s->out) upbc_Error(s, __func__, "could not allocate request");
71 }
72
73 static void upbc_State_Emit(upbc_State* s, const char* name,
74 upb_StringView encoding) {
75 const upb_StringView key = upb_StringView_FromString(name);
76 bool ok = upbc_CodeGeneratorRequest_mini_descriptors_set(s->out, key,
77 encoding, s->arena);
78 if (!ok) upbc_Error(s, __func__, "could not set mini descriptor in map");
79 }
80
81 /******************************************************************************/
82
83 // Forward declaration.
84 static void upbc_Scrape_Message(upbc_State*, const upb_MessageDef*);
85
86 static void upbc_Scrape_Enum(upbc_State* s, const upb_EnumDef* enum_def) {
87 const char* name = upb_EnumDef_FullName(enum_def);
88 const upb_StringView encoding =
89 upb_MiniDescriptor_EncodeEnum(enum_def, s->arena);
90 upbc_State_Emit(s, name, encoding);
91 }
92
93 static void upbc_Scrape_Extension(upbc_State* s,
94 const upb_FieldDef* field_def) {
95 const char* name = upb_FieldDef_FullName(field_def);
96 const upb_StringView encoding =
97 upb_MiniDescriptor_EncodeExtension(field_def, s->arena);
98 upbc_State_Emit(s, name, encoding);
99 }
100
101 static void upbc_Scrape_FileEnums(upbc_State* s, const upb_FileDef* file_def) {
102 const size_t len = upb_FileDef_TopLevelEnumCount(file_def);
103 for (size_t i = 0; i < len; i++) {
104 const upb_EnumDef* enum_def = upb_FileDef_TopLevelEnum(file_def, i);
105 upbc_Scrape_Enum(s, enum_def);
106 }
107 }
108
109 static void upbc_Scrape_FileExtensions(upbc_State* s,
110 const upb_FileDef* file_def) {
111 const size_t len = upb_FileDef_TopLevelExtensionCount(file_def);
112 for (size_t i = 0; i < len; i++) {
113 const upb_FieldDef* field_def = upb_FileDef_TopLevelExtension(file_def, i);
114 upbc_Scrape_Extension(s, field_def);
115 }
116 }
117
118 static void upbc_Scrape_FileMessages(upbc_State* s,
119 const upb_FileDef* file_def) {
120 const size_t len = upb_FileDef_TopLevelMessageCount(file_def);
121 for (size_t i = 0; i < len; i++) {
122 const upb_MessageDef* message_def =
123 upb_FileDef_TopLevelMessage(file_def, i);
124 upbc_Scrape_Message(s, message_def);
125 }
126 }
127
128 static void upbc_Scrape_File(upbc_State* s, const upb_FileDef* file_def) {
129 upbc_Scrape_FileEnums(s, file_def);
130 upbc_Scrape_FileExtensions(s, file_def);
131 upbc_Scrape_FileMessages(s, file_def);
132 }
133
134 static void upbc_Scrape_Files(upbc_State* s) {
135 const google_protobuf_compiler_CodeGeneratorRequest* request =
136 upbc_CodeGeneratorRequest_request(s->out);
137
138 size_t len = 0;
139 const google_protobuf_FileDescriptorProto* const* file_types =
140 google_protobuf_compiler_CodeGeneratorRequest_proto_file(request, &len);
141
142 for (size_t i = 0; i < len; i++) {
143 const upb_FileDef* file_def =
144 upb_DefPool_AddFile(s->symtab, file_types[i], s->status);
145 if (!file_def) upbc_Error(s, __func__, "could not add file to def pool");
146
147 upbc_Scrape_File(s, file_def);
148 }
149 }
150
151 static void upbc_Scrape_NestedEnums(upbc_State* s,
152 const upb_MessageDef* message_def) {
153 const size_t len = upb_MessageDef_NestedEnumCount(message_def);
154 for (size_t i = 0; i < len; i++) {
155 const upb_EnumDef* enum_def = upb_MessageDef_NestedEnum(message_def, i);
156 upbc_Scrape_Enum(s, enum_def);
157 }
158 }
159
160 static void upbc_Scrape_NestedExtensions(upbc_State* s,
161 const upb_MessageDef* message_def) {
162 const size_t len = upb_MessageDef_NestedExtensionCount(message_def);
163 for (size_t i = 0; i < len; i++) {
164 const upb_FieldDef* field_def =
165 upb_MessageDef_NestedExtension(message_def, i);
166 upbc_Scrape_Extension(s, field_def);
167 }
168 }
169
170 static void upbc_Scrape_NestedMessages(upbc_State* s,
171 const upb_MessageDef* message_def) {
172 const size_t len = upb_MessageDef_NestedMessageCount(message_def);
173 for (size_t i = 0; i < len; i++) {
174 const upb_MessageDef* nested_def =
175 upb_MessageDef_NestedMessage(message_def, i);
176 upbc_Scrape_Message(s, nested_def);
177 }
178 }
179
180 static void upbc_Scrape_Message(upbc_State* s,
181 const upb_MessageDef* message_def) {
182 const char* name = upb_MessageDef_FullName(message_def);
183 const upb_StringView encoding =
184 upb_MiniDescriptor_EncodeMessage(message_def, s->arena);
185 upbc_State_Emit(s, name, encoding);
186
187 upbc_Scrape_NestedEnums(s, message_def);
188 upbc_Scrape_NestedExtensions(s, message_def);
189 upbc_Scrape_NestedMessages(s, message_def);
190 }
191
192 upbc_CodeGeneratorRequest* upbc_MakeCodeGeneratorRequest(
193 google_protobuf_compiler_CodeGeneratorRequest* request, upb_Arena* arena,
194 upb_Status* status) {
195 upbc_State s = {
196 .arena = arena,
197 .status = status,
198 .symtab = NULL,
199 .out = NULL,
200 };
201
202 if (UPB_SETJMP(s.jmp)) return NULL;
203 upbc_State_Init(&s);
204
205 upbc_CodeGeneratorRequest_set_request(s.out, request);
206 upbc_Scrape_Files(&s);
207 upbc_State_Fini(&s);
208 return s.out;
209 }