upb.h 331 KB

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