upb.h 316 KB

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