upb.h 333 KB

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