upb.h 335 KB

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