upb.h 316 KB

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