upb.h 314 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025
  1. // Amalgamated source file
  2. /*
  3. * upb - a minimalist implementation of protocol buffers.
  4. *
  5. * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
  6. * Author: Josh Haberman <jhaberman@gmail.com>
  7. *
  8. * Defs are upb's internal representation of the constructs that can appear
  9. * in a .proto file:
  10. *
  11. * - upb_msgdef: describes a "message" construct.
  12. * - upb_fielddef: describes a message field.
  13. * - upb_enumdef: describes an enum.
  14. * (TODO: definitions of services).
  15. *
  16. * Like upb_refcounted objects, defs are mutable only until frozen, and are
  17. * only thread-safe once frozen.
  18. *
  19. * This is a mixed C/C++ interface that offers a full API to both languages.
  20. * See the top-level README for more information.
  21. */
  22. #ifndef UPB_DEF_H_
  23. #define UPB_DEF_H_
  24. /*
  25. * upb - a minimalist implementation of protocol buffers.
  26. *
  27. * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
  28. * Author: Josh Haberman <jhaberman@gmail.com>
  29. *
  30. * A refcounting scheme that supports circular refs. It accomplishes this by
  31. * partitioning the set of objects into groups such that no cycle spans groups;
  32. * we can then reference-count the group as a whole and ignore refs within the
  33. * group. When objects are mutable, these groups are computed very
  34. * conservatively; we group any objects that have ever had a link between them.
  35. * When objects are frozen, we compute strongly-connected components which
  36. * allows us to be precise and only group objects that are actually cyclic.
  37. *
  38. * This is a mixed C/C++ interface that offers a full API to both languages.
  39. * See the top-level README for more information.
  40. */
  41. #ifndef UPB_REFCOUNTED_H_
  42. #define UPB_REFCOUNTED_H_
  43. /*
  44. * upb - a minimalist implementation of protocol buffers.
  45. *
  46. * Copyright (c) 2009 Google Inc. See LICENSE for details.
  47. * Author: Josh Haberman <jhaberman@gmail.com>
  48. *
  49. * This header is INTERNAL-ONLY! Its interfaces are not public or stable!
  50. * This file defines very fast int->upb_value (inttable) and string->upb_value
  51. * (strtable) hash tables.
  52. *
  53. * The table uses chained scatter with Brent's variation (inspired by the Lua
  54. * implementation of hash tables). The hash function for strings is Austin
  55. * Appleby's "MurmurHash."
  56. *
  57. * The inttable uses uintptr_t as its key, which guarantees it can be used to
  58. * store pointers or integers of at least 32 bits (upb isn't really useful on
  59. * systems where sizeof(void*) < 4).
  60. *
  61. * The table must be homogenous (all values of the same type). In debug
  62. * mode, we check this on insert and lookup.
  63. */
  64. #ifndef UPB_TABLE_H_
  65. #define UPB_TABLE_H_
  66. #include <assert.h>
  67. #include <stdint.h>
  68. #include <string.h>
  69. /*
  70. * upb - a minimalist implementation of protocol buffers.
  71. *
  72. * Copyright (c) 2009 Google Inc. See LICENSE for details.
  73. * Author: Josh Haberman <jhaberman@gmail.com>
  74. *
  75. * This file contains shared definitions that are widely used across upb.
  76. *
  77. * This is a mixed C/C++ interface that offers a full API to both languages.
  78. * See the top-level README for more information.
  79. */
  80. #ifndef UPB_H_
  81. #define UPB_H_
  82. #include <assert.h>
  83. #include <stdarg.h>
  84. #include <stdbool.h>
  85. #include <stddef.h>
  86. // inline if possible, emit standalone code if required.
  87. #ifdef __cplusplus
  88. #define UPB_INLINE inline
  89. #else
  90. #define UPB_INLINE static inline
  91. #endif
  92. #if __STDC_VERSION__ >= 199901L
  93. #define UPB_C99
  94. #endif
  95. #if ((defined(__cplusplus) && __cplusplus >= 201103L) || \
  96. defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11)
  97. #define UPB_CXX11
  98. #endif
  99. #ifdef UPB_CXX11
  100. #include <type_traits>
  101. #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
  102. class_name(const class_name&) = delete; \
  103. void operator=(const class_name&) = delete;
  104. #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
  105. class_name() = delete; \
  106. ~class_name() = delete; \
  107. /* Friend Pointer<T> so it can access base class. */ \
  108. friend class Pointer<full_class_name>; \
  109. friend class Pointer<const full_class_name>; \
  110. UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
  111. #define UPB_ASSERT_STDLAYOUT(type) \
  112. static_assert(std::is_standard_layout<type>::value, \
  113. #type " must be standard layout");
  114. #else // !defined(UPB_CXX11)
  115. #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
  116. class_name(const class_name&); \
  117. void operator=(const class_name&);
  118. #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
  119. class_name(); \
  120. ~class_name(); \
  121. /* Friend Pointer<T> so it can access base class. */ \
  122. friend class Pointer<full_class_name>; \
  123. friend class Pointer<const full_class_name>; \
  124. UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
  125. #define UPB_ASSERT_STDLAYOUT(type)
  126. #endif
  127. #ifdef __cplusplus
  128. #define UPB_PRIVATE_FOR_CPP private:
  129. #define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname;
  130. #define UPB_BEGIN_EXTERN_C extern "C" {
  131. #define UPB_END_EXTERN_C }
  132. #define UPB_DEFINE_STRUCT0(cname, members) members;
  133. #define UPB_DEFINE_STRUCT(cname, cbase, members) \
  134. public: \
  135. cbase* base() { return &base_; } \
  136. const cbase* base() const { return &base_; } \
  137. \
  138. private: \
  139. cbase base_; \
  140. members;
  141. #define UPB_DEFINE_CLASS0(cppname, cppmethods, members) \
  142. class cppname { \
  143. cppmethods \
  144. members \
  145. }; \
  146. UPB_ASSERT_STDLAYOUT(cppname);
  147. #define UPB_DEFINE_CLASS1(cppname, cppbase, cppmethods, members) \
  148. UPB_DEFINE_CLASS0(cppname, cppmethods, members) \
  149. namespace upb { \
  150. template <> \
  151. class Pointer<cppname> : public PointerBase<cppname, cppbase> { \
  152. public: \
  153. explicit Pointer(cppname* ptr) : PointerBase(ptr) {} \
  154. }; \
  155. template <> \
  156. class Pointer<const cppname> \
  157. : public PointerBase<const cppname, const cppbase> { \
  158. public: \
  159. explicit Pointer(const cppname* ptr) : PointerBase(ptr) {} \
  160. }; \
  161. }
  162. #define UPB_DEFINE_CLASS2(cppname, cppbase, cppbase2, cppmethods, members) \
  163. UPB_DEFINE_CLASS0(cppname, UPB_QUOTE(cppmethods), members) \
  164. namespace upb { \
  165. template <> \
  166. class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \
  167. public: \
  168. explicit Pointer(cppname* ptr) : PointerBase2(ptr) {} \
  169. }; \
  170. template <> \
  171. class Pointer<const cppname> \
  172. : public PointerBase2<const cppname, const cppbase, const cppbase2> { \
  173. public: \
  174. explicit Pointer(const cppname* ptr) : PointerBase2(ptr) {} \
  175. }; \
  176. }
  177. #else // !defined(__cplusplus)
  178. #define UPB_PRIVATE_FOR_CPP
  179. #define UPB_DECLARE_TYPE(cppname, cname) \
  180. struct cname; \
  181. typedef struct cname cname;
  182. #define UPB_BEGIN_EXTERN_C
  183. #define UPB_END_EXTERN_C
  184. #define UPB_DEFINE_STRUCT0(cname, members) \
  185. struct cname { \
  186. members; \
  187. };
  188. #define UPB_DEFINE_STRUCT(cname, cbase, members) \
  189. struct cname { \
  190. cbase base; \
  191. members; \
  192. };
  193. #define UPB_DEFINE_CLASS0(cppname, cppmethods, members) members
  194. #define UPB_DEFINE_CLASS1(cppname, cppbase, cppmethods, members) members
  195. #define UPB_DEFINE_CLASS2(cppname, cppbase, cppbase2, cppmethods, members) \
  196. members
  197. #endif // defined(__cplusplus)
  198. #ifdef __GNUC__
  199. #define UPB_NORETURN __attribute__((__noreturn__))
  200. #else
  201. #define UPB_NORETURN
  202. #endif
  203. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  204. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  205. #define UPB_UNUSED(var) (void)var
  206. // Code with commas confuses the preprocessor when passed as arguments, whether
  207. // C++ type names with commas (eg. Foo<int, int>) or code blocks that declare
  208. // variables (ie. int foo, bar).
  209. #define UPB_QUOTE(...) __VA_ARGS__
  210. // For asserting something about a variable when the variable is not used for
  211. // anything else. This prevents "unused variable" warnings when compiling in
  212. // debug mode.
  213. #define UPB_ASSERT_VAR(var, predicate) UPB_UNUSED(var); assert(predicate)
  214. // Generic function type.
  215. typedef void upb_func();
  216. /* Casts **********************************************************************/
  217. // Upcasts for C. For downcasts see the definitions of the subtypes.
  218. #define UPB_UPCAST(obj) (&(obj)->base)
  219. #define UPB_UPCAST2(obj) UPB_UPCAST(UPB_UPCAST(obj))
  220. #ifdef __cplusplus
  221. // Downcasts for C++. We can't use C++ inheritance directly and maintain
  222. // compatibility with C. So our inheritance is undeclared in C++.
  223. // Specializations of these casting functions are defined for appropriate type
  224. // pairs, and perform the necessary checks.
  225. //
  226. // Example:
  227. // upb::Def* def = <...>;
  228. // upb::MessageDef* = upb::dyn_cast<upb::MessageDef*>(def);
  229. namespace upb {
  230. // Casts to a direct subclass. The caller must know that cast is correct; an
  231. // incorrect cast will throw an assertion failure in debug mode.
  232. template<class To, class From> To down_cast(From* f);
  233. // Casts to a direct subclass. If the class does not actually match the given
  234. // subtype, returns NULL.
  235. template<class To, class From> To dyn_cast(From* f);
  236. // Pointer<T> is a simple wrapper around a T*. It is only constructed for
  237. // upcast() below, and its sole purpose is to be implicitly convertable to T* or
  238. // pointers to base classes, just as a pointer would be in regular C++ if the
  239. // inheritance were directly expressed as C++ inheritance.
  240. template <class T> class Pointer;
  241. // Casts to any base class, or the type itself (ie. can be a no-op).
  242. template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); }
  243. template <class T, class Base>
  244. class PointerBase {
  245. public:
  246. explicit PointerBase(T* ptr) : ptr_(ptr) {}
  247. operator T*() { return ptr_; }
  248. operator Base*() { return ptr_->base(); }
  249. private:
  250. T* ptr_;
  251. };
  252. template <class T, class Base, class Base2>
  253. class PointerBase2 : public PointerBase<T, Base> {
  254. public:
  255. explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {}
  256. operator Base2*() { return Pointer<Base>(*this); }
  257. };
  258. }
  259. #endif
  260. /* upb::reffed_ptr ************************************************************/
  261. #ifdef __cplusplus
  262. #include <algorithm> // For std::swap().
  263. namespace upb {
  264. // Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a
  265. // ref on whatever object it points to (if any).
  266. template <class T> class reffed_ptr {
  267. public:
  268. reffed_ptr() : ptr_(NULL) {}
  269. // If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor.
  270. template <class U>
  271. reffed_ptr(U* val, const void* ref_donor = NULL)
  272. : ptr_(upb::upcast(val)) {
  273. if (ref_donor) {
  274. assert(ptr_);
  275. ptr_->DonateRef(ref_donor, this);
  276. } else if (ptr_) {
  277. ptr_->Ref(this);
  278. }
  279. }
  280. template <class U>
  281. reffed_ptr(const reffed_ptr<U>& other)
  282. : ptr_(upb::upcast(other.get())) {
  283. if (ptr_) ptr_->Ref(this);
  284. }
  285. ~reffed_ptr() { if (ptr_) ptr_->Unref(this); }
  286. template <class U>
  287. reffed_ptr& operator=(const reffed_ptr<U>& other) {
  288. reset(other.get());
  289. return *this;
  290. }
  291. reffed_ptr& operator=(const reffed_ptr& other) {
  292. reset(other.get());
  293. return *this;
  294. }
  295. // TODO(haberman): add C++11 move construction/assignment for greater
  296. // efficiency.
  297. void swap(reffed_ptr& other) {
  298. if (ptr_ == other.ptr_) {
  299. return;
  300. }
  301. if (ptr_) ptr_->DonateRef(this, &other);
  302. if (other.ptr_) other.ptr_->DonateRef(&other, this);
  303. std::swap(ptr_, other.ptr_);
  304. }
  305. T& operator*() const {
  306. assert(ptr_);
  307. return *ptr_;
  308. }
  309. T* operator->() const {
  310. assert(ptr_);
  311. return ptr_;
  312. }
  313. T* get() const { return ptr_; }
  314. // If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor.
  315. template <class U>
  316. void reset(U* ptr = NULL, const void* ref_donor = NULL) {
  317. reffed_ptr(ptr, ref_donor).swap(*this);
  318. }
  319. template <class U>
  320. reffed_ptr<U> down_cast() {
  321. return reffed_ptr<U>(upb::down_cast<U*>(get()));
  322. }
  323. template <class U>
  324. reffed_ptr<U> dyn_cast() {
  325. return reffed_ptr<U>(upb::dyn_cast<U*>(get()));
  326. }
  327. // Plain release() is unsafe; if we were the only owner, it would leak the
  328. // object. Instead we provide this:
  329. T* ReleaseTo(const void* new_owner) {
  330. T* ret = NULL;
  331. ptr_->DonateRef(this, new_owner);
  332. std::swap(ret, ptr_);
  333. return ret;
  334. }
  335. private:
  336. T* ptr_;
  337. };
  338. } // namespace upb
  339. #endif // __cplusplus
  340. /* upb::Status ****************************************************************/
  341. #ifdef __cplusplus
  342. namespace upb {
  343. class ErrorSpace;
  344. class Status;
  345. }
  346. #endif
  347. UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace);
  348. UPB_DECLARE_TYPE(upb::Status, upb_status);
  349. // The maximum length of an error message before it will get truncated.
  350. #define UPB_STATUS_MAX_MESSAGE 128
  351. // An error callback function is used to report errors from some component.
  352. // The function can return "true" to indicate that the component should try
  353. // to recover and proceed, but this is not always possible.
  354. typedef bool upb_errcb_t(void *closure, const upb_status* status);
  355. UPB_DEFINE_CLASS0(upb::ErrorSpace,
  356. ,
  357. UPB_DEFINE_STRUCT0(upb_errorspace,
  358. const char *name;
  359. // Should the error message in the status object according to this code.
  360. void (*set_message)(upb_status* status, int code);
  361. ));
  362. // Object representing a success or failure status.
  363. // It owns no resources and allocates no memory, so it should work
  364. // even in OOM situations.
  365. UPB_DEFINE_CLASS0(upb::Status,
  366. public:
  367. Status();
  368. // Returns true if there is no error.
  369. bool ok() const;
  370. // Optional error space and code, useful if the caller wants to
  371. // programmatically check the specific kind of error.
  372. ErrorSpace* error_space();
  373. int code() const;
  374. const char *error_message() const;
  375. // The error message will be truncated if it is longer than
  376. // UPB_STATUS_MAX_MESSAGE-4.
  377. void SetErrorMessage(const char* msg);
  378. void SetFormattedErrorMessage(const char* fmt, ...);
  379. // If there is no error message already, this will use the ErrorSpace to
  380. // populate the error message for this code. The caller can still call
  381. // SetErrorMessage() to give a more specific message.
  382. void SetErrorCode(ErrorSpace* space, int code);
  383. // Resets the status to a successful state with no message.
  384. void Clear();
  385. void CopyFrom(const Status& other);
  386. private:
  387. UPB_DISALLOW_COPY_AND_ASSIGN(Status);
  388. ,
  389. UPB_DEFINE_STRUCT0(upb_status,
  390. bool ok_;
  391. // Specific status code defined by some error space (optional).
  392. int code_;
  393. upb_errorspace *error_space_;
  394. // Error message; NULL-terminated.
  395. char msg[UPB_STATUS_MAX_MESSAGE];
  396. ));
  397. #define UPB_STATUS_INIT {true, 0, NULL, {0}}
  398. #ifdef __cplusplus
  399. extern "C" {
  400. #endif
  401. // The returned string is invalidated by any other call into the status.
  402. const char *upb_status_errmsg(const upb_status *status);
  403. bool upb_ok(const upb_status *status);
  404. upb_errorspace *upb_status_errspace(const upb_status *status);
  405. int upb_status_errcode(const upb_status *status);
  406. // Any of the functions that write to a status object allow status to be NULL,
  407. // to support use cases where the function's caller does not care about the
  408. // status message.
  409. void upb_status_clear(upb_status *status);
  410. void upb_status_seterrmsg(upb_status *status, const char *msg);
  411. void upb_status_seterrf(upb_status *status, const char *fmt, ...);
  412. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
  413. void upb_status_seterrcode(upb_status *status, upb_errorspace *space, int code);
  414. void upb_status_copy(upb_status *to, const upb_status *from);
  415. #ifdef __cplusplus
  416. } // extern "C"
  417. namespace upb {
  418. // C++ Wrappers
  419. inline Status::Status() { Clear(); }
  420. inline bool Status::ok() const { return upb_ok(this); }
  421. inline const char* Status::error_message() const {
  422. return upb_status_errmsg(this);
  423. }
  424. inline void Status::SetErrorMessage(const char* msg) {
  425. upb_status_seterrmsg(this, msg);
  426. }
  427. inline void Status::SetFormattedErrorMessage(const char* fmt, ...) {
  428. va_list args;
  429. va_start(args, fmt);
  430. upb_status_vseterrf(this, fmt, args);
  431. va_end(args);
  432. }
  433. inline void Status::SetErrorCode(ErrorSpace* space, int code) {
  434. upb_status_seterrcode(this, space, code);
  435. }
  436. inline void Status::Clear() { upb_status_clear(this); }
  437. inline void Status::CopyFrom(const Status& other) {
  438. upb_status_copy(this, &other);
  439. }
  440. } // namespace upb
  441. #endif
  442. #endif /* UPB_H_ */
  443. #ifdef __cplusplus
  444. extern "C" {
  445. #endif
  446. /* upb_value ******************************************************************/
  447. // A tagged union (stored untagged inside the table) so that we can check that
  448. // clients calling table accessors are correctly typed without having to have
  449. // an explosion of accessors.
  450. typedef enum {
  451. UPB_CTYPE_INT32 = 1,
  452. UPB_CTYPE_INT64 = 2,
  453. UPB_CTYPE_UINT32 = 3,
  454. UPB_CTYPE_UINT64 = 4,
  455. UPB_CTYPE_BOOL = 5,
  456. UPB_CTYPE_CSTR = 6,
  457. UPB_CTYPE_PTR = 7,
  458. UPB_CTYPE_CONSTPTR = 8,
  459. UPB_CTYPE_FPTR = 9,
  460. } upb_ctype_t;
  461. typedef union {
  462. int32_t int32;
  463. int64_t int64;
  464. uint64_t uint64;
  465. uint32_t uint32;
  466. bool _bool;
  467. char *cstr;
  468. void *ptr;
  469. const void *constptr;
  470. upb_func *fptr;
  471. } _upb_value;
  472. typedef struct {
  473. _upb_value val;
  474. #ifndef NDEBUG
  475. // In debug mode we carry the value type around also so we can check accesses
  476. // to be sure the right member is being read.
  477. upb_ctype_t ctype;
  478. #endif
  479. } upb_value;
  480. #ifdef UPB_C99
  481. #define UPB_VALUE_INIT(v, member) {.member = v}
  482. #endif
  483. #define UPB__VALUE_INIT_NONE UPB_VALUE_INIT(NULL, ptr)
  484. #ifdef NDEBUG
  485. #define SET_TYPE(dest, val) UPB_UNUSED(val)
  486. #define UPB_VALUE_INIT_NONE {UPB__VALUE_INIT_NONE}
  487. #else
  488. #define SET_TYPE(dest, val) dest = val
  489. // Non-existent type, all reads will fail.
  490. #define UPB_VALUE_INIT_NONE {UPB__VALUE_INIT_NONE, -1}
  491. #endif
  492. #define UPB_VALUE_INIT_INT32(v) UPB_VALUE_INIT(v, int32)
  493. #define UPB_VALUE_INIT_INT64(v) UPB_VALUE_INIT(v, int64)
  494. #define UPB_VALUE_INIT_UINT32(v) UPB_VALUE_INIT(v, uint32)
  495. #define UPB_VALUE_INIT_UINT64(v) UPB_VALUE_INIT(v, uint64)
  496. #define UPB_VALUE_INIT_BOOL(v) UPB_VALUE_INIT(v, _bool)
  497. #define UPB_VALUE_INIT_CSTR(v) UPB_VALUE_INIT(v, cstr)
  498. #define UPB_VALUE_INIT_PTR(v) UPB_VALUE_INIT(v, ptr)
  499. #define UPB_VALUE_INIT_CONSTPTR(v) UPB_VALUE_INIT(v, constptr)
  500. #define UPB_VALUE_INIT_FPTR(v) UPB_VALUE_INIT(v, fptr)
  501. // Like strdup(), which isn't always available since it's not ANSI C.
  502. char *upb_strdup(const char *s);
  503. // Variant that works with a length-delimited rather than NULL-delimited string,
  504. // as supported by strtable.
  505. char *upb_strdup2(const char *s, size_t len);
  506. UPB_INLINE void _upb_value_setval(upb_value *v, _upb_value val,
  507. upb_ctype_t ctype) {
  508. v->val = val;
  509. SET_TYPE(v->ctype, ctype);
  510. }
  511. UPB_INLINE upb_value _upb_value_val(_upb_value val, upb_ctype_t ctype) {
  512. upb_value ret;
  513. _upb_value_setval(&ret, val, ctype);
  514. return ret;
  515. }
  516. // For each value ctype, define the following set of functions:
  517. //
  518. // // Get/set an int32 from a upb_value.
  519. // int32_t upb_value_getint32(upb_value val);
  520. // void upb_value_setint32(upb_value *val, int32_t cval);
  521. //
  522. // // Construct a new upb_value from an int32.
  523. // upb_value upb_value_int32(int32_t val);
  524. #define FUNCS(name, membername, type_t, proto_type) \
  525. UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
  526. val->val.uint64 = 0; \
  527. SET_TYPE(val->ctype, proto_type); \
  528. val->val.membername = cval; \
  529. } \
  530. UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
  531. upb_value ret; \
  532. upb_value_set ## name(&ret, val); \
  533. return ret; \
  534. } \
  535. UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
  536. assert(val.ctype == proto_type); \
  537. return val.val.membername; \
  538. }
  539. FUNCS(int32, int32, int32_t, UPB_CTYPE_INT32);
  540. FUNCS(int64, int64, int64_t, UPB_CTYPE_INT64);
  541. FUNCS(uint32, uint32, uint32_t, UPB_CTYPE_UINT32);
  542. FUNCS(uint64, uint64, uint64_t, UPB_CTYPE_UINT64);
  543. FUNCS(bool, _bool, bool, UPB_CTYPE_BOOL);
  544. FUNCS(cstr, cstr, char*, UPB_CTYPE_CSTR);
  545. FUNCS(ptr, ptr, void*, UPB_CTYPE_PTR);
  546. FUNCS(constptr, constptr, const void*, UPB_CTYPE_CONSTPTR);
  547. FUNCS(fptr, fptr, upb_func*, UPB_CTYPE_FPTR);
  548. #undef FUNCS
  549. /* upb_table ******************************************************************/
  550. typedef union {
  551. uintptr_t num;
  552. struct {
  553. // We own this. NULL-terminated but may also contain binary data; see
  554. // explicit length below.
  555. // TODO: move the length to the start of the string in order to reduce
  556. // tabkey's size (to one machine word) in a way that supports static
  557. // initialization.
  558. const char *str;
  559. size_t length;
  560. } s;
  561. } upb_tabkey;
  562. #define UPB_TABKEY_NUM(n) {n}
  563. #ifdef UPB_C99
  564. // Given that |s| is a string literal, sizeof(s) gives us a
  565. // compile-time-constant strlen(). We must ensure that this works for static
  566. // data initializers.
  567. #define UPB_TABKEY_STR(strval) { .s = { .str = strval, \
  568. .length = sizeof(strval) - 1 } }
  569. #endif
  570. // TODO(haberman): C++
  571. #define UPB_TABKEY_NONE {0}
  572. typedef struct _upb_tabent {
  573. upb_tabkey key;
  574. _upb_value val;
  575. // Internal chaining. This is const so we can create static initializers for
  576. // tables. We cast away const sometimes, but *only* when the containing
  577. // upb_table is known to be non-const. This requires a bit of care, but
  578. // the subtlety is confined to table.c.
  579. const struct _upb_tabent *next;
  580. } upb_tabent;
  581. typedef struct {
  582. size_t count; // Number of entries in the hash part.
  583. size_t mask; // Mask to turn hash value -> bucket.
  584. upb_ctype_t ctype; // Type of all values.
  585. uint8_t size_lg2; // Size of the hash table part is 2^size_lg2 entries.
  586. // Hash table entries.
  587. // Making this const isn't entirely accurate; what we really want is for it to
  588. // have the same const-ness as the table it's inside. But there's no way to
  589. // declare that in C. So we have to make it const so that we can statically
  590. // initialize const hash tables. Then we cast away const when we have to.
  591. const upb_tabent *entries;
  592. } upb_table;
  593. typedef struct {
  594. upb_table t;
  595. } upb_strtable;
  596. #define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
  597. {{count, mask, ctype, size_lg2, entries}}
  598. #define UPB_EMPTY_STRTABLE_INIT(ctype) \
  599. UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL)
  600. typedef struct {
  601. upb_table t; // For entries that don't fit in the array part.
  602. const _upb_value *array; // Array part of the table. See const note above.
  603. size_t array_size; // Array part size.
  604. size_t array_count; // Array part number of elements.
  605. } upb_inttable;
  606. #define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
  607. {{count, mask, ctype, size_lg2, ent}, a, asize, acount}
  608. #define UPB_EMPTY_INTTABLE_INIT(ctype) \
  609. UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
  610. #define UPB_ARRAY_EMPTYVAL -1
  611. #define UPB_ARRAY_EMPTYENT UPB_VALUE_INIT_INT64(UPB_ARRAY_EMPTYVAL)
  612. UPB_INLINE size_t upb_table_size(const upb_table *t) {
  613. if (t->size_lg2 == 0)
  614. return 0;
  615. else
  616. return 1 << t->size_lg2;
  617. }
  618. // Internal-only functions, in .h file only out of necessity.
  619. UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
  620. return e->key.num == 0;
  621. }
  622. // Used by some of the unit tests for generic hashing functionality.
  623. uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
  624. UPB_INLINE upb_tabkey upb_intkey(uintptr_t key) {
  625. upb_tabkey k;
  626. k.num = key;
  627. return k;
  628. }
  629. UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
  630. return (uint32_t)key;
  631. }
  632. static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
  633. return t->entries + (hash & t->mask);
  634. }
  635. UPB_INLINE bool upb_arrhas(_upb_value v) {
  636. return v.uint64 != (uint64_t)UPB_ARRAY_EMPTYVAL;
  637. }
  638. // Initialize and uninitialize a table, respectively. If memory allocation
  639. // failed, false is returned that the table is uninitialized.
  640. bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype);
  641. bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype);
  642. void upb_inttable_uninit(upb_inttable *table);
  643. void upb_strtable_uninit(upb_strtable *table);
  644. // Returns the number of values in the table.
  645. size_t upb_inttable_count(const upb_inttable *t);
  646. UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
  647. return t->t.count;
  648. }
  649. // Inserts the given key into the hashtable with the given value. The key must
  650. // not already exist in the hash table. For string tables, the key must be
  651. // NULL-terminated, and the table will make an internal copy of the key.
  652. // Inttables must not insert a value of UINTPTR_MAX.
  653. //
  654. // If a table resize was required but memory allocation failed, false is
  655. // returned and the table is unchanged.
  656. bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val);
  657. bool upb_strtable_insert2(upb_strtable *t, const char *key, size_t len,
  658. upb_value val);
  659. // For NULL-terminated strings.
  660. UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
  661. upb_value val) {
  662. return upb_strtable_insert2(t, key, strlen(key), val);
  663. }
  664. // Looks up key in this table, returning "true" if the key was found.
  665. // If v is non-NULL, copies the value for this key into *v.
  666. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
  667. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  668. upb_value *v);
  669. // For NULL-terminated strings.
  670. UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
  671. upb_value *v) {
  672. return upb_strtable_lookup2(t, key, strlen(key), v);
  673. }
  674. // Removes an item from the table. Returns true if the remove was successful,
  675. // and stores the removed item in *val if non-NULL.
  676. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
  677. bool upb_strtable_remove2(upb_strtable *t, const char *key, size_t len,
  678. upb_value *val);
  679. // For NULL-terminated strings.
  680. UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
  681. upb_value *v) {
  682. return upb_strtable_remove2(t, key, strlen(key), v);
  683. }
  684. // Updates an existing entry in an inttable. If the entry does not exist,
  685. // returns false and does nothing. Unlike insert/remove, this does not
  686. // invalidate iterators.
  687. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
  688. // Handy routines for treating an inttable like a stack. May not be mixed with
  689. // other insert/remove calls.
  690. bool upb_inttable_push(upb_inttable *t, upb_value val);
  691. upb_value upb_inttable_pop(upb_inttable *t);
  692. // Convenience routines for inttables with pointer keys.
  693. bool upb_inttable_insertptr(upb_inttable *t, const void *key, upb_value val);
  694. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
  695. bool upb_inttable_lookupptr(
  696. const upb_inttable *t, const void *key, upb_value *val);
  697. // Optimizes the table for the current set of entries, for both memory use and
  698. // lookup time. Client should call this after all entries have been inserted;
  699. // inserting more entries is legal, but will likely require a table resize.
  700. void upb_inttable_compact(upb_inttable *t);
  701. // A special-case inlinable version of the lookup routine for 32-bit integers.
  702. UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
  703. upb_value *v) {
  704. *v = upb_value_int32(0); // Silence compiler warnings.
  705. if (key < t->array_size) {
  706. _upb_value arrval = t->array[key];
  707. if (upb_arrhas(arrval)) {
  708. _upb_value_setval(v, arrval, t->t.ctype);
  709. return true;
  710. } else {
  711. return false;
  712. }
  713. } else {
  714. const upb_tabent *e;
  715. if (t->t.entries == NULL) return false;
  716. for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
  717. if ((uint32_t)e->key.num == key) {
  718. _upb_value_setval(v, e->val, t->t.ctype);
  719. return true;
  720. }
  721. if (e->next == NULL) return false;
  722. }
  723. }
  724. }
  725. // Exposed for testing only.
  726. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2);
  727. /* Iterators ******************************************************************/
  728. // Iterators for int and string tables. We are subject to some kind of unusual
  729. // design constraints:
  730. //
  731. // For high-level languages:
  732. // - we must be able to guarantee that we don't crash or corrupt memory even if
  733. // the program accesses an invalidated iterator.
  734. //
  735. // For C++11 range-based for:
  736. // - iterators must be copyable
  737. // - iterators must be comparable
  738. // - it must be possible to construct an "end" value.
  739. //
  740. // Iteration order is undefined.
  741. //
  742. // Modifying the table invalidates iterators. upb_{str,int}table_done() is
  743. // guaranteed to work even on an invalidated iterator, as long as the table it
  744. // is iterating over has not been freed. Calling next() or accessing data from
  745. // an invalidated iterator yields unspecified elements from the table, but it is
  746. // guaranteed not to crash and to return real table elements (except when done()
  747. // is true).
  748. /* upb_strtable_iter **********************************************************/
  749. // upb_strtable_iter i;
  750. // upb_strtable_begin(&i, t);
  751. // for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  752. // const char *key = upb_strtable_iter_key(&i);
  753. // const upb_value val = upb_strtable_iter_value(&i);
  754. // // ...
  755. // }
  756. typedef struct {
  757. const upb_strtable *t;
  758. size_t index;
  759. } upb_strtable_iter;
  760. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
  761. void upb_strtable_next(upb_strtable_iter *i);
  762. bool upb_strtable_done(const upb_strtable_iter *i);
  763. const char *upb_strtable_iter_key(upb_strtable_iter *i);
  764. size_t upb_strtable_iter_keylength(upb_strtable_iter *i);
  765. upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
  766. void upb_strtable_iter_setdone(upb_strtable_iter *i);
  767. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  768. const upb_strtable_iter *i2);
  769. /* upb_inttable_iter **********************************************************/
  770. // upb_inttable_iter i;
  771. // upb_inttable_begin(&i, t);
  772. // for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  773. // uintptr_t key = upb_inttable_iter_key(&i);
  774. // upb_value val = upb_inttable_iter_value(&i);
  775. // // ...
  776. // }
  777. typedef struct {
  778. const upb_inttable *t;
  779. size_t index;
  780. bool array_part;
  781. } upb_inttable_iter;
  782. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
  783. void upb_inttable_next(upb_inttable_iter *i);
  784. bool upb_inttable_done(const upb_inttable_iter *i);
  785. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
  786. upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
  787. void upb_inttable_iter_setdone(upb_inttable_iter *i);
  788. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  789. const upb_inttable_iter *i2);
  790. #ifdef __cplusplus
  791. } /* extern "C" */
  792. #endif
  793. #endif /* UPB_TABLE_H_ */
  794. // Reference tracking will check ref()/unref() operations to make sure the
  795. // ref ownership is correct. Where possible it will also make tools like
  796. // Valgrind attribute ref leaks to the code that took the leaked ref, not
  797. // the code that originally created the object.
  798. //
  799. // Enabling this requires the application to define upb_lock()/upb_unlock()
  800. // functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE).
  801. #ifndef NDEBUG
  802. #define UPB_DEBUG_REFS
  803. #endif
  804. #ifdef __cplusplus
  805. namespace upb { class RefCounted; }
  806. #endif
  807. UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted);
  808. struct upb_refcounted_vtbl;
  809. UPB_DEFINE_CLASS0(upb::RefCounted,
  810. public:
  811. // Returns true if the given object is frozen.
  812. bool IsFrozen() const;
  813. // Increases the ref count, the new ref is owned by "owner" which must not
  814. // already own a ref (and should not itself be a refcounted object if the ref
  815. // could possibly be circular; see below).
  816. // Thread-safe iff "this" is frozen.
  817. void Ref(const void *owner) const;
  818. // Release a ref that was acquired from upb_refcounted_ref() and collects any
  819. // objects it can.
  820. void Unref(const void *owner) const;
  821. // Moves an existing ref from "from" to "to", without changing the overall
  822. // ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner),
  823. // but "to" may not be NULL.
  824. void DonateRef(const void *from, const void *to) const;
  825. // Verifies that a ref to the given object is currently held by the given
  826. // owner. Only effective in UPB_DEBUG_REFS builds.
  827. void CheckRef(const void *owner) const;
  828. private:
  829. UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted);
  830. ,
  831. UPB_DEFINE_STRUCT0(upb_refcounted,
  832. // A single reference count shared by all objects in the group.
  833. uint32_t *group;
  834. // A singly-linked list of all objects in the group.
  835. upb_refcounted *next;
  836. // Table of function pointers for this type.
  837. const struct upb_refcounted_vtbl *vtbl;
  838. // Maintained only when mutable, this tracks the number of refs (but not
  839. // ref2's) to this object. *group should be the sum of all individual_count
  840. // in the group.
  841. uint32_t individual_count;
  842. bool is_frozen;
  843. #ifdef UPB_DEBUG_REFS
  844. upb_inttable *refs; // Maps owner -> trackedref for incoming refs.
  845. upb_inttable *ref2s; // Set of targets for outgoing ref2s.
  846. #endif
  847. ));
  848. UPB_BEGIN_EXTERN_C // {
  849. // It is better to use tracked refs when possible, for the extra debugging
  850. // capability. But if this is not possible (because you don't have easy access
  851. // to a stable pointer value that is associated with the ref), you can pass
  852. // UPB_UNTRACKED_REF instead.
  853. extern const void *UPB_UNTRACKED_REF;
  854. // Native C API.
  855. bool upb_refcounted_isfrozen(const upb_refcounted *r);
  856. void upb_refcounted_ref(const upb_refcounted *r, const void *owner);
  857. void upb_refcounted_unref(const upb_refcounted *r, const void *owner);
  858. void upb_refcounted_donateref(
  859. const upb_refcounted *r, const void *from, const void *to);
  860. void upb_refcounted_checkref(const upb_refcounted *r, const void *owner);
  861. // Internal-to-upb Interface ///////////////////////////////////////////////////
  862. typedef void upb_refcounted_visit(const upb_refcounted *r,
  863. const upb_refcounted *subobj,
  864. void *closure);
  865. struct upb_refcounted_vtbl {
  866. // Must visit all subobjects that are currently ref'd via upb_refcounted_ref2.
  867. // Must be longjmp()-safe.
  868. void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c);
  869. // Must free the object and release all references to other objects.
  870. void (*free)(upb_refcounted *r);
  871. };
  872. // Initializes the refcounted with a single ref for the given owner. Returns
  873. // false if memory could not be allocated.
  874. bool upb_refcounted_init(upb_refcounted *r,
  875. const struct upb_refcounted_vtbl *vtbl,
  876. const void *owner);
  877. // Adds a ref from one refcounted object to another ("from" must not already
  878. // own a ref). These refs may be circular; cycles will be collected correctly
  879. // (if conservatively). These refs do not need to be freed in from's free()
  880. // function.
  881. void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from);
  882. // Removes a ref that was acquired from upb_refcounted_ref2(), and collects any
  883. // object it can. This is only necessary when "from" no longer points to "r",
  884. // and not from from's "free" function.
  885. void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from);
  886. #define upb_ref2(r, from) \
  887. upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from)
  888. #define upb_unref2(r, from) \
  889. upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from)
  890. // Freezes all mutable object reachable by ref2() refs from the given roots.
  891. // This will split refcounting groups into precise SCC groups, so that
  892. // refcounting of frozen objects can be more aggressive. If memory allocation
  893. // fails, or if more than 2**31 mutable objects are reachable from "roots", or
  894. // if the maximum depth of the graph exceeds "maxdepth", false is returned and
  895. // the objects are unchanged.
  896. //
  897. // After this operation succeeds, the objects are frozen/const, and may not be
  898. // used through non-const pointers. In particular, they may not be passed as
  899. // the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all
  900. // operations on frozen refcounteds are threadsafe, and objects will be freed
  901. // at the precise moment that they become unreachable.
  902. //
  903. // Caller must own refs on each object in the "roots" list.
  904. bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
  905. int maxdepth);
  906. // Shared by all compiled-in refcounted objects.
  907. extern uint32_t static_refcount;
  908. UPB_END_EXTERN_C // }
  909. #ifdef UPB_DEBUG_REFS
  910. #define UPB_REFCOUNT_INIT(refs, ref2s) \
  911. {&static_refcount, NULL, NULL, 0, true, refs, ref2s}
  912. #else
  913. #define UPB_REFCOUNT_INIT(refs, ref2s) {&static_refcount, NULL, NULL, 0, true}
  914. #endif
  915. #ifdef __cplusplus
  916. // C++ Wrappers.
  917. namespace upb {
  918. inline bool RefCounted::IsFrozen() const {
  919. return upb_refcounted_isfrozen(this);
  920. }
  921. inline void RefCounted::Ref(const void *owner) const {
  922. upb_refcounted_ref(this, owner);
  923. }
  924. inline void RefCounted::Unref(const void *owner) const {
  925. upb_refcounted_unref(this, owner);
  926. }
  927. inline void RefCounted::DonateRef(const void *from, const void *to) const {
  928. upb_refcounted_donateref(this, from, to);
  929. }
  930. inline void RefCounted::CheckRef(const void *owner) const {
  931. upb_refcounted_checkref(this, owner);
  932. }
  933. } // namespace upb
  934. #endif
  935. #endif // UPB_REFCOUNT_H_
  936. #ifdef __cplusplus
  937. #include <cstring>
  938. #include <string>
  939. #include <vector>
  940. namespace upb {
  941. class Def;
  942. class EnumDef;
  943. class FieldDef;
  944. class MessageDef;
  945. class OneofDef;
  946. }
  947. #endif
  948. UPB_DECLARE_TYPE(upb::Def, upb_def);
  949. UPB_DECLARE_TYPE(upb::EnumDef, upb_enumdef);
  950. UPB_DECLARE_TYPE(upb::FieldDef, upb_fielddef);
  951. UPB_DECLARE_TYPE(upb::MessageDef, upb_msgdef);
  952. UPB_DECLARE_TYPE(upb::OneofDef, upb_oneofdef);
  953. // Maximum field number allowed for FieldDefs. This is an inherent limit of the
  954. // protobuf wire format.
  955. #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
  956. // The maximum message depth that the type graph can have. This is a resource
  957. // limit for the C stack since we sometimes need to recursively traverse the
  958. // graph. Cycles are ok; the traversal will stop when it detects a cycle, but
  959. // we must hit the cycle before the maximum depth is reached.
  960. //
  961. // If having a single static limit is too inflexible, we can add another variant
  962. // of Def::Freeze that allows specifying this as a parameter.
  963. #define UPB_MAX_MESSAGE_DEPTH 64
  964. /* upb::Def: base class for defs *********************************************/
  965. // All the different kind of defs we support. These correspond 1:1 with
  966. // declarations in a .proto file.
  967. typedef enum {
  968. UPB_DEF_MSG,
  969. UPB_DEF_FIELD,
  970. UPB_DEF_ENUM,
  971. UPB_DEF_ONEOF,
  972. UPB_DEF_SERVICE, // Not yet implemented.
  973. UPB_DEF_ANY = -1, // Wildcard for upb_symtab_get*()
  974. } upb_deftype_t;
  975. // The base class of all defs. Its base is upb::RefCounted (use upb::upcast()
  976. // to convert).
  977. UPB_DEFINE_CLASS1(upb::Def, upb::RefCounted,
  978. public:
  979. typedef upb_deftype_t Type;
  980. Def* Dup(const void *owner) const;
  981. // Functionality from upb::RefCounted.
  982. bool IsFrozen() const;
  983. void Ref(const void* owner) const;
  984. void Unref(const void* owner) const;
  985. void DonateRef(const void* from, const void* to) const;
  986. void CheckRef(const void* owner) const;
  987. Type def_type() const;
  988. // "fullname" is the def's fully-qualified name (eg. foo.bar.Message).
  989. const char *full_name() const;
  990. // The def must be mutable. Caller retains ownership of fullname. Defs are
  991. // not required to have a name; if a def has no name when it is frozen, it
  992. // will remain an anonymous def. On failure, returns false and details in "s"
  993. // if non-NULL.
  994. bool set_full_name(const char* fullname, upb::Status* s);
  995. bool set_full_name(const std::string &fullname, upb::Status* s);
  996. // Freezes the given defs; this validates all constraints and marks the defs
  997. // as frozen (read-only). "defs" may not contain any fielddefs, but fields
  998. // of any msgdefs will be frozen.
  999. //
  1000. // Symbolic references to sub-types and enum defaults must have already been
  1001. // resolved. Any mutable defs reachable from any of "defs" must also be in
  1002. // the list; more formally, "defs" must be a transitive closure of mutable
  1003. // defs.
  1004. //
  1005. // After this operation succeeds, the finalized defs must only be accessed
  1006. // through a const pointer!
  1007. static bool Freeze(Def* const* defs, int n, Status* status);
  1008. static bool Freeze(const std::vector<Def*>& defs, Status* status);
  1009. private:
  1010. UPB_DISALLOW_POD_OPS(Def, upb::Def);
  1011. ,
  1012. UPB_DEFINE_STRUCT(upb_def, upb_refcounted,
  1013. const char *fullname;
  1014. upb_deftype_t type : 8;
  1015. // Used as a flag during the def's mutable stage. Must be false unless
  1016. // it is currently being used by a function on the stack. This allows
  1017. // us to easily determine which defs were passed into the function's
  1018. // current invocation.
  1019. bool came_from_user;
  1020. ));
  1021. #define UPB_DEF_INIT(name, type, refs, ref2s) \
  1022. { UPB_REFCOUNT_INIT(refs, ref2s), name, type, false }
  1023. UPB_BEGIN_EXTERN_C // {
  1024. // Native C API.
  1025. upb_def *upb_def_dup(const upb_def *def, const void *owner);
  1026. // From upb_refcounted.
  1027. bool upb_def_isfrozen(const upb_def *def);
  1028. void upb_def_ref(const upb_def *def, const void *owner);
  1029. void upb_def_unref(const upb_def *def, const void *owner);
  1030. void upb_def_donateref(const upb_def *def, const void *from, const void *to);
  1031. void upb_def_checkref(const upb_def *def, const void *owner);
  1032. upb_deftype_t upb_def_type(const upb_def *d);
  1033. const char *upb_def_fullname(const upb_def *d);
  1034. bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s);
  1035. bool upb_def_freeze(upb_def *const *defs, int n, upb_status *s);
  1036. UPB_END_EXTERN_C // }
  1037. /* upb::Def casts *************************************************************/
  1038. #ifdef __cplusplus
  1039. #define UPB_CPP_CASTS(cname, cpptype) \
  1040. namespace upb { \
  1041. template <> \
  1042. inline cpptype *down_cast<cpptype *, Def>(Def * def) { \
  1043. return upb_downcast_##cname##_mutable(def); \
  1044. } \
  1045. template <> \
  1046. inline cpptype *dyn_cast<cpptype *, Def>(Def * def) { \
  1047. return upb_dyncast_##cname##_mutable(def); \
  1048. } \
  1049. template <> \
  1050. inline const cpptype *down_cast<const cpptype *, const Def>( \
  1051. const Def *def) { \
  1052. return upb_downcast_##cname(def); \
  1053. } \
  1054. template <> \
  1055. inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \
  1056. return upb_dyncast_##cname(def); \
  1057. } \
  1058. template <> \
  1059. inline const cpptype *down_cast<const cpptype *, Def>(Def * def) { \
  1060. return upb_downcast_##cname(def); \
  1061. } \
  1062. template <> \
  1063. inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \
  1064. return upb_dyncast_##cname(def); \
  1065. } \
  1066. } // namespace upb
  1067. #else
  1068. #define UPB_CPP_CASTS(cname, cpptype)
  1069. #endif
  1070. // Dynamic casts, for determining if a def is of a particular type at runtime.
  1071. // Downcasts, for when some wants to assert that a def is of a particular type.
  1072. // These are only checked if we are building debug.
  1073. #define UPB_DEF_CASTS(lower, upper, cpptype) \
  1074. UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \
  1075. if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \
  1076. return (upb_##lower *)def; \
  1077. } \
  1078. UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \
  1079. assert(upb_def_type(def) == UPB_DEF_##upper); \
  1080. return (const upb_##lower *)def; \
  1081. } \
  1082. UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \
  1083. return (upb_##lower *)upb_dyncast_##lower(def); \
  1084. } \
  1085. UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \
  1086. return (upb_##lower *)upb_downcast_##lower(def); \
  1087. } \
  1088. UPB_CPP_CASTS(lower, cpptype)
  1089. #define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \
  1090. UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, UPB_QUOTE(cppmethods), \
  1091. members) \
  1092. UPB_DEF_CASTS(lower, upper, cppname)
  1093. /* upb::FieldDef **************************************************************/
  1094. // The types a field can have. Note that this list is not identical to the
  1095. // types defined in descriptor.proto, which gives INT32 and SINT32 separate
  1096. // types (we distinguish the two with the "integer encoding" enum below).
  1097. typedef enum {
  1098. UPB_TYPE_FLOAT = 1,
  1099. UPB_TYPE_DOUBLE = 2,
  1100. UPB_TYPE_BOOL = 3,
  1101. UPB_TYPE_STRING = 4,
  1102. UPB_TYPE_BYTES = 5,
  1103. UPB_TYPE_MESSAGE = 6,
  1104. UPB_TYPE_ENUM = 7, // Enum values are int32.
  1105. UPB_TYPE_INT32 = 8,
  1106. UPB_TYPE_UINT32 = 9,
  1107. UPB_TYPE_INT64 = 10,
  1108. UPB_TYPE_UINT64 = 11,
  1109. } upb_fieldtype_t;
  1110. // The repeated-ness of each field; this matches descriptor.proto.
  1111. typedef enum {
  1112. UPB_LABEL_OPTIONAL = 1,
  1113. UPB_LABEL_REQUIRED = 2,
  1114. UPB_LABEL_REPEATED = 3,
  1115. } upb_label_t;
  1116. // How integers should be encoded in serializations that offer multiple
  1117. // integer encoding methods.
  1118. typedef enum {
  1119. UPB_INTFMT_VARIABLE = 1,
  1120. UPB_INTFMT_FIXED = 2,
  1121. UPB_INTFMT_ZIGZAG = 3, // Only for signed types (INT32/INT64).
  1122. } upb_intfmt_t;
  1123. // Descriptor types, as defined in descriptor.proto.
  1124. typedef enum {
  1125. UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
  1126. UPB_DESCRIPTOR_TYPE_FLOAT = 2,
  1127. UPB_DESCRIPTOR_TYPE_INT64 = 3,
  1128. UPB_DESCRIPTOR_TYPE_UINT64 = 4,
  1129. UPB_DESCRIPTOR_TYPE_INT32 = 5,
  1130. UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
  1131. UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
  1132. UPB_DESCRIPTOR_TYPE_BOOL = 8,
  1133. UPB_DESCRIPTOR_TYPE_STRING = 9,
  1134. UPB_DESCRIPTOR_TYPE_GROUP = 10,
  1135. UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
  1136. UPB_DESCRIPTOR_TYPE_BYTES = 12,
  1137. UPB_DESCRIPTOR_TYPE_UINT32 = 13,
  1138. UPB_DESCRIPTOR_TYPE_ENUM = 14,
  1139. UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
  1140. UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
  1141. UPB_DESCRIPTOR_TYPE_SINT32 = 17,
  1142. UPB_DESCRIPTOR_TYPE_SINT64 = 18,
  1143. } upb_descriptortype_t;
  1144. // A upb_fielddef describes a single field in a message. It is most often
  1145. // found as a part of a upb_msgdef, but can also stand alone to represent
  1146. // an extension.
  1147. //
  1148. // Its base class is upb::Def (use upb::upcast() to convert).
  1149. UPB_DEFINE_DEF(upb::FieldDef, fielddef, FIELD,
  1150. public:
  1151. typedef upb_fieldtype_t Type;
  1152. typedef upb_label_t Label;
  1153. typedef upb_intfmt_t IntegerFormat;
  1154. typedef upb_descriptortype_t DescriptorType;
  1155. // These return true if the given value is a valid member of the enumeration.
  1156. static bool CheckType(int32_t val);
  1157. static bool CheckLabel(int32_t val);
  1158. static bool CheckDescriptorType(int32_t val);
  1159. static bool CheckIntegerFormat(int32_t val);
  1160. // These convert to the given enumeration; they require that the value is
  1161. // valid.
  1162. static Type ConvertType(int32_t val);
  1163. static Label ConvertLabel(int32_t val);
  1164. static DescriptorType ConvertDescriptorType(int32_t val);
  1165. static IntegerFormat ConvertIntegerFormat(int32_t val);
  1166. // Returns NULL if memory allocation failed.
  1167. static reffed_ptr<FieldDef> New();
  1168. // Duplicates the given field, returning NULL if memory allocation failed.
  1169. // When a fielddef is duplicated, the subdef (if any) is made symbolic if it
  1170. // wasn't already. If the subdef is set but has no name (which is possible
  1171. // since msgdefs are not required to have a name) the new fielddef's subdef
  1172. // will be unset.
  1173. FieldDef* Dup(const void* owner) const;
  1174. // Functionality from upb::RefCounted.
  1175. bool IsFrozen() const;
  1176. void Ref(const void* owner) const;
  1177. void Unref(const void* owner) const;
  1178. void DonateRef(const void* from, const void* to) const;
  1179. void CheckRef(const void* owner) const;
  1180. // Functionality from upb::Def.
  1181. const char* full_name() const;
  1182. bool type_is_set() const; // Whether set_[descriptor_]type() has been called.
  1183. Type type() const; // Requires that type_is_set() == true.
  1184. Label label() const; // Defaults to UPB_LABEL_OPTIONAL.
  1185. const char* name() const; // NULL if uninitialized.
  1186. uint32_t number() const; // Returns 0 if uninitialized.
  1187. bool is_extension() const;
  1188. // For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
  1189. // indicates whether this field should have lazy parsing handlers that yield
  1190. // the unparsed string for the submessage.
  1191. //
  1192. // TODO(haberman): I think we want to move this into a FieldOptions container
  1193. // when we add support for custom options (the FieldOptions struct will
  1194. // contain both regular FieldOptions like "lazy" *and* custom options).
  1195. bool lazy() const;
  1196. // For non-string, non-submessage fields, this indicates whether binary
  1197. // protobufs are encoded in packed or non-packed format.
  1198. //
  1199. // TODO(haberman): see note above about putting options like this into a
  1200. // FieldOptions container.
  1201. bool packed() const;
  1202. // An integer that can be used as an index into an array of fields for
  1203. // whatever message this field belongs to. Guaranteed to be less than
  1204. // f->containing_type()->field_count(). May only be accessed once the def has
  1205. // been finalized.
  1206. int index() const;
  1207. // The MessageDef to which this field belongs.
  1208. //
  1209. // If this field has been added to a MessageDef, that message can be retrieved
  1210. // directly (this is always the case for frozen FieldDefs).
  1211. //
  1212. // If the field has not yet been added to a MessageDef, you can set the name
  1213. // of the containing type symbolically instead. This is mostly useful for
  1214. // extensions, where the extension is declared separately from the message.
  1215. const MessageDef* containing_type() const;
  1216. const char* containing_type_name();
  1217. // The OneofDef to which this field belongs, or NULL if this field is not part
  1218. // of a oneof.
  1219. const OneofDef* containing_oneof() const;
  1220. // The field's type according to the enum in descriptor.proto. This is not
  1221. // the same as UPB_TYPE_*, because it distinguishes between (for example)
  1222. // INT32 and SINT32, whereas our "type" enum does not. This return of
  1223. // descriptor_type() is a function of type(), integer_format(), and
  1224. // is_tag_delimited(). Likewise set_descriptor_type() sets all three
  1225. // appropriately.
  1226. DescriptorType descriptor_type() const;
  1227. // Convenient field type tests.
  1228. bool IsSubMessage() const;
  1229. bool IsString() const;
  1230. bool IsSequence() const;
  1231. bool IsPrimitive() const;
  1232. // How integers are encoded. Only meaningful for integer types.
  1233. // Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes.
  1234. IntegerFormat integer_format() const;
  1235. // Whether a submessage field is tag-delimited or not (if false, then
  1236. // length-delimited). May only be set when type() == UPB_TYPE_MESSAGE.
  1237. bool is_tag_delimited() const;
  1238. // Returns the non-string default value for this fielddef, which may either
  1239. // be something the client set explicitly or the "default default" (0 for
  1240. // numbers, empty for strings). The field's type indicates the type of the
  1241. // returned value, except for enum fields that are still mutable.
  1242. //
  1243. // Requires that the given function matches the field's current type.
  1244. int64_t default_int64() const;
  1245. int32_t default_int32() const;
  1246. uint64_t default_uint64() const;
  1247. uint32_t default_uint32() const;
  1248. bool default_bool() const;
  1249. float default_float() const;
  1250. double default_double() const;
  1251. // The resulting string is always NULL-terminated. If non-NULL, the length
  1252. // will be stored in *len.
  1253. const char *default_string(size_t* len) const;
  1254. // For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either
  1255. // string or int32, and both of these methods will always return true.
  1256. //
  1257. // For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated.
  1258. // Enum defaults are unusual. They can be specified either as string or int32,
  1259. // but to be valid the enum must have that value as a member. And if no
  1260. // default is specified, the "default default" comes from the EnumDef.
  1261. //
  1262. // We allow reading the default as either an int32 or a string, but only if
  1263. // we have a meaningful value to report. We have a meaningful value if it was
  1264. // set explicitly, or if we could get the "default default" from the EnumDef.
  1265. // Also if you explicitly set the name and we find the number in the EnumDef
  1266. bool EnumHasStringDefault() const;
  1267. bool EnumHasInt32Default() const;
  1268. // Submessage and enum fields must reference a "subdef", which is the
  1269. // upb::MessageDef or upb::EnumDef that defines their type. Note that when
  1270. // the FieldDef is mutable it may not have a subdef *yet*, but this function
  1271. // still returns true to indicate that the field's type requires a subdef.
  1272. bool HasSubDef() const;
  1273. // Returns the enum or submessage def for this field, if any. The field's
  1274. // type must match (ie. you may only call enum_subdef() for fields where
  1275. // type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or
  1276. // is currently set symbolically.
  1277. const EnumDef* enum_subdef() const;
  1278. const MessageDef* message_subdef() const;
  1279. // Returns the generic subdef for this field. Requires that HasSubDef() (ie.
  1280. // only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields).
  1281. const Def* subdef() const;
  1282. // Returns the symbolic name of the subdef. If the subdef is currently set
  1283. // unresolved (ie. set symbolically) returns the symbolic name. If it has
  1284. // been resolved to a specific subdef, returns the name from that subdef.
  1285. const char* subdef_name() const;
  1286. //////////////////////////////////////////////////////////////////////////////
  1287. // Setters (non-const methods), only valid for mutable FieldDefs!
  1288. //////////////////////////////////////////////////////////////////////////////
  1289. bool set_full_name(const char* fullname, upb::Status* s);
  1290. bool set_full_name(const std::string& fullname, upb::Status* s);
  1291. // This may only be called if containing_type() == NULL (ie. the field has not
  1292. // been added to a message yet).
  1293. bool set_containing_type_name(const char *name, Status* status);
  1294. bool set_containing_type_name(const std::string& name, Status* status);
  1295. // Defaults to false. When we freeze, we ensure that this can only be true
  1296. // for length-delimited message fields. Prior to freezing this can be true or
  1297. // false with no restrictions.
  1298. void set_lazy(bool lazy);
  1299. // Defaults to true. Sets whether this field is encoded in packed format.
  1300. void set_packed(bool packed);
  1301. // "type" or "descriptor_type" MUST be set explicitly before the fielddef is
  1302. // finalized. These setters require that the enum value is valid; if the
  1303. // value did not come directly from an enum constant, the caller should
  1304. // validate it first with the functions above (CheckFieldType(), etc).
  1305. void set_type(Type type);
  1306. void set_label(Label label);
  1307. void set_descriptor_type(DescriptorType type);
  1308. void set_is_extension(bool is_extension);
  1309. // "number" and "name" must be set before the FieldDef is added to a
  1310. // MessageDef, and may not be set after that.
  1311. //
  1312. // "name" is the same as full_name()/set_full_name(), but since fielddefs
  1313. // most often use simple, non-qualified names, we provide this accessor
  1314. // also. Generally only extensions will want to think of this name as
  1315. // fully-qualified.
  1316. bool set_number(uint32_t number, upb::Status* s);
  1317. bool set_name(const char* name, upb::Status* s);
  1318. bool set_name(const std::string& name, upb::Status* s);
  1319. void set_integer_format(IntegerFormat format);
  1320. bool set_tag_delimited(bool tag_delimited, upb::Status* s);
  1321. // Sets default value for the field. The call must exactly match the type
  1322. // of the field. Enum fields may use either setint32 or setstring to set
  1323. // the default numerically or symbolically, respectively, but symbolic
  1324. // defaults must be resolved before finalizing (see ResolveEnumDefault()).
  1325. //
  1326. // Changing the type of a field will reset its default.
  1327. void set_default_int64(int64_t val);
  1328. void set_default_int32(int32_t val);
  1329. void set_default_uint64(uint64_t val);
  1330. void set_default_uint32(uint32_t val);
  1331. void set_default_bool(bool val);
  1332. void set_default_float(float val);
  1333. void set_default_double(double val);
  1334. bool set_default_string(const void *str, size_t len, Status *s);
  1335. bool set_default_string(const std::string &str, Status *s);
  1336. void set_default_cstr(const char *str, Status *s);
  1337. // Before a fielddef is frozen, its subdef may be set either directly (with a
  1338. // upb::Def*) or symbolically. Symbolic refs must be resolved before the
  1339. // containing msgdef can be frozen (see upb_resolve() above). upb always
  1340. // guarantees that any def reachable from a live def will also be kept alive.
  1341. //
  1342. // Both methods require that upb_hassubdef(f) (so the type must be set prior
  1343. // to calling these methods). Returns false if this is not the case, or if
  1344. // the given subdef is not of the correct type. The subdef is reset if the
  1345. // field's type is changed. The subdef can be set to NULL to clear it.
  1346. bool set_subdef(const Def* subdef, Status* s);
  1347. bool set_enum_subdef(const EnumDef* subdef, Status* s);
  1348. bool set_message_subdef(const MessageDef* subdef, Status* s);
  1349. bool set_subdef_name(const char* name, Status* s);
  1350. bool set_subdef_name(const std::string &name, Status* s);
  1351. private:
  1352. UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef);
  1353. ,
  1354. UPB_DEFINE_STRUCT(upb_fielddef, upb_def,
  1355. union {
  1356. int64_t sint;
  1357. uint64_t uint;
  1358. double dbl;
  1359. float flt;
  1360. void *bytes;
  1361. } defaultval;
  1362. union {
  1363. const upb_msgdef *def; // If !msg_is_symbolic.
  1364. char *name; // If msg_is_symbolic.
  1365. } msg;
  1366. union {
  1367. const upb_def *def; // If !subdef_is_symbolic.
  1368. char *name; // If subdef_is_symbolic.
  1369. } sub; // The msgdef or enumdef for this field, if upb_hassubdef(f).
  1370. bool subdef_is_symbolic;
  1371. bool msg_is_symbolic;
  1372. const upb_oneofdef *oneof;
  1373. bool default_is_string;
  1374. bool type_is_set_; // False until type is explicitly set.
  1375. bool is_extension_;
  1376. bool lazy_;
  1377. bool packed_;
  1378. upb_intfmt_t intfmt;
  1379. bool tagdelim;
  1380. upb_fieldtype_t type_;
  1381. upb_label_t label_;
  1382. uint32_t number_;
  1383. uint32_t selector_base; // Used to index into a upb::Handlers table.
  1384. uint32_t index_;
  1385. ));
  1386. #define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
  1387. packed, name, num, msgdef, subdef, selector_base, \
  1388. index, defaultval, refs, ref2s) \
  1389. { \
  1390. UPB_DEF_INIT(name, UPB_DEF_FIELD, refs, ref2s), defaultval, {msgdef}, \
  1391. {subdef}, NULL, false, false, \
  1392. type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
  1393. lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
  1394. }
  1395. UPB_BEGIN_EXTERN_C // {
  1396. // Native C API.
  1397. upb_fielddef *upb_fielddef_new(const void *owner);
  1398. upb_fielddef *upb_fielddef_dup(const upb_fielddef *f, const void *owner);
  1399. // From upb_refcounted.
  1400. bool upb_fielddef_isfrozen(const upb_fielddef *f);
  1401. void upb_fielddef_ref(const upb_fielddef *f, const void *owner);
  1402. void upb_fielddef_unref(const upb_fielddef *f, const void *owner);
  1403. void upb_fielddef_donateref(const upb_fielddef *f, const void *from,
  1404. const void *to);
  1405. void upb_fielddef_checkref(const upb_fielddef *f, const void *owner);
  1406. // From upb_def.
  1407. const char *upb_fielddef_fullname(const upb_fielddef *f);
  1408. bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname,
  1409. upb_status *s);
  1410. bool upb_fielddef_typeisset(const upb_fielddef *f);
  1411. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
  1412. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
  1413. upb_label_t upb_fielddef_label(const upb_fielddef *f);
  1414. uint32_t upb_fielddef_number(const upb_fielddef *f);
  1415. const char *upb_fielddef_name(const upb_fielddef *f);
  1416. bool upb_fielddef_isextension(const upb_fielddef *f);
  1417. bool upb_fielddef_lazy(const upb_fielddef *f);
  1418. bool upb_fielddef_packed(const upb_fielddef *f);
  1419. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
  1420. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
  1421. upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f);
  1422. const char *upb_fielddef_containingtypename(upb_fielddef *f);
  1423. upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f);
  1424. uint32_t upb_fielddef_index(const upb_fielddef *f);
  1425. bool upb_fielddef_istagdelim(const upb_fielddef *f);
  1426. bool upb_fielddef_issubmsg(const upb_fielddef *f);
  1427. bool upb_fielddef_isstring(const upb_fielddef *f);
  1428. bool upb_fielddef_isseq(const upb_fielddef *f);
  1429. bool upb_fielddef_isprimitive(const upb_fielddef *f);
  1430. int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
  1431. int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
  1432. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
  1433. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
  1434. bool upb_fielddef_defaultbool(const upb_fielddef *f);
  1435. float upb_fielddef_defaultfloat(const upb_fielddef *f);
  1436. double upb_fielddef_defaultdouble(const upb_fielddef *f);
  1437. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
  1438. bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f);
  1439. bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f);
  1440. bool upb_fielddef_hassubdef(const upb_fielddef *f);
  1441. const upb_def *upb_fielddef_subdef(const upb_fielddef *f);
  1442. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
  1443. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
  1444. const char *upb_fielddef_subdefname(const upb_fielddef *f);
  1445. void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type);
  1446. void upb_fielddef_setdescriptortype(upb_fielddef *f, int type);
  1447. void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label);
  1448. bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s);
  1449. bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s);
  1450. bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
  1451. upb_status *s);
  1452. void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension);
  1453. void upb_fielddef_setlazy(upb_fielddef *f, bool lazy);
  1454. void upb_fielddef_setpacked(upb_fielddef *f, bool packed);
  1455. void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt);
  1456. void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim);
  1457. void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val);
  1458. void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val);
  1459. void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val);
  1460. void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val);
  1461. void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val);
  1462. void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val);
  1463. void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val);
  1464. bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
  1465. upb_status *s);
  1466. void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
  1467. upb_status *s);
  1468. bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
  1469. upb_status *s);
  1470. bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
  1471. upb_status *s);
  1472. bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
  1473. upb_status *s);
  1474. bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
  1475. upb_status *s);
  1476. bool upb_fielddef_checklabel(int32_t label);
  1477. bool upb_fielddef_checktype(int32_t type);
  1478. bool upb_fielddef_checkdescriptortype(int32_t type);
  1479. bool upb_fielddef_checkintfmt(int32_t fmt);
  1480. UPB_END_EXTERN_C // }
  1481. /* upb::MessageDef ************************************************************/
  1482. typedef upb_inttable_iter upb_msg_field_iter;
  1483. typedef upb_strtable_iter upb_msg_oneof_iter;
  1484. // Structure that describes a single .proto message type.
  1485. //
  1486. // Its base class is upb::Def (use upb::upcast() to convert).
  1487. UPB_DEFINE_DEF(upb::MessageDef, msgdef, MSG, UPB_QUOTE(
  1488. public:
  1489. // Returns NULL if memory allocation failed.
  1490. static reffed_ptr<MessageDef> New();
  1491. // Functionality from upb::RefCounted.
  1492. bool IsFrozen() const;
  1493. void Ref(const void* owner) const;
  1494. void Unref(const void* owner) const;
  1495. void DonateRef(const void* from, const void* to) const;
  1496. void CheckRef(const void* owner) const;
  1497. // Functionality from upb::Def.
  1498. const char* full_name() const;
  1499. bool set_full_name(const char* fullname, Status* s);
  1500. bool set_full_name(const std::string& fullname, Status* s);
  1501. // Call to freeze this MessageDef.
  1502. // WARNING: this will fail if this message has any unfrozen submessages!
  1503. // Messages with cycles must be frozen as a batch using upb::Def::Freeze().
  1504. bool Freeze(Status* s);
  1505. // The number of fields that belong to the MessageDef.
  1506. int field_count() const;
  1507. // The number of oneofs that belong to the MessageDef.
  1508. int oneof_count() const;
  1509. // Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef
  1510. // and the fielddefs are mutable. The fielddef's name and number must be
  1511. // set, and the message may not already contain any field with this name or
  1512. // number, and this fielddef may not be part of another message. In error
  1513. // cases false is returned and the msgdef is unchanged.
  1514. //
  1515. // If the given field is part of a oneof, this call succeeds if and only if
  1516. // that oneof is already part of this msgdef. (Note that adding a oneof to a
  1517. // msgdef automatically adds all of its fields to the msgdef at the time that
  1518. // the oneof is added, so it is usually more idiomatic to add the oneof's
  1519. // fields first then add the oneof to the msgdef. This case is supported for
  1520. // convenience.)
  1521. //
  1522. // If |f| is already part of this MessageDef, this method performs no action
  1523. // and returns true (success). Thus, this method is idempotent.
  1524. bool AddField(FieldDef* f, Status* s);
  1525. bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
  1526. // Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef,
  1527. // oneof, and any fielddefs are mutable, that the fielddefs contained in the
  1528. // oneof do not have any name or number conflicts with existing fields in the
  1529. // msgdef, and that the oneof's name is unique among all oneofs in the msgdef.
  1530. // If the oneof is added successfully, all of its fields will be added
  1531. // directly to the msgdef as well. In error cases, false is returned and the
  1532. // msgdef is unchanged.
  1533. bool AddOneof(OneofDef* o, Status* s);
  1534. bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s);
  1535. // These return NULL if the field is not found.
  1536. FieldDef* FindFieldByNumber(uint32_t number);
  1537. FieldDef* FindFieldByName(const char *name, size_t len);
  1538. const FieldDef* FindFieldByNumber(uint32_t number) const;
  1539. const FieldDef* FindFieldByName(const char* name, size_t len) const;
  1540. FieldDef* FindFieldByName(const char *name) {
  1541. return FindFieldByName(name, strlen(name));
  1542. }
  1543. const FieldDef* FindFieldByName(const char *name) const {
  1544. return FindFieldByName(name, strlen(name));
  1545. }
  1546. template <class T>
  1547. FieldDef* FindFieldByName(const T& str) {
  1548. return FindFieldByName(str.c_str(), str.size());
  1549. }
  1550. template <class T>
  1551. const FieldDef* FindFieldByName(const T& str) const {
  1552. return FindFieldByName(str.c_str(), str.size());
  1553. }
  1554. OneofDef* FindOneofByName(const char* name, size_t len);
  1555. const OneofDef* FindOneofByName(const char* name, size_t len) const;
  1556. OneofDef* FindOneofByName(const char* name) {
  1557. return FindOneofByName(name, strlen(name));
  1558. }
  1559. const OneofDef* FindOneofByName(const char* name) const {
  1560. return FindOneofByName(name, strlen(name));
  1561. }
  1562. template<class T>
  1563. OneofDef* FindOneofByName(const T& str) {
  1564. return FindOneofByName(str.c_str(), str.size());
  1565. }
  1566. template<class T>
  1567. const OneofDef* FindOneofByName(const T& str) const {
  1568. return FindOneofByName(str.c_str(), str.size());
  1569. }
  1570. // Returns a new msgdef that is a copy of the given msgdef (and a copy of all
  1571. // the fields) but with any references to submessages broken and replaced
  1572. // with just the name of the submessage. Returns NULL if memory allocation
  1573. // failed.
  1574. //
  1575. // TODO(haberman): which is more useful, keeping fields resolved or
  1576. // unresolving them? If there's no obvious answer, Should this functionality
  1577. // just be moved into symtab.c?
  1578. MessageDef* Dup(const void* owner) const;
  1579. // Is this message a map entry?
  1580. void setmapentry(bool map_entry);
  1581. bool mapentry() const;
  1582. // Iteration over fields. The order is undefined.
  1583. class field_iterator
  1584. : public std::iterator<std::forward_iterator_tag, FieldDef*> {
  1585. public:
  1586. explicit field_iterator(MessageDef* md);
  1587. static field_iterator end(MessageDef* md);
  1588. void operator++();
  1589. FieldDef* operator*() const;
  1590. bool operator!=(const field_iterator& other) const;
  1591. bool operator==(const field_iterator& other) const;
  1592. private:
  1593. upb_msg_field_iter iter_;
  1594. };
  1595. class const_field_iterator
  1596. : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
  1597. public:
  1598. explicit const_field_iterator(const MessageDef* md);
  1599. static const_field_iterator end(const MessageDef* md);
  1600. void operator++();
  1601. const FieldDef* operator*() const;
  1602. bool operator!=(const const_field_iterator& other) const;
  1603. bool operator==(const const_field_iterator& other) const;
  1604. private:
  1605. upb_msg_field_iter iter_;
  1606. };
  1607. // Iteration over oneofs. The order is undefined.
  1608. class oneof_iterator
  1609. : public std::iterator<std::forward_iterator_tag, FieldDef*> {
  1610. public:
  1611. explicit oneof_iterator(MessageDef* md);
  1612. static oneof_iterator end(MessageDef* md);
  1613. void operator++();
  1614. OneofDef* operator*() const;
  1615. bool operator!=(const oneof_iterator& other) const;
  1616. bool operator==(const oneof_iterator& other) const;
  1617. private:
  1618. upb_msg_oneof_iter iter_;
  1619. };
  1620. class const_oneof_iterator
  1621. : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
  1622. public:
  1623. explicit const_oneof_iterator(const MessageDef* md);
  1624. static const_oneof_iterator end(const MessageDef* md);
  1625. void operator++();
  1626. const OneofDef* operator*() const;
  1627. bool operator!=(const const_oneof_iterator& other) const;
  1628. bool operator==(const const_oneof_iterator& other) const;
  1629. private:
  1630. upb_msg_oneof_iter iter_;
  1631. };
  1632. class FieldAccessor {
  1633. public:
  1634. explicit FieldAccessor(MessageDef* msg) : msg_(msg) {}
  1635. field_iterator begin() { return msg_->field_begin(); }
  1636. field_iterator end() { return msg_->field_end(); }
  1637. private:
  1638. MessageDef* msg_;
  1639. };
  1640. class ConstFieldAccessor {
  1641. public:
  1642. explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {}
  1643. const_field_iterator begin() { return msg_->field_begin(); }
  1644. const_field_iterator end() { return msg_->field_end(); }
  1645. private:
  1646. const MessageDef* msg_;
  1647. };
  1648. class OneofAccessor {
  1649. public:
  1650. explicit OneofAccessor(MessageDef* msg) : msg_(msg) {}
  1651. oneof_iterator begin() { return msg_->oneof_begin(); }
  1652. oneof_iterator end() { return msg_->oneof_end(); }
  1653. private:
  1654. MessageDef* msg_;
  1655. };
  1656. class ConstOneofAccessor {
  1657. public:
  1658. explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {}
  1659. const_oneof_iterator begin() { return msg_->oneof_begin(); }
  1660. const_oneof_iterator end() { return msg_->oneof_end(); }
  1661. private:
  1662. const MessageDef* msg_;
  1663. };
  1664. field_iterator field_begin();
  1665. field_iterator field_end();
  1666. const_field_iterator field_begin() const;
  1667. const_field_iterator field_end() const;
  1668. oneof_iterator oneof_begin();
  1669. oneof_iterator oneof_end();
  1670. const_oneof_iterator oneof_begin() const;
  1671. const_oneof_iterator oneof_end() const;
  1672. FieldAccessor fields() { return FieldAccessor(this); }
  1673. ConstFieldAccessor fields() const { return ConstFieldAccessor(this); }
  1674. OneofAccessor oneofs() { return OneofAccessor(this); }
  1675. ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); }
  1676. private:
  1677. UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef);
  1678. ),
  1679. UPB_DEFINE_STRUCT(upb_msgdef, upb_def,
  1680. size_t selector_count;
  1681. uint32_t submsg_field_count;
  1682. // Tables for looking up fields by number and name.
  1683. upb_inttable itof; // int to field
  1684. upb_strtable ntof; // name to field
  1685. // Tables for looking up oneofs by name.
  1686. upb_strtable ntoo; // name to oneof
  1687. // Is this a map-entry message?
  1688. // TODO: set this flag properly for static descriptors; regenerate
  1689. // descriptor.upb.c.
  1690. bool map_entry;
  1691. // TODO(haberman): proper extension ranges (there can be multiple).
  1692. ));
  1693. // TODO: also support static initialization of the oneofs table. This will be
  1694. // needed if we compile in descriptors that contain oneofs.
  1695. #define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
  1696. refs, ref2s) \
  1697. { \
  1698. UPB_DEF_INIT(name, UPB_DEF_MSG, refs, ref2s), selector_count, \
  1699. submsg_field_count, itof, ntof, \
  1700. UPB_EMPTY_STRTABLE_INIT(UPB_CTYPE_PTR), false \
  1701. }
  1702. UPB_BEGIN_EXTERN_C // {
  1703. // Returns NULL if memory allocation failed.
  1704. upb_msgdef *upb_msgdef_new(const void *owner);
  1705. // From upb_refcounted.
  1706. bool upb_msgdef_isfrozen(const upb_msgdef *m);
  1707. void upb_msgdef_ref(const upb_msgdef *m, const void *owner);
  1708. void upb_msgdef_unref(const upb_msgdef *m, const void *owner);
  1709. void upb_msgdef_donateref(const upb_msgdef *m, const void *from,
  1710. const void *to);
  1711. void upb_msgdef_checkref(const upb_msgdef *m, const void *owner);
  1712. bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status);
  1713. // From upb_def.
  1714. const char *upb_msgdef_fullname(const upb_msgdef *m);
  1715. bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
  1716. upb_msgdef *upb_msgdef_dup(const upb_msgdef *m, const void *owner);
  1717. bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
  1718. upb_status *s);
  1719. bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
  1720. upb_status *s);
  1721. // Field lookup in a couple of different variations:
  1722. // - itof = int to field
  1723. // - ntof = name to field
  1724. // - ntofz = name to field, null-terminated string.
  1725. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
  1726. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  1727. size_t len);
  1728. int upb_msgdef_numfields(const upb_msgdef *m);
  1729. UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
  1730. const char *name) {
  1731. return upb_msgdef_ntof(m, name, strlen(name));
  1732. }
  1733. UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) {
  1734. return (upb_fielddef*)upb_msgdef_itof(m, i);
  1735. }
  1736. UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
  1737. const char *name, size_t len) {
  1738. return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
  1739. }
  1740. // Oneof lookup:
  1741. // - ntoo = name to oneof
  1742. // - ntooz = name to oneof, null-terminated string.
  1743. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  1744. size_t len);
  1745. int upb_msgdef_numoneofs(const upb_msgdef *m);
  1746. UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
  1747. const char *name) {
  1748. return upb_msgdef_ntoo(m, name, strlen(name));
  1749. }
  1750. UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m,
  1751. const char *name, size_t len) {
  1752. return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len);
  1753. }
  1754. void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
  1755. bool upb_msgdef_mapentry(const upb_msgdef *m);
  1756. const upb_oneofdef *upb_msgdef_findoneof(const upb_msgdef *m,
  1757. const char *name);
  1758. int upb_msgdef_numoneofs(const upb_msgdef *m);
  1759. // upb_msg_field_iter i;
  1760. // for(upb_msg_field_begin(&i, m);
  1761. // !upb_msg_field_done(&i);
  1762. // upb_msg_field_next(&i)) {
  1763. // upb_fielddef *f = upb_msg_iter_field(&i);
  1764. // // ...
  1765. // }
  1766. //
  1767. // For C we don't have separate iterators for const and non-const.
  1768. // It is the caller's responsibility to cast the upb_fielddef* to
  1769. // const if the upb_msgdef* is const.
  1770. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
  1771. void upb_msg_field_next(upb_msg_field_iter *iter);
  1772. bool upb_msg_field_done(const upb_msg_field_iter *iter);
  1773. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
  1774. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
  1775. // Similar to above, we also support iterating through the oneofs in a msgdef.
  1776. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m);
  1777. void upb_msg_oneof_next(upb_msg_oneof_iter *iter);
  1778. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
  1779. upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
  1780. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter);
  1781. UPB_END_EXTERN_C // }
  1782. /* upb::EnumDef ***************************************************************/
  1783. typedef upb_strtable_iter upb_enum_iter;
  1784. // Class that represents an enum. Its base class is upb::Def (convert with
  1785. // upb::upcast()).
  1786. UPB_DEFINE_DEF(upb::EnumDef, enumdef, ENUM,
  1787. public:
  1788. // Returns NULL if memory allocation failed.
  1789. static reffed_ptr<EnumDef> New();
  1790. // Functionality from upb::RefCounted.
  1791. bool IsFrozen() const;
  1792. void Ref(const void* owner) const;
  1793. void Unref(const void* owner) const;
  1794. void DonateRef(const void* from, const void* to) const;
  1795. void CheckRef(const void* owner) const;
  1796. // Functionality from upb::Def.
  1797. const char* full_name() const;
  1798. bool set_full_name(const char* fullname, Status* s);
  1799. bool set_full_name(const std::string& fullname, Status* s);
  1800. // Call to freeze this EnumDef.
  1801. bool Freeze(Status* s);
  1802. // The value that is used as the default when no field default is specified.
  1803. // If not set explicitly, the first value that was added will be used.
  1804. // The default value must be a member of the enum.
  1805. // Requires that value_count() > 0.
  1806. int32_t default_value() const;
  1807. // Sets the default value. If this value is not valid, returns false and an
  1808. // error message in status.
  1809. bool set_default_value(int32_t val, Status* status);
  1810. // Returns the number of values currently defined in the enum. Note that
  1811. // multiple names can refer to the same number, so this may be greater than
  1812. // the total number of unique numbers.
  1813. int value_count() const;
  1814. // Adds a single name/number pair to the enum. Fails if this name has
  1815. // already been used by another value.
  1816. bool AddValue(const char* name, int32_t num, Status* status);
  1817. bool AddValue(const std::string& name, int32_t num, Status* status);
  1818. // Lookups from name to integer, returning true if found.
  1819. bool FindValueByName(const char* name, int32_t* num) const;
  1820. // Finds the name corresponding to the given number, or NULL if none was
  1821. // found. If more than one name corresponds to this number, returns the
  1822. // first one that was added.
  1823. const char* FindValueByNumber(int32_t num) const;
  1824. // Returns a new EnumDef with all the same values. The new EnumDef will be
  1825. // owned by the given owner.
  1826. EnumDef* Dup(const void* owner) const;
  1827. // Iteration over name/value pairs. The order is undefined.
  1828. // Adding an enum val invalidates any iterators.
  1829. //
  1830. // TODO: make compatible with range-for, with elements as pairs?
  1831. class Iterator {
  1832. public:
  1833. explicit Iterator(const EnumDef*);
  1834. int32_t number();
  1835. const char *name();
  1836. bool Done();
  1837. void Next();
  1838. private:
  1839. upb_enum_iter iter_;
  1840. };
  1841. private:
  1842. UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef);
  1843. ,
  1844. UPB_DEFINE_STRUCT(upb_enumdef, upb_def,
  1845. upb_strtable ntoi;
  1846. upb_inttable iton;
  1847. int32_t defaultval;
  1848. ));
  1849. #define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
  1850. { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntoi, iton, defaultval }
  1851. UPB_BEGIN_EXTERN_C // {
  1852. // Native C API.
  1853. upb_enumdef *upb_enumdef_new(const void *owner);
  1854. upb_enumdef *upb_enumdef_dup(const upb_enumdef *e, const void *owner);
  1855. // From upb_refcounted.
  1856. void upb_enumdef_unref(const upb_enumdef *e, const void *owner);
  1857. bool upb_enumdef_isfrozen(const upb_enumdef *e);
  1858. void upb_enumdef_ref(const upb_enumdef *e, const void *owner);
  1859. void upb_enumdef_donateref(const upb_enumdef *m, const void *from,
  1860. const void *to);
  1861. void upb_enumdef_checkref(const upb_enumdef *e, const void *owner);
  1862. bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status);
  1863. // From upb_def.
  1864. const char *upb_enumdef_fullname(const upb_enumdef *e);
  1865. bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
  1866. upb_status *s);
  1867. int32_t upb_enumdef_default(const upb_enumdef *e);
  1868. bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s);
  1869. int upb_enumdef_numvals(const upb_enumdef *e);
  1870. bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
  1871. upb_status *status);
  1872. // Enum lookups:
  1873. // - ntoi: look up a name with specified length.
  1874. // - ntoiz: look up a name provided as a null-terminated string.
  1875. // - iton: look up an integer, returning the name as a null-terminated string.
  1876. bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
  1877. int32_t *num);
  1878. UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
  1879. const char *name, int32_t *num) {
  1880. return upb_enumdef_ntoi(e, name, strlen(name), num);
  1881. }
  1882. const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
  1883. // upb_enum_iter i;
  1884. // for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
  1885. // // ...
  1886. // }
  1887. void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
  1888. void upb_enum_next(upb_enum_iter *iter);
  1889. bool upb_enum_done(upb_enum_iter *iter);
  1890. const char *upb_enum_iter_name(upb_enum_iter *iter);
  1891. int32_t upb_enum_iter_number(upb_enum_iter *iter);
  1892. UPB_END_EXTERN_C // }
  1893. /* upb::OneofDef **************************************************************/
  1894. typedef upb_inttable_iter upb_oneof_iter;
  1895. // Class that represents a oneof. Its base class is upb::Def (convert with
  1896. // upb::upcast()).
  1897. UPB_DEFINE_DEF(upb::OneofDef, oneofdef, ONEOF, UPB_QUOTE(
  1898. public:
  1899. // Returns NULL if memory allocation failed.
  1900. static reffed_ptr<OneofDef> New();
  1901. // Functionality from upb::RefCounted.
  1902. bool IsFrozen() const;
  1903. void Ref(const void* owner) const;
  1904. void Unref(const void* owner) const;
  1905. void DonateRef(const void* from, const void* to) const;
  1906. void CheckRef(const void* owner) const;
  1907. // Functionality from upb::Def.
  1908. const char* full_name() const;
  1909. // Returns the MessageDef that owns this OneofDef.
  1910. const MessageDef* containing_type() const;
  1911. // Returns the name of this oneof. This is the name used to look up the oneof
  1912. // by name once added to a message def.
  1913. const char* name() const;
  1914. bool set_name(const char* name, Status* s);
  1915. // Returns the number of fields currently defined in the oneof.
  1916. int field_count() const;
  1917. // Adds a field to the oneof. The field must not have been added to any other
  1918. // oneof or msgdef. If the oneof is not yet part of a msgdef, then when the
  1919. // oneof is eventually added to a msgdef, all fields added to the oneof will
  1920. // also be added to the msgdef at that time. If the oneof is already part of a
  1921. // msgdef, the field must either be a part of that msgdef already, or must not
  1922. // be a part of any msgdef; in the latter case, the field is added to the
  1923. // msgdef as a part of this operation.
  1924. //
  1925. // The field may only have an OPTIONAL label, never REQUIRED or REPEATED.
  1926. //
  1927. // If |f| is already part of this MessageDef, this method performs no action
  1928. // and returns true (success). Thus, this method is idempotent.
  1929. bool AddField(FieldDef* field, Status* s);
  1930. bool AddField(const reffed_ptr<FieldDef>& field, Status* s);
  1931. // Looks up by name.
  1932. const FieldDef* FindFieldByName(const char* name, size_t len) const;
  1933. FieldDef* FindFieldByName(const char* name, size_t len);
  1934. const FieldDef* FindFieldByName(const char* name) const {
  1935. return FindFieldByName(name, strlen(name));
  1936. }
  1937. FieldDef* FindFieldByName(const char* name) {
  1938. return FindFieldByName(name, strlen(name));
  1939. }
  1940. template <class T>
  1941. FieldDef* FindFieldByName(const T& str) {
  1942. return FindFieldByName(str.c_str(), str.size());
  1943. }
  1944. template <class T>
  1945. const FieldDef* FindFieldByName(const T& str) const {
  1946. return FindFieldByName(str.c_str(), str.size());
  1947. }
  1948. // Looks up by tag number.
  1949. const FieldDef* FindFieldByNumber(uint32_t num) const;
  1950. // Returns a new OneofDef with all the same fields. The OneofDef will be owned
  1951. // by the given owner.
  1952. OneofDef* Dup(const void* owner) const;
  1953. // Iteration over fields. The order is undefined.
  1954. class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
  1955. public:
  1956. explicit iterator(OneofDef* md);
  1957. static iterator end(OneofDef* md);
  1958. void operator++();
  1959. FieldDef* operator*() const;
  1960. bool operator!=(const iterator& other) const;
  1961. bool operator==(const iterator& other) const;
  1962. private:
  1963. upb_oneof_iter iter_;
  1964. };
  1965. class const_iterator
  1966. : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
  1967. public:
  1968. explicit const_iterator(const OneofDef* md);
  1969. static const_iterator end(const OneofDef* md);
  1970. void operator++();
  1971. const FieldDef* operator*() const;
  1972. bool operator!=(const const_iterator& other) const;
  1973. bool operator==(const const_iterator& other) const;
  1974. private:
  1975. upb_oneof_iter iter_;
  1976. };
  1977. iterator begin();
  1978. iterator end();
  1979. const_iterator begin() const;
  1980. const_iterator end() const;
  1981. private:
  1982. UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef);
  1983. ),
  1984. UPB_DEFINE_STRUCT(upb_oneofdef, upb_def,
  1985. upb_strtable ntof;
  1986. upb_inttable itof;
  1987. const upb_msgdef *parent;
  1988. ));
  1989. #define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
  1990. { UPB_DEF_INIT(name, UPB_DEF_ENUM, refs, ref2s), ntof, itof }
  1991. UPB_BEGIN_EXTERN_C // {
  1992. // Native C API.
  1993. upb_oneofdef *upb_oneofdef_new(const void *owner);
  1994. upb_oneofdef *upb_oneofdef_dup(const upb_oneofdef *o, const void *owner);
  1995. // From upb_refcounted.
  1996. void upb_oneofdef_unref(const upb_oneofdef *o, const void *owner);
  1997. bool upb_oneofdef_isfrozen(const upb_oneofdef *e);
  1998. void upb_oneofdef_ref(const upb_oneofdef *o, const void *owner);
  1999. void upb_oneofdef_donateref(const upb_oneofdef *m, const void *from,
  2000. const void *to);
  2001. void upb_oneofdef_checkref(const upb_oneofdef *o, const void *owner);
  2002. const char *upb_oneofdef_name(const upb_oneofdef *o);
  2003. bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s);
  2004. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
  2005. int upb_oneofdef_numfields(const upb_oneofdef *o);
  2006. bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
  2007. const void *ref_donor,
  2008. upb_status *s);
  2009. // Oneof lookups:
  2010. // - ntof: look up a field by name.
  2011. // - ntofz: look up a field by name (as a null-terminated string).
  2012. // - itof: look up a field by number.
  2013. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  2014. const char *name, size_t length);
  2015. UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
  2016. const char *name) {
  2017. return upb_oneofdef_ntof(o, name, strlen(name));
  2018. }
  2019. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
  2020. // upb_oneof_iter i;
  2021. // for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
  2022. // // ...
  2023. // }
  2024. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
  2025. void upb_oneof_next(upb_oneof_iter *iter);
  2026. bool upb_oneof_done(upb_oneof_iter *iter);
  2027. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
  2028. void upb_oneof_iter_setdone(upb_oneof_iter *iter);
  2029. UPB_END_EXTERN_C // }
  2030. #ifdef __cplusplus
  2031. UPB_INLINE const char* upb_safecstr(const std::string& str) {
  2032. assert(str.size() == std::strlen(str.c_str()));
  2033. return str.c_str();
  2034. }
  2035. // Inline C++ wrappers.
  2036. namespace upb {
  2037. inline Def* Def::Dup(const void* owner) const {
  2038. return upb_def_dup(this, owner);
  2039. }
  2040. inline bool Def::IsFrozen() const { return upb_def_isfrozen(this); }
  2041. inline void Def::Ref(const void* owner) const { upb_def_ref(this, owner); }
  2042. inline void Def::Unref(const void* owner) const { upb_def_unref(this, owner); }
  2043. inline void Def::DonateRef(const void* from, const void* to) const {
  2044. upb_def_donateref(this, from, to);
  2045. }
  2046. inline void Def::CheckRef(const void* owner) const {
  2047. upb_def_checkref(this, owner);
  2048. }
  2049. inline Def::Type Def::def_type() const { return upb_def_type(this); }
  2050. inline const char* Def::full_name() const { return upb_def_fullname(this); }
  2051. inline bool Def::set_full_name(const char* fullname, Status* s) {
  2052. return upb_def_setfullname(this, fullname, s);
  2053. }
  2054. inline bool Def::set_full_name(const std::string& fullname, Status* s) {
  2055. return upb_def_setfullname(this, upb_safecstr(fullname), s);
  2056. }
  2057. inline bool Def::Freeze(Def* const* defs, int n, Status* status) {
  2058. return upb_def_freeze(defs, n, status);
  2059. }
  2060. inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) {
  2061. return upb_def_freeze((Def* const*)&defs[0], defs.size(), status);
  2062. }
  2063. inline bool FieldDef::CheckType(int32_t val) {
  2064. return upb_fielddef_checktype(val);
  2065. }
  2066. inline bool FieldDef::CheckLabel(int32_t val) {
  2067. return upb_fielddef_checklabel(val);
  2068. }
  2069. inline bool FieldDef::CheckDescriptorType(int32_t val) {
  2070. return upb_fielddef_checkdescriptortype(val);
  2071. }
  2072. inline bool FieldDef::CheckIntegerFormat(int32_t val) {
  2073. return upb_fielddef_checkintfmt(val);
  2074. }
  2075. inline FieldDef::Type FieldDef::ConvertType(int32_t val) {
  2076. assert(CheckType(val));
  2077. return static_cast<FieldDef::Type>(val);
  2078. }
  2079. inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) {
  2080. assert(CheckLabel(val));
  2081. return static_cast<FieldDef::Label>(val);
  2082. }
  2083. inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) {
  2084. assert(CheckDescriptorType(val));
  2085. return static_cast<FieldDef::DescriptorType>(val);
  2086. }
  2087. inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) {
  2088. assert(CheckIntegerFormat(val));
  2089. return static_cast<FieldDef::IntegerFormat>(val);
  2090. }
  2091. inline reffed_ptr<FieldDef> FieldDef::New() {
  2092. upb_fielddef *f = upb_fielddef_new(&f);
  2093. return reffed_ptr<FieldDef>(f, &f);
  2094. }
  2095. inline FieldDef* FieldDef::Dup(const void* owner) const {
  2096. return upb_fielddef_dup(this, owner);
  2097. }
  2098. inline bool FieldDef::IsFrozen() const { return upb_fielddef_isfrozen(this); }
  2099. inline void FieldDef::Ref(const void* owner) const {
  2100. upb_fielddef_ref(this, owner);
  2101. }
  2102. inline void FieldDef::Unref(const void* owner) const {
  2103. upb_fielddef_unref(this, owner);
  2104. }
  2105. inline void FieldDef::DonateRef(const void* from, const void* to) const {
  2106. upb_fielddef_donateref(this, from, to);
  2107. }
  2108. inline void FieldDef::CheckRef(const void* owner) const {
  2109. upb_fielddef_checkref(this, owner);
  2110. }
  2111. inline const char* FieldDef::full_name() const {
  2112. return upb_fielddef_fullname(this);
  2113. }
  2114. inline bool FieldDef::set_full_name(const char* fullname, Status* s) {
  2115. return upb_fielddef_setfullname(this, fullname, s);
  2116. }
  2117. inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) {
  2118. return upb_fielddef_setfullname(this, upb_safecstr(fullname), s);
  2119. }
  2120. inline bool FieldDef::type_is_set() const {
  2121. return upb_fielddef_typeisset(this);
  2122. }
  2123. inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); }
  2124. inline FieldDef::DescriptorType FieldDef::descriptor_type() const {
  2125. return upb_fielddef_descriptortype(this);
  2126. }
  2127. inline FieldDef::Label FieldDef::label() const {
  2128. return upb_fielddef_label(this);
  2129. }
  2130. inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); }
  2131. inline const char* FieldDef::name() const { return upb_fielddef_name(this); }
  2132. inline bool FieldDef::is_extension() const {
  2133. return upb_fielddef_isextension(this);
  2134. }
  2135. inline bool FieldDef::lazy() const {
  2136. return upb_fielddef_lazy(this);
  2137. }
  2138. inline void FieldDef::set_lazy(bool lazy) {
  2139. upb_fielddef_setlazy(this, lazy);
  2140. }
  2141. inline bool FieldDef::packed() const {
  2142. return upb_fielddef_packed(this);
  2143. }
  2144. inline void FieldDef::set_packed(bool packed) {
  2145. upb_fielddef_setpacked(this, packed);
  2146. }
  2147. inline const MessageDef* FieldDef::containing_type() const {
  2148. return upb_fielddef_containingtype(this);
  2149. }
  2150. inline const OneofDef* FieldDef::containing_oneof() const {
  2151. return upb_fielddef_containingoneof(this);
  2152. }
  2153. inline const char* FieldDef::containing_type_name() {
  2154. return upb_fielddef_containingtypename(this);
  2155. }
  2156. inline bool FieldDef::set_number(uint32_t number, Status* s) {
  2157. return upb_fielddef_setnumber(this, number, s);
  2158. }
  2159. inline bool FieldDef::set_name(const char *name, Status* s) {
  2160. return upb_fielddef_setname(this, name, s);
  2161. }
  2162. inline bool FieldDef::set_name(const std::string& name, Status* s) {
  2163. return upb_fielddef_setname(this, upb_safecstr(name), s);
  2164. }
  2165. inline bool FieldDef::set_containing_type_name(const char *name, Status* s) {
  2166. return upb_fielddef_setcontainingtypename(this, name, s);
  2167. }
  2168. inline bool FieldDef::set_containing_type_name(const std::string &name,
  2169. Status *s) {
  2170. return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s);
  2171. }
  2172. inline void FieldDef::set_type(upb_fieldtype_t type) {
  2173. upb_fielddef_settype(this, type);
  2174. }
  2175. inline void FieldDef::set_is_extension(bool is_extension) {
  2176. upb_fielddef_setisextension(this, is_extension);
  2177. }
  2178. inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) {
  2179. upb_fielddef_setdescriptortype(this, type);
  2180. }
  2181. inline void FieldDef::set_label(upb_label_t label) {
  2182. upb_fielddef_setlabel(this, label);
  2183. }
  2184. inline bool FieldDef::IsSubMessage() const {
  2185. return upb_fielddef_issubmsg(this);
  2186. }
  2187. inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
  2188. inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
  2189. inline int64_t FieldDef::default_int64() const {
  2190. return upb_fielddef_defaultint64(this);
  2191. }
  2192. inline int32_t FieldDef::default_int32() const {
  2193. return upb_fielddef_defaultint32(this);
  2194. }
  2195. inline uint64_t FieldDef::default_uint64() const {
  2196. return upb_fielddef_defaultuint64(this);
  2197. }
  2198. inline uint32_t FieldDef::default_uint32() const {
  2199. return upb_fielddef_defaultuint32(this);
  2200. }
  2201. inline bool FieldDef::default_bool() const {
  2202. return upb_fielddef_defaultbool(this);
  2203. }
  2204. inline float FieldDef::default_float() const {
  2205. return upb_fielddef_defaultfloat(this);
  2206. }
  2207. inline double FieldDef::default_double() const {
  2208. return upb_fielddef_defaultdouble(this);
  2209. }
  2210. inline const char* FieldDef::default_string(size_t* len) const {
  2211. return upb_fielddef_defaultstr(this, len);
  2212. }
  2213. inline void FieldDef::set_default_int64(int64_t value) {
  2214. upb_fielddef_setdefaultint64(this, value);
  2215. }
  2216. inline void FieldDef::set_default_int32(int32_t value) {
  2217. upb_fielddef_setdefaultint32(this, value);
  2218. }
  2219. inline void FieldDef::set_default_uint64(uint64_t value) {
  2220. upb_fielddef_setdefaultuint64(this, value);
  2221. }
  2222. inline void FieldDef::set_default_uint32(uint32_t value) {
  2223. upb_fielddef_setdefaultuint32(this, value);
  2224. }
  2225. inline void FieldDef::set_default_bool(bool value) {
  2226. upb_fielddef_setdefaultbool(this, value);
  2227. }
  2228. inline void FieldDef::set_default_float(float value) {
  2229. upb_fielddef_setdefaultfloat(this, value);
  2230. }
  2231. inline void FieldDef::set_default_double(double value) {
  2232. upb_fielddef_setdefaultdouble(this, value);
  2233. }
  2234. inline bool FieldDef::set_default_string(const void *str, size_t len,
  2235. Status *s) {
  2236. return upb_fielddef_setdefaultstr(this, str, len, s);
  2237. }
  2238. inline bool FieldDef::set_default_string(const std::string& str, Status* s) {
  2239. return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s);
  2240. }
  2241. inline void FieldDef::set_default_cstr(const char* str, Status* s) {
  2242. return upb_fielddef_setdefaultcstr(this, str, s);
  2243. }
  2244. inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); }
  2245. inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); }
  2246. inline const MessageDef *FieldDef::message_subdef() const {
  2247. return upb_fielddef_msgsubdef(this);
  2248. }
  2249. inline const EnumDef *FieldDef::enum_subdef() const {
  2250. return upb_fielddef_enumsubdef(this);
  2251. }
  2252. inline const char* FieldDef::subdef_name() const {
  2253. return upb_fielddef_subdefname(this);
  2254. }
  2255. inline bool FieldDef::set_subdef(const Def* subdef, Status* s) {
  2256. return upb_fielddef_setsubdef(this, subdef, s);
  2257. }
  2258. inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) {
  2259. return upb_fielddef_setenumsubdef(this, subdef, s);
  2260. }
  2261. inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) {
  2262. return upb_fielddef_setmsgsubdef(this, subdef, s);
  2263. }
  2264. inline bool FieldDef::set_subdef_name(const char* name, Status* s) {
  2265. return upb_fielddef_setsubdefname(this, name, s);
  2266. }
  2267. inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) {
  2268. return upb_fielddef_setsubdefname(this, upb_safecstr(name), s);
  2269. }
  2270. inline reffed_ptr<MessageDef> MessageDef::New() {
  2271. upb_msgdef *m = upb_msgdef_new(&m);
  2272. return reffed_ptr<MessageDef>(m, &m);
  2273. }
  2274. inline bool MessageDef::IsFrozen() const { return upb_msgdef_isfrozen(this); }
  2275. inline void MessageDef::Ref(const void* owner) const {
  2276. return upb_msgdef_ref(this, owner);
  2277. }
  2278. inline void MessageDef::Unref(const void* owner) const {
  2279. return upb_msgdef_unref(this, owner);
  2280. }
  2281. inline void MessageDef::DonateRef(const void* from, const void* to) const {
  2282. return upb_msgdef_donateref(this, from, to);
  2283. }
  2284. inline void MessageDef::CheckRef(const void* owner) const {
  2285. return upb_msgdef_checkref(this, owner);
  2286. }
  2287. inline const char *MessageDef::full_name() const {
  2288. return upb_msgdef_fullname(this);
  2289. }
  2290. inline bool MessageDef::set_full_name(const char* fullname, Status* s) {
  2291. return upb_msgdef_setfullname(this, fullname, s);
  2292. }
  2293. inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) {
  2294. return upb_msgdef_setfullname(this, upb_safecstr(fullname), s);
  2295. }
  2296. inline bool MessageDef::Freeze(Status* status) {
  2297. return upb_msgdef_freeze(this, status);
  2298. }
  2299. inline int MessageDef::field_count() const {
  2300. return upb_msgdef_numfields(this);
  2301. }
  2302. inline int MessageDef::oneof_count() const {
  2303. return upb_msgdef_numoneofs(this);
  2304. }
  2305. inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
  2306. return upb_msgdef_addfield(this, f, NULL, s);
  2307. }
  2308. inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
  2309. return upb_msgdef_addfield(this, f.get(), NULL, s);
  2310. }
  2311. inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) {
  2312. return upb_msgdef_addoneof(this, o, NULL, s);
  2313. }
  2314. inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) {
  2315. return upb_msgdef_addoneof(this, o.get(), NULL, s);
  2316. }
  2317. inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
  2318. return upb_msgdef_itof_mutable(this, number);
  2319. }
  2320. inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) {
  2321. return upb_msgdef_ntof_mutable(this, name, len);
  2322. }
  2323. inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const {
  2324. return upb_msgdef_itof(this, number);
  2325. }
  2326. inline const FieldDef *MessageDef::FindFieldByName(const char *name,
  2327. size_t len) const {
  2328. return upb_msgdef_ntof(this, name, len);
  2329. }
  2330. inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) {
  2331. return upb_msgdef_ntoo_mutable(this, name, len);
  2332. }
  2333. inline const OneofDef* MessageDef::FindOneofByName(const char* name,
  2334. size_t len) const {
  2335. return upb_msgdef_ntoo(this, name, len);
  2336. }
  2337. inline MessageDef* MessageDef::Dup(const void *owner) const {
  2338. return upb_msgdef_dup(this, owner);
  2339. }
  2340. inline void MessageDef::setmapentry(bool map_entry) {
  2341. upb_msgdef_setmapentry(this, map_entry);
  2342. }
  2343. inline bool MessageDef::mapentry() const {
  2344. return upb_msgdef_mapentry(this);
  2345. }
  2346. inline MessageDef::field_iterator MessageDef::field_begin() {
  2347. return field_iterator(this);
  2348. }
  2349. inline MessageDef::field_iterator MessageDef::field_end() {
  2350. return field_iterator::end(this);
  2351. }
  2352. inline MessageDef::const_field_iterator MessageDef::field_begin() const {
  2353. return const_field_iterator(this);
  2354. }
  2355. inline MessageDef::const_field_iterator MessageDef::field_end() const {
  2356. return const_field_iterator::end(this);
  2357. }
  2358. inline MessageDef::oneof_iterator MessageDef::oneof_begin() {
  2359. return oneof_iterator(this);
  2360. }
  2361. inline MessageDef::oneof_iterator MessageDef::oneof_end() {
  2362. return oneof_iterator::end(this);
  2363. }
  2364. inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const {
  2365. return const_oneof_iterator(this);
  2366. }
  2367. inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const {
  2368. return const_oneof_iterator::end(this);
  2369. }
  2370. inline MessageDef::field_iterator::field_iterator(MessageDef* md) {
  2371. upb_msg_field_begin(&iter_, md);
  2372. }
  2373. inline MessageDef::field_iterator MessageDef::field_iterator::end(
  2374. MessageDef* md) {
  2375. MessageDef::field_iterator iter(md);
  2376. upb_msg_field_iter_setdone(&iter.iter_);
  2377. return iter;
  2378. }
  2379. inline FieldDef* MessageDef::field_iterator::operator*() const {
  2380. return upb_msg_iter_field(&iter_);
  2381. }
  2382. inline void MessageDef::field_iterator::operator++() {
  2383. return upb_msg_field_next(&iter_);
  2384. }
  2385. inline bool MessageDef::field_iterator::operator==(
  2386. const field_iterator &other) const {
  2387. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  2388. }
  2389. inline bool MessageDef::field_iterator::operator!=(
  2390. const field_iterator &other) const {
  2391. return !(*this == other);
  2392. }
  2393. inline MessageDef::const_field_iterator::const_field_iterator(
  2394. const MessageDef* md) {
  2395. upb_msg_field_begin(&iter_, md);
  2396. }
  2397. inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end(
  2398. const MessageDef *md) {
  2399. MessageDef::const_field_iterator iter(md);
  2400. upb_msg_field_iter_setdone(&iter.iter_);
  2401. return iter;
  2402. }
  2403. inline const FieldDef* MessageDef::const_field_iterator::operator*() const {
  2404. return upb_msg_iter_field(&iter_);
  2405. }
  2406. inline void MessageDef::const_field_iterator::operator++() {
  2407. return upb_msg_field_next(&iter_);
  2408. }
  2409. inline bool MessageDef::const_field_iterator::operator==(
  2410. const const_field_iterator &other) const {
  2411. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  2412. }
  2413. inline bool MessageDef::const_field_iterator::operator!=(
  2414. const const_field_iterator &other) const {
  2415. return !(*this == other);
  2416. }
  2417. inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) {
  2418. upb_msg_oneof_begin(&iter_, md);
  2419. }
  2420. inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end(
  2421. MessageDef* md) {
  2422. MessageDef::oneof_iterator iter(md);
  2423. upb_msg_oneof_iter_setdone(&iter.iter_);
  2424. return iter;
  2425. }
  2426. inline OneofDef* MessageDef::oneof_iterator::operator*() const {
  2427. return upb_msg_iter_oneof(&iter_);
  2428. }
  2429. inline void MessageDef::oneof_iterator::operator++() {
  2430. return upb_msg_oneof_next(&iter_);
  2431. }
  2432. inline bool MessageDef::oneof_iterator::operator==(
  2433. const oneof_iterator &other) const {
  2434. return upb_strtable_iter_isequal(&iter_, &other.iter_);
  2435. }
  2436. inline bool MessageDef::oneof_iterator::operator!=(
  2437. const oneof_iterator &other) const {
  2438. return !(*this == other);
  2439. }
  2440. inline MessageDef::const_oneof_iterator::const_oneof_iterator(
  2441. const MessageDef* md) {
  2442. upb_msg_oneof_begin(&iter_, md);
  2443. }
  2444. inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end(
  2445. const MessageDef *md) {
  2446. MessageDef::const_oneof_iterator iter(md);
  2447. upb_msg_oneof_iter_setdone(&iter.iter_);
  2448. return iter;
  2449. }
  2450. inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const {
  2451. return upb_msg_iter_oneof(&iter_);
  2452. }
  2453. inline void MessageDef::const_oneof_iterator::operator++() {
  2454. return upb_msg_oneof_next(&iter_);
  2455. }
  2456. inline bool MessageDef::const_oneof_iterator::operator==(
  2457. const const_oneof_iterator &other) const {
  2458. return upb_strtable_iter_isequal(&iter_, &other.iter_);
  2459. }
  2460. inline bool MessageDef::const_oneof_iterator::operator!=(
  2461. const const_oneof_iterator &other) const {
  2462. return !(*this == other);
  2463. }
  2464. inline reffed_ptr<EnumDef> EnumDef::New() {
  2465. upb_enumdef *e = upb_enumdef_new(&e);
  2466. return reffed_ptr<EnumDef>(e, &e);
  2467. }
  2468. inline bool EnumDef::IsFrozen() const { return upb_enumdef_isfrozen(this); }
  2469. inline void EnumDef::Ref(const void* owner) const {
  2470. return upb_enumdef_ref(this, owner);
  2471. }
  2472. inline void EnumDef::Unref(const void* owner) const {
  2473. return upb_enumdef_unref(this, owner);
  2474. }
  2475. inline void EnumDef::DonateRef(const void* from, const void* to) const {
  2476. return upb_enumdef_donateref(this, from, to);
  2477. }
  2478. inline void EnumDef::CheckRef(const void* owner) const {
  2479. return upb_enumdef_checkref(this, owner);
  2480. }
  2481. inline const char* EnumDef::full_name() const {
  2482. return upb_enumdef_fullname(this);
  2483. }
  2484. inline bool EnumDef::set_full_name(const char* fullname, Status* s) {
  2485. return upb_enumdef_setfullname(this, fullname, s);
  2486. }
  2487. inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) {
  2488. return upb_enumdef_setfullname(this, upb_safecstr(fullname), s);
  2489. }
  2490. inline bool EnumDef::Freeze(Status* status) {
  2491. return upb_enumdef_freeze(this, status);
  2492. }
  2493. inline int32_t EnumDef::default_value() const {
  2494. return upb_enumdef_default(this);
  2495. }
  2496. inline bool EnumDef::set_default_value(int32_t val, Status* status) {
  2497. return upb_enumdef_setdefault(this, val, status);
  2498. }
  2499. inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); }
  2500. inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) {
  2501. return upb_enumdef_addval(this, name, num, status);
  2502. }
  2503. inline bool EnumDef::AddValue(const std::string& name, int32_t num,
  2504. Status* status) {
  2505. return upb_enumdef_addval(this, upb_safecstr(name), num, status);
  2506. }
  2507. inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const {
  2508. return upb_enumdef_ntoiz(this, name, num);
  2509. }
  2510. inline const char* EnumDef::FindValueByNumber(int32_t num) const {
  2511. return upb_enumdef_iton(this, num);
  2512. }
  2513. inline EnumDef* EnumDef::Dup(const void* owner) const {
  2514. return upb_enumdef_dup(this, owner);
  2515. }
  2516. inline EnumDef::Iterator::Iterator(const EnumDef* e) {
  2517. upb_enum_begin(&iter_, e);
  2518. }
  2519. inline int32_t EnumDef::Iterator::number() {
  2520. return upb_enum_iter_number(&iter_);
  2521. }
  2522. inline const char* EnumDef::Iterator::name() {
  2523. return upb_enum_iter_name(&iter_);
  2524. }
  2525. inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
  2526. inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
  2527. inline reffed_ptr<OneofDef> OneofDef::New() {
  2528. upb_oneofdef *o = upb_oneofdef_new(&o);
  2529. return reffed_ptr<OneofDef>(o, &o);
  2530. }
  2531. inline bool OneofDef::IsFrozen() const { return upb_oneofdef_isfrozen(this); }
  2532. inline void OneofDef::Ref(const void* owner) const {
  2533. return upb_oneofdef_ref(this, owner);
  2534. }
  2535. inline void OneofDef::Unref(const void* owner) const {
  2536. return upb_oneofdef_unref(this, owner);
  2537. }
  2538. inline void OneofDef::DonateRef(const void* from, const void* to) const {
  2539. return upb_oneofdef_donateref(this, from, to);
  2540. }
  2541. inline void OneofDef::CheckRef(const void* owner) const {
  2542. return upb_oneofdef_checkref(this, owner);
  2543. }
  2544. inline const char* OneofDef::full_name() const {
  2545. return upb_oneofdef_name(this);
  2546. }
  2547. inline const MessageDef* OneofDef::containing_type() const {
  2548. return upb_oneofdef_containingtype(this);
  2549. }
  2550. inline const char* OneofDef::name() const {
  2551. return upb_oneofdef_name(this);
  2552. }
  2553. inline bool OneofDef::set_name(const char* name, Status* s) {
  2554. return upb_oneofdef_setname(this, name, s);
  2555. }
  2556. inline int OneofDef::field_count() const {
  2557. return upb_oneofdef_numfields(this);
  2558. }
  2559. inline bool OneofDef::AddField(FieldDef* field, Status* s) {
  2560. return upb_oneofdef_addfield(this, field, NULL, s);
  2561. }
  2562. inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) {
  2563. return upb_oneofdef_addfield(this, field.get(), NULL, s);
  2564. }
  2565. inline const FieldDef* OneofDef::FindFieldByName(const char* name,
  2566. size_t len) const {
  2567. return upb_oneofdef_ntof(this, name, len);
  2568. }
  2569. inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const {
  2570. return upb_oneofdef_itof(this, num);
  2571. }
  2572. inline OneofDef::iterator OneofDef::begin() { return iterator(this); }
  2573. inline OneofDef::iterator OneofDef::end() { return iterator::end(this); }
  2574. inline OneofDef::const_iterator OneofDef::begin() const {
  2575. return const_iterator(this);
  2576. }
  2577. inline OneofDef::const_iterator OneofDef::end() const {
  2578. return const_iterator::end(this);
  2579. }
  2580. inline OneofDef::iterator::iterator(OneofDef* o) {
  2581. upb_oneof_begin(&iter_, o);
  2582. }
  2583. inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) {
  2584. OneofDef::iterator iter(o);
  2585. upb_oneof_iter_setdone(&iter.iter_);
  2586. return iter;
  2587. }
  2588. inline FieldDef* OneofDef::iterator::operator*() const {
  2589. return upb_oneof_iter_field(&iter_);
  2590. }
  2591. inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); }
  2592. inline bool OneofDef::iterator::operator==(const iterator &other) const {
  2593. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  2594. }
  2595. inline bool OneofDef::iterator::operator!=(const iterator &other) const {
  2596. return !(*this == other);
  2597. }
  2598. inline OneofDef::const_iterator::const_iterator(const OneofDef* md) {
  2599. upb_oneof_begin(&iter_, md);
  2600. }
  2601. inline OneofDef::const_iterator OneofDef::const_iterator::end(
  2602. const OneofDef *md) {
  2603. OneofDef::const_iterator iter(md);
  2604. upb_oneof_iter_setdone(&iter.iter_);
  2605. return iter;
  2606. }
  2607. inline const FieldDef* OneofDef::const_iterator::operator*() const {
  2608. return upb_msg_iter_field(&iter_);
  2609. }
  2610. inline void OneofDef::const_iterator::operator++() {
  2611. return upb_oneof_next(&iter_);
  2612. }
  2613. inline bool OneofDef::const_iterator::operator==(
  2614. const const_iterator &other) const {
  2615. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  2616. }
  2617. inline bool OneofDef::const_iterator::operator!=(
  2618. const const_iterator &other) const {
  2619. return !(*this == other);
  2620. }
  2621. } // namespace upb
  2622. #endif
  2623. #undef UPB_DEFINE_DEF
  2624. #undef UPB_DEF_CASTS
  2625. #undef UPB_CPP_CASTS
  2626. #endif /* UPB_DEF_H_ */
  2627. // This file contains accessors for a set of compiled-in defs.
  2628. // Note that unlike Google's protobuf, it does *not* define
  2629. // generated classes or any other kind of data structure for
  2630. // actually storing protobufs. It only contains *defs* which
  2631. // let you reflect over a protobuf *schema*.
  2632. //
  2633. // This file was generated by upbc (the upb compiler).
  2634. // Do not edit -- your changes will be discarded when the file is
  2635. // regenerated.
  2636. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
  2637. #define GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
  2638. /*
  2639. * upb - a minimalist implementation of protocol buffers.
  2640. *
  2641. * Copyright (c) 2009-2012 Google Inc. See LICENSE for details.
  2642. * Author: Josh Haberman <jhaberman@gmail.com>
  2643. *
  2644. * A symtab (symbol table) stores a name->def map of upb_defs. Clients could
  2645. * always create such tables themselves, but upb_symtab has logic for resolving
  2646. * symbolic references, and in particular, for keeping a whole set of consistent
  2647. * defs when replacing some subset of those defs. This logic is nontrivial.
  2648. *
  2649. * This is a mixed C/C++ interface that offers a full API to both languages.
  2650. * See the top-level README for more information.
  2651. */
  2652. #ifndef UPB_SYMTAB_H_
  2653. #define UPB_SYMTAB_H_
  2654. #ifdef __cplusplus
  2655. #include <vector>
  2656. namespace upb { class SymbolTable; }
  2657. #endif
  2658. UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab);
  2659. typedef struct {
  2660. UPB_PRIVATE_FOR_CPP
  2661. upb_strtable_iter iter;
  2662. upb_deftype_t type;
  2663. } upb_symtab_iter;
  2664. // Non-const methods in upb::SymbolTable are NOT thread-safe.
  2665. UPB_DEFINE_CLASS1(upb::SymbolTable, upb::RefCounted,
  2666. public:
  2667. // Returns a new symbol table with a single ref owned by "owner."
  2668. // Returns NULL if memory allocation failed.
  2669. static reffed_ptr<SymbolTable> New();
  2670. // Functionality from upb::RefCounted.
  2671. bool IsFrozen() const;
  2672. void Ref(const void* owner) const;
  2673. void Unref(const void* owner) const;
  2674. void DonateRef(const void *from, const void *to) const;
  2675. void CheckRef(const void *owner) const;
  2676. // For all lookup functions, the returned pointer is not owned by the
  2677. // caller; it may be invalidated by any non-const call or unref of the
  2678. // SymbolTable! To protect against this, take a ref if desired.
  2679. // Freezes the symbol table: prevents further modification of it.
  2680. // After the Freeze() operation is successful, the SymbolTable must only be
  2681. // accessed via a const pointer.
  2682. //
  2683. // Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not
  2684. // a necessary step in using a SymbolTable. If you have no need for it to be
  2685. // immutable, there is no need to freeze it ever. However sometimes it is
  2686. // useful, and SymbolTables that are statically compiled into the binary are
  2687. // always frozen by nature.
  2688. void Freeze();
  2689. // Resolves the given symbol using the rules described in descriptor.proto,
  2690. // namely:
  2691. //
  2692. // If the name starts with a '.', it is fully-qualified. Otherwise,
  2693. // C++-like scoping rules are used to find the type (i.e. first the nested
  2694. // types within this message are searched, then within the parent, on up
  2695. // to the root namespace).
  2696. //
  2697. // If not found, returns NULL.
  2698. const Def* Resolve(const char* base, const char* sym) const;
  2699. // Finds an entry in the symbol table with this exact name. If not found,
  2700. // returns NULL.
  2701. const Def* Lookup(const char *sym) const;
  2702. const MessageDef* LookupMessage(const char *sym) const;
  2703. const EnumDef* LookupEnum(const char *sym) const;
  2704. // TODO: introduce a C++ iterator, but make it nice and templated so that if
  2705. // you ask for an iterator of MessageDef the iterated elements are strongly
  2706. // typed as MessageDef*.
  2707. // Adds the given mutable defs to the symtab, resolving all symbols
  2708. // (including enum default values) and finalizing the defs. Only one def per
  2709. // name may be in the list, but defs can replace existing defs in the symtab.
  2710. // All defs must have a name -- anonymous defs are not allowed. Anonymous
  2711. // defs can still be frozen by calling upb_def_freeze() directly.
  2712. //
  2713. // Any existing defs that can reach defs that are being replaced will
  2714. // themselves be replaced also, so that the resulting set of defs is fully
  2715. // consistent.
  2716. //
  2717. // This logic implemented in this method is a convenience; ultimately it
  2718. // calls some combination of upb_fielddef_setsubdef(), upb_def_dup(), and
  2719. // upb_freeze(), any of which the client could call themself. However, since
  2720. // the logic for doing so is nontrivial, we provide it here.
  2721. //
  2722. // The entire operation either succeeds or fails. If the operation fails,
  2723. // the symtab is unchanged, false is returned, and status indicates the
  2724. // error. The caller passes a ref on all defs to the symtab (even if the
  2725. // operation fails).
  2726. //
  2727. // TODO(haberman): currently failure will leave the symtab unchanged, but may
  2728. // leave the defs themselves partially resolved. Does this matter? If so we
  2729. // could do a prepass that ensures that all symbols are resolvable and bail
  2730. // if not, so we don't mutate anything until we know the operation will
  2731. // succeed.
  2732. //
  2733. // TODO(haberman): since the defs must be mutable, refining a frozen def
  2734. // requires making mutable copies of the entire tree. This is wasteful if
  2735. // only a few messages are changing. We may want to add a way of adding a
  2736. // tree of frozen defs to the symtab (perhaps an alternate constructor where
  2737. // you pass the root of the tree?)
  2738. bool Add(Def*const* defs, int n, void* ref_donor, upb_status* status);
  2739. bool Add(const std::vector<Def*>& defs, void *owner, Status* status) {
  2740. return Add((Def*const*)&defs[0], defs.size(), owner, status);
  2741. }
  2742. private:
  2743. UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable);
  2744. ,
  2745. UPB_DEFINE_STRUCT(upb_symtab, upb_refcounted,
  2746. upb_strtable symtab;
  2747. ));
  2748. #define UPB_SYMTAB_INIT(symtab, refs, ref2s) \
  2749. { UPB_REFCOUNT_INIT(refs, ref2s), symtab }
  2750. UPB_BEGIN_EXTERN_C // {
  2751. // Native C API.
  2752. // From upb_refcounted.
  2753. bool upb_symtab_isfrozen(const upb_symtab *s);
  2754. void upb_symtab_ref(const upb_symtab *s, const void *owner);
  2755. void upb_symtab_unref(const upb_symtab *s, const void *owner);
  2756. void upb_symtab_donateref(
  2757. const upb_symtab *s, const void *from, const void *to);
  2758. void upb_symtab_checkref(const upb_symtab *s, const void *owner);
  2759. upb_symtab *upb_symtab_new(const void *owner);
  2760. void upb_symtab_freeze(upb_symtab *s);
  2761. const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
  2762. const char *sym);
  2763. const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym);
  2764. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
  2765. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
  2766. bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, int n, void *ref_donor,
  2767. upb_status *status);
  2768. // upb_symtab_iter i;
  2769. // for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i);
  2770. // upb_symtab_next(&i)) {
  2771. // const upb_def *def = upb_symtab_iter_def(&i);
  2772. // // ...
  2773. // }
  2774. //
  2775. // For C we don't have separate iterators for const and non-const.
  2776. // It is the caller's responsibility to cast the upb_fielddef* to
  2777. // const if the upb_msgdef* is const.
  2778. void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
  2779. upb_deftype_t type);
  2780. void upb_symtab_next(upb_symtab_iter *iter);
  2781. bool upb_symtab_done(const upb_symtab_iter *iter);
  2782. const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter);
  2783. UPB_END_EXTERN_C // }
  2784. #ifdef __cplusplus
  2785. // C++ inline wrappers.
  2786. namespace upb {
  2787. inline reffed_ptr<SymbolTable> SymbolTable::New() {
  2788. upb_symtab *s = upb_symtab_new(&s);
  2789. return reffed_ptr<SymbolTable>(s, &s);
  2790. }
  2791. inline bool SymbolTable::IsFrozen() const {
  2792. return upb_symtab_isfrozen(this);
  2793. }
  2794. inline void SymbolTable::Ref(const void *owner) const {
  2795. upb_symtab_ref(this, owner);
  2796. }
  2797. inline void SymbolTable::Unref(const void *owner) const {
  2798. upb_symtab_unref(this, owner);
  2799. }
  2800. inline void SymbolTable::DonateRef(const void *from, const void *to) const {
  2801. upb_symtab_donateref(this, from, to);
  2802. }
  2803. inline void SymbolTable::CheckRef(const void *owner) const {
  2804. upb_symtab_checkref(this, owner);
  2805. }
  2806. inline void SymbolTable::Freeze() {
  2807. return upb_symtab_freeze(this);
  2808. }
  2809. inline const Def *SymbolTable::Resolve(const char *base,
  2810. const char *sym) const {
  2811. return upb_symtab_resolve(this, base, sym);
  2812. }
  2813. inline const Def* SymbolTable::Lookup(const char *sym) const {
  2814. return upb_symtab_lookup(this, sym);
  2815. }
  2816. inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const {
  2817. return upb_symtab_lookupmsg(this, sym);
  2818. }
  2819. inline bool SymbolTable::Add(
  2820. Def*const* defs, int n, void* ref_donor, upb_status* status) {
  2821. return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status);
  2822. }
  2823. } // namespace upb
  2824. #endif
  2825. #endif /* UPB_SYMTAB_H_ */
  2826. #ifdef __cplusplus
  2827. extern "C" {
  2828. #endif
  2829. // Enums
  2830. typedef enum {
  2831. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_OPTIONAL = 1,
  2832. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REQUIRED = 2,
  2833. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED = 3,
  2834. } google_protobuf_FieldDescriptorProto_Label;
  2835. typedef enum {
  2836. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE = 1,
  2837. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT = 2,
  2838. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64 = 3,
  2839. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64 = 4,
  2840. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 = 5,
  2841. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64 = 6,
  2842. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32 = 7,
  2843. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL = 8,
  2844. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING = 9,
  2845. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP = 10,
  2846. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE = 11,
  2847. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES = 12,
  2848. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13,
  2849. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM = 14,
  2850. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32 = 15,
  2851. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64 = 16,
  2852. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32 = 17,
  2853. GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64 = 18,
  2854. } google_protobuf_FieldDescriptorProto_Type;
  2855. typedef enum {
  2856. GOOGLE_PROTOBUF_FIELDOPTIONS_STRING = 0,
  2857. GOOGLE_PROTOBUF_FIELDOPTIONS_CORD = 1,
  2858. GOOGLE_PROTOBUF_FIELDOPTIONS_STRING_PIECE = 2,
  2859. } google_protobuf_FieldOptions_CType;
  2860. typedef enum {
  2861. GOOGLE_PROTOBUF_FILEOPTIONS_SPEED = 1,
  2862. GOOGLE_PROTOBUF_FILEOPTIONS_CODE_SIZE = 2,
  2863. GOOGLE_PROTOBUF_FILEOPTIONS_LITE_RUNTIME = 3,
  2864. } google_protobuf_FileOptions_OptimizeMode;
  2865. // Selectors
  2866. // google.protobuf.DescriptorProto
  2867. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSUBMSG 2
  2868. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSUBMSG 3
  2869. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 4
  2870. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSUBMSG 5
  2871. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSUBMSG 6
  2872. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_STARTSUBMSG 7
  2873. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_STARTSEQ 8
  2874. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSEQ 9
  2875. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_FIELD_ENDSUBMSG 10
  2876. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_STARTSEQ 11
  2877. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSEQ 12
  2878. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NESTED_TYPE_ENDSUBMSG 13
  2879. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 14
  2880. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 15
  2881. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 16
  2882. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_STARTSEQ 17
  2883. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSEQ 18
  2884. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_RANGE_ENDSUBMSG 19
  2885. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_STARTSEQ 20
  2886. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSEQ 21
  2887. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSION_ENDSUBMSG 22
  2888. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_OPTIONS_ENDSUBMSG 23
  2889. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STRING 24
  2890. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_STARTSTR 25
  2891. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_NAME_ENDSTR 26
  2892. // google.protobuf.DescriptorProto.ExtensionRange
  2893. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_START_INT32 2
  2894. #define SEL_GOOGLE_PROTOBUF_DESCRIPTORPROTO_EXTENSIONRANGE_END_INT32 3
  2895. // google.protobuf.EnumDescriptorProto
  2896. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSUBMSG 2
  2897. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3
  2898. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_STARTSEQ 4
  2899. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSEQ 5
  2900. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_VALUE_ENDSUBMSG 6
  2901. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7
  2902. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STRING 8
  2903. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_STARTSTR 9
  2904. #define SEL_GOOGLE_PROTOBUF_ENUMDESCRIPTORPROTO_NAME_ENDSTR 10
  2905. // google.protobuf.EnumOptions
  2906. #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  2907. #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  2908. #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  2909. #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  2910. #define SEL_GOOGLE_PROTOBUF_ENUMOPTIONS_ALLOW_ALIAS_BOOL 6
  2911. // google.protobuf.EnumValueDescriptorProto
  2912. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
  2913. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
  2914. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STRING 4
  2915. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_STARTSTR 5
  2916. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NAME_ENDSTR 6
  2917. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEDESCRIPTORPROTO_NUMBER_INT32 7
  2918. // google.protobuf.EnumValueOptions
  2919. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  2920. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  2921. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  2922. #define SEL_GOOGLE_PROTOBUF_ENUMVALUEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  2923. // google.protobuf.FieldDescriptorProto
  2924. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
  2925. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
  2926. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STRING 4
  2927. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_STARTSTR 5
  2928. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NAME_ENDSTR 6
  2929. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STRING 7
  2930. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_STARTSTR 8
  2931. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_EXTENDEE_ENDSTR 9
  2932. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_NUMBER_INT32 10
  2933. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_INT32 11
  2934. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 12
  2935. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STRING 13
  2936. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_STARTSTR 14
  2937. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_NAME_ENDSTR 15
  2938. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STRING 16
  2939. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_STARTSTR 17
  2940. #define SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_DEFAULT_VALUE_ENDSTR 18
  2941. // google.protobuf.FieldOptions
  2942. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  2943. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  2944. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  2945. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  2946. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_CTYPE_INT32 6
  2947. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_PACKED_BOOL 7
  2948. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_DEPRECATED_BOOL 8
  2949. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_LAZY_BOOL 9
  2950. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_EXPERIMENTAL_MAP_KEY_STRING 10
  2951. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_EXPERIMENTAL_MAP_KEY_STARTSTR 11
  2952. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_EXPERIMENTAL_MAP_KEY_ENDSTR 12
  2953. #define SEL_GOOGLE_PROTOBUF_FIELDOPTIONS_WEAK_BOOL 13
  2954. // google.protobuf.FileDescriptorProto
  2955. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSUBMSG 2
  2956. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSUBMSG 3
  2957. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSUBMSG 4
  2958. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSUBMSG 5
  2959. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 6
  2960. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_STARTSUBMSG 7
  2961. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_STARTSEQ 8
  2962. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSEQ 9
  2963. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_MESSAGE_TYPE_ENDSUBMSG 10
  2964. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_STARTSEQ 11
  2965. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSEQ 12
  2966. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_ENUM_TYPE_ENDSUBMSG 13
  2967. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_STARTSEQ 14
  2968. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSEQ 15
  2969. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SERVICE_ENDSUBMSG 16
  2970. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_STARTSEQ 17
  2971. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSEQ 18
  2972. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_EXTENSION_ENDSUBMSG 19
  2973. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 20
  2974. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_SOURCE_CODE_INFO_ENDSUBMSG 21
  2975. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STRING 22
  2976. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_STARTSTR 23
  2977. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_NAME_ENDSTR 24
  2978. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STRING 25
  2979. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_STARTSTR 26
  2980. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PACKAGE_ENDSTR 27
  2981. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSEQ 28
  2982. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSEQ 29
  2983. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STRING 30
  2984. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_STARTSTR 31
  2985. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_DEPENDENCY_ENDSTR 32
  2986. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_STARTSEQ 33
  2987. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_ENDSEQ 34
  2988. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_PUBLIC_DEPENDENCY_INT32 35
  2989. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_STARTSEQ 36
  2990. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_ENDSEQ 37
  2991. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORPROTO_WEAK_DEPENDENCY_INT32 38
  2992. // google.protobuf.FileDescriptorSet
  2993. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSUBMSG 2
  2994. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_STARTSEQ 3
  2995. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSEQ 4
  2996. #define SEL_GOOGLE_PROTOBUF_FILEDESCRIPTORSET_FILE_ENDSUBMSG 5
  2997. // google.protobuf.FileOptions
  2998. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  2999. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  3000. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  3001. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  3002. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STRING 6
  3003. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_STARTSTR 7
  3004. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_PACKAGE_ENDSTR 8
  3005. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STRING 9
  3006. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_STARTSTR 10
  3007. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_OUTER_CLASSNAME_ENDSTR 11
  3008. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_OPTIMIZE_FOR_INT32 12
  3009. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_MULTIPLE_FILES_BOOL 13
  3010. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STRING 14
  3011. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_STARTSTR 15
  3012. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_GO_PACKAGE_ENDSTR 16
  3013. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_CC_GENERIC_SERVICES_BOOL 17
  3014. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERIC_SERVICES_BOOL 18
  3015. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_PY_GENERIC_SERVICES_BOOL 19
  3016. #define SEL_GOOGLE_PROTOBUF_FILEOPTIONS_JAVA_GENERATE_EQUALS_AND_HASH_BOOL 20
  3017. // google.protobuf.MessageOptions
  3018. #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  3019. #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  3020. #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  3021. #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  3022. #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_MESSAGE_SET_WIRE_FORMAT_BOOL 6
  3023. #define SEL_GOOGLE_PROTOBUF_MESSAGEOPTIONS_NO_STANDARD_DESCRIPTOR_ACCESSOR_BOOL 7
  3024. // google.protobuf.MethodDescriptorProto
  3025. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 2
  3026. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 3
  3027. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STRING 4
  3028. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_STARTSTR 5
  3029. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_NAME_ENDSTR 6
  3030. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STRING 7
  3031. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_STARTSTR 8
  3032. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_INPUT_TYPE_ENDSTR 9
  3033. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STRING 10
  3034. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_STARTSTR 11
  3035. #define SEL_GOOGLE_PROTOBUF_METHODDESCRIPTORPROTO_OUTPUT_TYPE_ENDSTR 12
  3036. // google.protobuf.MethodOptions
  3037. #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  3038. #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  3039. #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  3040. #define SEL_GOOGLE_PROTOBUF_METHODOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  3041. // google.protobuf.ServiceDescriptorProto
  3042. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSUBMSG 2
  3043. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_STARTSUBMSG 3
  3044. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_STARTSEQ 4
  3045. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSEQ 5
  3046. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_METHOD_ENDSUBMSG 6
  3047. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_OPTIONS_ENDSUBMSG 7
  3048. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STRING 8
  3049. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_STARTSTR 9
  3050. #define SEL_GOOGLE_PROTOBUF_SERVICEDESCRIPTORPROTO_NAME_ENDSTR 10
  3051. // google.protobuf.ServiceOptions
  3052. #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSUBMSG 2
  3053. #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_STARTSEQ 3
  3054. #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSEQ 4
  3055. #define SEL_GOOGLE_PROTOBUF_SERVICEOPTIONS_UNINTERPRETED_OPTION_ENDSUBMSG 5
  3056. // google.protobuf.SourceCodeInfo
  3057. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSUBMSG 2
  3058. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_STARTSEQ 3
  3059. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSEQ 4
  3060. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_ENDSUBMSG 5
  3061. // google.protobuf.SourceCodeInfo.Location
  3062. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_STARTSEQ 2
  3063. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_ENDSEQ 3
  3064. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_PATH_INT32 4
  3065. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_STARTSEQ 5
  3066. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_ENDSEQ 6
  3067. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_SPAN_INT32 7
  3068. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STRING 8
  3069. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_STARTSTR 9
  3070. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_LEADING_COMMENTS_ENDSTR 10
  3071. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STRING 11
  3072. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_STARTSTR 12
  3073. #define SEL_GOOGLE_PROTOBUF_SOURCECODEINFO_LOCATION_TRAILING_COMMENTS_ENDSTR 13
  3074. // google.protobuf.UninterpretedOption
  3075. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSUBMSG 2
  3076. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_STARTSEQ 3
  3077. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSEQ 4
  3078. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAME_ENDSUBMSG 5
  3079. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STRING 6
  3080. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_STARTSTR 7
  3081. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_IDENTIFIER_VALUE_ENDSTR 8
  3082. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_POSITIVE_INT_VALUE_UINT64 9
  3083. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NEGATIVE_INT_VALUE_INT64 10
  3084. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_DOUBLE_VALUE_DOUBLE 11
  3085. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STRING 12
  3086. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_STARTSTR 13
  3087. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_STRING_VALUE_ENDSTR 14
  3088. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STRING 15
  3089. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_STARTSTR 16
  3090. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_AGGREGATE_VALUE_ENDSTR 17
  3091. // google.protobuf.UninterpretedOption.NamePart
  3092. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STRING 2
  3093. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_STARTSTR 3
  3094. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_NAME_PART_ENDSTR 4
  3095. #define SEL_GOOGLE_PROTOBUF_UNINTERPRETEDOPTION_NAMEPART_IS_EXTENSION_BOOL 5
  3096. const upb_symtab *upbdefs_google_protobuf_descriptor(const void *owner);
  3097. // MessageDefs
  3098. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto(const upb_symtab *s) {
  3099. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto");
  3100. assert(m);
  3101. return m;
  3102. }
  3103. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange(const upb_symtab *s) {
  3104. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.DescriptorProto.ExtensionRange");
  3105. assert(m);
  3106. return m;
  3107. }
  3108. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto(const upb_symtab *s) {
  3109. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumDescriptorProto");
  3110. assert(m);
  3111. return m;
  3112. }
  3113. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumOptions(const upb_symtab *s) {
  3114. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumOptions");
  3115. assert(m);
  3116. return m;
  3117. }
  3118. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto(const upb_symtab *s) {
  3119. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueDescriptorProto");
  3120. assert(m);
  3121. return m;
  3122. }
  3123. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions(const upb_symtab *s) {
  3124. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.EnumValueOptions");
  3125. assert(m);
  3126. return m;
  3127. }
  3128. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto(const upb_symtab *s) {
  3129. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldDescriptorProto");
  3130. assert(m);
  3131. return m;
  3132. }
  3133. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FieldOptions(const upb_symtab *s) {
  3134. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FieldOptions");
  3135. assert(m);
  3136. return m;
  3137. }
  3138. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto(const upb_symtab *s) {
  3139. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorProto");
  3140. assert(m);
  3141. return m;
  3142. }
  3143. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet(const upb_symtab *s) {
  3144. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileDescriptorSet");
  3145. assert(m);
  3146. return m;
  3147. }
  3148. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_FileOptions(const upb_symtab *s) {
  3149. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.FileOptions");
  3150. assert(m);
  3151. return m;
  3152. }
  3153. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MessageOptions(const upb_symtab *s) {
  3154. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MessageOptions");
  3155. assert(m);
  3156. return m;
  3157. }
  3158. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto(const upb_symtab *s) {
  3159. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodDescriptorProto");
  3160. assert(m);
  3161. return m;
  3162. }
  3163. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_MethodOptions(const upb_symtab *s) {
  3164. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.MethodOptions");
  3165. assert(m);
  3166. return m;
  3167. }
  3168. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto(const upb_symtab *s) {
  3169. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceDescriptorProto");
  3170. assert(m);
  3171. return m;
  3172. }
  3173. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_ServiceOptions(const upb_symtab *s) {
  3174. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.ServiceOptions");
  3175. assert(m);
  3176. return m;
  3177. }
  3178. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo(const upb_symtab *s) {
  3179. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo");
  3180. assert(m);
  3181. return m;
  3182. }
  3183. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location(const upb_symtab *s) {
  3184. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.SourceCodeInfo.Location");
  3185. assert(m);
  3186. return m;
  3187. }
  3188. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption(const upb_symtab *s) {
  3189. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption");
  3190. assert(m);
  3191. return m;
  3192. }
  3193. UPB_INLINE const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart(const upb_symtab *s) {
  3194. const upb_msgdef *m = upb_symtab_lookupmsg(s, "google.protobuf.UninterpretedOption.NamePart");
  3195. assert(m);
  3196. return m;
  3197. }
  3198. // EnumDefs
  3199. UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label(const upb_symtab *s) {
  3200. const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Label");
  3201. assert(e);
  3202. return e;
  3203. }
  3204. UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type(const upb_symtab *s) {
  3205. const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldDescriptorProto.Type");
  3206. assert(e);
  3207. return e;
  3208. }
  3209. UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType(const upb_symtab *s) {
  3210. const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FieldOptions.CType");
  3211. assert(e);
  3212. return e;
  3213. }
  3214. UPB_INLINE const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode(const upb_symtab *s) {
  3215. const upb_enumdef *e = upb_symtab_lookupenum(s, "google.protobuf.FileOptions.OptimizeMode");
  3216. assert(e);
  3217. return e;
  3218. }
  3219. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 2); }
  3220. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto_ExtensionRange(s), 1); }
  3221. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 4); }
  3222. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 6); }
  3223. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_extension_range(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 5); }
  3224. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_field(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 2); }
  3225. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 1); }
  3226. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_nested_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 3); }
  3227. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_DescriptorProto(s), 7); }
  3228. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 1); }
  3229. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 3); }
  3230. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumDescriptorProto(s), 2); }
  3231. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_allow_alias(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 2); }
  3232. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumOptions(s), 999); }
  3233. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 1); }
  3234. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 2); }
  3235. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueDescriptorProto(s), 3); }
  3236. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_EnumValueOptions(s), 999); }
  3237. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_default_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 7); }
  3238. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_extendee(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 2); }
  3239. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_label(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 4); }
  3240. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 1); }
  3241. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_number(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 3); }
  3242. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 8); }
  3243. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 5); }
  3244. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_type_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldDescriptorProto(s), 6); }
  3245. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_ctype(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 1); }
  3246. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_deprecated(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 3); }
  3247. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_experimental_map_key(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 9); }
  3248. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_lazy(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 5); }
  3249. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_packed(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 2); }
  3250. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 999); }
  3251. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_weak(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FieldOptions(s), 10); }
  3252. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 3); }
  3253. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_enum_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 5); }
  3254. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 7); }
  3255. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_message_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 4); }
  3256. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 1); }
  3257. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 8); }
  3258. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 2); }
  3259. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_public_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 10); }
  3260. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_service(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 6); }
  3261. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_source_code_info(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 9); }
  3262. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_weak_dependency(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorProto(s), 11); }
  3263. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_file(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileDescriptorSet(s), 1); }
  3264. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_cc_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 16); }
  3265. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_go_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 11); }
  3266. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 20); }
  3267. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 17); }
  3268. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_multiple_files(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 10); }
  3269. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_outer_classname(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 8); }
  3270. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_java_package(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 1); }
  3271. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_optimize_for(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 9); }
  3272. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_py_generic_services(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 18); }
  3273. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_FileOptions(s), 999); }
  3274. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_message_set_wire_format(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 1); }
  3275. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 2); }
  3276. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MessageOptions(s), 999); }
  3277. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_input_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 2); }
  3278. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 1); }
  3279. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 4); }
  3280. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_output_type(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodDescriptorProto(s), 3); }
  3281. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_MethodOptions(s), 999); }
  3282. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_method(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 2); }
  3283. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 1); }
  3284. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_options(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceDescriptorProto(s), 3); }
  3285. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_uninterpreted_option(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_ServiceOptions(s), 999); }
  3286. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 3); }
  3287. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_path(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 1); }
  3288. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_span(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 2); }
  3289. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo_Location(s), 4); }
  3290. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_location(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_SourceCodeInfo(s), 1); }
  3291. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 2); }
  3292. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption_NamePart(s), 1); }
  3293. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_aggregate_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 8); }
  3294. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_double_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 6); }
  3295. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_identifier_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 3); }
  3296. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_name(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 2); }
  3297. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_negative_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 5); }
  3298. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_positive_int_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 4); }
  3299. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_string_value(const upb_symtab *s) { return upb_msgdef_itof(upbdefs_google_protobuf_UninterpretedOption(s), 7); }
  3300. #ifdef __cplusplus
  3301. }; // extern "C"
  3302. #endif
  3303. #ifdef __cplusplus
  3304. namespace upbdefs {
  3305. namespace google {
  3306. namespace protobuf {
  3307. namespace descriptor {
  3308. inline upb::reffed_ptr<const upb::SymbolTable> SymbolTable() {
  3309. const upb::SymbolTable* s = upbdefs_google_protobuf_descriptor(&s);
  3310. return upb::reffed_ptr<const upb::SymbolTable>(s, &s);
  3311. }
  3312. } // namespace descriptor
  3313. } // namespace protobuf
  3314. } // namespace google
  3315. #define RETURN_REFFED(type, func) \
  3316. const type* obj = func(upbdefs::google::protobuf::descriptor::SymbolTable().get()); \
  3317. return upb::reffed_ptr<const type>(obj);
  3318. namespace google {
  3319. namespace protobuf {
  3320. namespace DescriptorProto {
  3321. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto) }
  3322. inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_enum_type) }
  3323. inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension) }
  3324. inline upb::reffed_ptr<const upb::FieldDef> extension_range() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_extension_range) }
  3325. inline upb::reffed_ptr<const upb::FieldDef> field() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_field) }
  3326. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_name) }
  3327. inline upb::reffed_ptr<const upb::FieldDef> nested_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_nested_type) }
  3328. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_options) }
  3329. } // namespace DescriptorProto
  3330. } // namespace protobuf
  3331. } // namespace google
  3332. namespace google {
  3333. namespace protobuf {
  3334. namespace DescriptorProto {
  3335. namespace ExtensionRange {
  3336. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange) }
  3337. inline upb::reffed_ptr<const upb::FieldDef> end() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_end) }
  3338. inline upb::reffed_ptr<const upb::FieldDef> start() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_DescriptorProto_ExtensionRange_start) }
  3339. } // namespace ExtensionRange
  3340. } // namespace DescriptorProto
  3341. } // namespace protobuf
  3342. } // namespace google
  3343. namespace google {
  3344. namespace protobuf {
  3345. namespace EnumDescriptorProto {
  3346. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumDescriptorProto) }
  3347. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_name) }
  3348. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_options) }
  3349. inline upb::reffed_ptr<const upb::FieldDef> value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumDescriptorProto_value) }
  3350. } // namespace EnumDescriptorProto
  3351. } // namespace protobuf
  3352. } // namespace google
  3353. namespace google {
  3354. namespace protobuf {
  3355. namespace EnumOptions {
  3356. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumOptions) }
  3357. inline upb::reffed_ptr<const upb::FieldDef> allow_alias() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_allow_alias) }
  3358. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumOptions_uninterpreted_option) }
  3359. } // namespace EnumOptions
  3360. } // namespace protobuf
  3361. } // namespace google
  3362. namespace google {
  3363. namespace protobuf {
  3364. namespace EnumValueDescriptorProto {
  3365. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueDescriptorProto) }
  3366. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_name) }
  3367. inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_number) }
  3368. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueDescriptorProto_options) }
  3369. } // namespace EnumValueDescriptorProto
  3370. } // namespace protobuf
  3371. } // namespace google
  3372. namespace google {
  3373. namespace protobuf {
  3374. namespace EnumValueOptions {
  3375. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_EnumValueOptions) }
  3376. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_EnumValueOptions_uninterpreted_option) }
  3377. } // namespace EnumValueOptions
  3378. } // namespace protobuf
  3379. } // namespace google
  3380. namespace google {
  3381. namespace protobuf {
  3382. namespace FieldDescriptorProto {
  3383. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldDescriptorProto) }
  3384. inline upb::reffed_ptr<const upb::FieldDef> default_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_default_value) }
  3385. inline upb::reffed_ptr<const upb::FieldDef> extendee() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_extendee) }
  3386. inline upb::reffed_ptr<const upb::FieldDef> label() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_label) }
  3387. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_name) }
  3388. inline upb::reffed_ptr<const upb::FieldDef> number() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_number) }
  3389. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_options) }
  3390. inline upb::reffed_ptr<const upb::FieldDef> type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type) }
  3391. inline upb::reffed_ptr<const upb::FieldDef> type_name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldDescriptorProto_type_name) }
  3392. inline upb::reffed_ptr<const upb::EnumDef> Label() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Label) }
  3393. inline upb::reffed_ptr<const upb::EnumDef> Type() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldDescriptorProto_Type) }
  3394. } // namespace FieldDescriptorProto
  3395. } // namespace protobuf
  3396. } // namespace google
  3397. namespace google {
  3398. namespace protobuf {
  3399. namespace FieldOptions {
  3400. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FieldOptions) }
  3401. inline upb::reffed_ptr<const upb::FieldDef> ctype() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_ctype) }
  3402. inline upb::reffed_ptr<const upb::FieldDef> deprecated() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_deprecated) }
  3403. inline upb::reffed_ptr<const upb::FieldDef> experimental_map_key() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_experimental_map_key) }
  3404. inline upb::reffed_ptr<const upb::FieldDef> lazy() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_lazy) }
  3405. inline upb::reffed_ptr<const upb::FieldDef> packed() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_packed) }
  3406. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_uninterpreted_option) }
  3407. inline upb::reffed_ptr<const upb::FieldDef> weak() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FieldOptions_weak) }
  3408. inline upb::reffed_ptr<const upb::EnumDef> CType() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FieldOptions_CType) }
  3409. } // namespace FieldOptions
  3410. } // namespace protobuf
  3411. } // namespace google
  3412. namespace google {
  3413. namespace protobuf {
  3414. namespace FileDescriptorProto {
  3415. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorProto) }
  3416. inline upb::reffed_ptr<const upb::FieldDef> dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_dependency) }
  3417. inline upb::reffed_ptr<const upb::FieldDef> enum_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_enum_type) }
  3418. inline upb::reffed_ptr<const upb::FieldDef> extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_extension) }
  3419. inline upb::reffed_ptr<const upb::FieldDef> message_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_message_type) }
  3420. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_name) }
  3421. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_options) }
  3422. inline upb::reffed_ptr<const upb::FieldDef> package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_package) }
  3423. inline upb::reffed_ptr<const upb::FieldDef> public_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_public_dependency) }
  3424. inline upb::reffed_ptr<const upb::FieldDef> service() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_service) }
  3425. inline upb::reffed_ptr<const upb::FieldDef> source_code_info() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_source_code_info) }
  3426. inline upb::reffed_ptr<const upb::FieldDef> weak_dependency() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorProto_weak_dependency) }
  3427. } // namespace FileDescriptorProto
  3428. } // namespace protobuf
  3429. } // namespace google
  3430. namespace google {
  3431. namespace protobuf {
  3432. namespace FileDescriptorSet {
  3433. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileDescriptorSet) }
  3434. inline upb::reffed_ptr<const upb::FieldDef> file() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileDescriptorSet_file) }
  3435. } // namespace FileDescriptorSet
  3436. } // namespace protobuf
  3437. } // namespace google
  3438. namespace google {
  3439. namespace protobuf {
  3440. namespace FileOptions {
  3441. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_FileOptions) }
  3442. inline upb::reffed_ptr<const upb::FieldDef> cc_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_cc_generic_services) }
  3443. inline upb::reffed_ptr<const upb::FieldDef> go_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_go_package) }
  3444. inline upb::reffed_ptr<const upb::FieldDef> java_generate_equals_and_hash() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generate_equals_and_hash) }
  3445. inline upb::reffed_ptr<const upb::FieldDef> java_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_generic_services) }
  3446. inline upb::reffed_ptr<const upb::FieldDef> java_multiple_files() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_multiple_files) }
  3447. inline upb::reffed_ptr<const upb::FieldDef> java_outer_classname() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_outer_classname) }
  3448. inline upb::reffed_ptr<const upb::FieldDef> java_package() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_java_package) }
  3449. inline upb::reffed_ptr<const upb::FieldDef> optimize_for() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_optimize_for) }
  3450. inline upb::reffed_ptr<const upb::FieldDef> py_generic_services() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_py_generic_services) }
  3451. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_FileOptions_uninterpreted_option) }
  3452. inline upb::reffed_ptr<const upb::EnumDef> OptimizeMode() { RETURN_REFFED(upb::EnumDef, upbdefs_google_protobuf_FileOptions_OptimizeMode) }
  3453. } // namespace FileOptions
  3454. } // namespace protobuf
  3455. } // namespace google
  3456. namespace google {
  3457. namespace protobuf {
  3458. namespace MessageOptions {
  3459. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MessageOptions) }
  3460. inline upb::reffed_ptr<const upb::FieldDef> message_set_wire_format() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_message_set_wire_format) }
  3461. inline upb::reffed_ptr<const upb::FieldDef> no_standard_descriptor_accessor() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_no_standard_descriptor_accessor) }
  3462. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MessageOptions_uninterpreted_option) }
  3463. } // namespace MessageOptions
  3464. } // namespace protobuf
  3465. } // namespace google
  3466. namespace google {
  3467. namespace protobuf {
  3468. namespace MethodDescriptorProto {
  3469. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodDescriptorProto) }
  3470. inline upb::reffed_ptr<const upb::FieldDef> input_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_input_type) }
  3471. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_name) }
  3472. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_options) }
  3473. inline upb::reffed_ptr<const upb::FieldDef> output_type() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodDescriptorProto_output_type) }
  3474. } // namespace MethodDescriptorProto
  3475. } // namespace protobuf
  3476. } // namespace google
  3477. namespace google {
  3478. namespace protobuf {
  3479. namespace MethodOptions {
  3480. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_MethodOptions) }
  3481. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_MethodOptions_uninterpreted_option) }
  3482. } // namespace MethodOptions
  3483. } // namespace protobuf
  3484. } // namespace google
  3485. namespace google {
  3486. namespace protobuf {
  3487. namespace ServiceDescriptorProto {
  3488. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceDescriptorProto) }
  3489. inline upb::reffed_ptr<const upb::FieldDef> method() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_method) }
  3490. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_name) }
  3491. inline upb::reffed_ptr<const upb::FieldDef> options() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceDescriptorProto_options) }
  3492. } // namespace ServiceDescriptorProto
  3493. } // namespace protobuf
  3494. } // namespace google
  3495. namespace google {
  3496. namespace protobuf {
  3497. namespace ServiceOptions {
  3498. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_ServiceOptions) }
  3499. inline upb::reffed_ptr<const upb::FieldDef> uninterpreted_option() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_ServiceOptions_uninterpreted_option) }
  3500. } // namespace ServiceOptions
  3501. } // namespace protobuf
  3502. } // namespace google
  3503. namespace google {
  3504. namespace protobuf {
  3505. namespace SourceCodeInfo {
  3506. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo) }
  3507. inline upb::reffed_ptr<const upb::FieldDef> location() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_location) }
  3508. } // namespace SourceCodeInfo
  3509. } // namespace protobuf
  3510. } // namespace google
  3511. namespace google {
  3512. namespace protobuf {
  3513. namespace SourceCodeInfo {
  3514. namespace Location {
  3515. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_SourceCodeInfo_Location) }
  3516. inline upb::reffed_ptr<const upb::FieldDef> leading_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_leading_comments) }
  3517. inline upb::reffed_ptr<const upb::FieldDef> path() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_path) }
  3518. inline upb::reffed_ptr<const upb::FieldDef> span() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_span) }
  3519. inline upb::reffed_ptr<const upb::FieldDef> trailing_comments() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_SourceCodeInfo_Location_trailing_comments) }
  3520. } // namespace Location
  3521. } // namespace SourceCodeInfo
  3522. } // namespace protobuf
  3523. } // namespace google
  3524. namespace google {
  3525. namespace protobuf {
  3526. namespace UninterpretedOption {
  3527. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption) }
  3528. inline upb::reffed_ptr<const upb::FieldDef> aggregate_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_aggregate_value) }
  3529. inline upb::reffed_ptr<const upb::FieldDef> double_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_double_value) }
  3530. inline upb::reffed_ptr<const upb::FieldDef> identifier_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_identifier_value) }
  3531. inline upb::reffed_ptr<const upb::FieldDef> name() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_name) }
  3532. inline upb::reffed_ptr<const upb::FieldDef> negative_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_negative_int_value) }
  3533. inline upb::reffed_ptr<const upb::FieldDef> positive_int_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_positive_int_value) }
  3534. inline upb::reffed_ptr<const upb::FieldDef> string_value() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_string_value) }
  3535. } // namespace UninterpretedOption
  3536. } // namespace protobuf
  3537. } // namespace google
  3538. namespace google {
  3539. namespace protobuf {
  3540. namespace UninterpretedOption {
  3541. namespace NamePart {
  3542. inline upb::reffed_ptr<const upb::MessageDef> MessageDef() { RETURN_REFFED(upb::MessageDef, upbdefs_google_protobuf_UninterpretedOption_NamePart) }
  3543. inline upb::reffed_ptr<const upb::FieldDef> is_extension() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_is_extension) }
  3544. inline upb::reffed_ptr<const upb::FieldDef> name_part() { RETURN_REFFED(upb::FieldDef, upbdefs_google_protobuf_UninterpretedOption_NamePart_name_part) }
  3545. } // namespace NamePart
  3546. } // namespace UninterpretedOption
  3547. } // namespace protobuf
  3548. } // namespace google
  3549. } // namespace upbdefs
  3550. #undef RETURN_REFFED
  3551. #endif // __cplusplus
  3552. #endif // GOOGLE_PROTOBUF_DESCRIPTOR_UPB_H_
  3553. /*
  3554. * upb - a minimalist implementation of protocol buffers.
  3555. *
  3556. * Copyright (c) 2010-2012 Google Inc. See LICENSE for details.
  3557. * Author: Josh Haberman <jhaberman@gmail.com>
  3558. *
  3559. * A upb_handlers is like a virtual table for a upb_msgdef. Each field of the
  3560. * message can have associated functions that will be called when we are
  3561. * parsing or visiting a stream of data. This is similar to how handlers work
  3562. * in SAX (the Simple API for XML).
  3563. *
  3564. * The handlers have no idea where the data is coming from, so a single set of
  3565. * handlers could be used with two completely different data sources (for
  3566. * example, a parser and a visitor over in-memory objects). This decoupling is
  3567. * the most important feature of upb, because it allows parsers and serializers
  3568. * to be highly reusable.
  3569. *
  3570. * This is a mixed C/C++ interface that offers a full API to both languages.
  3571. * See the top-level README for more information.
  3572. */
  3573. #ifndef UPB_HANDLERS_H
  3574. #define UPB_HANDLERS_H
  3575. #ifdef __cplusplus
  3576. namespace upb {
  3577. class BufferHandle;
  3578. class BytesHandler;
  3579. class HandlerAttributes;
  3580. class Handlers;
  3581. template <class T> class Handler;
  3582. template <class T> struct CanonicalType;
  3583. } // namespace upb
  3584. #endif
  3585. UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle);
  3586. UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler);
  3587. UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr);
  3588. UPB_DECLARE_TYPE(upb::Handlers, upb_handlers);
  3589. // The maximum depth that the handler graph can have. This is a resource limit
  3590. // for the C stack since we sometimes need to recursively traverse the graph.
  3591. // Cycles are ok; the traversal will stop when it detects a cycle, but we must
  3592. // hit the cycle before the maximum depth is reached.
  3593. //
  3594. // If having a single static limit is too inflexible, we can add another variant
  3595. // of Handlers::Freeze that allows specifying this as a parameter.
  3596. #define UPB_MAX_HANDLER_DEPTH 64
  3597. // All the different types of handlers that can be registered.
  3598. // Only needed for the advanced functions in upb::Handlers.
  3599. typedef enum {
  3600. UPB_HANDLER_INT32,
  3601. UPB_HANDLER_INT64,
  3602. UPB_HANDLER_UINT32,
  3603. UPB_HANDLER_UINT64,
  3604. UPB_HANDLER_FLOAT,
  3605. UPB_HANDLER_DOUBLE,
  3606. UPB_HANDLER_BOOL,
  3607. UPB_HANDLER_STARTSTR,
  3608. UPB_HANDLER_STRING,
  3609. UPB_HANDLER_ENDSTR,
  3610. UPB_HANDLER_STARTSUBMSG,
  3611. UPB_HANDLER_ENDSUBMSG,
  3612. UPB_HANDLER_STARTSEQ,
  3613. UPB_HANDLER_ENDSEQ,
  3614. } upb_handlertype_t;
  3615. #define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
  3616. #define UPB_BREAK NULL
  3617. // A convenient definition for when no closure is needed.
  3618. extern char _upb_noclosure;
  3619. #define UPB_NO_CLOSURE &_upb_noclosure
  3620. // A selector refers to a specific field handler in the Handlers object
  3621. // (for example: the STARTSUBMSG handler for field "field15").
  3622. typedef int32_t upb_selector_t;
  3623. UPB_BEGIN_EXTERN_C
  3624. // Forward-declares for C inline accessors. We need to declare these here
  3625. // so we can "friend" them in the class declarations in C++.
  3626. UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
  3627. upb_selector_t s);
  3628. UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr);
  3629. UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
  3630. upb_selector_t s);
  3631. UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h);
  3632. UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
  3633. const void *type);
  3634. UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
  3635. size_t ofs);
  3636. UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h);
  3637. UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h);
  3638. UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h);
  3639. UPB_END_EXTERN_C
  3640. // Static selectors for upb::Handlers.
  3641. #define UPB_STARTMSG_SELECTOR 0
  3642. #define UPB_ENDMSG_SELECTOR 1
  3643. #define UPB_STATIC_SELECTOR_COUNT 2
  3644. // Static selectors for upb::BytesHandler.
  3645. #define UPB_STARTSTR_SELECTOR 0
  3646. #define UPB_STRING_SELECTOR 1
  3647. #define UPB_ENDSTR_SELECTOR 2
  3648. typedef void upb_handlerfree(void *d);
  3649. // A set of attributes that accompanies a handler's function pointer.
  3650. UPB_DEFINE_CLASS0(upb::HandlerAttributes,
  3651. public:
  3652. HandlerAttributes();
  3653. ~HandlerAttributes();
  3654. // Sets the handler data that will be passed as the second parameter of the
  3655. // handler. To free this pointer when the handlers are freed, call
  3656. // Handlers::AddCleanup().
  3657. bool SetHandlerData(const void *handler_data);
  3658. const void* handler_data() const;
  3659. // Use this to specify the type of the closure. This will be checked against
  3660. // all other closure types for handler that use the same closure.
  3661. // Registration will fail if this does not match all other non-NULL closure
  3662. // types.
  3663. bool SetClosureType(const void *closure_type);
  3664. const void* closure_type() const;
  3665. // Use this to specify the type of the returned closure. Only used for
  3666. // Start*{String,SubMessage,Sequence} handlers. This must match the closure
  3667. // type of any handlers that use it (for example, the StringBuf handler must
  3668. // match the closure returned from StartString).
  3669. bool SetReturnClosureType(const void *return_closure_type);
  3670. const void* return_closure_type() const;
  3671. // Set to indicate that the handler always returns "ok" (either "true" or a
  3672. // non-NULL closure). This is a hint that can allow code generators to
  3673. // generate more efficient code.
  3674. bool SetAlwaysOk(bool always_ok);
  3675. bool always_ok() const;
  3676. private:
  3677. friend UPB_INLINE const void * ::upb_handlerattr_handlerdata(
  3678. const upb_handlerattr *attr);
  3679. ,
  3680. UPB_DEFINE_STRUCT0(upb_handlerattr,
  3681. const void *handler_data_;
  3682. const void *closure_type_;
  3683. const void *return_closure_type_;
  3684. bool alwaysok_;
  3685. ));
  3686. #define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false}
  3687. typedef struct {
  3688. upb_func *func;
  3689. // It is wasteful to include the entire attributes here:
  3690. //
  3691. // * Some of the information is redundant (like storing the closure type
  3692. // separately for each handler that must match).
  3693. // * Some of the info is only needed prior to freeze() (like closure types).
  3694. // * alignment padding wastes a lot of space for alwaysok_.
  3695. //
  3696. // If/when the size and locality of handlers is an issue, we can optimize this
  3697. // not to store the entire attr like this. We do not expose the table's
  3698. // layout to allow this optimization in the future.
  3699. upb_handlerattr attr;
  3700. } upb_handlers_tabent;
  3701. // Extra information about a buffer that is passed to a StringBuf handler.
  3702. // TODO(haberman): allow the handle to be pinned so that it will outlive
  3703. // the handler invocation.
  3704. UPB_DEFINE_CLASS0(upb::BufferHandle,
  3705. public:
  3706. BufferHandle();
  3707. ~BufferHandle();
  3708. // The beginning of the buffer. This may be different than the pointer
  3709. // passed to a StringBuf handler because the handler may receive data
  3710. // that is from the middle or end of a larger buffer.
  3711. const char* buffer() const;
  3712. // The offset within the attached object where this buffer begins. Only
  3713. // meaningful if there is an attached object.
  3714. size_t object_offset() const;
  3715. // Note that object_offset is the offset of "buf" within the attached object.
  3716. void SetBuffer(const char* buf, size_t object_offset);
  3717. // The BufferHandle can have an "attached object", which can be used to
  3718. // tunnel through a pointer to the buffer's underlying representation.
  3719. template <class T>
  3720. void SetAttachedObject(const T* obj);
  3721. // Returns NULL if the attached object is not of this type.
  3722. template <class T>
  3723. const T* GetAttachedObject() const;
  3724. private:
  3725. friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h);
  3726. friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h,
  3727. const void *obj,
  3728. const void *type);
  3729. friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h,
  3730. const char *buf, size_t ofs);
  3731. friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h);
  3732. friend UPB_INLINE const void* ::upb_bufhandle_objtype(
  3733. const upb_bufhandle *h);
  3734. friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h);
  3735. ,
  3736. UPB_DEFINE_STRUCT0(upb_bufhandle,
  3737. const char *buf_;
  3738. const void *obj_;
  3739. const void *objtype_;
  3740. size_t objofs_;
  3741. ));
  3742. // A upb::Handlers object represents the set of handlers associated with a
  3743. // message in the graph of messages. You can think of it as a big virtual
  3744. // table with functions corresponding to all the events that can fire while
  3745. // parsing or visiting a message of a specific type.
  3746. //
  3747. // Any handlers that are not set behave as if they had successfully consumed
  3748. // the value. Any unset Start* handlers will propagate their closure to the
  3749. // inner frame.
  3750. //
  3751. // The easiest way to create the *Handler objects needed by the Set* methods is
  3752. // with the UpbBind() and UpbMakeHandler() macros; see below.
  3753. UPB_DEFINE_CLASS1(upb::Handlers, upb::RefCounted,
  3754. public:
  3755. typedef upb_selector_t Selector;
  3756. typedef upb_handlertype_t Type;
  3757. typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
  3758. typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
  3759. typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
  3760. typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler;
  3761. typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
  3762. typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
  3763. const BufferHandle *)> StringHandler;
  3764. template <class T> struct ValueHandler {
  3765. typedef Handler<bool(*)(void *, const void *, T)> H;
  3766. };
  3767. typedef ValueHandler<int32_t>::H Int32Handler;
  3768. typedef ValueHandler<int64_t>::H Int64Handler;
  3769. typedef ValueHandler<uint32_t>::H UInt32Handler;
  3770. typedef ValueHandler<uint64_t>::H UInt64Handler;
  3771. typedef ValueHandler<float>::H FloatHandler;
  3772. typedef ValueHandler<double>::H DoubleHandler;
  3773. typedef ValueHandler<bool>::H BoolHandler;
  3774. // Any function pointer can be converted to this and converted back to its
  3775. // correct type.
  3776. typedef void GenericFunction();
  3777. typedef void HandlersCallback(const void *closure, upb_handlers *h);
  3778. // Returns a new handlers object for the given frozen msgdef.
  3779. // Returns NULL if memory allocation failed.
  3780. static reffed_ptr<Handlers> New(const MessageDef *m);
  3781. // Convenience function for registering a graph of handlers that mirrors the
  3782. // graph of msgdefs for some message. For "m" and all its children a new set
  3783. // of handlers will be created and the given callback will be invoked,
  3784. // allowing the client to register handlers for this message. Note that any
  3785. // subhandlers set by the callback will be overwritten.
  3786. static reffed_ptr<const Handlers> NewFrozen(const MessageDef *m,
  3787. HandlersCallback *callback,
  3788. const void *closure);
  3789. // Functionality from upb::RefCounted.
  3790. bool IsFrozen() const;
  3791. void Ref(const void* owner) const;
  3792. void Unref(const void* owner) const;
  3793. void DonateRef(const void *from, const void *to) const;
  3794. void CheckRef(const void *owner) const;
  3795. // All handler registration functions return bool to indicate success or
  3796. // failure; details about failures are stored in this status object. If a
  3797. // failure does occur, it must be cleared before the Handlers are frozen,
  3798. // otherwise the freeze() operation will fail. The functions may *only* be
  3799. // used while the Handlers are mutable.
  3800. const Status* status();
  3801. void ClearError();
  3802. // Call to freeze these Handlers. Requires that any SubHandlers are already
  3803. // frozen. For cycles, you must use the static version below and freeze the
  3804. // whole graph at once.
  3805. bool Freeze(Status* s);
  3806. // Freezes the given set of handlers. You may not freeze a handler without
  3807. // also freezing any handlers they point to.
  3808. static bool Freeze(Handlers*const* handlers, int n, Status* s);
  3809. static bool Freeze(const std::vector<Handlers*>& handlers, Status* s);
  3810. // Returns the msgdef associated with this handlers object.
  3811. const MessageDef* message_def() const;
  3812. // Adds the given pointer and function to the list of cleanup functions that
  3813. // will be run when these handlers are freed. If this pointer has previously
  3814. // been registered, the function returns false and does nothing.
  3815. bool AddCleanup(void *ptr, upb_handlerfree *cleanup);
  3816. // Sets the startmsg handler for the message, which is defined as follows:
  3817. //
  3818. // bool startmsg(MyType* closure) {
  3819. // // Called when the message begins. Returns true if processing should
  3820. // // continue.
  3821. // return true;
  3822. // }
  3823. bool SetStartMessageHandler(const StartMessageHandler& handler);
  3824. // Sets the endmsg handler for the message, which is defined as follows:
  3825. //
  3826. // bool endmsg(MyType* closure, upb_status *status) {
  3827. // // Called when processing of this message ends, whether in success or
  3828. // // failure. "status" indicates the final status of processing, and
  3829. // // can also be modified in-place to update the final status.
  3830. // }
  3831. bool SetEndMessageHandler(const EndMessageHandler& handler);
  3832. // Sets the value handler for the given field, which is defined as follows
  3833. // (this is for an int32 field; other field types will pass their native
  3834. // C/C++ type for "val"):
  3835. //
  3836. // bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3837. // // Called when the field's value is encountered. "d" contains
  3838. // // whatever data was bound to this field when it was registered.
  3839. // // Returns true if processing should continue.
  3840. // return true;
  3841. // }
  3842. //
  3843. // handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
  3844. //
  3845. // The value type must exactly match f->type().
  3846. // For example, a handler that takes an int32_t parameter may only be used for
  3847. // fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
  3848. //
  3849. // Returns false if the handler failed to register; in this case the cleanup
  3850. // handler (if any) will be called immediately.
  3851. bool SetInt32Handler (const FieldDef* f, const Int32Handler& h);
  3852. bool SetInt64Handler (const FieldDef* f, const Int64Handler& h);
  3853. bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h);
  3854. bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h);
  3855. bool SetFloatHandler (const FieldDef* f, const FloatHandler& h);
  3856. bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h);
  3857. bool SetBoolHandler (const FieldDef* f, const BoolHandler& h);
  3858. // Like the previous, but templated on the type on the value (ie. int32).
  3859. // This is mostly useful to call from other templates. To call this you must
  3860. // specify the template parameter explicitly, ie:
  3861. // h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData));
  3862. template <class T>
  3863. bool SetValueHandler(
  3864. const FieldDef *f,
  3865. const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler);
  3866. // Sets handlers for a string field, which are defined as follows:
  3867. //
  3868. // MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
  3869. // size_t size_hint) {
  3870. // // Called when a string value begins. The return value indicates the
  3871. // // closure for the string. "size_hint" indicates the size of the
  3872. // // string if it is known, however if the string is length-delimited
  3873. // // and the end-of-string is not available size_hint will be zero.
  3874. // // This case is indistinguishable from the case where the size is
  3875. // // known to be zero.
  3876. // //
  3877. // // TODO(haberman): is it important to distinguish these cases?
  3878. // // If we had ssize_t as a type we could make -1 "unknown", but
  3879. // // ssize_t is POSIX (not ANSI) and therefore less portable.
  3880. // // In practice I suspect it won't be important to distinguish.
  3881. // return closure;
  3882. // }
  3883. //
  3884. // size_t str(MyClosure* closure, const MyHandlerData* d,
  3885. // const char *str, size_t len) {
  3886. // // Called for each buffer of string data; the multiple physical buffers
  3887. // // are all part of the same logical string. The return value indicates
  3888. // // how many bytes were consumed. If this number is less than "len",
  3889. // // this will also indicate that processing should be halted for now,
  3890. // // like returning false or UPB_BREAK from any other callback. If
  3891. // // number is greater than "len", the excess bytes will be skipped over
  3892. // // and not passed to the callback.
  3893. // return len;
  3894. // }
  3895. //
  3896. // bool endstr(MyClosure* c, const MyHandlerData* d) {
  3897. // // Called when a string value ends. Return value indicates whether
  3898. // // processing should continue.
  3899. // return true;
  3900. // }
  3901. bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h);
  3902. bool SetStringHandler(const FieldDef* f, const StringHandler& h);
  3903. bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h);
  3904. // Sets the startseq handler, which is defined as follows:
  3905. //
  3906. // MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
  3907. // // Called when a sequence (repeated field) begins. The returned
  3908. // // pointer indicates the closure for the sequence (or UPB_BREAK
  3909. // // to interrupt processing).
  3910. // return closure;
  3911. // }
  3912. //
  3913. // h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
  3914. //
  3915. // Returns "false" if "f" does not belong to this message or is not a
  3916. // repeated field.
  3917. bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h);
  3918. // Sets the startsubmsg handler for the given field, which is defined as
  3919. // follows:
  3920. //
  3921. // MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
  3922. // // Called when a submessage begins. The returned pointer indicates the
  3923. // // closure for the sequence (or UPB_BREAK to interrupt processing).
  3924. // return closure;
  3925. // }
  3926. //
  3927. // h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
  3928. // new MyHandlerData(...)));
  3929. //
  3930. // Returns "false" if "f" does not belong to this message or is not a
  3931. // submessage/group field.
  3932. bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h);
  3933. // Sets the endsubmsg handler for the given field, which is defined as
  3934. // follows:
  3935. //
  3936. // bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
  3937. // // Called when a submessage ends. Returns true to continue processing.
  3938. // return true;
  3939. // }
  3940. //
  3941. // Returns "false" if "f" does not belong to this message or is not a
  3942. // submessage/group field.
  3943. bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h);
  3944. // Starts the endsubseq handler for the given field, which is defined as
  3945. // follows:
  3946. //
  3947. // bool endseq(MyClosure* c, const MyHandlerData* d) {
  3948. // // Called when a sequence ends. Returns true continue processing.
  3949. // return true;
  3950. // }
  3951. //
  3952. // Returns "false" if "f" does not belong to this message or is not a
  3953. // repeated field.
  3954. bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h);
  3955. // Sets or gets the object that specifies handlers for the given field, which
  3956. // must be a submessage or group. Returns NULL if no handlers are set.
  3957. bool SetSubHandlers(const FieldDef* f, const Handlers* sub);
  3958. const Handlers* GetSubHandlers(const FieldDef* f) const;
  3959. // Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the
  3960. // field.
  3961. const Handlers* GetSubHandlers(Selector startsubmsg) const;
  3962. // A selector refers to a specific field handler in the Handlers object
  3963. // (for example: the STARTSUBMSG handler for field "field15").
  3964. // On success, returns true and stores the selector in "s".
  3965. // If the FieldDef or Type are invalid, returns false.
  3966. // The returned selector is ONLY valid for Handlers whose MessageDef
  3967. // contains this FieldDef.
  3968. static bool GetSelector(const FieldDef* f, Type type, Selector* s);
  3969. // Given a START selector of any kind, returns the corresponding END selector.
  3970. static Selector GetEndSelector(Selector start_selector);
  3971. // Returns the function pointer for this handler. It is the client's
  3972. // responsibility to cast to the correct function type before calling it.
  3973. GenericFunction* GetHandler(Selector selector);
  3974. // Sets the given attributes to the attributes for this selector.
  3975. bool GetAttributes(Selector selector, HandlerAttributes* attr);
  3976. // Returns the handler data that was registered with this handler.
  3977. const void* GetHandlerData(Selector selector);
  3978. // Could add any of the following functions as-needed, with some minor
  3979. // implementation changes:
  3980. //
  3981. // const FieldDef* GetFieldDef(Selector selector);
  3982. // static bool IsSequence(Selector selector);
  3983. private:
  3984. UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers);
  3985. friend UPB_INLINE GenericFunction *::upb_handlers_gethandler(
  3986. const upb_handlers *h, upb_selector_t s);
  3987. friend UPB_INLINE const void *::upb_handlers_gethandlerdata(
  3988. const upb_handlers *h, upb_selector_t s);
  3989. ,
  3990. UPB_DEFINE_STRUCT(upb_handlers, upb_refcounted,
  3991. const upb_msgdef *msg;
  3992. const upb_handlers **sub;
  3993. const void *top_closure_type;
  3994. upb_inttable cleanup_;
  3995. upb_status status_; // Used only when mutable.
  3996. upb_handlers_tabent table[1]; // Dynamically-sized field handler array.
  3997. ));
  3998. #ifdef __cplusplus
  3999. namespace upb {
  4000. // Convenience macros for creating a Handler object that is wrapped with a
  4001. // type-safe wrapper function that converts the "void*" parameters/returns
  4002. // of the underlying C API into nice C++ function.
  4003. //
  4004. // Sample usage:
  4005. // void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
  4006. // // do stuff ...
  4007. // }
  4008. //
  4009. // // Handler that doesn't need any data bound to it.
  4010. // void OnValue2(MyClosure* c, int32_t val) {
  4011. // // do stuff ...
  4012. // }
  4013. //
  4014. // // Handler that returns bool so it can return failure if necessary.
  4015. // bool OnValue3(MyClosure* c, int32_t val) {
  4016. // // do stuff ...
  4017. // return ok;
  4018. // }
  4019. //
  4020. // // Member function handler.
  4021. // class MyClosure {
  4022. // public:
  4023. // void OnValue(int32_t val) {
  4024. // // do stuff ...
  4025. // }
  4026. // };
  4027. //
  4028. // // Takes ownership of the MyHandlerData.
  4029. // handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
  4030. // handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
  4031. // handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
  4032. // handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
  4033. #ifdef UPB_CXX11
  4034. // In C++11, the "template" disambiguator can appear even outside templates,
  4035. // so all calls can safely use this pair of macros.
  4036. #define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
  4037. // We have to be careful to only evaluate "d" once.
  4038. #define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
  4039. #else
  4040. // Prior to C++11, the "template" disambiguator may only appear inside a
  4041. // template, so the regular macro must not use "template"
  4042. #define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>()
  4043. #define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d))
  4044. #endif // UPB_CXX11
  4045. // This macro must be used in C++98 for calls from inside a template. But we
  4046. // define this variant in all cases; code that wants to be compatible with both
  4047. // C++98 and C++11 should always use this macro when calling from a template.
  4048. #define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc<f>()
  4049. // We have to be careful to only evaluate "d" once.
  4050. #define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
  4051. // Handler: a struct that contains the (handler, data, deleter) tuple that is
  4052. // used to register all handlers. Users can Make() these directly but it's
  4053. // more convenient to use the UpbMakeHandler/UpbBind macros above.
  4054. template <class T> class Handler {
  4055. public:
  4056. // The underlying, handler function signature that upb uses internally.
  4057. typedef T FuncPtr;
  4058. // Intentionally implicit.
  4059. template <class F> Handler(F func);
  4060. ~Handler();
  4061. private:
  4062. void AddCleanup(Handlers* h) const {
  4063. if (cleanup_func_) {
  4064. bool ok = h->AddCleanup(cleanup_data_, cleanup_func_);
  4065. UPB_ASSERT_VAR(ok, ok);
  4066. }
  4067. }
  4068. UPB_DISALLOW_COPY_AND_ASSIGN(Handler);
  4069. friend class Handlers;
  4070. FuncPtr handler_;
  4071. mutable HandlerAttributes attr_;
  4072. mutable bool registered_;
  4073. void *cleanup_data_;
  4074. upb_handlerfree *cleanup_func_;
  4075. };
  4076. } // namespace upb
  4077. #endif // __cplusplus
  4078. UPB_BEGIN_EXTERN_C
  4079. // Native C API.
  4080. // Handler function typedefs.
  4081. typedef bool upb_startmsg_handlerfunc(void *c, const void*);
  4082. typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
  4083. typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
  4084. typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
  4085. typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
  4086. typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
  4087. typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
  4088. typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
  4089. typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
  4090. typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
  4091. typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
  4092. typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
  4093. size_t size_hint);
  4094. typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
  4095. size_t n, const upb_bufhandle* handle);
  4096. // upb_bufhandle
  4097. size_t upb_bufhandle_objofs(const upb_bufhandle *h);
  4098. // upb_handlerattr
  4099. void upb_handlerattr_init(upb_handlerattr *attr);
  4100. void upb_handlerattr_uninit(upb_handlerattr *attr);
  4101. bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd);
  4102. bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type);
  4103. const void *upb_handlerattr_closuretype(const upb_handlerattr *attr);
  4104. bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
  4105. const void *type);
  4106. const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr);
  4107. bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok);
  4108. bool upb_handlerattr_alwaysok(const upb_handlerattr *attr);
  4109. UPB_INLINE const void *upb_handlerattr_handlerdata(
  4110. const upb_handlerattr *attr) {
  4111. return attr->handler_data_;
  4112. }
  4113. // upb_handlers
  4114. typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
  4115. upb_handlers *upb_handlers_new(const upb_msgdef *m,
  4116. const void *owner);
  4117. const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
  4118. const void *owner,
  4119. upb_handlers_callback *callback,
  4120. const void *closure);
  4121. bool upb_handlers_isfrozen(const upb_handlers *h);
  4122. void upb_handlers_ref(const upb_handlers *h, const void *owner);
  4123. void upb_handlers_unref(const upb_handlers *h, const void *owner);
  4124. void upb_handlers_donateref(const upb_handlers *h, const void *from,
  4125. const void *to);
  4126. void upb_handlers_checkref(const upb_handlers *h, const void *owner);
  4127. const upb_status *upb_handlers_status(upb_handlers *h);
  4128. void upb_handlers_clearerr(upb_handlers *h);
  4129. const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
  4130. bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
  4131. bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
  4132. upb_handlerattr *attr);
  4133. bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
  4134. upb_handlerattr *attr);
  4135. bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
  4136. upb_int32_handlerfunc *func, upb_handlerattr *attr);
  4137. bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
  4138. upb_int64_handlerfunc *func, upb_handlerattr *attr);
  4139. bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
  4140. upb_uint32_handlerfunc *func,
  4141. upb_handlerattr *attr);
  4142. bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
  4143. upb_uint64_handlerfunc *func,
  4144. upb_handlerattr *attr);
  4145. bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
  4146. upb_float_handlerfunc *func, upb_handlerattr *attr);
  4147. bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
  4148. upb_double_handlerfunc *func,
  4149. upb_handlerattr *attr);
  4150. bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
  4151. upb_bool_handlerfunc *func,
  4152. upb_handlerattr *attr);
  4153. bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
  4154. upb_startstr_handlerfunc *func,
  4155. upb_handlerattr *attr);
  4156. bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
  4157. upb_string_handlerfunc *func,
  4158. upb_handlerattr *attr);
  4159. bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
  4160. upb_endfield_handlerfunc *func,
  4161. upb_handlerattr *attr);
  4162. bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
  4163. upb_startfield_handlerfunc *func,
  4164. upb_handlerattr *attr);
  4165. bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
  4166. upb_startfield_handlerfunc *func,
  4167. upb_handlerattr *attr);
  4168. bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
  4169. upb_endfield_handlerfunc *func,
  4170. upb_handlerattr *attr);
  4171. bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
  4172. upb_endfield_handlerfunc *func,
  4173. upb_handlerattr *attr);
  4174. bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
  4175. const upb_handlers *sub);
  4176. const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
  4177. const upb_fielddef *f);
  4178. const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
  4179. upb_selector_t sel);
  4180. UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
  4181. upb_selector_t s) {
  4182. return (upb_func *)h->table[s].func;
  4183. }
  4184. bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
  4185. upb_handlerattr *attr);
  4186. UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
  4187. upb_selector_t s) {
  4188. return upb_handlerattr_handlerdata(&h->table[s].attr);
  4189. }
  4190. // Handler types for single fields.
  4191. // Right now we only have one for TYPE_BYTES but ones for other types
  4192. // should follow.
  4193. //
  4194. // These follow the same handlers protocol for fields of a message.
  4195. UPB_DEFINE_CLASS0(upb::BytesHandler,
  4196. public:
  4197. BytesHandler();
  4198. ~BytesHandler();
  4199. ,
  4200. UPB_DEFINE_STRUCT0(upb_byteshandler,
  4201. upb_handlers_tabent table[3];
  4202. ));
  4203. void upb_byteshandler_init(upb_byteshandler *h);
  4204. void upb_byteshandler_uninit(upb_byteshandler *h);
  4205. // Caller must ensure that "d" outlives the handlers.
  4206. // TODO(haberman): support handlerfree function for the data.
  4207. // TODO(haberman): should this have a "freeze" operation? It's not necessary
  4208. // for memory management, but could be useful to force immutability and provide
  4209. // a convenient moment to verify that all registration succeeded.
  4210. bool upb_byteshandler_setstartstr(upb_byteshandler *h,
  4211. upb_startstr_handlerfunc *func, void *d);
  4212. bool upb_byteshandler_setstring(upb_byteshandler *h,
  4213. upb_string_handlerfunc *func, void *d);
  4214. bool upb_byteshandler_setendstr(upb_byteshandler *h,
  4215. upb_endfield_handlerfunc *func, void *d);
  4216. // "Static" methods
  4217. bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s);
  4218. upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
  4219. bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
  4220. upb_selector_t *s);
  4221. UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
  4222. return start + 1;
  4223. }
  4224. // Internal-only.
  4225. uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
  4226. uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
  4227. UPB_END_EXTERN_C
  4228. /*
  4229. * upb - a minimalist implementation of protocol buffers.
  4230. *
  4231. * Copyright (c) 2011-2012 Google Inc. See LICENSE for details.
  4232. * Author: Josh Haberman <jhaberman@gmail.com>
  4233. *
  4234. * Inline definitions for handlers.h, which are particularly long and a bit
  4235. * tricky.
  4236. */
  4237. #ifndef UPB_HANDLERS_INL_H_
  4238. #define UPB_HANDLERS_INL_H_
  4239. #include <limits.h>
  4240. // Type detection and typedefs for integer types.
  4241. // For platforms where there are multiple 32-bit or 64-bit types, we need to be
  4242. // able to enumerate them so we can properly create overloads for all variants.
  4243. //
  4244. // If any platform existed where there were three integer types with the same
  4245. // size, this would have to become more complicated. For example, short, int,
  4246. // and long could all be 32-bits. Even more diabolically, short, int, long,
  4247. // and long long could all be 64 bits and still be standard-compliant.
  4248. // However, few platforms are this strange, and it's unlikely that upb will be
  4249. // used on the strangest ones.
  4250. // Can't count on stdint.h limits like INT32_MAX, because in C++ these are
  4251. // only defined when __STDC_LIMIT_MACROS are defined before the *first* include
  4252. // of stdint.h. We can't guarantee that someone else didn't include these first
  4253. // without defining __STDC_LIMIT_MACROS.
  4254. #define UPB_INT32_MAX 0x7fffffffLL
  4255. #define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
  4256. #define UPB_INT64_MAX 0x7fffffffffffffffLL
  4257. #define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
  4258. #if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
  4259. #define UPB_INT_IS_32BITS 1
  4260. #endif
  4261. #if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
  4262. #define UPB_LONG_IS_32BITS 1
  4263. #endif
  4264. #if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
  4265. #define UPB_LONG_IS_64BITS 1
  4266. #endif
  4267. #if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
  4268. #define UPB_LLONG_IS_64BITS 1
  4269. #endif
  4270. // We use macros instead of typedefs so we can undefine them later and avoid
  4271. // leaking them outside this header file.
  4272. #if UPB_INT_IS_32BITS
  4273. #define UPB_INT32_T int
  4274. #define UPB_UINT32_T unsigned int
  4275. #if UPB_LONG_IS_32BITS
  4276. #define UPB_TWO_32BIT_TYPES 1
  4277. #define UPB_INT32ALT_T long
  4278. #define UPB_UINT32ALT_T unsigned long
  4279. #endif // UPB_LONG_IS_32BITS
  4280. #elif UPB_LONG_IS_32BITS // && !UPB_INT_IS_32BITS
  4281. #define UPB_INT32_T long
  4282. #define UPB_UINT32_T unsigned long
  4283. #endif // UPB_INT_IS_32BITS
  4284. #if UPB_LONG_IS_64BITS
  4285. #define UPB_INT64_T long
  4286. #define UPB_UINT64_T unsigned long
  4287. #if UPB_LLONG_IS_64BITS
  4288. #define UPB_TWO_64BIT_TYPES 1
  4289. #define UPB_INT64ALT_T long long
  4290. #define UPB_UINT64ALT_T unsigned long long
  4291. #endif // UPB_LLONG_IS_64BITS
  4292. #elif UPB_LLONG_IS_64BITS // && !UPB_LONG_IS_64BITS
  4293. #define UPB_INT64_T long long
  4294. #define UPB_UINT64_T unsigned long long
  4295. #endif // UPB_LONG_IS_64BITS
  4296. #undef UPB_INT32_MAX
  4297. #undef UPB_INT32_MIN
  4298. #undef UPB_INT64_MAX
  4299. #undef UPB_INT64_MIN
  4300. #undef UPB_INT_IS_32BITS
  4301. #undef UPB_LONG_IS_32BITS
  4302. #undef UPB_LONG_IS_64BITS
  4303. #undef UPB_LLONG_IS_64BITS
  4304. // C inline methods.
  4305. // upb_bufhandle
  4306. UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) {
  4307. h->obj_ = NULL;
  4308. h->objtype_ = NULL;
  4309. h->buf_ = NULL;
  4310. h->objofs_ = 0;
  4311. }
  4312. UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) {
  4313. UPB_UNUSED(h);
  4314. }
  4315. UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
  4316. const void *type) {
  4317. h->obj_ = obj;
  4318. h->objtype_ = type;
  4319. }
  4320. UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
  4321. size_t ofs) {
  4322. h->buf_ = buf;
  4323. h->objofs_ = ofs;
  4324. }
  4325. UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) {
  4326. return h->obj_;
  4327. }
  4328. UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) {
  4329. return h->objtype_;
  4330. }
  4331. UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) {
  4332. return h->buf_;
  4333. }
  4334. #ifdef __cplusplus
  4335. namespace upb {
  4336. typedef void CleanupFunc(void *ptr);
  4337. // Template to remove "const" from "const T*" and just return "T*".
  4338. //
  4339. // We define a nonsense default because otherwise it will fail to instantiate as
  4340. // a function parameter type even in cases where we don't expect any caller to
  4341. // actually match the overload.
  4342. class CouldntRemoveConst {};
  4343. template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
  4344. template <class T> struct remove_constptr<const T *> { typedef T *type; };
  4345. // Template that we use below to remove a template specialization from
  4346. // consideration if it matches a specific type.
  4347. template <class T, class U> struct disable_if_same { typedef void Type; };
  4348. template <class T> struct disable_if_same<T, T> {};
  4349. template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
  4350. template <class T1, class T2>
  4351. struct FirstUnlessVoid {
  4352. typedef T1 value;
  4353. };
  4354. template <class T2>
  4355. struct FirstUnlessVoid<void, T2> {
  4356. typedef T2 value;
  4357. };
  4358. template<class T, class U>
  4359. struct is_same {
  4360. static bool value;
  4361. };
  4362. template<class T>
  4363. struct is_same<T, T> {
  4364. static bool value;
  4365. };
  4366. template<class T, class U>
  4367. bool is_same<T, U>::value = false;
  4368. template<class T>
  4369. bool is_same<T, T>::value = true;
  4370. // FuncInfo ////////////////////////////////////////////////////////////////////
  4371. // Info about the user's original, pre-wrapped function.
  4372. template <class C, class R = void>
  4373. struct FuncInfo {
  4374. // The type of the closure that the function takes (its first param).
  4375. typedef C Closure;
  4376. // The return type.
  4377. typedef R Return;
  4378. };
  4379. // Func ////////////////////////////////////////////////////////////////////////
  4380. // Func1, Func2, Func3: Template classes representing a function and its
  4381. // signature.
  4382. //
  4383. // Since the function is a template parameter, calling the function can be
  4384. // inlined at compile-time and does not require a function pointer at runtime.
  4385. // These functions are not bound to a handler data so have no data or cleanup
  4386. // handler.
  4387. struct UnboundFunc {
  4388. CleanupFunc *GetCleanup() { return NULL; }
  4389. void *GetData() { return NULL; }
  4390. };
  4391. template <class R, class P1, R F(P1), class I>
  4392. struct Func1 : public UnboundFunc {
  4393. typedef R Return;
  4394. typedef I FuncInfo;
  4395. static R Call(P1 p1) { return F(p1); }
  4396. };
  4397. template <class R, class P1, class P2, R F(P1, P2), class I>
  4398. struct Func2 : public UnboundFunc {
  4399. typedef R Return;
  4400. typedef I FuncInfo;
  4401. static R Call(P1 p1, P2 p2) { return F(p1, p2); }
  4402. };
  4403. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4404. struct Func3 : public UnboundFunc {
  4405. typedef R Return;
  4406. typedef I FuncInfo;
  4407. static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
  4408. };
  4409. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4410. class I>
  4411. struct Func4 : public UnboundFunc {
  4412. typedef R Return;
  4413. typedef I FuncInfo;
  4414. static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
  4415. };
  4416. template <class R, class P1, class P2, class P3, class P4, class P5,
  4417. R F(P1, P2, P3, P4, P5), class I>
  4418. struct Func5 : public UnboundFunc {
  4419. typedef R Return;
  4420. typedef I FuncInfo;
  4421. static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  4422. return F(p1, p2, p3, p4, p5);
  4423. }
  4424. };
  4425. // BoundFunc ///////////////////////////////////////////////////////////////////
  4426. // BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
  4427. // shall be bound to the function's second parameter.
  4428. //
  4429. // Note that the second parameter is a const pointer, but our stored bound value
  4430. // is non-const so we can free it when the handlers are destroyed.
  4431. template <class T>
  4432. struct BoundFunc {
  4433. typedef typename remove_constptr<T>::type MutableP2;
  4434. explicit BoundFunc(MutableP2 data_) : data(data_) {}
  4435. CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
  4436. MutableP2 GetData() { return data; }
  4437. MutableP2 data;
  4438. };
  4439. template <class R, class P1, class P2, R F(P1, P2), class I>
  4440. struct BoundFunc2 : public BoundFunc<P2> {
  4441. typedef BoundFunc<P2> Base;
  4442. typedef I FuncInfo;
  4443. explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
  4444. };
  4445. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4446. struct BoundFunc3 : public BoundFunc<P2> {
  4447. typedef BoundFunc<P2> Base;
  4448. typedef I FuncInfo;
  4449. explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
  4450. };
  4451. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4452. class I>
  4453. struct BoundFunc4 : public BoundFunc<P2> {
  4454. typedef BoundFunc<P2> Base;
  4455. typedef I FuncInfo;
  4456. explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
  4457. };
  4458. template <class R, class P1, class P2, class P3, class P4, class P5,
  4459. R F(P1, P2, P3, P4, P5), class I>
  4460. struct BoundFunc5 : public BoundFunc<P2> {
  4461. typedef BoundFunc<P2> Base;
  4462. typedef I FuncInfo;
  4463. explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
  4464. };
  4465. // FuncSig /////////////////////////////////////////////////////////////////////
  4466. // FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
  4467. // *signature*, but without a specific function attached.
  4468. //
  4469. // These classes contain member functions that can be invoked with a
  4470. // specific function to return a Func/BoundFunc class.
  4471. template <class R, class P1>
  4472. struct FuncSig1 {
  4473. template <R F(P1)>
  4474. Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
  4475. return Func1<R, P1, F, FuncInfo<P1, R> >();
  4476. }
  4477. };
  4478. template <class R, class P1, class P2>
  4479. struct FuncSig2 {
  4480. template <R F(P1, P2)>
  4481. Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
  4482. return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
  4483. }
  4484. template <R F(P1, P2)>
  4485. BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
  4486. typename remove_constptr<P2>::type param2) {
  4487. return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
  4488. }
  4489. };
  4490. template <class R, class P1, class P2, class P3>
  4491. struct FuncSig3 {
  4492. template <R F(P1, P2, P3)>
  4493. Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
  4494. return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
  4495. }
  4496. template <R F(P1, P2, P3)>
  4497. BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
  4498. typename remove_constptr<P2>::type param2) {
  4499. return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
  4500. }
  4501. };
  4502. template <class R, class P1, class P2, class P3, class P4>
  4503. struct FuncSig4 {
  4504. template <R F(P1, P2, P3, P4)>
  4505. Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
  4506. return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
  4507. }
  4508. template <R F(P1, P2, P3, P4)>
  4509. BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
  4510. typename remove_constptr<P2>::type param2) {
  4511. return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
  4512. }
  4513. };
  4514. template <class R, class P1, class P2, class P3, class P4, class P5>
  4515. struct FuncSig5 {
  4516. template <R F(P1, P2, P3, P4, P5)>
  4517. Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
  4518. return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
  4519. }
  4520. template <R F(P1, P2, P3, P4, P5)>
  4521. BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
  4522. typename remove_constptr<P2>::type param2) {
  4523. return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
  4524. }
  4525. };
  4526. // Overloaded template function that can construct the appropriate FuncSig*
  4527. // class given a function pointer by deducing the template parameters.
  4528. template <class R, class P1>
  4529. inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
  4530. UPB_UNUSED(f); // Only used for template parameter deduction.
  4531. return FuncSig1<R, P1>();
  4532. }
  4533. template <class R, class P1, class P2>
  4534. inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
  4535. UPB_UNUSED(f); // Only used for template parameter deduction.
  4536. return FuncSig2<R, P1, P2>();
  4537. }
  4538. template <class R, class P1, class P2, class P3>
  4539. inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
  4540. UPB_UNUSED(f); // Only used for template parameter deduction.
  4541. return FuncSig3<R, P1, P2, P3>();
  4542. }
  4543. template <class R, class P1, class P2, class P3, class P4>
  4544. inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
  4545. UPB_UNUSED(f); // Only used for template parameter deduction.
  4546. return FuncSig4<R, P1, P2, P3, P4>();
  4547. }
  4548. template <class R, class P1, class P2, class P3, class P4, class P5>
  4549. inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
  4550. UPB_UNUSED(f); // Only used for template parameter deduction.
  4551. return FuncSig5<R, P1, P2, P3, P4, P5>();
  4552. }
  4553. // MethodSig ///////////////////////////////////////////////////////////////////
  4554. // CallMethod*: a function template that calls a given method.
  4555. template <class R, class C, R (C::*F)()>
  4556. R CallMethod0(C *obj) {
  4557. return ((*obj).*F)();
  4558. }
  4559. template <class R, class C, class P1, R (C::*F)(P1)>
  4560. R CallMethod1(C *obj, P1 arg1) {
  4561. return ((*obj).*F)(arg1);
  4562. }
  4563. template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
  4564. R CallMethod2(C *obj, P1 arg1, P2 arg2) {
  4565. return ((*obj).*F)(arg1, arg2);
  4566. }
  4567. template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
  4568. R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
  4569. return ((*obj).*F)(arg1, arg2, arg3);
  4570. }
  4571. template <class R, class C, class P1, class P2, class P3, class P4,
  4572. R (C::*F)(P1, P2, P3, P4)>
  4573. R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
  4574. return ((*obj).*F)(arg1, arg2, arg3, arg4);
  4575. }
  4576. // MethodSig: like FuncSig, but for member functions.
  4577. //
  4578. // GetFunc() returns a normal FuncN object, so after calling GetFunc() no
  4579. // more logic is required to special-case methods.
  4580. template <class R, class C>
  4581. struct MethodSig0 {
  4582. template <R (C::*F)()>
  4583. Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
  4584. return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
  4585. }
  4586. };
  4587. template <class R, class C, class P1>
  4588. struct MethodSig1 {
  4589. template <R (C::*F)(P1)>
  4590. Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
  4591. return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
  4592. }
  4593. template <R (C::*F)(P1)>
  4594. BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
  4595. typename remove_constptr<P1>::type param1) {
  4596. return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
  4597. param1);
  4598. }
  4599. };
  4600. template <class R, class C, class P1, class P2>
  4601. struct MethodSig2 {
  4602. template <R (C::*F)(P1, P2)>
  4603. Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4604. GetFunc() {
  4605. return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4606. FuncInfo<C *, R> >();
  4607. }
  4608. template <R (C::*F)(P1, P2)>
  4609. BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4610. GetFunc(typename remove_constptr<P1>::type param1) {
  4611. return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4612. FuncInfo<C *, R> >(param1);
  4613. }
  4614. };
  4615. template <class R, class C, class P1, class P2, class P3>
  4616. struct MethodSig3 {
  4617. template <R (C::*F)(P1, P2, P3)>
  4618. Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
  4619. GetFunc() {
  4620. return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4621. FuncInfo<C *, R> >();
  4622. }
  4623. template <R (C::*F)(P1, P2, P3)>
  4624. BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4625. FuncInfo<C *, R> >
  4626. GetFunc(typename remove_constptr<P1>::type param1) {
  4627. return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4628. FuncInfo<C *, R> >(param1);
  4629. }
  4630. };
  4631. template <class R, class C, class P1, class P2, class P3, class P4>
  4632. struct MethodSig4 {
  4633. template <R (C::*F)(P1, P2, P3, P4)>
  4634. Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4635. FuncInfo<C *, R> >
  4636. GetFunc() {
  4637. return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4638. FuncInfo<C *, R> >();
  4639. }
  4640. template <R (C::*F)(P1, P2, P3, P4)>
  4641. BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4642. FuncInfo<C *, R> >
  4643. GetFunc(typename remove_constptr<P1>::type param1) {
  4644. return BoundFunc5<R, C *, P1, P2, P3, P4,
  4645. CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
  4646. param1);
  4647. }
  4648. };
  4649. template <class R, class C>
  4650. inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
  4651. UPB_UNUSED(f); // Only used for template parameter deduction.
  4652. return MethodSig0<R, C>();
  4653. }
  4654. template <class R, class C, class P1>
  4655. inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
  4656. UPB_UNUSED(f); // Only used for template parameter deduction.
  4657. return MethodSig1<R, C, P1>();
  4658. }
  4659. template <class R, class C, class P1, class P2>
  4660. inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
  4661. UPB_UNUSED(f); // Only used for template parameter deduction.
  4662. return MethodSig2<R, C, P1, P2>();
  4663. }
  4664. template <class R, class C, class P1, class P2, class P3>
  4665. inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
  4666. UPB_UNUSED(f); // Only used for template parameter deduction.
  4667. return MethodSig3<R, C, P1, P2, P3>();
  4668. }
  4669. template <class R, class C, class P1, class P2, class P3, class P4>
  4670. inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
  4671. UPB_UNUSED(f); // Only used for template parameter deduction.
  4672. return MethodSig4<R, C, P1, P2, P3, P4>();
  4673. }
  4674. // MaybeWrapReturn /////////////////////////////////////////////////////////////
  4675. // Template class that attempts to wrap the return value of the function so it
  4676. // matches the expected type. There are two main adjustments it may make:
  4677. //
  4678. // 1. If the function returns void, make it return the expected type and with
  4679. // a value that always indicates success.
  4680. // 2. If the function is expected to return void* but doesn't, wrap it so it
  4681. // does (either by returning the closure param if the wrapped function
  4682. // returns void or by casting a different pointer type to void* for
  4683. // return).
  4684. // Template parameters are FuncN type and desired return type.
  4685. template <class F, class R, class Enable = void>
  4686. struct MaybeWrapReturn;
  4687. // If the return type matches, return the given function unwrapped.
  4688. template <class F>
  4689. struct MaybeWrapReturn<F, typename F::Return> {
  4690. typedef F Func;
  4691. };
  4692. // Function wrapper that munges the return value from void to (bool)true.
  4693. template <class P1, class P2, void F(P1, P2)>
  4694. bool ReturnTrue2(P1 p1, P2 p2) {
  4695. F(p1, p2);
  4696. return true;
  4697. }
  4698. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4699. bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
  4700. F(p1, p2, p3);
  4701. return true;
  4702. }
  4703. // Function wrapper that munges the return value from void to (void*)arg1
  4704. template <class P1, class P2, void F(P1, P2)>
  4705. void *ReturnClosure2(P1 p1, P2 p2) {
  4706. F(p1, p2);
  4707. return p1;
  4708. }
  4709. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4710. void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
  4711. F(p1, p2, p3);
  4712. return p1;
  4713. }
  4714. // Function wrapper that munges the return value from R to void*.
  4715. template <class R, class P1, class P2, R F(P1, P2)>
  4716. void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
  4717. return F(p1, p2);
  4718. }
  4719. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4720. void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
  4721. return F(p1, p2, p3);
  4722. }
  4723. // Function wrapper that munges the return value from bool to void*.
  4724. template <class P1, class P2, bool F(P1, P2)>
  4725. void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
  4726. return F(p1, p2) ? p1 : UPB_BREAK;
  4727. }
  4728. template <class P1, class P2, class P3, bool F(P1, P2, P3)>
  4729. void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
  4730. return F(p1, p2, p3) ? p1 : UPB_BREAK;
  4731. }
  4732. // For the string callback, which takes five params, returns the size param.
  4733. template <class P1, class P2,
  4734. void F(P1, P2, const char *, size_t, const BufferHandle *)>
  4735. size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
  4736. const BufferHandle *p5) {
  4737. F(p1, p2, p3, p4, p5);
  4738. return p4;
  4739. }
  4740. // For the string callback, which takes five params, returns the size param or
  4741. // zero.
  4742. template <class P1, class P2,
  4743. bool F(P1, P2, const char *, size_t, const BufferHandle *)>
  4744. size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
  4745. const BufferHandle *p5) {
  4746. return F(p1, p2, p3, p4, p5) ? p4 : 0;
  4747. }
  4748. // If we have a function returning void but want a function returning bool, wrap
  4749. // it in a function that returns true.
  4750. template <class P1, class P2, void F(P1, P2), class I>
  4751. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
  4752. typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
  4753. };
  4754. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4755. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
  4756. typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
  4757. };
  4758. // If our function returns void but we want one returning void*, wrap it in a
  4759. // function that returns the first argument.
  4760. template <class P1, class P2, void F(P1, P2), class I>
  4761. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
  4762. typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
  4763. };
  4764. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4765. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
  4766. typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
  4767. };
  4768. // If our function returns R* but we want one returning void*, wrap it in a
  4769. // function that casts to void*.
  4770. template <class R, class P1, class P2, R *F(P1, P2), class I>
  4771. struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
  4772. typename disable_if_same<R *, void *>::Type> {
  4773. typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
  4774. };
  4775. template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
  4776. struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
  4777. typename disable_if_same<R *, void *>::Type> {
  4778. typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
  4779. Func;
  4780. };
  4781. // If our function returns bool but we want one returning void*, wrap it in a
  4782. // function that returns either the first param or UPB_BREAK.
  4783. template <class P1, class P2, bool F(P1, P2), class I>
  4784. struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
  4785. typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
  4786. };
  4787. template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
  4788. struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
  4789. typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
  4790. Func;
  4791. };
  4792. // If our function returns void but we want one returning size_t, wrap it in a
  4793. // function that returns the size argument.
  4794. template <class P1, class P2,
  4795. void F(P1, P2, const char *, size_t, const BufferHandle *), class I>
  4796. struct MaybeWrapReturn<
  4797. Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
  4798. size_t> {
  4799. typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
  4800. ReturnStringLen<P1, P2, F>, I> Func;
  4801. };
  4802. // If our function returns bool but we want one returning size_t, wrap it in a
  4803. // function that returns either 0 or the buf size.
  4804. template <class P1, class P2,
  4805. bool F(P1, P2, const char *, size_t, const BufferHandle *), class I>
  4806. struct MaybeWrapReturn<
  4807. Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
  4808. size_t> {
  4809. typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
  4810. ReturnNOr0<P1, P2, F>, I> Func;
  4811. };
  4812. // ConvertParams ///////////////////////////////////////////////////////////////
  4813. // Template class that converts the function parameters if necessary, and
  4814. // ignores the HandlerData parameter if appropriate.
  4815. //
  4816. // Template parameter is the are FuncN function type.
  4817. template <class F, class T>
  4818. struct ConvertParams;
  4819. // Function that discards the handler data parameter.
  4820. template <class R, class P1, R F(P1)>
  4821. R IgnoreHandlerData2(void *p1, const void *hd) {
  4822. UPB_UNUSED(hd);
  4823. return F(static_cast<P1>(p1));
  4824. }
  4825. template <class R, class P1, class P2Wrapper, class P2Wrapped,
  4826. R F(P1, P2Wrapped)>
  4827. R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
  4828. UPB_UNUSED(hd);
  4829. return F(static_cast<P1>(p1), p2);
  4830. }
  4831. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4832. R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
  4833. UPB_UNUSED(hd);
  4834. return F(static_cast<P1>(p1), p2, p3);
  4835. }
  4836. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
  4837. R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
  4838. UPB_UNUSED(hd);
  4839. return F(static_cast<P1>(p1), p2, p3, p4);
  4840. }
  4841. template <class R, class P1, R F(P1, const char*, size_t)>
  4842. R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
  4843. size_t p3, const BufferHandle *handle) {
  4844. UPB_UNUSED(hd);
  4845. UPB_UNUSED(handle);
  4846. return F(static_cast<P1>(p1), p2, p3);
  4847. }
  4848. // Function that casts the handler data parameter.
  4849. template <class R, class P1, class P2, R F(P1, P2)>
  4850. R CastHandlerData2(void *c, const void *hd) {
  4851. return F(static_cast<P1>(c), static_cast<P2>(hd));
  4852. }
  4853. template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
  4854. R F(P1, P2, P3Wrapped)>
  4855. R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
  4856. return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
  4857. }
  4858. template <class R, class P1, class P2, class P3, class P4, class P5,
  4859. R F(P1, P2, P3, P4, P5)>
  4860. R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
  4861. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
  4862. }
  4863. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
  4864. R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
  4865. size_t p4, const BufferHandle *handle) {
  4866. UPB_UNUSED(handle);
  4867. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
  4868. }
  4869. // For unbound functions, ignore the handler data.
  4870. template <class R, class P1, R F(P1), class I, class T>
  4871. struct ConvertParams<Func1<R, P1, F, I>, T> {
  4872. typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
  4873. };
  4874. template <class R, class P1, class P2, R F(P1, P2), class I,
  4875. class R2, class P1_2, class P2_2, class P3_2>
  4876. struct ConvertParams<Func2<R, P1, P2, F, I>,
  4877. R2 (*)(P1_2, P2_2, P3_2)> {
  4878. typedef Func3<R, void *, const void *, P3_2,
  4879. IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
  4880. };
  4881. // For StringBuffer only; this ignores both the handler data and the
  4882. // BufferHandle.
  4883. template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
  4884. struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
  4885. typedef Func5<R, void *, const void *, const char *, size_t,
  4886. const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
  4887. I> Func;
  4888. };
  4889. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4890. class I, class T>
  4891. struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
  4892. typedef Func5<R, void *, const void *, P2, P3, P4,
  4893. IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
  4894. };
  4895. // For bound functions, cast the handler data.
  4896. template <class R, class P1, class P2, R F(P1, P2), class I, class T>
  4897. struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
  4898. typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
  4899. Func;
  4900. };
  4901. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
  4902. class R2, class P1_2, class P2_2, class P3_2>
  4903. struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
  4904. R2 (*)(P1_2, P2_2, P3_2)> {
  4905. typedef Func3<R, void *, const void *, P3_2,
  4906. CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
  4907. };
  4908. // For StringBuffer only; this ignores the BufferHandle.
  4909. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
  4910. class I, class T>
  4911. struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
  4912. typedef Func5<R, void *, const void *, const char *, size_t,
  4913. const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>,
  4914. I> Func;
  4915. };
  4916. template <class R, class P1, class P2, class P3, class P4, class P5,
  4917. R F(P1, P2, P3, P4, P5), class I, class T>
  4918. struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
  4919. typedef Func5<R, void *, const void *, P3, P4, P5,
  4920. CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
  4921. };
  4922. // utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
  4923. // variant C type.
  4924. #define TYPE_METHODS(utype, ltype, ctype, vtype) \
  4925. template <> struct CanonicalType<vtype> { \
  4926. typedef ctype Type; \
  4927. }; \
  4928. template <> \
  4929. inline bool Handlers::SetValueHandler<vtype>( \
  4930. const FieldDef *f, \
  4931. const Handlers::utype ## Handler& handler) { \
  4932. assert(!handler.registered_); \
  4933. handler.AddCleanup(this); \
  4934. handler.registered_ = true; \
  4935. return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \
  4936. } \
  4937. TYPE_METHODS(Double, double, double, double);
  4938. TYPE_METHODS(Float, float, float, float);
  4939. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T);
  4940. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T);
  4941. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T);
  4942. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T);
  4943. TYPE_METHODS(Bool, bool, bool, bool);
  4944. #ifdef UPB_TWO_32BIT_TYPES
  4945. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T);
  4946. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T);
  4947. #endif
  4948. #ifdef UPB_TWO_64BIT_TYPES
  4949. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T);
  4950. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T);
  4951. #endif
  4952. #undef TYPE_METHODS
  4953. template <> struct CanonicalType<Status*> {
  4954. typedef Status* Type;
  4955. };
  4956. // Type methods that are only one-per-canonical-type and not one-per-cvariant.
  4957. #define TYPE_METHODS(utype, ctype) \
  4958. inline bool Handlers::Set##utype##Handler(const FieldDef *f, \
  4959. const utype##Handler &h) { \
  4960. return SetValueHandler<ctype>(f, h); \
  4961. } \
  4962. TYPE_METHODS(Double, double);
  4963. TYPE_METHODS(Float, float);
  4964. TYPE_METHODS(UInt64, uint64_t);
  4965. TYPE_METHODS(UInt32, uint32_t);
  4966. TYPE_METHODS(Int64, int64_t);
  4967. TYPE_METHODS(Int32, int32_t);
  4968. TYPE_METHODS(Bool, bool);
  4969. #undef TYPE_METHODS
  4970. template <class F> struct ReturnOf;
  4971. template <class R, class P1, class P2>
  4972. struct ReturnOf<R (*)(P1, P2)> {
  4973. typedef R Return;
  4974. };
  4975. template <class R, class P1, class P2, class P3>
  4976. struct ReturnOf<R (*)(P1, P2, P3)> {
  4977. typedef R Return;
  4978. };
  4979. template <class R, class P1, class P2, class P3, class P4>
  4980. struct ReturnOf<R (*)(P1, P2, P3, P4)> {
  4981. typedef R Return;
  4982. };
  4983. template <class R, class P1, class P2, class P3, class P4, class P5>
  4984. struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
  4985. typedef R Return;
  4986. };
  4987. template<class T> const void *UniquePtrForType() {
  4988. static const char ch = 0;
  4989. return &ch;
  4990. }
  4991. template <class T>
  4992. template <class F>
  4993. inline Handler<T>::Handler(F func)
  4994. : registered_(false),
  4995. cleanup_data_(func.GetData()),
  4996. cleanup_func_(func.GetCleanup()) {
  4997. upb_handlerattr_sethandlerdata(&attr_, func.GetData());
  4998. typedef typename ReturnOf<T>::Return Return;
  4999. typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
  5000. typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
  5001. ReturnWrappedFunc;
  5002. handler_ = ReturnWrappedFunc().Call;
  5003. // Set attributes based on what templates can statically tell us about the
  5004. // user's function.
  5005. // If the original function returns void, then we know that we wrapped it to
  5006. // always return ok.
  5007. bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
  5008. attr_.SetAlwaysOk(always_ok);
  5009. // Closure parameter and return type.
  5010. attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
  5011. // We use the closure type (from the first parameter) if the return type is
  5012. // void. This is all nonsense for non START* handlers, but it doesn't matter
  5013. // because in that case the value will be ignored.
  5014. typedef typename FirstUnlessVoid<typename F::FuncInfo::Return,
  5015. typename F::FuncInfo::Closure>::value
  5016. EffectiveReturn;
  5017. attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
  5018. }
  5019. template <class T>
  5020. inline Handler<T>::~Handler() {
  5021. assert(registered_);
  5022. }
  5023. inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); }
  5024. inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); }
  5025. inline bool HandlerAttributes::SetHandlerData(const void *hd) {
  5026. return upb_handlerattr_sethandlerdata(this, hd);
  5027. }
  5028. inline const void* HandlerAttributes::handler_data() const {
  5029. return upb_handlerattr_handlerdata(this);
  5030. }
  5031. inline bool HandlerAttributes::SetClosureType(const void *type) {
  5032. return upb_handlerattr_setclosuretype(this, type);
  5033. }
  5034. inline const void* HandlerAttributes::closure_type() const {
  5035. return upb_handlerattr_closuretype(this);
  5036. }
  5037. inline bool HandlerAttributes::SetReturnClosureType(const void *type) {
  5038. return upb_handlerattr_setreturnclosuretype(this, type);
  5039. }
  5040. inline const void* HandlerAttributes::return_closure_type() const {
  5041. return upb_handlerattr_returnclosuretype(this);
  5042. }
  5043. inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) {
  5044. return upb_handlerattr_setalwaysok(this, always_ok);
  5045. }
  5046. inline bool HandlerAttributes::always_ok() const {
  5047. return upb_handlerattr_alwaysok(this);
  5048. }
  5049. inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); }
  5050. inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); }
  5051. inline const char* BufferHandle::buffer() const {
  5052. return upb_bufhandle_buf(this);
  5053. }
  5054. inline size_t BufferHandle::object_offset() const {
  5055. return upb_bufhandle_objofs(this);
  5056. }
  5057. inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) {
  5058. upb_bufhandle_setbuf(this, buf, ofs);
  5059. }
  5060. template <class T>
  5061. void BufferHandle::SetAttachedObject(const T* obj) {
  5062. upb_bufhandle_setobj(this, obj, UniquePtrForType<T>());
  5063. }
  5064. template <class T>
  5065. const T* BufferHandle::GetAttachedObject() const {
  5066. return upb_bufhandle_objtype(this) == UniquePtrForType<T>()
  5067. ? static_cast<const T *>(upb_bufhandle_obj(this))
  5068. : NULL;
  5069. }
  5070. inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) {
  5071. upb_handlers *h = upb_handlers_new(m, &h);
  5072. return reffed_ptr<Handlers>(h, &h);
  5073. }
  5074. inline reffed_ptr<const Handlers> Handlers::NewFrozen(
  5075. const MessageDef *m, upb_handlers_callback *callback,
  5076. const void *closure) {
  5077. const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure);
  5078. return reffed_ptr<const Handlers>(h, &h);
  5079. }
  5080. inline bool Handlers::IsFrozen() const { return upb_handlers_isfrozen(this); }
  5081. inline void Handlers::Ref(const void *owner) const {
  5082. upb_handlers_ref(this, owner);
  5083. }
  5084. inline void Handlers::Unref(const void *owner) const {
  5085. upb_handlers_unref(this, owner);
  5086. }
  5087. inline void Handlers::DonateRef(const void *from, const void *to) const {
  5088. upb_handlers_donateref(this, from, to);
  5089. }
  5090. inline void Handlers::CheckRef(const void *owner) const {
  5091. upb_handlers_checkref(this, owner);
  5092. }
  5093. inline const Status* Handlers::status() {
  5094. return upb_handlers_status(this);
  5095. }
  5096. inline void Handlers::ClearError() {
  5097. return upb_handlers_clearerr(this);
  5098. }
  5099. inline bool Handlers::Freeze(Status *s) {
  5100. upb::Handlers* h = this;
  5101. return upb_handlers_freeze(&h, 1, s);
  5102. }
  5103. inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) {
  5104. return upb_handlers_freeze(handlers, n, s);
  5105. }
  5106. inline bool Handlers::Freeze(const std::vector<Handlers*>& h, Status* status) {
  5107. return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status);
  5108. }
  5109. inline const MessageDef *Handlers::message_def() const {
  5110. return upb_handlers_msgdef(this);
  5111. }
  5112. inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) {
  5113. return upb_handlers_addcleanup(this, p, func);
  5114. }
  5115. inline bool Handlers::SetStartMessageHandler(
  5116. const Handlers::StartMessageHandler &handler) {
  5117. assert(!handler.registered_);
  5118. handler.registered_ = true;
  5119. handler.AddCleanup(this);
  5120. return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_);
  5121. }
  5122. inline bool Handlers::SetEndMessageHandler(
  5123. const Handlers::EndMessageHandler &handler) {
  5124. assert(!handler.registered_);
  5125. handler.registered_ = true;
  5126. handler.AddCleanup(this);
  5127. return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_);
  5128. }
  5129. inline bool Handlers::SetStartStringHandler(const FieldDef *f,
  5130. const StartStringHandler &handler) {
  5131. assert(!handler.registered_);
  5132. handler.registered_ = true;
  5133. handler.AddCleanup(this);
  5134. return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_);
  5135. }
  5136. inline bool Handlers::SetEndStringHandler(const FieldDef *f,
  5137. const EndFieldHandler &handler) {
  5138. assert(!handler.registered_);
  5139. handler.registered_ = true;
  5140. handler.AddCleanup(this);
  5141. return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_);
  5142. }
  5143. inline bool Handlers::SetStringHandler(const FieldDef *f,
  5144. const StringHandler& handler) {
  5145. assert(!handler.registered_);
  5146. handler.registered_ = true;
  5147. handler.AddCleanup(this);
  5148. return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_);
  5149. }
  5150. inline bool Handlers::SetStartSequenceHandler(
  5151. const FieldDef *f, const StartFieldHandler &handler) {
  5152. assert(!handler.registered_);
  5153. handler.registered_ = true;
  5154. handler.AddCleanup(this);
  5155. return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_);
  5156. }
  5157. inline bool Handlers::SetStartSubMessageHandler(
  5158. const FieldDef *f, const StartFieldHandler &handler) {
  5159. assert(!handler.registered_);
  5160. handler.registered_ = true;
  5161. handler.AddCleanup(this);
  5162. return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_);
  5163. }
  5164. inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f,
  5165. const EndFieldHandler &handler) {
  5166. assert(!handler.registered_);
  5167. handler.registered_ = true;
  5168. handler.AddCleanup(this);
  5169. return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_);
  5170. }
  5171. inline bool Handlers::SetEndSequenceHandler(const FieldDef *f,
  5172. const EndFieldHandler &handler) {
  5173. assert(!handler.registered_);
  5174. handler.registered_ = true;
  5175. handler.AddCleanup(this);
  5176. return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_);
  5177. }
  5178. inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) {
  5179. return upb_handlers_setsubhandlers(this, f, sub);
  5180. }
  5181. inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const {
  5182. return upb_handlers_getsubhandlers(this, f);
  5183. }
  5184. inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const {
  5185. return upb_handlers_getsubhandlers_sel(this, sel);
  5186. }
  5187. inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type,
  5188. Handlers::Selector *s) {
  5189. return upb_handlers_getselector(f, type, s);
  5190. }
  5191. inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) {
  5192. return upb_handlers_getendselector(start);
  5193. }
  5194. inline Handlers::GenericFunction *Handlers::GetHandler(
  5195. Handlers::Selector selector) {
  5196. return upb_handlers_gethandler(this, selector);
  5197. }
  5198. inline const void *Handlers::GetHandlerData(Handlers::Selector selector) {
  5199. return upb_handlers_gethandlerdata(this, selector);
  5200. }
  5201. inline BytesHandler::BytesHandler() {
  5202. upb_byteshandler_init(this);
  5203. }
  5204. inline BytesHandler::~BytesHandler() {
  5205. upb_byteshandler_uninit(this);
  5206. }
  5207. } // namespace upb
  5208. #endif // __cplusplus
  5209. #undef UPB_TWO_32BIT_TYPES
  5210. #undef UPB_TWO_64BIT_TYPES
  5211. #undef UPB_INT32_T
  5212. #undef UPB_UINT32_T
  5213. #undef UPB_INT32ALT_T
  5214. #undef UPB_UINT32ALT_T
  5215. #undef UPB_INT64_T
  5216. #undef UPB_UINT64_T
  5217. #undef UPB_INT64ALT_T
  5218. #undef UPB_UINT64ALT_T
  5219. #endif // UPB_HANDLERS_INL_H_
  5220. #endif // UPB_HANDLERS_H
  5221. /*
  5222. * upb - a minimalist implementation of protocol buffers.
  5223. *
  5224. * Copyright (c) 2010-2012 Google Inc. See LICENSE for details.
  5225. * Author: Josh Haberman <jhaberman@gmail.com>
  5226. *
  5227. * A upb_sink is an object that binds a upb_handlers object to some runtime
  5228. * state. It is the object that can actually receive data via the upb_handlers
  5229. * interface.
  5230. *
  5231. * Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
  5232. * thread-safe. You can create as many of them as you want, but each one may
  5233. * only be used in a single thread at a time.
  5234. *
  5235. * If we compare with class-based OOP, a you can think of a upb_def as an
  5236. * abstract base class, a upb_handlers as a concrete derived class, and a
  5237. * upb_sink as an object (class instance).
  5238. */
  5239. #ifndef UPB_SINK_H
  5240. #define UPB_SINK_H
  5241. #ifdef __cplusplus
  5242. namespace upb {
  5243. class BufferSource;
  5244. class BytesSink;
  5245. class Sink;
  5246. }
  5247. #endif
  5248. UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc);
  5249. UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink);
  5250. UPB_DECLARE_TYPE(upb::Sink, upb_sink);
  5251. // Internal-only struct for the sink.
  5252. struct upb_sinkframe {
  5253. UPB_PRIVATE_FOR_CPP
  5254. const upb_handlers *h;
  5255. void *closure;
  5256. // For any frames besides the top, this is the END* callback that will run
  5257. // when the subframe is popped (for example, for a "sequence" frame the frame
  5258. // above it will be a UPB_HANDLER_ENDSEQ handler). But this is only
  5259. // necessary for assertion checking inside upb_sink and can be omitted if the
  5260. // sink has only one caller.
  5261. //
  5262. // TODO(haberman): have a mechanism for ensuring that a sink only has one
  5263. // caller.
  5264. upb_selector_t selector;
  5265. };
  5266. // The maximum nesting depth that upb::Sink will allow. Matches proto2's limit.
  5267. // TODO: make this a runtime-settable property of Sink.
  5268. #define UPB_SINK_MAX_NESTING 64
  5269. // A upb::Sink is an object that binds a upb::Handlers object to some runtime
  5270. // state. It represents an endpoint to which data can be sent.
  5271. //
  5272. // TODO(haberman): right now all of these functions take selectors. Should they
  5273. // take selectorbase instead?
  5274. //
  5275. // ie. instead of calling:
  5276. // sink->StartString(FOO_FIELD_START_STRING, ...)
  5277. // a selector base would let you say:
  5278. // sink->StartString(FOO_FIELD, ...)
  5279. //
  5280. // This would make call sites a little nicer and require emitting fewer selector
  5281. // definitions in .h files.
  5282. //
  5283. // But the current scheme has the benefit that you can retrieve a function
  5284. // pointer for any handler with handlers->GetHandler(selector), without having
  5285. // to have a separate GetHandler() function for each handler type. The JIT
  5286. // compiler uses this. To accommodate we'd have to expose a separate
  5287. // GetHandler() for every handler type.
  5288. //
  5289. // Also to ponder: selectors right now are independent of a specific Handlers
  5290. // instance. In other words, they allocate a number to every possible handler
  5291. // that *could* be registered, without knowing anything about what handlers
  5292. // *are* registered. That means that using selectors as table offsets prohibits
  5293. // us from compacting the handler table at Freeze() time. If the table is very
  5294. // sparse, this could be wasteful.
  5295. //
  5296. // Having another selector-like thing that is specific to a Handlers instance
  5297. // would allow this compacting, but then it would be impossible to write code
  5298. // ahead-of-time that can be bound to any Handlers instance at runtime. For
  5299. // example, a .proto file parser written as straight C will not know what
  5300. // Handlers it will be bound to, so when it calls sink->StartString() what
  5301. // selector will it pass? It needs a selector like we have today, that is
  5302. // independent of any particular upb::Handlers.
  5303. //
  5304. // Is there a way then to allow Handlers table compaction?
  5305. UPB_DEFINE_CLASS0(upb::Sink,
  5306. public:
  5307. // Constructor with no initialization; must be Reset() before use.
  5308. Sink() {}
  5309. // Constructs a new sink for the given frozen handlers and closure.
  5310. //
  5311. // TODO: once the Handlers know the expected closure type, verify that T
  5312. // matches it.
  5313. template <class T> Sink(const Handlers* handlers, T* closure);
  5314. // Resets the value of the sink.
  5315. template <class T> void Reset(const Handlers* handlers, T* closure);
  5316. // Returns the top-level object that is bound to this sink.
  5317. //
  5318. // TODO: once the Handlers know the expected closure type, verify that T
  5319. // matches it.
  5320. template <class T> T* GetObject() const;
  5321. // Functions for pushing data into the sink.
  5322. //
  5323. // These return false if processing should stop (either due to error or just
  5324. // to suspend).
  5325. //
  5326. // These may not be called from within one of the same sink's handlers (in
  5327. // other words, handlers are not re-entrant).
  5328. // Should be called at the start and end of every message; both the top-level
  5329. // message and submessages. This means that submessages should use the
  5330. // following sequence:
  5331. // sink->StartSubMessage(startsubmsg_selector);
  5332. // sink->StartMessage();
  5333. // // ...
  5334. // sink->EndMessage(&status);
  5335. // sink->EndSubMessage(endsubmsg_selector);
  5336. bool StartMessage();
  5337. bool EndMessage(Status* status);
  5338. // Putting of individual values. These work for both repeated and
  5339. // non-repeated fields, but for repeated fields you must wrap them in
  5340. // calls to StartSequence()/EndSequence().
  5341. bool PutInt32(Handlers::Selector s, int32_t val);
  5342. bool PutInt64(Handlers::Selector s, int64_t val);
  5343. bool PutUInt32(Handlers::Selector s, uint32_t val);
  5344. bool PutUInt64(Handlers::Selector s, uint64_t val);
  5345. bool PutFloat(Handlers::Selector s, float val);
  5346. bool PutDouble(Handlers::Selector s, double val);
  5347. bool PutBool(Handlers::Selector s, bool val);
  5348. // Putting of string/bytes values. Each string can consist of zero or more
  5349. // non-contiguous buffers of data.
  5350. //
  5351. // For StartString(), the function will write a sink for the string to "sub."
  5352. // The sub-sink must be used for any/all PutStringBuffer() calls.
  5353. bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub);
  5354. size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len,
  5355. const BufferHandle *handle);
  5356. bool EndString(Handlers::Selector s);
  5357. // For submessage fields.
  5358. //
  5359. // For StartSubMessage(), the function will write a sink for the string to
  5360. // "sub." The sub-sink must be used for any/all handlers called within the
  5361. // submessage.
  5362. bool StartSubMessage(Handlers::Selector s, Sink* sub);
  5363. bool EndSubMessage(Handlers::Selector s);
  5364. // For repeated fields of any type, the sequence of values must be wrapped in
  5365. // these calls.
  5366. //
  5367. // For StartSequence(), the function will write a sink for the string to
  5368. // "sub." The sub-sink must be used for any/all handlers called within the
  5369. // sequence.
  5370. bool StartSequence(Handlers::Selector s, Sink* sub);
  5371. bool EndSequence(Handlers::Selector s);
  5372. // Copy and assign specifically allowed.
  5373. // We don't even bother making these members private because so many
  5374. // functions need them and this is mainly just a dumb data container anyway.
  5375. ,
  5376. UPB_DEFINE_STRUCT0(upb_sink,
  5377. const upb_handlers *handlers;
  5378. void *closure;
  5379. ));
  5380. UPB_DEFINE_CLASS0(upb::BytesSink,
  5381. public:
  5382. BytesSink() {}
  5383. // Constructs a new sink for the given frozen handlers and closure.
  5384. //
  5385. // TODO(haberman): once the Handlers know the expected closure type, verify
  5386. // that T matches it.
  5387. template <class T> BytesSink(const BytesHandler* handler, T* closure);
  5388. // Resets the value of the sink.
  5389. template <class T> void Reset(const BytesHandler* handler, T* closure);
  5390. bool Start(size_t size_hint, void **subc);
  5391. size_t PutBuffer(void *subc, const char *buf, size_t len,
  5392. const BufferHandle *handle);
  5393. bool End();
  5394. ,
  5395. UPB_DEFINE_STRUCT0(upb_bytessink,
  5396. const upb_byteshandler *handler;
  5397. void *closure;
  5398. ));
  5399. // A class for pushing a flat buffer of data to a BytesSink.
  5400. // You can construct an instance of this to get a resumable source,
  5401. // or just call the static PutBuffer() to do a non-resumable push all in one go.
  5402. UPB_DEFINE_CLASS0(upb::BufferSource,
  5403. public:
  5404. BufferSource();
  5405. BufferSource(const char* buf, size_t len, BytesSink* sink);
  5406. // Returns true if the entire buffer was pushed successfully. Otherwise the
  5407. // next call to PutNext() will resume where the previous one left off.
  5408. // TODO(haberman): implement this.
  5409. bool PutNext();
  5410. // A static version; with this version is it not possible to resume in the
  5411. // case of failure or a partially-consumed buffer.
  5412. static bool PutBuffer(const char* buf, size_t len, BytesSink* sink);
  5413. template <class T> static bool PutBuffer(const T& str, BytesSink* sink) {
  5414. return PutBuffer(str.c_str(), str.size(), sink);
  5415. }
  5416. ,
  5417. UPB_DEFINE_STRUCT0(upb_bufsrc,
  5418. ));
  5419. UPB_BEGIN_EXTERN_C // {
  5420. // Inline definitions.
  5421. UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
  5422. void *closure) {
  5423. s->handler = h;
  5424. s->closure = closure;
  5425. }
  5426. UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
  5427. void **subc) {
  5428. *subc = s->closure;
  5429. if (!s->handler) return true;
  5430. upb_startstr_handlerfunc *start =
  5431. (upb_startstr_handlerfunc *)s->handler->table[UPB_STARTSTR_SELECTOR].func;
  5432. if (!start) return true;
  5433. *subc = start(s->closure, upb_handlerattr_handlerdata(
  5434. &s->handler->table[UPB_STARTSTR_SELECTOR].attr),
  5435. size_hint);
  5436. return *subc != NULL;
  5437. }
  5438. UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc,
  5439. const char *buf, size_t size,
  5440. const upb_bufhandle* handle) {
  5441. if (!s->handler) return true;
  5442. upb_string_handlerfunc *putbuf =
  5443. (upb_string_handlerfunc *)s->handler->table[UPB_STRING_SELECTOR].func;
  5444. if (!putbuf) return true;
  5445. return putbuf(subc, upb_handlerattr_handlerdata(
  5446. &s->handler->table[UPB_STRING_SELECTOR].attr),
  5447. buf, size, handle);
  5448. }
  5449. UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) {
  5450. if (!s->handler) return true;
  5451. upb_endfield_handlerfunc *end =
  5452. (upb_endfield_handlerfunc *)s->handler->table[UPB_ENDSTR_SELECTOR].func;
  5453. if (!end) return true;
  5454. return end(s->closure,
  5455. upb_handlerattr_handlerdata(
  5456. &s->handler->table[UPB_ENDSTR_SELECTOR].attr));
  5457. }
  5458. UPB_INLINE bool upb_bufsrc_putbuf(const char *buf, size_t len,
  5459. upb_bytessink *sink) {
  5460. void *subc;
  5461. upb_bufhandle handle;
  5462. upb_bufhandle_init(&handle);
  5463. upb_bufhandle_setbuf(&handle, buf, 0);
  5464. bool ret = upb_bytessink_start(sink, len, &subc);
  5465. if (ret && len != 0) {
  5466. ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) == len);
  5467. }
  5468. if (ret) {
  5469. ret = upb_bytessink_end(sink);
  5470. }
  5471. upb_bufhandle_uninit(&handle);
  5472. return ret;
  5473. }
  5474. #define PUTVAL(type, ctype) \
  5475. UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \
  5476. ctype val) { \
  5477. if (!s->handlers) return true; \
  5478. upb_##type##_handlerfunc *func = \
  5479. (upb_##type##_handlerfunc *)upb_handlers_gethandler(s->handlers, sel); \
  5480. if (!func) return true; \
  5481. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel); \
  5482. return func(s->closure, hd, val); \
  5483. }
  5484. PUTVAL(int32, int32_t);
  5485. PUTVAL(int64, int64_t);
  5486. PUTVAL(uint32, uint32_t);
  5487. PUTVAL(uint64, uint64_t);
  5488. PUTVAL(float, float);
  5489. PUTVAL(double, double);
  5490. PUTVAL(bool, bool);
  5491. #undef PUTVAL
  5492. UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
  5493. s->handlers = h;
  5494. s->closure = c;
  5495. }
  5496. UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel,
  5497. const char *buf, size_t n,
  5498. const upb_bufhandle *handle) {
  5499. if (!s->handlers) return n;
  5500. upb_string_handlerfunc *handler =
  5501. (upb_string_handlerfunc *)upb_handlers_gethandler(s->handlers, sel);
  5502. if (!handler) return n;
  5503. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5504. return handler(s->closure, hd, buf, n, handle);
  5505. }
  5506. UPB_INLINE bool upb_sink_startmsg(upb_sink *s) {
  5507. if (!s->handlers) return true;
  5508. upb_startmsg_handlerfunc *startmsg =
  5509. (upb_startmsg_handlerfunc *)upb_handlers_gethandler(s->handlers,
  5510. UPB_STARTMSG_SELECTOR);
  5511. if (!startmsg) return true;
  5512. const void *hd =
  5513. upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR);
  5514. return startmsg(s->closure, hd);
  5515. }
  5516. UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) {
  5517. if (!s->handlers) return true;
  5518. upb_endmsg_handlerfunc *endmsg =
  5519. (upb_endmsg_handlerfunc *)upb_handlers_gethandler(s->handlers,
  5520. UPB_ENDMSG_SELECTOR);
  5521. if (!endmsg) return true;
  5522. const void *hd =
  5523. upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR);
  5524. return endmsg(s->closure, hd, status);
  5525. }
  5526. UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel,
  5527. upb_sink *sub) {
  5528. sub->closure = s->closure;
  5529. sub->handlers = s->handlers;
  5530. if (!s->handlers) return true;
  5531. upb_startfield_handlerfunc *startseq =
  5532. (upb_startfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
  5533. if (!startseq) return true;
  5534. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5535. sub->closure = startseq(s->closure, hd);
  5536. return sub->closure ? true : false;
  5537. }
  5538. UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) {
  5539. if (!s->handlers) return true;
  5540. upb_endfield_handlerfunc *endseq =
  5541. (upb_endfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
  5542. if (!endseq) return true;
  5543. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5544. return endseq(s->closure, hd);
  5545. }
  5546. UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel,
  5547. size_t size_hint, upb_sink *sub) {
  5548. sub->closure = s->closure;
  5549. sub->handlers = s->handlers;
  5550. if (!s->handlers) return true;
  5551. upb_startstr_handlerfunc *startstr =
  5552. (upb_startstr_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
  5553. if (!startstr) return true;
  5554. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5555. sub->closure = startstr(s->closure, hd, size_hint);
  5556. return sub->closure ? true : false;
  5557. }
  5558. UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) {
  5559. if (!s->handlers) return true;
  5560. upb_endfield_handlerfunc *endstr =
  5561. (upb_endfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
  5562. if (!endstr) return true;
  5563. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5564. return endstr(s->closure, hd);
  5565. }
  5566. UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel,
  5567. upb_sink *sub) {
  5568. sub->closure = s->closure;
  5569. if (!s->handlers) {
  5570. sub->handlers = NULL;
  5571. return true;
  5572. }
  5573. sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel);
  5574. upb_startfield_handlerfunc *startsubmsg =
  5575. (upb_startfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
  5576. if (!startsubmsg) return true;
  5577. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5578. sub->closure = startsubmsg(s->closure, hd);
  5579. return sub->closure ? true : false;
  5580. }
  5581. UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) {
  5582. if (!s->handlers) return true;
  5583. upb_endfield_handlerfunc *endsubmsg =
  5584. (upb_endfield_handlerfunc*)upb_handlers_gethandler(s->handlers, sel);
  5585. if (!endsubmsg) return s->closure;
  5586. const void *hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5587. return endsubmsg(s->closure, hd);
  5588. }
  5589. UPB_END_EXTERN_C // }
  5590. #ifdef __cplusplus
  5591. namespace upb {
  5592. template <class T> Sink::Sink(const Handlers* handlers, T* closure) {
  5593. upb_sink_reset(this, handlers, closure);
  5594. }
  5595. template <class T>
  5596. inline void Sink::Reset(const Handlers* handlers, T* closure) {
  5597. upb_sink_reset(this, handlers, closure);
  5598. }
  5599. inline bool Sink::StartMessage() {
  5600. return upb_sink_startmsg(this);
  5601. }
  5602. inline bool Sink::EndMessage(Status* status) {
  5603. return upb_sink_endmsg(this, status);
  5604. }
  5605. inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) {
  5606. return upb_sink_putint32(this, sel, val);
  5607. }
  5608. inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) {
  5609. return upb_sink_putint64(this, sel, val);
  5610. }
  5611. inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) {
  5612. return upb_sink_putuint32(this, sel, val);
  5613. }
  5614. inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) {
  5615. return upb_sink_putuint64(this, sel, val);
  5616. }
  5617. inline bool Sink::PutFloat(Handlers::Selector sel, float val) {
  5618. return upb_sink_putfloat(this, sel, val);
  5619. }
  5620. inline bool Sink::PutDouble(Handlers::Selector sel, double val) {
  5621. return upb_sink_putdouble(this, sel, val);
  5622. }
  5623. inline bool Sink::PutBool(Handlers::Selector sel, bool val) {
  5624. return upb_sink_putbool(this, sel, val);
  5625. }
  5626. inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint,
  5627. Sink *sub) {
  5628. return upb_sink_startstr(this, sel, size_hint, sub);
  5629. }
  5630. inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf,
  5631. size_t len, const BufferHandle* handle) {
  5632. return upb_sink_putstring(this, sel, buf, len, handle);
  5633. }
  5634. inline bool Sink::EndString(Handlers::Selector sel) {
  5635. return upb_sink_endstr(this, sel);
  5636. }
  5637. inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) {
  5638. return upb_sink_startsubmsg(this, sel, sub);
  5639. }
  5640. inline bool Sink::EndSubMessage(Handlers::Selector sel) {
  5641. return upb_sink_endsubmsg(this, sel);
  5642. }
  5643. inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) {
  5644. return upb_sink_startseq(this, sel, sub);
  5645. }
  5646. inline bool Sink::EndSequence(Handlers::Selector sel) {
  5647. return upb_sink_endseq(this, sel);
  5648. }
  5649. template <class T>
  5650. BytesSink::BytesSink(const BytesHandler* handler, T* closure) {
  5651. Reset(handler, closure);
  5652. }
  5653. template <class T>
  5654. void BytesSink::Reset(const BytesHandler *handler, T *closure) {
  5655. upb_bytessink_reset(this, handler, closure);
  5656. }
  5657. inline bool BytesSink::Start(size_t size_hint, void **subc) {
  5658. return upb_bytessink_start(this, size_hint, subc);
  5659. }
  5660. inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len,
  5661. const BufferHandle *handle) {
  5662. return upb_bytessink_putbuf(this, subc, buf, len, handle);
  5663. }
  5664. inline bool BytesSink::End() {
  5665. return upb_bytessink_end(this);
  5666. }
  5667. inline bool BufferSource::PutBuffer(const char *buf, size_t len,
  5668. BytesSink *sink) {
  5669. return upb_bufsrc_putbuf(buf, len, sink);
  5670. }
  5671. } // namespace upb
  5672. #endif
  5673. #endif
  5674. /*
  5675. * upb - a minimalist implementation of protocol buffers.
  5676. *
  5677. * Copyright (c) 2013 Google Inc. See LICENSE for details.
  5678. * Author: Josh Haberman <jhaberman@gmail.com>
  5679. *
  5680. * For handlers that do very tiny, very simple operations, the function call
  5681. * overhead of calling a handler can be significant. This file allows the
  5682. * user to define handlers that do something very simple like store the value
  5683. * to memory and/or set a hasbit. JIT compilers can then special-case these
  5684. * handlers and emit specialized code for them instead of actually calling the
  5685. * handler.
  5686. *
  5687. * The functionality is very simple/limited right now but may expand to be able
  5688. * to call another function.
  5689. */
  5690. #ifndef UPB_SHIM_H
  5691. #define UPB_SHIM_H
  5692. typedef struct {
  5693. size_t offset;
  5694. int32_t hasbit;
  5695. } upb_shim_data;
  5696. #ifdef __cplusplus
  5697. namespace upb {
  5698. struct Shim {
  5699. typedef upb_shim_data Data;
  5700. // Sets a handler for the given field that writes the value to the given
  5701. // offset and, if hasbit >= 0, sets a bit at the given bit offset. Returns
  5702. // true if the handler was set successfully.
  5703. static bool Set(Handlers *h, const FieldDef *f, size_t ofs, int32_t hasbit);
  5704. // If this handler is a shim, returns the corresponding upb::Shim::Data and
  5705. // stores the type in "type". Otherwise returns NULL.
  5706. static const Data* GetData(const Handlers* h, Handlers::Selector s,
  5707. FieldDef::Type* type);
  5708. };
  5709. } // namespace upb
  5710. #endif
  5711. UPB_BEGIN_EXTERN_C // {
  5712. // C API.
  5713. bool upb_shim_set(upb_handlers *h, const upb_fielddef *f, size_t offset,
  5714. int32_t hasbit);
  5715. const upb_shim_data *upb_shim_getdata(const upb_handlers *h, upb_selector_t s,
  5716. upb_fieldtype_t *type);
  5717. UPB_END_EXTERN_C // }
  5718. #ifdef __cplusplus
  5719. // C++ Wrappers.
  5720. namespace upb {
  5721. inline bool Shim::Set(Handlers* h, const FieldDef* f, size_t ofs,
  5722. int32_t hasbit) {
  5723. return upb_shim_set(h, f, ofs, hasbit);
  5724. }
  5725. inline const Shim::Data* Shim::GetData(const Handlers* h, Handlers::Selector s,
  5726. FieldDef::Type* type) {
  5727. return upb_shim_getdata(h, s, type);
  5728. }
  5729. } // namespace upb
  5730. #endif
  5731. #endif // UPB_SHIM_H
  5732. /*
  5733. * upb - a minimalist implementation of protocol buffers.
  5734. *
  5735. * Copyright (c) 2011 Google Inc. See LICENSE for details.
  5736. * Author: Josh Haberman <jhaberman@gmail.com>
  5737. *
  5738. * upb::descriptor::Reader provides a way of building upb::Defs from
  5739. * data in descriptor.proto format.
  5740. */
  5741. #ifndef UPB_DESCRIPTOR_H
  5742. #define UPB_DESCRIPTOR_H
  5743. #ifdef __cplusplus
  5744. namespace upb {
  5745. namespace descriptor {
  5746. class Reader;
  5747. } // namespace descriptor
  5748. } // namespace upb
  5749. #endif
  5750. UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader);
  5751. // Internal-only structs used by Reader.
  5752. // upb_deflist is an internal-only dynamic array for storing a growing list of
  5753. // upb_defs.
  5754. typedef struct {
  5755. UPB_PRIVATE_FOR_CPP
  5756. upb_def **defs;
  5757. size_t len;
  5758. size_t size;
  5759. bool owned;
  5760. } upb_deflist;
  5761. // We keep a stack of all the messages scopes we are currently in, as well as
  5762. // the top-level file scope. This is necessary to correctly qualify the
  5763. // definitions that are contained inside. "name" tracks the name of the
  5764. // message or package (a bare name -- not qualified by any enclosing scopes).
  5765. typedef struct {
  5766. UPB_PRIVATE_FOR_CPP
  5767. char *name;
  5768. // Index of the first def that is under this scope. For msgdefs, the
  5769. // msgdef itself is at start-1.
  5770. int start;
  5771. } upb_descreader_frame;
  5772. // The maximum number of nested declarations that are allowed, ie.
  5773. // message Foo {
  5774. // message Bar {
  5775. // message Baz {
  5776. // }
  5777. // }
  5778. // }
  5779. //
  5780. // This is a resource limit that affects how big our runtime stack can grow.
  5781. // TODO: make this a runtime-settable property of the Reader instance.
  5782. #define UPB_MAX_MESSAGE_NESTING 64
  5783. // Class that receives descriptor data according to the descriptor.proto schema
  5784. // and use it to build upb::Defs corresponding to that schema.
  5785. UPB_DEFINE_CLASS0(upb::descriptor::Reader,
  5786. public:
  5787. // These handlers must have come from NewHandlers() and must outlive the
  5788. // Reader.
  5789. //
  5790. // TODO: generate the handlers statically (like we do with the
  5791. // descriptor.proto defs) so that there is no need to pass this parameter (or
  5792. // to build/memory-manage the handlers at runtime at all). Unfortunately this
  5793. // is a bit tricky to implement for Handlers, but necessary to simplify this
  5794. // interface.
  5795. Reader(const Handlers* handlers, Status* status);
  5796. ~Reader();
  5797. // Resets the reader's state and discards any defs it may have built.
  5798. void Reset();
  5799. // The reader's input; this is where descriptor.proto data should be sent.
  5800. Sink* input();
  5801. // Returns an array of all defs that have been parsed, and transfers ownership
  5802. // of them to "owner". The number of defs is stored in *n. Ownership of the
  5803. // returned array is retained and is invalidated by any other call into
  5804. // Reader.
  5805. //
  5806. // These defs are not frozen or resolved; they are ready to be added to a
  5807. // symtab.
  5808. upb::Def** GetDefs(void* owner, int* n);
  5809. // Builds and returns handlers for the reader, owned by "owner."
  5810. static Handlers* NewHandlers(const void* owner);
  5811. ,
  5812. UPB_DEFINE_STRUCT0(upb_descreader,
  5813. upb_sink sink;
  5814. upb_deflist defs;
  5815. upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING];
  5816. int stack_len;
  5817. uint32_t number;
  5818. char *name;
  5819. bool saw_number;
  5820. bool saw_name;
  5821. char *default_string;
  5822. upb_fielddef *f;
  5823. ));
  5824. UPB_BEGIN_EXTERN_C // {
  5825. // C API.
  5826. void upb_descreader_init(upb_descreader *r, const upb_handlers *handlers,
  5827. upb_status *status);
  5828. void upb_descreader_uninit(upb_descreader *r);
  5829. void upb_descreader_reset(upb_descreader *r);
  5830. upb_sink *upb_descreader_input(upb_descreader *r);
  5831. upb_def **upb_descreader_getdefs(upb_descreader *r, void *owner, int *n);
  5832. const upb_handlers *upb_descreader_newhandlers(const void *owner);
  5833. UPB_END_EXTERN_C // }
  5834. #ifdef __cplusplus
  5835. // C++ implementation details. /////////////////////////////////////////////////
  5836. namespace upb {
  5837. namespace descriptor {
  5838. inline Reader::Reader(const Handlers *h, Status *s) {
  5839. upb_descreader_init(this, h, s);
  5840. }
  5841. inline Reader::~Reader() { upb_descreader_uninit(this); }
  5842. inline void Reader::Reset() { upb_descreader_reset(this); }
  5843. inline Sink* Reader::input() { return upb_descreader_input(this); }
  5844. inline upb::Def** Reader::GetDefs(void* owner, int* n) {
  5845. return upb_descreader_getdefs(this, owner, n);
  5846. }
  5847. } // namespace descriptor
  5848. } // namespace upb
  5849. #endif
  5850. #endif // UPB_DESCRIPTOR_H
  5851. /*
  5852. * upb - a minimalist implementation of protocol buffers.
  5853. *
  5854. * Copyright (c) 2009-2014 Google Inc. See LICENSE for details.
  5855. * Author: Josh Haberman <jhaberman@gmail.com>
  5856. *
  5857. * Internal-only definitions for the decoder.
  5858. */
  5859. #ifndef UPB_DECODER_INT_H_
  5860. #define UPB_DECODER_INT_H_
  5861. #include <stdlib.h>
  5862. /*
  5863. * upb - a minimalist implementation of protocol buffers.
  5864. *
  5865. * Copyright (c) 2009-2014 Google Inc. See LICENSE for details.
  5866. * Author: Josh Haberman <jhaberman@gmail.com>
  5867. *
  5868. * upb::pb::Decoder implements a high performance, streaming, resumable decoder
  5869. * for the binary protobuf format.
  5870. *
  5871. * This interface works the same regardless of what decoder backend is being
  5872. * used. A client of this class does not need to know whether decoding is using
  5873. * a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
  5874. * it will always use the fastest available decoder. However, you can call
  5875. * set_allow_jit(false) to disable any JIT decoder that might be available.
  5876. * This is primarily useful for testing purposes.
  5877. */
  5878. #ifndef UPB_DECODER_H_
  5879. #define UPB_DECODER_H_
  5880. #ifdef __cplusplus
  5881. namespace upb {
  5882. namespace pb {
  5883. class CodeCache;
  5884. class Decoder;
  5885. class DecoderMethod;
  5886. class DecoderMethodOptions;
  5887. } // namespace pb
  5888. } // namespace upb
  5889. #endif
  5890. UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache);
  5891. UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder);
  5892. UPB_DECLARE_TYPE(upb::pb::DecoderMethod, upb_pbdecodermethod);
  5893. UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts);
  5894. // The maximum that any submessages can be nested. Matches proto2's limit.
  5895. // This specifies the size of the decoder's statically-sized array and therefore
  5896. // setting it high will cause the upb::pb::Decoder object to be larger.
  5897. //
  5898. // If necessary we can add a runtime-settable property to Decoder that allow
  5899. // this to be larger than the compile-time setting, but this would add
  5900. // complexity, particularly since we would have to decide how/if to give users
  5901. // the ability to set a custom memory allocation function.
  5902. #define UPB_DECODER_MAX_NESTING 64
  5903. // Internal-only struct used by the decoder.
  5904. typedef struct {
  5905. UPB_PRIVATE_FOR_CPP
  5906. // Space optimization note: we store two pointers here that the JIT
  5907. // doesn't need at all; the upb_handlers* inside the sink and
  5908. // the dispatch table pointer. We can optimze so that the JIT uses
  5909. // smaller stack frames than the interpreter. The only thing we need
  5910. // to guarantee is that the fallback routines can find end_ofs.
  5911. upb_sink sink;
  5912. // The absolute stream offset of the end-of-frame delimiter.
  5913. // Non-delimited frames (groups and non-packed repeated fields) reuse the
  5914. // delimiter of their parent, even though the frame may not end there.
  5915. //
  5916. // NOTE: the JIT stores a slightly different value here for non-top frames.
  5917. // It stores the value relative to the end of the enclosed message. But the
  5918. // top frame is still stored the same way, which is important for ensuring
  5919. // that calls from the JIT into C work correctly.
  5920. uint64_t end_ofs;
  5921. const uint32_t *base;
  5922. // 0 indicates a length-delimited field.
  5923. // A positive number indicates a known group.
  5924. // A negative number indicates an unknown group.
  5925. int32_t groupnum;
  5926. upb_inttable *dispatch; // Not used by the JIT.
  5927. } upb_pbdecoder_frame;
  5928. // The parameters one uses to construct a DecoderMethod.
  5929. // TODO(haberman): move allowjit here? Seems more convenient for users.
  5930. UPB_DEFINE_CLASS0(upb::pb::DecoderMethodOptions,
  5931. public:
  5932. // Parameter represents the destination handlers that this method will push
  5933. // to.
  5934. explicit DecoderMethodOptions(const Handlers* dest_handlers);
  5935. // Should the decoder push submessages to lazy handlers for fields that have
  5936. // them? The caller should set this iff the lazy handlers expect data that is
  5937. // in protobuf binary format and the caller wishes to lazy parse it.
  5938. void set_lazy(bool lazy);
  5939. ,
  5940. UPB_DEFINE_STRUCT0(upb_pbdecodermethodopts,
  5941. const upb_handlers *handlers;
  5942. bool lazy;
  5943. ));
  5944. // Represents the code to parse a protobuf according to a destination Handlers.
  5945. UPB_DEFINE_CLASS1(upb::pb::DecoderMethod, upb::RefCounted,
  5946. public:
  5947. // From upb::ReferenceCounted.
  5948. void Ref(const void* owner) const;
  5949. void Unref(const void* owner) const;
  5950. void DonateRef(const void* from, const void* to) const;
  5951. void CheckRef(const void* owner) const;
  5952. // The destination handlers that are statically bound to this method.
  5953. // This method is only capable of outputting to a sink that uses these
  5954. // handlers.
  5955. const Handlers* dest_handlers() const;
  5956. // The input handlers for this decoder method.
  5957. const BytesHandler* input_handler() const;
  5958. // Whether this method is native.
  5959. bool is_native() const;
  5960. // Convenience method for generating a DecoderMethod without explicitly
  5961. // creating a CodeCache.
  5962. static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts);
  5963. private:
  5964. UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod);
  5965. ,
  5966. UPB_DEFINE_STRUCT(upb_pbdecodermethod, upb_refcounted,
  5967. // While compiling, the base is relative in "ofs", after compiling it is
  5968. // absolute in "ptr".
  5969. union {
  5970. uint32_t ofs; // PC offset of method.
  5971. void *ptr; // Pointer to bytecode or machine code for this method.
  5972. } code_base;
  5973. // The decoder method group to which this method belongs. We own a ref.
  5974. // Owning a ref on the entire group is more coarse-grained than is strictly
  5975. // necessary; all we truly require is that methods we directly reference
  5976. // outlive us, while the group could contain many other messages we don't
  5977. // require. But the group represents the messages that were
  5978. // allocated+compiled together, so it makes the most sense to free them
  5979. // together also.
  5980. const upb_refcounted *group;
  5981. // Whether this method is native code or bytecode.
  5982. bool is_native_;
  5983. // The handler one calls to invoke this method.
  5984. upb_byteshandler input_handler_;
  5985. // The destination handlers this method is bound to. We own a ref.
  5986. const upb_handlers *dest_handlers_;
  5987. // Dispatch table -- used by both bytecode decoder and JIT when encountering a
  5988. // field number that wasn't the one we were expecting to see. See
  5989. // decoder.int.h for the layout of this table.
  5990. upb_inttable dispatch;
  5991. ));
  5992. // A Decoder receives binary protobuf data on its input sink and pushes the
  5993. // decoded data to its output sink.
  5994. UPB_DEFINE_CLASS0(upb::pb::Decoder,
  5995. public:
  5996. // Constructs a decoder instance for the given method, which must outlive this
  5997. // decoder. Any errors during parsing will be set on the given status, which
  5998. // must also outlive this decoder.
  5999. Decoder(const DecoderMethod* method, Status* status);
  6000. ~Decoder();
  6001. // Returns the DecoderMethod this decoder is parsing from.
  6002. // TODO(haberman): Do users need to be able to rebind this?
  6003. const DecoderMethod* method() const;
  6004. // Resets the state of the decoder.
  6005. void Reset();
  6006. // Returns number of bytes successfully parsed.
  6007. //
  6008. // This can be useful for determining the stream position where an error
  6009. // occurred.
  6010. //
  6011. // This value may not be up-to-date when called from inside a parsing
  6012. // callback.
  6013. uint64_t BytesParsed() const;
  6014. // Resets the output sink of the Decoder.
  6015. // The given sink must match method()->dest_handlers().
  6016. //
  6017. // This must be called at least once before the decoder can be used. It may
  6018. // only be called with the decoder is in a state where it was just created or
  6019. // reset with pipeline.Reset(). The given sink must be from the same pipeline
  6020. // as this decoder.
  6021. bool ResetOutput(Sink* sink);
  6022. // The sink on which this decoder receives input.
  6023. BytesSink* input();
  6024. private:
  6025. UPB_DISALLOW_COPY_AND_ASSIGN(Decoder);
  6026. ,
  6027. UPB_DEFINE_STRUCT0(upb_pbdecoder, UPB_QUOTE(
  6028. // Our input sink.
  6029. upb_bytessink input_;
  6030. // The decoder method we are parsing with (owned).
  6031. const upb_pbdecodermethod *method_;
  6032. size_t call_len;
  6033. const uint32_t *pc, *last;
  6034. // Current input buffer and its stream offset.
  6035. const char *buf, *ptr, *end, *checkpoint;
  6036. // End of the delimited region, relative to ptr, or NULL if not in this buf.
  6037. const char *delim_end;
  6038. // End of the delimited region, relative to ptr, or end if not in this buf.
  6039. const char *data_end;
  6040. // Overall stream offset of "buf."
  6041. uint64_t bufstart_ofs;
  6042. // Buffer for residual bytes not parsed from the previous buffer.
  6043. // The maximum number of residual bytes we require is 12; a five-byte
  6044. // unknown tag plus an eight-byte value, less one because the value
  6045. // is only a partial value.
  6046. char residual[12];
  6047. char *residual_end;
  6048. // Stores the user buffer passed to our decode function.
  6049. const char *buf_param;
  6050. size_t size_param;
  6051. const upb_bufhandle *handle;
  6052. #ifdef UPB_USE_JIT_X64
  6053. // Used momentarily by the generated code to store a value while a user
  6054. // function is called.
  6055. uint32_t tmp_len;
  6056. const void *saved_rsp;
  6057. #endif
  6058. upb_status *status;
  6059. // Our internal stack.
  6060. upb_pbdecoder_frame *top, *limit;
  6061. upb_pbdecoder_frame stack[UPB_DECODER_MAX_NESTING];
  6062. #ifdef UPB_USE_JIT_X64
  6063. // Each native stack frame needs two pointers, plus we need a few frames for
  6064. // the enter/exit trampolines.
  6065. const uint32_t *callstack[(UPB_DECODER_MAX_NESTING * 2) + 10];
  6066. #else
  6067. const uint32_t *callstack[UPB_DECODER_MAX_NESTING];
  6068. #endif
  6069. )));
  6070. // A class for caching protobuf processing code, whether bytecode for the
  6071. // interpreted decoder or machine code for the JIT.
  6072. //
  6073. // This class is not thread-safe.
  6074. UPB_DEFINE_CLASS0(upb::pb::CodeCache,
  6075. public:
  6076. CodeCache();
  6077. ~CodeCache();
  6078. // Whether the cache is allowed to generate machine code. Defaults to true.
  6079. // There is no real reason to turn it off except for testing or if you are
  6080. // having a specific problem with the JIT.
  6081. //
  6082. // Note that allow_jit = true does not *guarantee* that the code will be JIT
  6083. // compiled. If this platform is not supported or the JIT was not compiled
  6084. // in, the code may still be interpreted.
  6085. bool allow_jit() const;
  6086. // This may only be called when the object is first constructed, and prior to
  6087. // any code generation, otherwise returns false and does nothing.
  6088. bool set_allow_jit(bool allow);
  6089. // Returns a DecoderMethod that can push data to the given handlers.
  6090. // If a suitable method already exists, it will be returned from the cache.
  6091. //
  6092. // Specifying the destination handlers here allows the DecoderMethod to be
  6093. // statically bound to the destination handlers if possible, which can allow
  6094. // more efficient decoding. However the returned method may or may not
  6095. // actually be statically bound. But in all cases, the returned method can
  6096. // push data to the given handlers.
  6097. const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts);
  6098. // If/when someone needs to explicitly create a dynamically-bound
  6099. // DecoderMethod*, we can add a method to get it here.
  6100. private:
  6101. UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache);
  6102. ,
  6103. UPB_DEFINE_STRUCT0(upb_pbcodecache,
  6104. bool allow_jit_;
  6105. // Array of mgroups.
  6106. upb_inttable groups;
  6107. ));
  6108. UPB_BEGIN_EXTERN_C // {
  6109. void upb_pbdecoder_init(upb_pbdecoder *d, const upb_pbdecodermethod *method,
  6110. upb_status *status);
  6111. void upb_pbdecoder_uninit(upb_pbdecoder *d);
  6112. void upb_pbdecoder_reset(upb_pbdecoder *d);
  6113. const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
  6114. bool upb_pbdecoder_resetoutput(upb_pbdecoder *d, upb_sink *sink);
  6115. upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
  6116. uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
  6117. void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
  6118. const upb_handlers *h);
  6119. void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy);
  6120. void upb_pbdecodermethod_ref(const upb_pbdecodermethod *m, const void *owner);
  6121. void upb_pbdecodermethod_unref(const upb_pbdecodermethod *m, const void *owner);
  6122. void upb_pbdecodermethod_donateref(const upb_pbdecodermethod *m,
  6123. const void *from, const void *to);
  6124. void upb_pbdecodermethod_checkref(const upb_pbdecodermethod *m,
  6125. const void *owner);
  6126. const upb_handlers *upb_pbdecodermethod_desthandlers(
  6127. const upb_pbdecodermethod *m);
  6128. const upb_byteshandler *upb_pbdecodermethod_inputhandler(
  6129. const upb_pbdecodermethod *m);
  6130. bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
  6131. const upb_pbdecodermethod *upb_pbdecodermethod_new(
  6132. const upb_pbdecodermethodopts *opts, const void *owner);
  6133. void upb_pbcodecache_init(upb_pbcodecache *c);
  6134. void upb_pbcodecache_uninit(upb_pbcodecache *c);
  6135. bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
  6136. bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
  6137. const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
  6138. upb_pbcodecache *c, const upb_pbdecodermethodopts *opts);
  6139. UPB_END_EXTERN_C // }
  6140. #ifdef __cplusplus
  6141. namespace upb {
  6142. namespace pb {
  6143. inline Decoder::Decoder(const DecoderMethod* m, Status* s) {
  6144. upb_pbdecoder_init(this, m, s);
  6145. }
  6146. inline Decoder::~Decoder() {
  6147. upb_pbdecoder_uninit(this);
  6148. }
  6149. inline const DecoderMethod* Decoder::method() const {
  6150. return upb_pbdecoder_method(this);
  6151. }
  6152. inline void Decoder::Reset() {
  6153. upb_pbdecoder_reset(this);
  6154. }
  6155. inline uint64_t Decoder::BytesParsed() const {
  6156. return upb_pbdecoder_bytesparsed(this);
  6157. }
  6158. inline bool Decoder::ResetOutput(Sink* sink) {
  6159. return upb_pbdecoder_resetoutput(this, sink);
  6160. }
  6161. inline BytesSink* Decoder::input() {
  6162. return upb_pbdecoder_input(this);
  6163. }
  6164. inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
  6165. upb_pbdecodermethodopts_init(this, h);
  6166. }
  6167. inline void DecoderMethodOptions::set_lazy(bool lazy) {
  6168. upb_pbdecodermethodopts_setlazy(this, lazy);
  6169. }
  6170. inline void DecoderMethod::Ref(const void *owner) const {
  6171. upb_pbdecodermethod_ref(this, owner);
  6172. }
  6173. inline void DecoderMethod::Unref(const void *owner) const {
  6174. upb_pbdecodermethod_unref(this, owner);
  6175. }
  6176. inline void DecoderMethod::DonateRef(const void *from, const void *to) const {
  6177. upb_pbdecodermethod_donateref(this, from, to);
  6178. }
  6179. inline void DecoderMethod::CheckRef(const void *owner) const {
  6180. upb_pbdecodermethod_checkref(this, owner);
  6181. }
  6182. inline const Handlers* DecoderMethod::dest_handlers() const {
  6183. return upb_pbdecodermethod_desthandlers(this);
  6184. }
  6185. inline const BytesHandler* DecoderMethod::input_handler() const {
  6186. return upb_pbdecodermethod_inputhandler(this);
  6187. }
  6188. inline bool DecoderMethod::is_native() const {
  6189. return upb_pbdecodermethod_isnative(this);
  6190. }
  6191. // static
  6192. inline reffed_ptr<const DecoderMethod> DecoderMethod::New(
  6193. const DecoderMethodOptions &opts) {
  6194. const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m);
  6195. return reffed_ptr<const DecoderMethod>(m, &m);
  6196. }
  6197. inline CodeCache::CodeCache() {
  6198. upb_pbcodecache_init(this);
  6199. }
  6200. inline CodeCache::~CodeCache() {
  6201. upb_pbcodecache_uninit(this);
  6202. }
  6203. inline bool CodeCache::allow_jit() const {
  6204. return upb_pbcodecache_allowjit(this);
  6205. }
  6206. inline bool CodeCache::set_allow_jit(bool allow) {
  6207. return upb_pbcodecache_setallowjit(this, allow);
  6208. }
  6209. inline const DecoderMethod *CodeCache::GetDecoderMethod(
  6210. const DecoderMethodOptions& opts) {
  6211. return upb_pbcodecache_getdecodermethod(this, &opts);
  6212. }
  6213. } // namespace pb
  6214. } // namespace upb
  6215. #endif // __cplusplus
  6216. #endif /* UPB_DECODER_H_ */
  6217. // Opcode definitions. The canonical meaning of each opcode is its
  6218. // implementation in the interpreter (the JIT is written to match this).
  6219. //
  6220. // All instructions have the opcode in the low byte.
  6221. // Instruction format for most instructions is:
  6222. //
  6223. // +-------------------+--------+
  6224. // | arg (24) | op (8) |
  6225. // +-------------------+--------+
  6226. //
  6227. // Exceptions are indicated below. A few opcodes are multi-word.
  6228. typedef enum {
  6229. // Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
  6230. // Arg for all of these is the upb selector for this field.
  6231. #define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
  6232. T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
  6233. T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
  6234. #undef T
  6235. OP_STARTMSG = 9, // No arg.
  6236. OP_ENDMSG = 10, // No arg.
  6237. OP_STARTSEQ = 11,
  6238. OP_ENDSEQ = 12,
  6239. OP_STARTSUBMSG = 14,
  6240. OP_ENDSUBMSG = 19,
  6241. OP_STARTSTR = 20,
  6242. OP_STRING = 21,
  6243. OP_ENDSTR = 22,
  6244. OP_PUSHTAGDELIM = 23, // No arg.
  6245. OP_PUSHLENDELIM = 24, // No arg.
  6246. OP_POP = 25, // No arg.
  6247. OP_SETDELIM = 26, // No arg.
  6248. OP_SETBIGGROUPNUM = 27, // two words: | unused (24) | opc || groupnum (32) |
  6249. OP_CHECKDELIM = 28,
  6250. OP_CALL = 29,
  6251. OP_RET = 30,
  6252. OP_BRANCH = 31,
  6253. // Different opcodes depending on how many bytes expected.
  6254. OP_TAG1 = 32, // | expected tag (16) | jump target (8) | opc (8) |
  6255. OP_TAG2 = 33, // | expected tag (16) | jump target (8) | opc (8) |
  6256. OP_TAGN = 34, // three words:
  6257. // | unused (16) | jump target(8) | opc (8) |
  6258. // | expected tag 1 (32) |
  6259. // | expected tag 2 (32) |
  6260. OP_SETDISPATCH = 35, // N words:
  6261. // | unused (24) | opc |
  6262. // | upb_inttable* (32 or 64) |
  6263. OP_DISPATCH = 36, // No arg.
  6264. OP_HALT = 37, // No arg.
  6265. } opcode;
  6266. #define OP_MAX OP_HALT
  6267. UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; }
  6268. // Method group; represents a set of decoder methods that had their code
  6269. // emitted together, and must therefore be freed together. Immutable once
  6270. // created. It is possible we may want to expose this to users at some point.
  6271. //
  6272. // Overall ownership of Decoder objects looks like this:
  6273. //
  6274. // +----------+
  6275. // | | <---> DecoderMethod
  6276. // | method |
  6277. // CodeCache ---> | group | <---> DecoderMethod
  6278. // | |
  6279. // | (mgroup) | <---> DecoderMethod
  6280. // +----------+
  6281. typedef struct {
  6282. upb_refcounted base;
  6283. // Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the
  6284. // methods.
  6285. upb_inttable methods;
  6286. // When we add the ability to link to previously existing mgroups, we'll
  6287. // need an array of mgroups we reference here, and own refs on them.
  6288. // The bytecode for our methods, if any exists. Owned by us.
  6289. uint32_t *bytecode;
  6290. uint32_t *bytecode_end;
  6291. #ifdef UPB_USE_JIT_X64
  6292. // JIT-generated machine code, if any.
  6293. upb_string_handlerfunc *jit_code;
  6294. // The size of the jit_code (required to munmap()).
  6295. size_t jit_size;
  6296. char *debug_info;
  6297. void *dl;
  6298. #endif
  6299. } mgroup;
  6300. // Decoder entry points; used as handlers.
  6301. void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
  6302. void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
  6303. size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
  6304. size_t size, const upb_bufhandle *handle);
  6305. bool upb_pbdecoder_end(void *closure, const void *handler_data);
  6306. // Decoder-internal functions that the JIT calls to handle fallback paths.
  6307. int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
  6308. size_t size, const upb_bufhandle *handle);
  6309. size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
  6310. int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
  6311. uint8_t wire_type);
  6312. int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
  6313. int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
  6314. int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
  6315. int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
  6316. void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
  6317. // Error messages that are shared between the bytecode and JIT decoders.
  6318. extern const char *kPbDecoderStackOverflow;
  6319. // Access to decoderplan members needed by the decoder.
  6320. const char *upb_pbdecoder_getopname(unsigned int op);
  6321. // JIT codegen entry point.
  6322. void upb_pbdecoder_jit(mgroup *group);
  6323. void upb_pbdecoder_freejit(mgroup *group);
  6324. // A special label that means "do field dispatch for this message and branch to
  6325. // wherever that takes you."
  6326. #define LABEL_DISPATCH 0
  6327. // A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
  6328. // RET) for branching to when we find an appropriate ENDGROUP tag.
  6329. #define DISPATCH_ENDMSG 0
  6330. // It's important to use this invalid wire type instead of 0 (which is a valid
  6331. // wire type).
  6332. #define NO_WIRE_TYPE 0xff
  6333. // The dispatch table layout is:
  6334. // [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
  6335. //
  6336. // If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
  6337. // (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
  6338. //
  6339. // We need two wire types because of packed/non-packed compatibility. A
  6340. // primitive repeated field can use either wire type and be valid. While we
  6341. // could key the table on fieldnum+wiretype, the table would be 8x sparser.
  6342. //
  6343. // Storing two wire types in the primary value allows us to quickly rule out
  6344. // the second wire type without needing to do a separate lookup (this case is
  6345. // less common than an unknown field).
  6346. UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
  6347. uint8_t wt2) {
  6348. return (ofs << 16) | (wt2 << 8) | wt1;
  6349. }
  6350. UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
  6351. uint8_t *wt1, uint8_t *wt2) {
  6352. *wt1 = (uint8_t)dispatch;
  6353. *wt2 = (uint8_t)(dispatch >> 8);
  6354. *ofs = dispatch >> 16;
  6355. }
  6356. // All of the functions in decoder.c that return int32_t return values according
  6357. // to the following scheme:
  6358. // 1. negative values indicate a return code from the following list.
  6359. // 2. positive values indicate that error or end of buffer was hit, and
  6360. // that the decode function should immediately return the given value
  6361. // (the decoder state has already been suspended and is ready to be
  6362. // resumed).
  6363. #define DECODE_OK -1
  6364. #define DECODE_MISMATCH -2 // Used only from checktag_slow().
  6365. #define DECODE_ENDGROUP -3 // Used only from checkunknown().
  6366. #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
  6367. #endif // UPB_DECODER_INT_H_
  6368. /*
  6369. * upb - a minimalist implementation of protocol buffers.
  6370. *
  6371. * Copyright (c) 2011 Google Inc. See LICENSE for details.
  6372. * Author: Josh Haberman <jhaberman@gmail.com>
  6373. *
  6374. * A number of routines for varint manipulation (we keep them all around to
  6375. * have multiple approaches available for benchmarking).
  6376. */
  6377. #ifndef UPB_VARINT_DECODER_H_
  6378. #define UPB_VARINT_DECODER_H_
  6379. #include <assert.h>
  6380. #include <stdint.h>
  6381. #include <string.h>
  6382. #ifdef __cplusplus
  6383. extern "C" {
  6384. #endif
  6385. // A list of types as they are encoded on-the-wire.
  6386. typedef enum {
  6387. UPB_WIRE_TYPE_VARINT = 0,
  6388. UPB_WIRE_TYPE_64BIT = 1,
  6389. UPB_WIRE_TYPE_DELIMITED = 2,
  6390. UPB_WIRE_TYPE_START_GROUP = 3,
  6391. UPB_WIRE_TYPE_END_GROUP = 4,
  6392. UPB_WIRE_TYPE_32BIT = 5,
  6393. } upb_wiretype_t;
  6394. #define UPB_MAX_WIRE_TYPE 5
  6395. // The maximum number of bytes that it takes to encode a 64-bit varint.
  6396. // Note that with a better encoding this could be 9 (TODO: write up a
  6397. // wiki document about this).
  6398. #define UPB_PB_VARINT_MAX_LEN 10
  6399. // Array of the "native" (ie. non-packed-repeated) wire type for the given a
  6400. // descriptor type (upb_descriptortype_t).
  6401. extern const uint8_t upb_pb_native_wire_types[];
  6402. /* Zig-zag encoding/decoding **************************************************/
  6403. UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
  6404. return (n >> 1) ^ -(int32_t)(n & 1);
  6405. }
  6406. UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
  6407. return (n >> 1) ^ -(int64_t)(n & 1);
  6408. }
  6409. UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
  6410. UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
  6411. /* Decoding *******************************************************************/
  6412. // All decoding functions return this struct by value.
  6413. typedef struct {
  6414. const char *p; // NULL if the varint was unterminated.
  6415. uint64_t val;
  6416. } upb_decoderet;
  6417. // Four functions for decoding a varint of at most eight bytes. They are all
  6418. // functionally identical, but are implemented in different ways and likely have
  6419. // different performance profiles. We keep them around for performance testing.
  6420. //
  6421. // Note that these functions may not read byte-by-byte, so they must not be used
  6422. // unless there are at least eight bytes left in the buffer!
  6423. upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
  6424. upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
  6425. upb_decoderet upb_vdecode_max8_wright(upb_decoderet r);
  6426. upb_decoderet upb_vdecode_max8_massimino(upb_decoderet r);
  6427. // Template for a function that checks the first two bytes with branching
  6428. // and dispatches 2-10 bytes with a separate function. Note that this may read
  6429. // up to 10 bytes, so it must not be used unless there are at least ten bytes
  6430. // left in the buffer!
  6431. #define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
  6432. UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
  6433. uint8_t *p = (uint8_t*)_p; \
  6434. if ((*p & 0x80) == 0) { upb_decoderet r = {_p + 1, *p & 0x7fU}; return r; } \
  6435. upb_decoderet r = {_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)}; \
  6436. if ((*(p + 1) & 0x80) == 0) return r; \
  6437. return decode_max8_function(r); \
  6438. }
  6439. UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32);
  6440. UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64);
  6441. UPB_VARINT_DECODER_CHECK2(wright, upb_vdecode_max8_wright);
  6442. UPB_VARINT_DECODER_CHECK2(massimino, upb_vdecode_max8_massimino);
  6443. #undef UPB_VARINT_DECODER_CHECK2
  6444. // Our canonical functions for decoding varints, based on the currently
  6445. // favored best-performing implementations.
  6446. UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
  6447. if (sizeof(long) == 8)
  6448. return upb_vdecode_check2_branch64(p);
  6449. else
  6450. return upb_vdecode_check2_branch32(p);
  6451. }
  6452. UPB_INLINE upb_decoderet upb_vdecode_max8_fast(upb_decoderet r) {
  6453. return upb_vdecode_max8_massimino(r);
  6454. }
  6455. /* Encoding *******************************************************************/
  6456. UPB_INLINE int upb_value_size(uint64_t val) {
  6457. #ifdef __GNUC__
  6458. int high_bit = 63 - __builtin_clzll(val); // 0-based, undef if val == 0.
  6459. #else
  6460. int high_bit = 0;
  6461. uint64_t tmp = val;
  6462. while(tmp >>= 1) high_bit++;
  6463. #endif
  6464. return val == 0 ? 1 : high_bit / 8 + 1;
  6465. }
  6466. // Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
  6467. // bytes long), returning how many bytes were used.
  6468. //
  6469. // TODO: benchmark and optimize if necessary.
  6470. UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
  6471. if (val == 0) { buf[0] = 0; return 1; }
  6472. size_t i = 0;
  6473. while (val) {
  6474. uint8_t byte = val & 0x7fU;
  6475. val >>= 7;
  6476. if (val) byte |= 0x80U;
  6477. buf[i++] = byte;
  6478. }
  6479. return i;
  6480. }
  6481. UPB_INLINE size_t upb_varint_size(uint64_t val) {
  6482. char buf[UPB_PB_VARINT_MAX_LEN];
  6483. return upb_vencode64(val, buf);
  6484. }
  6485. // Encodes a 32-bit varint, *not* sign-extended.
  6486. UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
  6487. char buf[UPB_PB_VARINT_MAX_LEN];
  6488. size_t bytes = upb_vencode64(val, buf);
  6489. uint64_t ret = 0;
  6490. assert(bytes <= 5);
  6491. memcpy(&ret, buf, bytes);
  6492. assert(ret <= 0xffffffffffU);
  6493. return ret;
  6494. }
  6495. #ifdef __cplusplus
  6496. } /* extern "C" */
  6497. #endif
  6498. #endif /* UPB_VARINT_DECODER_H_ */
  6499. /*
  6500. * upb - a minimalist implementation of protocol buffers.
  6501. *
  6502. * Copyright (c) 2009-2010 Google Inc. See LICENSE for details.
  6503. * Author: Josh Haberman <jhaberman@gmail.com>
  6504. *
  6505. * Implements a set of upb_handlers that write protobuf data to the binary wire
  6506. * format.
  6507. *
  6508. * This encoder implementation does not have any access to any out-of-band or
  6509. * precomputed lengths for submessages, so it must buffer submessages internally
  6510. * before it can emit the first byte.
  6511. */
  6512. #ifndef UPB_ENCODER_H_
  6513. #define UPB_ENCODER_H_
  6514. #ifdef __cplusplus
  6515. namespace upb {
  6516. namespace pb {
  6517. class Encoder;
  6518. } // namespace pb
  6519. } // namespace upb
  6520. #endif
  6521. UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder);
  6522. #define UPB_PBENCODER_MAX_NESTING 100
  6523. /* upb::pb::Encoder ***********************************************************/
  6524. // The output buffer is divided into segments; a segment is a string of data
  6525. // that is "ready to go" -- it does not need any varint lengths inserted into
  6526. // the middle. The seams between segments are where varints will be inserted
  6527. // once they are known.
  6528. //
  6529. // We also use the concept of a "run", which is a range of encoded bytes that
  6530. // occur at a single submessage level. Every segment contains one or more runs.
  6531. //
  6532. // A segment can span messages. Consider:
  6533. //
  6534. // .--Submessage lengths---------.
  6535. // | | |
  6536. // | V V
  6537. // V | |--------------- | |-----------------
  6538. // Submessages: | |-----------------------------------------------
  6539. // Top-level msg: ------------------------------------------------------------
  6540. //
  6541. // Segments: ----- ------------------- -----------------
  6542. // Runs: *---- *--------------*--- *----------------
  6543. // (* marks the start)
  6544. //
  6545. // Note that the top-level menssage is not in any segment because it does not
  6546. // have any length preceding it.
  6547. //
  6548. // A segment is only interrupted when another length needs to be inserted. So
  6549. // observe how the second segment spans both the inner submessage and part of
  6550. // the next enclosing message.
  6551. typedef struct {
  6552. UPB_PRIVATE_FOR_CPP
  6553. uint32_t msglen; // The length to varint-encode before this segment.
  6554. uint32_t seglen; // Length of the segment.
  6555. } upb_pb_encoder_segment;
  6556. UPB_DEFINE_CLASS0(upb::pb::Encoder,
  6557. public:
  6558. Encoder(const upb::Handlers* handlers);
  6559. ~Encoder();
  6560. static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* msg);
  6561. // Resets the state of the printer, so that it will expect to begin a new
  6562. // document.
  6563. void Reset();
  6564. // Resets the output pointer which will serve as our closure.
  6565. void ResetOutput(BytesSink* output);
  6566. // The input to the encoder.
  6567. Sink* input();
  6568. private:
  6569. UPB_DISALLOW_COPY_AND_ASSIGN(Encoder);
  6570. ,
  6571. UPB_DEFINE_STRUCT0(upb_pb_encoder, UPB_QUOTE(
  6572. // Our input and output.
  6573. upb_sink input_;
  6574. upb_bytessink *output_;
  6575. // The "subclosure" -- used as the inner closure as part of the bytessink
  6576. // protocol.
  6577. void *subc;
  6578. // The output buffer and limit, and our current write position. "buf"
  6579. // initially points to "initbuf", but is dynamically allocated if we need to
  6580. // grow beyond the initial size.
  6581. char *buf, *ptr, *limit;
  6582. // The beginning of the current run, or undefined if we are at the top level.
  6583. char *runbegin;
  6584. // The list of segments we are accumulating.
  6585. upb_pb_encoder_segment *segbuf, *segptr, *seglimit;
  6586. // The stack of enclosing submessages. Each entry in the stack points to the
  6587. // segment where this submessage's length is being accumulated.
  6588. int stack[UPB_PBENCODER_MAX_NESTING], *top, *stacklimit;
  6589. // Depth of startmsg/endmsg calls.
  6590. int depth;
  6591. // Initial buffers for the output buffer and segment buffer. If we outgrow
  6592. // these we will dynamically allocate bigger ones.
  6593. char initbuf[256];
  6594. upb_pb_encoder_segment seginitbuf[32];
  6595. )));
  6596. UPB_BEGIN_EXTERN_C
  6597. const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
  6598. const void *owner);
  6599. void upb_pb_encoder_reset(upb_pb_encoder *e);
  6600. upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
  6601. void upb_pb_encoder_init(upb_pb_encoder *e, const upb_handlers *h);
  6602. void upb_pb_encoder_resetoutput(upb_pb_encoder *e, upb_bytessink *output);
  6603. void upb_pb_encoder_uninit(upb_pb_encoder *e);
  6604. UPB_END_EXTERN_C
  6605. #ifdef __cplusplus
  6606. namespace upb {
  6607. namespace pb {
  6608. inline Encoder::Encoder(const upb::Handlers* handlers) {
  6609. upb_pb_encoder_init(this, handlers);
  6610. }
  6611. inline Encoder::~Encoder() {
  6612. upb_pb_encoder_uninit(this);
  6613. }
  6614. inline void Encoder::Reset() {
  6615. upb_pb_encoder_reset(this);
  6616. }
  6617. inline void Encoder::ResetOutput(BytesSink* output) {
  6618. upb_pb_encoder_resetoutput(this, output);
  6619. }
  6620. inline Sink* Encoder::input() {
  6621. return upb_pb_encoder_input(this);
  6622. }
  6623. inline reffed_ptr<const Handlers> Encoder::NewHandlers(
  6624. const upb::MessageDef *md) {
  6625. const Handlers* h = upb_pb_encoder_newhandlers(md, &h);
  6626. return reffed_ptr<const Handlers>(h, &h);
  6627. }
  6628. } // namespace pb
  6629. } // namespace upb
  6630. #endif
  6631. #endif /* UPB_ENCODER_H_ */
  6632. /*
  6633. * upb - a minimalist implementation of protocol buffers.
  6634. *
  6635. * Copyright (c) 2011-2012 Google Inc. See LICENSE for details.
  6636. * Author: Josh Haberman <jhaberman@gmail.com>
  6637. *
  6638. * upb's core components like upb_decoder and upb_msg are carefully designed to
  6639. * avoid depending on each other for maximum orthogonality. In other words,
  6640. * you can use a upb_decoder to decode into *any* kind of structure; upb_msg is
  6641. * just one such structure. A upb_msg can be serialized/deserialized into any
  6642. * format, protobuf binary format is just one such format.
  6643. *
  6644. * However, for convenience we provide functions here for doing common
  6645. * operations like deserializing protobuf binary format into a upb_msg. The
  6646. * compromise is that this file drags in almost all of upb as a dependency,
  6647. * which could be undesirable if you're trying to use a trimmed-down build of
  6648. * upb.
  6649. *
  6650. * While these routines are convenient, they do not reuse any encoding/decoding
  6651. * state. For example, if a decoder is JIT-based, it will be re-JITted every
  6652. * time these functions are called. For this reason, if you are parsing lots
  6653. * of data and efficiency is an issue, these may not be the best functions to
  6654. * use (though they are useful for prototyping, before optimizing).
  6655. */
  6656. #ifndef UPB_GLUE_H
  6657. #define UPB_GLUE_H
  6658. #include <stdbool.h>
  6659. #ifdef __cplusplus
  6660. extern "C" {
  6661. #endif
  6662. // Loads all defs from the given protobuf binary descriptor, setting default
  6663. // accessors and a default layout on all messages. The caller owns the
  6664. // returned array of defs, which will be of length *n. On error NULL is
  6665. // returned and status is set (if non-NULL).
  6666. upb_def **upb_load_defs_from_descriptor(const char *str, size_t len, int *n,
  6667. void *owner, upb_status *status);
  6668. // Like the previous but also adds the loaded defs to the given symtab.
  6669. bool upb_load_descriptor_into_symtab(upb_symtab *symtab, const char *str,
  6670. size_t len, upb_status *status);
  6671. // Like the previous but also reads the descriptor from the given filename.
  6672. bool upb_load_descriptor_file_into_symtab(upb_symtab *symtab, const char *fname,
  6673. upb_status *status);
  6674. // Reads the given filename into a character string, returning NULL if there
  6675. // was an error.
  6676. char *upb_readfile(const char *filename, size_t *len);
  6677. #ifdef __cplusplus
  6678. } /* extern "C" */
  6679. namespace upb {
  6680. // All routines that load descriptors expect the descriptor to be a
  6681. // FileDescriptorSet.
  6682. inline bool LoadDescriptorFileIntoSymtab(SymbolTable* s, const char *fname,
  6683. Status* status) {
  6684. return upb_load_descriptor_file_into_symtab(s, fname, status);
  6685. }
  6686. inline bool LoadDescriptorIntoSymtab(SymbolTable* s, const char* str,
  6687. size_t len, Status* status) {
  6688. return upb_load_descriptor_into_symtab(s, str, len, status);
  6689. }
  6690. // Templated so it can accept both string and std::string.
  6691. template <typename T>
  6692. bool LoadDescriptorIntoSymtab(SymbolTable* s, const T& desc, Status* status) {
  6693. return upb_load_descriptor_into_symtab(s, desc.c_str(), desc.size(), status);
  6694. }
  6695. } // namespace upb
  6696. #endif
  6697. #endif
  6698. /*
  6699. * upb - a minimalist implementation of protocol buffers.
  6700. *
  6701. * Copyright (c) 2009 Google Inc. See LICENSE for details.
  6702. * Author: Josh Haberman <jhaberman@gmail.com>
  6703. */
  6704. #ifndef UPB_TEXT_H_
  6705. #define UPB_TEXT_H_
  6706. #ifdef __cplusplus
  6707. namespace upb {
  6708. namespace pb {
  6709. class TextPrinter;
  6710. } // namespace pb
  6711. } // namespace upb
  6712. #endif
  6713. UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter);
  6714. UPB_DEFINE_CLASS0(upb::pb::TextPrinter,
  6715. public:
  6716. // The given handlers must have come from NewHandlers(). It must outlive the
  6717. // TextPrinter.
  6718. explicit TextPrinter(const upb::Handlers* handlers);
  6719. void SetSingleLineMode(bool single_line);
  6720. bool ResetOutput(BytesSink* output);
  6721. Sink* input();
  6722. // If handler caching becomes a requirement we can add a code cache as in
  6723. // decoder.h
  6724. static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
  6725. private:
  6726. ,
  6727. UPB_DEFINE_STRUCT0(upb_textprinter,
  6728. upb_sink input_;
  6729. upb_bytessink *output_;
  6730. int indent_depth_;
  6731. bool single_line_;
  6732. void *subc;
  6733. ));
  6734. UPB_BEGIN_EXTERN_C // {
  6735. // C API.
  6736. void upb_textprinter_init(upb_textprinter *p, const upb_handlers *h);
  6737. void upb_textprinter_uninit(upb_textprinter *p);
  6738. bool upb_textprinter_resetoutput(upb_textprinter *p, upb_bytessink *output);
  6739. void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
  6740. upb_sink *upb_textprinter_input(upb_textprinter *p);
  6741. const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
  6742. const void *owner);
  6743. UPB_END_EXTERN_C // }
  6744. #ifdef __cplusplus
  6745. namespace upb {
  6746. namespace pb {
  6747. inline TextPrinter::TextPrinter(const upb::Handlers* handlers) {
  6748. upb_textprinter_init(this, handlers);
  6749. }
  6750. inline void TextPrinter::SetSingleLineMode(bool single_line) {
  6751. upb_textprinter_setsingleline(this, single_line);
  6752. }
  6753. inline bool TextPrinter::ResetOutput(BytesSink* output) {
  6754. return upb_textprinter_resetoutput(this, output);
  6755. }
  6756. inline Sink* TextPrinter::input() {
  6757. return upb_textprinter_input(this);
  6758. }
  6759. inline reffed_ptr<const Handlers> TextPrinter::NewHandlers(
  6760. const MessageDef *md) {
  6761. const Handlers* h = upb_textprinter_newhandlers(md, &h);
  6762. return reffed_ptr<const Handlers>(h, &h);
  6763. }
  6764. } // namespace pb
  6765. } // namespace upb
  6766. #endif
  6767. #endif /* UPB_TEXT_H_ */
  6768. /*
  6769. * upb - a minimalist implementation of protocol buffers.
  6770. *
  6771. * Copyright (c) 2014 Google Inc. See LICENSE for details.
  6772. * Author: Josh Haberman <jhaberman@gmail.com>
  6773. *
  6774. * upb::json::Parser can parse JSON according to a specific schema.
  6775. * Support for parsing arbitrary JSON (schema-less) will be added later.
  6776. */
  6777. #ifndef UPB_JSON_PARSER_H_
  6778. #define UPB_JSON_PARSER_H_
  6779. #ifdef __cplusplus
  6780. namespace upb {
  6781. namespace json {
  6782. class Parser;
  6783. } // namespace json
  6784. } // namespace upb
  6785. #endif
  6786. UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser);
  6787. // Internal-only struct used by the parser.
  6788. typedef struct {
  6789. UPB_PRIVATE_FOR_CPP
  6790. upb_sink sink;
  6791. const upb_msgdef *m;
  6792. const upb_fielddef *f;
  6793. } upb_jsonparser_frame;
  6794. /* upb::json::Parser **********************************************************/
  6795. #define UPB_JSON_MAX_DEPTH 64
  6796. // Parses an incoming BytesStream, pushing the results to the destination sink.
  6797. UPB_DEFINE_CLASS0(upb::json::Parser,
  6798. public:
  6799. Parser(Status* status);
  6800. ~Parser();
  6801. // Resets the state of the printer, so that it will expect to begin a new
  6802. // document.
  6803. void Reset();
  6804. // Resets the output pointer which will serve as our closure. Implies
  6805. // Reset().
  6806. void ResetOutput(Sink* output);
  6807. // The input to the printer.
  6808. BytesSink* input();
  6809. ,
  6810. UPB_DEFINE_STRUCT0(upb_json_parser,
  6811. upb_byteshandler input_handler_;
  6812. upb_bytessink input_;
  6813. // Stack to track the JSON scopes we are in.
  6814. upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH];
  6815. upb_jsonparser_frame *top;
  6816. upb_jsonparser_frame *limit;
  6817. upb_status *status;
  6818. // Ragel's internal parsing stack for the parsing state machine.
  6819. int current_state;
  6820. int parser_stack[UPB_JSON_MAX_DEPTH];
  6821. int parser_top;
  6822. // The handle for the current buffer.
  6823. const upb_bufhandle *handle;
  6824. // Accumulate buffer. See details in parser.rl.
  6825. const char *accumulated;
  6826. size_t accumulated_len;
  6827. char *accumulate_buf;
  6828. size_t accumulate_buf_size;
  6829. // Multi-part text data. See details in parser.rl.
  6830. int multipart_state;
  6831. upb_selector_t string_selector;
  6832. // Input capture. See details in parser.rl.
  6833. const char *capture;
  6834. // Intermediate result of parsing a unicode escape sequence.
  6835. uint32_t digit;
  6836. ));
  6837. UPB_BEGIN_EXTERN_C
  6838. void upb_json_parser_init(upb_json_parser *p, upb_status *status);
  6839. void upb_json_parser_uninit(upb_json_parser *p);
  6840. void upb_json_parser_reset(upb_json_parser *p);
  6841. void upb_json_parser_resetoutput(upb_json_parser *p, upb_sink *output);
  6842. upb_bytessink *upb_json_parser_input(upb_json_parser *p);
  6843. UPB_END_EXTERN_C
  6844. #ifdef __cplusplus
  6845. namespace upb {
  6846. namespace json {
  6847. inline Parser::Parser(Status* status) { upb_json_parser_init(this, status); }
  6848. inline Parser::~Parser() { upb_json_parser_uninit(this); }
  6849. inline void Parser::Reset() { upb_json_parser_reset(this); }
  6850. inline void Parser::ResetOutput(Sink* output) {
  6851. upb_json_parser_resetoutput(this, output);
  6852. }
  6853. inline BytesSink* Parser::input() {
  6854. return upb_json_parser_input(this);
  6855. }
  6856. } // namespace json
  6857. } // namespace upb
  6858. #endif
  6859. #endif // UPB_JSON_PARSER_H_
  6860. /*
  6861. * upb - a minimalist implementation of protocol buffers.
  6862. *
  6863. * Copyright (c) 2014 Google Inc. See LICENSE for details.
  6864. * Author: Josh Haberman <jhaberman@gmail.com>
  6865. *
  6866. * upb::json::Printer allows you to create handlers that emit JSON
  6867. * according to a specific protobuf schema.
  6868. */
  6869. #ifndef UPB_JSON_TYPED_PRINTER_H_
  6870. #define UPB_JSON_TYPED_PRINTER_H_
  6871. #ifdef __cplusplus
  6872. namespace upb {
  6873. namespace json {
  6874. class Printer;
  6875. } // namespace json
  6876. } // namespace upb
  6877. #endif
  6878. UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer);
  6879. /* upb::json::Printer *********************************************************/
  6880. // Prints an incoming stream of data to a BytesSink in JSON format.
  6881. UPB_DEFINE_CLASS0(upb::json::Printer,
  6882. public:
  6883. Printer(const upb::Handlers* handlers);
  6884. ~Printer();
  6885. // Resets the state of the printer, so that it will expect to begin a new
  6886. // document.
  6887. void Reset();
  6888. // Resets the output pointer which will serve as our closure. Implies
  6889. // Reset().
  6890. void ResetOutput(BytesSink* output);
  6891. // The input to the printer.
  6892. Sink* input();
  6893. // Returns handlers for printing according to the specified schema.
  6894. static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md);
  6895. ,
  6896. UPB_DEFINE_STRUCT0(upb_json_printer,
  6897. upb_sink input_;
  6898. // BytesSink closure.
  6899. void *subc_;
  6900. upb_bytessink *output_;
  6901. // We track the depth so that we know when to emit startstr/endstr on the
  6902. // output.
  6903. int depth_;
  6904. // Have we emitted the first element? This state is necessary to emit commas
  6905. // without leaving a trailing comma in arrays/maps. We keep this state per
  6906. // frame depth.
  6907. //
  6908. // Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages.
  6909. // We count frames (contexts in which we separate elements by commas) as both
  6910. // repeated fields and messages (maps), and the worst case is a
  6911. // message->repeated field->submessage->repeated field->... nesting.
  6912. bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2];
  6913. ));
  6914. UPB_BEGIN_EXTERN_C // {
  6915. // Native C API.
  6916. void upb_json_printer_init(upb_json_printer *p, const upb_handlers *h);
  6917. void upb_json_printer_uninit(upb_json_printer *p);
  6918. void upb_json_printer_reset(upb_json_printer *p);
  6919. void upb_json_printer_resetoutput(upb_json_printer *p, upb_bytessink *output);
  6920. upb_sink *upb_json_printer_input(upb_json_printer *p);
  6921. const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
  6922. const void *owner);
  6923. UPB_END_EXTERN_C // }
  6924. #ifdef __cplusplus
  6925. namespace upb {
  6926. namespace json {
  6927. inline Printer::Printer(const upb::Handlers* handlers) {
  6928. upb_json_printer_init(this, handlers);
  6929. }
  6930. inline Printer::~Printer() { upb_json_printer_uninit(this); }
  6931. inline void Printer::Reset() { upb_json_printer_reset(this); }
  6932. inline void Printer::ResetOutput(BytesSink* output) {
  6933. upb_json_printer_resetoutput(this, output);
  6934. }
  6935. inline Sink* Printer::input() { return upb_json_printer_input(this); }
  6936. inline reffed_ptr<const Handlers> Printer::NewHandlers(
  6937. const upb::MessageDef *md) {
  6938. const Handlers* h = upb_json_printer_newhandlers(md, &h);
  6939. return reffed_ptr<const Handlers>(h, &h);
  6940. }
  6941. } // namespace json
  6942. } // namespace upb
  6943. #endif
  6944. #endif // UPB_JSON_TYPED_PRINTER_H_