upb.h 295 KB

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