upb.h 318 KB

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