def.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413
  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" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  20. * DIRECT, 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. #include "upb/def.h"
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <setjmp.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include "google/protobuf/descriptor.upb.h"
  34. #include "upb/reflection.h"
  35. /* Must be last. */
  36. #include "upb/port_def.inc"
  37. typedef struct {
  38. size_t len;
  39. char str[1]; /* Null-terminated string data follows. */
  40. } str_t;
  41. struct upb_fielddef {
  42. const upb_filedef *file;
  43. const upb_msgdef *msgdef;
  44. const char *full_name;
  45. const char *json_name;
  46. union {
  47. int64_t sint;
  48. uint64_t uint;
  49. double dbl;
  50. float flt;
  51. bool boolean;
  52. str_t *str;
  53. } defaultval;
  54. const upb_oneofdef *oneof;
  55. union {
  56. const upb_msgdef *msgdef;
  57. const upb_enumdef *enumdef;
  58. const google_protobuf_FieldDescriptorProto *unresolved;
  59. } sub;
  60. uint32_t number_;
  61. uint16_t index_;
  62. uint16_t layout_index;
  63. bool is_extension_;
  64. bool lazy_;
  65. bool packed_;
  66. bool proto3_optional_;
  67. upb_descriptortype_t type_;
  68. upb_label_t label_;
  69. };
  70. struct upb_extrange {
  71. const google_protobuf_ExtensionRangeOptions *opts;
  72. int32_t start;
  73. int32_t end;
  74. };
  75. struct upb_msgdef {
  76. const upb_msglayout *layout;
  77. const upb_filedef *file;
  78. const char *full_name;
  79. /* Tables for looking up fields by number and name. */
  80. upb_inttable itof;
  81. upb_strtable ntof;
  82. const upb_extrange *ext_ranges;
  83. const upb_fielddef *fields;
  84. const upb_oneofdef *oneofs;
  85. int field_count;
  86. int oneof_count;
  87. int real_oneof_count;
  88. int ext_range_count;
  89. /* Is this a map-entry message? */
  90. bool map_entry;
  91. bool is_message_set;
  92. upb_wellknowntype_t well_known_type;
  93. const upb_fielddef *message_set_ext;
  94. };
  95. struct upb_enumdef {
  96. const upb_filedef *file;
  97. const char *full_name;
  98. upb_strtable ntoi;
  99. upb_inttable iton;
  100. const upb_enumvaldef *values;
  101. int value_count;
  102. int32_t defaultval;
  103. };
  104. struct upb_enumvaldef {
  105. const upb_enumdef *enum_;
  106. const char *full_name;
  107. int32_t number;
  108. };
  109. struct upb_oneofdef {
  110. const upb_msgdef *parent;
  111. const char *full_name;
  112. int field_count;
  113. bool synthetic;
  114. const upb_fielddef **fields;
  115. upb_strtable ntof;
  116. upb_inttable itof;
  117. };
  118. struct upb_filedef {
  119. const char *name;
  120. const char *package;
  121. const char *phpprefix;
  122. const char *phpnamespace;
  123. const upb_filedef **deps;
  124. const upb_msgdef *msgs;
  125. const upb_enumdef *enums;
  126. const upb_fielddef *exts;
  127. const upb_msglayout_ext **ext_layouts;
  128. const upb_symtab *symtab;
  129. int dep_count;
  130. int msg_count;
  131. int enum_count;
  132. int ext_count;
  133. upb_syntax_t syntax;
  134. };
  135. struct upb_symtab {
  136. upb_arena *arena;
  137. upb_strtable syms; /* full_name -> packed def ptr */
  138. upb_strtable files; /* file_name -> upb_filedef* */
  139. upb_inttable exts; /* upb_msglayout_ext* -> upb_fielddef* */
  140. upb_extreg *extreg;
  141. size_t bytes_loaded;
  142. };
  143. /* Inside a symtab we store tagged pointers to specific def types. */
  144. typedef enum {
  145. UPB_DEFTYPE_MASK = 7,
  146. UPB_DEFTYPE_FIELD = 0,
  147. /* Only inside symtab table. */
  148. UPB_DEFTYPE_MSG = 1,
  149. UPB_DEFTYPE_ENUM = 2,
  150. UPB_DEFTYPE_ENUMVAL = 3,
  151. /* Only inside message table. */
  152. UPB_DEFTYPE_ONEOF = 1,
  153. UPB_DEFTYPE_FIELD_JSONNAME = 2
  154. } upb_deftype_t;
  155. static upb_deftype_t deftype(upb_value v) {
  156. uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
  157. return num & UPB_DEFTYPE_MASK;
  158. }
  159. static const void *unpack_def(upb_value v, upb_deftype_t type) {
  160. uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
  161. return (num & UPB_DEFTYPE_MASK) == type
  162. ? (const void *)(num & ~UPB_DEFTYPE_MASK)
  163. : NULL;
  164. }
  165. static upb_value pack_def(const void *ptr, upb_deftype_t type) {
  166. uintptr_t num = (uintptr_t)ptr;
  167. UPB_ASSERT((num & UPB_DEFTYPE_MASK) == 0);
  168. num |= type;
  169. return upb_value_constptr((const void*)num);
  170. }
  171. /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
  172. static bool upb_isbetween(char c, char low, char high) {
  173. return c >= low && c <= high;
  174. }
  175. static bool upb_isletter(char c) {
  176. return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
  177. }
  178. static bool upb_isalphanum(char c) {
  179. return upb_isletter(c) || upb_isbetween(c, '0', '9');
  180. }
  181. static const char *shortdefname(const char *fullname) {
  182. const char *p;
  183. if (fullname == NULL) {
  184. return NULL;
  185. } else if ((p = strrchr(fullname, '.')) == NULL) {
  186. /* No '.' in the name, return the full string. */
  187. return fullname;
  188. } else {
  189. /* Return one past the last '.'. */
  190. return p + 1;
  191. }
  192. }
  193. /* All submessage fields are lower than all other fields.
  194. * Secondly, fields are increasing in order. */
  195. uint32_t field_rank(const upb_fielddef *f) {
  196. uint32_t ret = upb_fielddef_number(f);
  197. const uint32_t high_bit = 1 << 30;
  198. UPB_ASSERT(ret < high_bit);
  199. if (!upb_fielddef_issubmsg(f))
  200. ret |= high_bit;
  201. return ret;
  202. }
  203. int cmp_fields(const void *p1, const void *p2) {
  204. const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
  205. const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
  206. return field_rank(f1) - field_rank(f2);
  207. }
  208. static void upb_status_setoom(upb_status *status) {
  209. upb_status_seterrmsg(status, "out of memory");
  210. }
  211. static void assign_msg_wellknowntype(upb_msgdef *m) {
  212. const char *name = upb_msgdef_fullname(m);
  213. if (name == NULL) {
  214. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  215. return;
  216. }
  217. if (!strcmp(name, "google.protobuf.Any")) {
  218. m->well_known_type = UPB_WELLKNOWN_ANY;
  219. } else if (!strcmp(name, "google.protobuf.FieldMask")) {
  220. m->well_known_type = UPB_WELLKNOWN_FIELDMASK;
  221. } else if (!strcmp(name, "google.protobuf.Duration")) {
  222. m->well_known_type = UPB_WELLKNOWN_DURATION;
  223. } else if (!strcmp(name, "google.protobuf.Timestamp")) {
  224. m->well_known_type = UPB_WELLKNOWN_TIMESTAMP;
  225. } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
  226. m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE;
  227. } else if (!strcmp(name, "google.protobuf.FloatValue")) {
  228. m->well_known_type = UPB_WELLKNOWN_FLOATVALUE;
  229. } else if (!strcmp(name, "google.protobuf.Int64Value")) {
  230. m->well_known_type = UPB_WELLKNOWN_INT64VALUE;
  231. } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
  232. m->well_known_type = UPB_WELLKNOWN_UINT64VALUE;
  233. } else if (!strcmp(name, "google.protobuf.Int32Value")) {
  234. m->well_known_type = UPB_WELLKNOWN_INT32VALUE;
  235. } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
  236. m->well_known_type = UPB_WELLKNOWN_UINT32VALUE;
  237. } else if (!strcmp(name, "google.protobuf.BoolValue")) {
  238. m->well_known_type = UPB_WELLKNOWN_BOOLVALUE;
  239. } else if (!strcmp(name, "google.protobuf.StringValue")) {
  240. m->well_known_type = UPB_WELLKNOWN_STRINGVALUE;
  241. } else if (!strcmp(name, "google.protobuf.BytesValue")) {
  242. m->well_known_type = UPB_WELLKNOWN_BYTESVALUE;
  243. } else if (!strcmp(name, "google.protobuf.Value")) {
  244. m->well_known_type = UPB_WELLKNOWN_VALUE;
  245. } else if (!strcmp(name, "google.protobuf.ListValue")) {
  246. m->well_known_type = UPB_WELLKNOWN_LISTVALUE;
  247. } else if (!strcmp(name, "google.protobuf.Struct")) {
  248. m->well_known_type = UPB_WELLKNOWN_STRUCT;
  249. } else {
  250. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  251. }
  252. }
  253. /* upb_enumdef ****************************************************************/
  254. const char *upb_enumdef_fullname(const upb_enumdef *e) {
  255. return e->full_name;
  256. }
  257. const char *upb_enumdef_name(const upb_enumdef *e) {
  258. return shortdefname(e->full_name);
  259. }
  260. const upb_filedef *upb_enumdef_file(const upb_enumdef *e) {
  261. return e->file;
  262. }
  263. int32_t upb_enumdef_default(const upb_enumdef *e) {
  264. UPB_ASSERT(upb_enumdef_lookupnum(e, e->defaultval));
  265. return e->defaultval;
  266. }
  267. const upb_enumvaldef *upb_enumdef_lookupname(const upb_enumdef *def,
  268. const char *name, size_t len) {
  269. upb_value v;
  270. return upb_strtable_lookup2(&def->ntoi, name, len, &v)
  271. ? upb_value_getconstptr(v)
  272. : NULL;
  273. }
  274. const upb_enumvaldef *upb_enumdef_lookupnum(const upb_enumdef *def, int32_t num) {
  275. upb_value v;
  276. return upb_inttable_lookup(&def->iton, num, &v) ? upb_value_getconstptr(v)
  277. : NULL;
  278. }
  279. const upb_enumvaldef *upb_enumdef_value(const upb_enumdef *e, int i) {
  280. UPB_ASSERT(0 <= i && i < e->value_count);
  281. return &e->values[i];
  282. }
  283. // Deprecated functions.
  284. int upb_enumdef_numvals(const upb_enumdef *e) {
  285. return (int)upb_strtable_count(&e->ntoi);
  286. }
  287. void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
  288. /* We iterate over the ntoi table, to account for duplicate numbers. */
  289. upb_strtable_begin(i, &e->ntoi);
  290. }
  291. void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
  292. bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
  293. const char *upb_enum_iter_name(upb_enum_iter *iter) {
  294. return upb_strtable_iter_key(iter).data;
  295. }
  296. int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  297. return upb_value_getint32(upb_strtable_iter_value(iter));
  298. }
  299. /* upb_enumvaldef *************************************************************/
  300. const upb_enumdef *upb_enumvaldef_enum(const upb_enumvaldef *ev) {
  301. return ev->enum_;
  302. }
  303. const char *upb_enumvaldef_fullname(const upb_enumvaldef *ev) {
  304. return ev->full_name;
  305. }
  306. const char *upb_enumvaldef_name(const upb_enumvaldef *ev) {
  307. return shortdefname(ev->full_name);
  308. }
  309. int32_t upb_enumvaldef_number(const upb_enumvaldef *ev) {
  310. return ev->number;
  311. }
  312. /* upb_fielddef ***************************************************************/
  313. const char *upb_fielddef_fullname(const upb_fielddef *f) {
  314. return f->full_name;
  315. }
  316. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
  317. switch (f->type_) {
  318. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  319. return UPB_TYPE_DOUBLE;
  320. case UPB_DESCRIPTOR_TYPE_FLOAT:
  321. return UPB_TYPE_FLOAT;
  322. case UPB_DESCRIPTOR_TYPE_INT64:
  323. case UPB_DESCRIPTOR_TYPE_SINT64:
  324. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  325. return UPB_TYPE_INT64;
  326. case UPB_DESCRIPTOR_TYPE_INT32:
  327. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  328. case UPB_DESCRIPTOR_TYPE_SINT32:
  329. return UPB_TYPE_INT32;
  330. case UPB_DESCRIPTOR_TYPE_UINT64:
  331. case UPB_DESCRIPTOR_TYPE_FIXED64:
  332. return UPB_TYPE_UINT64;
  333. case UPB_DESCRIPTOR_TYPE_UINT32:
  334. case UPB_DESCRIPTOR_TYPE_FIXED32:
  335. return UPB_TYPE_UINT32;
  336. case UPB_DESCRIPTOR_TYPE_ENUM:
  337. return UPB_TYPE_ENUM;
  338. case UPB_DESCRIPTOR_TYPE_BOOL:
  339. return UPB_TYPE_BOOL;
  340. case UPB_DESCRIPTOR_TYPE_STRING:
  341. return UPB_TYPE_STRING;
  342. case UPB_DESCRIPTOR_TYPE_BYTES:
  343. return UPB_TYPE_BYTES;
  344. case UPB_DESCRIPTOR_TYPE_GROUP:
  345. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  346. return UPB_TYPE_MESSAGE;
  347. }
  348. UPB_UNREACHABLE();
  349. }
  350. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
  351. return f->type_;
  352. }
  353. uint32_t upb_fielddef_index(const upb_fielddef *f) {
  354. return f->index_;
  355. }
  356. upb_label_t upb_fielddef_label(const upb_fielddef *f) {
  357. return f->label_;
  358. }
  359. uint32_t upb_fielddef_number(const upb_fielddef *f) {
  360. return f->number_;
  361. }
  362. bool upb_fielddef_isextension(const upb_fielddef *f) {
  363. return f->is_extension_;
  364. }
  365. bool upb_fielddef_lazy(const upb_fielddef *f) {
  366. return f->lazy_;
  367. }
  368. bool upb_fielddef_packed(const upb_fielddef *f) {
  369. return f->packed_;
  370. }
  371. const char *upb_fielddef_name(const upb_fielddef *f) {
  372. return shortdefname(f->full_name);
  373. }
  374. const char *upb_fielddef_jsonname(const upb_fielddef *f) {
  375. return f->json_name;
  376. }
  377. const upb_filedef *upb_fielddef_file(const upb_fielddef *f) {
  378. return f->file;
  379. }
  380. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
  381. return f->msgdef;
  382. }
  383. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
  384. return f->oneof;
  385. }
  386. const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f) {
  387. if (!f->oneof || upb_oneofdef_issynthetic(f->oneof)) return NULL;
  388. return f->oneof;
  389. }
  390. upb_msgval upb_fielddef_default(const upb_fielddef *f) {
  391. UPB_ASSERT(!upb_fielddef_issubmsg(f));
  392. upb_msgval ret;
  393. if (upb_fielddef_isstring(f)) {
  394. str_t *str = f->defaultval.str;
  395. if (str) {
  396. ret.str_val.data = str->str;
  397. ret.str_val.size = str->len;
  398. } else {
  399. ret.str_val.size = 0;
  400. }
  401. } else {
  402. memcpy(&ret, &f->defaultval, 8);
  403. }
  404. return ret;
  405. }
  406. static void chkdefaulttype(const upb_fielddef *f, int ctype) {
  407. UPB_UNUSED(f);
  408. UPB_UNUSED(ctype);
  409. }
  410. int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
  411. chkdefaulttype(f, UPB_TYPE_INT64);
  412. return f->defaultval.sint;
  413. }
  414. int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
  415. chkdefaulttype(f, UPB_TYPE_INT32);
  416. return (int32_t)f->defaultval.sint;
  417. }
  418. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
  419. chkdefaulttype(f, UPB_TYPE_UINT64);
  420. return f->defaultval.uint;
  421. }
  422. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
  423. chkdefaulttype(f, UPB_TYPE_UINT32);
  424. return (uint32_t)f->defaultval.uint;
  425. }
  426. bool upb_fielddef_defaultbool(const upb_fielddef *f) {
  427. chkdefaulttype(f, UPB_TYPE_BOOL);
  428. return f->defaultval.boolean;
  429. }
  430. float upb_fielddef_defaultfloat(const upb_fielddef *f) {
  431. chkdefaulttype(f, UPB_TYPE_FLOAT);
  432. return f->defaultval.flt;
  433. }
  434. double upb_fielddef_defaultdouble(const upb_fielddef *f) {
  435. chkdefaulttype(f, UPB_TYPE_DOUBLE);
  436. return f->defaultval.dbl;
  437. }
  438. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
  439. str_t *str = f->defaultval.str;
  440. UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
  441. upb_fielddef_type(f) == UPB_TYPE_BYTES ||
  442. upb_fielddef_type(f) == UPB_TYPE_ENUM);
  443. if (str) {
  444. if (len) *len = str->len;
  445. return str->str;
  446. } else {
  447. if (len) *len = 0;
  448. return NULL;
  449. }
  450. }
  451. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
  452. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE ? f->sub.msgdef : NULL;
  453. }
  454. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
  455. return upb_fielddef_type(f) == UPB_TYPE_ENUM ? f->sub.enumdef : NULL;
  456. }
  457. const upb_msglayout_field *upb_fielddef_layout(const upb_fielddef *f) {
  458. UPB_ASSERT(!upb_fielddef_isextension(f));
  459. return &f->msgdef->layout->fields[f->layout_index];
  460. }
  461. const upb_msglayout_ext *_upb_fielddef_extlayout(const upb_fielddef *f) {
  462. UPB_ASSERT(upb_fielddef_isextension(f));
  463. return f->file->ext_layouts[f->layout_index];
  464. }
  465. bool upb_fielddef_issubmsg(const upb_fielddef *f) {
  466. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
  467. }
  468. bool upb_fielddef_isstring(const upb_fielddef *f) {
  469. return upb_fielddef_type(f) == UPB_TYPE_STRING ||
  470. upb_fielddef_type(f) == UPB_TYPE_BYTES;
  471. }
  472. bool upb_fielddef_isseq(const upb_fielddef *f) {
  473. return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
  474. }
  475. bool upb_fielddef_isprimitive(const upb_fielddef *f) {
  476. return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
  477. }
  478. bool upb_fielddef_ismap(const upb_fielddef *f) {
  479. return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
  480. upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
  481. }
  482. bool upb_fielddef_hassubdef(const upb_fielddef *f) {
  483. return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
  484. }
  485. bool upb_fielddef_haspresence(const upb_fielddef *f) {
  486. if (upb_fielddef_isseq(f)) return false;
  487. return upb_fielddef_issubmsg(f) || upb_fielddef_containingoneof(f) ||
  488. f->file->syntax == UPB_SYNTAX_PROTO2;
  489. }
  490. static bool between(int32_t x, int32_t low, int32_t high) {
  491. return x >= low && x <= high;
  492. }
  493. bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
  494. bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
  495. bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
  496. bool upb_fielddef_checkdescriptortype(int32_t type) {
  497. return between(type, 1, 18);
  498. }
  499. /* upb_msgdef *****************************************************************/
  500. const char *upb_msgdef_fullname(const upb_msgdef *m) {
  501. return m->full_name;
  502. }
  503. const upb_filedef *upb_msgdef_file(const upb_msgdef *m) {
  504. return m->file;
  505. }
  506. const char *upb_msgdef_name(const upb_msgdef *m) {
  507. return shortdefname(m->full_name);
  508. }
  509. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
  510. return m->file->syntax;
  511. }
  512. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
  513. upb_value val;
  514. return upb_inttable_lookup(&m->itof, i, &val) ? upb_value_getconstptr(val)
  515. : NULL;
  516. }
  517. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  518. size_t len) {
  519. upb_value val;
  520. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  521. return NULL;
  522. }
  523. return unpack_def(val, UPB_DEFTYPE_FIELD);
  524. }
  525. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  526. size_t len) {
  527. upb_value val;
  528. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  529. return NULL;
  530. }
  531. return unpack_def(val, UPB_DEFTYPE_ONEOF);
  532. }
  533. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  534. const upb_fielddef **f, const upb_oneofdef **o) {
  535. upb_value val;
  536. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  537. return false;
  538. }
  539. *o = unpack_def(val, UPB_DEFTYPE_ONEOF);
  540. *f = unpack_def(val, UPB_DEFTYPE_FIELD);
  541. return *o || *f; /* False if this was a JSON name. */
  542. }
  543. const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
  544. const char *name, size_t len) {
  545. upb_value val;
  546. const upb_fielddef* f;
  547. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  548. return NULL;
  549. }
  550. f = unpack_def(val, UPB_DEFTYPE_FIELD);
  551. if (!f) f = unpack_def(val, UPB_DEFTYPE_FIELD_JSONNAME);
  552. return f;
  553. }
  554. int upb_msgdef_numfields(const upb_msgdef *m) {
  555. return m->field_count;
  556. }
  557. int upb_msgdef_numoneofs(const upb_msgdef *m) {
  558. return m->oneof_count;
  559. }
  560. int upb_msgdef_numrealoneofs(const upb_msgdef *m) {
  561. return m->real_oneof_count;
  562. }
  563. int upb_msgdef_extrangecount(const upb_msgdef *m) {
  564. return m->ext_range_count;
  565. }
  566. int upb_msgdef_fieldcount(const upb_msgdef *m) {
  567. return m->field_count;
  568. }
  569. int upb_msgdef_oneofcount(const upb_msgdef *m) {
  570. return m->oneof_count;
  571. }
  572. int upb_msgdef_realoneofcount(const upb_msgdef *m) {
  573. return m->real_oneof_count;
  574. }
  575. const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m) {
  576. return m->layout;
  577. }
  578. const upb_extrange *upb_msgdef_extrange(const upb_msgdef *m, int i) {
  579. UPB_ASSERT(i >= 0 && i < m->ext_range_count);
  580. return &m->ext_ranges[i];
  581. }
  582. const upb_fielddef *upb_msgdef_field(const upb_msgdef *m, int i) {
  583. UPB_ASSERT(i >= 0 && i < m->field_count);
  584. return &m->fields[i];
  585. }
  586. const upb_oneofdef *upb_msgdef_oneof(const upb_msgdef *m, int i) {
  587. UPB_ASSERT(i >= 0 && i < m->oneof_count);
  588. return &m->oneofs[i];
  589. }
  590. bool upb_msgdef_mapentry(const upb_msgdef *m) {
  591. return m->map_entry;
  592. }
  593. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) {
  594. return m->well_known_type;
  595. }
  596. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) {
  597. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  598. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  599. type <= UPB_WELLKNOWN_UINT32VALUE;
  600. }
  601. bool upb_msgdef_iswrapper(const upb_msgdef *m) {
  602. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  603. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  604. type <= UPB_WELLKNOWN_BOOLVALUE;
  605. }
  606. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
  607. upb_inttable_begin(iter, &m->itof);
  608. }
  609. void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
  610. bool upb_msg_field_done(const upb_msg_field_iter *iter) {
  611. return upb_inttable_done(iter);
  612. }
  613. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
  614. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  615. }
  616. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
  617. upb_inttable_iter_setdone(iter);
  618. }
  619. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  620. const upb_msg_field_iter * iter2) {
  621. return upb_inttable_iter_isequal(iter1, iter2);
  622. }
  623. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
  624. upb_strtable_begin(iter, &m->ntof);
  625. /* We need to skip past any initial fields. */
  626. while (!upb_strtable_done(iter) &&
  627. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) {
  628. upb_strtable_next(iter);
  629. }
  630. }
  631. void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
  632. /* We need to skip past fields to return only oneofs. */
  633. do {
  634. upb_strtable_next(iter);
  635. } while (!upb_strtable_done(iter) &&
  636. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF));
  637. }
  638. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
  639. return upb_strtable_done(iter);
  640. }
  641. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
  642. return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF);
  643. }
  644. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
  645. upb_strtable_iter_setdone(iter);
  646. }
  647. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  648. const upb_msg_oneof_iter *iter2) {
  649. return upb_strtable_iter_isequal(iter1, iter2);
  650. }
  651. /* upb_oneofdef ***************************************************************/
  652. const char *upb_oneofdef_name(const upb_oneofdef *o) {
  653. return shortdefname(o->full_name);
  654. }
  655. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
  656. return o->parent;
  657. }
  658. int upb_oneofdef_fieldcount(const upb_oneofdef *o) {
  659. return o->field_count;
  660. }
  661. const upb_fielddef *upb_oneofdef_field(const upb_oneofdef *o, int i) {
  662. UPB_ASSERT(i < o->field_count);
  663. return o->fields[i];
  664. }
  665. int upb_oneofdef_numfields(const upb_oneofdef *o) {
  666. return o->field_count;
  667. }
  668. uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
  669. return o - o->parent->oneofs;
  670. }
  671. bool upb_oneofdef_issynthetic(const upb_oneofdef *o) {
  672. return o->synthetic;
  673. }
  674. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  675. const char *name, size_t length) {
  676. upb_value val;
  677. return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
  678. upb_value_getptr(val) : NULL;
  679. }
  680. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
  681. upb_value val;
  682. return upb_inttable_lookup(&o->itof, num, &val) ? upb_value_getptr(val)
  683. : NULL;
  684. }
  685. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
  686. upb_inttable_begin(iter, &o->itof);
  687. }
  688. void upb_oneof_next(upb_oneof_iter *iter) {
  689. upb_inttable_next(iter);
  690. }
  691. bool upb_oneof_done(upb_oneof_iter *iter) {
  692. return upb_inttable_done(iter);
  693. }
  694. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
  695. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  696. }
  697. void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
  698. upb_inttable_iter_setdone(iter);
  699. }
  700. /* upb_filedef ****************************************************************/
  701. const char *upb_filedef_name(const upb_filedef *f) {
  702. return f->name;
  703. }
  704. const char *upb_filedef_package(const upb_filedef *f) {
  705. return f->package;
  706. }
  707. const char *upb_filedef_phpprefix(const upb_filedef *f) {
  708. return f->phpprefix;
  709. }
  710. const char *upb_filedef_phpnamespace(const upb_filedef *f) {
  711. return f->phpnamespace;
  712. }
  713. upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
  714. return f->syntax;
  715. }
  716. int upb_filedef_msgcount(const upb_filedef *f) {
  717. return f->msg_count;
  718. }
  719. int upb_filedef_depcount(const upb_filedef *f) {
  720. return f->dep_count;
  721. }
  722. int upb_filedef_enumcount(const upb_filedef *f) {
  723. return f->enum_count;
  724. }
  725. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) {
  726. return i < 0 || i >= f->dep_count ? NULL : f->deps[i];
  727. }
  728. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) {
  729. return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i];
  730. }
  731. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) {
  732. return i < 0 || i >= f->enum_count ? NULL : &f->enums[i];
  733. }
  734. const upb_symtab *upb_filedef_symtab(const upb_filedef *f) {
  735. return f->symtab;
  736. }
  737. void upb_symtab_free(upb_symtab *s) {
  738. upb_arena_free(s->arena);
  739. upb_gfree(s);
  740. }
  741. upb_symtab *upb_symtab_new(void) {
  742. upb_symtab *s = upb_gmalloc(sizeof(*s));
  743. if (!s) {
  744. return NULL;
  745. }
  746. s->arena = upb_arena_new();
  747. s->bytes_loaded = 0;
  748. if (!upb_strtable_init(&s->syms, 32, s->arena) ||
  749. !upb_strtable_init(&s->files, 4, s->arena) ||
  750. !upb_inttable_init(&s->exts, s->arena)) {
  751. goto err;
  752. }
  753. s->extreg = upb_extreg_new(s->arena);
  754. if (!s->extreg) goto err;
  755. return s;
  756. err:
  757. upb_arena_free(s->arena);
  758. upb_gfree(s);
  759. return NULL;
  760. }
  761. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
  762. upb_value v;
  763. return upb_strtable_lookup(&s->syms, sym, &v) ?
  764. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  765. }
  766. static const void *symtab_lookup2(const upb_symtab *s, const char *sym,
  767. size_t size, upb_deftype_t type) {
  768. upb_value v;
  769. return upb_strtable_lookup2(&s->syms, sym, size, &v) ? unpack_def(v, type)
  770. : NULL;
  771. }
  772. const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym,
  773. size_t len) {
  774. return symtab_lookup2(s, sym, len, UPB_DEFTYPE_MSG);
  775. }
  776. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
  777. upb_value v;
  778. return upb_strtable_lookup(&s->syms, sym, &v) ?
  779. unpack_def(v, UPB_DEFTYPE_ENUM) : NULL;
  780. }
  781. const upb_enumvaldef *upb_symtab_lookupenumval(const upb_symtab *s,
  782. const char *sym) {
  783. upb_value v;
  784. return upb_strtable_lookup(&s->syms, sym, &v)
  785. ? unpack_def(v, UPB_DEFTYPE_ENUMVAL)
  786. : NULL;
  787. }
  788. const upb_fielddef *upb_symtab_lookupext2(const upb_symtab *s, const char *name,
  789. size_t size) {
  790. upb_value v;
  791. if (!upb_strtable_lookup2(&s->syms, name, size, &v)) return NULL;
  792. switch (deftype(v)) {
  793. case UPB_DEFTYPE_FIELD:
  794. return unpack_def(v, UPB_DEFTYPE_FIELD);
  795. case UPB_DEFTYPE_MSG: {
  796. const upb_msgdef *m = unpack_def(v, UPB_DEFTYPE_MSG);
  797. return m->message_set_ext; /* May be NULL if not in MessageeSet. */
  798. }
  799. default:
  800. break;
  801. }
  802. return NULL;
  803. }
  804. const upb_fielddef *upb_symtab_lookupext(const upb_symtab *s, const char *sym) {
  805. return upb_symtab_lookupext2(s, sym, strlen(sym));
  806. }
  807. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) {
  808. upb_value v;
  809. return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
  810. : NULL;
  811. }
  812. const upb_filedef *upb_symtab_lookupfile2(
  813. const upb_symtab *s, const char *name, size_t len) {
  814. upb_value v;
  815. return upb_strtable_lookup2(&s->files, name, len, &v) ?
  816. upb_value_getconstptr(v) : NULL;
  817. }
  818. int upb_symtab_filecount(const upb_symtab *s) {
  819. return (int)upb_strtable_count(&s->files);
  820. }
  821. /* Code to build defs from descriptor protos. *********************************/
  822. /* There is a question of how much validation to do here. It will be difficult
  823. * to perfectly match the amount of validation performed by proto2. But since
  824. * this code is used to directly build defs from Ruby (for example) we do need
  825. * to validate important constraints like uniqueness of names and numbers. */
  826. #define CHK_OOM(x) if (!(x)) { symtab_oomerr(ctx); }
  827. typedef struct {
  828. upb_symtab *symtab;
  829. upb_filedef *file; /* File we are building. */
  830. upb_arena *arena; /* Allocate defs here. */
  831. const upb_msglayout_file *layout; /* NULL if we should build layouts. */
  832. int enum_count; /* Count of enums built so far. */
  833. int msg_count; /* Count of messages built so far. */
  834. int ext_count; /* Count of extensions built so far. */
  835. upb_status *status; /* Record errors here. */
  836. jmp_buf err; /* longjmp() on error. */
  837. } symtab_addctx;
  838. UPB_NORETURN UPB_NOINLINE UPB_PRINTF(2, 3)
  839. static void symtab_errf(symtab_addctx *ctx, const char *fmt, ...) {
  840. va_list argp;
  841. va_start(argp, fmt);
  842. upb_status_vseterrf(ctx->status, fmt, argp);
  843. va_end(argp);
  844. UPB_LONGJMP(ctx->err, 1);
  845. }
  846. UPB_NORETURN UPB_NOINLINE
  847. static void symtab_oomerr(symtab_addctx *ctx) {
  848. upb_status_setoom(ctx->status);
  849. UPB_LONGJMP(ctx->err, 1);
  850. }
  851. void *symtab_alloc(symtab_addctx *ctx, size_t bytes) {
  852. void *ret = upb_arena_malloc(ctx->arena, bytes);
  853. if (!ret) symtab_oomerr(ctx);
  854. return ret;
  855. }
  856. static void check_ident(symtab_addctx *ctx, upb_strview name, bool full) {
  857. const char *str = name.data;
  858. size_t len = name.size;
  859. bool start = true;
  860. size_t i;
  861. for (i = 0; i < len; i++) {
  862. char c = str[i];
  863. if (c == '.') {
  864. if (start || !full) {
  865. symtab_errf(ctx, "invalid name: unexpected '.' (%.*s)", (int)len, str);
  866. }
  867. start = true;
  868. } else if (start) {
  869. if (!upb_isletter(c)) {
  870. symtab_errf(
  871. ctx,
  872. "invalid name: path components must start with a letter (%.*s)",
  873. (int)len, str);
  874. }
  875. start = false;
  876. } else {
  877. if (!upb_isalphanum(c)) {
  878. symtab_errf(ctx, "invalid name: non-alphanumeric character (%.*s)",
  879. (int)len, str);
  880. }
  881. }
  882. }
  883. if (start) {
  884. symtab_errf(ctx, "invalid name: empty part (%.*s)", (int)len, str);
  885. }
  886. }
  887. static size_t div_round_up(size_t n, size_t d) {
  888. return (n + d - 1) / d;
  889. }
  890. static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
  891. switch (type) {
  892. case UPB_TYPE_DOUBLE:
  893. case UPB_TYPE_INT64:
  894. case UPB_TYPE_UINT64:
  895. return 8;
  896. case UPB_TYPE_ENUM:
  897. case UPB_TYPE_INT32:
  898. case UPB_TYPE_UINT32:
  899. case UPB_TYPE_FLOAT:
  900. return 4;
  901. case UPB_TYPE_BOOL:
  902. return 1;
  903. case UPB_TYPE_MESSAGE:
  904. return sizeof(void*);
  905. case UPB_TYPE_BYTES:
  906. case UPB_TYPE_STRING:
  907. return sizeof(upb_strview);
  908. }
  909. UPB_UNREACHABLE();
  910. }
  911. static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
  912. if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) {
  913. upb_map_entry ent;
  914. UPB_ASSERT(sizeof(ent.k) == sizeof(ent.v));
  915. return sizeof(ent.k);
  916. } else if (upb_fielddef_isseq(f)) {
  917. return sizeof(void*);
  918. } else {
  919. return upb_msgval_sizeof(upb_fielddef_type(f));
  920. }
  921. }
  922. static uint32_t upb_msglayout_place(upb_msglayout *l, size_t size) {
  923. uint32_t ret;
  924. l->size = UPB_ALIGN_UP(l->size, size);
  925. ret = l->size;
  926. l->size += size;
  927. return ret;
  928. }
  929. static int field_number_cmp(const void *p1, const void *p2) {
  930. const upb_msglayout_field *f1 = p1;
  931. const upb_msglayout_field *f2 = p2;
  932. return f1->number - f2->number;
  933. }
  934. static void assign_layout_indices(const upb_msgdef *m, upb_msglayout *l,
  935. upb_msglayout_field *fields) {
  936. int i;
  937. int n = upb_msgdef_numfields(m);
  938. int dense_below = 0;
  939. for (i = 0; i < n; i++) {
  940. upb_fielddef *f = (upb_fielddef*)upb_msgdef_itof(m, fields[i].number);
  941. UPB_ASSERT(f);
  942. f->layout_index = i;
  943. if (i < UINT8_MAX && fields[i].number == i + 1 &&
  944. (i == 0 || fields[i-1].number == i)) {
  945. dense_below = i + 1;
  946. }
  947. }
  948. l->dense_below = dense_below;
  949. }
  950. static void fill_fieldlayout(upb_msglayout_field *field, const upb_fielddef *f) {
  951. field->number = upb_fielddef_number(f);
  952. field->descriptortype = upb_fielddef_descriptortype(f);
  953. if (field->descriptortype == UPB_DTYPE_STRING &&
  954. f->file->syntax == UPB_SYNTAX_PROTO2) {
  955. /* See TableDescriptorType() in upbc/generator.cc for details and
  956. * rationale. */
  957. field->descriptortype = UPB_DTYPE_BYTES;
  958. }
  959. if (upb_fielddef_ismap(f)) {
  960. field->mode = _UPB_MODE_MAP | (_UPB_REP_PTR << _UPB_REP_SHIFT);
  961. } else if (upb_fielddef_isseq(f)) {
  962. field->mode = _UPB_MODE_ARRAY | (_UPB_REP_PTR << _UPB_REP_SHIFT);
  963. } else {
  964. /* Maps descriptor type -> elem_size_lg2. */
  965. static const uint8_t sizes[] = {
  966. -1, /* invalid descriptor type */
  967. _UPB_REP_8BYTE, /* DOUBLE */
  968. _UPB_REP_4BYTE, /* FLOAT */
  969. _UPB_REP_8BYTE, /* INT64 */
  970. _UPB_REP_8BYTE, /* UINT64 */
  971. _UPB_REP_4BYTE, /* INT32 */
  972. _UPB_REP_8BYTE, /* FIXED64 */
  973. _UPB_REP_4BYTE, /* FIXED32 */
  974. _UPB_REP_1BYTE, /* BOOL */
  975. _UPB_REP_STRVIEW, /* STRING */
  976. _UPB_REP_PTR, /* GROUP */
  977. _UPB_REP_PTR, /* MESSAGE */
  978. _UPB_REP_STRVIEW, /* BYTES */
  979. _UPB_REP_4BYTE, /* UINT32 */
  980. _UPB_REP_4BYTE, /* ENUM */
  981. _UPB_REP_4BYTE, /* SFIXED32 */
  982. _UPB_REP_8BYTE, /* SFIXED64 */
  983. _UPB_REP_4BYTE, /* SINT32 */
  984. _UPB_REP_8BYTE, /* SINT64 */
  985. };
  986. field->mode =
  987. _UPB_MODE_SCALAR | (sizes[field->descriptortype] << _UPB_REP_SHIFT);
  988. }
  989. if (upb_fielddef_packed(f)) {
  990. field->mode |= _UPB_MODE_IS_PACKED;
  991. }
  992. if (upb_fielddef_isextension(f)) {
  993. field->mode |= _UPB_MODE_IS_EXTENSION;
  994. }
  995. }
  996. /* This function is the dynamic equivalent of message_layout.{cc,h} in upbc.
  997. * It computes a dynamic layout for all of the fields in |m|. */
  998. static void make_layout(symtab_addctx *ctx, const upb_msgdef *m) {
  999. upb_msglayout *l = (upb_msglayout*)m->layout;
  1000. upb_msg_field_iter it;
  1001. upb_msg_oneof_iter oit;
  1002. size_t hasbit;
  1003. size_t field_count = upb_msgdef_numfields(m);
  1004. size_t sublayout_count = 0;
  1005. upb_msglayout_sub *subs;
  1006. upb_msglayout_field *fields;
  1007. memset(l, 0, sizeof(*l) + sizeof(_upb_fasttable_entry));
  1008. /* Count sub-messages. */
  1009. for (size_t i = 0; i < field_count; i++) {
  1010. if (upb_fielddef_issubmsg(&m->fields[i])) {
  1011. sublayout_count++;
  1012. }
  1013. }
  1014. fields = symtab_alloc(ctx, field_count * sizeof(*fields));
  1015. subs = symtab_alloc(ctx, sublayout_count * sizeof(*subs));
  1016. l->field_count = upb_msgdef_numfields(m);
  1017. l->fields = fields;
  1018. l->subs = subs;
  1019. l->table_mask = 0;
  1020. if (upb_msgdef_extrangecount(m) > 0) {
  1021. if (m->is_message_set) {
  1022. l->ext = _UPB_MSGEXT_MSGSET;
  1023. } else {
  1024. l->ext = _UPB_MSGEXT_EXTENDABLE;
  1025. }
  1026. } else {
  1027. l->ext = _UPB_MSGEXT_NONE;
  1028. }
  1029. /* TODO(haberman): initialize fast tables so that reflection-based parsing
  1030. * can get the same speeds as linked-in types. */
  1031. l->fasttable[0].field_parser = &fastdecode_generic;
  1032. l->fasttable[0].field_data = 0;
  1033. if (upb_msgdef_mapentry(m)) {
  1034. /* TODO(haberman): refactor this method so this special case is more
  1035. * elegant. */
  1036. const upb_fielddef *key = upb_msgdef_itof(m, 1);
  1037. const upb_fielddef *val = upb_msgdef_itof(m, 2);
  1038. fields[0].number = 1;
  1039. fields[1].number = 2;
  1040. fields[0].mode = _UPB_MODE_SCALAR;
  1041. fields[1].mode = _UPB_MODE_SCALAR;
  1042. fields[0].presence = 0;
  1043. fields[1].presence = 0;
  1044. fields[0].descriptortype = upb_fielddef_descriptortype(key);
  1045. fields[1].descriptortype = upb_fielddef_descriptortype(val);
  1046. fields[0].offset = 0;
  1047. fields[1].offset = sizeof(upb_strview);
  1048. fields[1].submsg_index = 0;
  1049. if (upb_fielddef_type(val) == UPB_TYPE_MESSAGE) {
  1050. subs[0].submsg = upb_fielddef_msgsubdef(val)->layout;
  1051. }
  1052. l->field_count = 2;
  1053. l->size = 2 * sizeof(upb_strview);
  1054. l->size = UPB_ALIGN_UP(l->size, 8);
  1055. return;
  1056. }
  1057. /* Allocate data offsets in three stages:
  1058. *
  1059. * 1. hasbits.
  1060. * 2. regular fields.
  1061. * 3. oneof fields.
  1062. *
  1063. * OPT: There is a lot of room for optimization here to minimize the size.
  1064. */
  1065. /* Allocate hasbits and set basic field attributes. */
  1066. sublayout_count = 0;
  1067. for (upb_msg_field_begin(&it, m), hasbit = 0;
  1068. !upb_msg_field_done(&it);
  1069. upb_msg_field_next(&it)) {
  1070. upb_fielddef* f = upb_msg_iter_field(&it);
  1071. upb_msglayout_field *field = &fields[upb_fielddef_index(f)];
  1072. fill_fieldlayout(field, f);
  1073. if (upb_fielddef_issubmsg(f)) {
  1074. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  1075. field->submsg_index = sublayout_count++;
  1076. subs[field->submsg_index].submsg = subm->layout;
  1077. }
  1078. if (upb_fielddef_haspresence(f) && !upb_fielddef_realcontainingoneof(f)) {
  1079. /* We don't use hasbit 0, so that 0 can indicate "no presence" in the
  1080. * table. This wastes one hasbit, but we don't worry about it for now. */
  1081. field->presence = ++hasbit;
  1082. } else {
  1083. field->presence = 0;
  1084. }
  1085. }
  1086. /* Account for space used by hasbits. */
  1087. l->size = div_round_up(hasbit, 8);
  1088. /* Allocate non-oneof fields. */
  1089. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  1090. upb_msg_field_next(&it)) {
  1091. const upb_fielddef* f = upb_msg_iter_field(&it);
  1092. size_t field_size = upb_msg_fielddefsize(f);
  1093. size_t index = upb_fielddef_index(f);
  1094. if (upb_fielddef_realcontainingoneof(f)) {
  1095. /* Oneofs are handled separately below. */
  1096. continue;
  1097. }
  1098. fields[index].offset = upb_msglayout_place(l, field_size);
  1099. }
  1100. /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
  1101. * and space for the actual data. */
  1102. for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
  1103. upb_msg_oneof_next(&oit)) {
  1104. const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
  1105. upb_oneof_iter fit;
  1106. size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
  1107. size_t field_size = 0;
  1108. uint32_t case_offset;
  1109. uint32_t data_offset;
  1110. if (upb_oneofdef_issynthetic(o)) continue;
  1111. /* Calculate field size: the max of all field sizes. */
  1112. for (upb_oneof_begin(&fit, o);
  1113. !upb_oneof_done(&fit);
  1114. upb_oneof_next(&fit)) {
  1115. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  1116. field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
  1117. }
  1118. /* Align and allocate case offset. */
  1119. case_offset = upb_msglayout_place(l, case_size);
  1120. data_offset = upb_msglayout_place(l, field_size);
  1121. for (upb_oneof_begin(&fit, o);
  1122. !upb_oneof_done(&fit);
  1123. upb_oneof_next(&fit)) {
  1124. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  1125. fields[upb_fielddef_index(f)].offset = data_offset;
  1126. fields[upb_fielddef_index(f)].presence = ~case_offset;
  1127. }
  1128. }
  1129. /* Size of the entire structure should be a multiple of its greatest
  1130. * alignment. TODO: track overall alignment for real? */
  1131. l->size = UPB_ALIGN_UP(l->size, 8);
  1132. /* Sort fields by number. */
  1133. qsort(fields, upb_msgdef_numfields(m), sizeof(*fields), field_number_cmp);
  1134. assign_layout_indices(m, l, fields);
  1135. }
  1136. static char *strviewdup(symtab_addctx *ctx, upb_strview view) {
  1137. char *ret = upb_strdup2(view.data, view.size, ctx->arena);
  1138. CHK_OOM(ret);
  1139. return ret;
  1140. }
  1141. static bool streql2(const char *a, size_t n, const char *b) {
  1142. return n == strlen(b) && memcmp(a, b, n) == 0;
  1143. }
  1144. static bool streql_view(upb_strview view, const char *b) {
  1145. return streql2(view.data, view.size, b);
  1146. }
  1147. static const char *makefullname(symtab_addctx *ctx, const char *prefix,
  1148. upb_strview name) {
  1149. if (prefix) {
  1150. /* ret = prefix + '.' + name; */
  1151. size_t n = strlen(prefix);
  1152. char *ret = symtab_alloc(ctx, n + name.size + 2);
  1153. strcpy(ret, prefix);
  1154. ret[n] = '.';
  1155. memcpy(&ret[n + 1], name.data, name.size);
  1156. ret[n + 1 + name.size] = '\0';
  1157. return ret;
  1158. } else {
  1159. return strviewdup(ctx, name);
  1160. }
  1161. }
  1162. static void finalize_oneofs(symtab_addctx *ctx, upb_msgdef *m) {
  1163. int i;
  1164. int synthetic_count = 0;
  1165. upb_oneofdef *mutable_oneofs = (upb_oneofdef*)m->oneofs;
  1166. for (i = 0; i < m->oneof_count; i++) {
  1167. upb_oneofdef *o = &mutable_oneofs[i];
  1168. if (o->synthetic && o->field_count != 1) {
  1169. symtab_errf(ctx, "Synthetic oneofs must have one field, not %d: %s",
  1170. o->field_count, upb_oneofdef_name(o));
  1171. }
  1172. if (o->synthetic) {
  1173. synthetic_count++;
  1174. } else if (synthetic_count != 0) {
  1175. symtab_errf(ctx, "Synthetic oneofs must be after all other oneofs: %s",
  1176. upb_oneofdef_name(o));
  1177. }
  1178. o->fields = symtab_alloc(ctx, sizeof(upb_fielddef *) * o->field_count);
  1179. o->field_count = 0;
  1180. }
  1181. for (i = 0; i < m->field_count; i++) {
  1182. const upb_fielddef *f = &m->fields[i];
  1183. upb_oneofdef *o = (upb_oneofdef*)f->oneof;
  1184. if (o) {
  1185. o->fields[o->field_count++] = f;
  1186. }
  1187. }
  1188. m->real_oneof_count = m->oneof_count - synthetic_count;
  1189. }
  1190. size_t getjsonname(const char *name, char *buf, size_t len) {
  1191. size_t src, dst = 0;
  1192. bool ucase_next = false;
  1193. #define WRITE(byte) \
  1194. ++dst; \
  1195. if (dst < len) buf[dst - 1] = byte; \
  1196. else if (dst == len) buf[dst - 1] = '\0'
  1197. if (!name) {
  1198. WRITE('\0');
  1199. return 0;
  1200. }
  1201. /* Implement the transformation as described in the spec:
  1202. * 1. upper case all letters after an underscore.
  1203. * 2. remove all underscores.
  1204. */
  1205. for (src = 0; name[src]; src++) {
  1206. if (name[src] == '_') {
  1207. ucase_next = true;
  1208. continue;
  1209. }
  1210. if (ucase_next) {
  1211. WRITE(toupper(name[src]));
  1212. ucase_next = false;
  1213. } else {
  1214. WRITE(name[src]);
  1215. }
  1216. }
  1217. WRITE('\0');
  1218. return dst;
  1219. #undef WRITE
  1220. }
  1221. static char* makejsonname(symtab_addctx *ctx, const char* name) {
  1222. size_t size = getjsonname(name, NULL, 0);
  1223. char* json_name = symtab_alloc(ctx, size);
  1224. getjsonname(name, json_name, size);
  1225. return json_name;
  1226. }
  1227. static void symtab_add(symtab_addctx *ctx, const char *name, upb_value v) {
  1228. // TODO: table should support an operation "tryinsert" to avoid the double
  1229. // lookup.
  1230. if (upb_strtable_lookup(&ctx->symtab->syms, name, NULL)) {
  1231. symtab_errf(ctx, "duplicate symbol '%s'", name);
  1232. }
  1233. size_t len = strlen(name);
  1234. CHK_OOM(upb_strtable_insert(&ctx->symtab->syms, name, len, v,
  1235. ctx->symtab->arena));
  1236. }
  1237. /* Given a symbol and the base symbol inside which it is defined, find the
  1238. * symbol's definition in t. */
  1239. static const void *symtab_resolve(symtab_addctx *ctx, const upb_fielddef *f,
  1240. const char *base, upb_strview sym,
  1241. upb_deftype_t type) {
  1242. const upb_strtable *t = &ctx->symtab->syms;
  1243. if(sym.size == 0) goto notfound;
  1244. if(sym.data[0] == '.') {
  1245. /* Symbols starting with '.' are absolute, so we do a single lookup.
  1246. * Slice to omit the leading '.' */
  1247. upb_value v;
  1248. if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) {
  1249. goto notfound;
  1250. }
  1251. const void *ret = unpack_def(v, type);
  1252. if (!ret) {
  1253. symtab_errf(ctx, "type mismatch when resolving field %s, name %s",
  1254. f->full_name, sym.data);
  1255. }
  1256. return ret;
  1257. } else {
  1258. /* Remove components from base until we find an entry or run out.
  1259. * TODO: This branch is totally broken, but currently not used. */
  1260. (void)base;
  1261. UPB_ASSERT(false);
  1262. goto notfound;
  1263. }
  1264. notfound:
  1265. symtab_errf(ctx, "couldn't resolve name '" UPB_STRVIEW_FORMAT "'",
  1266. UPB_STRVIEW_ARGS(sym));
  1267. }
  1268. static void create_oneofdef(
  1269. symtab_addctx *ctx, upb_msgdef *m,
  1270. const google_protobuf_OneofDescriptorProto *oneof_proto) {
  1271. upb_oneofdef *o;
  1272. upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
  1273. upb_value v;
  1274. o = (upb_oneofdef*)&m->oneofs[m->oneof_count++];
  1275. o->parent = m;
  1276. o->full_name = makefullname(ctx, m->full_name, name);
  1277. o->field_count = 0;
  1278. o->synthetic = false;
  1279. v = pack_def(o, UPB_DEFTYPE_ONEOF);
  1280. symtab_add(ctx, o->full_name, v);
  1281. CHK_OOM(upb_strtable_insert(&m->ntof, name.data, name.size, v, ctx->arena));
  1282. CHK_OOM(upb_inttable_init(&o->itof, ctx->arena));
  1283. CHK_OOM(upb_strtable_init(&o->ntof, 4, ctx->arena));
  1284. }
  1285. static str_t *newstr(symtab_addctx *ctx, const char *data, size_t len) {
  1286. str_t *ret = symtab_alloc(ctx, sizeof(*ret) + len);
  1287. if (!ret) return NULL;
  1288. ret->len = len;
  1289. if (len) memcpy(ret->str, data, len);
  1290. ret->str[len] = '\0';
  1291. return ret;
  1292. }
  1293. static void parse_default(symtab_addctx *ctx, const char *str, size_t len,
  1294. upb_fielddef *f) {
  1295. char *end;
  1296. char nullz[64];
  1297. errno = 0;
  1298. switch (upb_fielddef_type(f)) {
  1299. case UPB_TYPE_INT32:
  1300. case UPB_TYPE_INT64:
  1301. case UPB_TYPE_UINT32:
  1302. case UPB_TYPE_UINT64:
  1303. case UPB_TYPE_DOUBLE:
  1304. case UPB_TYPE_FLOAT:
  1305. /* Standard C number parsing functions expect null-terminated strings. */
  1306. if (len >= sizeof(nullz) - 1) {
  1307. symtab_errf(ctx, "Default too long: %.*s", (int)len, str);
  1308. }
  1309. memcpy(nullz, str, len);
  1310. nullz[len] = '\0';
  1311. str = nullz;
  1312. break;
  1313. default:
  1314. break;
  1315. }
  1316. switch (upb_fielddef_type(f)) {
  1317. case UPB_TYPE_INT32: {
  1318. long val = strtol(str, &end, 0);
  1319. if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end) {
  1320. goto invalid;
  1321. }
  1322. f->defaultval.sint = val;
  1323. break;
  1324. }
  1325. case UPB_TYPE_ENUM: {
  1326. const upb_enumdef *e = f->sub.enumdef;
  1327. const upb_enumvaldef *ev = upb_enumdef_lookupname(e, str, len);
  1328. if (!ev) {
  1329. goto invalid;
  1330. }
  1331. f->defaultval.sint = ev->number;
  1332. break;
  1333. }
  1334. case UPB_TYPE_INT64: {
  1335. long long val = strtoll(str, &end, 0);
  1336. if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) {
  1337. goto invalid;
  1338. }
  1339. f->defaultval.sint = val;
  1340. break;
  1341. }
  1342. case UPB_TYPE_UINT32: {
  1343. unsigned long val = strtoul(str, &end, 0);
  1344. if (val > UINT32_MAX || errno == ERANGE || *end) {
  1345. goto invalid;
  1346. }
  1347. f->defaultval.uint = val;
  1348. break;
  1349. }
  1350. case UPB_TYPE_UINT64: {
  1351. unsigned long long val = strtoull(str, &end, 0);
  1352. if (val > UINT64_MAX || errno == ERANGE || *end) {
  1353. goto invalid;
  1354. }
  1355. f->defaultval.uint = val;
  1356. break;
  1357. }
  1358. case UPB_TYPE_DOUBLE: {
  1359. double val = strtod(str, &end);
  1360. if (errno == ERANGE || *end) {
  1361. goto invalid;
  1362. }
  1363. f->defaultval.dbl = val;
  1364. break;
  1365. }
  1366. case UPB_TYPE_FLOAT: {
  1367. float val = strtof(str, &end);
  1368. if (errno == ERANGE || *end) {
  1369. goto invalid;
  1370. }
  1371. f->defaultval.flt = val;
  1372. break;
  1373. }
  1374. case UPB_TYPE_BOOL: {
  1375. if (streql2(str, len, "false")) {
  1376. f->defaultval.boolean = false;
  1377. } else if (streql2(str, len, "true")) {
  1378. f->defaultval.boolean = true;
  1379. } else {
  1380. }
  1381. break;
  1382. }
  1383. case UPB_TYPE_STRING:
  1384. f->defaultval.str = newstr(ctx, str, len);
  1385. break;
  1386. case UPB_TYPE_BYTES:
  1387. /* XXX: need to interpret the C-escaped value. */
  1388. f->defaultval.str = newstr(ctx, str, len);
  1389. break;
  1390. case UPB_TYPE_MESSAGE:
  1391. /* Should not have a default value. */
  1392. symtab_errf(ctx, "Message should not have a default (%s)",
  1393. upb_fielddef_fullname(f));
  1394. }
  1395. return;
  1396. invalid:
  1397. symtab_errf(ctx, "Invalid default '%.*s' for field %s", (int)len, str,
  1398. upb_fielddef_fullname(f));
  1399. }
  1400. static void set_default_default(symtab_addctx *ctx, upb_fielddef *f) {
  1401. switch (upb_fielddef_type(f)) {
  1402. case UPB_TYPE_INT32:
  1403. case UPB_TYPE_INT64:
  1404. case UPB_TYPE_ENUM:
  1405. f->defaultval.sint = 0;
  1406. break;
  1407. case UPB_TYPE_UINT64:
  1408. case UPB_TYPE_UINT32:
  1409. f->defaultval.uint = 0;
  1410. break;
  1411. case UPB_TYPE_DOUBLE:
  1412. case UPB_TYPE_FLOAT:
  1413. f->defaultval.dbl = 0;
  1414. break;
  1415. case UPB_TYPE_STRING:
  1416. case UPB_TYPE_BYTES:
  1417. f->defaultval.str = newstr(ctx, NULL, 0);
  1418. break;
  1419. case UPB_TYPE_BOOL:
  1420. f->defaultval.boolean = false;
  1421. break;
  1422. case UPB_TYPE_MESSAGE:
  1423. break;
  1424. }
  1425. }
  1426. static void create_fielddef(
  1427. symtab_addctx *ctx, const char *prefix, upb_msgdef *m,
  1428. const google_protobuf_FieldDescriptorProto *field_proto) {
  1429. upb_fielddef *f;
  1430. const google_protobuf_FieldOptions *options;
  1431. upb_strview name;
  1432. const char *full_name;
  1433. const char *json_name;
  1434. const char *shortname;
  1435. uint32_t field_number;
  1436. if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
  1437. symtab_errf(ctx, "field has no name (%s)", upb_msgdef_fullname(m));
  1438. }
  1439. name = google_protobuf_FieldDescriptorProto_name(field_proto);
  1440. check_ident(ctx, name, false);
  1441. full_name = makefullname(ctx, prefix, name);
  1442. shortname = shortdefname(full_name);
  1443. if (google_protobuf_FieldDescriptorProto_has_json_name(field_proto)) {
  1444. json_name = strviewdup(
  1445. ctx, google_protobuf_FieldDescriptorProto_json_name(field_proto));
  1446. } else {
  1447. json_name = makejsonname(ctx, shortname);
  1448. }
  1449. field_number = google_protobuf_FieldDescriptorProto_number(field_proto);
  1450. if (field_number == 0 || field_number > UPB_MAX_FIELDNUMBER) {
  1451. symtab_errf(ctx, "invalid field number (%u)", field_number);
  1452. }
  1453. if (m) {
  1454. /* direct message field. */
  1455. upb_value v, field_v, json_v;
  1456. size_t json_size;
  1457. f = (upb_fielddef*)&m->fields[m->field_count];
  1458. f->index_ = m->field_count++;
  1459. f->msgdef = m;
  1460. f->is_extension_ = false;
  1461. if (upb_strtable_lookup(&m->ntof, shortname, NULL)) {
  1462. symtab_errf(ctx, "duplicate field name (%s)", shortname);
  1463. }
  1464. if (upb_strtable_lookup(&m->ntof, json_name, NULL)) {
  1465. symtab_errf(ctx, "duplicate json_name (%s)", json_name);
  1466. }
  1467. if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
  1468. symtab_errf(ctx, "duplicate field number (%u)", field_number);
  1469. }
  1470. field_v = pack_def(f, UPB_DEFTYPE_FIELD);
  1471. json_v = pack_def(f, UPB_DEFTYPE_FIELD_JSONNAME);
  1472. v = upb_value_constptr(f);
  1473. json_size = strlen(json_name);
  1474. CHK_OOM(upb_strtable_insert(&m->ntof, name.data, name.size, field_v,
  1475. ctx->arena));
  1476. CHK_OOM(upb_inttable_insert(&m->itof, field_number, v, ctx->arena));
  1477. if (strcmp(shortname, json_name) != 0) {
  1478. upb_strtable_insert(&m->ntof, json_name, json_size, json_v, ctx->arena);
  1479. }
  1480. if (ctx->layout) {
  1481. const upb_msglayout_field *fields = m->layout->fields;
  1482. int count = m->layout->field_count;
  1483. bool found = false;
  1484. for (int i = 0; i < count; i++) {
  1485. if (fields[i].number == field_number) {
  1486. f->layout_index = i;
  1487. found = true;
  1488. break;
  1489. }
  1490. }
  1491. UPB_ASSERT(found);
  1492. }
  1493. } else {
  1494. /* extension field. */
  1495. uint16_t layout_index = ctx->ext_count++;
  1496. f = (upb_fielddef*)&ctx->file->exts[layout_index];
  1497. f->layout_index = layout_index;
  1498. f->is_extension_ = true;
  1499. symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD));
  1500. if (ctx->layout) {
  1501. UPB_ASSERT(ctx->file->ext_layouts[f->layout_index]->field.number ==
  1502. field_number);
  1503. }
  1504. }
  1505. f->full_name = full_name;
  1506. f->json_name = json_name;
  1507. f->file = ctx->file;
  1508. f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto);
  1509. f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto);
  1510. f->number_ = field_number;
  1511. f->oneof = NULL;
  1512. f->proto3_optional_ =
  1513. google_protobuf_FieldDescriptorProto_proto3_optional(field_proto);
  1514. /* We can't resolve the subdef or (in the case of extensions) the containing
  1515. * message yet, because it may not have been defined yet. We stash a pointer
  1516. * to the field_proto until later when we can properly resolve it. */
  1517. f->sub.unresolved = field_proto;
  1518. if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) {
  1519. symtab_errf(ctx, "proto3 fields cannot be required (%s)", f->full_name);
  1520. }
  1521. if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) {
  1522. int oneof_index =
  1523. google_protobuf_FieldDescriptorProto_oneof_index(field_proto);
  1524. upb_oneofdef *oneof;
  1525. upb_value v = upb_value_constptr(f);
  1526. if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
  1527. symtab_errf(ctx, "fields in oneof must have OPTIONAL label (%s)",
  1528. f->full_name);
  1529. }
  1530. if (!m) {
  1531. symtab_errf(ctx, "oneof_index provided for extension field (%s)",
  1532. f->full_name);
  1533. }
  1534. if (oneof_index >= m->oneof_count) {
  1535. symtab_errf(ctx, "oneof_index out of range (%s)", f->full_name);
  1536. }
  1537. oneof = (upb_oneofdef *)&m->oneofs[oneof_index];
  1538. f->oneof = oneof;
  1539. oneof->field_count++;
  1540. if (f->proto3_optional_) {
  1541. oneof->synthetic = true;
  1542. }
  1543. CHK_OOM(upb_inttable_insert(&oneof->itof, f->number_, v, ctx->arena));
  1544. CHK_OOM(
  1545. upb_strtable_insert(&oneof->ntof, name.data, name.size, v, ctx->arena));
  1546. } else {
  1547. f->oneof = NULL;
  1548. if (f->proto3_optional_) {
  1549. symtab_errf(ctx, "field with proto3_optional was not in a oneof (%s)",
  1550. f->full_name);
  1551. }
  1552. }
  1553. options = google_protobuf_FieldDescriptorProto_has_options(field_proto) ?
  1554. google_protobuf_FieldDescriptorProto_options(field_proto) : NULL;
  1555. if (options && google_protobuf_FieldOptions_has_packed(options)) {
  1556. f->packed_ = google_protobuf_FieldOptions_packed(options);
  1557. } else {
  1558. /* Repeated fields default to packed for proto3 only. */
  1559. f->packed_ = upb_fielddef_isprimitive(f) &&
  1560. f->label_ == UPB_LABEL_REPEATED && f->file->syntax == UPB_SYNTAX_PROTO3;
  1561. }
  1562. if (options) {
  1563. f->lazy_ = google_protobuf_FieldOptions_lazy(options);
  1564. } else {
  1565. f->lazy_ = false;
  1566. }
  1567. }
  1568. static void create_enumdef(
  1569. symtab_addctx *ctx, const char *prefix,
  1570. const google_protobuf_EnumDescriptorProto *enum_proto) {
  1571. upb_enumdef *e;
  1572. const google_protobuf_EnumValueDescriptorProto *const *values;
  1573. upb_strview name;
  1574. size_t i, n;
  1575. name = google_protobuf_EnumDescriptorProto_name(enum_proto);
  1576. check_ident(ctx, name, false);
  1577. e = (upb_enumdef*)&ctx->file->enums[ctx->enum_count++];
  1578. e->full_name = makefullname(ctx, prefix, name);
  1579. symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM));
  1580. values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n);
  1581. CHK_OOM(upb_strtable_init(&e->ntoi, n, ctx->arena));
  1582. CHK_OOM(upb_inttable_init(&e->iton, ctx->arena));
  1583. e->file = ctx->file;
  1584. e->defaultval = 0;
  1585. e->value_count = n;
  1586. e->values = symtab_alloc(ctx, sizeof(*e->values) * n);
  1587. if (n == 0) {
  1588. symtab_errf(ctx, "enums must contain at least one value (%s)",
  1589. e->full_name);
  1590. }
  1591. for (i = 0; i < n; i++) {
  1592. const google_protobuf_EnumValueDescriptorProto *val_proto = values[i];
  1593. upb_enumvaldef *val = (upb_enumvaldef*)&e->values[i];
  1594. upb_strview name = google_protobuf_EnumValueDescriptorProto_name(val_proto);
  1595. upb_value v = upb_value_constptr(val);
  1596. val->enum_ = e;
  1597. val->full_name = makefullname(ctx, prefix, name);
  1598. val->number = google_protobuf_EnumValueDescriptorProto_number(val_proto);
  1599. symtab_add(ctx, val->full_name, pack_def(val, UPB_DEFTYPE_ENUMVAL));
  1600. if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && val->number != 0) {
  1601. symtab_errf(ctx, "for proto3, the first enum value must be zero (%s)",
  1602. e->full_name);
  1603. }
  1604. CHK_OOM(upb_strtable_insert(&e->ntoi, name.data, name.size, v, ctx->arena));
  1605. // Multiple enumerators can have the same number, first one wins.
  1606. if (!upb_inttable_lookup(&e->iton, val->number, NULL)) {
  1607. CHK_OOM(upb_inttable_insert(&e->iton, val->number, v, ctx->arena));
  1608. }
  1609. }
  1610. upb_inttable_compact(&e->iton, ctx->arena);
  1611. }
  1612. static void create_msgdef(symtab_addctx *ctx, const char *prefix,
  1613. const google_protobuf_DescriptorProto *msg_proto) {
  1614. upb_msgdef *m;
  1615. const google_protobuf_MessageOptions *options;
  1616. const google_protobuf_OneofDescriptorProto *const *oneofs;
  1617. const google_protobuf_FieldDescriptorProto *const *fields;
  1618. const google_protobuf_EnumDescriptorProto *const *enums;
  1619. const google_protobuf_DescriptorProto *const *msgs;
  1620. const google_protobuf_DescriptorProto_ExtensionRange *const *ext_ranges;
  1621. size_t i, n_oneof, n_field, n_ext_range, n;
  1622. upb_strview name;
  1623. name = google_protobuf_DescriptorProto_name(msg_proto);
  1624. check_ident(ctx, name, false);
  1625. int msg_index = ctx->msg_count;
  1626. m = (upb_msgdef*)&ctx->file->msgs[msg_index];
  1627. m->full_name = makefullname(ctx, prefix, name);
  1628. ctx->msg_count++;
  1629. symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG));
  1630. oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n_oneof);
  1631. fields = google_protobuf_DescriptorProto_field(msg_proto, &n_field);
  1632. ext_ranges =
  1633. google_protobuf_DescriptorProto_extension_range(msg_proto, &n_ext_range);
  1634. CHK_OOM(upb_inttable_init(&m->itof, ctx->arena));
  1635. CHK_OOM(upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena));
  1636. m->file = ctx->file;
  1637. m->map_entry = false;
  1638. m->is_message_set = false;
  1639. m->message_set_ext = NULL;
  1640. options = google_protobuf_DescriptorProto_options(msg_proto);
  1641. if (options) {
  1642. m->map_entry = google_protobuf_MessageOptions_map_entry(options);
  1643. m->is_message_set =
  1644. google_protobuf_MessageOptions_message_set_wire_format(options);
  1645. }
  1646. if (ctx->layout) {
  1647. /* create_fielddef() below depends on this being set. */
  1648. m->layout = ctx->layout->msgs[msg_index];
  1649. UPB_ASSERT(n_field == m->layout->field_count);
  1650. } else {
  1651. /* Allocate now (to allow cross-linking), populate later. */
  1652. m->layout = symtab_alloc(
  1653. ctx, sizeof(*m->layout) + sizeof(_upb_fasttable_entry));
  1654. }
  1655. m->oneof_count = 0;
  1656. m->oneofs = symtab_alloc(ctx, sizeof(*m->oneofs) * n_oneof);
  1657. for (i = 0; i < n_oneof; i++) {
  1658. create_oneofdef(ctx, m, oneofs[i]);
  1659. }
  1660. m->field_count = 0;
  1661. m->fields = symtab_alloc(ctx, sizeof(*m->fields) * n_field);
  1662. for (i = 0; i < n_field; i++) {
  1663. create_fielddef(ctx, m->full_name, m, fields[i]);
  1664. }
  1665. m->ext_range_count = n_ext_range;
  1666. m->ext_ranges = symtab_alloc(ctx, sizeof(*m->ext_ranges) * n_ext_range);
  1667. for (i = 0; i < n_ext_range; i++) {
  1668. const google_protobuf_DescriptorProto_ExtensionRange *r = ext_ranges[i];
  1669. upb_extrange *r_def = (upb_extrange*)&m->ext_ranges[i];
  1670. r_def->start = google_protobuf_DescriptorProto_ExtensionRange_start(r);
  1671. r_def->end = google_protobuf_DescriptorProto_ExtensionRange_end(r);
  1672. }
  1673. finalize_oneofs(ctx, m);
  1674. assign_msg_wellknowntype(m);
  1675. upb_inttable_compact(&m->itof, ctx->arena);
  1676. /* This message is built. Now build nested messages and enums. */
  1677. enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  1678. for (i = 0; i < n; i++) {
  1679. create_enumdef(ctx, m->full_name, enums[i]);
  1680. }
  1681. fields = google_protobuf_DescriptorProto_extension(msg_proto, &n);
  1682. for (i = 0; i < n; i++) {
  1683. create_fielddef(ctx, m->full_name, NULL, fields[i]);
  1684. }
  1685. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  1686. for (i = 0; i < n; i++) {
  1687. create_msgdef(ctx, m->full_name, msgs[i]);
  1688. }
  1689. }
  1690. static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto,
  1691. upb_filedef *file) {
  1692. const google_protobuf_DescriptorProto *const *msgs;
  1693. size_t i, n;
  1694. file->msg_count++;
  1695. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  1696. for (i = 0; i < n; i++) {
  1697. count_types_in_msg(msgs[i], file);
  1698. }
  1699. google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  1700. file->enum_count += n;
  1701. google_protobuf_DescriptorProto_extension(msg_proto, &n);
  1702. file->ext_count += n;
  1703. }
  1704. static void count_types_in_file(
  1705. const google_protobuf_FileDescriptorProto *file_proto,
  1706. upb_filedef *file) {
  1707. const google_protobuf_DescriptorProto *const *msgs;
  1708. size_t i, n;
  1709. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  1710. for (i = 0; i < n; i++) {
  1711. count_types_in_msg(msgs[i], file);
  1712. }
  1713. google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  1714. file->enum_count += n;
  1715. google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  1716. file->ext_count += n;
  1717. }
  1718. static void resolve_fielddef(symtab_addctx *ctx, const char *prefix,
  1719. upb_fielddef *f) {
  1720. upb_strview name;
  1721. const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved;
  1722. if (f->is_extension_) {
  1723. if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) {
  1724. symtab_errf(ctx, "extension for field '%s' had no extendee",
  1725. f->full_name);
  1726. }
  1727. name = google_protobuf_FieldDescriptorProto_extendee(field_proto);
  1728. f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  1729. const upb_msglayout_ext *ext = ctx->file->ext_layouts[f->layout_index];
  1730. if (ctx->layout) {
  1731. UPB_ASSERT(upb_fielddef_number(f) == ext->field.number);
  1732. } else {
  1733. upb_msglayout_ext *mut_ext = (upb_msglayout_ext*)ext;
  1734. fill_fieldlayout(&mut_ext->field, f);
  1735. mut_ext->field.presence = 0;
  1736. mut_ext->field.offset = 0;
  1737. mut_ext->field.submsg_index = 0;
  1738. mut_ext->extendee = f->msgdef->layout;
  1739. mut_ext->sub.submsg = f->sub.msgdef->layout;
  1740. }
  1741. CHK_OOM(upb_inttable_insert(&ctx->symtab->exts, (uintptr_t)ext,
  1742. upb_value_constptr(f), ctx->arena));
  1743. }
  1744. if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) &&
  1745. !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) {
  1746. symtab_errf(ctx, "field '%s' is missing type name", f->full_name);
  1747. }
  1748. name = google_protobuf_FieldDescriptorProto_type_name(field_proto);
  1749. if (upb_fielddef_issubmsg(f)) {
  1750. f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  1751. if (f->is_extension_ && f->msgdef->is_message_set &&
  1752. f->file == f->msgdef->file) {
  1753. // TODO: When defs are restructured to follow message nesting, we can make
  1754. // this check more robust. The actual rules for what make something
  1755. // qualify as a MessageSet item are more strict.
  1756. ((upb_msgdef*)f->sub.msgdef)->message_set_ext = f;
  1757. }
  1758. } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) {
  1759. f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM);
  1760. }
  1761. /* Have to delay resolving of the default value until now because of the enum
  1762. * case, since enum defaults are specified with a label. */
  1763. if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) {
  1764. upb_strview defaultval =
  1765. google_protobuf_FieldDescriptorProto_default_value(field_proto);
  1766. if (f->file->syntax == UPB_SYNTAX_PROTO3) {
  1767. symtab_errf(ctx, "proto3 fields cannot have explicit defaults (%s)",
  1768. f->full_name);
  1769. }
  1770. if (upb_fielddef_issubmsg(f)) {
  1771. symtab_errf(ctx, "message fields cannot have explicit defaults (%s)",
  1772. f->full_name);
  1773. }
  1774. parse_default(ctx, defaultval.data, defaultval.size, f);
  1775. } else {
  1776. set_default_default(ctx, f);
  1777. }
  1778. }
  1779. static void build_filedef(
  1780. symtab_addctx *ctx, upb_filedef *file,
  1781. const google_protobuf_FileDescriptorProto *file_proto) {
  1782. const google_protobuf_FileOptions *file_options_proto;
  1783. const google_protobuf_DescriptorProto *const *msgs;
  1784. const google_protobuf_EnumDescriptorProto *const *enums;
  1785. const google_protobuf_FieldDescriptorProto *const *exts;
  1786. const upb_strview* strs;
  1787. size_t i, n;
  1788. file->symtab = ctx->symtab;
  1789. /* One pass to count and allocate. */
  1790. file->msg_count = 0;
  1791. file->enum_count = 0;
  1792. file->ext_count = 0;
  1793. count_types_in_file(file_proto, file);
  1794. file->msgs = symtab_alloc(ctx, sizeof(*file->msgs) * file->msg_count);
  1795. file->enums = symtab_alloc(ctx, sizeof(*file->enums) * file->enum_count);
  1796. file->exts = symtab_alloc(ctx, sizeof(*file->exts) * file->ext_count);
  1797. ctx->msg_count = 0;
  1798. ctx->enum_count = 0;
  1799. ctx->ext_count = 0;
  1800. if (ctx->layout) {
  1801. /* We are using the ext layouts that were passed in. */
  1802. file->ext_layouts = ctx->layout->exts;
  1803. if (ctx->layout->ext_count != file->ext_count) {
  1804. symtab_errf(ctx, "Extension count did not match layout (%d vs %d)",
  1805. ctx->layout->ext_count, file->ext_count);
  1806. }
  1807. } else {
  1808. /* We are building ext layouts from scratch. */
  1809. file->ext_layouts =
  1810. symtab_alloc(ctx, sizeof(*file->ext_layouts) * file->ext_count);
  1811. upb_msglayout_ext *ext = symtab_alloc(ctx, sizeof(*ext) * file->ext_count);
  1812. for (int i = 0; i < file->ext_count; i++) {
  1813. file->ext_layouts[i] = &ext[i];
  1814. }
  1815. }
  1816. if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) {
  1817. symtab_errf(ctx, "File has no name");
  1818. }
  1819. file->name =
  1820. strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto));
  1821. file->phpprefix = NULL;
  1822. file->phpnamespace = NULL;
  1823. if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
  1824. upb_strview package =
  1825. google_protobuf_FileDescriptorProto_package(file_proto);
  1826. check_ident(ctx, package, true);
  1827. file->package = strviewdup(ctx, package);
  1828. } else {
  1829. file->package = NULL;
  1830. }
  1831. if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) {
  1832. upb_strview syntax =
  1833. google_protobuf_FileDescriptorProto_syntax(file_proto);
  1834. if (streql_view(syntax, "proto2")) {
  1835. file->syntax = UPB_SYNTAX_PROTO2;
  1836. } else if (streql_view(syntax, "proto3")) {
  1837. file->syntax = UPB_SYNTAX_PROTO3;
  1838. } else {
  1839. symtab_errf(ctx, "Invalid syntax '" UPB_STRVIEW_FORMAT "'",
  1840. UPB_STRVIEW_ARGS(syntax));
  1841. }
  1842. } else {
  1843. file->syntax = UPB_SYNTAX_PROTO2;
  1844. }
  1845. /* Read options. */
  1846. file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto);
  1847. if (file_options_proto) {
  1848. if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) {
  1849. file->phpprefix = strviewdup(
  1850. ctx,
  1851. google_protobuf_FileOptions_php_class_prefix(file_options_proto));
  1852. }
  1853. if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) {
  1854. file->phpnamespace = strviewdup(
  1855. ctx, google_protobuf_FileOptions_php_namespace(file_options_proto));
  1856. }
  1857. }
  1858. /* Verify dependencies. */
  1859. strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n);
  1860. file->deps = symtab_alloc(ctx, sizeof(*file->deps) * n);
  1861. for (i = 0; i < n; i++) {
  1862. upb_strview dep_name = strs[i];
  1863. upb_value v;
  1864. if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data,
  1865. dep_name.size, &v)) {
  1866. symtab_errf(ctx,
  1867. "Depends on file '" UPB_STRVIEW_FORMAT
  1868. "', but it has not been loaded",
  1869. UPB_STRVIEW_ARGS(dep_name));
  1870. }
  1871. file->deps[i] = upb_value_getconstptr(v);
  1872. }
  1873. /* Create messages. */
  1874. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  1875. for (i = 0; i < n; i++) {
  1876. create_msgdef(ctx, file->package, msgs[i]);
  1877. }
  1878. /* Create enums. */
  1879. enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  1880. for (i = 0; i < n; i++) {
  1881. create_enumdef(ctx, file->package, enums[i]);
  1882. }
  1883. /* Create extensions. */
  1884. exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  1885. for (i = 0; i < n; i++) {
  1886. create_fielddef(ctx, file->package, NULL, exts[i]);
  1887. }
  1888. UPB_ASSERT(ctx->ext_count == file->ext_count);
  1889. /* Now that all names are in the table, build layouts and resolve refs. */
  1890. for (i = 0; i < (size_t)file->ext_count; i++) {
  1891. resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i]);
  1892. }
  1893. for (i = 0; i < (size_t)file->msg_count; i++) {
  1894. const upb_msgdef *m = &file->msgs[i];
  1895. int j;
  1896. for (j = 0; j < m->field_count; j++) {
  1897. resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j]);
  1898. }
  1899. }
  1900. if (!ctx->layout) {
  1901. for (i = 0; i < (size_t)file->msg_count; i++) {
  1902. const upb_msgdef *m = &file->msgs[i];
  1903. make_layout(ctx, m);
  1904. }
  1905. }
  1906. CHK_OOM(
  1907. _upb_extreg_add(ctx->symtab->extreg, file->ext_layouts, file->ext_count));
  1908. }
  1909. static void remove_filedef(symtab_addctx *ctx, upb_symtab *s, upb_filedef *file) {
  1910. int i;
  1911. for (i = 0; i < ctx->msg_count; i++) {
  1912. const char *name = file->msgs[i].full_name;
  1913. upb_strtable_remove(&s->syms, name, strlen(name), NULL);
  1914. }
  1915. for (i = 0; i < ctx->enum_count; i++) {
  1916. const char *name = file->enums[i].full_name;
  1917. upb_strtable_remove(&s->syms, name, strlen(name), NULL);
  1918. }
  1919. for (i = 0; i < ctx->ext_count; i++) {
  1920. const char *name = file->exts[i].full_name;
  1921. upb_strtable_remove(&s->syms, name, strlen(name), NULL);
  1922. }
  1923. }
  1924. static const upb_filedef *_upb_symtab_addfile(
  1925. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  1926. const upb_msglayout_file *layout, upb_status *status) {
  1927. symtab_addctx ctx;
  1928. upb_strview name = google_protobuf_FileDescriptorProto_name(file_proto);
  1929. if (upb_strtable_lookup2(&s->files, name.data, name.size, NULL)) {
  1930. upb_status_seterrf(status, "duplicate file name (%.*s)",
  1931. UPB_STRVIEW_ARGS(name));
  1932. return NULL;
  1933. }
  1934. ctx.symtab = s;
  1935. ctx.layout = layout;
  1936. ctx.status = status;
  1937. ctx.file = NULL;
  1938. ctx.arena = upb_arena_new();
  1939. if (!ctx.arena) {
  1940. upb_status_setoom(status);
  1941. return NULL;
  1942. }
  1943. if (UPB_UNLIKELY(UPB_SETJMP(ctx.err))) {
  1944. UPB_ASSERT(!upb_ok(status));
  1945. if (ctx.file) {
  1946. remove_filedef(&ctx, s, ctx.file);
  1947. ctx.file = NULL;
  1948. }
  1949. } else {
  1950. ctx.file = symtab_alloc(&ctx, sizeof(*ctx.file));
  1951. build_filedef(&ctx, ctx.file, file_proto);
  1952. upb_strtable_insert(&s->files, name.data, name.size,
  1953. upb_value_constptr(ctx.file), ctx.arena);
  1954. UPB_ASSERT(upb_ok(status));
  1955. upb_arena_fuse(s->arena, ctx.arena);
  1956. }
  1957. upb_arena_free(ctx.arena);
  1958. return ctx.file;
  1959. }
  1960. const upb_filedef *upb_symtab_addfile(
  1961. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  1962. upb_status *status) {
  1963. return _upb_symtab_addfile(s, file_proto, NULL, status);
  1964. }
  1965. /* Include here since we want most of this file to be stdio-free. */
  1966. #include <stdio.h>
  1967. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) {
  1968. /* Since this function should never fail (it would indicate a bug in upb) we
  1969. * print errors to stderr instead of returning error status to the user. */
  1970. upb_def_init **deps = init->deps;
  1971. google_protobuf_FileDescriptorProto *file;
  1972. upb_arena *arena;
  1973. upb_status status;
  1974. upb_status_clear(&status);
  1975. if (upb_strtable_lookup(&s->files, init->filename, NULL)) {
  1976. return true;
  1977. }
  1978. arena = upb_arena_new();
  1979. for (; *deps; deps++) {
  1980. if (!_upb_symtab_loaddefinit(s, *deps)) goto err;
  1981. }
  1982. file = google_protobuf_FileDescriptorProto_parse_ex(
  1983. init->descriptor.data, init->descriptor.size, NULL, UPB_DECODE_ALIAS,
  1984. arena);
  1985. s->bytes_loaded += init->descriptor.size;
  1986. if (!file) {
  1987. upb_status_seterrf(
  1988. &status,
  1989. "Failed to parse compiled-in descriptor for file '%s'. This should "
  1990. "never happen.",
  1991. init->filename);
  1992. goto err;
  1993. }
  1994. if (!_upb_symtab_addfile(s, file, init->layout, &status)) goto err;
  1995. upb_arena_free(arena);
  1996. return true;
  1997. err:
  1998. fprintf(stderr,
  1999. "Error loading compiled-in descriptor for file '%s' (this should "
  2000. "never happen): %s\n",
  2001. init->filename, upb_status_errmsg(&status));
  2002. upb_arena_free(arena);
  2003. return false;
  2004. }
  2005. size_t _upb_symtab_bytesloaded(const upb_symtab *s) {
  2006. return s->bytes_loaded;
  2007. }
  2008. upb_arena *_upb_symtab_arena(const upb_symtab *s) {
  2009. return s->arena;
  2010. }
  2011. const upb_fielddef *_upb_symtab_lookupextfield(const upb_symtab *s,
  2012. const upb_msglayout_ext *ext) {
  2013. upb_value v;
  2014. bool ok = upb_inttable_lookup(&s->exts, (uintptr_t)ext, &v);
  2015. UPB_ASSERT(ok);
  2016. return upb_value_getconstptr(v);
  2017. }
  2018. const upb_extreg *upb_symtab_extreg(const upb_symtab *s) {
  2019. return s->extreg;
  2020. }
  2021. #undef CHK_OOM