upb.h 311 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248
  1. /* Amalgamated source file */
  2. #include <stdint.h>
  3. #ifndef UINTPTR_MAX
  4. #error must include stdint.h first
  5. #endif
  6. #if UINTPTR_MAX == 0xffffffff
  7. #define UPB_SIZE(size32, size64) size32
  8. #else
  9. #define UPB_SIZE(size32, size64) size64
  10. #endif
  11. #define UPB_FIELD_AT(msg, fieldtype, offset) \
  12. *(fieldtype*)((const char*)(msg) + offset)
  13. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  14. UPB_FIELD_AT(msg, int, case_offset) == case_val \
  15. ? UPB_FIELD_AT(msg, fieldtype, offset) \
  16. : default
  17. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  18. UPB_FIELD_AT(msg, int, case_offset) = case_val; \
  19. UPB_FIELD_AT(msg, fieldtype, offset) = value;
  20. /*
  21. ** upb::Message is a representation for protobuf messages.
  22. **
  23. ** However it differs from other common representations like
  24. ** google::protobuf::Message in one key way: it does not prescribe any
  25. ** ownership between messages and submessages, and it relies on the
  26. ** client to ensure that each submessage/array/map outlives its parent.
  27. **
  28. ** All messages, arrays, and maps live in an Arena. If the entire message
  29. ** tree is in the same arena, ensuring proper lifetimes is simple. However
  30. ** the client can mix arenas as long as they ensure that there are no
  31. ** dangling pointers.
  32. **
  33. ** A client can access a upb::Message without knowing anything about
  34. ** ownership semantics, but to create or mutate a message a user needs
  35. ** to implement the memory management themselves.
  36. **
  37. ** TODO: UTF-8 checking?
  38. **/
  39. #ifndef UPB_MSG_H_
  40. #define UPB_MSG_H_
  41. #include <stdint.h>
  42. #include <string.h>
  43. /*
  44. ** This file contains shared definitions that are widely used across upb.
  45. **
  46. ** This is a mixed C/C++ interface that offers a full API to both languages.
  47. ** See the top-level README for more information.
  48. */
  49. #ifndef UPB_H_
  50. #define UPB_H_
  51. #include <assert.h>
  52. #include <stdarg.h>
  53. #include <stdbool.h>
  54. #include <stddef.h>
  55. #include <stdint.h>
  56. #ifdef __cplusplus
  57. #include <memory>
  58. namespace upb {
  59. class Arena;
  60. class Status;
  61. template <int N> class InlinedArena;
  62. }
  63. #endif
  64. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  65. #ifdef __cplusplus
  66. #define UPB_INLINE inline
  67. #elif defined (__GNUC__)
  68. #define UPB_INLINE static __inline__
  69. #else
  70. #define UPB_INLINE static
  71. #endif
  72. /* Hints to the compiler about likely/unlikely branches. */
  73. #define UPB_LIKELY(x) __builtin_expect((x),1)
  74. /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
  75. * doesn't provide these preprocessor symbols. */
  76. #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  77. #define UPB_BIG_ENDIAN
  78. #endif
  79. /* Macros for function attributes on compilers that support them. */
  80. #ifdef __GNUC__
  81. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  82. #define UPB_NOINLINE __attribute__((noinline))
  83. #define UPB_NORETURN __attribute__((__noreturn__))
  84. #else /* !defined(__GNUC__) */
  85. #define UPB_FORCEINLINE
  86. #define UPB_NOINLINE
  87. #define UPB_NORETURN
  88. #endif
  89. #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
  90. /* C99/C++11 versions. */
  91. #include <stdio.h>
  92. #define _upb_snprintf snprintf
  93. #define _upb_vsnprintf vsnprintf
  94. #define _upb_va_copy(a, b) va_copy(a, b)
  95. #elif defined __GNUC__
  96. /* A few hacky workarounds for functions not in C89.
  97. * For internal use only!
  98. * TODO(haberman): fix these by including our own implementations, or finding
  99. * another workaround.
  100. */
  101. #define _upb_snprintf __builtin_snprintf
  102. #define _upb_vsnprintf __builtin_vsnprintf
  103. #define _upb_va_copy(a, b) __va_copy(a, b)
  104. #else
  105. #error Need implementations of [v]snprintf and va_copy
  106. #endif
  107. #ifdef __cplusplus
  108. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
  109. (defined(_MSC_VER) && _MSC_VER >= 1900)
  110. // C++11 is present
  111. #else
  112. #error upb requires C++11 for C++ support
  113. #endif
  114. #endif
  115. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  116. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  117. #define UPB_UNUSED(var) (void)var
  118. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  119. * evaluated. This prevents "unused variable" warnings. */
  120. #ifdef NDEBUG
  121. #define UPB_ASSERT(expr) do {} while (false && (expr))
  122. #else
  123. #define UPB_ASSERT(expr) assert(expr)
  124. #endif
  125. /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
  126. * exist in debug mode. This turns into regular assert. */
  127. #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
  128. #ifdef __GNUC__
  129. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  130. #else
  131. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  132. #endif
  133. /* upb_status *****************************************************************/
  134. /* upb_status represents a success or failure status and error message.
  135. * It owns no resources and allocates no memory, so it should work
  136. * even in OOM situations. */
  137. /* The maximum length of an error message before it will get truncated. */
  138. #define UPB_STATUS_MAX_MESSAGE 127
  139. typedef struct {
  140. bool ok;
  141. char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */
  142. } upb_status;
  143. #ifdef __cplusplus
  144. extern "C" {
  145. #endif
  146. const char *upb_status_errmsg(const upb_status *status);
  147. bool upb_ok(const upb_status *status);
  148. /* Any of the functions that write to a status object allow status to be NULL,
  149. * to support use cases where the function's caller does not care about the
  150. * status message. */
  151. void upb_status_clear(upb_status *status);
  152. void upb_status_seterrmsg(upb_status *status, const char *msg);
  153. void upb_status_seterrf(upb_status *status, const char *fmt, ...);
  154. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
  155. UPB_INLINE void upb_status_setoom(upb_status *status) {
  156. upb_status_seterrmsg(status, "out of memory");
  157. }
  158. #ifdef __cplusplus
  159. } /* extern "C" */
  160. class upb::Status {
  161. public:
  162. Status() { upb_status_clear(&status_); }
  163. upb_status* ptr() { return &status_; }
  164. /* Returns true if there is no error. */
  165. bool ok() const { return upb_ok(&status_); }
  166. /* Guaranteed to be NULL-terminated. */
  167. const char *error_message() const { return upb_status_errmsg(&status_); }
  168. /* The error message will be truncated if it is longer than
  169. * UPB_STATUS_MAX_MESSAGE-4. */
  170. void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); }
  171. void SetFormattedErrorMessage(const char *fmt, ...) {
  172. va_list args;
  173. va_start(args, fmt);
  174. upb_status_vseterrf(&status_, fmt, args);
  175. va_end(args);
  176. }
  177. /* Resets the status to a successful state with no message. */
  178. void Clear() { upb_status_clear(&status_); }
  179. private:
  180. upb_status status_;
  181. };
  182. #endif /* __cplusplus */
  183. /** upb_alloc *****************************************************************/
  184. /* A upb_alloc is a possibly-stateful allocator object.
  185. *
  186. * It could either be an arena allocator (which doesn't require individual
  187. * free() calls) or a regular malloc() (which does). The client must therefore
  188. * free memory unless it knows that the allocator is an arena allocator. */
  189. struct upb_alloc;
  190. typedef struct upb_alloc upb_alloc;
  191. /* A malloc()/free() function.
  192. * If "size" is 0 then the function acts like free(), otherwise it acts like
  193. * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
  194. typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
  195. size_t size);
  196. struct upb_alloc {
  197. upb_alloc_func *func;
  198. };
  199. UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
  200. UPB_ASSERT(alloc);
  201. return alloc->func(alloc, NULL, 0, size);
  202. }
  203. UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  204. size_t size) {
  205. UPB_ASSERT(alloc);
  206. return alloc->func(alloc, ptr, oldsize, size);
  207. }
  208. UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
  209. assert(alloc);
  210. alloc->func(alloc, ptr, 0, 0);
  211. }
  212. /* The global allocator used by upb. Uses the standard malloc()/free(). */
  213. extern upb_alloc upb_alloc_global;
  214. /* Functions that hard-code the global malloc.
  215. *
  216. * We still get benefit because we can put custom logic into our global
  217. * allocator, like injecting out-of-memory faults in debug/testing builds. */
  218. UPB_INLINE void *upb_gmalloc(size_t size) {
  219. return upb_malloc(&upb_alloc_global, size);
  220. }
  221. UPB_INLINE void *upb_grealloc(void *ptr, size_t oldsize, size_t size) {
  222. return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
  223. }
  224. UPB_INLINE void upb_gfree(void *ptr) {
  225. upb_free(&upb_alloc_global, ptr);
  226. }
  227. /* upb_arena ******************************************************************/
  228. /* upb_arena is a specific allocator implementation that uses arena allocation.
  229. * The user provides an allocator that will be used to allocate the underlying
  230. * arena blocks. Arenas by nature do not require the individual allocations
  231. * to be freed. However the Arena does allow users to register cleanup
  232. * functions that will run when the arena is destroyed.
  233. *
  234. * A upb_arena is *not* thread-safe.
  235. *
  236. * You could write a thread-safe arena allocator that satisfies the
  237. * upb_alloc interface, but it would not be as efficient for the
  238. * single-threaded case. */
  239. typedef void upb_cleanup_func(void *ud);
  240. struct upb_arena;
  241. typedef struct upb_arena upb_arena;
  242. #ifdef __cplusplus
  243. extern "C" {
  244. #endif
  245. /* Creates an arena from the given initial block (if any -- n may be 0).
  246. * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this
  247. * is a fixed-size arena and cannot grow. */
  248. upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc);
  249. void upb_arena_free(upb_arena *a);
  250. bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func);
  251. size_t upb_arena_bytesallocated(const upb_arena *a);
  252. UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
  253. /* Convenience wrappers around upb_alloc functions. */
  254. UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) {
  255. return upb_malloc(upb_arena_alloc(a), size);
  256. }
  257. UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize,
  258. size_t size) {
  259. return upb_realloc(upb_arena_alloc(a), ptr, oldsize, size);
  260. }
  261. UPB_INLINE upb_arena *upb_arena_new() {
  262. return upb_arena_init(NULL, 0, &upb_alloc_global);
  263. }
  264. #ifdef __cplusplus
  265. } /* extern "C" */
  266. class upb::Arena {
  267. public:
  268. /* A simple arena with no initial memory block and the default allocator. */
  269. Arena() : ptr_(upb_arena_new(), upb_arena_free) {}
  270. upb_arena* ptr() { return ptr_.get(); }
  271. /* Allows this arena to be used as a generic allocator.
  272. *
  273. * The arena does not need free() calls so when using Arena as an allocator
  274. * it is safe to skip them. However they are no-ops so there is no harm in
  275. * calling free() either. */
  276. upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); }
  277. /* Add a cleanup function to run when the arena is destroyed.
  278. * Returns false on out-of-memory. */
  279. bool AddCleanup(void *ud, upb_cleanup_func* func) {
  280. return upb_arena_addcleanup(ptr_.get(), ud, func);
  281. }
  282. /* Total number of bytes that have been allocated. It is undefined what
  283. * Realloc() does to &arena_ counter. */
  284. size_t BytesAllocated() const { return upb_arena_bytesallocated(ptr_.get()); }
  285. private:
  286. std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
  287. };
  288. #endif
  289. /* upb::InlinedArena **********************************************************/
  290. /* upb::InlinedArena seeds the arenas with a predefined amount of memory. No
  291. * heap memory will be allocated until the initial block is exceeded.
  292. *
  293. * These types only exist in C++ */
  294. #ifdef __cplusplus
  295. template <int N> class upb::InlinedArena : public upb::Arena {
  296. public:
  297. InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {}
  298. upb_arena* ptr() { return ptr_.get(); }
  299. private:
  300. InlinedArena(const InlinedArena*) = delete;
  301. InlinedArena& operator=(const InlinedArena*) = delete;
  302. std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
  303. char initial_block_[N];
  304. };
  305. #endif /* __cplusplus */
  306. /* Constants ******************************************************************/
  307. /* Generic function type. */
  308. typedef void upb_func();
  309. /* A list of types as they are encoded on-the-wire. */
  310. typedef enum {
  311. UPB_WIRE_TYPE_VARINT = 0,
  312. UPB_WIRE_TYPE_64BIT = 1,
  313. UPB_WIRE_TYPE_DELIMITED = 2,
  314. UPB_WIRE_TYPE_START_GROUP = 3,
  315. UPB_WIRE_TYPE_END_GROUP = 4,
  316. UPB_WIRE_TYPE_32BIT = 5
  317. } upb_wiretype_t;
  318. /* The types a field can have. Note that this list is not identical to the
  319. * types defined in descriptor.proto, which gives INT32 and SINT32 separate
  320. * types (we distinguish the two with the "integer encoding" enum below). */
  321. typedef enum {
  322. /* Types stored in 1 byte. */
  323. UPB_TYPE_BOOL = 1,
  324. /* Types stored in 4 bytes. */
  325. UPB_TYPE_FLOAT = 2,
  326. UPB_TYPE_INT32 = 3,
  327. UPB_TYPE_UINT32 = 4,
  328. UPB_TYPE_ENUM = 5, /* Enum values are int32. */
  329. /* Types stored as pointers (probably 4 or 8 bytes). */
  330. UPB_TYPE_STRING = 6,
  331. UPB_TYPE_BYTES = 7,
  332. UPB_TYPE_MESSAGE = 8,
  333. /* Types stored as 8 bytes. */
  334. UPB_TYPE_DOUBLE = 9,
  335. UPB_TYPE_INT64 = 10,
  336. UPB_TYPE_UINT64 = 11
  337. } upb_fieldtype_t;
  338. /* The repeated-ness of each field; this matches descriptor.proto. */
  339. typedef enum {
  340. UPB_LABEL_OPTIONAL = 1,
  341. UPB_LABEL_REQUIRED = 2,
  342. UPB_LABEL_REPEATED = 3
  343. } upb_label_t;
  344. /* Descriptor types, as defined in descriptor.proto. */
  345. typedef enum {
  346. UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
  347. UPB_DESCRIPTOR_TYPE_FLOAT = 2,
  348. UPB_DESCRIPTOR_TYPE_INT64 = 3,
  349. UPB_DESCRIPTOR_TYPE_UINT64 = 4,
  350. UPB_DESCRIPTOR_TYPE_INT32 = 5,
  351. UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
  352. UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
  353. UPB_DESCRIPTOR_TYPE_BOOL = 8,
  354. UPB_DESCRIPTOR_TYPE_STRING = 9,
  355. UPB_DESCRIPTOR_TYPE_GROUP = 10,
  356. UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
  357. UPB_DESCRIPTOR_TYPE_BYTES = 12,
  358. UPB_DESCRIPTOR_TYPE_UINT32 = 13,
  359. UPB_DESCRIPTOR_TYPE_ENUM = 14,
  360. UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
  361. UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
  362. UPB_DESCRIPTOR_TYPE_SINT32 = 17,
  363. UPB_DESCRIPTOR_TYPE_SINT64 = 18
  364. } upb_descriptortype_t;
  365. extern const uint8_t upb_desctype_to_fieldtype[];
  366. #endif /* UPB_H_ */
  367. /*
  368. ** structs.int.h: structures definitions that are internal to upb.
  369. */
  370. #ifndef UPB_STRUCTS_H_
  371. #define UPB_STRUCTS_H_
  372. struct upb_array {
  373. upb_fieldtype_t type;
  374. uint8_t element_size;
  375. void *data; /* Each element is element_size. */
  376. size_t len; /* Measured in elements. */
  377. size_t size; /* Measured in elements. */
  378. upb_arena *arena;
  379. };
  380. #endif /* UPB_STRUCTS_H_ */
  381. #ifdef __cplusplus
  382. namespace upb {
  383. class Array;
  384. class Map;
  385. class MapIterator;
  386. class MessageLayout;
  387. }
  388. #endif
  389. /* TODO(haberman): C++ accessors */
  390. #ifdef __cplusplus
  391. extern "C" {
  392. #endif
  393. typedef void upb_msg;
  394. struct upb_array;
  395. typedef struct upb_array upb_array;
  396. struct upb_map;
  397. typedef struct upb_map upb_map;
  398. struct upb_mapiter;
  399. typedef struct upb_mapiter upb_mapiter;
  400. /** upb_msglayout *************************************************************/
  401. /* upb_msglayout represents the memory layout of a given upb_msgdef. The
  402. * members are public so generated code can initialize them, but users MUST NOT
  403. * read or write any of its members. */
  404. typedef struct {
  405. uint32_t number;
  406. uint16_t offset;
  407. int16_t presence; /* If >0, hasbit_index+1. If <0, oneof_index+1. */
  408. uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */
  409. uint8_t descriptortype;
  410. uint8_t label;
  411. } upb_msglayout_field;
  412. typedef struct upb_msglayout {
  413. const struct upb_msglayout *const* submsgs;
  414. const upb_msglayout_field *fields;
  415. /* Must be aligned to sizeof(void*). Doesn't include internal members like
  416. * unknown fields, extension dict, pointer to msglayout, etc. */
  417. uint16_t size;
  418. uint16_t field_count;
  419. bool extendable;
  420. } upb_msglayout;
  421. /** upb_strview ************************************************************/
  422. typedef struct {
  423. const char *data;
  424. size_t size;
  425. } upb_strview;
  426. UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
  427. upb_strview ret;
  428. ret.data = data;
  429. ret.size = size;
  430. return ret;
  431. }
  432. UPB_INLINE upb_strview upb_strview_makez(const char *data) {
  433. return upb_strview_make(data, strlen(data));
  434. }
  435. UPB_INLINE bool upb_strview_eql(upb_strview a, upb_strview b) {
  436. return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
  437. }
  438. #define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
  439. #define UPB_STRVIEW_FORMAT "%.*s"
  440. #define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data
  441. /** upb_msgval ****************************************************************/
  442. /* A union representing all possible protobuf values. Used for generic get/set
  443. * operations. */
  444. typedef union {
  445. bool b;
  446. float flt;
  447. double dbl;
  448. int32_t i32;
  449. int64_t i64;
  450. uint32_t u32;
  451. uint64_t u64;
  452. const upb_map* map;
  453. const upb_msg* msg;
  454. const upb_array* arr;
  455. const void* ptr;
  456. upb_strview str;
  457. } upb_msgval;
  458. #define ACCESSORS(name, membername, ctype) \
  459. UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \
  460. return v.membername; \
  461. } \
  462. UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \
  463. v->membername = cval; \
  464. } \
  465. UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \
  466. upb_msgval ret; \
  467. ret.membername = v; \
  468. return ret; \
  469. }
  470. ACCESSORS(bool, b, bool)
  471. ACCESSORS(float, flt, float)
  472. ACCESSORS(double, dbl, double)
  473. ACCESSORS(int32, i32, int32_t)
  474. ACCESSORS(int64, i64, int64_t)
  475. ACCESSORS(uint32, u32, uint32_t)
  476. ACCESSORS(uint64, u64, uint64_t)
  477. ACCESSORS(map, map, const upb_map*)
  478. ACCESSORS(msg, msg, const upb_msg*)
  479. ACCESSORS(ptr, ptr, const void*)
  480. ACCESSORS(arr, arr, const upb_array*)
  481. ACCESSORS(str, str, upb_strview)
  482. #undef ACCESSORS
  483. UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
  484. return upb_msgval_str(upb_strview_make(data, size));
  485. }
  486. /** upb_msg *******************************************************************/
  487. /* A upb_msg represents a protobuf message. It always corresponds to a specific
  488. * upb_msglayout, which describes how it is laid out in memory. */
  489. /* Creates a new message of the given type/layout in this arena. */
  490. upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a);
  491. /* Returns the arena for the given message. */
  492. upb_arena *upb_msg_arena(const upb_msg *msg);
  493. void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len);
  494. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
  495. /* Read-only message API. Can be safely called by anyone. */
  496. /* Returns the value associated with this field:
  497. * - for scalar fields (including strings), the value directly.
  498. * - return upb_msg*, or upb_map* for msg/map.
  499. * If the field is unset for these field types, returns NULL.
  500. *
  501. * TODO(haberman): should we let users store cached array/map/msg
  502. * pointers here for fields that are unset? Could be useful for the
  503. * strongly-owned submessage model (ie. generated C API that doesn't use
  504. * arenas).
  505. */
  506. upb_msgval upb_msg_get(const upb_msg *msg,
  507. int field_index,
  508. const upb_msglayout *l);
  509. /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
  510. bool upb_msg_has(const upb_msg *msg,
  511. int field_index,
  512. const upb_msglayout *l);
  513. /* Mutable message API. May only be called by the owner of the message who
  514. * knows its ownership scheme and how to keep it consistent. */
  515. /* Sets the given field to the given value. Does not perform any memory
  516. * management: if you overwrite a pointer to a msg/array/map/string without
  517. * cleaning it up (or using an arena) it will leak.
  518. */
  519. void upb_msg_set(upb_msg *msg,
  520. int field_index,
  521. upb_msgval val,
  522. const upb_msglayout *l);
  523. /* For a primitive field, set it back to its default. For repeated, string, and
  524. * submessage fields set it back to NULL. This could involve releasing some
  525. * internal memory (for example, from an extension dictionary), but it is not
  526. * recursive in any way and will not recover any memory that may be used by
  527. * arrays/maps/strings/msgs that this field may have pointed to.
  528. */
  529. bool upb_msg_clearfield(upb_msg *msg,
  530. int field_index,
  531. const upb_msglayout *l);
  532. /* TODO(haberman): copyfrom()/mergefrom()? */
  533. /** upb_array *****************************************************************/
  534. /* A upb_array stores data for a repeated field. The memory management
  535. * semantics are the same as upb_msg. A upb_array allocates dynamic
  536. * memory internally for the array elements. */
  537. upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a);
  538. upb_fieldtype_t upb_array_type(const upb_array *arr);
  539. /* Read-only interface. Safe for anyone to call. */
  540. size_t upb_array_size(const upb_array *arr);
  541. upb_msgval upb_array_get(const upb_array *arr, size_t i);
  542. /* Write interface. May only be called by the message's owner who can enforce
  543. * its memory management invariants. */
  544. bool upb_array_set(upb_array *arr, size_t i, upb_msgval val);
  545. /** upb_map *******************************************************************/
  546. /* A upb_map stores data for a map field. The memory management semantics are
  547. * the same as upb_msg, with one notable exception. upb_map will internally
  548. * store a copy of all string keys, but *not* any string values or submessages.
  549. * So you must ensure that any string or message values outlive the map, and you
  550. * must delete them manually when they are no longer required. */
  551. upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
  552. upb_arena *a);
  553. /* Read-only interface. Safe for anyone to call. */
  554. size_t upb_map_size(const upb_map *map);
  555. upb_fieldtype_t upb_map_keytype(const upb_map *map);
  556. upb_fieldtype_t upb_map_valuetype(const upb_map *map);
  557. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
  558. /* Write interface. May only be called by the message's owner who can enforce
  559. * its memory management invariants. */
  560. /* Sets or overwrites an entry in the map. Return value indicates whether
  561. * the operation succeeded or failed with OOM, and also whether an existing
  562. * key was replaced or not. */
  563. bool upb_map_set(upb_map *map,
  564. upb_msgval key, upb_msgval val,
  565. upb_msgval *valremoved);
  566. /* Deletes an entry in the map. Returns true if the key was present. */
  567. bool upb_map_del(upb_map *map, upb_msgval key);
  568. /** upb_mapiter ***************************************************************/
  569. /* For iterating over a map. Map iterators are invalidated by mutations to the
  570. * map, but an invalidated iterator will never return junk or crash the process.
  571. * An invalidated iterator may return entries that were already returned though,
  572. * and if you keep invalidating the iterator during iteration, the program may
  573. * enter an infinite loop. */
  574. size_t upb_mapiter_sizeof();
  575. void upb_mapiter_begin(upb_mapiter *i, const upb_map *t);
  576. upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a);
  577. void upb_mapiter_free(upb_mapiter *i, upb_alloc *a);
  578. void upb_mapiter_next(upb_mapiter *i);
  579. bool upb_mapiter_done(const upb_mapiter *i);
  580. upb_msgval upb_mapiter_key(const upb_mapiter *i);
  581. upb_msgval upb_mapiter_value(const upb_mapiter *i);
  582. void upb_mapiter_setdone(upb_mapiter *i);
  583. bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2);
  584. #ifdef __cplusplus
  585. } /* extern "C" */
  586. #endif
  587. #endif /* UPB_MSG_H_ */
  588. /* This file was generated by upbc (the upb compiler) from the input
  589. * file:
  590. *
  591. * google/protobuf/descriptor.proto
  592. *
  593. * Do not edit -- your changes will be discarded when the file is
  594. * regenerated. */
  595. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
  596. #define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
  597. /*
  598. ** Functions for use by generated code. These are not public and users must
  599. ** not call them directly.
  600. */
  601. #ifndef UPB_GENERATED_UTIL_H_
  602. #define UPB_GENERATED_UTIL_H_
  603. #include <stdint.h>
  604. #define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
  605. UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
  606. size_t *size) {
  607. const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
  608. if (arr) {
  609. if (size) *size = arr->len;
  610. return arr->data;
  611. } else {
  612. if (size) *size = 0;
  613. return NULL;
  614. }
  615. }
  616. UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
  617. size_t *size) {
  618. upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
  619. if (arr) {
  620. if (size) *size = arr->len;
  621. return arr->data;
  622. } else {
  623. if (size) *size = 0;
  624. return NULL;
  625. }
  626. }
  627. /* TODO(haberman): this is a mess. It will improve when upb_array no longer
  628. * carries reflective state (type, elem_size). */
  629. UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
  630. size_t elem_size,
  631. upb_fieldtype_t type,
  632. upb_arena *arena) {
  633. upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
  634. if (!arr) {
  635. arr = upb_array_new(type, arena);
  636. if (!arr) return NULL;
  637. *PTR_AT(msg, ofs, upb_array*) = arr;
  638. }
  639. if (size > arr->size) {
  640. size_t new_size = UPB_MAX(arr->size, 4);
  641. size_t old_bytes = arr->size * elem_size;
  642. size_t new_bytes;
  643. while (new_size < size) new_size *= 2;
  644. new_bytes = new_size * elem_size;
  645. arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes);
  646. if (!arr->data) {
  647. return NULL;
  648. }
  649. arr->size = new_size;
  650. }
  651. arr->len = size;
  652. return arr->data;
  653. }
  654. UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
  655. size_t elem_size,
  656. upb_fieldtype_t type,
  657. const void *value,
  658. upb_arena *arena) {
  659. upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
  660. size_t i = arr ? arr->len : 0;
  661. void *data =
  662. _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
  663. if (!data) return false;
  664. memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
  665. return true;
  666. }
  667. UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
  668. return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
  669. }
  670. UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
  671. return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8));
  672. }
  673. UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
  674. return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8));
  675. }
  676. UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
  677. return *PTR_AT(msg, case_ofs, int32_t) == num;
  678. }
  679. #undef PTR_AT
  680. #endif /* UPB_GENERATED_UTIL_H_ */
  681. /*
  682. ** upb_decode: parsing into a upb_msg using a upb_msglayout.
  683. */
  684. #ifndef UPB_DECODE_H_
  685. #define UPB_DECODE_H_
  686. #ifdef __cplusplus
  687. extern "C" {
  688. #endif
  689. bool upb_decode(const char *buf, size_t size, upb_msg *msg,
  690. const upb_msglayout *l);
  691. #ifdef __cplusplus
  692. } /* extern "C" */
  693. #endif
  694. #endif /* UPB_DECODE_H_ */
  695. /*
  696. ** upb_encode: parsing into a upb_msg using a upb_msglayout.
  697. */
  698. #ifndef UPB_ENCODE_H_
  699. #define UPB_ENCODE_H_
  700. #ifdef __cplusplus
  701. extern "C" {
  702. #endif
  703. char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
  704. size_t *size);
  705. #ifdef __cplusplus
  706. } /* extern "C" */
  707. #endif
  708. #endif /* UPB_ENCODE_H_ */
  709. #ifdef __cplusplus
  710. extern "C" {
  711. #endif
  712. struct google_protobuf_FileDescriptorSet;
  713. struct google_protobuf_FileDescriptorProto;
  714. struct google_protobuf_DescriptorProto;
  715. struct google_protobuf_DescriptorProto_ExtensionRange;
  716. struct google_protobuf_DescriptorProto_ReservedRange;
  717. struct google_protobuf_ExtensionRangeOptions;
  718. struct google_protobuf_FieldDescriptorProto;
  719. struct google_protobuf_OneofDescriptorProto;
  720. struct google_protobuf_EnumDescriptorProto;
  721. struct google_protobuf_EnumDescriptorProto_EnumReservedRange;
  722. struct google_protobuf_EnumValueDescriptorProto;
  723. struct google_protobuf_ServiceDescriptorProto;
  724. struct google_protobuf_MethodDescriptorProto;
  725. struct google_protobuf_FileOptions;
  726. struct google_protobuf_MessageOptions;
  727. struct google_protobuf_FieldOptions;
  728. struct google_protobuf_OneofOptions;
  729. struct google_protobuf_EnumOptions;
  730. struct google_protobuf_EnumValueOptions;
  731. struct google_protobuf_ServiceOptions;
  732. struct google_protobuf_MethodOptions;
  733. struct google_protobuf_UninterpretedOption;
  734. struct google_protobuf_UninterpretedOption_NamePart;
  735. struct google_protobuf_SourceCodeInfo;
  736. struct google_protobuf_SourceCodeInfo_Location;
  737. struct google_protobuf_GeneratedCodeInfo;
  738. struct google_protobuf_GeneratedCodeInfo_Annotation;
  739. typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet;
  740. typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto;
  741. typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto;
  742. typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange;
  743. typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange;
  744. typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions;
  745. typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto;
  746. typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto;
  747. typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto;
  748. typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange;
  749. typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto;
  750. typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto;
  751. typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto;
  752. typedef struct google_protobuf_FileOptions google_protobuf_FileOptions;
  753. typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions;
  754. typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions;
  755. typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions;
  756. typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions;
  757. typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions;
  758. typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions;
  759. typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions;
  760. typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption;
  761. typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart;
  762. typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo;
  763. typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location;
  764. typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo;
  765. typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation;
  766. extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
  767. extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
  768. extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
  769. extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
  770. extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
  771. extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
  772. extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
  773. extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
  774. extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
  775. extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
  776. extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
  777. extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
  778. extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
  779. extern const upb_msglayout google_protobuf_FileOptions_msginit;
  780. extern const upb_msglayout google_protobuf_MessageOptions_msginit;
  781. extern const upb_msglayout google_protobuf_FieldOptions_msginit;
  782. extern const upb_msglayout google_protobuf_OneofOptions_msginit;
  783. extern const upb_msglayout google_protobuf_EnumOptions_msginit;
  784. extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
  785. extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
  786. extern const upb_msglayout google_protobuf_MethodOptions_msginit;
  787. extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
  788. extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
  789. extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
  790. extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
  791. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
  792. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
  793. /* Enums */
  794. typedef enum {
  795. google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1,
  796. google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2,
  797. google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3
  798. } google_protobuf_FieldDescriptorProto_Label;
  799. typedef enum {
  800. google_protobuf_FieldDescriptorProto_TYPE_DOUBLE = 1,
  801. google_protobuf_FieldDescriptorProto_TYPE_FLOAT = 2,
  802. google_protobuf_FieldDescriptorProto_TYPE_INT64 = 3,
  803. google_protobuf_FieldDescriptorProto_TYPE_UINT64 = 4,
  804. google_protobuf_FieldDescriptorProto_TYPE_INT32 = 5,
  805. google_protobuf_FieldDescriptorProto_TYPE_FIXED64 = 6,
  806. google_protobuf_FieldDescriptorProto_TYPE_FIXED32 = 7,
  807. google_protobuf_FieldDescriptorProto_TYPE_BOOL = 8,
  808. google_protobuf_FieldDescriptorProto_TYPE_STRING = 9,
  809. google_protobuf_FieldDescriptorProto_TYPE_GROUP = 10,
  810. google_protobuf_FieldDescriptorProto_TYPE_MESSAGE = 11,
  811. google_protobuf_FieldDescriptorProto_TYPE_BYTES = 12,
  812. google_protobuf_FieldDescriptorProto_TYPE_UINT32 = 13,
  813. google_protobuf_FieldDescriptorProto_TYPE_ENUM = 14,
  814. google_protobuf_FieldDescriptorProto_TYPE_SFIXED32 = 15,
  815. google_protobuf_FieldDescriptorProto_TYPE_SFIXED64 = 16,
  816. google_protobuf_FieldDescriptorProto_TYPE_SINT32 = 17,
  817. google_protobuf_FieldDescriptorProto_TYPE_SINT64 = 18
  818. } google_protobuf_FieldDescriptorProto_Type;
  819. typedef enum {
  820. google_protobuf_FieldOptions_STRING = 0,
  821. google_protobuf_FieldOptions_CORD = 1,
  822. google_protobuf_FieldOptions_STRING_PIECE = 2
  823. } google_protobuf_FieldOptions_CType;
  824. typedef enum {
  825. google_protobuf_FieldOptions_JS_NORMAL = 0,
  826. google_protobuf_FieldOptions_JS_STRING = 1,
  827. google_protobuf_FieldOptions_JS_NUMBER = 2
  828. } google_protobuf_FieldOptions_JSType;
  829. typedef enum {
  830. google_protobuf_FileOptions_SPEED = 1,
  831. google_protobuf_FileOptions_CODE_SIZE = 2,
  832. google_protobuf_FileOptions_LITE_RUNTIME = 3
  833. } google_protobuf_FileOptions_OptimizeMode;
  834. typedef enum {
  835. google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0,
  836. google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1,
  837. google_protobuf_MethodOptions_IDEMPOTENT = 2
  838. } google_protobuf_MethodOptions_IdempotencyLevel;
  839. /* google.protobuf.FileDescriptorSet */
  840. UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) {
  841. return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena);
  842. }
  843. UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parse(const char *buf, size_t size,
  844. upb_arena *arena) {
  845. google_protobuf_FileDescriptorSet *ret = google_protobuf_FileDescriptorSet_new(arena);
  846. return (ret && upb_decode(buf, size, ret, &google_protobuf_FileDescriptorSet_msginit)) ? ret : NULL;
  847. }
  848. UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) {
  849. return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len);
  850. }
  851. UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg, size_t *len) { return (const google_protobuf_FileDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  852. UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_mutable_file(google_protobuf_FileDescriptorSet *msg, size_t *len) {
  853. return (google_protobuf_FileDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  854. }
  855. UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_resize_file(google_protobuf_FileDescriptorSet *msg, size_t len, upb_arena *arena) {
  856. return (google_protobuf_FileDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  857. }
  858. UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorSet_add_file(google_protobuf_FileDescriptorSet *msg, upb_arena *arena) {
  859. struct google_protobuf_FileDescriptorProto* sub = (struct google_protobuf_FileDescriptorProto*)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
  860. bool ok = _upb_array_append_accessor(
  861. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  862. if (!ok) return NULL;
  863. return sub;
  864. }
  865. /* google.protobuf.FileDescriptorProto */
  866. UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) {
  867. return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
  868. }
  869. UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parse(const char *buf, size_t size,
  870. upb_arena *arena) {
  871. google_protobuf_FileDescriptorProto *ret = google_protobuf_FileDescriptorProto_new(arena);
  872. return (ret && upb_decode(buf, size, ret, &google_protobuf_FileDescriptorProto_msginit)) ? ret : NULL;
  873. }
  874. UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) {
  875. return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len);
  876. }
  877. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  878. UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  879. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  880. UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
  881. UPB_INLINE upb_strview const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); }
  882. UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); }
  883. UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); }
  884. UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_ServiceDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(48, 96), len); }
  885. UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(52, 104), len); }
  886. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); }
  887. UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FileOptions*, UPB_SIZE(28, 56)); }
  888. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 5); }
  889. UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)); }
  890. UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(56, 112), len); }
  891. UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(60, 120), len); }
  892. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  893. UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); }
  894. UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
  895. _upb_sethas(msg, 1);
  896. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  897. }
  898. UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
  899. _upb_sethas(msg, 2);
  900. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
  901. }
  902. UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_mutable_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  903. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len);
  904. }
  905. UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_resize_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  906. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  907. }
  908. UPB_INLINE bool google_protobuf_FileDescriptorProto_add_dependency(google_protobuf_FileDescriptorProto *msg, upb_strview val, upb_arena *arena) {
  909. return _upb_array_append_accessor(
  910. msg, UPB_SIZE(36, 72), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  911. }
  912. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_mutable_message_type(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  913. return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len);
  914. }
  915. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_resize_message_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  916. return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  917. }
  918. UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_FileDescriptorProto_add_message_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  919. struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  920. bool ok = _upb_array_append_accessor(
  921. msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  922. if (!ok) return NULL;
  923. return sub;
  924. }
  925. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_mutable_enum_type(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  926. return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len);
  927. }
  928. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_resize_enum_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  929. return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  930. }
  931. UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_FileDescriptorProto_add_enum_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  932. struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  933. bool ok = _upb_array_append_accessor(
  934. msg, UPB_SIZE(44, 88), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  935. if (!ok) return NULL;
  936. return sub;
  937. }
  938. UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_mutable_service(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  939. return (google_protobuf_ServiceDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(48, 96), len);
  940. }
  941. UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_resize_service(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  942. return (google_protobuf_ServiceDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(48, 96), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  943. }
  944. UPB_INLINE struct google_protobuf_ServiceDescriptorProto* google_protobuf_FileDescriptorProto_add_service(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  945. struct google_protobuf_ServiceDescriptorProto* sub = (struct google_protobuf_ServiceDescriptorProto*)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
  946. bool ok = _upb_array_append_accessor(
  947. msg, UPB_SIZE(48, 96), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  948. if (!ok) return NULL;
  949. return sub;
  950. }
  951. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_mutable_extension(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  952. return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(52, 104), len);
  953. }
  954. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_resize_extension(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  955. return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(52, 104), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  956. }
  957. UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_FileDescriptorProto_add_extension(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  958. struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  959. bool ok = _upb_array_append_accessor(
  960. msg, UPB_SIZE(52, 104), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  961. if (!ok) return NULL;
  962. return sub;
  963. }
  964. UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) {
  965. _upb_sethas(msg, 4);
  966. UPB_FIELD_AT(msg, google_protobuf_FileOptions*, UPB_SIZE(28, 56)) = value;
  967. }
  968. UPB_INLINE struct google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_mutable_options(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  969. struct google_protobuf_FileOptions* sub = (struct google_protobuf_FileOptions*)google_protobuf_FileDescriptorProto_options(msg);
  970. if (sub == NULL) {
  971. sub = (struct google_protobuf_FileOptions*)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
  972. if (!sub) return NULL;
  973. google_protobuf_FileDescriptorProto_set_options(msg, sub);
  974. }
  975. return sub;
  976. }
  977. UPB_INLINE void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value) {
  978. _upb_sethas(msg, 5);
  979. UPB_FIELD_AT(msg, google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)) = value;
  980. }
  981. UPB_INLINE struct google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_mutable_source_code_info(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  982. struct google_protobuf_SourceCodeInfo* sub = (struct google_protobuf_SourceCodeInfo*)google_protobuf_FileDescriptorProto_source_code_info(msg);
  983. if (sub == NULL) {
  984. sub = (struct google_protobuf_SourceCodeInfo*)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
  985. if (!sub) return NULL;
  986. google_protobuf_FileDescriptorProto_set_source_code_info(msg, sub);
  987. }
  988. return sub;
  989. }
  990. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  991. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 112), len);
  992. }
  993. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  994. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(56, 112), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  995. }
  996. UPB_INLINE bool google_protobuf_FileDescriptorProto_add_public_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) {
  997. return _upb_array_append_accessor(
  998. msg, UPB_SIZE(56, 112), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  999. }
  1000. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  1001. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(60, 120), len);
  1002. }
  1003. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  1004. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(60, 120), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  1005. }
  1006. UPB_INLINE bool google_protobuf_FileDescriptorProto_add_weak_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) {
  1007. return _upb_array_append_accessor(
  1008. msg, UPB_SIZE(60, 120), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  1009. }
  1010. UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
  1011. _upb_sethas(msg, 3);
  1012. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value;
  1013. }
  1014. /* google.protobuf.DescriptorProto */
  1015. UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) {
  1016. return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  1017. }
  1018. UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parse(const char *buf, size_t size,
  1019. upb_arena *arena) {
  1020. google_protobuf_DescriptorProto *ret = google_protobuf_DescriptorProto_new(arena);
  1021. return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_msginit)) ? ret : NULL;
  1022. }
  1023. UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) {
  1024. return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len);
  1025. }
  1026. UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1027. UPB_INLINE upb_strview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1028. UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
  1029. UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
  1030. UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
  1031. UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ExtensionRange* const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); }
  1032. UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(32, 64), len); }
  1033. UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1034. UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MessageOptions*, UPB_SIZE(12, 24)); }
  1035. UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_OneofDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); }
  1036. UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); }
  1037. UPB_INLINE upb_strview const* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); }
  1038. UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_strview value) {
  1039. _upb_sethas(msg, 1);
  1040. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1041. }
  1042. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_field(google_protobuf_DescriptorProto *msg, size_t *len) {
  1043. return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
  1044. }
  1045. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_field(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1046. return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1047. }
  1048. UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_field(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1049. struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  1050. bool ok = _upb_array_append_accessor(
  1051. msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1052. if (!ok) return NULL;
  1053. return sub;
  1054. }
  1055. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_mutable_nested_type(google_protobuf_DescriptorProto *msg, size_t *len) {
  1056. return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
  1057. }
  1058. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_resize_nested_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1059. return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1060. }
  1061. UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_add_nested_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1062. struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  1063. bool ok = _upb_array_append_accessor(
  1064. msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1065. if (!ok) return NULL;
  1066. return sub;
  1067. }
  1068. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_mutable_enum_type(google_protobuf_DescriptorProto *msg, size_t *len) {
  1069. return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
  1070. }
  1071. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_resize_enum_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1072. return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1073. }
  1074. UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_DescriptorProto_add_enum_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1075. struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  1076. bool ok = _upb_array_append_accessor(
  1077. msg, UPB_SIZE(24, 48), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1078. if (!ok) return NULL;
  1079. return sub;
  1080. }
  1081. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_mutable_extension_range(google_protobuf_DescriptorProto *msg, size_t *len) {
  1082. return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len);
  1083. }
  1084. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_resize_extension_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1085. return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1086. }
  1087. UPB_INLINE struct google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_add_extension_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1088. struct google_protobuf_DescriptorProto_ExtensionRange* sub = (struct google_protobuf_DescriptorProto_ExtensionRange*)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
  1089. bool ok = _upb_array_append_accessor(
  1090. msg, UPB_SIZE(28, 56), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1091. if (!ok) return NULL;
  1092. return sub;
  1093. }
  1094. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_extension(google_protobuf_DescriptorProto *msg, size_t *len) {
  1095. return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(32, 64), len);
  1096. }
  1097. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_extension(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1098. return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(32, 64), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1099. }
  1100. UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_extension(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1101. struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  1102. bool ok = _upb_array_append_accessor(
  1103. msg, UPB_SIZE(32, 64), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1104. if (!ok) return NULL;
  1105. return sub;
  1106. }
  1107. UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) {
  1108. _upb_sethas(msg, 2);
  1109. UPB_FIELD_AT(msg, google_protobuf_MessageOptions*, UPB_SIZE(12, 24)) = value;
  1110. }
  1111. UPB_INLINE struct google_protobuf_MessageOptions* google_protobuf_DescriptorProto_mutable_options(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1112. struct google_protobuf_MessageOptions* sub = (struct google_protobuf_MessageOptions*)google_protobuf_DescriptorProto_options(msg);
  1113. if (sub == NULL) {
  1114. sub = (struct google_protobuf_MessageOptions*)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
  1115. if (!sub) return NULL;
  1116. google_protobuf_DescriptorProto_set_options(msg, sub);
  1117. }
  1118. return sub;
  1119. }
  1120. UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_mutable_oneof_decl(google_protobuf_DescriptorProto *msg, size_t *len) {
  1121. return (google_protobuf_OneofDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len);
  1122. }
  1123. UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_resize_oneof_decl(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1124. return (google_protobuf_OneofDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1125. }
  1126. UPB_INLINE struct google_protobuf_OneofDescriptorProto* google_protobuf_DescriptorProto_add_oneof_decl(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1127. struct google_protobuf_OneofDescriptorProto* sub = (struct google_protobuf_OneofDescriptorProto*)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
  1128. bool ok = _upb_array_append_accessor(
  1129. msg, UPB_SIZE(36, 72), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1130. if (!ok) return NULL;
  1131. return sub;
  1132. }
  1133. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_mutable_reserved_range(google_protobuf_DescriptorProto *msg, size_t *len) {
  1134. return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len);
  1135. }
  1136. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_resize_reserved_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1137. return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1138. }
  1139. UPB_INLINE struct google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_add_reserved_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1140. struct google_protobuf_DescriptorProto_ReservedRange* sub = (struct google_protobuf_DescriptorProto_ReservedRange*)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
  1141. bool ok = _upb_array_append_accessor(
  1142. msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1143. if (!ok) return NULL;
  1144. return sub;
  1145. }
  1146. UPB_INLINE upb_strview* google_protobuf_DescriptorProto_mutable_reserved_name(google_protobuf_DescriptorProto *msg, size_t *len) {
  1147. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len);
  1148. }
  1149. UPB_INLINE upb_strview* google_protobuf_DescriptorProto_resize_reserved_name(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1150. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  1151. }
  1152. UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobuf_DescriptorProto *msg, upb_strview val, upb_arena *arena) {
  1153. return _upb_array_append_accessor(
  1154. msg, UPB_SIZE(44, 88), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  1155. }
  1156. /* google.protobuf.DescriptorProto.ExtensionRange */
  1157. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) {
  1158. return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
  1159. }
  1160. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parse(const char *buf, size_t size,
  1161. upb_arena *arena) {
  1162. google_protobuf_DescriptorProto_ExtensionRange *ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena);
  1163. return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_ExtensionRange_msginit)) ? ret : NULL;
  1164. }
  1165. UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) {
  1166. return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len);
  1167. }
  1168. UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); }
  1169. UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  1170. UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); }
  1171. UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1172. UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 3); }
  1173. UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)); }
  1174. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
  1175. _upb_sethas(msg, 1);
  1176. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1177. }
  1178. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
  1179. _upb_sethas(msg, 2);
  1180. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1181. }
  1182. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) {
  1183. _upb_sethas(msg, 3);
  1184. UPB_FIELD_AT(msg, google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)) = value;
  1185. }
  1186. UPB_INLINE struct google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_mutable_options(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena) {
  1187. struct google_protobuf_ExtensionRangeOptions* sub = (struct google_protobuf_ExtensionRangeOptions*)google_protobuf_DescriptorProto_ExtensionRange_options(msg);
  1188. if (sub == NULL) {
  1189. sub = (struct google_protobuf_ExtensionRangeOptions*)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
  1190. if (!sub) return NULL;
  1191. google_protobuf_DescriptorProto_ExtensionRange_set_options(msg, sub);
  1192. }
  1193. return sub;
  1194. }
  1195. /* google.protobuf.DescriptorProto.ReservedRange */
  1196. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) {
  1197. return (google_protobuf_DescriptorProto_ReservedRange *)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
  1198. }
  1199. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parse(const char *buf, size_t size,
  1200. upb_arena *arena) {
  1201. google_protobuf_DescriptorProto_ReservedRange *ret = google_protobuf_DescriptorProto_ReservedRange_new(arena);
  1202. return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_ReservedRange_msginit)) ? ret : NULL;
  1203. }
  1204. UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) {
  1205. return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len);
  1206. }
  1207. UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); }
  1208. UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  1209. UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 2); }
  1210. UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1211. UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
  1212. _upb_sethas(msg, 1);
  1213. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1214. }
  1215. UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
  1216. _upb_sethas(msg, 2);
  1217. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1218. }
  1219. /* google.protobuf.ExtensionRangeOptions */
  1220. UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) {
  1221. return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
  1222. }
  1223. UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parse(const char *buf, size_t size,
  1224. upb_arena *arena) {
  1225. google_protobuf_ExtensionRangeOptions *ret = google_protobuf_ExtensionRangeOptions_new(arena);
  1226. return (ret && upb_decode(buf, size, ret, &google_protobuf_ExtensionRangeOptions_msginit)) ? ret : NULL;
  1227. }
  1228. UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) {
  1229. return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len);
  1230. }
  1231. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  1232. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_mutable_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t *len) {
  1233. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  1234. }
  1235. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_resize_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t len, upb_arena *arena) {
  1236. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1237. }
  1238. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ExtensionRangeOptions_add_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena) {
  1239. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1240. bool ok = _upb_array_append_accessor(
  1241. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1242. if (!ok) return NULL;
  1243. return sub;
  1244. }
  1245. /* google.protobuf.FieldDescriptorProto */
  1246. UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) {
  1247. return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  1248. }
  1249. UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parse(const char *buf, size_t size,
  1250. upb_arena *arena) {
  1251. google_protobuf_FieldDescriptorProto *ret = google_protobuf_FieldDescriptorProto_new(arena);
  1252. return (ret && upb_decode(buf, size, ret, &google_protobuf_FieldDescriptorProto_msginit)) ? ret : NULL;
  1253. }
  1254. UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1255. return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len);
  1256. }
  1257. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); }
  1258. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); }
  1259. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); }
  1260. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); }
  1261. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  1262. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); }
  1263. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1264. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1265. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1266. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)); }
  1267. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); }
  1268. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); }
  1269. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); }
  1270. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)); }
  1271. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 10); }
  1272. UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FieldOptions*, UPB_SIZE(72, 112)); }
  1273. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); }
  1274. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)); }
  1275. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); }
  1276. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)); }
  1277. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1278. _upb_sethas(msg, 5);
  1279. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value;
  1280. }
  1281. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1282. _upb_sethas(msg, 6);
  1283. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value;
  1284. }
  1285. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1286. _upb_sethas(msg, 3);
  1287. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)) = value;
  1288. }
  1289. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1290. _upb_sethas(msg, 1);
  1291. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1292. }
  1293. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1294. _upb_sethas(msg, 2);
  1295. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)) = value;
  1296. }
  1297. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1298. _upb_sethas(msg, 7);
  1299. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value;
  1300. }
  1301. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1302. _upb_sethas(msg, 8);
  1303. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)) = value;
  1304. }
  1305. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) {
  1306. _upb_sethas(msg, 10);
  1307. UPB_FIELD_AT(msg, google_protobuf_FieldOptions*, UPB_SIZE(72, 112)) = value;
  1308. }
  1309. UPB_INLINE struct google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_mutable_options(google_protobuf_FieldDescriptorProto *msg, upb_arena *arena) {
  1310. struct google_protobuf_FieldOptions* sub = (struct google_protobuf_FieldOptions*)google_protobuf_FieldDescriptorProto_options(msg);
  1311. if (sub == NULL) {
  1312. sub = (struct google_protobuf_FieldOptions*)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
  1313. if (!sub) return NULL;
  1314. google_protobuf_FieldDescriptorProto_set_options(msg, sub);
  1315. }
  1316. return sub;
  1317. }
  1318. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1319. _upb_sethas(msg, 4);
  1320. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)) = value;
  1321. }
  1322. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1323. _upb_sethas(msg, 9);
  1324. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)) = value;
  1325. }
  1326. /* google.protobuf.OneofDescriptorProto */
  1327. UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) {
  1328. return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
  1329. }
  1330. UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parse(const char *buf, size_t size,
  1331. upb_arena *arena) {
  1332. google_protobuf_OneofDescriptorProto *ret = google_protobuf_OneofDescriptorProto_new(arena);
  1333. return (ret && upb_decode(buf, size, ret, &google_protobuf_OneofDescriptorProto_msginit)) ? ret : NULL;
  1334. }
  1335. UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1336. return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len);
  1337. }
  1338. UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1339. UPB_INLINE upb_strview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1340. UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1341. UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_OneofOptions*, UPB_SIZE(12, 24)); }
  1342. UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_strview value) {
  1343. _upb_sethas(msg, 1);
  1344. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1345. }
  1346. UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) {
  1347. _upb_sethas(msg, 2);
  1348. UPB_FIELD_AT(msg, google_protobuf_OneofOptions*, UPB_SIZE(12, 24)) = value;
  1349. }
  1350. UPB_INLINE struct google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_mutable_options(google_protobuf_OneofDescriptorProto *msg, upb_arena *arena) {
  1351. struct google_protobuf_OneofOptions* sub = (struct google_protobuf_OneofOptions*)google_protobuf_OneofDescriptorProto_options(msg);
  1352. if (sub == NULL) {
  1353. sub = (struct google_protobuf_OneofOptions*)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
  1354. if (!sub) return NULL;
  1355. google_protobuf_OneofDescriptorProto_set_options(msg, sub);
  1356. }
  1357. return sub;
  1358. }
  1359. /* google.protobuf.EnumDescriptorProto */
  1360. UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) {
  1361. return (google_protobuf_EnumDescriptorProto *)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  1362. }
  1363. UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parse(const char *buf, size_t size,
  1364. upb_arena *arena) {
  1365. google_protobuf_EnumDescriptorProto *ret = google_protobuf_EnumDescriptorProto_new(arena);
  1366. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumDescriptorProto_msginit)) ? ret : NULL;
  1367. }
  1368. UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1369. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len);
  1370. }
  1371. UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1372. UPB_INLINE upb_strview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1373. UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumValueDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
  1374. UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1375. UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumOptions*, UPB_SIZE(12, 24)); }
  1376. UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
  1377. UPB_INLINE upb_strview const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
  1378. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_strview value) {
  1379. _upb_sethas(msg, 1);
  1380. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1381. }
  1382. UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_mutable_value(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
  1383. return (google_protobuf_EnumValueDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
  1384. }
  1385. UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_resize_value(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
  1386. return (google_protobuf_EnumValueDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1387. }
  1388. UPB_INLINE struct google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumDescriptorProto_add_value(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
  1389. struct google_protobuf_EnumValueDescriptorProto* sub = (struct google_protobuf_EnumValueDescriptorProto*)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
  1390. bool ok = _upb_array_append_accessor(
  1391. msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1392. if (!ok) return NULL;
  1393. return sub;
  1394. }
  1395. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) {
  1396. _upb_sethas(msg, 2);
  1397. UPB_FIELD_AT(msg, google_protobuf_EnumOptions*, UPB_SIZE(12, 24)) = value;
  1398. }
  1399. UPB_INLINE struct google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_mutable_options(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
  1400. struct google_protobuf_EnumOptions* sub = (struct google_protobuf_EnumOptions*)google_protobuf_EnumDescriptorProto_options(msg);
  1401. if (sub == NULL) {
  1402. sub = (struct google_protobuf_EnumOptions*)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
  1403. if (!sub) return NULL;
  1404. google_protobuf_EnumDescriptorProto_set_options(msg, sub);
  1405. }
  1406. return sub;
  1407. }
  1408. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_mutable_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
  1409. return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
  1410. }
  1411. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_resize_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
  1412. return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1413. }
  1414. UPB_INLINE struct google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_add_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
  1415. struct google_protobuf_EnumDescriptorProto_EnumReservedRange* sub = (struct google_protobuf_EnumDescriptorProto_EnumReservedRange*)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
  1416. bool ok = _upb_array_append_accessor(
  1417. msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1418. if (!ok) return NULL;
  1419. return sub;
  1420. }
  1421. UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_mutable_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
  1422. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
  1423. }
  1424. UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_resize_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
  1425. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  1426. }
  1427. UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_strview val, upb_arena *arena) {
  1428. return _upb_array_append_accessor(
  1429. msg, UPB_SIZE(24, 48), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  1430. }
  1431. /* google.protobuf.EnumDescriptorProto.EnumReservedRange */
  1432. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) {
  1433. return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
  1434. }
  1435. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parse(const char *buf, size_t size,
  1436. upb_arena *arena) {
  1437. google_protobuf_EnumDescriptorProto_EnumReservedRange *ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena);
  1438. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit)) ? ret : NULL;
  1439. }
  1440. UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) {
  1441. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len);
  1442. }
  1443. UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); }
  1444. UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  1445. UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 2); }
  1446. UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1447. UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
  1448. _upb_sethas(msg, 1);
  1449. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1450. }
  1451. UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
  1452. _upb_sethas(msg, 2);
  1453. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1454. }
  1455. /* google.protobuf.EnumValueDescriptorProto */
  1456. UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) {
  1457. return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
  1458. }
  1459. UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parse(const char *buf, size_t size,
  1460. upb_arena *arena) {
  1461. google_protobuf_EnumValueDescriptorProto *ret = google_protobuf_EnumValueDescriptorProto_new(arena);
  1462. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumValueDescriptorProto_msginit)) ? ret : NULL;
  1463. }
  1464. UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1465. return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len);
  1466. }
  1467. UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1468. UPB_INLINE upb_strview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)); }
  1469. UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1470. UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  1471. UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  1472. UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)); }
  1473. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_strview value) {
  1474. _upb_sethas(msg, 2);
  1475. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)) = value;
  1476. }
  1477. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) {
  1478. _upb_sethas(msg, 1);
  1479. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1480. }
  1481. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) {
  1482. _upb_sethas(msg, 3);
  1483. UPB_FIELD_AT(msg, google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)) = value;
  1484. }
  1485. UPB_INLINE struct google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_mutable_options(google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena) {
  1486. struct google_protobuf_EnumValueOptions* sub = (struct google_protobuf_EnumValueOptions*)google_protobuf_EnumValueDescriptorProto_options(msg);
  1487. if (sub == NULL) {
  1488. sub = (struct google_protobuf_EnumValueOptions*)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
  1489. if (!sub) return NULL;
  1490. google_protobuf_EnumValueDescriptorProto_set_options(msg, sub);
  1491. }
  1492. return sub;
  1493. }
  1494. /* google.protobuf.ServiceDescriptorProto */
  1495. UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) {
  1496. return (google_protobuf_ServiceDescriptorProto *)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
  1497. }
  1498. UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parse(const char *buf, size_t size,
  1499. upb_arena *arena) {
  1500. google_protobuf_ServiceDescriptorProto *ret = google_protobuf_ServiceDescriptorProto_new(arena);
  1501. return (ret && upb_decode(buf, size, ret, &google_protobuf_ServiceDescriptorProto_msginit)) ? ret : NULL;
  1502. }
  1503. UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1504. return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len);
  1505. }
  1506. UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1507. UPB_INLINE upb_strview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1508. UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg, size_t *len) { return (const google_protobuf_MethodDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
  1509. UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1510. UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)); }
  1511. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_strview value) {
  1512. _upb_sethas(msg, 1);
  1513. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1514. }
  1515. UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_mutable_method(google_protobuf_ServiceDescriptorProto *msg, size_t *len) {
  1516. return (google_protobuf_MethodDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
  1517. }
  1518. UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_resize_method(google_protobuf_ServiceDescriptorProto *msg, size_t len, upb_arena *arena) {
  1519. return (google_protobuf_MethodDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1520. }
  1521. UPB_INLINE struct google_protobuf_MethodDescriptorProto* google_protobuf_ServiceDescriptorProto_add_method(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) {
  1522. struct google_protobuf_MethodDescriptorProto* sub = (struct google_protobuf_MethodDescriptorProto*)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
  1523. bool ok = _upb_array_append_accessor(
  1524. msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1525. if (!ok) return NULL;
  1526. return sub;
  1527. }
  1528. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) {
  1529. _upb_sethas(msg, 2);
  1530. UPB_FIELD_AT(msg, google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)) = value;
  1531. }
  1532. UPB_INLINE struct google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_mutable_options(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) {
  1533. struct google_protobuf_ServiceOptions* sub = (struct google_protobuf_ServiceOptions*)google_protobuf_ServiceDescriptorProto_options(msg);
  1534. if (sub == NULL) {
  1535. sub = (struct google_protobuf_ServiceOptions*)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
  1536. if (!sub) return NULL;
  1537. google_protobuf_ServiceDescriptorProto_set_options(msg, sub);
  1538. }
  1539. return sub;
  1540. }
  1541. /* google.protobuf.MethodDescriptorProto */
  1542. UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) {
  1543. return (google_protobuf_MethodDescriptorProto *)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
  1544. }
  1545. UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parse(const char *buf, size_t size,
  1546. upb_arena *arena) {
  1547. google_protobuf_MethodDescriptorProto *ret = google_protobuf_MethodDescriptorProto_new(arena);
  1548. return (ret && upb_decode(buf, size, ret, &google_protobuf_MethodDescriptorProto_msginit)) ? ret : NULL;
  1549. }
  1550. UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1551. return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len);
  1552. }
  1553. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  1554. UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1555. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); }
  1556. UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
  1557. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); }
  1558. UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); }
  1559. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 6); }
  1560. UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MethodOptions*, UPB_SIZE(28, 56)); }
  1561. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1562. UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1563. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1564. UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  1565. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
  1566. _upb_sethas(msg, 3);
  1567. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1568. }
  1569. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
  1570. _upb_sethas(msg, 4);
  1571. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
  1572. }
  1573. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
  1574. _upb_sethas(msg, 5);
  1575. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value;
  1576. }
  1577. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) {
  1578. _upb_sethas(msg, 6);
  1579. UPB_FIELD_AT(msg, google_protobuf_MethodOptions*, UPB_SIZE(28, 56)) = value;
  1580. }
  1581. UPB_INLINE struct google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_mutable_options(google_protobuf_MethodDescriptorProto *msg, upb_arena *arena) {
  1582. struct google_protobuf_MethodOptions* sub = (struct google_protobuf_MethodOptions*)google_protobuf_MethodDescriptorProto_options(msg);
  1583. if (sub == NULL) {
  1584. sub = (struct google_protobuf_MethodOptions*)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
  1585. if (!sub) return NULL;
  1586. google_protobuf_MethodDescriptorProto_set_options(msg, sub);
  1587. }
  1588. return sub;
  1589. }
  1590. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
  1591. _upb_sethas(msg, 1);
  1592. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1593. }
  1594. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
  1595. _upb_sethas(msg, 2);
  1596. UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
  1597. }
  1598. /* google.protobuf.FileOptions */
  1599. UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) {
  1600. return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
  1601. }
  1602. UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parse(const char *buf, size_t size,
  1603. upb_arena *arena) {
  1604. google_protobuf_FileOptions *ret = google_protobuf_FileOptions_new(arena);
  1605. return (ret && upb_decode(buf, size, ret, &google_protobuf_FileOptions_msginit)) ? ret : NULL;
  1606. }
  1607. UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) {
  1608. return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len);
  1609. }
  1610. UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); }
  1611. UPB_INLINE upb_strview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)); }
  1612. UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); }
  1613. UPB_INLINE upb_strview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)); }
  1614. UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); }
  1615. UPB_INLINE int32_t google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1616. UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); }
  1617. UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
  1618. UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); }
  1619. UPB_INLINE upb_strview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)); }
  1620. UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); }
  1621. UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); }
  1622. UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); }
  1623. UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); }
  1624. UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); }
  1625. UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); }
  1626. UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); }
  1627. UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)); }
  1628. UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); }
  1629. UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); }
  1630. UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); }
  1631. UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)); }
  1632. UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); }
  1633. UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); }
  1634. UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); }
  1635. UPB_INLINE upb_strview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)); }
  1636. UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); }
  1637. UPB_INLINE upb_strview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)); }
  1638. UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); }
  1639. UPB_INLINE upb_strview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)); }
  1640. UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); }
  1641. UPB_INLINE upb_strview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)); }
  1642. UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 18); }
  1643. UPB_INLINE upb_strview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)); }
  1644. UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); }
  1645. UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
  1646. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(92, 160), len); }
  1647. UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_strview value) {
  1648. _upb_sethas(msg, 11);
  1649. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)) = value;
  1650. }
  1651. UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_strview value) {
  1652. _upb_sethas(msg, 12);
  1653. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)) = value;
  1654. }
  1655. UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, int32_t value) {
  1656. _upb_sethas(msg, 1);
  1657. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1658. }
  1659. UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) {
  1660. _upb_sethas(msg, 2);
  1661. UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value;
  1662. }
  1663. UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_strview value) {
  1664. _upb_sethas(msg, 13);
  1665. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)) = value;
  1666. }
  1667. UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1668. _upb_sethas(msg, 3);
  1669. UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)) = value;
  1670. }
  1671. UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1672. _upb_sethas(msg, 4);
  1673. UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)) = value;
  1674. }
  1675. UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1676. _upb_sethas(msg, 5);
  1677. UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)) = value;
  1678. }
  1679. UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) {
  1680. _upb_sethas(msg, 6);
  1681. UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)) = value;
  1682. }
  1683. UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) {
  1684. _upb_sethas(msg, 7);
  1685. UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)) = value;
  1686. }
  1687. UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) {
  1688. _upb_sethas(msg, 8);
  1689. UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)) = value;
  1690. }
  1691. UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) {
  1692. _upb_sethas(msg, 9);
  1693. UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)) = value;
  1694. }
  1695. UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
  1696. _upb_sethas(msg, 14);
  1697. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)) = value;
  1698. }
  1699. UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_strview value) {
  1700. _upb_sethas(msg, 15);
  1701. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)) = value;
  1702. }
  1703. UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
  1704. _upb_sethas(msg, 16);
  1705. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)) = value;
  1706. }
  1707. UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
  1708. _upb_sethas(msg, 17);
  1709. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)) = value;
  1710. }
  1711. UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_strview value) {
  1712. _upb_sethas(msg, 18);
  1713. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)) = value;
  1714. }
  1715. UPB_INLINE void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1716. _upb_sethas(msg, 10);
  1717. UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value;
  1718. }
  1719. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_mutable_uninterpreted_option(google_protobuf_FileOptions *msg, size_t *len) {
  1720. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(92, 160), len);
  1721. }
  1722. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_resize_uninterpreted_option(google_protobuf_FileOptions *msg, size_t len, upb_arena *arena) {
  1723. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(92, 160), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1724. }
  1725. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptions_add_uninterpreted_option(google_protobuf_FileOptions *msg, upb_arena *arena) {
  1726. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1727. bool ok = _upb_array_append_accessor(
  1728. msg, UPB_SIZE(92, 160), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1729. if (!ok) return NULL;
  1730. return sub;
  1731. }
  1732. /* google.protobuf.MessageOptions */
  1733. UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) {
  1734. return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
  1735. }
  1736. UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parse(const char *buf, size_t size,
  1737. upb_arena *arena) {
  1738. google_protobuf_MessageOptions *ret = google_protobuf_MessageOptions_new(arena);
  1739. return (ret && upb_decode(buf, size, ret, &google_protobuf_MessageOptions_msginit)) ? ret : NULL;
  1740. }
  1741. UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) {
  1742. return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len);
  1743. }
  1744. UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); }
  1745. UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1746. UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); }
  1747. UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  1748. UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); }
  1749. UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); }
  1750. UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 4); }
  1751. UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); }
  1752. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(8, 8), len); }
  1753. UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) {
  1754. _upb_sethas(msg, 1);
  1755. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1756. }
  1757. UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) {
  1758. _upb_sethas(msg, 2);
  1759. UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
  1760. }
  1761. UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) {
  1762. _upb_sethas(msg, 3);
  1763. UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)) = value;
  1764. }
  1765. UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) {
  1766. _upb_sethas(msg, 4);
  1767. UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)) = value;
  1768. }
  1769. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_mutable_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t *len) {
  1770. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(8, 8), len);
  1771. }
  1772. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_resize_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t len, upb_arena *arena) {
  1773. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(8, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1774. }
  1775. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOptions_add_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_arena *arena) {
  1776. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1777. bool ok = _upb_array_append_accessor(
  1778. msg, UPB_SIZE(8, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1779. if (!ok) return NULL;
  1780. return sub;
  1781. }
  1782. /* google.protobuf.FieldOptions */
  1783. UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) {
  1784. return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
  1785. }
  1786. UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parse(const char *buf, size_t size,
  1787. upb_arena *arena) {
  1788. google_protobuf_FieldOptions *ret = google_protobuf_FieldOptions_new(arena);
  1789. return (ret && upb_decode(buf, size, ret, &google_protobuf_FieldOptions_msginit)) ? ret : NULL;
  1790. }
  1791. UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) {
  1792. return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len);
  1793. }
  1794. UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); }
  1795. UPB_INLINE int32_t google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1796. UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); }
  1797. UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
  1798. UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); }
  1799. UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); }
  1800. UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); }
  1801. UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); }
  1802. UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); }
  1803. UPB_INLINE int32_t google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)); }
  1804. UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 6); }
  1805. UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); }
  1806. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(28, 32), len); }
  1807. UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, int32_t value) {
  1808. _upb_sethas(msg, 1);
  1809. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1810. }
  1811. UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) {
  1812. _upb_sethas(msg, 3);
  1813. UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value;
  1814. }
  1815. UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) {
  1816. _upb_sethas(msg, 4);
  1817. UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)) = value;
  1818. }
  1819. UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) {
  1820. _upb_sethas(msg, 5);
  1821. UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)) = value;
  1822. }
  1823. UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, int32_t value) {
  1824. _upb_sethas(msg, 2);
  1825. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)) = value;
  1826. }
  1827. UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) {
  1828. _upb_sethas(msg, 6);
  1829. UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)) = value;
  1830. }
  1831. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_mutable_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t *len) {
  1832. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 32), len);
  1833. }
  1834. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_resize_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t len, upb_arena *arena) {
  1835. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1836. }
  1837. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOptions_add_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_arena *arena) {
  1838. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1839. bool ok = _upb_array_append_accessor(
  1840. msg, UPB_SIZE(28, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1841. if (!ok) return NULL;
  1842. return sub;
  1843. }
  1844. /* google.protobuf.OneofOptions */
  1845. UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) {
  1846. return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
  1847. }
  1848. UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parse(const char *buf, size_t size,
  1849. upb_arena *arena) {
  1850. google_protobuf_OneofOptions *ret = google_protobuf_OneofOptions_new(arena);
  1851. return (ret && upb_decode(buf, size, ret, &google_protobuf_OneofOptions_msginit)) ? ret : NULL;
  1852. }
  1853. UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) {
  1854. return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len);
  1855. }
  1856. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  1857. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_mutable_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t *len) {
  1858. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  1859. }
  1860. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_resize_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t len, upb_arena *arena) {
  1861. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1862. }
  1863. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOptions_add_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_arena *arena) {
  1864. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1865. bool ok = _upb_array_append_accessor(
  1866. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1867. if (!ok) return NULL;
  1868. return sub;
  1869. }
  1870. /* google.protobuf.EnumOptions */
  1871. UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) {
  1872. return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
  1873. }
  1874. UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parse(const char *buf, size_t size,
  1875. upb_arena *arena) {
  1876. google_protobuf_EnumOptions *ret = google_protobuf_EnumOptions_new(arena);
  1877. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumOptions_msginit)) ? ret : NULL;
  1878. }
  1879. UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) {
  1880. return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len);
  1881. }
  1882. UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); }
  1883. UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1884. UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 2); }
  1885. UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  1886. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
  1887. UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) {
  1888. _upb_sethas(msg, 1);
  1889. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1890. }
  1891. UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) {
  1892. _upb_sethas(msg, 2);
  1893. UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
  1894. }
  1895. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_mutable_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t *len) {
  1896. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
  1897. }
  1898. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_resize_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t len, upb_arena *arena) {
  1899. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1900. }
  1901. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptions_add_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_arena *arena) {
  1902. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1903. bool ok = _upb_array_append_accessor(
  1904. msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1905. if (!ok) return NULL;
  1906. return sub;
  1907. }
  1908. /* google.protobuf.EnumValueOptions */
  1909. UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) {
  1910. return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
  1911. }
  1912. UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parse(const char *buf, size_t size,
  1913. upb_arena *arena) {
  1914. google_protobuf_EnumValueOptions *ret = google_protobuf_EnumValueOptions_new(arena);
  1915. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumValueOptions_msginit)) ? ret : NULL;
  1916. }
  1917. UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) {
  1918. return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len);
  1919. }
  1920. UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 1); }
  1921. UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1922. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
  1923. UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) {
  1924. _upb_sethas(msg, 1);
  1925. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1926. }
  1927. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_mutable_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t *len) {
  1928. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
  1929. }
  1930. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_resize_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t len, upb_arena *arena) {
  1931. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1932. }
  1933. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValueOptions_add_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_arena *arena) {
  1934. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1935. bool ok = _upb_array_append_accessor(
  1936. msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1937. if (!ok) return NULL;
  1938. return sub;
  1939. }
  1940. /* google.protobuf.ServiceOptions */
  1941. UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) {
  1942. return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
  1943. }
  1944. UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parse(const char *buf, size_t size,
  1945. upb_arena *arena) {
  1946. google_protobuf_ServiceOptions *ret = google_protobuf_ServiceOptions_new(arena);
  1947. return (ret && upb_decode(buf, size, ret, &google_protobuf_ServiceOptions_msginit)) ? ret : NULL;
  1948. }
  1949. UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) {
  1950. return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len);
  1951. }
  1952. UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 1); }
  1953. UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1954. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
  1955. UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) {
  1956. _upb_sethas(msg, 1);
  1957. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1958. }
  1959. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_mutable_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t *len) {
  1960. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
  1961. }
  1962. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_resize_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t len, upb_arena *arena) {
  1963. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1964. }
  1965. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOptions_add_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_arena *arena) {
  1966. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1967. bool ok = _upb_array_append_accessor(
  1968. msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1969. if (!ok) return NULL;
  1970. return sub;
  1971. }
  1972. /* google.protobuf.MethodOptions */
  1973. UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) {
  1974. return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
  1975. }
  1976. UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parse(const char *buf, size_t size,
  1977. upb_arena *arena) {
  1978. google_protobuf_MethodOptions *ret = google_protobuf_MethodOptions_new(arena);
  1979. return (ret && upb_decode(buf, size, ret, &google_protobuf_MethodOptions_msginit)) ? ret : NULL;
  1980. }
  1981. UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) {
  1982. return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len);
  1983. }
  1984. UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 2); }
  1985. UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
  1986. UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); }
  1987. UPB_INLINE int32_t google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1988. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(20, 24), len); }
  1989. UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) {
  1990. _upb_sethas(msg, 2);
  1991. UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value;
  1992. }
  1993. UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, int32_t value) {
  1994. _upb_sethas(msg, 1);
  1995. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1996. }
  1997. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_mutable_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t *len) {
  1998. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 24), len);
  1999. }
  2000. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_resize_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t len, upb_arena *arena) {
  2001. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 24), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2002. }
  2003. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOptions_add_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_arena *arena) {
  2004. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  2005. bool ok = _upb_array_append_accessor(
  2006. msg, UPB_SIZE(20, 24), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2007. if (!ok) return NULL;
  2008. return sub;
  2009. }
  2010. /* google.protobuf.UninterpretedOption */
  2011. UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) {
  2012. return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  2013. }
  2014. UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parse(const char *buf, size_t size,
  2015. upb_arena *arena) {
  2016. google_protobuf_UninterpretedOption *ret = google_protobuf_UninterpretedOption_new(arena);
  2017. return (ret && upb_decode(buf, size, ret, &google_protobuf_UninterpretedOption_msginit)) ? ret : NULL;
  2018. }
  2019. UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) {
  2020. return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len);
  2021. }
  2022. UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); }
  2023. UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); }
  2024. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); }
  2025. UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); }
  2026. UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)); }
  2027. UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); }
  2028. UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)); }
  2029. UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); }
  2030. UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); }
  2031. UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); }
  2032. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); }
  2033. UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 6); }
  2034. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); }
  2035. UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_mutable_name(google_protobuf_UninterpretedOption *msg, size_t *len) {
  2036. return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 80), len);
  2037. }
  2038. UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_resize_name(google_protobuf_UninterpretedOption *msg, size_t len, upb_arena *arena) {
  2039. return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_resize_accessor(msg, UPB_SIZE(56, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2040. }
  2041. UPB_INLINE struct google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_add_name(google_protobuf_UninterpretedOption *msg, upb_arena *arena) {
  2042. struct google_protobuf_UninterpretedOption_NamePart* sub = (struct google_protobuf_UninterpretedOption_NamePart*)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
  2043. bool ok = _upb_array_append_accessor(
  2044. msg, UPB_SIZE(56, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2045. if (!ok) return NULL;
  2046. return sub;
  2047. }
  2048. UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
  2049. _upb_sethas(msg, 4);
  2050. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value;
  2051. }
  2052. UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) {
  2053. _upb_sethas(msg, 1);
  2054. UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)) = value;
  2055. }
  2056. UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) {
  2057. _upb_sethas(msg, 2);
  2058. UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)) = value;
  2059. }
  2060. UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) {
  2061. _upb_sethas(msg, 3);
  2062. UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)) = value;
  2063. }
  2064. UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
  2065. _upb_sethas(msg, 5);
  2066. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value;
  2067. }
  2068. UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
  2069. _upb_sethas(msg, 6);
  2070. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value;
  2071. }
  2072. /* google.protobuf.UninterpretedOption.NamePart */
  2073. UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) {
  2074. return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
  2075. }
  2076. UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parse(const char *buf, size_t size,
  2077. upb_arena *arena) {
  2078. google_protobuf_UninterpretedOption_NamePart *ret = google_protobuf_UninterpretedOption_NamePart_new(arena);
  2079. return (ret && upb_decode(buf, size, ret, &google_protobuf_UninterpretedOption_NamePart_msginit)) ? ret : NULL;
  2080. }
  2081. UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) {
  2082. return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len);
  2083. }
  2084. UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 2); }
  2085. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  2086. UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); }
  2087. UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  2088. UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_strview value) {
  2089. _upb_sethas(msg, 2);
  2090. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  2091. }
  2092. UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) {
  2093. _upb_sethas(msg, 1);
  2094. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  2095. }
  2096. /* google.protobuf.SourceCodeInfo */
  2097. UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) {
  2098. return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
  2099. }
  2100. UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parse(const char *buf, size_t size,
  2101. upb_arena *arena) {
  2102. google_protobuf_SourceCodeInfo *ret = google_protobuf_SourceCodeInfo_new(arena);
  2103. return (ret && upb_decode(buf, size, ret, &google_protobuf_SourceCodeInfo_msginit)) ? ret : NULL;
  2104. }
  2105. UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) {
  2106. return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len);
  2107. }
  2108. UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg, size_t *len) { return (const google_protobuf_SourceCodeInfo_Location* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  2109. UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_mutable_location(google_protobuf_SourceCodeInfo *msg, size_t *len) {
  2110. return (google_protobuf_SourceCodeInfo_Location**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  2111. }
  2112. UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_resize_location(google_protobuf_SourceCodeInfo *msg, size_t len, upb_arena *arena) {
  2113. return (google_protobuf_SourceCodeInfo_Location**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2114. }
  2115. UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_add_location(google_protobuf_SourceCodeInfo *msg, upb_arena *arena) {
  2116. struct google_protobuf_SourceCodeInfo_Location* sub = (struct google_protobuf_SourceCodeInfo_Location*)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
  2117. bool ok = _upb_array_append_accessor(
  2118. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2119. if (!ok) return NULL;
  2120. return sub;
  2121. }
  2122. /* google.protobuf.SourceCodeInfo.Location */
  2123. UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) {
  2124. return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
  2125. }
  2126. UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parse(const char *buf, size_t size,
  2127. upb_arena *arena) {
  2128. google_protobuf_SourceCodeInfo_Location *ret = google_protobuf_SourceCodeInfo_Location_new(arena);
  2129. return (ret && upb_decode(buf, size, ret, &google_protobuf_SourceCodeInfo_Location_msginit)) ? ret : NULL;
  2130. }
  2131. UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) {
  2132. return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len);
  2133. }
  2134. UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
  2135. UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
  2136. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); }
  2137. UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  2138. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 2); }
  2139. UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
  2140. UPB_INLINE upb_strview const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); }
  2141. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_path(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
  2142. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
  2143. }
  2144. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_path(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
  2145. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  2146. }
  2147. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_path(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) {
  2148. return _upb_array_append_accessor(
  2149. msg, UPB_SIZE(20, 40), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  2150. }
  2151. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_span(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
  2152. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
  2153. }
  2154. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_span(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
  2155. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  2156. }
  2157. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_span(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) {
  2158. return _upb_array_append_accessor(
  2159. msg, UPB_SIZE(24, 48), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  2160. }
  2161. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) {
  2162. _upb_sethas(msg, 1);
  2163. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  2164. }
  2165. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) {
  2166. _upb_sethas(msg, 2);
  2167. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
  2168. }
  2169. UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_mutable_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
  2170. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len);
  2171. }
  2172. UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_resize_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
  2173. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  2174. }
  2175. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview val, upb_arena *arena) {
  2176. return _upb_array_append_accessor(
  2177. msg, UPB_SIZE(28, 56), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  2178. }
  2179. /* google.protobuf.GeneratedCodeInfo */
  2180. UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) {
  2181. return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena);
  2182. }
  2183. UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parse(const char *buf, size_t size,
  2184. upb_arena *arena) {
  2185. google_protobuf_GeneratedCodeInfo *ret = google_protobuf_GeneratedCodeInfo_new(arena);
  2186. return (ret && upb_decode(buf, size, ret, &google_protobuf_GeneratedCodeInfo_msginit)) ? ret : NULL;
  2187. }
  2188. UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) {
  2189. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len);
  2190. }
  2191. UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg, size_t *len) { return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  2192. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_mutable_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t *len) {
  2193. return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  2194. }
  2195. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_resize_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t len, upb_arena *arena) {
  2196. return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2197. }
  2198. UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_add_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena) {
  2199. struct google_protobuf_GeneratedCodeInfo_Annotation* sub = (struct google_protobuf_GeneratedCodeInfo_Annotation*)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
  2200. bool ok = _upb_array_append_accessor(
  2201. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2202. if (!ok) return NULL;
  2203. return sub;
  2204. }
  2205. /* google.protobuf.GeneratedCodeInfo.Annotation */
  2206. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) {
  2207. return (google_protobuf_GeneratedCodeInfo_Annotation *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
  2208. }
  2209. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parse(const char *buf, size_t size,
  2210. upb_arena *arena) {
  2211. google_protobuf_GeneratedCodeInfo_Annotation *ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena);
  2212. return (ret && upb_decode(buf, size, ret, &google_protobuf_GeneratedCodeInfo_Annotation_msginit)) ? ret : NULL;
  2213. }
  2214. UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) {
  2215. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len);
  2216. }
  2217. UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); }
  2218. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 3); }
  2219. UPB_INLINE upb_strview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)); }
  2220. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); }
  2221. UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  2222. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); }
  2223. UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  2224. UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_mutable_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) {
  2225. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 32), len);
  2226. }
  2227. UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_resize_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t len, upb_arena *arena) {
  2228. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 32), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  2229. }
  2230. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_add_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t val, upb_arena *arena) {
  2231. return _upb_array_append_accessor(
  2232. msg, UPB_SIZE(20, 32), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  2233. }
  2234. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_strview value) {
  2235. _upb_sethas(msg, 3);
  2236. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)) = value;
  2237. }
  2238. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
  2239. _upb_sethas(msg, 1);
  2240. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  2241. }
  2242. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
  2243. _upb_sethas(msg, 2);
  2244. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  2245. }
  2246. #ifdef __cplusplus
  2247. } /* extern "C" */
  2248. #endif
  2249. #endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */
  2250. /*
  2251. ** Defs are upb's internal representation of the constructs that can appear
  2252. ** in a .proto file:
  2253. **
  2254. ** - upb::MessageDefPtr (upb_msgdef): describes a "message" construct.
  2255. ** - upb::FieldDefPtr (upb_fielddef): describes a message field.
  2256. ** - upb::FileDefPtr (upb_filedef): describes a .proto file and its defs.
  2257. ** - upb::EnumDefPtr (upb_enumdef): describes an enum.
  2258. ** - upb::OneofDefPtr (upb_oneofdef): describes a oneof.
  2259. **
  2260. ** TODO: definitions of services.
  2261. **
  2262. ** This is a mixed C/C++ interface that offers a full API to both languages.
  2263. ** See the top-level README for more information.
  2264. */
  2265. #ifndef UPB_DEF_H_
  2266. #define UPB_DEF_H_
  2267. /*
  2268. ** upb_table
  2269. **
  2270. ** This header is INTERNAL-ONLY! Its interfaces are not public or stable!
  2271. ** This file defines very fast int->upb_value (inttable) and string->upb_value
  2272. ** (strtable) hash tables.
  2273. **
  2274. ** The table uses chained scatter with Brent's variation (inspired by the Lua
  2275. ** implementation of hash tables). The hash function for strings is Austin
  2276. ** Appleby's "MurmurHash."
  2277. **
  2278. ** The inttable uses uintptr_t as its key, which guarantees it can be used to
  2279. ** store pointers or integers of at least 32 bits (upb isn't really useful on
  2280. ** systems where sizeof(void*) < 4).
  2281. **
  2282. ** The table must be homogenous (all values of the same type). In debug
  2283. ** mode, we check this on insert and lookup.
  2284. */
  2285. #ifndef UPB_TABLE_H_
  2286. #define UPB_TABLE_H_
  2287. #include <stdint.h>
  2288. #include <string.h>
  2289. #ifdef __cplusplus
  2290. extern "C" {
  2291. #endif
  2292. /* upb_value ******************************************************************/
  2293. /* A tagged union (stored untagged inside the table) so that we can check that
  2294. * clients calling table accessors are correctly typed without having to have
  2295. * an explosion of accessors. */
  2296. typedef enum {
  2297. UPB_CTYPE_INT32 = 1,
  2298. UPB_CTYPE_INT64 = 2,
  2299. UPB_CTYPE_UINT32 = 3,
  2300. UPB_CTYPE_UINT64 = 4,
  2301. UPB_CTYPE_BOOL = 5,
  2302. UPB_CTYPE_CSTR = 6,
  2303. UPB_CTYPE_PTR = 7,
  2304. UPB_CTYPE_CONSTPTR = 8,
  2305. UPB_CTYPE_FPTR = 9,
  2306. UPB_CTYPE_FLOAT = 10,
  2307. UPB_CTYPE_DOUBLE = 11
  2308. } upb_ctype_t;
  2309. typedef struct {
  2310. uint64_t val;
  2311. #ifndef NDEBUG
  2312. /* In debug mode we carry the value type around also so we can check accesses
  2313. * to be sure the right member is being read. */
  2314. upb_ctype_t ctype;
  2315. #endif
  2316. } upb_value;
  2317. #ifdef NDEBUG
  2318. #define SET_TYPE(dest, val) UPB_UNUSED(val)
  2319. #else
  2320. #define SET_TYPE(dest, val) dest = val
  2321. #endif
  2322. /* Like strdup(), which isn't always available since it's not ANSI C. */
  2323. char *upb_strdup(const char *s, upb_alloc *a);
  2324. /* Variant that works with a length-delimited rather than NULL-delimited string,
  2325. * as supported by strtable. */
  2326. char *upb_strdup2(const char *s, size_t len, upb_alloc *a);
  2327. UPB_INLINE char *upb_gstrdup(const char *s) {
  2328. return upb_strdup(s, &upb_alloc_global);
  2329. }
  2330. UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
  2331. upb_ctype_t ctype) {
  2332. v->val = val;
  2333. SET_TYPE(v->ctype, ctype);
  2334. }
  2335. UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
  2336. upb_value ret;
  2337. _upb_value_setval(&ret, val, ctype);
  2338. return ret;
  2339. }
  2340. /* For each value ctype, define the following set of functions:
  2341. *
  2342. * // Get/set an int32 from a upb_value.
  2343. * int32_t upb_value_getint32(upb_value val);
  2344. * void upb_value_setint32(upb_value *val, int32_t cval);
  2345. *
  2346. * // Construct a new upb_value from an int32.
  2347. * upb_value upb_value_int32(int32_t val); */
  2348. #define FUNCS(name, membername, type_t, converter, proto_type) \
  2349. UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
  2350. val->val = (converter)cval; \
  2351. SET_TYPE(val->ctype, proto_type); \
  2352. } \
  2353. UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
  2354. upb_value ret; \
  2355. upb_value_set ## name(&ret, val); \
  2356. return ret; \
  2357. } \
  2358. UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
  2359. UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \
  2360. return (type_t)(converter)val.val; \
  2361. }
  2362. FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
  2363. FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
  2364. FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
  2365. FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
  2366. FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
  2367. FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
  2368. FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
  2369. FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
  2370. FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
  2371. #undef FUNCS
  2372. UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
  2373. memcpy(&val->val, &cval, sizeof(cval));
  2374. SET_TYPE(val->ctype, UPB_CTYPE_FLOAT);
  2375. }
  2376. UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
  2377. memcpy(&val->val, &cval, sizeof(cval));
  2378. SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE);
  2379. }
  2380. UPB_INLINE upb_value upb_value_float(float cval) {
  2381. upb_value ret;
  2382. upb_value_setfloat(&ret, cval);
  2383. return ret;
  2384. }
  2385. UPB_INLINE upb_value upb_value_double(double cval) {
  2386. upb_value ret;
  2387. upb_value_setdouble(&ret, cval);
  2388. return ret;
  2389. }
  2390. #undef SET_TYPE
  2391. /* upb_tabkey *****************************************************************/
  2392. /* Either:
  2393. * 1. an actual integer key, or
  2394. * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
  2395. *
  2396. * ...depending on whether this is a string table or an int table. We would
  2397. * make this a union of those two types, but C89 doesn't support statically
  2398. * initializing a non-first union member. */
  2399. typedef uintptr_t upb_tabkey;
  2400. UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
  2401. char* mem = (char*)key;
  2402. if (len) memcpy(len, mem, sizeof(*len));
  2403. return mem + sizeof(*len);
  2404. }
  2405. /* upb_tabval *****************************************************************/
  2406. typedef struct {
  2407. uint64_t val;
  2408. } upb_tabval;
  2409. #define UPB_TABVALUE_EMPTY_INIT {-1}
  2410. /* upb_table ******************************************************************/
  2411. typedef struct _upb_tabent {
  2412. upb_tabkey key;
  2413. upb_tabval val;
  2414. /* Internal chaining. This is const so we can create static initializers for
  2415. * tables. We cast away const sometimes, but *only* when the containing
  2416. * upb_table is known to be non-const. This requires a bit of care, but
  2417. * the subtlety is confined to table.c. */
  2418. const struct _upb_tabent *next;
  2419. } upb_tabent;
  2420. typedef struct {
  2421. size_t count; /* Number of entries in the hash part. */
  2422. size_t mask; /* Mask to turn hash value -> bucket. */
  2423. upb_ctype_t ctype; /* Type of all values. */
  2424. uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
  2425. /* Hash table entries.
  2426. * Making this const isn't entirely accurate; what we really want is for it to
  2427. * have the same const-ness as the table it's inside. But there's no way to
  2428. * declare that in C. So we have to make it const so that we can statically
  2429. * initialize const hash tables. Then we cast away const when we have to.
  2430. */
  2431. const upb_tabent *entries;
  2432. #ifndef NDEBUG
  2433. /* This table's allocator. We make the user pass it in to every relevant
  2434. * function and only use this to check it in debug mode. We do this solely
  2435. * to keep upb_table as small as possible. This might seem slightly paranoid
  2436. * but the plan is to use upb_table for all map fields and extension sets in
  2437. * a forthcoming message representation, so there could be a lot of these.
  2438. * If this turns out to be too annoying later, we can change it (since this
  2439. * is an internal-only header file). */
  2440. upb_alloc *alloc;
  2441. #endif
  2442. } upb_table;
  2443. typedef struct {
  2444. upb_table t;
  2445. } upb_strtable;
  2446. typedef struct {
  2447. upb_table t; /* For entries that don't fit in the array part. */
  2448. const upb_tabval *array; /* Array part of the table. See const note above. */
  2449. size_t array_size; /* Array part size. */
  2450. size_t array_count; /* Array part number of elements. */
  2451. } upb_inttable;
  2452. #define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
  2453. {UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount}
  2454. #define UPB_EMPTY_INTTABLE_INIT(ctype) \
  2455. UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
  2456. #define UPB_ARRAY_EMPTYENT -1
  2457. UPB_INLINE size_t upb_table_size(const upb_table *t) {
  2458. if (t->size_lg2 == 0)
  2459. return 0;
  2460. else
  2461. return 1 << t->size_lg2;
  2462. }
  2463. /* Internal-only functions, in .h file only out of necessity. */
  2464. UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
  2465. return e->key == 0;
  2466. }
  2467. /* Used by some of the unit tests for generic hashing functionality. */
  2468. uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
  2469. UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
  2470. return key;
  2471. }
  2472. UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
  2473. return (uint32_t)key;
  2474. }
  2475. static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
  2476. return t->entries + (hash & t->mask);
  2477. }
  2478. UPB_INLINE bool upb_arrhas(upb_tabval key) {
  2479. return key.val != (uint64_t)-1;
  2480. }
  2481. /* Initialize and uninitialize a table, respectively. If memory allocation
  2482. * failed, false is returned that the table is uninitialized. */
  2483. bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a);
  2484. bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a);
  2485. void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a);
  2486. void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a);
  2487. UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) {
  2488. return upb_inttable_init2(table, ctype, &upb_alloc_global);
  2489. }
  2490. UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) {
  2491. return upb_strtable_init2(table, ctype, &upb_alloc_global);
  2492. }
  2493. UPB_INLINE void upb_inttable_uninit(upb_inttable *table) {
  2494. upb_inttable_uninit2(table, &upb_alloc_global);
  2495. }
  2496. UPB_INLINE void upb_strtable_uninit(upb_strtable *table) {
  2497. upb_strtable_uninit2(table, &upb_alloc_global);
  2498. }
  2499. /* Returns the number of values in the table. */
  2500. size_t upb_inttable_count(const upb_inttable *t);
  2501. UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
  2502. return t->t.count;
  2503. }
  2504. void upb_inttable_packedsize(const upb_inttable *t, size_t *size);
  2505. void upb_strtable_packedsize(const upb_strtable *t, size_t *size);
  2506. upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs,
  2507. size_t size);
  2508. upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs,
  2509. size_t size);
  2510. /* Inserts the given key into the hashtable with the given value. The key must
  2511. * not already exist in the hash table. For string tables, the key must be
  2512. * NULL-terminated, and the table will make an internal copy of the key.
  2513. * Inttables must not insert a value of UINTPTR_MAX.
  2514. *
  2515. * If a table resize was required but memory allocation failed, false is
  2516. * returned and the table is unchanged. */
  2517. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  2518. upb_alloc *a);
  2519. bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len,
  2520. upb_value val, upb_alloc *a);
  2521. UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key,
  2522. upb_value val) {
  2523. return upb_inttable_insert2(t, key, val, &upb_alloc_global);
  2524. }
  2525. UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key,
  2526. size_t len, upb_value val) {
  2527. return upb_strtable_insert3(t, key, len, val, &upb_alloc_global);
  2528. }
  2529. /* For NULL-terminated strings. */
  2530. UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
  2531. upb_value val) {
  2532. return upb_strtable_insert2(t, key, strlen(key), val);
  2533. }
  2534. /* Looks up key in this table, returning "true" if the key was found.
  2535. * If v is non-NULL, copies the value for this key into *v. */
  2536. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
  2537. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  2538. upb_value *v);
  2539. /* For NULL-terminated strings. */
  2540. UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
  2541. upb_value *v) {
  2542. return upb_strtable_lookup2(t, key, strlen(key), v);
  2543. }
  2544. /* Removes an item from the table. Returns true if the remove was successful,
  2545. * and stores the removed item in *val if non-NULL. */
  2546. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
  2547. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  2548. upb_value *val, upb_alloc *alloc);
  2549. UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key,
  2550. size_t len, upb_value *val) {
  2551. return upb_strtable_remove3(t, key, len, val, &upb_alloc_global);
  2552. }
  2553. /* For NULL-terminated strings. */
  2554. UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
  2555. upb_value *v) {
  2556. return upb_strtable_remove2(t, key, strlen(key), v);
  2557. }
  2558. /* Updates an existing entry in an inttable. If the entry does not exist,
  2559. * returns false and does nothing. Unlike insert/remove, this does not
  2560. * invalidate iterators. */
  2561. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
  2562. /* Handy routines for treating an inttable like a stack. May not be mixed with
  2563. * other insert/remove calls. */
  2564. bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a);
  2565. upb_value upb_inttable_pop(upb_inttable *t);
  2566. UPB_INLINE bool upb_inttable_push(upb_inttable *t, upb_value val) {
  2567. return upb_inttable_push2(t, val, &upb_alloc_global);
  2568. }
  2569. /* Convenience routines for inttables with pointer keys. */
  2570. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  2571. upb_alloc *a);
  2572. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
  2573. bool upb_inttable_lookupptr(
  2574. const upb_inttable *t, const void *key, upb_value *val);
  2575. UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key,
  2576. upb_value val) {
  2577. return upb_inttable_insertptr2(t, key, val, &upb_alloc_global);
  2578. }
  2579. /* Optimizes the table for the current set of entries, for both memory use and
  2580. * lookup time. Client should call this after all entries have been inserted;
  2581. * inserting more entries is legal, but will likely require a table resize. */
  2582. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a);
  2583. UPB_INLINE void upb_inttable_compact(upb_inttable *t) {
  2584. upb_inttable_compact2(t, &upb_alloc_global);
  2585. }
  2586. /* A special-case inlinable version of the lookup routine for 32-bit
  2587. * integers. */
  2588. UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
  2589. upb_value *v) {
  2590. *v = upb_value_int32(0); /* Silence compiler warnings. */
  2591. if (key < t->array_size) {
  2592. upb_tabval arrval = t->array[key];
  2593. if (upb_arrhas(arrval)) {
  2594. _upb_value_setval(v, arrval.val, t->t.ctype);
  2595. return true;
  2596. } else {
  2597. return false;
  2598. }
  2599. } else {
  2600. const upb_tabent *e;
  2601. if (t->t.entries == NULL) return false;
  2602. for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
  2603. if ((uint32_t)e->key == key) {
  2604. _upb_value_setval(v, e->val.val, t->t.ctype);
  2605. return true;
  2606. }
  2607. if (e->next == NULL) return false;
  2608. }
  2609. }
  2610. }
  2611. /* Exposed for testing only. */
  2612. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a);
  2613. /* Iterators ******************************************************************/
  2614. /* Iterators for int and string tables. We are subject to some kind of unusual
  2615. * design constraints:
  2616. *
  2617. * For high-level languages:
  2618. * - we must be able to guarantee that we don't crash or corrupt memory even if
  2619. * the program accesses an invalidated iterator.
  2620. *
  2621. * For C++11 range-based for:
  2622. * - iterators must be copyable
  2623. * - iterators must be comparable
  2624. * - it must be possible to construct an "end" value.
  2625. *
  2626. * Iteration order is undefined.
  2627. *
  2628. * Modifying the table invalidates iterators. upb_{str,int}table_done() is
  2629. * guaranteed to work even on an invalidated iterator, as long as the table it
  2630. * is iterating over has not been freed. Calling next() or accessing data from
  2631. * an invalidated iterator yields unspecified elements from the table, but it is
  2632. * guaranteed not to crash and to return real table elements (except when done()
  2633. * is true). */
  2634. /* upb_strtable_iter **********************************************************/
  2635. /* upb_strtable_iter i;
  2636. * upb_strtable_begin(&i, t);
  2637. * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  2638. * const char *key = upb_strtable_iter_key(&i);
  2639. * const upb_value val = upb_strtable_iter_value(&i);
  2640. * // ...
  2641. * }
  2642. */
  2643. typedef struct {
  2644. const upb_strtable *t;
  2645. size_t index;
  2646. } upb_strtable_iter;
  2647. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
  2648. void upb_strtable_next(upb_strtable_iter *i);
  2649. bool upb_strtable_done(const upb_strtable_iter *i);
  2650. const char *upb_strtable_iter_key(const upb_strtable_iter *i);
  2651. size_t upb_strtable_iter_keylength(const upb_strtable_iter *i);
  2652. upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
  2653. void upb_strtable_iter_setdone(upb_strtable_iter *i);
  2654. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  2655. const upb_strtable_iter *i2);
  2656. /* upb_inttable_iter **********************************************************/
  2657. /* upb_inttable_iter i;
  2658. * upb_inttable_begin(&i, t);
  2659. * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  2660. * uintptr_t key = upb_inttable_iter_key(&i);
  2661. * upb_value val = upb_inttable_iter_value(&i);
  2662. * // ...
  2663. * }
  2664. */
  2665. typedef struct {
  2666. const upb_inttable *t;
  2667. size_t index;
  2668. bool array_part;
  2669. } upb_inttable_iter;
  2670. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
  2671. void upb_inttable_next(upb_inttable_iter *i);
  2672. bool upb_inttable_done(const upb_inttable_iter *i);
  2673. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
  2674. upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
  2675. void upb_inttable_iter_setdone(upb_inttable_iter *i);
  2676. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  2677. const upb_inttable_iter *i2);
  2678. #ifdef __cplusplus
  2679. } /* extern "C" */
  2680. #endif
  2681. #endif /* UPB_TABLE_H_ */
  2682. #ifdef __cplusplus
  2683. #include <cstring>
  2684. #include <memory>
  2685. #include <string>
  2686. #include <vector>
  2687. namespace upb {
  2688. class EnumDefPtr;
  2689. class FieldDefPtr;
  2690. class FileDefPtr;
  2691. class MessageDefPtr;
  2692. class OneofDefPtr;
  2693. class SymbolTable;
  2694. }
  2695. #endif
  2696. struct upb_enumdef;
  2697. typedef struct upb_enumdef upb_enumdef;
  2698. struct upb_fielddef;
  2699. typedef struct upb_fielddef upb_fielddef;
  2700. struct upb_filedef;
  2701. typedef struct upb_filedef upb_filedef;
  2702. struct upb_msgdef;
  2703. typedef struct upb_msgdef upb_msgdef;
  2704. struct upb_oneofdef;
  2705. typedef struct upb_oneofdef upb_oneofdef;
  2706. struct upb_symtab;
  2707. typedef struct upb_symtab upb_symtab;
  2708. typedef enum {
  2709. UPB_SYNTAX_PROTO2 = 2,
  2710. UPB_SYNTAX_PROTO3 = 3
  2711. } upb_syntax_t;
  2712. /* All the different kind of well known type messages. For simplicity of check,
  2713. * number wrappers and string wrappers are grouped together. Make sure the
  2714. * order and merber of these groups are not changed.
  2715. */
  2716. typedef enum {
  2717. UPB_WELLKNOWN_UNSPECIFIED,
  2718. UPB_WELLKNOWN_ANY,
  2719. UPB_WELLKNOWN_FIELDMASK,
  2720. UPB_WELLKNOWN_DURATION,
  2721. UPB_WELLKNOWN_TIMESTAMP,
  2722. /* number wrappers */
  2723. UPB_WELLKNOWN_DOUBLEVALUE,
  2724. UPB_WELLKNOWN_FLOATVALUE,
  2725. UPB_WELLKNOWN_INT64VALUE,
  2726. UPB_WELLKNOWN_UINT64VALUE,
  2727. UPB_WELLKNOWN_INT32VALUE,
  2728. UPB_WELLKNOWN_UINT32VALUE,
  2729. /* string wrappers */
  2730. UPB_WELLKNOWN_STRINGVALUE,
  2731. UPB_WELLKNOWN_BYTESVALUE,
  2732. UPB_WELLKNOWN_BOOLVALUE,
  2733. UPB_WELLKNOWN_VALUE,
  2734. UPB_WELLKNOWN_LISTVALUE,
  2735. UPB_WELLKNOWN_STRUCT
  2736. } upb_wellknowntype_t;
  2737. /* upb_fielddef ***************************************************************/
  2738. /* Maximum field number allowed for FieldDefs. This is an inherent limit of the
  2739. * protobuf wire format. */
  2740. #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
  2741. #ifdef __cplusplus
  2742. extern "C" {
  2743. #endif
  2744. const char *upb_fielddef_fullname(const upb_fielddef *f);
  2745. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
  2746. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
  2747. upb_label_t upb_fielddef_label(const upb_fielddef *f);
  2748. uint32_t upb_fielddef_number(const upb_fielddef *f);
  2749. const char *upb_fielddef_name(const upb_fielddef *f);
  2750. bool upb_fielddef_isextension(const upb_fielddef *f);
  2751. bool upb_fielddef_lazy(const upb_fielddef *f);
  2752. bool upb_fielddef_packed(const upb_fielddef *f);
  2753. size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
  2754. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
  2755. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
  2756. uint32_t upb_fielddef_index(const upb_fielddef *f);
  2757. bool upb_fielddef_issubmsg(const upb_fielddef *f);
  2758. bool upb_fielddef_isstring(const upb_fielddef *f);
  2759. bool upb_fielddef_isseq(const upb_fielddef *f);
  2760. bool upb_fielddef_isprimitive(const upb_fielddef *f);
  2761. bool upb_fielddef_ismap(const upb_fielddef *f);
  2762. int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
  2763. int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
  2764. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
  2765. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
  2766. bool upb_fielddef_defaultbool(const upb_fielddef *f);
  2767. float upb_fielddef_defaultfloat(const upb_fielddef *f);
  2768. double upb_fielddef_defaultdouble(const upb_fielddef *f);
  2769. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
  2770. bool upb_fielddef_hassubdef(const upb_fielddef *f);
  2771. bool upb_fielddef_haspresence(const upb_fielddef *f);
  2772. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
  2773. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
  2774. /* Internal only. */
  2775. uint32_t upb_fielddef_selectorbase(const upb_fielddef *f);
  2776. #ifdef __cplusplus
  2777. } /* extern "C" */
  2778. /* A upb_fielddef describes a single field in a message. It is most often
  2779. * found as a part of a upb_msgdef, but can also stand alone to represent
  2780. * an extension. */
  2781. class upb::FieldDefPtr {
  2782. public:
  2783. FieldDefPtr() : ptr_(nullptr) {}
  2784. explicit FieldDefPtr(const upb_fielddef *ptr) : ptr_(ptr) {}
  2785. const upb_fielddef* ptr() const { return ptr_; }
  2786. explicit operator bool() const { return ptr_ != nullptr; }
  2787. typedef upb_fieldtype_t Type;
  2788. typedef upb_label_t Label;
  2789. typedef upb_descriptortype_t DescriptorType;
  2790. const char* full_name() const { return upb_fielddef_fullname(ptr_); }
  2791. Type type() const { return upb_fielddef_type(ptr_); }
  2792. Label label() const { return upb_fielddef_label(ptr_); }
  2793. const char* name() const { return upb_fielddef_name(ptr_); }
  2794. uint32_t number() const { return upb_fielddef_number(ptr_); }
  2795. bool is_extension() const { return upb_fielddef_isextension(ptr_); }
  2796. /* Copies the JSON name for this field into the given buffer. Returns the
  2797. * actual size of the JSON name, including the NULL terminator. If the
  2798. * return value is 0, the JSON name is unset. If the return value is
  2799. * greater than len, the JSON name was truncated. The buffer is always
  2800. * NULL-terminated if len > 0.
  2801. *
  2802. * The JSON name always defaults to a camelCased version of the regular
  2803. * name. However if the regular name is unset, the JSON name will be unset
  2804. * also.
  2805. */
  2806. size_t GetJsonName(char *buf, size_t len) const {
  2807. return upb_fielddef_getjsonname(ptr_, buf, len);
  2808. }
  2809. /* Convenience version of the above function which copies the JSON name
  2810. * into the given string, returning false if the name is not set. */
  2811. template <class T>
  2812. bool GetJsonName(T* str) {
  2813. str->resize(GetJsonName(NULL, 0));
  2814. GetJsonName(&(*str)[0], str->size());
  2815. return str->size() > 0;
  2816. }
  2817. /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
  2818. * indicates whether this field should have lazy parsing handlers that yield
  2819. * the unparsed string for the submessage.
  2820. *
  2821. * TODO(haberman): I think we want to move this into a FieldOptions container
  2822. * when we add support for custom options (the FieldOptions struct will
  2823. * contain both regular FieldOptions like "lazy" *and* custom options). */
  2824. bool lazy() const { return upb_fielddef_lazy(ptr_); }
  2825. /* For non-string, non-submessage fields, this indicates whether binary
  2826. * protobufs are encoded in packed or non-packed format.
  2827. *
  2828. * TODO(haberman): see note above about putting options like this into a
  2829. * FieldOptions container. */
  2830. bool packed() const { return upb_fielddef_packed(ptr_); }
  2831. /* An integer that can be used as an index into an array of fields for
  2832. * whatever message this field belongs to. Guaranteed to be less than
  2833. * f->containing_type()->field_count(). May only be accessed once the def has
  2834. * been finalized. */
  2835. uint32_t index() const { return upb_fielddef_index(ptr_); }
  2836. /* The MessageDef to which this field belongs.
  2837. *
  2838. * If this field has been added to a MessageDef, that message can be retrieved
  2839. * directly (this is always the case for frozen FieldDefs).
  2840. *
  2841. * If the field has not yet been added to a MessageDef, you can set the name
  2842. * of the containing type symbolically instead. This is mostly useful for
  2843. * extensions, where the extension is declared separately from the message. */
  2844. MessageDefPtr containing_type() const;
  2845. /* The OneofDef to which this field belongs, or NULL if this field is not part
  2846. * of a oneof. */
  2847. OneofDefPtr containing_oneof() const;
  2848. /* The field's type according to the enum in descriptor.proto. This is not
  2849. * the same as UPB_TYPE_*, because it distinguishes between (for example)
  2850. * INT32 and SINT32, whereas our "type" enum does not. This return of
  2851. * descriptor_type() is a function of type(), integer_format(), and
  2852. * is_tag_delimited(). */
  2853. DescriptorType descriptor_type() const {
  2854. return upb_fielddef_descriptortype(ptr_);
  2855. }
  2856. /* Convenient field type tests. */
  2857. bool IsSubMessage() const { return upb_fielddef_issubmsg(ptr_); }
  2858. bool IsString() const { return upb_fielddef_isstring(ptr_); }
  2859. bool IsSequence() const { return upb_fielddef_isseq(ptr_); }
  2860. bool IsPrimitive() const { return upb_fielddef_isprimitive(ptr_); }
  2861. bool IsMap() const { return upb_fielddef_ismap(ptr_); }
  2862. /* Returns the non-string default value for this fielddef, which may either
  2863. * be something the client set explicitly or the "default default" (0 for
  2864. * numbers, empty for strings). The field's type indicates the type of the
  2865. * returned value, except for enum fields that are still mutable.
  2866. *
  2867. * Requires that the given function matches the field's current type. */
  2868. int64_t default_int64() const { return upb_fielddef_defaultint64(ptr_); }
  2869. int32_t default_int32() const { return upb_fielddef_defaultint32(ptr_); }
  2870. uint64_t default_uint64() const { return upb_fielddef_defaultuint64(ptr_); }
  2871. uint32_t default_uint32() const { return upb_fielddef_defaultuint32(ptr_); }
  2872. bool default_bool() const { return upb_fielddef_defaultbool(ptr_); }
  2873. float default_float() const { return upb_fielddef_defaultfloat(ptr_); }
  2874. double default_double() const { return upb_fielddef_defaultdouble(ptr_); }
  2875. /* The resulting string is always NULL-terminated. If non-NULL, the length
  2876. * will be stored in *len. */
  2877. const char *default_string(size_t * len) const {
  2878. return upb_fielddef_defaultstr(ptr_, len);
  2879. }
  2880. /* Returns the enum or submessage def for this field, if any. The field's
  2881. * type must match (ie. you may only call enum_subdef() for fields where
  2882. * type() == UPB_TYPE_ENUM). */
  2883. EnumDefPtr enum_subdef() const;
  2884. MessageDefPtr message_subdef() const;
  2885. private:
  2886. const upb_fielddef *ptr_;
  2887. };
  2888. #endif /* __cplusplus */
  2889. /* upb_oneofdef ***************************************************************/
  2890. #ifdef __cplusplus
  2891. extern "C" {
  2892. #endif
  2893. typedef upb_inttable_iter upb_oneof_iter;
  2894. const char *upb_oneofdef_name(const upb_oneofdef *o);
  2895. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
  2896. int upb_oneofdef_numfields(const upb_oneofdef *o);
  2897. uint32_t upb_oneofdef_index(const upb_oneofdef *o);
  2898. /* Oneof lookups:
  2899. * - ntof: look up a field by name.
  2900. * - ntofz: look up a field by name (as a null-terminated string).
  2901. * - itof: look up a field by number. */
  2902. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  2903. const char *name, size_t length);
  2904. UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
  2905. const char *name) {
  2906. return upb_oneofdef_ntof(o, name, strlen(name));
  2907. }
  2908. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
  2909. /* upb_oneof_iter i;
  2910. * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
  2911. * // ...
  2912. * }
  2913. */
  2914. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
  2915. void upb_oneof_next(upb_oneof_iter *iter);
  2916. bool upb_oneof_done(upb_oneof_iter *iter);
  2917. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
  2918. void upb_oneof_iter_setdone(upb_oneof_iter *iter);
  2919. bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1,
  2920. const upb_oneof_iter *iter2);
  2921. #ifdef __cplusplus
  2922. } /* extern "C" */
  2923. /* Class that represents a oneof. */
  2924. class upb::OneofDefPtr {
  2925. public:
  2926. OneofDefPtr() : ptr_(nullptr) {}
  2927. explicit OneofDefPtr(const upb_oneofdef *ptr) : ptr_(ptr) {}
  2928. const upb_oneofdef* ptr() const { return ptr_; }
  2929. explicit operator bool() { return ptr_ != nullptr; }
  2930. /* Returns the MessageDef that owns this OneofDef. */
  2931. MessageDefPtr containing_type() const;
  2932. /* Returns the name of this oneof. This is the name used to look up the oneof
  2933. * by name once added to a message def. */
  2934. const char* name() const { return upb_oneofdef_name(ptr_); }
  2935. /* Returns the number of fields currently defined in the oneof. */
  2936. int field_count() const { return upb_oneofdef_numfields(ptr_); }
  2937. /* Looks up by name. */
  2938. FieldDefPtr FindFieldByName(const char *name, size_t len) const {
  2939. return FieldDefPtr(upb_oneofdef_ntof(ptr_, name, len));
  2940. }
  2941. FieldDefPtr FindFieldByName(const char* name) const {
  2942. return FieldDefPtr(upb_oneofdef_ntofz(ptr_, name));
  2943. }
  2944. template <class T>
  2945. FieldDefPtr FindFieldByName(const T& str) const {
  2946. return FindFieldByName(str.c_str(), str.size());
  2947. }
  2948. /* Looks up by tag number. */
  2949. FieldDefPtr FindFieldByNumber(uint32_t num) const {
  2950. return FieldDefPtr(upb_oneofdef_itof(ptr_, num));
  2951. }
  2952. class const_iterator
  2953. : public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
  2954. public:
  2955. void operator++() { upb_oneof_next(&iter_); }
  2956. FieldDefPtr operator*() const {
  2957. return FieldDefPtr(upb_oneof_iter_field(&iter_));
  2958. }
  2959. bool operator!=(const const_iterator& other) const {
  2960. return !upb_oneof_iter_isequal(&iter_, &other.iter_);
  2961. }
  2962. bool operator==(const const_iterator& other) const {
  2963. return upb_oneof_iter_isequal(&iter_, &other.iter_);
  2964. }
  2965. private:
  2966. friend class OneofDefPtr;
  2967. const_iterator() {}
  2968. explicit const_iterator(OneofDefPtr o) {
  2969. upb_oneof_begin(&iter_, o.ptr());
  2970. }
  2971. static const_iterator end() {
  2972. const_iterator iter;
  2973. upb_oneof_iter_setdone(&iter.iter_);
  2974. return iter;
  2975. }
  2976. upb_oneof_iter iter_;
  2977. };
  2978. const_iterator begin() const { return const_iterator(*this); }
  2979. const_iterator end() const { return const_iterator::end(); }
  2980. private:
  2981. const upb_oneofdef *ptr_;
  2982. };
  2983. inline upb::OneofDefPtr upb::FieldDefPtr::containing_oneof() const {
  2984. return OneofDefPtr(upb_fielddef_containingoneof(ptr_));
  2985. }
  2986. #endif /* __cplusplus */
  2987. /* upb_msgdef *****************************************************************/
  2988. typedef upb_inttable_iter upb_msg_field_iter;
  2989. typedef upb_strtable_iter upb_msg_oneof_iter;
  2990. /* Well-known field tag numbers for map-entry messages. */
  2991. #define UPB_MAPENTRY_KEY 1
  2992. #define UPB_MAPENTRY_VALUE 2
  2993. /* Well-known field tag numbers for Any messages. */
  2994. #define UPB_ANY_TYPE 1
  2995. #define UPB_ANY_VALUE 2
  2996. /* Well-known field tag numbers for timestamp messages. */
  2997. #define UPB_DURATION_SECONDS 1
  2998. #define UPB_DURATION_NANOS 2
  2999. /* Well-known field tag numbers for duration messages. */
  3000. #define UPB_TIMESTAMP_SECONDS 1
  3001. #define UPB_TIMESTAMP_NANOS 2
  3002. #ifdef __cplusplus
  3003. extern "C" {
  3004. #endif
  3005. const char *upb_msgdef_fullname(const upb_msgdef *m);
  3006. const upb_filedef *upb_msgdef_file(const upb_msgdef *m);
  3007. const char *upb_msgdef_name(const upb_msgdef *m);
  3008. int upb_msgdef_numoneofs(const upb_msgdef *m);
  3009. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
  3010. bool upb_msgdef_mapentry(const upb_msgdef *m);
  3011. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
  3012. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
  3013. bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax);
  3014. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
  3015. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  3016. size_t len);
  3017. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  3018. size_t len);
  3019. int upb_msgdef_numfields(const upb_msgdef *m);
  3020. int upb_msgdef_numoneofs(const upb_msgdef *m);
  3021. UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
  3022. const char *name) {
  3023. return upb_msgdef_ntoo(m, name, strlen(name));
  3024. }
  3025. UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
  3026. const char *name) {
  3027. return upb_msgdef_ntof(m, name, strlen(name));
  3028. }
  3029. /* Internal-only. */
  3030. size_t upb_msgdef_selectorcount(const upb_msgdef *m);
  3031. uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m);
  3032. /* Lookup of either field or oneof by name. Returns whether either was found.
  3033. * If the return is true, then the found def will be set, and the non-found
  3034. * one set to NULL. */
  3035. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  3036. const upb_fielddef **f, const upb_oneofdef **o);
  3037. UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
  3038. const upb_fielddef **f,
  3039. const upb_oneofdef **o) {
  3040. return upb_msgdef_lookupname(m, name, strlen(name), f, o);
  3041. }
  3042. /* Iteration over fields and oneofs. For example:
  3043. *
  3044. * upb_msg_field_iter i;
  3045. * for(upb_msg_field_begin(&i, m);
  3046. * !upb_msg_field_done(&i);
  3047. * upb_msg_field_next(&i)) {
  3048. * upb_fielddef *f = upb_msg_iter_field(&i);
  3049. * // ...
  3050. * }
  3051. *
  3052. * For C we don't have separate iterators for const and non-const.
  3053. * It is the caller's responsibility to cast the upb_fielddef* to
  3054. * const if the upb_msgdef* is const. */
  3055. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
  3056. void upb_msg_field_next(upb_msg_field_iter *iter);
  3057. bool upb_msg_field_done(const upb_msg_field_iter *iter);
  3058. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
  3059. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
  3060. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  3061. const upb_msg_field_iter * iter2);
  3062. /* Similar to above, we also support iterating through the oneofs in a
  3063. * msgdef. */
  3064. void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m);
  3065. void upb_msg_oneof_next(upb_msg_oneof_iter * iter);
  3066. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
  3067. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
  3068. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter);
  3069. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  3070. const upb_msg_oneof_iter *iter2);
  3071. #ifdef __cplusplus
  3072. } /* extern "C" */
  3073. /* Structure that describes a single .proto message type. */
  3074. class upb::MessageDefPtr {
  3075. public:
  3076. MessageDefPtr() : ptr_(nullptr) {}
  3077. explicit MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {}
  3078. const upb_msgdef *ptr() const { return ptr_; }
  3079. explicit operator bool() const { return ptr_ != nullptr; }
  3080. const char* full_name() const { return upb_msgdef_fullname(ptr_); }
  3081. const char* name() const { return upb_msgdef_name(ptr_); }
  3082. /* The number of fields that belong to the MessageDef. */
  3083. int field_count() const { return upb_msgdef_numfields(ptr_); }
  3084. /* The number of oneofs that belong to the MessageDef. */
  3085. int oneof_count() const { return upb_msgdef_numoneofs(ptr_); }
  3086. upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); }
  3087. /* These return null pointers if the field is not found. */
  3088. FieldDefPtr FindFieldByNumber(uint32_t number) const {
  3089. return FieldDefPtr(upb_msgdef_itof(ptr_, number));
  3090. }
  3091. FieldDefPtr FindFieldByName(const char* name, size_t len) const {
  3092. return FieldDefPtr(upb_msgdef_ntof(ptr_, name, len));
  3093. }
  3094. FieldDefPtr FindFieldByName(const char *name) const {
  3095. return FieldDefPtr(upb_msgdef_ntofz(ptr_, name));
  3096. }
  3097. template <class T>
  3098. FieldDefPtr FindFieldByName(const T& str) const {
  3099. return FindFieldByName(str.c_str(), str.size());
  3100. }
  3101. OneofDefPtr FindOneofByName(const char* name, size_t len) const {
  3102. return OneofDefPtr(upb_msgdef_ntoo(ptr_, name, len));
  3103. }
  3104. OneofDefPtr FindOneofByName(const char *name) const {
  3105. return OneofDefPtr(upb_msgdef_ntooz(ptr_, name));
  3106. }
  3107. template <class T>
  3108. OneofDefPtr FindOneofByName(const T &str) const {
  3109. return FindOneofByName(str.c_str(), str.size());
  3110. }
  3111. /* Is this message a map entry? */
  3112. bool mapentry() const { return upb_msgdef_mapentry(ptr_); }
  3113. /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
  3114. * non-well-known message. */
  3115. upb_wellknowntype_t wellknowntype() const {
  3116. return upb_msgdef_wellknowntype(ptr_);
  3117. }
  3118. /* Whether is a number wrapper. */
  3119. bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); }
  3120. /* Iteration over fields. The order is undefined. */
  3121. class const_field_iterator
  3122. : public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
  3123. public:
  3124. void operator++() { upb_msg_field_next(&iter_); }
  3125. FieldDefPtr operator*() const {
  3126. return FieldDefPtr(upb_msg_iter_field(&iter_));
  3127. }
  3128. bool operator!=(const const_field_iterator &other) const {
  3129. return !upb_msg_field_iter_isequal(&iter_, &other.iter_);
  3130. }
  3131. bool operator==(const const_field_iterator &other) const {
  3132. return upb_msg_field_iter_isequal(&iter_, &other.iter_);
  3133. }
  3134. private:
  3135. friend class MessageDefPtr;
  3136. explicit const_field_iterator() {}
  3137. explicit const_field_iterator(MessageDefPtr msg) {
  3138. upb_msg_field_begin(&iter_, msg.ptr());
  3139. }
  3140. static const_field_iterator end() {
  3141. const_field_iterator iter;
  3142. upb_msg_field_iter_setdone(&iter.iter_);
  3143. return iter;
  3144. }
  3145. upb_msg_field_iter iter_;
  3146. };
  3147. /* Iteration over oneofs. The order is undefined. */
  3148. class const_oneof_iterator
  3149. : public std::iterator<std::forward_iterator_tag, OneofDefPtr> {
  3150. public:
  3151. void operator++() { upb_msg_oneof_next(&iter_); }
  3152. OneofDefPtr operator*() const {
  3153. return OneofDefPtr(upb_msg_iter_oneof(&iter_));
  3154. }
  3155. bool operator!=(const const_oneof_iterator& other) const {
  3156. return !upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
  3157. }
  3158. bool operator==(const const_oneof_iterator &other) const {
  3159. return upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
  3160. }
  3161. private:
  3162. friend class MessageDefPtr;
  3163. const_oneof_iterator() {}
  3164. explicit const_oneof_iterator(MessageDefPtr msg) {
  3165. upb_msg_oneof_begin(&iter_, msg.ptr());
  3166. }
  3167. static const_oneof_iterator end() {
  3168. const_oneof_iterator iter;
  3169. upb_msg_oneof_iter_setdone(&iter.iter_);
  3170. return iter;
  3171. }
  3172. upb_msg_oneof_iter iter_;
  3173. };
  3174. class ConstFieldAccessor {
  3175. public:
  3176. explicit ConstFieldAccessor(const upb_msgdef* md) : md_(md) {}
  3177. const_field_iterator begin() { return MessageDefPtr(md_).field_begin(); }
  3178. const_field_iterator end() { return MessageDefPtr(md_).field_end(); }
  3179. private:
  3180. const upb_msgdef* md_;
  3181. };
  3182. class ConstOneofAccessor {
  3183. public:
  3184. explicit ConstOneofAccessor(const upb_msgdef* md) : md_(md) {}
  3185. const_oneof_iterator begin() { return MessageDefPtr(md_).oneof_begin(); }
  3186. const_oneof_iterator end() { return MessageDefPtr(md_).oneof_end(); }
  3187. private:
  3188. const upb_msgdef* md_;
  3189. };
  3190. const_field_iterator field_begin() const {
  3191. return const_field_iterator(*this);
  3192. }
  3193. const_field_iterator field_end() const { return const_field_iterator::end(); }
  3194. const_oneof_iterator oneof_begin() const {
  3195. return const_oneof_iterator(*this);
  3196. }
  3197. const_oneof_iterator oneof_end() const { return const_oneof_iterator::end(); }
  3198. ConstFieldAccessor fields() const { return ConstFieldAccessor(ptr()); }
  3199. ConstOneofAccessor oneofs() const { return ConstOneofAccessor(ptr()); }
  3200. private:
  3201. const upb_msgdef* ptr_;
  3202. };
  3203. inline upb::MessageDefPtr upb::FieldDefPtr::message_subdef() const {
  3204. return MessageDefPtr(upb_fielddef_msgsubdef(ptr_));
  3205. }
  3206. inline upb::MessageDefPtr upb::FieldDefPtr::containing_type() const {
  3207. return MessageDefPtr(upb_fielddef_containingtype(ptr_));
  3208. }
  3209. inline upb::MessageDefPtr upb::OneofDefPtr::containing_type() const {
  3210. return MessageDefPtr(upb_oneofdef_containingtype(ptr_));
  3211. }
  3212. #endif /* __cplusplus */
  3213. /* upb_enumdef ****************************************************************/
  3214. typedef upb_strtable_iter upb_enum_iter;
  3215. const char *upb_enumdef_fullname(const upb_enumdef *e);
  3216. const char *upb_enumdef_name(const upb_enumdef *e);
  3217. const upb_filedef *upb_enumdef_file(const upb_enumdef *e);
  3218. int32_t upb_enumdef_default(const upb_enumdef *e);
  3219. int upb_enumdef_numvals(const upb_enumdef *e);
  3220. /* Enum lookups:
  3221. * - ntoi: look up a name with specified length.
  3222. * - ntoiz: look up a name provided as a null-terminated string.
  3223. * - iton: look up an integer, returning the name as a null-terminated
  3224. * string. */
  3225. bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
  3226. int32_t *num);
  3227. UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
  3228. const char *name, int32_t *num) {
  3229. return upb_enumdef_ntoi(e, name, strlen(name), num);
  3230. }
  3231. const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
  3232. /* upb_enum_iter i;
  3233. * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
  3234. * // ...
  3235. * }
  3236. */
  3237. void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
  3238. void upb_enum_next(upb_enum_iter *iter);
  3239. bool upb_enum_done(upb_enum_iter *iter);
  3240. const char *upb_enum_iter_name(upb_enum_iter *iter);
  3241. int32_t upb_enum_iter_number(upb_enum_iter *iter);
  3242. #ifdef __cplusplus
  3243. class upb::EnumDefPtr {
  3244. public:
  3245. EnumDefPtr() : ptr_(nullptr) {}
  3246. explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {}
  3247. const upb_enumdef* ptr() const { return ptr_; }
  3248. explicit operator bool() const { return ptr_ != nullptr; }
  3249. const char* full_name() const { return upb_enumdef_fullname(ptr_); }
  3250. const char* name() const { return upb_enumdef_name(ptr_); }
  3251. /* The value that is used as the default when no field default is specified.
  3252. * If not set explicitly, the first value that was added will be used.
  3253. * The default value must be a member of the enum.
  3254. * Requires that value_count() > 0. */
  3255. int32_t default_value() const { return upb_enumdef_default(ptr_); }
  3256. /* Returns the number of values currently defined in the enum. Note that
  3257. * multiple names can refer to the same number, so this may be greater than
  3258. * the total number of unique numbers. */
  3259. int value_count() const { return upb_enumdef_numvals(ptr_); }
  3260. /* Lookups from name to integer, returning true if found. */
  3261. bool FindValueByName(const char *name, int32_t *num) const {
  3262. return upb_enumdef_ntoiz(ptr_, name, num);
  3263. }
  3264. /* Finds the name corresponding to the given number, or NULL if none was
  3265. * found. If more than one name corresponds to this number, returns the
  3266. * first one that was added. */
  3267. const char *FindValueByNumber(int32_t num) const {
  3268. return upb_enumdef_iton(ptr_, num);
  3269. }
  3270. /* Iteration over name/value pairs. The order is undefined.
  3271. * Adding an enum val invalidates any iterators.
  3272. *
  3273. * TODO: make compatible with range-for, with elements as pairs? */
  3274. class Iterator {
  3275. public:
  3276. explicit Iterator(EnumDefPtr e) { upb_enum_begin(&iter_, e.ptr()); }
  3277. int32_t number() { return upb_enum_iter_number(&iter_); }
  3278. const char *name() { return upb_enum_iter_name(&iter_); }
  3279. bool Done() { return upb_enum_done(&iter_); }
  3280. void Next() { return upb_enum_next(&iter_); }
  3281. private:
  3282. upb_enum_iter iter_;
  3283. };
  3284. private:
  3285. const upb_enumdef *ptr_;
  3286. };
  3287. inline upb::EnumDefPtr upb::FieldDefPtr::enum_subdef() const {
  3288. return EnumDefPtr(upb_fielddef_enumsubdef(ptr_));
  3289. }
  3290. #endif /* __cplusplus */
  3291. /* upb_filedef ****************************************************************/
  3292. #ifdef __cplusplus
  3293. extern "C" {
  3294. #endif
  3295. const char *upb_filedef_name(const upb_filedef *f);
  3296. const char *upb_filedef_package(const upb_filedef *f);
  3297. const char *upb_filedef_phpprefix(const upb_filedef *f);
  3298. const char *upb_filedef_phpnamespace(const upb_filedef *f);
  3299. upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
  3300. int upb_filedef_depcount(const upb_filedef *f);
  3301. int upb_filedef_msgcount(const upb_filedef *f);
  3302. int upb_filedef_enumcount(const upb_filedef *f);
  3303. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i);
  3304. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i);
  3305. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i);
  3306. #ifdef __cplusplus
  3307. } /* extern "C" */
  3308. /* Class that represents a .proto file with some things defined in it.
  3309. *
  3310. * Many users won't care about FileDefs, but they are necessary if you want to
  3311. * read the values of file-level options. */
  3312. class upb::FileDefPtr {
  3313. public:
  3314. explicit FileDefPtr(const upb_filedef *ptr) : ptr_(ptr) {}
  3315. const upb_filedef* ptr() const { return ptr_; }
  3316. explicit operator bool() const { return ptr_ != nullptr; }
  3317. /* Get/set name of the file (eg. "foo/bar.proto"). */
  3318. const char* name() const { return upb_filedef_name(ptr_); }
  3319. /* Package name for definitions inside the file (eg. "foo.bar"). */
  3320. const char* package() const { return upb_filedef_package(ptr_); }
  3321. /* Sets the php class prefix which is prepended to all php generated classes
  3322. * from this .proto. Default is empty. */
  3323. const char* phpprefix() const { return upb_filedef_phpprefix(ptr_); }
  3324. /* Use this option to change the namespace of php generated classes. Default
  3325. * is empty. When this option is empty, the package name will be used for
  3326. * determining the namespace. */
  3327. const char* phpnamespace() const { return upb_filedef_phpnamespace(ptr_); }
  3328. /* Syntax for the file. Defaults to proto2. */
  3329. upb_syntax_t syntax() const { return upb_filedef_syntax(ptr_); }
  3330. /* Get the list of dependencies from the file. These are returned in the
  3331. * order that they were added to the FileDefPtr. */
  3332. int dependency_count() const { return upb_filedef_depcount(ptr_); }
  3333. const FileDefPtr dependency(int index) const {
  3334. return FileDefPtr(upb_filedef_dep(ptr_, index));
  3335. }
  3336. private:
  3337. const upb_filedef* ptr_;
  3338. };
  3339. #endif /* __cplusplus */
  3340. /* upb_symtab *****************************************************************/
  3341. #ifdef __cplusplus
  3342. extern "C" {
  3343. #endif
  3344. upb_symtab *upb_symtab_new();
  3345. void upb_symtab_free(upb_symtab* s);
  3346. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
  3347. const upb_msgdef *upb_symtab_lookupmsg2(
  3348. const upb_symtab *s, const char *sym, size_t len);
  3349. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
  3350. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name);
  3351. int upb_symtab_filecount(const upb_symtab *s);
  3352. const upb_filedef *upb_symtab_addfile(
  3353. upb_symtab *s, const google_protobuf_FileDescriptorProto *file,
  3354. upb_status *status);
  3355. /* For generated code only: loads a generated descriptor. */
  3356. typedef struct upb_def_init {
  3357. struct upb_def_init **deps;
  3358. const char *filename;
  3359. upb_strview descriptor;
  3360. } upb_def_init;
  3361. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init);
  3362. #ifdef __cplusplus
  3363. } /* extern "C" */
  3364. /* Non-const methods in upb::SymbolTable are NOT thread-safe. */
  3365. class upb::SymbolTable {
  3366. public:
  3367. SymbolTable() : ptr_(upb_symtab_new(), upb_symtab_free) {}
  3368. explicit SymbolTable(upb_symtab* s) : ptr_(s, upb_symtab_free) {}
  3369. const upb_symtab* ptr() const { return ptr_.get(); }
  3370. upb_symtab* ptr() { return ptr_.get(); }
  3371. /* Finds an entry in the symbol table with this exact name. If not found,
  3372. * returns NULL. */
  3373. MessageDefPtr LookupMessage(const char *sym) const {
  3374. return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym));
  3375. }
  3376. EnumDefPtr LookupEnum(const char *sym) const {
  3377. return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym));
  3378. }
  3379. FileDefPtr LookupFile(const char *name) const {
  3380. return FileDefPtr(upb_symtab_lookupfile(ptr_.get(), name));
  3381. }
  3382. /* TODO: iteration? */
  3383. /* Adds the given serialized FileDescriptorProto to the pool. */
  3384. FileDefPtr AddFile(const google_protobuf_FileDescriptorProto *file_proto,
  3385. Status *status) {
  3386. return FileDefPtr(
  3387. upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()));
  3388. }
  3389. private:
  3390. std::unique_ptr<upb_symtab, decltype(&upb_symtab_free)> ptr_;
  3391. };
  3392. UPB_INLINE const char* upb_safecstr(const std::string& str) {
  3393. UPB_ASSERT(str.size() == std::strlen(str.c_str()));
  3394. return str.c_str();
  3395. }
  3396. #endif /* __cplusplus */
  3397. #endif /* UPB_DEF_H_ */
  3398. /*
  3399. ** upb::Handlers (upb_handlers)
  3400. **
  3401. ** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the
  3402. ** message can have associated functions that will be called when we are
  3403. ** parsing or visiting a stream of data. This is similar to how handlers work
  3404. ** in SAX (the Simple API for XML).
  3405. **
  3406. ** The handlers have no idea where the data is coming from, so a single set of
  3407. ** handlers could be used with two completely different data sources (for
  3408. ** example, a parser and a visitor over in-memory objects). This decoupling is
  3409. ** the most important feature of upb, because it allows parsers and serializers
  3410. ** to be highly reusable.
  3411. **
  3412. ** This is a mixed C/C++ interface that offers a full API to both languages.
  3413. ** See the top-level README for more information.
  3414. */
  3415. #ifndef UPB_HANDLERS_H
  3416. #define UPB_HANDLERS_H
  3417. #ifdef __cplusplus
  3418. namespace upb {
  3419. class HandlersPtr;
  3420. class HandlerCache;
  3421. template <class T> class Handler;
  3422. template <class T> struct CanonicalType;
  3423. } /* namespace upb */
  3424. #endif
  3425. /* The maximum depth that the handler graph can have. This is a resource limit
  3426. * for the C stack since we sometimes need to recursively traverse the graph.
  3427. * Cycles are ok; the traversal will stop when it detects a cycle, but we must
  3428. * hit the cycle before the maximum depth is reached.
  3429. *
  3430. * If having a single static limit is too inflexible, we can add another variant
  3431. * of Handlers::Freeze that allows specifying this as a parameter. */
  3432. #define UPB_MAX_HANDLER_DEPTH 64
  3433. /* All the different types of handlers that can be registered.
  3434. * Only needed for the advanced functions in upb::Handlers. */
  3435. typedef enum {
  3436. UPB_HANDLER_INT32,
  3437. UPB_HANDLER_INT64,
  3438. UPB_HANDLER_UINT32,
  3439. UPB_HANDLER_UINT64,
  3440. UPB_HANDLER_FLOAT,
  3441. UPB_HANDLER_DOUBLE,
  3442. UPB_HANDLER_BOOL,
  3443. UPB_HANDLER_STARTSTR,
  3444. UPB_HANDLER_STRING,
  3445. UPB_HANDLER_ENDSTR,
  3446. UPB_HANDLER_STARTSUBMSG,
  3447. UPB_HANDLER_ENDSUBMSG,
  3448. UPB_HANDLER_STARTSEQ,
  3449. UPB_HANDLER_ENDSEQ
  3450. } upb_handlertype_t;
  3451. #define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
  3452. #define UPB_BREAK NULL
  3453. /* A convenient definition for when no closure is needed. */
  3454. extern char _upb_noclosure;
  3455. #define UPB_NO_CLOSURE &_upb_noclosure
  3456. /* A selector refers to a specific field handler in the Handlers object
  3457. * (for example: the STARTSUBMSG handler for field "field15"). */
  3458. typedef int32_t upb_selector_t;
  3459. /* Static selectors for upb::Handlers. */
  3460. #define UPB_STARTMSG_SELECTOR 0
  3461. #define UPB_ENDMSG_SELECTOR 1
  3462. #define UPB_UNKNOWN_SELECTOR 2
  3463. #define UPB_STATIC_SELECTOR_COUNT 3
  3464. /* Static selectors for upb::BytesHandler. */
  3465. #define UPB_STARTSTR_SELECTOR 0
  3466. #define UPB_STRING_SELECTOR 1
  3467. #define UPB_ENDSTR_SELECTOR 2
  3468. #ifdef __cplusplus
  3469. template<class T> const void *UniquePtrForType() {
  3470. static const char ch = 0;
  3471. return &ch;
  3472. }
  3473. #endif
  3474. /* upb_handlers ************************************************************/
  3475. /* Handler attributes, to be registered with the handler itself. */
  3476. typedef struct {
  3477. const void *handler_data;
  3478. const void *closure_type;
  3479. const void *return_closure_type;
  3480. bool alwaysok;
  3481. } upb_handlerattr;
  3482. #define UPB_HANDLERATTR_INIT {NULL, NULL, NULL, false}
  3483. /* Bufhandle, data passed along with a buffer to indicate its provenance. */
  3484. typedef struct {
  3485. /* The beginning of the buffer. This may be different than the pointer
  3486. * passed to a StringBuf handler because the handler may receive data
  3487. * that is from the middle or end of a larger buffer. */
  3488. const char *buf;
  3489. /* The offset within the attached object where this buffer begins. Only
  3490. * meaningful if there is an attached object. */
  3491. size_t objofs;
  3492. /* The attached object (if any) and a pointer representing its type. */
  3493. const void *obj;
  3494. const void *objtype;
  3495. #ifdef __cplusplus
  3496. template <class T>
  3497. void SetAttachedObject(const T* _obj) {
  3498. obj = _obj;
  3499. objtype = UniquePtrForType<T>();
  3500. }
  3501. template <class T>
  3502. const T *GetAttachedObject() const {
  3503. return objtype == UniquePtrForType<T>() ? static_cast<const T *>(obj)
  3504. : NULL;
  3505. }
  3506. #endif
  3507. } upb_bufhandle;
  3508. #define UPB_BUFHANDLE_INIT {NULL, 0, NULL, NULL}
  3509. /* Handler function typedefs. */
  3510. typedef void upb_handlerfree(void *d);
  3511. typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf,
  3512. size_t n);
  3513. typedef bool upb_startmsg_handlerfunc(void *c, const void*);
  3514. typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
  3515. typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
  3516. typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
  3517. typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
  3518. typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
  3519. typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
  3520. typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
  3521. typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
  3522. typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
  3523. typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
  3524. typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
  3525. size_t size_hint);
  3526. typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
  3527. size_t n, const upb_bufhandle* handle);
  3528. struct upb_handlers;
  3529. typedef struct upb_handlers upb_handlers;
  3530. #ifdef __cplusplus
  3531. extern "C" {
  3532. #endif
  3533. /* Mutating accessors. */
  3534. const upb_status *upb_handlers_status(upb_handlers *h);
  3535. void upb_handlers_clearerr(upb_handlers *h);
  3536. const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
  3537. bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
  3538. bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func,
  3539. const upb_handlerattr *attr);
  3540. bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
  3541. const upb_handlerattr *attr);
  3542. bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
  3543. const upb_handlerattr *attr);
  3544. bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
  3545. upb_int32_handlerfunc *func,
  3546. const upb_handlerattr *attr);
  3547. bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
  3548. upb_int64_handlerfunc *func,
  3549. const upb_handlerattr *attr);
  3550. bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
  3551. upb_uint32_handlerfunc *func,
  3552. const upb_handlerattr *attr);
  3553. bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
  3554. upb_uint64_handlerfunc *func,
  3555. const upb_handlerattr *attr);
  3556. bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
  3557. upb_float_handlerfunc *func,
  3558. const upb_handlerattr *attr);
  3559. bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
  3560. upb_double_handlerfunc *func,
  3561. const upb_handlerattr *attr);
  3562. bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
  3563. upb_bool_handlerfunc *func,
  3564. const upb_handlerattr *attr);
  3565. bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
  3566. upb_startstr_handlerfunc *func,
  3567. const upb_handlerattr *attr);
  3568. bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
  3569. upb_string_handlerfunc *func,
  3570. const upb_handlerattr *attr);
  3571. bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
  3572. upb_endfield_handlerfunc *func,
  3573. const upb_handlerattr *attr);
  3574. bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
  3575. upb_startfield_handlerfunc *func,
  3576. const upb_handlerattr *attr);
  3577. bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
  3578. upb_startfield_handlerfunc *func,
  3579. const upb_handlerattr *attr);
  3580. bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
  3581. upb_endfield_handlerfunc *func,
  3582. const upb_handlerattr *attr);
  3583. bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
  3584. upb_endfield_handlerfunc *func,
  3585. const upb_handlerattr *attr);
  3586. /* Read-only accessors. */
  3587. const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
  3588. const upb_fielddef *f);
  3589. const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
  3590. upb_selector_t sel);
  3591. upb_func *upb_handlers_gethandler(const upb_handlers *h, upb_selector_t s,
  3592. const void **handler_data);
  3593. bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
  3594. upb_handlerattr *attr);
  3595. /* "Static" methods */
  3596. upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
  3597. bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
  3598. upb_selector_t *s);
  3599. UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
  3600. return start + 1;
  3601. }
  3602. /* Internal-only. */
  3603. uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
  3604. uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
  3605. #ifdef __cplusplus
  3606. } /* extern "C" */
  3607. namespace upb {
  3608. typedef upb_handlers Handlers;
  3609. }
  3610. /* Convenience macros for creating a Handler object that is wrapped with a
  3611. * type-safe wrapper function that converts the "void*" parameters/returns
  3612. * of the underlying C API into nice C++ function.
  3613. *
  3614. * Sample usage:
  3615. * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3616. * // do stuff ...
  3617. * }
  3618. *
  3619. * // Handler that doesn't need any data bound to it.
  3620. * void OnValue2(MyClosure* c, int32_t val) {
  3621. * // do stuff ...
  3622. * }
  3623. *
  3624. * // Handler that returns bool so it can return failure if necessary.
  3625. * bool OnValue3(MyClosure* c, int32_t val) {
  3626. * // do stuff ...
  3627. * return ok;
  3628. * }
  3629. *
  3630. * // Member function handler.
  3631. * class MyClosure {
  3632. * public:
  3633. * void OnValue(int32_t val) {
  3634. * // do stuff ...
  3635. * }
  3636. * };
  3637. *
  3638. * // Takes ownership of the MyHandlerData.
  3639. * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
  3640. * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
  3641. * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
  3642. * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
  3643. */
  3644. /* In C++11, the "template" disambiguator can appear even outside templates,
  3645. * so all calls can safely use this pair of macros. */
  3646. #define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
  3647. /* We have to be careful to only evaluate "d" once. */
  3648. #define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
  3649. /* Handler: a struct that contains the (handler, data, deleter) tuple that is
  3650. * used to register all handlers. Users can Make() these directly but it's
  3651. * more convenient to use the UpbMakeHandler/UpbBind macros above. */
  3652. template <class T> class upb::Handler {
  3653. public:
  3654. /* The underlying, handler function signature that upb uses internally. */
  3655. typedef T FuncPtr;
  3656. /* Intentionally implicit. */
  3657. template <class F> Handler(F func);
  3658. ~Handler() { UPB_ASSERT(registered_); }
  3659. void AddCleanup(upb_handlers* h) const;
  3660. FuncPtr handler() const { return handler_; }
  3661. const upb_handlerattr& attr() const { return attr_; }
  3662. private:
  3663. Handler(const Handler&) = delete;
  3664. Handler& operator=(const Handler&) = delete;
  3665. FuncPtr handler_;
  3666. mutable upb_handlerattr attr_;
  3667. mutable bool registered_;
  3668. void *cleanup_data_;
  3669. upb_handlerfree *cleanup_func_;
  3670. };
  3671. /* A upb::Handlers object represents the set of handlers associated with a
  3672. * message in the graph of messages. You can think of it as a big virtual
  3673. * table with functions corresponding to all the events that can fire while
  3674. * parsing or visiting a message of a specific type.
  3675. *
  3676. * Any handlers that are not set behave as if they had successfully consumed
  3677. * the value. Any unset Start* handlers will propagate their closure to the
  3678. * inner frame.
  3679. *
  3680. * The easiest way to create the *Handler objects needed by the Set* methods is
  3681. * with the UpbBind() and UpbMakeHandler() macros; see below. */
  3682. class upb::HandlersPtr {
  3683. public:
  3684. HandlersPtr(upb_handlers* ptr) : ptr_(ptr) {}
  3685. upb_handlers* ptr() const { return ptr_; }
  3686. typedef upb_selector_t Selector;
  3687. typedef upb_handlertype_t Type;
  3688. typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
  3689. typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
  3690. typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
  3691. typedef Handler<bool (*)(void *, const void *, upb_status *)>
  3692. EndMessageHandler;
  3693. typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
  3694. typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
  3695. const upb_bufhandle *)>
  3696. StringHandler;
  3697. template <class T> struct ValueHandler {
  3698. typedef Handler<bool(*)(void *, const void *, T)> H;
  3699. };
  3700. typedef ValueHandler<int32_t>::H Int32Handler;
  3701. typedef ValueHandler<int64_t>::H Int64Handler;
  3702. typedef ValueHandler<uint32_t>::H UInt32Handler;
  3703. typedef ValueHandler<uint64_t>::H UInt64Handler;
  3704. typedef ValueHandler<float>::H FloatHandler;
  3705. typedef ValueHandler<double>::H DoubleHandler;
  3706. typedef ValueHandler<bool>::H BoolHandler;
  3707. /* Any function pointer can be converted to this and converted back to its
  3708. * correct type. */
  3709. typedef void GenericFunction();
  3710. typedef void HandlersCallback(const void *closure, upb_handlers *h);
  3711. /* Returns the msgdef associated with this handlers object. */
  3712. MessageDefPtr message_def() const {
  3713. return MessageDefPtr(upb_handlers_msgdef(ptr()));
  3714. }
  3715. /* Adds the given pointer and function to the list of cleanup functions that
  3716. * will be run when these handlers are freed. If this pointer has previously
  3717. * been registered, the function returns false and does nothing. */
  3718. bool AddCleanup(void *ptr, upb_handlerfree *cleanup) {
  3719. return upb_handlers_addcleanup(ptr_, ptr, cleanup);
  3720. }
  3721. /* Sets the startmsg handler for the message, which is defined as follows:
  3722. *
  3723. * bool startmsg(MyType* closure) {
  3724. * // Called when the message begins. Returns true if processing should
  3725. * // continue.
  3726. * return true;
  3727. * }
  3728. */
  3729. bool SetStartMessageHandler(const StartMessageHandler &h) {
  3730. h.AddCleanup(ptr());
  3731. return upb_handlers_setstartmsg(ptr(), h.handler(), &h.attr());
  3732. }
  3733. /* Sets the endmsg handler for the message, which is defined as follows:
  3734. *
  3735. * bool endmsg(MyType* closure, upb_status *status) {
  3736. * // Called when processing of this message ends, whether in success or
  3737. * // failure. "status" indicates the final status of processing, and
  3738. * // can also be modified in-place to update the final status.
  3739. * }
  3740. */
  3741. bool SetEndMessageHandler(const EndMessageHandler& h) {
  3742. h.AddCleanup(ptr());
  3743. return upb_handlers_setendmsg(ptr(), h.handler(), &h.attr());
  3744. }
  3745. /* Sets the value handler for the given field, which is defined as follows
  3746. * (this is for an int32 field; other field types will pass their native
  3747. * C/C++ type for "val"):
  3748. *
  3749. * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3750. * // Called when the field's value is encountered. "d" contains
  3751. * // whatever data was bound to this field when it was registered.
  3752. * // Returns true if processing should continue.
  3753. * return true;
  3754. * }
  3755. *
  3756. * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
  3757. *
  3758. * The value type must exactly match f->type().
  3759. * For example, a handler that takes an int32_t parameter may only be used for
  3760. * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
  3761. *
  3762. * Returns false if the handler failed to register; in this case the cleanup
  3763. * handler (if any) will be called immediately.
  3764. */
  3765. bool SetInt32Handler(FieldDefPtr f, const Int32Handler &h) {
  3766. h.AddCleanup(ptr());
  3767. return upb_handlers_setint32(ptr(), f.ptr(), h.handler(), &h.attr());
  3768. }
  3769. bool SetInt64Handler (FieldDefPtr f, const Int64Handler& h) {
  3770. h.AddCleanup(ptr());
  3771. return upb_handlers_setint64(ptr(), f.ptr(), h.handler(), &h.attr());
  3772. }
  3773. bool SetUInt32Handler(FieldDefPtr f, const UInt32Handler& h) {
  3774. h.AddCleanup(ptr());
  3775. return upb_handlers_setuint32(ptr(), f.ptr(), h.handler(), &h.attr());
  3776. }
  3777. bool SetUInt64Handler(FieldDefPtr f, const UInt64Handler& h) {
  3778. h.AddCleanup(ptr());
  3779. return upb_handlers_setuint64(ptr(), f.ptr(), h.handler(), &h.attr());
  3780. }
  3781. bool SetFloatHandler (FieldDefPtr f, const FloatHandler& h) {
  3782. h.AddCleanup(ptr());
  3783. return upb_handlers_setfloat(ptr(), f.ptr(), h.handler(), &h.attr());
  3784. }
  3785. bool SetDoubleHandler(FieldDefPtr f, const DoubleHandler& h) {
  3786. h.AddCleanup(ptr());
  3787. return upb_handlers_setdouble(ptr(), f.ptr(), h.handler(), &h.attr());
  3788. }
  3789. bool SetBoolHandler(FieldDefPtr f, const BoolHandler &h) {
  3790. h.AddCleanup(ptr());
  3791. return upb_handlers_setbool(ptr(), f.ptr(), h.handler(), &h.attr());
  3792. }
  3793. /* Like the previous, but templated on the type on the value (ie. int32).
  3794. * This is mostly useful to call from other templates. To call this you must
  3795. * specify the template parameter explicitly, ie:
  3796. * h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */
  3797. template <class T>
  3798. bool SetValueHandler(
  3799. FieldDefPtr f,
  3800. const typename ValueHandler<typename CanonicalType<T>::Type>::H &handler);
  3801. /* Sets handlers for a string field, which are defined as follows:
  3802. *
  3803. * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
  3804. * size_t size_hint) {
  3805. * // Called when a string value begins. The return value indicates the
  3806. * // closure for the string. "size_hint" indicates the size of the
  3807. * // string if it is known, however if the string is length-delimited
  3808. * // and the end-of-string is not available size_hint will be zero.
  3809. * // This case is indistinguishable from the case where the size is
  3810. * // known to be zero.
  3811. * //
  3812. * // TODO(haberman): is it important to distinguish these cases?
  3813. * // If we had ssize_t as a type we could make -1 "unknown", but
  3814. * // ssize_t is POSIX (not ANSI) and therefore less portable.
  3815. * // In practice I suspect it won't be important to distinguish.
  3816. * return closure;
  3817. * }
  3818. *
  3819. * size_t str(MyClosure* closure, const MyHandlerData* d,
  3820. * const char *str, size_t len) {
  3821. * // Called for each buffer of string data; the multiple physical buffers
  3822. * // are all part of the same logical string. The return value indicates
  3823. * // how many bytes were consumed. If this number is less than "len",
  3824. * // this will also indicate that processing should be halted for now,
  3825. * // like returning false or UPB_BREAK from any other callback. If
  3826. * // number is greater than "len", the excess bytes will be skipped over
  3827. * // and not passed to the callback.
  3828. * return len;
  3829. * }
  3830. *
  3831. * bool endstr(MyClosure* c, const MyHandlerData* d) {
  3832. * // Called when a string value ends. Return value indicates whether
  3833. * // processing should continue.
  3834. * return true;
  3835. * }
  3836. */
  3837. bool SetStartStringHandler(FieldDefPtr f, const StartStringHandler &h) {
  3838. h.AddCleanup(ptr());
  3839. return upb_handlers_setstartstr(ptr(), f.ptr(), h.handler(), &h.attr());
  3840. }
  3841. bool SetStringHandler(FieldDefPtr f, const StringHandler& h) {
  3842. h.AddCleanup(ptr());
  3843. return upb_handlers_setstring(ptr(), f.ptr(), h.handler(), &h.attr());
  3844. }
  3845. bool SetEndStringHandler(FieldDefPtr f, const EndFieldHandler& h) {
  3846. h.AddCleanup(ptr());
  3847. return upb_handlers_setendstr(ptr(), f.ptr(), h.handler(), &h.attr());
  3848. }
  3849. /* Sets the startseq handler, which is defined as follows:
  3850. *
  3851. * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
  3852. * // Called when a sequence (repeated field) begins. The returned
  3853. * // pointer indicates the closure for the sequence (or UPB_BREAK
  3854. * // to interrupt processing).
  3855. * return closure;
  3856. * }
  3857. *
  3858. * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
  3859. *
  3860. * Returns "false" if "f" does not belong to this message or is not a
  3861. * repeated field.
  3862. */
  3863. bool SetStartSequenceHandler(FieldDefPtr f, const StartFieldHandler &h) {
  3864. h.AddCleanup(ptr());
  3865. return upb_handlers_setstartseq(ptr(), f.ptr(), h.handler(), &h.attr());
  3866. }
  3867. /* Sets the startsubmsg handler for the given field, which is defined as
  3868. * follows:
  3869. *
  3870. * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
  3871. * // Called when a submessage begins. The returned pointer indicates the
  3872. * // closure for the sequence (or UPB_BREAK to interrupt processing).
  3873. * return closure;
  3874. * }
  3875. *
  3876. * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
  3877. * new MyHandlerData(...)));
  3878. *
  3879. * Returns "false" if "f" does not belong to this message or is not a
  3880. * submessage/group field.
  3881. */
  3882. bool SetStartSubMessageHandler(FieldDefPtr f, const StartFieldHandler& h) {
  3883. h.AddCleanup(ptr());
  3884. return upb_handlers_setstartsubmsg(ptr(), f.ptr(), h.handler(), &h.attr());
  3885. }
  3886. /* Sets the endsubmsg handler for the given field, which is defined as
  3887. * follows:
  3888. *
  3889. * bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
  3890. * // Called when a submessage ends. Returns true to continue processing.
  3891. * return true;
  3892. * }
  3893. *
  3894. * Returns "false" if "f" does not belong to this message or is not a
  3895. * submessage/group field.
  3896. */
  3897. bool SetEndSubMessageHandler(FieldDefPtr f, const EndFieldHandler &h) {
  3898. h.AddCleanup(ptr());
  3899. return upb_handlers_setendsubmsg(ptr(), f.ptr(), h.handler(), &h.attr());
  3900. }
  3901. /* Starts the endsubseq handler for the given field, which is defined as
  3902. * follows:
  3903. *
  3904. * bool endseq(MyClosure* c, const MyHandlerData* d) {
  3905. * // Called when a sequence ends. Returns true continue processing.
  3906. * return true;
  3907. * }
  3908. *
  3909. * Returns "false" if "f" does not belong to this message or is not a
  3910. * repeated field.
  3911. */
  3912. bool SetEndSequenceHandler(FieldDefPtr f, const EndFieldHandler &h) {
  3913. h.AddCleanup(ptr());
  3914. return upb_handlers_setendseq(ptr(), f.ptr(), h.handler(), &h.attr());
  3915. }
  3916. private:
  3917. upb_handlers* ptr_;
  3918. };
  3919. #endif /* __cplusplus */
  3920. /* upb_handlercache ***********************************************************/
  3921. /* A upb_handlercache lazily builds and caches upb_handlers. You pass it a
  3922. * function (with optional closure) that can build handlers for a given
  3923. * message on-demand, and the cache maintains a map of msgdef->handlers. */
  3924. #ifdef __cplusplus
  3925. extern "C" {
  3926. #endif
  3927. struct upb_handlercache;
  3928. typedef struct upb_handlercache upb_handlercache;
  3929. typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
  3930. upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback,
  3931. const void *closure);
  3932. void upb_handlercache_free(upb_handlercache *cache);
  3933. const upb_handlers *upb_handlercache_get(upb_handlercache *cache,
  3934. const upb_msgdef *md);
  3935. bool upb_handlercache_addcleanup(upb_handlercache *h, void *p,
  3936. upb_handlerfree *hfree);
  3937. #ifdef __cplusplus
  3938. } /* extern "C" */
  3939. class upb::HandlerCache {
  3940. public:
  3941. HandlerCache(upb_handlers_callback *callback, const void *closure)
  3942. : ptr_(upb_handlercache_new(callback, closure), upb_handlercache_free) {}
  3943. HandlerCache(HandlerCache&&) = default;
  3944. HandlerCache& operator=(HandlerCache&&) = default;
  3945. HandlerCache(upb_handlercache* c) : ptr_(c, upb_handlercache_free) {}
  3946. upb_handlercache* ptr() { return ptr_.get(); }
  3947. const upb_handlers *Get(MessageDefPtr md) {
  3948. return upb_handlercache_get(ptr_.get(), md.ptr());
  3949. }
  3950. private:
  3951. std::unique_ptr<upb_handlercache, decltype(&upb_handlercache_free)> ptr_;
  3952. };
  3953. #endif /* __cplusplus */
  3954. /* upb_byteshandler ***********************************************************/
  3955. typedef struct {
  3956. upb_func *func;
  3957. /* It is wasteful to include the entire attributes here:
  3958. *
  3959. * * Some of the information is redundant (like storing the closure type
  3960. * separately for each handler that must match).
  3961. * * Some of the info is only needed prior to freeze() (like closure types).
  3962. * * alignment padding wastes a lot of space for alwaysok_.
  3963. *
  3964. * If/when the size and locality of handlers is an issue, we can optimize this
  3965. * not to store the entire attr like this. We do not expose the table's
  3966. * layout to allow this optimization in the future. */
  3967. upb_handlerattr attr;
  3968. } upb_handlers_tabent;
  3969. #define UPB_TABENT_INIT {NULL, UPB_HANDLERATTR_INIT}
  3970. typedef struct {
  3971. upb_handlers_tabent table[3];
  3972. } upb_byteshandler;
  3973. #define UPB_BYTESHANDLER_INIT \
  3974. { \
  3975. { UPB_TABENT_INIT, UPB_TABENT_INIT, UPB_TABENT_INIT } \
  3976. }
  3977. UPB_INLINE void upb_byteshandler_init(upb_byteshandler *handler) {
  3978. upb_byteshandler init = UPB_BYTESHANDLER_INIT;
  3979. *handler = init;
  3980. }
  3981. #ifdef __cplusplus
  3982. extern "C" {
  3983. #endif
  3984. /* Caller must ensure that "d" outlives the handlers. */
  3985. bool upb_byteshandler_setstartstr(upb_byteshandler *h,
  3986. upb_startstr_handlerfunc *func, void *d);
  3987. bool upb_byteshandler_setstring(upb_byteshandler *h,
  3988. upb_string_handlerfunc *func, void *d);
  3989. bool upb_byteshandler_setendstr(upb_byteshandler *h,
  3990. upb_endfield_handlerfunc *func, void *d);
  3991. #ifdef __cplusplus
  3992. } /* extern "C" */
  3993. namespace upb {
  3994. typedef upb_byteshandler BytesHandler;
  3995. }
  3996. #endif
  3997. /** Message handlers ******************************************************************/
  3998. #ifdef __cplusplus
  3999. extern "C" {
  4000. #endif
  4001. /* These are the handlers used internally by upb_msgfactory_getmergehandlers().
  4002. * They write scalar data to a known offset from the message pointer.
  4003. *
  4004. * These would be trivial for anyone to implement themselves, but it's better
  4005. * to use these because some JITs will recognize and specialize these instead
  4006. * of actually calling the function. */
  4007. /* Sets a handler for the given primitive field that will write the data at the
  4008. * given offset. If hasbit > 0, also sets a hasbit at the given bit offset
  4009. * (addressing each byte low to high). */
  4010. bool upb_msg_setscalarhandler(upb_handlers *h,
  4011. const upb_fielddef *f,
  4012. size_t offset,
  4013. int32_t hasbit);
  4014. /* If the given handler is a msghandlers_primitive field, returns true and sets
  4015. * *type, *offset and *hasbit. Otherwise returns false. */
  4016. bool upb_msg_getscalarhandlerdata(const upb_handlers *h,
  4017. upb_selector_t s,
  4018. upb_fieldtype_t *type,
  4019. size_t *offset,
  4020. int32_t *hasbit);
  4021. #ifdef __cplusplus
  4022. } /* extern "C" */
  4023. #endif
  4024. /*
  4025. ** Inline definitions for handlers.h, which are particularly long and a bit
  4026. ** tricky.
  4027. */
  4028. #ifndef UPB_HANDLERS_INL_H_
  4029. #define UPB_HANDLERS_INL_H_
  4030. #include <limits.h>
  4031. #include <stddef.h>
  4032. #ifdef __cplusplus
  4033. /* Type detection and typedefs for integer types.
  4034. * For platforms where there are multiple 32-bit or 64-bit types, we need to be
  4035. * able to enumerate them so we can properly create overloads for all variants.
  4036. *
  4037. * If any platform existed where there were three integer types with the same
  4038. * size, this would have to become more complicated. For example, short, int,
  4039. * and long could all be 32-bits. Even more diabolically, short, int, long,
  4040. * and long long could all be 64 bits and still be standard-compliant.
  4041. * However, few platforms are this strange, and it's unlikely that upb will be
  4042. * used on the strangest ones. */
  4043. /* Can't count on stdint.h limits like INT32_MAX, because in C++ these are
  4044. * only defined when __STDC_LIMIT_MACROS are defined before the *first* include
  4045. * of stdint.h. We can't guarantee that someone else didn't include these first
  4046. * without defining __STDC_LIMIT_MACROS. */
  4047. #define UPB_INT32_MAX 0x7fffffffLL
  4048. #define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
  4049. #define UPB_INT64_MAX 0x7fffffffffffffffLL
  4050. #define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
  4051. #if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
  4052. #define UPB_INT_IS_32BITS 1
  4053. #endif
  4054. #if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
  4055. #define UPB_LONG_IS_32BITS 1
  4056. #endif
  4057. #if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
  4058. #define UPB_LONG_IS_64BITS 1
  4059. #endif
  4060. #if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
  4061. #define UPB_LLONG_IS_64BITS 1
  4062. #endif
  4063. /* We use macros instead of typedefs so we can undefine them later and avoid
  4064. * leaking them outside this header file. */
  4065. #if UPB_INT_IS_32BITS
  4066. #define UPB_INT32_T int
  4067. #define UPB_UINT32_T unsigned int
  4068. #if UPB_LONG_IS_32BITS
  4069. #define UPB_TWO_32BIT_TYPES 1
  4070. #define UPB_INT32ALT_T long
  4071. #define UPB_UINT32ALT_T unsigned long
  4072. #endif /* UPB_LONG_IS_32BITS */
  4073. #elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */
  4074. #define UPB_INT32_T long
  4075. #define UPB_UINT32_T unsigned long
  4076. #endif /* UPB_INT_IS_32BITS */
  4077. #if UPB_LONG_IS_64BITS
  4078. #define UPB_INT64_T long
  4079. #define UPB_UINT64_T unsigned long
  4080. #if UPB_LLONG_IS_64BITS
  4081. #define UPB_TWO_64BIT_TYPES 1
  4082. #define UPB_INT64ALT_T long long
  4083. #define UPB_UINT64ALT_T unsigned long long
  4084. #endif /* UPB_LLONG_IS_64BITS */
  4085. #elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */
  4086. #define UPB_INT64_T long long
  4087. #define UPB_UINT64_T unsigned long long
  4088. #endif /* UPB_LONG_IS_64BITS */
  4089. #undef UPB_INT32_MAX
  4090. #undef UPB_INT32_MIN
  4091. #undef UPB_INT64_MAX
  4092. #undef UPB_INT64_MIN
  4093. #undef UPB_INT_IS_32BITS
  4094. #undef UPB_LONG_IS_32BITS
  4095. #undef UPB_LONG_IS_64BITS
  4096. #undef UPB_LLONG_IS_64BITS
  4097. namespace upb {
  4098. typedef void CleanupFunc(void *ptr);
  4099. /* Template to remove "const" from "const T*" and just return "T*".
  4100. *
  4101. * We define a nonsense default because otherwise it will fail to instantiate as
  4102. * a function parameter type even in cases where we don't expect any caller to
  4103. * actually match the overload. */
  4104. class CouldntRemoveConst {};
  4105. template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
  4106. template <class T> struct remove_constptr<const T *> { typedef T *type; };
  4107. /* Template that we use below to remove a template specialization from
  4108. * consideration if it matches a specific type. */
  4109. template <class T, class U> struct disable_if_same { typedef void Type; };
  4110. template <class T> struct disable_if_same<T, T> {};
  4111. template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
  4112. template <class T1, class T2>
  4113. struct FirstUnlessVoidOrBool {
  4114. typedef T1 value;
  4115. };
  4116. template <class T2>
  4117. struct FirstUnlessVoidOrBool<void, T2> {
  4118. typedef T2 value;
  4119. };
  4120. template <class T2>
  4121. struct FirstUnlessVoidOrBool<bool, T2> {
  4122. typedef T2 value;
  4123. };
  4124. template<class T, class U>
  4125. struct is_same {
  4126. static bool value;
  4127. };
  4128. template<class T>
  4129. struct is_same<T, T> {
  4130. static bool value;
  4131. };
  4132. template<class T, class U>
  4133. bool is_same<T, U>::value = false;
  4134. template<class T>
  4135. bool is_same<T, T>::value = true;
  4136. /* FuncInfo *******************************************************************/
  4137. /* Info about the user's original, pre-wrapped function. */
  4138. template <class C, class R = void>
  4139. struct FuncInfo {
  4140. /* The type of the closure that the function takes (its first param). */
  4141. typedef C Closure;
  4142. /* The return type. */
  4143. typedef R Return;
  4144. };
  4145. /* Func ***********************************************************************/
  4146. /* Func1, Func2, Func3: Template classes representing a function and its
  4147. * signature.
  4148. *
  4149. * Since the function is a template parameter, calling the function can be
  4150. * inlined at compile-time and does not require a function pointer at runtime.
  4151. * These functions are not bound to a handler data so have no data or cleanup
  4152. * handler. */
  4153. struct UnboundFunc {
  4154. CleanupFunc *GetCleanup() { return nullptr; }
  4155. void *GetData() { return nullptr; }
  4156. };
  4157. template <class R, class P1, R F(P1), class I>
  4158. struct Func1 : public UnboundFunc {
  4159. typedef R Return;
  4160. typedef I FuncInfo;
  4161. static R Call(P1 p1) { return F(p1); }
  4162. };
  4163. template <class R, class P1, class P2, R F(P1, P2), class I>
  4164. struct Func2 : public UnboundFunc {
  4165. typedef R Return;
  4166. typedef I FuncInfo;
  4167. static R Call(P1 p1, P2 p2) { return F(p1, p2); }
  4168. };
  4169. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4170. struct Func3 : public UnboundFunc {
  4171. typedef R Return;
  4172. typedef I FuncInfo;
  4173. static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
  4174. };
  4175. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4176. class I>
  4177. struct Func4 : public UnboundFunc {
  4178. typedef R Return;
  4179. typedef I FuncInfo;
  4180. static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
  4181. };
  4182. template <class R, class P1, class P2, class P3, class P4, class P5,
  4183. R F(P1, P2, P3, P4, P5), class I>
  4184. struct Func5 : public UnboundFunc {
  4185. typedef R Return;
  4186. typedef I FuncInfo;
  4187. static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  4188. return F(p1, p2, p3, p4, p5);
  4189. }
  4190. };
  4191. /* BoundFunc ******************************************************************/
  4192. /* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
  4193. * shall be bound to the function's second parameter.
  4194. *
  4195. * Note that the second parameter is a const pointer, but our stored bound value
  4196. * is non-const so we can free it when the handlers are destroyed. */
  4197. template <class T>
  4198. struct BoundFunc {
  4199. typedef typename remove_constptr<T>::type MutableP2;
  4200. explicit BoundFunc(MutableP2 data_) : data(data_) {}
  4201. CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
  4202. MutableP2 GetData() { return data; }
  4203. MutableP2 data;
  4204. };
  4205. template <class R, class P1, class P2, R F(P1, P2), class I>
  4206. struct BoundFunc2 : public BoundFunc<P2> {
  4207. typedef BoundFunc<P2> Base;
  4208. typedef I FuncInfo;
  4209. explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
  4210. };
  4211. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4212. struct BoundFunc3 : public BoundFunc<P2> {
  4213. typedef BoundFunc<P2> Base;
  4214. typedef I FuncInfo;
  4215. explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
  4216. };
  4217. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4218. class I>
  4219. struct BoundFunc4 : public BoundFunc<P2> {
  4220. typedef BoundFunc<P2> Base;
  4221. typedef I FuncInfo;
  4222. explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
  4223. };
  4224. template <class R, class P1, class P2, class P3, class P4, class P5,
  4225. R F(P1, P2, P3, P4, P5), class I>
  4226. struct BoundFunc5 : public BoundFunc<P2> {
  4227. typedef BoundFunc<P2> Base;
  4228. typedef I FuncInfo;
  4229. explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
  4230. };
  4231. /* FuncSig ********************************************************************/
  4232. /* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
  4233. * *signature*, but without a specific function attached.
  4234. *
  4235. * These classes contain member functions that can be invoked with a
  4236. * specific function to return a Func/BoundFunc class. */
  4237. template <class R, class P1>
  4238. struct FuncSig1 {
  4239. template <R F(P1)>
  4240. Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
  4241. return Func1<R, P1, F, FuncInfo<P1, R> >();
  4242. }
  4243. };
  4244. template <class R, class P1, class P2>
  4245. struct FuncSig2 {
  4246. template <R F(P1, P2)>
  4247. Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
  4248. return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
  4249. }
  4250. template <R F(P1, P2)>
  4251. BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
  4252. typename remove_constptr<P2>::type param2) {
  4253. return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
  4254. }
  4255. };
  4256. template <class R, class P1, class P2, class P3>
  4257. struct FuncSig3 {
  4258. template <R F(P1, P2, P3)>
  4259. Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
  4260. return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
  4261. }
  4262. template <R F(P1, P2, P3)>
  4263. BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
  4264. typename remove_constptr<P2>::type param2) {
  4265. return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
  4266. }
  4267. };
  4268. template <class R, class P1, class P2, class P3, class P4>
  4269. struct FuncSig4 {
  4270. template <R F(P1, P2, P3, P4)>
  4271. Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
  4272. return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
  4273. }
  4274. template <R F(P1, P2, P3, P4)>
  4275. BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
  4276. typename remove_constptr<P2>::type param2) {
  4277. return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
  4278. }
  4279. };
  4280. template <class R, class P1, class P2, class P3, class P4, class P5>
  4281. struct FuncSig5 {
  4282. template <R F(P1, P2, P3, P4, P5)>
  4283. Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
  4284. return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
  4285. }
  4286. template <R F(P1, P2, P3, P4, P5)>
  4287. BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
  4288. typename remove_constptr<P2>::type param2) {
  4289. return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
  4290. }
  4291. };
  4292. /* Overloaded template function that can construct the appropriate FuncSig*
  4293. * class given a function pointer by deducing the template parameters. */
  4294. template <class R, class P1>
  4295. inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
  4296. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4297. return FuncSig1<R, P1>();
  4298. }
  4299. template <class R, class P1, class P2>
  4300. inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
  4301. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4302. return FuncSig2<R, P1, P2>();
  4303. }
  4304. template <class R, class P1, class P2, class P3>
  4305. inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
  4306. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4307. return FuncSig3<R, P1, P2, P3>();
  4308. }
  4309. template <class R, class P1, class P2, class P3, class P4>
  4310. inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
  4311. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4312. return FuncSig4<R, P1, P2, P3, P4>();
  4313. }
  4314. template <class R, class P1, class P2, class P3, class P4, class P5>
  4315. inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
  4316. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4317. return FuncSig5<R, P1, P2, P3, P4, P5>();
  4318. }
  4319. /* MethodSig ******************************************************************/
  4320. /* CallMethod*: a function template that calls a given method. */
  4321. template <class R, class C, R (C::*F)()>
  4322. R CallMethod0(C *obj) {
  4323. return ((*obj).*F)();
  4324. }
  4325. template <class R, class C, class P1, R (C::*F)(P1)>
  4326. R CallMethod1(C *obj, P1 arg1) {
  4327. return ((*obj).*F)(arg1);
  4328. }
  4329. template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
  4330. R CallMethod2(C *obj, P1 arg1, P2 arg2) {
  4331. return ((*obj).*F)(arg1, arg2);
  4332. }
  4333. template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
  4334. R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
  4335. return ((*obj).*F)(arg1, arg2, arg3);
  4336. }
  4337. template <class R, class C, class P1, class P2, class P3, class P4,
  4338. R (C::*F)(P1, P2, P3, P4)>
  4339. R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
  4340. return ((*obj).*F)(arg1, arg2, arg3, arg4);
  4341. }
  4342. /* MethodSig: like FuncSig, but for member functions.
  4343. *
  4344. * GetFunc() returns a normal FuncN object, so after calling GetFunc() no
  4345. * more logic is required to special-case methods. */
  4346. template <class R, class C>
  4347. struct MethodSig0 {
  4348. template <R (C::*F)()>
  4349. Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
  4350. return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
  4351. }
  4352. };
  4353. template <class R, class C, class P1>
  4354. struct MethodSig1 {
  4355. template <R (C::*F)(P1)>
  4356. Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
  4357. return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
  4358. }
  4359. template <R (C::*F)(P1)>
  4360. BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
  4361. typename remove_constptr<P1>::type param1) {
  4362. return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
  4363. param1);
  4364. }
  4365. };
  4366. template <class R, class C, class P1, class P2>
  4367. struct MethodSig2 {
  4368. template <R (C::*F)(P1, P2)>
  4369. Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4370. GetFunc() {
  4371. return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4372. FuncInfo<C *, R> >();
  4373. }
  4374. template <R (C::*F)(P1, P2)>
  4375. BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4376. GetFunc(typename remove_constptr<P1>::type param1) {
  4377. return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4378. FuncInfo<C *, R> >(param1);
  4379. }
  4380. };
  4381. template <class R, class C, class P1, class P2, class P3>
  4382. struct MethodSig3 {
  4383. template <R (C::*F)(P1, P2, P3)>
  4384. Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
  4385. GetFunc() {
  4386. return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4387. FuncInfo<C *, R> >();
  4388. }
  4389. template <R (C::*F)(P1, P2, P3)>
  4390. BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4391. FuncInfo<C *, R> >
  4392. GetFunc(typename remove_constptr<P1>::type param1) {
  4393. return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4394. FuncInfo<C *, R> >(param1);
  4395. }
  4396. };
  4397. template <class R, class C, class P1, class P2, class P3, class P4>
  4398. struct MethodSig4 {
  4399. template <R (C::*F)(P1, P2, P3, P4)>
  4400. Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4401. FuncInfo<C *, R> >
  4402. GetFunc() {
  4403. return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4404. FuncInfo<C *, R> >();
  4405. }
  4406. template <R (C::*F)(P1, P2, P3, P4)>
  4407. BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4408. FuncInfo<C *, R> >
  4409. GetFunc(typename remove_constptr<P1>::type param1) {
  4410. return BoundFunc5<R, C *, P1, P2, P3, P4,
  4411. CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
  4412. param1);
  4413. }
  4414. };
  4415. template <class R, class C>
  4416. inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
  4417. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4418. return MethodSig0<R, C>();
  4419. }
  4420. template <class R, class C, class P1>
  4421. inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
  4422. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4423. return MethodSig1<R, C, P1>();
  4424. }
  4425. template <class R, class C, class P1, class P2>
  4426. inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
  4427. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4428. return MethodSig2<R, C, P1, P2>();
  4429. }
  4430. template <class R, class C, class P1, class P2, class P3>
  4431. inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
  4432. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4433. return MethodSig3<R, C, P1, P2, P3>();
  4434. }
  4435. template <class R, class C, class P1, class P2, class P3, class P4>
  4436. inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
  4437. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4438. return MethodSig4<R, C, P1, P2, P3, P4>();
  4439. }
  4440. /* MaybeWrapReturn ************************************************************/
  4441. /* Template class that attempts to wrap the return value of the function so it
  4442. * matches the expected type. There are two main adjustments it may make:
  4443. *
  4444. * 1. If the function returns void, make it return the expected type and with
  4445. * a value that always indicates success.
  4446. * 2. If the function returns bool, make it return the expected type with a
  4447. * value that indicates success or failure.
  4448. *
  4449. * The "expected type" for return is:
  4450. * 1. void* for start handlers. If the closure parameter has a different type
  4451. * we will cast it to void* for the return in the success case.
  4452. * 2. size_t for string buffer handlers.
  4453. * 3. bool for everything else. */
  4454. /* Template parameters are FuncN type and desired return type. */
  4455. template <class F, class R, class Enable = void>
  4456. struct MaybeWrapReturn;
  4457. /* If the return type matches, return the given function unwrapped. */
  4458. template <class F>
  4459. struct MaybeWrapReturn<F, typename F::Return> {
  4460. typedef F Func;
  4461. };
  4462. /* Function wrapper that munges the return value from void to (bool)true. */
  4463. template <class P1, class P2, void F(P1, P2)>
  4464. bool ReturnTrue2(P1 p1, P2 p2) {
  4465. F(p1, p2);
  4466. return true;
  4467. }
  4468. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4469. bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
  4470. F(p1, p2, p3);
  4471. return true;
  4472. }
  4473. /* Function wrapper that munges the return value from void to (void*)arg1 */
  4474. template <class P1, class P2, void F(P1, P2)>
  4475. void *ReturnClosure2(P1 p1, P2 p2) {
  4476. F(p1, p2);
  4477. return p1;
  4478. }
  4479. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4480. void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
  4481. F(p1, p2, p3);
  4482. return p1;
  4483. }
  4484. /* Function wrapper that munges the return value from R to void*. */
  4485. template <class R, class P1, class P2, R F(P1, P2)>
  4486. void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
  4487. return F(p1, p2);
  4488. }
  4489. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4490. void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
  4491. return F(p1, p2, p3);
  4492. }
  4493. /* Function wrapper that munges the return value from bool to void*. */
  4494. template <class P1, class P2, bool F(P1, P2)>
  4495. void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
  4496. return F(p1, p2) ? p1 : UPB_BREAK;
  4497. }
  4498. template <class P1, class P2, class P3, bool F(P1, P2, P3)>
  4499. void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
  4500. return F(p1, p2, p3) ? p1 : UPB_BREAK;
  4501. }
  4502. /* For the string callback, which takes five params, returns the size param. */
  4503. template <class P1, class P2,
  4504. void F(P1, P2, const char *, size_t, const upb_bufhandle *)>
  4505. size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
  4506. const upb_bufhandle *p5) {
  4507. F(p1, p2, p3, p4, p5);
  4508. return p4;
  4509. }
  4510. /* For the string callback, which takes five params, returns the size param or
  4511. * zero. */
  4512. template <class P1, class P2,
  4513. bool F(P1, P2, const char *, size_t, const upb_bufhandle *)>
  4514. size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
  4515. const upb_bufhandle *p5) {
  4516. return F(p1, p2, p3, p4, p5) ? p4 : 0;
  4517. }
  4518. /* If we have a function returning void but want a function returning bool, wrap
  4519. * it in a function that returns true. */
  4520. template <class P1, class P2, void F(P1, P2), class I>
  4521. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
  4522. typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
  4523. };
  4524. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4525. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
  4526. typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
  4527. };
  4528. /* If our function returns void but we want one returning void*, wrap it in a
  4529. * function that returns the first argument. */
  4530. template <class P1, class P2, void F(P1, P2), class I>
  4531. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
  4532. typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
  4533. };
  4534. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4535. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
  4536. typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
  4537. };
  4538. /* If our function returns R* but we want one returning void*, wrap it in a
  4539. * function that casts to void*. */
  4540. template <class R, class P1, class P2, R *F(P1, P2), class I>
  4541. struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
  4542. typename disable_if_same<R *, void *>::Type> {
  4543. typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
  4544. };
  4545. template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
  4546. struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
  4547. typename disable_if_same<R *, void *>::Type> {
  4548. typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
  4549. Func;
  4550. };
  4551. /* If our function returns bool but we want one returning void*, wrap it in a
  4552. * function that returns either the first param or UPB_BREAK. */
  4553. template <class P1, class P2, bool F(P1, P2), class I>
  4554. struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
  4555. typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
  4556. };
  4557. template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
  4558. struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
  4559. typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
  4560. Func;
  4561. };
  4562. /* If our function returns void but we want one returning size_t, wrap it in a
  4563. * function that returns the size argument. */
  4564. template <class P1, class P2,
  4565. void F(P1, P2, const char *, size_t, const upb_bufhandle *), class I>
  4566. struct MaybeWrapReturn<
  4567. Func5<void, P1, P2, const char *, size_t, const upb_bufhandle *, F, I>,
  4568. size_t> {
  4569. typedef Func5<size_t, P1, P2, const char *, size_t, const upb_bufhandle *,
  4570. ReturnStringLen<P1, P2, F>, I> Func;
  4571. };
  4572. /* If our function returns bool but we want one returning size_t, wrap it in a
  4573. * function that returns either 0 or the buf size. */
  4574. template <class P1, class P2,
  4575. bool F(P1, P2, const char *, size_t, const upb_bufhandle *), class I>
  4576. struct MaybeWrapReturn<
  4577. Func5<bool, P1, P2, const char *, size_t, const upb_bufhandle *, F, I>,
  4578. size_t> {
  4579. typedef Func5<size_t, P1, P2, const char *, size_t, const upb_bufhandle *,
  4580. ReturnNOr0<P1, P2, F>, I> Func;
  4581. };
  4582. /* ConvertParams **************************************************************/
  4583. /* Template class that converts the function parameters if necessary, and
  4584. * ignores the HandlerData parameter if appropriate.
  4585. *
  4586. * Template parameter is the are FuncN function type. */
  4587. template <class F, class T>
  4588. struct ConvertParams;
  4589. /* Function that discards the handler data parameter. */
  4590. template <class R, class P1, R F(P1)>
  4591. R IgnoreHandlerData2(void *p1, const void *hd) {
  4592. UPB_UNUSED(hd);
  4593. return F(static_cast<P1>(p1));
  4594. }
  4595. template <class R, class P1, class P2Wrapper, class P2Wrapped,
  4596. R F(P1, P2Wrapped)>
  4597. R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
  4598. UPB_UNUSED(hd);
  4599. return F(static_cast<P1>(p1), p2);
  4600. }
  4601. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4602. R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
  4603. UPB_UNUSED(hd);
  4604. return F(static_cast<P1>(p1), p2, p3);
  4605. }
  4606. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
  4607. R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
  4608. UPB_UNUSED(hd);
  4609. return F(static_cast<P1>(p1), p2, p3, p4);
  4610. }
  4611. template <class R, class P1, R F(P1, const char*, size_t)>
  4612. R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
  4613. size_t p3, const upb_bufhandle *handle) {
  4614. UPB_UNUSED(hd);
  4615. UPB_UNUSED(handle);
  4616. return F(static_cast<P1>(p1), p2, p3);
  4617. }
  4618. /* Function that casts the handler data parameter. */
  4619. template <class R, class P1, class P2, R F(P1, P2)>
  4620. R CastHandlerData2(void *c, const void *hd) {
  4621. return F(static_cast<P1>(c), static_cast<P2>(hd));
  4622. }
  4623. template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
  4624. R F(P1, P2, P3Wrapped)>
  4625. R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
  4626. return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
  4627. }
  4628. template <class R, class P1, class P2, class P3, class P4, class P5,
  4629. R F(P1, P2, P3, P4, P5)>
  4630. R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
  4631. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
  4632. }
  4633. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
  4634. R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
  4635. size_t p4, const upb_bufhandle *handle) {
  4636. UPB_UNUSED(handle);
  4637. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
  4638. }
  4639. /* For unbound functions, ignore the handler data. */
  4640. template <class R, class P1, R F(P1), class I, class T>
  4641. struct ConvertParams<Func1<R, P1, F, I>, T> {
  4642. typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
  4643. };
  4644. template <class R, class P1, class P2, R F(P1, P2), class I,
  4645. class R2, class P1_2, class P2_2, class P3_2>
  4646. struct ConvertParams<Func2<R, P1, P2, F, I>,
  4647. R2 (*)(P1_2, P2_2, P3_2)> {
  4648. typedef Func3<R, void *, const void *, P3_2,
  4649. IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
  4650. };
  4651. /* For StringBuffer only; this ignores both the handler data and the
  4652. * upb_bufhandle. */
  4653. template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
  4654. struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
  4655. typedef Func5<R, void *, const void *, const char *, size_t,
  4656. const upb_bufhandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
  4657. I> Func;
  4658. };
  4659. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4660. class I, class T>
  4661. struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
  4662. typedef Func5<R, void *, const void *, P2, P3, P4,
  4663. IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
  4664. };
  4665. /* For bound functions, cast the handler data. */
  4666. template <class R, class P1, class P2, R F(P1, P2), class I, class T>
  4667. struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
  4668. typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
  4669. Func;
  4670. };
  4671. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
  4672. class R2, class P1_2, class P2_2, class P3_2>
  4673. struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
  4674. R2 (*)(P1_2, P2_2, P3_2)> {
  4675. typedef Func3<R, void *, const void *, P3_2,
  4676. CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
  4677. };
  4678. /* For StringBuffer only; this ignores the upb_bufhandle. */
  4679. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
  4680. class I, class T>
  4681. struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
  4682. typedef Func5<R, void *, const void *, const char *, size_t,
  4683. const upb_bufhandle *,
  4684. CastHandlerDataIgnoreHandle<R, P1, P2, F>, I>
  4685. Func;
  4686. };
  4687. template <class R, class P1, class P2, class P3, class P4, class P5,
  4688. R F(P1, P2, P3, P4, P5), class I, class T>
  4689. struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
  4690. typedef Func5<R, void *, const void *, P3, P4, P5,
  4691. CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
  4692. };
  4693. /* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
  4694. * variant C type. */
  4695. #define TYPE_METHODS(utype, ltype, ctype, vtype) \
  4696. template <> \
  4697. struct CanonicalType<vtype> { \
  4698. typedef ctype Type; \
  4699. }; \
  4700. template <> \
  4701. inline bool HandlersPtr::SetValueHandler<vtype>( \
  4702. FieldDefPtr f, const HandlersPtr::utype##Handler &handler) { \
  4703. handler.AddCleanup(ptr()); \
  4704. return upb_handlers_set##ltype(ptr(), f.ptr(), handler.handler(), \
  4705. &handler.attr()); \
  4706. }
  4707. TYPE_METHODS(Double, double, double, double)
  4708. TYPE_METHODS(Float, float, float, float)
  4709. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T)
  4710. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T)
  4711. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T)
  4712. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T)
  4713. TYPE_METHODS(Bool, bool, bool, bool)
  4714. #ifdef UPB_TWO_32BIT_TYPES
  4715. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T)
  4716. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T)
  4717. #endif
  4718. #ifdef UPB_TWO_64BIT_TYPES
  4719. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T)
  4720. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T)
  4721. #endif
  4722. #undef TYPE_METHODS
  4723. template <> struct CanonicalType<Status*> {
  4724. typedef Status* Type;
  4725. };
  4726. template <class F> struct ReturnOf;
  4727. template <class R, class P1, class P2>
  4728. struct ReturnOf<R (*)(P1, P2)> {
  4729. typedef R Return;
  4730. };
  4731. template <class R, class P1, class P2, class P3>
  4732. struct ReturnOf<R (*)(P1, P2, P3)> {
  4733. typedef R Return;
  4734. };
  4735. template <class R, class P1, class P2, class P3, class P4>
  4736. struct ReturnOf<R (*)(P1, P2, P3, P4)> {
  4737. typedef R Return;
  4738. };
  4739. template <class R, class P1, class P2, class P3, class P4, class P5>
  4740. struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
  4741. typedef R Return;
  4742. };
  4743. template <class T>
  4744. template <class F>
  4745. inline Handler<T>::Handler(F func)
  4746. : registered_(false),
  4747. cleanup_data_(func.GetData()),
  4748. cleanup_func_(func.GetCleanup()) {
  4749. attr_.handler_data = func.GetData();
  4750. typedef typename ReturnOf<T>::Return Return;
  4751. typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
  4752. typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
  4753. ReturnWrappedFunc;
  4754. handler_ = ReturnWrappedFunc().Call;
  4755. /* Set attributes based on what templates can statically tell us about the
  4756. * user's function. */
  4757. /* If the original function returns void, then we know that we wrapped it to
  4758. * always return ok. */
  4759. bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
  4760. attr_.alwaysok = always_ok;
  4761. /* Closure parameter and return type. */
  4762. attr_.closure_type = UniquePtrForType<typename F::FuncInfo::Closure>();
  4763. /* We use the closure type (from the first parameter) if the return type is
  4764. * void or bool, since these are the two cases we wrap to return the closure's
  4765. * type anyway.
  4766. *
  4767. * This is all nonsense for non START* handlers, but it doesn't matter because
  4768. * in that case the value will be ignored. */
  4769. typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
  4770. typename F::FuncInfo::Closure>::value
  4771. EffectiveReturn;
  4772. attr_.return_closure_type = UniquePtrForType<EffectiveReturn>();
  4773. }
  4774. template <class T>
  4775. inline void Handler<T>::AddCleanup(upb_handlers* h) const {
  4776. UPB_ASSERT(!registered_);
  4777. registered_ = true;
  4778. if (cleanup_func_) {
  4779. bool ok = upb_handlers_addcleanup(h, cleanup_data_, cleanup_func_);
  4780. UPB_ASSERT(ok);
  4781. }
  4782. }
  4783. } /* namespace upb */
  4784. #endif /* __cplusplus */
  4785. #undef UPB_TWO_32BIT_TYPES
  4786. #undef UPB_TWO_64BIT_TYPES
  4787. #undef UPB_INT32_T
  4788. #undef UPB_UINT32_T
  4789. #undef UPB_INT32ALT_T
  4790. #undef UPB_UINT32ALT_T
  4791. #undef UPB_INT64_T
  4792. #undef UPB_UINT64_T
  4793. #undef UPB_INT64ALT_T
  4794. #undef UPB_UINT64ALT_T
  4795. #endif /* UPB_HANDLERS_INL_H_ */
  4796. #endif /* UPB_HANDLERS_H */
  4797. /*
  4798. ** upb::Sink (upb_sink)
  4799. ** upb::BytesSink (upb_bytessink)
  4800. **
  4801. ** A upb_sink is an object that binds a upb_handlers object to some runtime
  4802. ** state. It is the object that can actually receive data via the upb_handlers
  4803. ** interface.
  4804. **
  4805. ** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
  4806. ** thread-safe. You can create as many of them as you want, but each one may
  4807. ** only be used in a single thread at a time.
  4808. **
  4809. ** If we compare with class-based OOP, a you can think of a upb_def as an
  4810. ** abstract base class, a upb_handlers as a concrete derived class, and a
  4811. ** upb_sink as an object (class instance).
  4812. */
  4813. #ifndef UPB_SINK_H
  4814. #define UPB_SINK_H
  4815. #ifdef __cplusplus
  4816. namespace upb {
  4817. class BytesSink;
  4818. class Sink;
  4819. }
  4820. #endif
  4821. /* upb_sink *******************************************************************/
  4822. #ifdef __cplusplus
  4823. extern "C" {
  4824. #endif
  4825. typedef struct {
  4826. const upb_handlers *handlers;
  4827. void *closure;
  4828. } upb_sink;
  4829. #define PUTVAL(type, ctype) \
  4830. UPB_INLINE bool upb_sink_put##type(upb_sink s, upb_selector_t sel, \
  4831. ctype val) { \
  4832. typedef upb_##type##_handlerfunc functype; \
  4833. functype *func; \
  4834. const void *hd; \
  4835. if (!s.handlers) return true; \
  4836. func = (functype *)upb_handlers_gethandler(s.handlers, sel, &hd); \
  4837. if (!func) return true; \
  4838. return func(s.closure, hd, val); \
  4839. }
  4840. PUTVAL(int32, int32_t)
  4841. PUTVAL(int64, int64_t)
  4842. PUTVAL(uint32, uint32_t)
  4843. PUTVAL(uint64, uint64_t)
  4844. PUTVAL(float, float)
  4845. PUTVAL(double, double)
  4846. PUTVAL(bool, bool)
  4847. #undef PUTVAL
  4848. UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
  4849. s->handlers = h;
  4850. s->closure = c;
  4851. }
  4852. UPB_INLINE size_t upb_sink_putstring(upb_sink s, upb_selector_t sel,
  4853. const char *buf, size_t n,
  4854. const upb_bufhandle *handle) {
  4855. typedef upb_string_handlerfunc func;
  4856. func *handler;
  4857. const void *hd;
  4858. if (!s.handlers) return n;
  4859. handler = (func *)upb_handlers_gethandler(s.handlers, sel, &hd);
  4860. if (!handler) return n;
  4861. return handler(s.closure, hd, buf, n, handle);
  4862. }
  4863. UPB_INLINE bool upb_sink_putunknown(upb_sink s, const char *buf, size_t n) {
  4864. typedef upb_unknown_handlerfunc func;
  4865. func *handler;
  4866. const void *hd;
  4867. if (!s.handlers) return true;
  4868. handler =
  4869. (func *)upb_handlers_gethandler(s.handlers, UPB_UNKNOWN_SELECTOR, &hd);
  4870. if (!handler) return n;
  4871. return handler(s.closure, hd, buf, n);
  4872. }
  4873. UPB_INLINE bool upb_sink_startmsg(upb_sink s) {
  4874. typedef upb_startmsg_handlerfunc func;
  4875. func *startmsg;
  4876. const void *hd;
  4877. if (!s.handlers) return true;
  4878. startmsg =
  4879. (func *)upb_handlers_gethandler(s.handlers, UPB_STARTMSG_SELECTOR, &hd);
  4880. if (!startmsg) return true;
  4881. return startmsg(s.closure, hd);
  4882. }
  4883. UPB_INLINE bool upb_sink_endmsg(upb_sink s, upb_status *status) {
  4884. typedef upb_endmsg_handlerfunc func;
  4885. func *endmsg;
  4886. const void *hd;
  4887. if (!s.handlers) return true;
  4888. endmsg =
  4889. (func *)upb_handlers_gethandler(s.handlers, UPB_ENDMSG_SELECTOR, &hd);
  4890. if (!endmsg) return true;
  4891. return endmsg(s.closure, hd, status);
  4892. }
  4893. UPB_INLINE bool upb_sink_startseq(upb_sink s, upb_selector_t sel,
  4894. upb_sink *sub) {
  4895. typedef upb_startfield_handlerfunc func;
  4896. func *startseq;
  4897. const void *hd;
  4898. sub->closure = s.closure;
  4899. sub->handlers = s.handlers;
  4900. if (!s.handlers) return true;
  4901. startseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4902. if (!startseq) return true;
  4903. sub->closure = startseq(s.closure, hd);
  4904. return sub->closure ? true : false;
  4905. }
  4906. UPB_INLINE bool upb_sink_endseq(upb_sink s, upb_selector_t sel) {
  4907. typedef upb_endfield_handlerfunc func;
  4908. func *endseq;
  4909. const void *hd;
  4910. if (!s.handlers) return true;
  4911. endseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4912. if (!endseq) return true;
  4913. return endseq(s.closure, hd);
  4914. }
  4915. UPB_INLINE bool upb_sink_startstr(upb_sink s, upb_selector_t sel,
  4916. size_t size_hint, upb_sink *sub) {
  4917. typedef upb_startstr_handlerfunc func;
  4918. func *startstr;
  4919. const void *hd;
  4920. sub->closure = s.closure;
  4921. sub->handlers = s.handlers;
  4922. if (!s.handlers) return true;
  4923. startstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4924. if (!startstr) return true;
  4925. sub->closure = startstr(s.closure, hd, size_hint);
  4926. return sub->closure ? true : false;
  4927. }
  4928. UPB_INLINE bool upb_sink_endstr(upb_sink s, upb_selector_t sel) {
  4929. typedef upb_endfield_handlerfunc func;
  4930. func *endstr;
  4931. const void *hd;
  4932. if (!s.handlers) return true;
  4933. endstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4934. if (!endstr) return true;
  4935. return endstr(s.closure, hd);
  4936. }
  4937. UPB_INLINE bool upb_sink_startsubmsg(upb_sink s, upb_selector_t sel,
  4938. upb_sink *sub) {
  4939. typedef upb_startfield_handlerfunc func;
  4940. func *startsubmsg;
  4941. const void *hd;
  4942. sub->closure = s.closure;
  4943. if (!s.handlers) {
  4944. sub->handlers = NULL;
  4945. return true;
  4946. }
  4947. sub->handlers = upb_handlers_getsubhandlers_sel(s.handlers, sel);
  4948. startsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4949. if (!startsubmsg) return true;
  4950. sub->closure = startsubmsg(s.closure, hd);
  4951. return sub->closure ? true : false;
  4952. }
  4953. UPB_INLINE bool upb_sink_endsubmsg(upb_sink s, upb_selector_t sel) {
  4954. typedef upb_endfield_handlerfunc func;
  4955. func *endsubmsg;
  4956. const void *hd;
  4957. if (!s.handlers) return true;
  4958. endsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4959. if (!endsubmsg) return s.closure;
  4960. return endsubmsg(s.closure, hd);
  4961. }
  4962. #ifdef __cplusplus
  4963. } /* extern "C" */
  4964. /* A upb::Sink is an object that binds a upb::Handlers object to some runtime
  4965. * state. It represents an endpoint to which data can be sent.
  4966. *
  4967. * TODO(haberman): right now all of these functions take selectors. Should they
  4968. * take selectorbase instead?
  4969. *
  4970. * ie. instead of calling:
  4971. * sink->StartString(FOO_FIELD_START_STRING, ...)
  4972. * a selector base would let you say:
  4973. * sink->StartString(FOO_FIELD, ...)
  4974. *
  4975. * This would make call sites a little nicer and require emitting fewer selector
  4976. * definitions in .h files.
  4977. *
  4978. * But the current scheme has the benefit that you can retrieve a function
  4979. * pointer for any handler with handlers->GetHandler(selector), without having
  4980. * to have a separate GetHandler() function for each handler type. The JIT
  4981. * compiler uses this. To accommodate we'd have to expose a separate
  4982. * GetHandler() for every handler type.
  4983. *
  4984. * Also to ponder: selectors right now are independent of a specific Handlers
  4985. * instance. In other words, they allocate a number to every possible handler
  4986. * that *could* be registered, without knowing anything about what handlers
  4987. * *are* registered. That means that using selectors as table offsets prohibits
  4988. * us from compacting the handler table at Freeze() time. If the table is very
  4989. * sparse, this could be wasteful.
  4990. *
  4991. * Having another selector-like thing that is specific to a Handlers instance
  4992. * would allow this compacting, but then it would be impossible to write code
  4993. * ahead-of-time that can be bound to any Handlers instance at runtime. For
  4994. * example, a .proto file parser written as straight C will not know what
  4995. * Handlers it will be bound to, so when it calls sink->StartString() what
  4996. * selector will it pass? It needs a selector like we have today, that is
  4997. * independent of any particular upb::Handlers.
  4998. *
  4999. * Is there a way then to allow Handlers table compaction? */
  5000. class upb::Sink {
  5001. public:
  5002. /* Constructor with no initialization; must be Reset() before use. */
  5003. Sink() {}
  5004. Sink(const Sink&) = default;
  5005. Sink& operator=(const Sink&) = default;
  5006. Sink(const upb_sink& sink) : sink_(sink) {}
  5007. Sink &operator=(const upb_sink &sink) {
  5008. sink_ = sink;
  5009. return *this;
  5010. }
  5011. upb_sink sink() { return sink_; }
  5012. /* Constructs a new sink for the given frozen handlers and closure.
  5013. *
  5014. * TODO: once the Handlers know the expected closure type, verify that T
  5015. * matches it. */
  5016. template <class T> Sink(const upb_handlers* handlers, T* closure) {
  5017. Reset(handlers, closure);
  5018. }
  5019. upb_sink* ptr() { return &sink_; }
  5020. /* Resets the value of the sink. */
  5021. template <class T> void Reset(const upb_handlers* handlers, T* closure) {
  5022. upb_sink_reset(&sink_, handlers, closure);
  5023. }
  5024. /* Returns the top-level object that is bound to this sink.
  5025. *
  5026. * TODO: once the Handlers know the expected closure type, verify that T
  5027. * matches it. */
  5028. template <class T> T* GetObject() const {
  5029. return static_cast<T*>(sink_.closure);
  5030. }
  5031. /* Functions for pushing data into the sink.
  5032. *
  5033. * These return false if processing should stop (either due to error or just
  5034. * to suspend).
  5035. *
  5036. * These may not be called from within one of the same sink's handlers (in
  5037. * other words, handlers are not re-entrant). */
  5038. /* Should be called at the start and end of every message; both the top-level
  5039. * message and submessages. This means that submessages should use the
  5040. * following sequence:
  5041. * sink->StartSubMessage(startsubmsg_selector);
  5042. * sink->StartMessage();
  5043. * // ...
  5044. * sink->EndMessage(&status);
  5045. * sink->EndSubMessage(endsubmsg_selector); */
  5046. bool StartMessage() { return upb_sink_startmsg(sink_); }
  5047. bool EndMessage(upb_status *status) {
  5048. return upb_sink_endmsg(sink_, status);
  5049. }
  5050. /* Putting of individual values. These work for both repeated and
  5051. * non-repeated fields, but for repeated fields you must wrap them in
  5052. * calls to StartSequence()/EndSequence(). */
  5053. bool PutInt32(HandlersPtr::Selector s, int32_t val) {
  5054. return upb_sink_putint32(sink_, s, val);
  5055. }
  5056. bool PutInt64(HandlersPtr::Selector s, int64_t val) {
  5057. return upb_sink_putint64(sink_, s, val);
  5058. }
  5059. bool PutUInt32(HandlersPtr::Selector s, uint32_t val) {
  5060. return upb_sink_putuint32(sink_, s, val);
  5061. }
  5062. bool PutUInt64(HandlersPtr::Selector s, uint64_t val) {
  5063. return upb_sink_putuint64(sink_, s, val);
  5064. }
  5065. bool PutFloat(HandlersPtr::Selector s, float val) {
  5066. return upb_sink_putfloat(sink_, s, val);
  5067. }
  5068. bool PutDouble(HandlersPtr::Selector s, double val) {
  5069. return upb_sink_putdouble(sink_, s, val);
  5070. }
  5071. bool PutBool(HandlersPtr::Selector s, bool val) {
  5072. return upb_sink_putbool(sink_, s, val);
  5073. }
  5074. /* Putting of string/bytes values. Each string can consist of zero or more
  5075. * non-contiguous buffers of data.
  5076. *
  5077. * For StartString(), the function will write a sink for the string to "sub."
  5078. * The sub-sink must be used for any/all PutStringBuffer() calls. */
  5079. bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub) {
  5080. upb_sink sub_c;
  5081. bool ret = upb_sink_startstr(sink_, s, size_hint, &sub_c);
  5082. *sub = sub_c;
  5083. return ret;
  5084. }
  5085. size_t PutStringBuffer(HandlersPtr::Selector s, const char *buf, size_t len,
  5086. const upb_bufhandle *handle) {
  5087. return upb_sink_putstring(sink_, s, buf, len, handle);
  5088. }
  5089. bool EndString(HandlersPtr::Selector s) {
  5090. return upb_sink_endstr(sink_, s);
  5091. }
  5092. /* For submessage fields.
  5093. *
  5094. * For StartSubMessage(), the function will write a sink for the string to
  5095. * "sub." The sub-sink must be used for any/all handlers called within the
  5096. * submessage. */
  5097. bool StartSubMessage(HandlersPtr::Selector s, Sink* sub) {
  5098. upb_sink sub_c;
  5099. bool ret = upb_sink_startsubmsg(sink_, s, &sub_c);
  5100. *sub = sub_c;
  5101. return ret;
  5102. }
  5103. bool EndSubMessage(HandlersPtr::Selector s) {
  5104. return upb_sink_endsubmsg(sink_, s);
  5105. }
  5106. /* For repeated fields of any type, the sequence of values must be wrapped in
  5107. * these calls.
  5108. *
  5109. * For StartSequence(), the function will write a sink for the string to
  5110. * "sub." The sub-sink must be used for any/all handlers called within the
  5111. * sequence. */
  5112. bool StartSequence(HandlersPtr::Selector s, Sink* sub) {
  5113. upb_sink sub_c;
  5114. bool ret = upb_sink_startseq(sink_, s, &sub_c);
  5115. *sub = sub_c;
  5116. return ret;
  5117. }
  5118. bool EndSequence(HandlersPtr::Selector s) {
  5119. return upb_sink_endseq(sink_, s);
  5120. }
  5121. /* Copy and assign specifically allowed.
  5122. * We don't even bother making these members private because so many
  5123. * functions need them and this is mainly just a dumb data container anyway.
  5124. */
  5125. private:
  5126. upb_sink sink_;
  5127. };
  5128. #endif /* __cplusplus */
  5129. /* upb_bytessink **************************************************************/
  5130. typedef struct {
  5131. const upb_byteshandler *handler;
  5132. void *closure;
  5133. } upb_bytessink ;
  5134. UPB_INLINE void upb_bytessink_reset(upb_bytessink* s, const upb_byteshandler *h,
  5135. void *closure) {
  5136. s->handler = h;
  5137. s->closure = closure;
  5138. }
  5139. UPB_INLINE bool upb_bytessink_start(upb_bytessink s, size_t size_hint,
  5140. void **subc) {
  5141. typedef upb_startstr_handlerfunc func;
  5142. func *start;
  5143. *subc = s.closure;
  5144. if (!s.handler) return true;
  5145. start = (func *)s.handler->table[UPB_STARTSTR_SELECTOR].func;
  5146. if (!start) return true;
  5147. *subc = start(s.closure,
  5148. s.handler->table[UPB_STARTSTR_SELECTOR].attr.handler_data,
  5149. size_hint);
  5150. return *subc != NULL;
  5151. }
  5152. UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink s, void *subc,
  5153. const char *buf, size_t size,
  5154. const upb_bufhandle* handle) {
  5155. typedef upb_string_handlerfunc func;
  5156. func *putbuf;
  5157. if (!s.handler) return true;
  5158. putbuf = (func *)s.handler->table[UPB_STRING_SELECTOR].func;
  5159. if (!putbuf) return true;
  5160. return putbuf(subc, s.handler->table[UPB_STRING_SELECTOR].attr.handler_data,
  5161. buf, size, handle);
  5162. }
  5163. UPB_INLINE bool upb_bytessink_end(upb_bytessink s) {
  5164. typedef upb_endfield_handlerfunc func;
  5165. func *end;
  5166. if (!s.handler) return true;
  5167. end = (func *)s.handler->table[UPB_ENDSTR_SELECTOR].func;
  5168. if (!end) return true;
  5169. return end(s.closure,
  5170. s.handler->table[UPB_ENDSTR_SELECTOR].attr.handler_data);
  5171. }
  5172. #ifdef __cplusplus
  5173. class upb::BytesSink {
  5174. public:
  5175. BytesSink() {}
  5176. BytesSink(const BytesSink&) = default;
  5177. BytesSink& operator=(const BytesSink&) = default;
  5178. BytesSink(const upb_bytessink& sink) : sink_(sink) {}
  5179. BytesSink &operator=(const upb_bytessink &sink) {
  5180. sink_ = sink;
  5181. return *this;
  5182. }
  5183. upb_bytessink sink() { return sink_; }
  5184. /* Constructs a new sink for the given frozen handlers and closure.
  5185. *
  5186. * TODO(haberman): once the Handlers know the expected closure type, verify
  5187. * that T matches it. */
  5188. template <class T> BytesSink(const upb_byteshandler* handler, T* closure) {
  5189. upb_bytessink_reset(sink_, handler, closure);
  5190. }
  5191. /* Resets the value of the sink. */
  5192. template <class T> void Reset(const upb_byteshandler* handler, T* closure) {
  5193. upb_bytessink_reset(&sink_, handler, closure);
  5194. }
  5195. bool Start(size_t size_hint, void **subc) {
  5196. return upb_bytessink_start(sink_, size_hint, subc);
  5197. }
  5198. size_t PutBuffer(void *subc, const char *buf, size_t len,
  5199. const upb_bufhandle *handle) {
  5200. return upb_bytessink_putbuf(sink_, subc, buf, len, handle);
  5201. }
  5202. bool End() {
  5203. return upb_bytessink_end(sink_);
  5204. }
  5205. private:
  5206. upb_bytessink sink_;
  5207. };
  5208. #endif /* __cplusplus */
  5209. /* upb_bufsrc *****************************************************************/
  5210. #ifdef __cplusplus
  5211. extern "C" {
  5212. #endif
  5213. bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink);
  5214. #ifdef __cplusplus
  5215. } /* extern "C" */
  5216. namespace upb {
  5217. template <class T> bool PutBuffer(const T& str, BytesSink sink) {
  5218. return upb_bufsrc_putbuf(str.data(), str.size(), sink.sink());
  5219. }
  5220. }
  5221. #endif /* __cplusplus */
  5222. #endif
  5223. #ifndef UPB_MSGFACTORY_H_
  5224. #define UPB_MSGFACTORY_H_
  5225. /** upb_msgfactory ************************************************************/
  5226. struct upb_msgfactory;
  5227. typedef struct upb_msgfactory upb_msgfactory;
  5228. #ifdef __cplusplus
  5229. extern "C" {
  5230. #endif
  5231. /* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and
  5232. * upb_visitorplan objects. These are the objects necessary to represent,
  5233. * populate, and and visit upb_msg objects.
  5234. *
  5235. * These caches are all populated by upb_msgdef, and lazily created on demand.
  5236. */
  5237. /* Creates and destroys a msgfactory, respectively. The messages for this
  5238. * msgfactory must come from |symtab| (which should outlive the msgfactory). */
  5239. upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab);
  5240. void upb_msgfactory_free(upb_msgfactory *f);
  5241. const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f);
  5242. /* The functions to get cached objects, lazily creating them on demand. These
  5243. * all require:
  5244. *
  5245. * - m is in upb_msgfactory_symtab(f)
  5246. * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts).
  5247. *
  5248. * The returned objects will live for as long as the msgfactory does.
  5249. *
  5250. * TODO(haberman): consider making this thread-safe and take a const
  5251. * upb_msgfactory. */
  5252. const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f,
  5253. const upb_msgdef *m);
  5254. #ifdef __cplusplus
  5255. } /* extern "C" */
  5256. #endif
  5257. #endif /* UPB_MSGFACTORY_H_ */
  5258. /*
  5259. ** Internal-only definitions for the decoder.
  5260. */
  5261. #ifndef UPB_DECODER_INT_H_
  5262. #define UPB_DECODER_INT_H_
  5263. /*
  5264. ** upb::pb::Decoder
  5265. **
  5266. ** A high performance, streaming, resumable decoder for the binary protobuf
  5267. ** format.
  5268. **
  5269. ** This interface works the same regardless of what decoder backend is being
  5270. ** used. A client of this class does not need to know whether decoding is using
  5271. ** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
  5272. ** it will always use the fastest available decoder. However, you can call
  5273. ** set_allow_jit(false) to disable any JIT decoder that might be available.
  5274. ** This is primarily useful for testing purposes.
  5275. */
  5276. #ifndef UPB_DECODER_H_
  5277. #define UPB_DECODER_H_
  5278. #ifdef __cplusplus
  5279. namespace upb {
  5280. namespace pb {
  5281. class CodeCache;
  5282. class DecoderPtr;
  5283. class DecoderMethodPtr;
  5284. class DecoderMethodOptions;
  5285. } /* namespace pb */
  5286. } /* namespace upb */
  5287. #endif
  5288. /* The maximum number of bytes we are required to buffer internally between
  5289. * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
  5290. * varint, less one because we are buffering an incomplete value.
  5291. *
  5292. * Should only be used by unit tests. */
  5293. #define UPB_DECODER_MAX_RESIDUAL_BYTES 14
  5294. /* upb_pbdecodermethod ********************************************************/
  5295. struct upb_pbdecodermethod;
  5296. typedef struct upb_pbdecodermethod upb_pbdecodermethod;
  5297. #ifdef __cplusplus
  5298. extern "C" {
  5299. #endif
  5300. const upb_handlers *upb_pbdecodermethod_desthandlers(
  5301. const upb_pbdecodermethod *m);
  5302. const upb_byteshandler *upb_pbdecodermethod_inputhandler(
  5303. const upb_pbdecodermethod *m);
  5304. bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
  5305. #ifdef __cplusplus
  5306. } /* extern "C" */
  5307. /* Represents the code to parse a protobuf according to a destination
  5308. * Handlers. */
  5309. class upb::pb::DecoderMethodPtr {
  5310. public:
  5311. DecoderMethodPtr() : ptr_(nullptr) {}
  5312. DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {}
  5313. const upb_pbdecodermethod* ptr() { return ptr_; }
  5314. /* The destination handlers that are statically bound to this method.
  5315. * This method is only capable of outputting to a sink that uses these
  5316. * handlers. */
  5317. const Handlers *dest_handlers() const {
  5318. return upb_pbdecodermethod_desthandlers(ptr_);
  5319. }
  5320. /* The input handlers for this decoder method. */
  5321. const BytesHandler* input_handler() const {
  5322. return upb_pbdecodermethod_inputhandler(ptr_);
  5323. }
  5324. /* Whether this method is native. */
  5325. bool is_native() const {
  5326. return upb_pbdecodermethod_isnative(ptr_);
  5327. }
  5328. private:
  5329. const upb_pbdecodermethod* ptr_;
  5330. };
  5331. #endif
  5332. /* upb_pbdecoder **************************************************************/
  5333. /* Preallocation hint: decoder won't allocate more bytes than this when first
  5334. * constructed. This hint may be an overestimate for some build configurations.
  5335. * But if the decoder library is upgraded without recompiling the application,
  5336. * it may be an underestimate. */
  5337. #define UPB_PB_DECODER_SIZE 4416
  5338. struct upb_pbdecoder;
  5339. typedef struct upb_pbdecoder upb_pbdecoder;
  5340. #ifdef __cplusplus
  5341. extern "C" {
  5342. #endif
  5343. upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena,
  5344. const upb_pbdecodermethod *method,
  5345. upb_sink output, upb_status *status);
  5346. const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
  5347. upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d);
  5348. uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
  5349. size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
  5350. bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
  5351. void upb_pbdecoder_reset(upb_pbdecoder *d);
  5352. #ifdef __cplusplus
  5353. } /* extern "C" */
  5354. /* A Decoder receives binary protobuf data on its input sink and pushes the
  5355. * decoded data to its output sink. */
  5356. class upb::pb::DecoderPtr {
  5357. public:
  5358. DecoderPtr() : ptr_(nullptr) {}
  5359. DecoderPtr(upb_pbdecoder* ptr) : ptr_(ptr) {}
  5360. upb_pbdecoder* ptr() { return ptr_; }
  5361. /* Constructs a decoder instance for the given method, which must outlive this
  5362. * decoder. Any errors during parsing will be set on the given status, which
  5363. * must also outlive this decoder.
  5364. *
  5365. * The sink must match the given method. */
  5366. static DecoderPtr Create(Arena *arena, DecoderMethodPtr method,
  5367. upb::Sink output, Status *status) {
  5368. return DecoderPtr(upb_pbdecoder_create(arena->ptr(), method.ptr(),
  5369. output.sink(), status->ptr()));
  5370. }
  5371. /* Returns the DecoderMethod this decoder is parsing from. */
  5372. const DecoderMethodPtr method() const {
  5373. return DecoderMethodPtr(upb_pbdecoder_method(ptr_));
  5374. }
  5375. /* The sink on which this decoder receives input. */
  5376. BytesSink input() { return BytesSink(upb_pbdecoder_input(ptr())); }
  5377. /* Returns number of bytes successfully parsed.
  5378. *
  5379. * This can be useful for determining the stream position where an error
  5380. * occurred.
  5381. *
  5382. * This value may not be up-to-date when called from inside a parsing
  5383. * callback. */
  5384. uint64_t BytesParsed() { return upb_pbdecoder_bytesparsed(ptr()); }
  5385. /* Gets/sets the parsing nexting limit. If the total number of nested
  5386. * submessages and repeated fields hits this limit, parsing will fail. This
  5387. * is a resource limit that controls the amount of memory used by the parsing
  5388. * stack.
  5389. *
  5390. * Setting the limit will fail if the parser is currently suspended at a depth
  5391. * greater than this, or if memory allocation of the stack fails. */
  5392. size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); }
  5393. bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); }
  5394. void Reset() { upb_pbdecoder_reset(ptr()); }
  5395. static const size_t kSize = UPB_PB_DECODER_SIZE;
  5396. private:
  5397. upb_pbdecoder *ptr_;
  5398. };
  5399. #endif /* __cplusplus */
  5400. /* upb_pbcodecache ************************************************************/
  5401. /* Lazily builds and caches decoder methods that will push data to the given
  5402. * handlers. The destination handlercache must outlive this object. */
  5403. struct upb_pbcodecache;
  5404. typedef struct upb_pbcodecache upb_pbcodecache;
  5405. #ifdef __cplusplus
  5406. extern "C" {
  5407. #endif
  5408. upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest);
  5409. void upb_pbcodecache_free(upb_pbcodecache *c);
  5410. bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
  5411. void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
  5412. void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy);
  5413. const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c,
  5414. const upb_msgdef *md);
  5415. #ifdef __cplusplus
  5416. } /* extern "C" */
  5417. /* A class for caching protobuf processing code, whether bytecode for the
  5418. * interpreted decoder or machine code for the JIT.
  5419. *
  5420. * This class is not thread-safe. */
  5421. class upb::pb::CodeCache {
  5422. public:
  5423. CodeCache(upb::HandlerCache *dest)
  5424. : ptr_(upb_pbcodecache_new(dest->ptr()), upb_pbcodecache_free) {}
  5425. CodeCache(CodeCache&&) = default;
  5426. CodeCache& operator=(CodeCache&&) = default;
  5427. upb_pbcodecache* ptr() { return ptr_.get(); }
  5428. const upb_pbcodecache* ptr() const { return ptr_.get(); }
  5429. /* Whether the cache is allowed to generate machine code. Defaults to true.
  5430. * There is no real reason to turn it off except for testing or if you are
  5431. * having a specific problem with the JIT.
  5432. *
  5433. * Note that allow_jit = true does not *guarantee* that the code will be JIT
  5434. * compiled. If this platform is not supported or the JIT was not compiled
  5435. * in, the code may still be interpreted. */
  5436. bool allow_jit() const { return upb_pbcodecache_allowjit(ptr()); }
  5437. /* This may only be called when the object is first constructed, and prior to
  5438. * any code generation. */
  5439. void set_allow_jit(bool allow) { upb_pbcodecache_setallowjit(ptr(), allow); }
  5440. /* Should the decoder push submessages to lazy handlers for fields that have
  5441. * them? The caller should set this iff the lazy handlers expect data that is
  5442. * in protobuf binary format and the caller wishes to lazy parse it. */
  5443. void set_lazy(bool lazy) { upb_pbcodecache_setlazy(ptr(), lazy); }
  5444. /* Returns a DecoderMethod that can push data to the given handlers.
  5445. * If a suitable method already exists, it will be returned from the cache. */
  5446. const DecoderMethodPtr Get(MessageDefPtr md) {
  5447. return DecoderMethodPtr(upb_pbcodecache_get(ptr(), md.ptr()));
  5448. }
  5449. private:
  5450. std::unique_ptr<upb_pbcodecache, decltype(&upb_pbcodecache_free)> ptr_;
  5451. };
  5452. #endif /* __cplusplus */
  5453. #endif /* UPB_DECODER_H_ */
  5454. /* Opcode definitions. The canonical meaning of each opcode is its
  5455. * implementation in the interpreter (the JIT is written to match this).
  5456. *
  5457. * All instructions have the opcode in the low byte.
  5458. * Instruction format for most instructions is:
  5459. *
  5460. * +-------------------+--------+
  5461. * | arg (24) | op (8) |
  5462. * +-------------------+--------+
  5463. *
  5464. * Exceptions are indicated below. A few opcodes are multi-word. */
  5465. typedef enum {
  5466. /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
  5467. * Arg for all of these is the upb selector for this field. */
  5468. #define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
  5469. T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
  5470. T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
  5471. #undef T
  5472. OP_STARTMSG = 9, /* No arg. */
  5473. OP_ENDMSG = 10, /* No arg. */
  5474. OP_STARTSEQ = 11,
  5475. OP_ENDSEQ = 12,
  5476. OP_STARTSUBMSG = 14,
  5477. OP_ENDSUBMSG = 19,
  5478. OP_STARTSTR = 20,
  5479. OP_STRING = 21,
  5480. OP_ENDSTR = 22,
  5481. OP_PUSHTAGDELIM = 23, /* No arg. */
  5482. OP_PUSHLENDELIM = 24, /* No arg. */
  5483. OP_POP = 25, /* No arg. */
  5484. OP_SETDELIM = 26, /* No arg. */
  5485. OP_SETBIGGROUPNUM = 27, /* two words:
  5486. * | unused (24) | opc (8) |
  5487. * | groupnum (32) | */
  5488. OP_CHECKDELIM = 28,
  5489. OP_CALL = 29,
  5490. OP_RET = 30,
  5491. OP_BRANCH = 31,
  5492. /* Different opcodes depending on how many bytes expected. */
  5493. OP_TAG1 = 32, /* | match tag (16) | jump target (8) | opc (8) | */
  5494. OP_TAG2 = 33, /* | match tag (16) | jump target (8) | opc (8) | */
  5495. OP_TAGN = 34, /* three words: */
  5496. /* | unused (16) | jump target(8) | opc (8) | */
  5497. /* | match tag 1 (32) | */
  5498. /* | match tag 2 (32) | */
  5499. OP_SETDISPATCH = 35, /* N words: */
  5500. /* | unused (24) | opc | */
  5501. /* | upb_inttable* (32 or 64) | */
  5502. OP_DISPATCH = 36, /* No arg. */
  5503. OP_HALT = 37 /* No arg. */
  5504. } opcode;
  5505. #define OP_MAX OP_HALT
  5506. UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); }
  5507. struct upb_pbcodecache {
  5508. upb_arena *arena;
  5509. upb_handlercache *dest;
  5510. bool allow_jit;
  5511. bool lazy;
  5512. /* Array of mgroups. */
  5513. upb_inttable groups;
  5514. };
  5515. /* Method group; represents a set of decoder methods that had their code
  5516. * emitted together. Immutable once created. */
  5517. typedef struct {
  5518. /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. Owned by us.
  5519. *
  5520. * Ideally this would be on pbcodecache (if we were actually caching code).
  5521. * Right now we don't actually cache anything, which is wasteful. */
  5522. upb_inttable methods;
  5523. /* The bytecode for our methods, if any exists. Owned by us. */
  5524. uint32_t *bytecode;
  5525. uint32_t *bytecode_end;
  5526. #ifdef UPB_USE_JIT_X64
  5527. /* JIT-generated machine code, if any. */
  5528. upb_string_handlerfunc *jit_code;
  5529. /* The size of the jit_code (required to munmap()). */
  5530. size_t jit_size;
  5531. char *debug_info;
  5532. void *dl;
  5533. #endif
  5534. } mgroup;
  5535. /* The maximum that any submessages can be nested. Matches proto2's limit.
  5536. * This specifies the size of the decoder's statically-sized array and therefore
  5537. * setting it high will cause the upb::pb::Decoder object to be larger.
  5538. *
  5539. * If necessary we can add a runtime-settable property to Decoder that allow
  5540. * this to be larger than the compile-time setting, but this would add
  5541. * complexity, particularly since we would have to decide how/if to give users
  5542. * the ability to set a custom memory allocation function. */
  5543. #define UPB_DECODER_MAX_NESTING 64
  5544. /* Internal-only struct used by the decoder. */
  5545. typedef struct {
  5546. /* Space optimization note: we store two pointers here that the JIT
  5547. * doesn't need at all; the upb_handlers* inside the sink and
  5548. * the dispatch table pointer. We can optimze so that the JIT uses
  5549. * smaller stack frames than the interpreter. The only thing we need
  5550. * to guarantee is that the fallback routines can find end_ofs. */
  5551. upb_sink sink;
  5552. /* The absolute stream offset of the end-of-frame delimiter.
  5553. * Non-delimited frames (groups and non-packed repeated fields) reuse the
  5554. * delimiter of their parent, even though the frame may not end there.
  5555. *
  5556. * NOTE: the JIT stores a slightly different value here for non-top frames.
  5557. * It stores the value relative to the end of the enclosed message. But the
  5558. * top frame is still stored the same way, which is important for ensuring
  5559. * that calls from the JIT into C work correctly. */
  5560. uint64_t end_ofs;
  5561. const uint32_t *base;
  5562. /* 0 indicates a length-delimited field.
  5563. * A positive number indicates a known group.
  5564. * A negative number indicates an unknown group. */
  5565. int32_t groupnum;
  5566. upb_inttable *dispatch; /* Not used by the JIT. */
  5567. } upb_pbdecoder_frame;
  5568. struct upb_pbdecodermethod {
  5569. /* While compiling, the base is relative in "ofs", after compiling it is
  5570. * absolute in "ptr". */
  5571. union {
  5572. uint32_t ofs; /* PC offset of method. */
  5573. void *ptr; /* Pointer to bytecode or machine code for this method. */
  5574. } code_base;
  5575. /* The decoder method group to which this method belongs. */
  5576. const mgroup *group;
  5577. /* Whether this method is native code or bytecode. */
  5578. bool is_native_;
  5579. /* The handler one calls to invoke this method. */
  5580. upb_byteshandler input_handler_;
  5581. /* The destination handlers this method is bound to. We own a ref. */
  5582. const upb_handlers *dest_handlers_;
  5583. /* Dispatch table -- used by both bytecode decoder and JIT when encountering a
  5584. * field number that wasn't the one we were expecting to see. See
  5585. * decoder.int.h for the layout of this table. */
  5586. upb_inttable dispatch;
  5587. };
  5588. struct upb_pbdecoder {
  5589. upb_arena *arena;
  5590. /* Our input sink. */
  5591. upb_bytessink input_;
  5592. /* The decoder method we are parsing with (owned). */
  5593. const upb_pbdecodermethod *method_;
  5594. size_t call_len;
  5595. const uint32_t *pc, *last;
  5596. /* Current input buffer and its stream offset. */
  5597. const char *buf, *ptr, *end, *checkpoint;
  5598. /* End of the delimited region, relative to ptr, NULL if not in this buf. */
  5599. const char *delim_end;
  5600. /* End of the delimited region, relative to ptr, end if not in this buf. */
  5601. const char *data_end;
  5602. /* Overall stream offset of "buf." */
  5603. uint64_t bufstart_ofs;
  5604. /* Buffer for residual bytes not parsed from the previous buffer. */
  5605. char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
  5606. char *residual_end;
  5607. /* Bytes of data that should be discarded from the input beore we start
  5608. * parsing again. We set this when we internally determine that we can
  5609. * safely skip the next N bytes, but this region extends past the current
  5610. * user buffer. */
  5611. size_t skip;
  5612. /* Stores the user buffer passed to our decode function. */
  5613. const char *buf_param;
  5614. size_t size_param;
  5615. const upb_bufhandle *handle;
  5616. /* Our internal stack. */
  5617. upb_pbdecoder_frame *stack, *top, *limit;
  5618. const uint32_t **callstack;
  5619. size_t stack_size;
  5620. upb_status *status;
  5621. #ifdef UPB_USE_JIT_X64
  5622. /* Used momentarily by the generated code to store a value while a user
  5623. * function is called. */
  5624. uint32_t tmp_len;
  5625. const void *saved_rsp;
  5626. #endif
  5627. };
  5628. /* Decoder entry points; used as handlers. */
  5629. void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
  5630. void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
  5631. size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
  5632. size_t size, const upb_bufhandle *handle);
  5633. bool upb_pbdecoder_end(void *closure, const void *handler_data);
  5634. /* Decoder-internal functions that the JIT calls to handle fallback paths. */
  5635. int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
  5636. size_t size, const upb_bufhandle *handle);
  5637. size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
  5638. int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
  5639. uint8_t wire_type);
  5640. int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
  5641. int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
  5642. int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
  5643. int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
  5644. void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
  5645. /* Error messages that are shared between the bytecode and JIT decoders. */
  5646. extern const char *kPbDecoderStackOverflow;
  5647. extern const char *kPbDecoderSubmessageTooLong;
  5648. /* Access to decoderplan members needed by the decoder. */
  5649. const char *upb_pbdecoder_getopname(unsigned int op);
  5650. /* JIT codegen entry point. */
  5651. void upb_pbdecoder_jit(mgroup *group);
  5652. void upb_pbdecoder_freejit(mgroup *group);
  5653. /* A special label that means "do field dispatch for this message and branch to
  5654. * wherever that takes you." */
  5655. #define LABEL_DISPATCH 0
  5656. /* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
  5657. * RET) for branching to when we find an appropriate ENDGROUP tag. */
  5658. #define DISPATCH_ENDMSG 0
  5659. /* It's important to use this invalid wire type instead of 0 (which is a valid
  5660. * wire type). */
  5661. #define NO_WIRE_TYPE 0xff
  5662. /* The dispatch table layout is:
  5663. * [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
  5664. *
  5665. * If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
  5666. * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
  5667. *
  5668. * We need two wire types because of packed/non-packed compatibility. A
  5669. * primitive repeated field can use either wire type and be valid. While we
  5670. * could key the table on fieldnum+wiretype, the table would be 8x sparser.
  5671. *
  5672. * Storing two wire types in the primary value allows us to quickly rule out
  5673. * the second wire type without needing to do a separate lookup (this case is
  5674. * less common than an unknown field). */
  5675. UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
  5676. uint8_t wt2) {
  5677. return (ofs << 16) | (wt2 << 8) | wt1;
  5678. }
  5679. UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
  5680. uint8_t *wt1, uint8_t *wt2) {
  5681. *wt1 = (uint8_t)dispatch;
  5682. *wt2 = (uint8_t)(dispatch >> 8);
  5683. *ofs = dispatch >> 16;
  5684. }
  5685. /* All of the functions in decoder.c that return int32_t return values according
  5686. * to the following scheme:
  5687. * 1. negative values indicate a return code from the following list.
  5688. * 2. positive values indicate that error or end of buffer was hit, and
  5689. * that the decode function should immediately return the given value
  5690. * (the decoder state has already been suspended and is ready to be
  5691. * resumed). */
  5692. #define DECODE_OK -1
  5693. #define DECODE_MISMATCH -2 /* Used only from checktag_slow(). */
  5694. #define DECODE_ENDGROUP -3 /* Used only from checkunknown(). */
  5695. #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
  5696. #endif /* UPB_DECODER_INT_H_ */
  5697. /*
  5698. ** A number of routines for varint manipulation (we keep them all around to
  5699. ** have multiple approaches available for benchmarking).
  5700. */
  5701. #ifndef UPB_VARINT_DECODER_H_
  5702. #define UPB_VARINT_DECODER_H_
  5703. #include <assert.h>
  5704. #include <stdint.h>
  5705. #include <string.h>
  5706. #ifdef __cplusplus
  5707. extern "C" {
  5708. #endif
  5709. #define UPB_MAX_WIRE_TYPE 5
  5710. /* The maximum number of bytes that it takes to encode a 64-bit varint. */
  5711. #define UPB_PB_VARINT_MAX_LEN 10
  5712. /* Array of the "native" (ie. non-packed-repeated) wire type for the given a
  5713. * descriptor type (upb_descriptortype_t). */
  5714. extern const uint8_t upb_pb_native_wire_types[];
  5715. UPB_INLINE uint64_t byteswap64(uint64_t val)
  5716. {
  5717. return ((((val) & 0xff00000000000000ull) >> 56)
  5718. | (((val) & 0x00ff000000000000ull) >> 40)
  5719. | (((val) & 0x0000ff0000000000ull) >> 24)
  5720. | (((val) & 0x000000ff00000000ull) >> 8)
  5721. | (((val) & 0x00000000ff000000ull) << 8)
  5722. | (((val) & 0x0000000000ff0000ull) << 24)
  5723. | (((val) & 0x000000000000ff00ull) << 40)
  5724. | (((val) & 0x00000000000000ffull) << 56));
  5725. }
  5726. /* Zig-zag encoding/decoding **************************************************/
  5727. UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
  5728. return (n >> 1) ^ -(int32_t)(n & 1);
  5729. }
  5730. UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
  5731. return (n >> 1) ^ -(int64_t)(n & 1);
  5732. }
  5733. UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
  5734. UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
  5735. /* Decoding *******************************************************************/
  5736. /* All decoding functions return this struct by value. */
  5737. typedef struct {
  5738. const char *p; /* NULL if the varint was unterminated. */
  5739. uint64_t val;
  5740. } upb_decoderet;
  5741. UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
  5742. upb_decoderet ret;
  5743. ret.p = p;
  5744. ret.val = val;
  5745. return ret;
  5746. }
  5747. upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
  5748. upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
  5749. /* Template for a function that checks the first two bytes with branching
  5750. * and dispatches 2-10 bytes with a separate function. Note that this may read
  5751. * up to 10 bytes, so it must not be used unless there are at least ten bytes
  5752. * left in the buffer! */
  5753. #define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
  5754. UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
  5755. uint8_t *p = (uint8_t*)_p; \
  5756. upb_decoderet r; \
  5757. if ((*p & 0x80) == 0) { \
  5758. /* Common case: one-byte varint. */ \
  5759. return upb_decoderet_make(_p + 1, *p & 0x7fU); \
  5760. } \
  5761. r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)); \
  5762. if ((*(p + 1) & 0x80) == 0) { \
  5763. /* Two-byte varint. */ \
  5764. return r; \
  5765. } \
  5766. /* Longer varint, fallback to out-of-line function. */ \
  5767. return decode_max8_function(r); \
  5768. }
  5769. UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
  5770. UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
  5771. #undef UPB_VARINT_DECODER_CHECK2
  5772. /* Our canonical functions for decoding varints, based on the currently
  5773. * favored best-performing implementations. */
  5774. UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
  5775. if (sizeof(long) == 8)
  5776. return upb_vdecode_check2_branch64(p);
  5777. else
  5778. return upb_vdecode_check2_branch32(p);
  5779. }
  5780. /* Encoding *******************************************************************/
  5781. UPB_INLINE int upb_value_size(uint64_t val) {
  5782. #ifdef __GNUC__
  5783. int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */
  5784. #else
  5785. int high_bit = 0;
  5786. uint64_t tmp = val;
  5787. while(tmp >>= 1) high_bit++;
  5788. #endif
  5789. return val == 0 ? 1 : high_bit / 8 + 1;
  5790. }
  5791. /* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
  5792. * bytes long), returning how many bytes were used.
  5793. *
  5794. * TODO: benchmark and optimize if necessary. */
  5795. UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
  5796. size_t i;
  5797. if (val == 0) { buf[0] = 0; return 1; }
  5798. i = 0;
  5799. while (val) {
  5800. uint8_t byte = val & 0x7fU;
  5801. val >>= 7;
  5802. if (val) byte |= 0x80U;
  5803. buf[i++] = byte;
  5804. }
  5805. return i;
  5806. }
  5807. UPB_INLINE size_t upb_varint_size(uint64_t val) {
  5808. char buf[UPB_PB_VARINT_MAX_LEN];
  5809. return upb_vencode64(val, buf);
  5810. }
  5811. /* Encodes a 32-bit varint, *not* sign-extended. */
  5812. UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
  5813. char buf[UPB_PB_VARINT_MAX_LEN];
  5814. size_t bytes = upb_vencode64(val, buf);
  5815. uint64_t ret = 0;
  5816. UPB_ASSERT(bytes <= 5);
  5817. memcpy(&ret, buf, bytes);
  5818. #ifdef UPB_BIG_ENDIAN
  5819. ret = byteswap64(ret);
  5820. #endif
  5821. UPB_ASSERT(ret <= 0xffffffffffU);
  5822. return ret;
  5823. }
  5824. #ifdef __cplusplus
  5825. } /* extern "C" */
  5826. #endif
  5827. #endif /* UPB_VARINT_DECODER_H_ */
  5828. /*
  5829. ** upb::pb::Encoder (upb_pb_encoder)
  5830. **
  5831. ** Implements a set of upb_handlers that write protobuf data to the binary wire
  5832. ** format.
  5833. **
  5834. ** This encoder implementation does not have any access to any out-of-band or
  5835. ** precomputed lengths for submessages, so it must buffer submessages internally
  5836. ** before it can emit the first byte.
  5837. */
  5838. #ifndef UPB_ENCODER_H_
  5839. #define UPB_ENCODER_H_
  5840. #ifdef __cplusplus
  5841. namespace upb {
  5842. namespace pb {
  5843. class EncoderPtr;
  5844. } /* namespace pb */
  5845. } /* namespace upb */
  5846. #endif
  5847. #define UPB_PBENCODER_MAX_NESTING 100
  5848. /* upb_pb_encoder *************************************************************/
  5849. /* Preallocation hint: decoder won't allocate more bytes than this when first
  5850. * constructed. This hint may be an overestimate for some build configurations.
  5851. * But if the decoder library is upgraded without recompiling the application,
  5852. * it may be an underestimate. */
  5853. #define UPB_PB_ENCODER_SIZE 784
  5854. struct upb_pb_encoder;
  5855. typedef struct upb_pb_encoder upb_pb_encoder;
  5856. #ifdef __cplusplus
  5857. extern "C" {
  5858. #endif
  5859. upb_sink upb_pb_encoder_input(upb_pb_encoder *p);
  5860. upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h,
  5861. upb_bytessink output);
  5862. /* Lazily builds and caches handlers that will push encoded data to a bytessink.
  5863. * Any msgdef objects used with this object must outlive it. */
  5864. upb_handlercache *upb_pb_encoder_newcache();
  5865. #ifdef __cplusplus
  5866. } /* extern "C" { */
  5867. class upb::pb::EncoderPtr {
  5868. public:
  5869. EncoderPtr(upb_pb_encoder* ptr) : ptr_(ptr) {}
  5870. upb_pb_encoder* ptr() { return ptr_; }
  5871. /* Creates a new encoder in the given environment. The Handlers must have
  5872. * come from NewHandlers() below. */
  5873. static EncoderPtr Create(Arena* arena, const Handlers* handlers,
  5874. BytesSink output) {
  5875. return EncoderPtr(
  5876. upb_pb_encoder_create(arena->ptr(), handlers, output.sink()));
  5877. }
  5878. /* The input to the encoder. */
  5879. upb::Sink input() { return upb_pb_encoder_input(ptr()); }
  5880. /* Creates a new set of handlers for this MessageDef. */
  5881. static HandlerCache NewCache() {
  5882. return HandlerCache(upb_pb_encoder_newcache());
  5883. }
  5884. static const size_t kSize = UPB_PB_ENCODER_SIZE;
  5885. private:
  5886. upb_pb_encoder* ptr_;
  5887. };
  5888. #endif /* __cplusplus */
  5889. #endif /* UPB_ENCODER_H_ */
  5890. /*
  5891. ** upb::pb::TextPrinter (upb_textprinter)
  5892. **
  5893. ** Handlers for writing to protobuf text format.
  5894. */
  5895. #ifndef UPB_TEXT_H_
  5896. #define UPB_TEXT_H_
  5897. #ifdef __cplusplus
  5898. namespace upb {
  5899. namespace pb {
  5900. class TextPrinterPtr;
  5901. } /* namespace pb */
  5902. } /* namespace upb */
  5903. #endif
  5904. /* upb_textprinter ************************************************************/
  5905. struct upb_textprinter;
  5906. typedef struct upb_textprinter upb_textprinter;
  5907. #ifdef __cplusplus
  5908. extern "C" {
  5909. #endif
  5910. /* C API. */
  5911. upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h,
  5912. upb_bytessink output);
  5913. void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
  5914. upb_sink upb_textprinter_input(upb_textprinter *p);
  5915. upb_handlercache *upb_textprinter_newcache();
  5916. #ifdef __cplusplus
  5917. } /* extern "C" */
  5918. class upb::pb::TextPrinterPtr {
  5919. public:
  5920. TextPrinterPtr(upb_textprinter* ptr) : ptr_(ptr) {}
  5921. /* The given handlers must have come from NewHandlers(). It must outlive the
  5922. * TextPrinter. */
  5923. static TextPrinterPtr Create(Arena *arena, upb::HandlersPtr *handlers,
  5924. BytesSink output) {
  5925. return TextPrinterPtr(
  5926. upb_textprinter_create(arena->ptr(), handlers->ptr(), output.sink()));
  5927. }
  5928. void SetSingleLineMode(bool single_line) {
  5929. upb_textprinter_setsingleline(ptr_, single_line);
  5930. }
  5931. Sink input() { return upb_textprinter_input(ptr_); }
  5932. /* If handler caching becomes a requirement we can add a code cache as in
  5933. * decoder.h */
  5934. static HandlerCache NewCache() {
  5935. return HandlerCache(upb_textprinter_newcache());
  5936. }
  5937. private:
  5938. upb_textprinter* ptr_;
  5939. };
  5940. #endif
  5941. #endif /* UPB_TEXT_H_ */
  5942. /*
  5943. ** upb::json::Parser (upb_json_parser)
  5944. **
  5945. ** Parses JSON according to a specific schema.
  5946. ** Support for parsing arbitrary JSON (schema-less) will be added later.
  5947. */
  5948. #ifndef UPB_JSON_PARSER_H_
  5949. #define UPB_JSON_PARSER_H_
  5950. #ifdef __cplusplus
  5951. namespace upb {
  5952. namespace json {
  5953. class CodeCache;
  5954. class ParserPtr;
  5955. class ParserMethodPtr;
  5956. } /* namespace json */
  5957. } /* namespace upb */
  5958. #endif
  5959. /* upb_json_parsermethod ******************************************************/
  5960. struct upb_json_parsermethod;
  5961. typedef struct upb_json_parsermethod upb_json_parsermethod;
  5962. #ifdef __cplusplus
  5963. extern "C" {
  5964. #endif
  5965. const upb_byteshandler* upb_json_parsermethod_inputhandler(
  5966. const upb_json_parsermethod* m);
  5967. #ifdef __cplusplus
  5968. } /* extern "C" */
  5969. class upb::json::ParserMethodPtr {
  5970. public:
  5971. ParserMethodPtr() : ptr_(nullptr) {}
  5972. ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {}
  5973. const upb_json_parsermethod* ptr() const { return ptr_; }
  5974. const BytesHandler* input_handler() const {
  5975. return upb_json_parsermethod_inputhandler(ptr());
  5976. }
  5977. private:
  5978. const upb_json_parsermethod* ptr_;
  5979. };
  5980. #endif /* __cplusplus */
  5981. /* upb_json_parser ************************************************************/
  5982. /* Preallocation hint: parser won't allocate more bytes than this when first
  5983. * constructed. This hint may be an overestimate for some build configurations.
  5984. * But if the parser library is upgraded without recompiling the application,
  5985. * it may be an underestimate. */
  5986. #define UPB_JSON_PARSER_SIZE 5712
  5987. struct upb_json_parser;
  5988. typedef struct upb_json_parser upb_json_parser;
  5989. #ifdef __cplusplus
  5990. extern "C" {
  5991. #endif
  5992. upb_json_parser* upb_json_parser_create(upb_arena* a,
  5993. const upb_json_parsermethod* m,
  5994. const upb_symtab* symtab,
  5995. upb_sink output,
  5996. upb_status *status,
  5997. bool ignore_json_unknown);
  5998. upb_bytessink upb_json_parser_input(upb_json_parser* p);
  5999. #ifdef __cplusplus
  6000. } /* extern "C" */
  6001. /* Parses an incoming BytesStream, pushing the results to the destination
  6002. * sink. */
  6003. class upb::json::ParserPtr {
  6004. public:
  6005. ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {}
  6006. static ParserPtr Create(Arena* arena, ParserMethodPtr method,
  6007. SymbolTable* symtab, Sink output, Status* status,
  6008. bool ignore_json_unknown) {
  6009. upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr;
  6010. return ParserPtr(upb_json_parser_create(
  6011. arena->ptr(), method.ptr(), symtab_ptr, output.sink(), status->ptr(),
  6012. ignore_json_unknown));
  6013. }
  6014. BytesSink input() { return upb_json_parser_input(ptr_); }
  6015. private:
  6016. upb_json_parser* ptr_;
  6017. };
  6018. #endif /* __cplusplus */
  6019. /* upb_json_codecache *********************************************************/
  6020. /* Lazily builds and caches decoder methods that will push data to the given
  6021. * handlers. The upb_symtab object(s) must outlive this object. */
  6022. struct upb_json_codecache;
  6023. typedef struct upb_json_codecache upb_json_codecache;
  6024. #ifdef __cplusplus
  6025. extern "C" {
  6026. #endif
  6027. upb_json_codecache *upb_json_codecache_new();
  6028. void upb_json_codecache_free(upb_json_codecache *cache);
  6029. const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
  6030. const upb_msgdef* md);
  6031. #ifdef __cplusplus
  6032. } /* extern "C" */
  6033. class upb::json::CodeCache {
  6034. public:
  6035. CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {}
  6036. /* Returns a DecoderMethod that can push data to the given handlers.
  6037. * If a suitable method already exists, it will be returned from the cache. */
  6038. ParserMethodPtr Get(MessageDefPtr md) {
  6039. return upb_json_codecache_get(ptr_.get(), md.ptr());
  6040. }
  6041. private:
  6042. std::unique_ptr<upb_json_codecache, decltype(&upb_json_codecache_free)> ptr_;
  6043. };
  6044. #endif
  6045. #endif /* UPB_JSON_PARSER_H_ */
  6046. /*
  6047. ** upb::json::Printer
  6048. **
  6049. ** Handlers that emit JSON according to a specific protobuf schema.
  6050. */
  6051. #ifndef UPB_JSON_TYPED_PRINTER_H_
  6052. #define UPB_JSON_TYPED_PRINTER_H_
  6053. #ifdef __cplusplus
  6054. namespace upb {
  6055. namespace json {
  6056. class PrinterPtr;
  6057. } /* namespace json */
  6058. } /* namespace upb */
  6059. #endif
  6060. /* upb_json_printer ***********************************************************/
  6061. #define UPB_JSON_PRINTER_SIZE 192
  6062. struct upb_json_printer;
  6063. typedef struct upb_json_printer upb_json_printer;
  6064. #ifdef __cplusplus
  6065. extern "C" {
  6066. #endif
  6067. /* Native C API. */
  6068. upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h,
  6069. upb_bytessink output);
  6070. upb_sink upb_json_printer_input(upb_json_printer *p);
  6071. const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
  6072. bool preserve_fieldnames,
  6073. const void *owner);
  6074. /* Lazily builds and caches handlers that will push encoded data to a bytessink.
  6075. * Any msgdef objects used with this object must outlive it. */
  6076. upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames);
  6077. #ifdef __cplusplus
  6078. } /* extern "C" */
  6079. /* Prints an incoming stream of data to a BytesSink in JSON format. */
  6080. class upb::json::PrinterPtr {
  6081. public:
  6082. PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {}
  6083. static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers,
  6084. BytesSink output) {
  6085. return PrinterPtr(
  6086. upb_json_printer_create(arena->ptr(), handlers, output.sink()));
  6087. }
  6088. /* The input to the printer. */
  6089. Sink input() { return upb_json_printer_input(ptr_); }
  6090. static const size_t kSize = UPB_JSON_PRINTER_SIZE;
  6091. static HandlerCache NewCache(bool preserve_proto_fieldnames) {
  6092. return upb_json_printer_newcache(preserve_proto_fieldnames);
  6093. }
  6094. private:
  6095. upb_json_printer* ptr_;
  6096. };
  6097. #endif /* __cplusplus */
  6098. #endif /* UPB_JSON_TYPED_PRINTER_H_ */
  6099. #undef UPB_SIZE
  6100. #undef UPB_FIELD_AT
  6101. #undef UPB_READ_ONEOF
  6102. #undef UPB_WRITE_ONEOF