upb.h 314 KB

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