upb.h 407 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755
  1. // Amalgamated source file
  2. #if UINTPTR_MAX == 0xffffffff
  3. #define UPB_SIZE(size32, size64) size32
  4. #else
  5. #define UPB_SIZE(size32, size64) size64
  6. #endif
  7. #define UPB_FIELD_AT(msg, fieldtype, offset) \
  8. *(fieldtype*)((const char*)(msg) + offset)
  9. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  10. UPB_FIELD_AT(msg, int, case_offset) == case_val \
  11. ? UPB_FIELD_AT(msg, fieldtype, offset) \
  12. : default
  13. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  14. UPB_FIELD_AT(msg, int, case_offset) = case_val; \
  15. UPB_FIELD_AT(msg, fieldtype, offset) = value;
  16. /*
  17. ** upb::Message is a representation for protobuf messages.
  18. **
  19. ** However it differs from other common representations like
  20. ** google::protobuf::Message in one key way: it does not prescribe any
  21. ** ownership between messages and submessages, and it relies on the
  22. ** client to ensure that each submessage/array/map outlives its parent.
  23. **
  24. ** All messages, arrays, and maps live in an Arena. If the entire message
  25. ** tree is in the same arena, ensuring proper lifetimes is simple. However
  26. ** the client can mix arenas as long as they ensure that there are no
  27. ** dangling pointers.
  28. **
  29. ** A client can access a upb::Message without knowing anything about
  30. ** ownership semantics, but to create or mutate a message a user needs
  31. ** to implement the memory management themselves.
  32. **
  33. ** TODO: UTF-8 checking?
  34. **/
  35. #ifndef UPB_MSG_H_
  36. #define UPB_MSG_H_
  37. /*
  38. ** Defs are upb's internal representation of the constructs that can appear
  39. ** in a .proto file:
  40. **
  41. ** - upb::MessageDef (upb_msgdef): describes a "message" construct.
  42. ** - upb::FieldDef (upb_fielddef): describes a message field.
  43. ** - upb::FileDef (upb_filedef): describes a .proto file and its defs.
  44. ** - upb::EnumDef (upb_enumdef): describes an enum.
  45. ** - upb::OneofDef (upb_oneofdef): describes a oneof.
  46. ** - upb::Def (upb_def): base class of all the others.
  47. **
  48. ** TODO: definitions of services.
  49. **
  50. ** Like upb_refcounted objects, defs are mutable only until frozen, and are
  51. ** only thread-safe once frozen.
  52. **
  53. ** This is a mixed C/C++ interface that offers a full API to both languages.
  54. ** See the top-level README for more information.
  55. */
  56. #ifndef UPB_DEF_H_
  57. #define UPB_DEF_H_
  58. /*
  59. ** upb::RefCounted (upb_refcounted)
  60. **
  61. ** A refcounting scheme that supports circular refs. It accomplishes this by
  62. ** partitioning the set of objects into groups such that no cycle spans groups;
  63. ** we can then reference-count the group as a whole and ignore refs within the
  64. ** group. When objects are mutable, these groups are computed very
  65. ** conservatively; we group any objects that have ever had a link between them.
  66. ** When objects are frozen, we compute strongly-connected components which
  67. ** allows us to be precise and only group objects that are actually cyclic.
  68. **
  69. ** This is a mixed C/C++ interface that offers a full API to both languages.
  70. ** See the top-level README for more information.
  71. */
  72. #ifndef UPB_REFCOUNTED_H_
  73. #define UPB_REFCOUNTED_H_
  74. /*
  75. ** upb_table
  76. **
  77. ** This header is INTERNAL-ONLY! Its interfaces are not public or stable!
  78. ** This file defines very fast int->upb_value (inttable) and string->upb_value
  79. ** (strtable) hash tables.
  80. **
  81. ** The table uses chained scatter with Brent's variation (inspired by the Lua
  82. ** implementation of hash tables). The hash function for strings is Austin
  83. ** Appleby's "MurmurHash."
  84. **
  85. ** The inttable uses uintptr_t as its key, which guarantees it can be used to
  86. ** store pointers or integers of at least 32 bits (upb isn't really useful on
  87. ** systems where sizeof(void*) < 4).
  88. **
  89. ** The table must be homogenous (all values of the same type). In debug
  90. ** mode, we check this on insert and lookup.
  91. */
  92. #ifndef UPB_TABLE_H_
  93. #define UPB_TABLE_H_
  94. #include <stdint.h>
  95. #include <string.h>
  96. /*
  97. ** This file contains shared definitions that are widely used across upb.
  98. **
  99. ** This is a mixed C/C++ interface that offers a full API to both languages.
  100. ** See the top-level README for more information.
  101. */
  102. #ifndef UPB_H_
  103. #define UPB_H_
  104. #include <assert.h>
  105. #include <stdarg.h>
  106. #include <stdbool.h>
  107. #include <stddef.h>
  108. #ifdef __cplusplus
  109. namespace upb {
  110. class Allocator;
  111. class Arena;
  112. class Environment;
  113. class ErrorSpace;
  114. class Status;
  115. template <int N> class InlinedArena;
  116. template <int N> class InlinedEnvironment;
  117. }
  118. #endif
  119. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  120. #ifdef __cplusplus
  121. #define UPB_INLINE inline
  122. #elif defined (__GNUC__)
  123. #define UPB_INLINE static __inline__
  124. #else
  125. #define UPB_INLINE static
  126. #endif
  127. /* Hints to the compiler about likely/unlikely branches. */
  128. #define UPB_LIKELY(x) __builtin_expect((x),1)
  129. /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
  130. * doesn't provide these preprocessor symbols. */
  131. #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  132. #define UPB_BIG_ENDIAN
  133. #endif
  134. /* Macros for function attributes on compilers that support them. */
  135. #ifdef __GNUC__
  136. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  137. #define UPB_NOINLINE __attribute__((noinline))
  138. #define UPB_NORETURN __attribute__((__noreturn__))
  139. #else /* !defined(__GNUC__) */
  140. #define UPB_FORCEINLINE
  141. #define UPB_NOINLINE
  142. #define UPB_NORETURN
  143. #endif
  144. #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
  145. /* C99/C++11 versions. */
  146. #include <stdio.h>
  147. #define _upb_snprintf snprintf
  148. #define _upb_vsnprintf vsnprintf
  149. #define _upb_va_copy(a, b) va_copy(a, b)
  150. #elif defined __GNUC__
  151. /* A few hacky workarounds for functions not in C89.
  152. * For internal use only!
  153. * TODO(haberman): fix these by including our own implementations, or finding
  154. * another workaround.
  155. */
  156. #define _upb_snprintf __builtin_snprintf
  157. #define _upb_vsnprintf __builtin_vsnprintf
  158. #define _upb_va_copy(a, b) __va_copy(a, b)
  159. #else
  160. #error Need implementations of [v]snprintf and va_copy
  161. #endif
  162. #if ((defined(__cplusplus) && __cplusplus >= 201103L) || \
  163. defined(__GXX_EXPERIMENTAL_CXX0X__)) && !defined(UPB_NO_CXX11)
  164. #define UPB_CXX11
  165. #endif
  166. /* UPB_DISALLOW_COPY_AND_ASSIGN()
  167. * UPB_DISALLOW_POD_OPS()
  168. *
  169. * Declare these in the "private" section of a C++ class to forbid copy/assign
  170. * or all POD ops (construct, destruct, copy, assign) on that class. */
  171. #ifdef UPB_CXX11
  172. #include <type_traits>
  173. #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
  174. class_name(const class_name&) = delete; \
  175. void operator=(const class_name&) = delete;
  176. #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
  177. class_name() = delete; \
  178. ~class_name() = delete; \
  179. UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
  180. #define UPB_ASSERT_STDLAYOUT(type) \
  181. static_assert(std::is_standard_layout<type>::value, \
  182. #type " must be standard layout");
  183. #define UPB_FINAL final
  184. #else /* !defined(UPB_CXX11) */
  185. #define UPB_DISALLOW_COPY_AND_ASSIGN(class_name) \
  186. class_name(const class_name&); \
  187. void operator=(const class_name&);
  188. #define UPB_DISALLOW_POD_OPS(class_name, full_class_name) \
  189. class_name(); \
  190. ~class_name(); \
  191. UPB_DISALLOW_COPY_AND_ASSIGN(class_name)
  192. #define UPB_ASSERT_STDLAYOUT(type)
  193. #define UPB_FINAL
  194. #endif
  195. /* UPB_DECLARE_TYPE()
  196. * UPB_DECLARE_DERIVED_TYPE()
  197. * UPB_DECLARE_DERIVED_TYPE2()
  198. *
  199. * Macros for declaring C and C++ types both, including inheritance.
  200. * The inheritance doesn't use real C++ inheritance, to stay compatible with C.
  201. *
  202. * These macros also provide upcasts:
  203. * - in C: types-specific functions (ie. upb_foo_upcast(foo))
  204. * - in C++: upb::upcast(foo) along with implicit conversions
  205. *
  206. * Downcasts are not provided, but upb/def.h defines downcasts for upb::Def. */
  207. #define UPB_C_UPCASTS(ty, base) \
  208. UPB_INLINE base *ty ## _upcast_mutable(ty *p) { return (base*)p; } \
  209. UPB_INLINE const base *ty ## _upcast(const ty *p) { return (const base*)p; }
  210. #define UPB_C_UPCASTS2(ty, base, base2) \
  211. UPB_C_UPCASTS(ty, base) \
  212. UPB_INLINE base2 *ty ## _upcast2_mutable(ty *p) { return (base2*)p; } \
  213. UPB_INLINE const base2 *ty ## _upcast2(const ty *p) { return (const base2*)p; }
  214. #ifdef __cplusplus
  215. #define UPB_BEGIN_EXTERN_C extern "C" {
  216. #define UPB_END_EXTERN_C }
  217. #define UPB_PRIVATE_FOR_CPP private:
  218. #define UPB_DECLARE_TYPE(cppname, cname) typedef cppname cname;
  219. #define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
  220. UPB_DECLARE_TYPE(cppname, cname) \
  221. UPB_C_UPCASTS(cname, cbase) \
  222. namespace upb { \
  223. template <> \
  224. class Pointer<cppname> : public PointerBase<cppname, cppbase> { \
  225. public: \
  226. explicit Pointer(cppname* ptr) \
  227. : PointerBase<cppname, cppbase>(ptr) {} \
  228. }; \
  229. template <> \
  230. class Pointer<const cppname> \
  231. : public PointerBase<const cppname, const cppbase> { \
  232. public: \
  233. explicit Pointer(const cppname* ptr) \
  234. : PointerBase<const cppname, const cppbase>(ptr) {} \
  235. }; \
  236. }
  237. #define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, cname, cbase, \
  238. cbase2) \
  239. UPB_DECLARE_TYPE(cppname, cname) \
  240. UPB_C_UPCASTS2(cname, cbase, cbase2) \
  241. namespace upb { \
  242. template <> \
  243. class Pointer<cppname> : public PointerBase2<cppname, cppbase, cppbase2> { \
  244. public: \
  245. explicit Pointer(cppname* ptr) \
  246. : PointerBase2<cppname, cppbase, cppbase2>(ptr) {} \
  247. }; \
  248. template <> \
  249. class Pointer<const cppname> \
  250. : public PointerBase2<const cppname, const cppbase, const cppbase2> { \
  251. public: \
  252. explicit Pointer(const cppname* ptr) \
  253. : PointerBase2<const cppname, const cppbase, const cppbase2>(ptr) {} \
  254. }; \
  255. }
  256. #else /* !defined(__cplusplus) */
  257. #define UPB_BEGIN_EXTERN_C
  258. #define UPB_END_EXTERN_C
  259. #define UPB_PRIVATE_FOR_CPP
  260. #define UPB_DECLARE_TYPE(cppname, cname) \
  261. struct cname; \
  262. typedef struct cname cname;
  263. #define UPB_DECLARE_DERIVED_TYPE(cppname, cppbase, cname, cbase) \
  264. UPB_DECLARE_TYPE(cppname, cname) \
  265. UPB_C_UPCASTS(cname, cbase)
  266. #define UPB_DECLARE_DERIVED_TYPE2(cppname, cppbase, cppbase2, \
  267. cname, cbase, cbase2) \
  268. UPB_DECLARE_TYPE(cppname, cname) \
  269. UPB_C_UPCASTS2(cname, cbase, cbase2)
  270. #endif /* defined(__cplusplus) */
  271. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  272. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  273. #define UPB_UNUSED(var) (void)var
  274. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  275. * evaluated. This prevents "unused variable" warnings. */
  276. #ifdef NDEBUG
  277. #define UPB_ASSERT(expr) do {} while (false && (expr))
  278. #else
  279. #define UPB_ASSERT(expr) assert(expr)
  280. #endif
  281. /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
  282. * exist in debug mode. This turns into regular assert. */
  283. #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
  284. #ifdef __GNUC__
  285. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  286. #else
  287. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  288. #endif
  289. /* Generic function type. */
  290. typedef void upb_func();
  291. /* C++ Casts ******************************************************************/
  292. #ifdef __cplusplus
  293. namespace upb {
  294. template <class T> class Pointer;
  295. /* Casts to a subclass. The caller must know that cast is correct; an
  296. * incorrect cast will throw an assertion failure in debug mode.
  297. *
  298. * Example:
  299. * upb::Def* def = GetDef();
  300. * // Assert-fails if this was not actually a MessageDef.
  301. * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
  302. *
  303. * Note that downcasts are only defined for some types (at the moment you can
  304. * only downcast from a upb::Def to a specific Def type). */
  305. template<class To, class From> To down_cast(From* f);
  306. /* Casts to a subclass. If the class does not actually match the given To type,
  307. * returns NULL.
  308. *
  309. * Example:
  310. * upb::Def* def = GetDef();
  311. * // md will be NULL if this was not actually a MessageDef.
  312. * upb::MessgeDef* md = upb::down_cast<upb::MessageDef>(def);
  313. *
  314. * Note that dynamic casts are only defined for some types (at the moment you
  315. * can only downcast from a upb::Def to a specific Def type).. */
  316. template<class To, class From> To dyn_cast(From* f);
  317. /* Casts to any base class, or the type itself (ie. can be a no-op).
  318. *
  319. * Example:
  320. * upb::MessageDef* md = GetDef();
  321. * // This will fail to compile if this wasn't actually a base class.
  322. * upb::Def* def = upb::upcast(md);
  323. */
  324. template <class T> inline Pointer<T> upcast(T *f) { return Pointer<T>(f); }
  325. /* Attempt upcast to specific base class.
  326. *
  327. * Example:
  328. * upb::MessageDef* md = GetDef();
  329. * upb::upcast_to<upb::Def>(md)->MethodOnDef();
  330. */
  331. template <class T, class F> inline T* upcast_to(F *f) {
  332. return static_cast<T*>(upcast(f));
  333. }
  334. /* PointerBase<T>: implementation detail of upb::upcast().
  335. * It is implicitly convertable to pointers to the Base class(es).
  336. */
  337. template <class T, class Base>
  338. class PointerBase {
  339. public:
  340. explicit PointerBase(T* ptr) : ptr_(ptr) {}
  341. operator T*() { return ptr_; }
  342. operator Base*() { return (Base*)ptr_; }
  343. private:
  344. T* ptr_;
  345. };
  346. template <class T, class Base, class Base2>
  347. class PointerBase2 : public PointerBase<T, Base> {
  348. public:
  349. explicit PointerBase2(T* ptr) : PointerBase<T, Base>(ptr) {}
  350. operator Base2*() { return Pointer<Base>(*this); }
  351. };
  352. }
  353. #endif
  354. /* A list of types as they are encoded on-the-wire. */
  355. typedef enum {
  356. UPB_WIRE_TYPE_VARINT = 0,
  357. UPB_WIRE_TYPE_64BIT = 1,
  358. UPB_WIRE_TYPE_DELIMITED = 2,
  359. UPB_WIRE_TYPE_START_GROUP = 3,
  360. UPB_WIRE_TYPE_END_GROUP = 4,
  361. UPB_WIRE_TYPE_32BIT = 5
  362. } upb_wiretype_t;
  363. /* upb::ErrorSpace ************************************************************/
  364. /* A upb::ErrorSpace represents some domain of possible error values. This lets
  365. * upb::Status attach specific error codes to operations, like POSIX/C errno,
  366. * Win32 error codes, etc. Clients who want to know the very specific error
  367. * code can check the error space and then know the type of the integer code.
  368. *
  369. * NOTE: upb::ErrorSpace is currently not used and should be considered
  370. * experimental. It is important primarily in cases where upb is performing
  371. * I/O, but upb doesn't currently have any components that do this. */
  372. UPB_DECLARE_TYPE(upb::ErrorSpace, upb_errorspace)
  373. #ifdef __cplusplus
  374. class upb::ErrorSpace {
  375. #else
  376. struct upb_errorspace {
  377. #endif
  378. const char *name;
  379. };
  380. /* upb::Status ****************************************************************/
  381. /* upb::Status represents a success or failure status and error message.
  382. * It owns no resources and allocates no memory, so it should work
  383. * even in OOM situations. */
  384. UPB_DECLARE_TYPE(upb::Status, upb_status)
  385. /* The maximum length of an error message before it will get truncated. */
  386. #define UPB_STATUS_MAX_MESSAGE 128
  387. UPB_BEGIN_EXTERN_C
  388. const char *upb_status_errmsg(const upb_status *status);
  389. bool upb_ok(const upb_status *status);
  390. upb_errorspace *upb_status_errspace(const upb_status *status);
  391. int upb_status_errcode(const upb_status *status);
  392. /* Any of the functions that write to a status object allow status to be NULL,
  393. * to support use cases where the function's caller does not care about the
  394. * status message. */
  395. void upb_status_clear(upb_status *status);
  396. void upb_status_seterrmsg(upb_status *status, const char *msg);
  397. void upb_status_seterrf(upb_status *status, const char *fmt, ...);
  398. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
  399. void upb_status_copy(upb_status *to, const upb_status *from);
  400. UPB_END_EXTERN_C
  401. #ifdef __cplusplus
  402. class upb::Status {
  403. public:
  404. Status() { upb_status_clear(this); }
  405. /* Returns true if there is no error. */
  406. bool ok() const { return upb_ok(this); }
  407. /* Optional error space and code, useful if the caller wants to
  408. * programmatically check the specific kind of error. */
  409. ErrorSpace* error_space() { return upb_status_errspace(this); }
  410. int error_code() const { return upb_status_errcode(this); }
  411. /* The returned string is invalidated by any other call into the status. */
  412. const char *error_message() const { return upb_status_errmsg(this); }
  413. /* The error message will be truncated if it is longer than
  414. * UPB_STATUS_MAX_MESSAGE-4. */
  415. void SetErrorMessage(const char* msg) { upb_status_seterrmsg(this, msg); }
  416. void SetFormattedErrorMessage(const char* fmt, ...) {
  417. va_list args;
  418. va_start(args, fmt);
  419. upb_status_vseterrf(this, fmt, args);
  420. va_end(args);
  421. }
  422. /* Resets the status to a successful state with no message. */
  423. void Clear() { upb_status_clear(this); }
  424. void CopyFrom(const Status& other) { upb_status_copy(this, &other); }
  425. private:
  426. UPB_DISALLOW_COPY_AND_ASSIGN(Status)
  427. #else
  428. struct upb_status {
  429. #endif
  430. bool ok_;
  431. /* Specific status code defined by some error space (optional). */
  432. int code_;
  433. upb_errorspace *error_space_;
  434. /* TODO(haberman): add file/line of error? */
  435. /* Error message; NULL-terminated. */
  436. char msg[UPB_STATUS_MAX_MESSAGE];
  437. };
  438. #define UPB_STATUS_INIT {true, 0, NULL, {0}}
  439. /** Built-in error spaces. ****************************************************/
  440. /* Errors raised by upb that we want to be able to detect programmatically. */
  441. typedef enum {
  442. UPB_NOMEM /* Can't reuse ENOMEM because it is POSIX, not ISO C. */
  443. } upb_errcode_t;
  444. extern upb_errorspace upb_upberr;
  445. void upb_upberr_setoom(upb_status *s);
  446. /* Since errno is defined by standard C, we define an error space for it in
  447. * core upb. Other error spaces should be defined in other, platform-specific
  448. * modules. */
  449. extern upb_errorspace upb_errnoerr;
  450. /** upb::Allocator ************************************************************/
  451. /* A upb::Allocator is a possibly-stateful allocator object.
  452. *
  453. * It could either be an arena allocator (which doesn't require individual
  454. * free() calls) or a regular malloc() (which does). The client must therefore
  455. * free memory unless it knows that the allocator is an arena allocator. */
  456. UPB_DECLARE_TYPE(upb::Allocator, upb_alloc)
  457. /* A malloc()/free() function.
  458. * If "size" is 0 then the function acts like free(), otherwise it acts like
  459. * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
  460. typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
  461. size_t size);
  462. #ifdef __cplusplus
  463. class upb::Allocator UPB_FINAL {
  464. public:
  465. Allocator() {}
  466. private:
  467. UPB_DISALLOW_COPY_AND_ASSIGN(Allocator)
  468. public:
  469. #else
  470. struct upb_alloc {
  471. #endif /* __cplusplus */
  472. upb_alloc_func *func;
  473. };
  474. UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
  475. UPB_ASSERT(alloc);
  476. return alloc->func(alloc, NULL, 0, size);
  477. }
  478. UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  479. size_t size) {
  480. UPB_ASSERT(alloc);
  481. return alloc->func(alloc, ptr, oldsize, size);
  482. }
  483. UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
  484. assert(alloc);
  485. alloc->func(alloc, ptr, 0, 0);
  486. }
  487. /* The global allocator used by upb. Uses the standard malloc()/free(). */
  488. extern upb_alloc upb_alloc_global;
  489. /* Functions that hard-code the global malloc.
  490. *
  491. * We still get benefit because we can put custom logic into our global
  492. * allocator, like injecting out-of-memory faults in debug/testing builds. */
  493. UPB_INLINE void *upb_gmalloc(size_t size) {
  494. return upb_malloc(&upb_alloc_global, size);
  495. }
  496. UPB_INLINE void *upb_grealloc(void *ptr, size_t oldsize, size_t size) {
  497. return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
  498. }
  499. UPB_INLINE void upb_gfree(void *ptr) {
  500. upb_free(&upb_alloc_global, ptr);
  501. }
  502. /* upb::Arena *****************************************************************/
  503. /* upb::Arena is a specific allocator implementation that uses arena allocation.
  504. * The user provides an allocator that will be used to allocate the underlying
  505. * arena blocks. Arenas by nature do not require the individual allocations
  506. * to be freed. However the Arena does allow users to register cleanup
  507. * functions that will run when the arena is destroyed.
  508. *
  509. * A upb::Arena is *not* thread-safe.
  510. *
  511. * You could write a thread-safe arena allocator that satisfies the
  512. * upb::Allocator interface, but it would not be as efficient for the
  513. * single-threaded case. */
  514. UPB_DECLARE_TYPE(upb::Arena, upb_arena)
  515. typedef void upb_cleanup_func(void *ud);
  516. #define UPB_ARENA_BLOCK_OVERHEAD (sizeof(size_t)*4)
  517. UPB_BEGIN_EXTERN_C
  518. void upb_arena_init(upb_arena *a);
  519. void upb_arena_init2(upb_arena *a, void *mem, size_t n, upb_alloc *alloc);
  520. void upb_arena_uninit(upb_arena *a);
  521. bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud);
  522. size_t upb_arena_bytesallocated(const upb_arena *a);
  523. void upb_arena_setnextblocksize(upb_arena *a, size_t size);
  524. void upb_arena_setmaxblocksize(upb_arena *a, size_t size);
  525. UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
  526. UPB_END_EXTERN_C
  527. #ifdef __cplusplus
  528. class upb::Arena {
  529. public:
  530. /* A simple arena with no initial memory block and the default allocator. */
  531. Arena() { upb_arena_init(this); }
  532. /* Constructs an arena with the given initial block which allocates blocks
  533. * with the given allocator. The given allocator must outlive the Arena.
  534. *
  535. * If you pass NULL for the allocator it will default to the global allocator
  536. * upb_alloc_global, and NULL/0 for the initial block will cause there to be
  537. * no initial block. */
  538. Arena(void *mem, size_t len, Allocator* a) {
  539. upb_arena_init2(this, mem, len, a);
  540. }
  541. ~Arena() { upb_arena_uninit(this); }
  542. /* Sets the size of the next block the Arena will request (unless the
  543. * requested allocation is larger). Each block will double in size until the
  544. * max limit is reached. */
  545. void SetNextBlockSize(size_t size) { upb_arena_setnextblocksize(this, size); }
  546. /* Sets the maximum block size. No blocks larger than this will be requested
  547. * from the underlying allocator unless individual arena allocations are
  548. * larger. */
  549. void SetMaxBlockSize(size_t size) { upb_arena_setmaxblocksize(this, size); }
  550. /* Allows this arena to be used as a generic allocator.
  551. *
  552. * The arena does not need free() calls so when using Arena as an allocator
  553. * it is safe to skip them. However they are no-ops so there is no harm in
  554. * calling free() either. */
  555. Allocator* allocator() { return upb_arena_alloc(this); }
  556. /* Add a cleanup function to run when the arena is destroyed.
  557. * Returns false on out-of-memory. */
  558. bool AddCleanup(upb_cleanup_func* func, void* ud) {
  559. return upb_arena_addcleanup(this, func, ud);
  560. }
  561. /* Total number of bytes that have been allocated. It is undefined what
  562. * Realloc() does to this counter. */
  563. size_t BytesAllocated() const {
  564. return upb_arena_bytesallocated(this);
  565. }
  566. private:
  567. UPB_DISALLOW_COPY_AND_ASSIGN(Arena)
  568. #else
  569. struct upb_arena {
  570. #endif /* __cplusplus */
  571. /* We implement the allocator interface.
  572. * This must be the first member of upb_arena! */
  573. upb_alloc alloc;
  574. /* Allocator to allocate arena blocks. We are responsible for freeing these
  575. * when we are destroyed. */
  576. upb_alloc *block_alloc;
  577. size_t bytes_allocated;
  578. size_t next_block_size;
  579. size_t max_block_size;
  580. /* Linked list of blocks. Points to an arena_block, defined in env.c */
  581. void *block_head;
  582. /* Cleanup entries. Pointer to a cleanup_ent, defined in env.c */
  583. void *cleanup_head;
  584. /* For future expansion, since the size of this struct is exposed to users. */
  585. void *future1;
  586. void *future2;
  587. };
  588. /* upb::Environment ***********************************************************/
  589. /* A upb::Environment provides a means for injecting malloc and an
  590. * error-reporting callback into encoders/decoders. This allows them to be
  591. * independent of nearly all assumptions about their actual environment.
  592. *
  593. * It is also a container for allocating the encoders/decoders themselves that
  594. * insulates clients from knowing their actual size. This provides ABI
  595. * compatibility even if the size of the objects change. And this allows the
  596. * structure definitions to be in the .c files instead of the .h files, making
  597. * the .h files smaller and more readable.
  598. *
  599. * We might want to consider renaming this to "Pipeline" if/when the concept of
  600. * a pipeline element becomes more formalized. */
  601. UPB_DECLARE_TYPE(upb::Environment, upb_env)
  602. /* A function that receives an error report from an encoder or decoder. The
  603. * callback can return true to request that the error should be recovered, but
  604. * if the error is not recoverable this has no effect. */
  605. typedef bool upb_error_func(void *ud, const upb_status *status);
  606. UPB_BEGIN_EXTERN_C
  607. void upb_env_init(upb_env *e);
  608. void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc);
  609. void upb_env_uninit(upb_env *e);
  610. void upb_env_initonly(upb_env *e);
  611. UPB_INLINE upb_arena *upb_env_arena(upb_env *e) { return (upb_arena*)e; }
  612. bool upb_env_ok(const upb_env *e);
  613. void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud);
  614. /* Convenience wrappers around the methods of the contained arena. */
  615. void upb_env_reporterrorsto(upb_env *e, upb_status *s);
  616. bool upb_env_reporterror(upb_env *e, const upb_status *s);
  617. void *upb_env_malloc(upb_env *e, size_t size);
  618. void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size);
  619. void upb_env_free(upb_env *e, void *ptr);
  620. bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud);
  621. size_t upb_env_bytesallocated(const upb_env *e);
  622. UPB_END_EXTERN_C
  623. #ifdef __cplusplus
  624. class upb::Environment {
  625. public:
  626. /* The given Arena must outlive this environment. */
  627. Environment() { upb_env_initonly(this); }
  628. Environment(void *mem, size_t len, Allocator *a) : arena_(mem, len, a) {
  629. upb_env_initonly(this);
  630. }
  631. Arena* arena() { return upb_env_arena(this); }
  632. /* Set a custom error reporting function. */
  633. void SetErrorFunction(upb_error_func* func, void* ud) {
  634. upb_env_seterrorfunc(this, func, ud);
  635. }
  636. /* Set the error reporting function to simply copy the status to the given
  637. * status and abort. */
  638. void ReportErrorsTo(Status* status) { upb_env_reporterrorsto(this, status); }
  639. /* Returns true if all allocations and AddCleanup() calls have succeeded,
  640. * and no errors were reported with ReportError() (except ones that recovered
  641. * successfully). */
  642. bool ok() const { return upb_env_ok(this); }
  643. /* Reports an error to this environment's callback, returning true if
  644. * the caller should try to recover. */
  645. bool ReportError(const Status* status) {
  646. return upb_env_reporterror(this, status);
  647. }
  648. private:
  649. UPB_DISALLOW_COPY_AND_ASSIGN(Environment)
  650. #else
  651. struct upb_env {
  652. #endif /* __cplusplus */
  653. upb_arena arena_;
  654. upb_error_func *error_func_;
  655. void *error_ud_;
  656. bool ok_;
  657. };
  658. /* upb::InlinedArena **********************************************************/
  659. /* upb::InlinedEnvironment ****************************************************/
  660. /* upb::InlinedArena and upb::InlinedEnvironment seed their arenas with a
  661. * predefined amount of memory. No heap memory will be allocated until the
  662. * initial block is exceeded.
  663. *
  664. * These types only exist in C++ */
  665. #ifdef __cplusplus
  666. template <int N> class upb::InlinedArena : public upb::Arena {
  667. public:
  668. InlinedArena() : Arena(initial_block_, N, NULL) {}
  669. explicit InlinedArena(Allocator* a) : Arena(initial_block_, N, a) {}
  670. private:
  671. UPB_DISALLOW_COPY_AND_ASSIGN(InlinedArena)
  672. char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD];
  673. };
  674. template <int N> class upb::InlinedEnvironment : public upb::Environment {
  675. public:
  676. InlinedEnvironment() : Environment(initial_block_, N, NULL) {}
  677. explicit InlinedEnvironment(Allocator *a)
  678. : Environment(initial_block_, N, a) {}
  679. private:
  680. UPB_DISALLOW_COPY_AND_ASSIGN(InlinedEnvironment)
  681. char initial_block_[N + UPB_ARENA_BLOCK_OVERHEAD];
  682. };
  683. #endif /* __cplusplus */
  684. #endif /* UPB_H_ */
  685. #ifdef __cplusplus
  686. extern "C" {
  687. #endif
  688. /* upb_value ******************************************************************/
  689. /* A tagged union (stored untagged inside the table) so that we can check that
  690. * clients calling table accessors are correctly typed without having to have
  691. * an explosion of accessors. */
  692. typedef enum {
  693. UPB_CTYPE_INT32 = 1,
  694. UPB_CTYPE_INT64 = 2,
  695. UPB_CTYPE_UINT32 = 3,
  696. UPB_CTYPE_UINT64 = 4,
  697. UPB_CTYPE_BOOL = 5,
  698. UPB_CTYPE_CSTR = 6,
  699. UPB_CTYPE_PTR = 7,
  700. UPB_CTYPE_CONSTPTR = 8,
  701. UPB_CTYPE_FPTR = 9,
  702. UPB_CTYPE_FLOAT = 10,
  703. UPB_CTYPE_DOUBLE = 11
  704. } upb_ctype_t;
  705. typedef struct {
  706. uint64_t val;
  707. #ifndef NDEBUG
  708. /* In debug mode we carry the value type around also so we can check accesses
  709. * to be sure the right member is being read. */
  710. upb_ctype_t ctype;
  711. #endif
  712. } upb_value;
  713. #ifdef NDEBUG
  714. #define SET_TYPE(dest, val) UPB_UNUSED(val)
  715. #else
  716. #define SET_TYPE(dest, val) dest = val
  717. #endif
  718. /* Like strdup(), which isn't always available since it's not ANSI C. */
  719. char *upb_strdup(const char *s, upb_alloc *a);
  720. /* Variant that works with a length-delimited rather than NULL-delimited string,
  721. * as supported by strtable. */
  722. char *upb_strdup2(const char *s, size_t len, upb_alloc *a);
  723. UPB_INLINE char *upb_gstrdup(const char *s) {
  724. return upb_strdup(s, &upb_alloc_global);
  725. }
  726. UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
  727. upb_ctype_t ctype) {
  728. v->val = val;
  729. SET_TYPE(v->ctype, ctype);
  730. }
  731. UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
  732. upb_value ret;
  733. _upb_value_setval(&ret, val, ctype);
  734. return ret;
  735. }
  736. /* For each value ctype, define the following set of functions:
  737. *
  738. * // Get/set an int32 from a upb_value.
  739. * int32_t upb_value_getint32(upb_value val);
  740. * void upb_value_setint32(upb_value *val, int32_t cval);
  741. *
  742. * // Construct a new upb_value from an int32.
  743. * upb_value upb_value_int32(int32_t val); */
  744. #define FUNCS(name, membername, type_t, converter, proto_type) \
  745. UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
  746. val->val = (converter)cval; \
  747. SET_TYPE(val->ctype, proto_type); \
  748. } \
  749. UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
  750. upb_value ret; \
  751. upb_value_set ## name(&ret, val); \
  752. return ret; \
  753. } \
  754. UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
  755. UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \
  756. return (type_t)(converter)val.val; \
  757. }
  758. FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
  759. FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
  760. FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
  761. FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
  762. FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
  763. FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
  764. FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
  765. FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
  766. FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
  767. #undef FUNCS
  768. UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
  769. memcpy(&val->val, &cval, sizeof(cval));
  770. SET_TYPE(val->ctype, UPB_CTYPE_FLOAT);
  771. }
  772. UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
  773. memcpy(&val->val, &cval, sizeof(cval));
  774. SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE);
  775. }
  776. UPB_INLINE upb_value upb_value_float(float cval) {
  777. upb_value ret;
  778. upb_value_setfloat(&ret, cval);
  779. return ret;
  780. }
  781. UPB_INLINE upb_value upb_value_double(double cval) {
  782. upb_value ret;
  783. upb_value_setdouble(&ret, cval);
  784. return ret;
  785. }
  786. #undef SET_TYPE
  787. /* upb_tabkey *****************************************************************/
  788. /* Either:
  789. * 1. an actual integer key, or
  790. * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
  791. *
  792. * ...depending on whether this is a string table or an int table. We would
  793. * make this a union of those two types, but C89 doesn't support statically
  794. * initializing a non-first union member. */
  795. typedef uintptr_t upb_tabkey;
  796. #define UPB_TABKEY_NUM(n) n
  797. #define UPB_TABKEY_NONE 0
  798. /* The preprocessor isn't quite powerful enough to turn the compile-time string
  799. * length into a byte-wise string representation, so code generation needs to
  800. * help it along.
  801. *
  802. * "len1" is the low byte and len4 is the high byte. */
  803. #ifdef UPB_BIG_ENDIAN
  804. #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
  805. (uintptr_t)(len4 len3 len2 len1 strval)
  806. #else
  807. #define UPB_TABKEY_STR(len1, len2, len3, len4, strval) \
  808. (uintptr_t)(len1 len2 len3 len4 strval)
  809. #endif
  810. UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
  811. char* mem = (char*)key;
  812. if (len) memcpy(len, mem, sizeof(*len));
  813. return mem + sizeof(*len);
  814. }
  815. /* upb_tabval *****************************************************************/
  816. #ifdef __cplusplus
  817. /* Status initialization not supported.
  818. *
  819. * This separate definition is necessary because in C++, UINTPTR_MAX isn't
  820. * reliably available. */
  821. typedef struct {
  822. uint64_t val;
  823. } upb_tabval;
  824. #else
  825. /* C -- supports static initialization, but to support static initialization of
  826. * both integers and points for both 32 and 64 bit targets, it takes a little
  827. * bit of doing. */
  828. #if UINTPTR_MAX == 0xffffffffffffffffULL
  829. #define UPB_PTR_IS_64BITS
  830. #elif UINTPTR_MAX != 0xffffffff
  831. #error Could not determine how many bits pointers are.
  832. #endif
  833. typedef union {
  834. /* For static initialization.
  835. *
  836. * Unfortunately this ugliness is necessary -- it is the only way that we can,
  837. * with -std=c89 -pedantic, statically initialize this to either a pointer or
  838. * an integer on 32-bit platforms. */
  839. struct {
  840. #ifdef UPB_PTR_IS_64BITS
  841. uintptr_t val;
  842. #else
  843. uintptr_t val1;
  844. uintptr_t val2;
  845. #endif
  846. } staticinit;
  847. /* The normal accessor that we use for everything at runtime. */
  848. uint64_t val;
  849. } upb_tabval;
  850. #ifdef UPB_PTR_IS_64BITS
  851. #define UPB_TABVALUE_INT_INIT(v) {{v}}
  852. #define UPB_TABVALUE_EMPTY_INIT {{-1}}
  853. #else
  854. /* 32-bit pointers */
  855. #ifdef UPB_BIG_ENDIAN
  856. #define UPB_TABVALUE_INT_INIT(v) {{0, v}}
  857. #define UPB_TABVALUE_EMPTY_INIT {{-1, -1}}
  858. #else
  859. #define UPB_TABVALUE_INT_INIT(v) {{v, 0}}
  860. #define UPB_TABVALUE_EMPTY_INIT {{-1, -1}}
  861. #endif
  862. #endif
  863. #define UPB_TABVALUE_PTR_INIT(v) UPB_TABVALUE_INT_INIT((uintptr_t)v)
  864. #undef UPB_PTR_IS_64BITS
  865. #endif /* __cplusplus */
  866. /* upb_table ******************************************************************/
  867. typedef struct _upb_tabent {
  868. upb_tabkey key;
  869. upb_tabval val;
  870. /* Internal chaining. This is const so we can create static initializers for
  871. * tables. We cast away const sometimes, but *only* when the containing
  872. * upb_table is known to be non-const. This requires a bit of care, but
  873. * the subtlety is confined to table.c. */
  874. const struct _upb_tabent *next;
  875. } upb_tabent;
  876. typedef struct {
  877. size_t count; /* Number of entries in the hash part. */
  878. size_t mask; /* Mask to turn hash value -> bucket. */
  879. upb_ctype_t ctype; /* Type of all values. */
  880. uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
  881. /* Hash table entries.
  882. * Making this const isn't entirely accurate; what we really want is for it to
  883. * have the same const-ness as the table it's inside. But there's no way to
  884. * declare that in C. So we have to make it const so that we can statically
  885. * initialize const hash tables. Then we cast away const when we have to.
  886. */
  887. const upb_tabent *entries;
  888. #ifndef NDEBUG
  889. /* This table's allocator. We make the user pass it in to every relevant
  890. * function and only use this to check it in debug mode. We do this solely
  891. * to keep upb_table as small as possible. This might seem slightly paranoid
  892. * but the plan is to use upb_table for all map fields and extension sets in
  893. * a forthcoming message representation, so there could be a lot of these.
  894. * If this turns out to be too annoying later, we can change it (since this
  895. * is an internal-only header file). */
  896. upb_alloc *alloc;
  897. #endif
  898. } upb_table;
  899. #ifdef NDEBUG
  900. # define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \
  901. {count, mask, ctype, size_lg2, entries}
  902. #else
  903. # ifdef UPB_DEBUG_REFS
  904. /* At the moment the only mutable tables we statically initialize are debug
  905. * ref tables. */
  906. # define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \
  907. {count, mask, ctype, size_lg2, entries, &upb_alloc_debugrefs}
  908. # else
  909. # define UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries) \
  910. {count, mask, ctype, size_lg2, entries, NULL}
  911. # endif
  912. #endif
  913. typedef struct {
  914. upb_table t;
  915. } upb_strtable;
  916. #define UPB_STRTABLE_INIT(count, mask, ctype, size_lg2, entries) \
  917. {UPB_TABLE_INIT(count, mask, ctype, size_lg2, entries)}
  918. #define UPB_EMPTY_STRTABLE_INIT(ctype) \
  919. UPB_STRTABLE_INIT(0, 0, ctype, 0, NULL)
  920. typedef struct {
  921. upb_table t; /* For entries that don't fit in the array part. */
  922. const upb_tabval *array; /* Array part of the table. See const note above. */
  923. size_t array_size; /* Array part size. */
  924. size_t array_count; /* Array part number of elements. */
  925. } upb_inttable;
  926. #define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
  927. {UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount}
  928. #define UPB_EMPTY_INTTABLE_INIT(ctype) \
  929. UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
  930. #define UPB_ARRAY_EMPTYENT -1
  931. UPB_INLINE size_t upb_table_size(const upb_table *t) {
  932. if (t->size_lg2 == 0)
  933. return 0;
  934. else
  935. return 1 << t->size_lg2;
  936. }
  937. /* Internal-only functions, in .h file only out of necessity. */
  938. UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
  939. return e->key == 0;
  940. }
  941. /* Used by some of the unit tests for generic hashing functionality. */
  942. uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
  943. UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
  944. return key;
  945. }
  946. UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
  947. return (uint32_t)key;
  948. }
  949. static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
  950. return t->entries + (hash & t->mask);
  951. }
  952. UPB_INLINE bool upb_arrhas(upb_tabval key) {
  953. return key.val != (uint64_t)-1;
  954. }
  955. /* Initialize and uninitialize a table, respectively. If memory allocation
  956. * failed, false is returned that the table is uninitialized. */
  957. bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a);
  958. bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a);
  959. void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a);
  960. void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a);
  961. UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) {
  962. return upb_inttable_init2(table, ctype, &upb_alloc_global);
  963. }
  964. UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) {
  965. return upb_strtable_init2(table, ctype, &upb_alloc_global);
  966. }
  967. UPB_INLINE void upb_inttable_uninit(upb_inttable *table) {
  968. upb_inttable_uninit2(table, &upb_alloc_global);
  969. }
  970. UPB_INLINE void upb_strtable_uninit(upb_strtable *table) {
  971. upb_strtable_uninit2(table, &upb_alloc_global);
  972. }
  973. /* Returns the number of values in the table. */
  974. size_t upb_inttable_count(const upb_inttable *t);
  975. UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
  976. return t->t.count;
  977. }
  978. void upb_inttable_packedsize(const upb_inttable *t, size_t *size);
  979. void upb_strtable_packedsize(const upb_strtable *t, size_t *size);
  980. upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs,
  981. size_t size);
  982. upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs,
  983. size_t size);
  984. /* Inserts the given key into the hashtable with the given value. The key must
  985. * not already exist in the hash table. For string tables, the key must be
  986. * NULL-terminated, and the table will make an internal copy of the key.
  987. * Inttables must not insert a value of UINTPTR_MAX.
  988. *
  989. * If a table resize was required but memory allocation failed, false is
  990. * returned and the table is unchanged. */
  991. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  992. upb_alloc *a);
  993. bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len,
  994. upb_value val, upb_alloc *a);
  995. UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key,
  996. upb_value val) {
  997. return upb_inttable_insert2(t, key, val, &upb_alloc_global);
  998. }
  999. UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key,
  1000. size_t len, upb_value val) {
  1001. return upb_strtable_insert3(t, key, len, val, &upb_alloc_global);
  1002. }
  1003. /* For NULL-terminated strings. */
  1004. UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
  1005. upb_value val) {
  1006. return upb_strtable_insert2(t, key, strlen(key), val);
  1007. }
  1008. /* Looks up key in this table, returning "true" if the key was found.
  1009. * If v is non-NULL, copies the value for this key into *v. */
  1010. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
  1011. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  1012. upb_value *v);
  1013. /* For NULL-terminated strings. */
  1014. UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
  1015. upb_value *v) {
  1016. return upb_strtable_lookup2(t, key, strlen(key), v);
  1017. }
  1018. /* Removes an item from the table. Returns true if the remove was successful,
  1019. * and stores the removed item in *val if non-NULL. */
  1020. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
  1021. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  1022. upb_value *val, upb_alloc *alloc);
  1023. UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key,
  1024. size_t len, upb_value *val) {
  1025. return upb_strtable_remove3(t, key, len, val, &upb_alloc_global);
  1026. }
  1027. /* For NULL-terminated strings. */
  1028. UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
  1029. upb_value *v) {
  1030. return upb_strtable_remove2(t, key, strlen(key), v);
  1031. }
  1032. /* Updates an existing entry in an inttable. If the entry does not exist,
  1033. * returns false and does nothing. Unlike insert/remove, this does not
  1034. * invalidate iterators. */
  1035. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
  1036. /* Handy routines for treating an inttable like a stack. May not be mixed with
  1037. * other insert/remove calls. */
  1038. bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a);
  1039. upb_value upb_inttable_pop(upb_inttable *t);
  1040. UPB_INLINE bool upb_inttable_push(upb_inttable *t, upb_value val) {
  1041. return upb_inttable_push2(t, val, &upb_alloc_global);
  1042. }
  1043. /* Convenience routines for inttables with pointer keys. */
  1044. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  1045. upb_alloc *a);
  1046. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
  1047. bool upb_inttable_lookupptr(
  1048. const upb_inttable *t, const void *key, upb_value *val);
  1049. UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key,
  1050. upb_value val) {
  1051. return upb_inttable_insertptr2(t, key, val, &upb_alloc_global);
  1052. }
  1053. /* Optimizes the table for the current set of entries, for both memory use and
  1054. * lookup time. Client should call this after all entries have been inserted;
  1055. * inserting more entries is legal, but will likely require a table resize. */
  1056. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a);
  1057. UPB_INLINE void upb_inttable_compact(upb_inttable *t) {
  1058. upb_inttable_compact2(t, &upb_alloc_global);
  1059. }
  1060. /* A special-case inlinable version of the lookup routine for 32-bit
  1061. * integers. */
  1062. UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
  1063. upb_value *v) {
  1064. *v = upb_value_int32(0); /* Silence compiler warnings. */
  1065. if (key < t->array_size) {
  1066. upb_tabval arrval = t->array[key];
  1067. if (upb_arrhas(arrval)) {
  1068. _upb_value_setval(v, arrval.val, t->t.ctype);
  1069. return true;
  1070. } else {
  1071. return false;
  1072. }
  1073. } else {
  1074. const upb_tabent *e;
  1075. if (t->t.entries == NULL) return false;
  1076. for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
  1077. if ((uint32_t)e->key == key) {
  1078. _upb_value_setval(v, e->val.val, t->t.ctype);
  1079. return true;
  1080. }
  1081. if (e->next == NULL) return false;
  1082. }
  1083. }
  1084. }
  1085. /* Exposed for testing only. */
  1086. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a);
  1087. /* Iterators ******************************************************************/
  1088. /* Iterators for int and string tables. We are subject to some kind of unusual
  1089. * design constraints:
  1090. *
  1091. * For high-level languages:
  1092. * - we must be able to guarantee that we don't crash or corrupt memory even if
  1093. * the program accesses an invalidated iterator.
  1094. *
  1095. * For C++11 range-based for:
  1096. * - iterators must be copyable
  1097. * - iterators must be comparable
  1098. * - it must be possible to construct an "end" value.
  1099. *
  1100. * Iteration order is undefined.
  1101. *
  1102. * Modifying the table invalidates iterators. upb_{str,int}table_done() is
  1103. * guaranteed to work even on an invalidated iterator, as long as the table it
  1104. * is iterating over has not been freed. Calling next() or accessing data from
  1105. * an invalidated iterator yields unspecified elements from the table, but it is
  1106. * guaranteed not to crash and to return real table elements (except when done()
  1107. * is true). */
  1108. /* upb_strtable_iter **********************************************************/
  1109. /* upb_strtable_iter i;
  1110. * upb_strtable_begin(&i, t);
  1111. * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1112. * const char *key = upb_strtable_iter_key(&i);
  1113. * const upb_value val = upb_strtable_iter_value(&i);
  1114. * // ...
  1115. * }
  1116. */
  1117. typedef struct {
  1118. const upb_strtable *t;
  1119. size_t index;
  1120. } upb_strtable_iter;
  1121. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
  1122. void upb_strtable_next(upb_strtable_iter *i);
  1123. bool upb_strtable_done(const upb_strtable_iter *i);
  1124. const char *upb_strtable_iter_key(const upb_strtable_iter *i);
  1125. size_t upb_strtable_iter_keylength(const upb_strtable_iter *i);
  1126. upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
  1127. void upb_strtable_iter_setdone(upb_strtable_iter *i);
  1128. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  1129. const upb_strtable_iter *i2);
  1130. /* upb_inttable_iter **********************************************************/
  1131. /* upb_inttable_iter i;
  1132. * upb_inttable_begin(&i, t);
  1133. * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1134. * uintptr_t key = upb_inttable_iter_key(&i);
  1135. * upb_value val = upb_inttable_iter_value(&i);
  1136. * // ...
  1137. * }
  1138. */
  1139. typedef struct {
  1140. const upb_inttable *t;
  1141. size_t index;
  1142. bool array_part;
  1143. } upb_inttable_iter;
  1144. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
  1145. void upb_inttable_next(upb_inttable_iter *i);
  1146. bool upb_inttable_done(const upb_inttable_iter *i);
  1147. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
  1148. upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
  1149. void upb_inttable_iter_setdone(upb_inttable_iter *i);
  1150. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  1151. const upb_inttable_iter *i2);
  1152. #ifdef __cplusplus
  1153. } /* extern "C" */
  1154. #endif
  1155. #endif /* UPB_TABLE_H_ */
  1156. /* Reference tracking will check ref()/unref() operations to make sure the
  1157. * ref ownership is correct. Where possible it will also make tools like
  1158. * Valgrind attribute ref leaks to the code that took the leaked ref, not
  1159. * the code that originally created the object.
  1160. *
  1161. * Enabling this requires the application to define upb_lock()/upb_unlock()
  1162. * functions that acquire/release a global mutex (or #define UPB_THREAD_UNSAFE).
  1163. * For this reason we don't enable it by default, even in debug builds.
  1164. */
  1165. /* #define UPB_DEBUG_REFS */
  1166. #ifdef __cplusplus
  1167. namespace upb {
  1168. class RefCounted;
  1169. template <class T> class reffed_ptr;
  1170. }
  1171. #endif
  1172. UPB_DECLARE_TYPE(upb::RefCounted, upb_refcounted)
  1173. struct upb_refcounted_vtbl;
  1174. #ifdef __cplusplus
  1175. class upb::RefCounted {
  1176. public:
  1177. /* Returns true if the given object is frozen. */
  1178. bool IsFrozen() const;
  1179. /* Increases the ref count, the new ref is owned by "owner" which must not
  1180. * already own a ref (and should not itself be a refcounted object if the ref
  1181. * could possibly be circular; see below).
  1182. * Thread-safe iff "this" is frozen. */
  1183. void Ref(const void *owner) const;
  1184. /* Release a ref that was acquired from upb_refcounted_ref() and collects any
  1185. * objects it can. */
  1186. void Unref(const void *owner) const;
  1187. /* Moves an existing ref from "from" to "to", without changing the overall
  1188. * ref count. DonateRef(foo, NULL, owner) is the same as Ref(foo, owner),
  1189. * but "to" may not be NULL. */
  1190. void DonateRef(const void *from, const void *to) const;
  1191. /* Verifies that a ref to the given object is currently held by the given
  1192. * owner. Only effective in UPB_DEBUG_REFS builds. */
  1193. void CheckRef(const void *owner) const;
  1194. private:
  1195. UPB_DISALLOW_POD_OPS(RefCounted, upb::RefCounted)
  1196. #else
  1197. struct upb_refcounted {
  1198. #endif
  1199. /* TODO(haberman): move the actual structure definition to structdefs.int.h.
  1200. * The only reason they are here is because inline functions need to see the
  1201. * definition of upb_handlers, which needs to see this definition. But we
  1202. * can change the upb_handlers inline functions to deal in raw offsets
  1203. * instead.
  1204. */
  1205. /* A single reference count shared by all objects in the group. */
  1206. uint32_t *group;
  1207. /* A singly-linked list of all objects in the group. */
  1208. upb_refcounted *next;
  1209. /* Table of function pointers for this type. */
  1210. const struct upb_refcounted_vtbl *vtbl;
  1211. /* Maintained only when mutable, this tracks the number of refs (but not
  1212. * ref2's) to this object. *group should be the sum of all individual_count
  1213. * in the group. */
  1214. uint32_t individual_count;
  1215. bool is_frozen;
  1216. #ifdef UPB_DEBUG_REFS
  1217. upb_inttable *refs; /* Maps owner -> trackedref for incoming refs. */
  1218. upb_inttable *ref2s; /* Set of targets for outgoing ref2s. */
  1219. #endif
  1220. };
  1221. #ifdef UPB_DEBUG_REFS
  1222. extern upb_alloc upb_alloc_debugrefs;
  1223. #define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \
  1224. {&static_refcount, NULL, vtbl, 0, true, refs, ref2s}
  1225. #else
  1226. #define UPB_REFCOUNT_INIT(vtbl, refs, ref2s) \
  1227. {&static_refcount, NULL, vtbl, 0, true}
  1228. #endif
  1229. UPB_BEGIN_EXTERN_C
  1230. /* It is better to use tracked refs when possible, for the extra debugging
  1231. * capability. But if this is not possible (because you don't have easy access
  1232. * to a stable pointer value that is associated with the ref), you can pass
  1233. * UPB_UNTRACKED_REF instead. */
  1234. extern const void *UPB_UNTRACKED_REF;
  1235. /* Native C API. */
  1236. bool upb_refcounted_isfrozen(const upb_refcounted *r);
  1237. void upb_refcounted_ref(const upb_refcounted *r, const void *owner);
  1238. void upb_refcounted_unref(const upb_refcounted *r, const void *owner);
  1239. void upb_refcounted_donateref(
  1240. const upb_refcounted *r, const void *from, const void *to);
  1241. void upb_refcounted_checkref(const upb_refcounted *r, const void *owner);
  1242. #define UPB_REFCOUNTED_CMETHODS(type, upcastfunc) \
  1243. UPB_INLINE bool type ## _isfrozen(const type *v) { \
  1244. return upb_refcounted_isfrozen(upcastfunc(v)); \
  1245. } \
  1246. UPB_INLINE void type ## _ref(const type *v, const void *owner) { \
  1247. upb_refcounted_ref(upcastfunc(v), owner); \
  1248. } \
  1249. UPB_INLINE void type ## _unref(const type *v, const void *owner) { \
  1250. upb_refcounted_unref(upcastfunc(v), owner); \
  1251. } \
  1252. UPB_INLINE void type ## _donateref(const type *v, const void *from, const void *to) { \
  1253. upb_refcounted_donateref(upcastfunc(v), from, to); \
  1254. } \
  1255. UPB_INLINE void type ## _checkref(const type *v, const void *owner) { \
  1256. upb_refcounted_checkref(upcastfunc(v), owner); \
  1257. }
  1258. #define UPB_REFCOUNTED_CPPMETHODS \
  1259. bool IsFrozen() const { \
  1260. return upb::upcast_to<const upb::RefCounted>(this)->IsFrozen(); \
  1261. } \
  1262. void Ref(const void *owner) const { \
  1263. return upb::upcast_to<const upb::RefCounted>(this)->Ref(owner); \
  1264. } \
  1265. void Unref(const void *owner) const { \
  1266. return upb::upcast_to<const upb::RefCounted>(this)->Unref(owner); \
  1267. } \
  1268. void DonateRef(const void *from, const void *to) const { \
  1269. return upb::upcast_to<const upb::RefCounted>(this)->DonateRef(from, to); \
  1270. } \
  1271. void CheckRef(const void *owner) const { \
  1272. return upb::upcast_to<const upb::RefCounted>(this)->CheckRef(owner); \
  1273. }
  1274. /* Internal-to-upb Interface **************************************************/
  1275. typedef void upb_refcounted_visit(const upb_refcounted *r,
  1276. const upb_refcounted *subobj,
  1277. void *closure);
  1278. struct upb_refcounted_vtbl {
  1279. /* Must visit all subobjects that are currently ref'd via upb_refcounted_ref2.
  1280. * Must be longjmp()-safe. */
  1281. void (*visit)(const upb_refcounted *r, upb_refcounted_visit *visit, void *c);
  1282. /* Must free the object and release all references to other objects. */
  1283. void (*free)(upb_refcounted *r);
  1284. };
  1285. /* Initializes the refcounted with a single ref for the given owner. Returns
  1286. * false if memory could not be allocated. */
  1287. bool upb_refcounted_init(upb_refcounted *r,
  1288. const struct upb_refcounted_vtbl *vtbl,
  1289. const void *owner);
  1290. /* Adds a ref from one refcounted object to another ("from" must not already
  1291. * own a ref). These refs may be circular; cycles will be collected correctly
  1292. * (if conservatively). These refs do not need to be freed in from's free()
  1293. * function. */
  1294. void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from);
  1295. /* Removes a ref that was acquired from upb_refcounted_ref2(), and collects any
  1296. * object it can. This is only necessary when "from" no longer points to "r",
  1297. * and not from from's "free" function. */
  1298. void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from);
  1299. #define upb_ref2(r, from) \
  1300. upb_refcounted_ref2((const upb_refcounted*)r, (upb_refcounted*)from)
  1301. #define upb_unref2(r, from) \
  1302. upb_refcounted_unref2((const upb_refcounted*)r, (upb_refcounted*)from)
  1303. /* Freezes all mutable object reachable by ref2() refs from the given roots.
  1304. * This will split refcounting groups into precise SCC groups, so that
  1305. * refcounting of frozen objects can be more aggressive. If memory allocation
  1306. * fails, or if more than 2**31 mutable objects are reachable from "roots", or
  1307. * if the maximum depth of the graph exceeds "maxdepth", false is returned and
  1308. * the objects are unchanged.
  1309. *
  1310. * After this operation succeeds, the objects are frozen/const, and may not be
  1311. * used through non-const pointers. In particular, they may not be passed as
  1312. * the second parameter of upb_refcounted_{ref,unref}2(). On the upside, all
  1313. * operations on frozen refcounteds are threadsafe, and objects will be freed
  1314. * at the precise moment that they become unreachable.
  1315. *
  1316. * Caller must own refs on each object in the "roots" list. */
  1317. bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
  1318. int maxdepth);
  1319. /* Shared by all compiled-in refcounted objects. */
  1320. extern uint32_t static_refcount;
  1321. UPB_END_EXTERN_C
  1322. #ifdef __cplusplus
  1323. /* C++ Wrappers. */
  1324. namespace upb {
  1325. inline bool RefCounted::IsFrozen() const {
  1326. return upb_refcounted_isfrozen(this);
  1327. }
  1328. inline void RefCounted::Ref(const void *owner) const {
  1329. upb_refcounted_ref(this, owner);
  1330. }
  1331. inline void RefCounted::Unref(const void *owner) const {
  1332. upb_refcounted_unref(this, owner);
  1333. }
  1334. inline void RefCounted::DonateRef(const void *from, const void *to) const {
  1335. upb_refcounted_donateref(this, from, to);
  1336. }
  1337. inline void RefCounted::CheckRef(const void *owner) const {
  1338. upb_refcounted_checkref(this, owner);
  1339. }
  1340. } /* namespace upb */
  1341. #endif
  1342. /* upb::reffed_ptr ************************************************************/
  1343. #ifdef __cplusplus
  1344. #include <algorithm> /* For std::swap(). */
  1345. /* Provides RAII semantics for upb refcounted objects. Each reffed_ptr owns a
  1346. * ref on whatever object it points to (if any). */
  1347. template <class T> class upb::reffed_ptr {
  1348. public:
  1349. reffed_ptr() : ptr_(NULL) {}
  1350. /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
  1351. template <class U>
  1352. reffed_ptr(U* val, const void* ref_donor = NULL)
  1353. : ptr_(upb::upcast(val)) {
  1354. if (ref_donor) {
  1355. UPB_ASSERT(ptr_);
  1356. ptr_->DonateRef(ref_donor, this);
  1357. } else if (ptr_) {
  1358. ptr_->Ref(this);
  1359. }
  1360. }
  1361. template <class U>
  1362. reffed_ptr(const reffed_ptr<U>& other)
  1363. : ptr_(upb::upcast(other.get())) {
  1364. if (ptr_) ptr_->Ref(this);
  1365. }
  1366. reffed_ptr(const reffed_ptr& other)
  1367. : ptr_(upb::upcast(other.get())) {
  1368. if (ptr_) ptr_->Ref(this);
  1369. }
  1370. ~reffed_ptr() { if (ptr_) ptr_->Unref(this); }
  1371. template <class U>
  1372. reffed_ptr& operator=(const reffed_ptr<U>& other) {
  1373. reset(other.get());
  1374. return *this;
  1375. }
  1376. reffed_ptr& operator=(const reffed_ptr& other) {
  1377. reset(other.get());
  1378. return *this;
  1379. }
  1380. /* TODO(haberman): add C++11 move construction/assignment for greater
  1381. * efficiency. */
  1382. void swap(reffed_ptr& other) {
  1383. if (ptr_ == other.ptr_) {
  1384. return;
  1385. }
  1386. if (ptr_) ptr_->DonateRef(this, &other);
  1387. if (other.ptr_) other.ptr_->DonateRef(&other, this);
  1388. std::swap(ptr_, other.ptr_);
  1389. }
  1390. T& operator*() const {
  1391. UPB_ASSERT(ptr_);
  1392. return *ptr_;
  1393. }
  1394. T* operator->() const {
  1395. UPB_ASSERT(ptr_);
  1396. return ptr_;
  1397. }
  1398. T* get() const { return ptr_; }
  1399. /* If ref_donor is NULL, takes a new ref, otherwise adopts from ref_donor. */
  1400. template <class U>
  1401. void reset(U* ptr = NULL, const void* ref_donor = NULL) {
  1402. reffed_ptr(ptr, ref_donor).swap(*this);
  1403. }
  1404. template <class U>
  1405. reffed_ptr<U> down_cast() {
  1406. return reffed_ptr<U>(upb::down_cast<U*>(get()));
  1407. }
  1408. template <class U>
  1409. reffed_ptr<U> dyn_cast() {
  1410. return reffed_ptr<U>(upb::dyn_cast<U*>(get()));
  1411. }
  1412. /* Plain release() is unsafe; if we were the only owner, it would leak the
  1413. * object. Instead we provide this: */
  1414. T* ReleaseTo(const void* new_owner) {
  1415. T* ret = NULL;
  1416. ptr_->DonateRef(this, new_owner);
  1417. std::swap(ret, ptr_);
  1418. return ret;
  1419. }
  1420. private:
  1421. T* ptr_;
  1422. };
  1423. #endif /* __cplusplus */
  1424. #endif /* UPB_REFCOUNT_H_ */
  1425. #ifdef __cplusplus
  1426. #include <cstring>
  1427. #include <string>
  1428. #include <vector>
  1429. namespace upb {
  1430. class Def;
  1431. class EnumDef;
  1432. class FieldDef;
  1433. class FileDef;
  1434. class MessageDef;
  1435. class OneofDef;
  1436. class SymbolTable;
  1437. }
  1438. #endif
  1439. UPB_DECLARE_DERIVED_TYPE(upb::Def, upb::RefCounted, upb_def, upb_refcounted)
  1440. UPB_DECLARE_DERIVED_TYPE(upb::OneofDef, upb::RefCounted, upb_oneofdef,
  1441. upb_refcounted)
  1442. UPB_DECLARE_DERIVED_TYPE(upb::FileDef, upb::RefCounted, upb_filedef,
  1443. upb_refcounted)
  1444. UPB_DECLARE_TYPE(upb::SymbolTable, upb_symtab)
  1445. /* The maximum message depth that the type graph can have. This is a resource
  1446. * limit for the C stack since we sometimes need to recursively traverse the
  1447. * graph. Cycles are ok; the traversal will stop when it detects a cycle, but
  1448. * we must hit the cycle before the maximum depth is reached.
  1449. *
  1450. * If having a single static limit is too inflexible, we can add another variant
  1451. * of Def::Freeze that allows specifying this as a parameter. */
  1452. #define UPB_MAX_MESSAGE_DEPTH 64
  1453. /* upb::Def: base class for top-level defs ***********************************/
  1454. /* All the different kind of defs that can be defined at the top-level and put
  1455. * in a SymbolTable or appear in a FileDef::defs() list. This excludes some
  1456. * defs (like oneofs and files). It only includes fields because they can be
  1457. * defined as extensions. */
  1458. typedef enum {
  1459. UPB_DEF_MSG,
  1460. UPB_DEF_FIELD,
  1461. UPB_DEF_ENUM,
  1462. UPB_DEF_SERVICE, /* Not yet implemented. */
  1463. UPB_DEF_ANY = -1 /* Wildcard for upb_symtab_get*() */
  1464. } upb_deftype_t;
  1465. #ifdef __cplusplus
  1466. /* The base class of all defs. Its base is upb::RefCounted (use upb::upcast()
  1467. * to convert). */
  1468. class upb::Def {
  1469. public:
  1470. typedef upb_deftype_t Type;
  1471. /* upb::RefCounted methods like Ref()/Unref(). */
  1472. UPB_REFCOUNTED_CPPMETHODS
  1473. Type def_type() const;
  1474. /* "fullname" is the def's fully-qualified name (eg. foo.bar.Message). */
  1475. const char *full_name() const;
  1476. /* The final part of a def's name (eg. Message). */
  1477. const char *name() const;
  1478. /* The def must be mutable. Caller retains ownership of fullname. Defs are
  1479. * not required to have a name; if a def has no name when it is frozen, it
  1480. * will remain an anonymous def. On failure, returns false and details in "s"
  1481. * if non-NULL. */
  1482. bool set_full_name(const char* fullname, upb::Status* s);
  1483. bool set_full_name(const std::string &fullname, upb::Status* s);
  1484. /* The file in which this def appears. It is not necessary to add a def to a
  1485. * file (and consequently the accessor may return NULL). Set this by calling
  1486. * file->Add(def). */
  1487. FileDef* file() const;
  1488. /* Freezes the given defs; this validates all constraints and marks the defs
  1489. * as frozen (read-only). "defs" may not contain any fielddefs, but fields
  1490. * of any msgdefs will be frozen.
  1491. *
  1492. * Symbolic references to sub-types and enum defaults must have already been
  1493. * resolved. Any mutable defs reachable from any of "defs" must also be in
  1494. * the list; more formally, "defs" must be a transitive closure of mutable
  1495. * defs.
  1496. *
  1497. * After this operation succeeds, the finalized defs must only be accessed
  1498. * through a const pointer! */
  1499. static bool Freeze(Def* const* defs, size_t n, Status* status);
  1500. static bool Freeze(const std::vector<Def*>& defs, Status* status);
  1501. private:
  1502. UPB_DISALLOW_POD_OPS(Def, upb::Def)
  1503. };
  1504. #endif /* __cplusplus */
  1505. UPB_BEGIN_EXTERN_C
  1506. /* Include upb_refcounted methods like upb_def_ref()/upb_def_unref(). */
  1507. UPB_REFCOUNTED_CMETHODS(upb_def, upb_def_upcast)
  1508. upb_deftype_t upb_def_type(const upb_def *d);
  1509. const char *upb_def_fullname(const upb_def *d);
  1510. const char *upb_def_name(const upb_def *d);
  1511. const upb_filedef *upb_def_file(const upb_def *d);
  1512. bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s);
  1513. bool upb_def_freeze(upb_def *const *defs, size_t n, upb_status *s);
  1514. /* Temporary API: for internal use only. */
  1515. bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s);
  1516. UPB_END_EXTERN_C
  1517. /* upb::Def casts *************************************************************/
  1518. #ifdef __cplusplus
  1519. #define UPB_CPP_CASTS(cname, cpptype) \
  1520. namespace upb { \
  1521. template <> \
  1522. inline cpptype *down_cast<cpptype *, Def>(Def * def) { \
  1523. return upb_downcast_##cname##_mutable(def); \
  1524. } \
  1525. template <> \
  1526. inline cpptype *dyn_cast<cpptype *, Def>(Def * def) { \
  1527. return upb_dyncast_##cname##_mutable(def); \
  1528. } \
  1529. template <> \
  1530. inline const cpptype *down_cast<const cpptype *, const Def>( \
  1531. const Def *def) { \
  1532. return upb_downcast_##cname(def); \
  1533. } \
  1534. template <> \
  1535. inline const cpptype *dyn_cast<const cpptype *, const Def>(const Def *def) { \
  1536. return upb_dyncast_##cname(def); \
  1537. } \
  1538. template <> \
  1539. inline const cpptype *down_cast<const cpptype *, Def>(Def * def) { \
  1540. return upb_downcast_##cname(def); \
  1541. } \
  1542. template <> \
  1543. inline const cpptype *dyn_cast<const cpptype *, Def>(Def * def) { \
  1544. return upb_dyncast_##cname(def); \
  1545. } \
  1546. } /* namespace upb */
  1547. #else
  1548. #define UPB_CPP_CASTS(cname, cpptype)
  1549. #endif /* __cplusplus */
  1550. /* Dynamic casts, for determining if a def is of a particular type at runtime.
  1551. * Downcasts, for when some wants to assert that a def is of a particular type.
  1552. * These are only checked if we are building debug. */
  1553. #define UPB_DEF_CASTS(lower, upper, cpptype) \
  1554. UPB_INLINE const upb_##lower *upb_dyncast_##lower(const upb_def *def) { \
  1555. if (upb_def_type(def) != UPB_DEF_##upper) return NULL; \
  1556. return (upb_##lower *)def; \
  1557. } \
  1558. UPB_INLINE const upb_##lower *upb_downcast_##lower(const upb_def *def) { \
  1559. UPB_ASSERT(upb_def_type(def) == UPB_DEF_##upper); \
  1560. return (const upb_##lower *)def; \
  1561. } \
  1562. UPB_INLINE upb_##lower *upb_dyncast_##lower##_mutable(upb_def *def) { \
  1563. return (upb_##lower *)upb_dyncast_##lower(def); \
  1564. } \
  1565. UPB_INLINE upb_##lower *upb_downcast_##lower##_mutable(upb_def *def) { \
  1566. return (upb_##lower *)upb_downcast_##lower(def); \
  1567. } \
  1568. UPB_CPP_CASTS(lower, cpptype)
  1569. #define UPB_DEFINE_DEF(cppname, lower, upper, cppmethods, members) \
  1570. UPB_DEFINE_CLASS2(cppname, upb::Def, upb::RefCounted, cppmethods, \
  1571. members) \
  1572. UPB_DEF_CASTS(lower, upper, cppname)
  1573. #define UPB_DECLARE_DEF_TYPE(cppname, lower, upper) \
  1574. UPB_DECLARE_DERIVED_TYPE2(cppname, upb::Def, upb::RefCounted, \
  1575. upb_ ## lower, upb_def, upb_refcounted) \
  1576. UPB_DEF_CASTS(lower, upper, cppname)
  1577. UPB_DECLARE_DEF_TYPE(upb::FieldDef, fielddef, FIELD)
  1578. UPB_DECLARE_DEF_TYPE(upb::MessageDef, msgdef, MSG)
  1579. UPB_DECLARE_DEF_TYPE(upb::EnumDef, enumdef, ENUM)
  1580. #undef UPB_DECLARE_DEF_TYPE
  1581. #undef UPB_DEF_CASTS
  1582. #undef UPB_CPP_CASTS
  1583. /* upb::FieldDef **************************************************************/
  1584. /* The types a field can have. Note that this list is not identical to the
  1585. * types defined in descriptor.proto, which gives INT32 and SINT32 separate
  1586. * types (we distinguish the two with the "integer encoding" enum below). */
  1587. typedef enum {
  1588. /* Types stored in 1 byte. */
  1589. UPB_TYPE_BOOL = 1,
  1590. /* Types stored in 4 bytes. */
  1591. UPB_TYPE_FLOAT = 2,
  1592. UPB_TYPE_INT32 = 3,
  1593. UPB_TYPE_UINT32 = 4,
  1594. UPB_TYPE_ENUM = 5, /* Enum values are int32. */
  1595. /* Types stored as pointers (probably 4 or 8 bytes). */
  1596. UPB_TYPE_STRING = 6,
  1597. UPB_TYPE_BYTES = 7,
  1598. UPB_TYPE_MESSAGE = 8,
  1599. /* Types stored as 8 bytes. */
  1600. UPB_TYPE_DOUBLE = 9,
  1601. UPB_TYPE_INT64 = 10,
  1602. UPB_TYPE_UINT64 = 11
  1603. } upb_fieldtype_t;
  1604. /* The repeated-ness of each field; this matches descriptor.proto. */
  1605. typedef enum {
  1606. UPB_LABEL_OPTIONAL = 1,
  1607. UPB_LABEL_REQUIRED = 2,
  1608. UPB_LABEL_REPEATED = 3
  1609. } upb_label_t;
  1610. /* How integers should be encoded in serializations that offer multiple
  1611. * integer encoding methods. */
  1612. typedef enum {
  1613. UPB_INTFMT_VARIABLE = 1,
  1614. UPB_INTFMT_FIXED = 2,
  1615. UPB_INTFMT_ZIGZAG = 3 /* Only for signed types (INT32/INT64). */
  1616. } upb_intfmt_t;
  1617. /* Descriptor types, as defined in descriptor.proto. */
  1618. typedef enum {
  1619. UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
  1620. UPB_DESCRIPTOR_TYPE_FLOAT = 2,
  1621. UPB_DESCRIPTOR_TYPE_INT64 = 3,
  1622. UPB_DESCRIPTOR_TYPE_UINT64 = 4,
  1623. UPB_DESCRIPTOR_TYPE_INT32 = 5,
  1624. UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
  1625. UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
  1626. UPB_DESCRIPTOR_TYPE_BOOL = 8,
  1627. UPB_DESCRIPTOR_TYPE_STRING = 9,
  1628. UPB_DESCRIPTOR_TYPE_GROUP = 10,
  1629. UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
  1630. UPB_DESCRIPTOR_TYPE_BYTES = 12,
  1631. UPB_DESCRIPTOR_TYPE_UINT32 = 13,
  1632. UPB_DESCRIPTOR_TYPE_ENUM = 14,
  1633. UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
  1634. UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
  1635. UPB_DESCRIPTOR_TYPE_SINT32 = 17,
  1636. UPB_DESCRIPTOR_TYPE_SINT64 = 18
  1637. } upb_descriptortype_t;
  1638. typedef enum {
  1639. UPB_SYNTAX_PROTO2 = 2,
  1640. UPB_SYNTAX_PROTO3 = 3
  1641. } upb_syntax_t;
  1642. /* All the different kind of well known type messages. For simplicity of check,
  1643. * number wrappers and string wrappers are grouped together. Make sure the
  1644. * order and merber of these groups are not changed.
  1645. */
  1646. typedef enum {
  1647. UPB_WELLKNOWN_UNSPECIFIED,
  1648. UPB_WELLKNOWN_ANY,
  1649. UPB_WELLKNOWN_FIELDMASK,
  1650. UPB_WELLKNOWN_DURATION,
  1651. UPB_WELLKNOWN_TIMESTAMP,
  1652. /* number wrappers */
  1653. UPB_WELLKNOWN_DOUBLEVALUE,
  1654. UPB_WELLKNOWN_FLOATVALUE,
  1655. UPB_WELLKNOWN_INT64VALUE,
  1656. UPB_WELLKNOWN_UINT64VALUE,
  1657. UPB_WELLKNOWN_INT32VALUE,
  1658. UPB_WELLKNOWN_UINT32VALUE,
  1659. /* string wrappers */
  1660. UPB_WELLKNOWN_STRINGVALUE,
  1661. UPB_WELLKNOWN_BYTESVALUE,
  1662. UPB_WELLKNOWN_BOOLVALUE,
  1663. UPB_WELLKNOWN_VALUE,
  1664. UPB_WELLKNOWN_LISTVALUE,
  1665. UPB_WELLKNOWN_STRUCT
  1666. } upb_wellknowntype_t;
  1667. /* Maps descriptor type -> upb field type. */
  1668. extern const uint8_t upb_desctype_to_fieldtype[];
  1669. /* Maximum field number allowed for FieldDefs. This is an inherent limit of the
  1670. * protobuf wire format. */
  1671. #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
  1672. #ifdef __cplusplus
  1673. /* A upb_fielddef describes a single field in a message. It is most often
  1674. * found as a part of a upb_msgdef, but can also stand alone to represent
  1675. * an extension.
  1676. *
  1677. * Its base class is upb::Def (use upb::upcast() to convert). */
  1678. class upb::FieldDef {
  1679. public:
  1680. typedef upb_fieldtype_t Type;
  1681. typedef upb_label_t Label;
  1682. typedef upb_intfmt_t IntegerFormat;
  1683. typedef upb_descriptortype_t DescriptorType;
  1684. /* These return true if the given value is a valid member of the enumeration. */
  1685. static bool CheckType(int32_t val);
  1686. static bool CheckLabel(int32_t val);
  1687. static bool CheckDescriptorType(int32_t val);
  1688. static bool CheckIntegerFormat(int32_t val);
  1689. /* These convert to the given enumeration; they require that the value is
  1690. * valid. */
  1691. static Type ConvertType(int32_t val);
  1692. static Label ConvertLabel(int32_t val);
  1693. static DescriptorType ConvertDescriptorType(int32_t val);
  1694. static IntegerFormat ConvertIntegerFormat(int32_t val);
  1695. /* Returns NULL if memory allocation failed. */
  1696. static reffed_ptr<FieldDef> New();
  1697. /* upb::RefCounted methods like Ref()/Unref(). */
  1698. UPB_REFCOUNTED_CPPMETHODS
  1699. /* Functionality from upb::Def. */
  1700. const char* full_name() const;
  1701. bool type_is_set() const; /* set_[descriptor_]type() has been called? */
  1702. Type type() const; /* Requires that type_is_set() == true. */
  1703. Label label() const; /* Defaults to UPB_LABEL_OPTIONAL. */
  1704. const char* name() const; /* NULL if uninitialized. */
  1705. uint32_t number() const; /* Returns 0 if uninitialized. */
  1706. bool is_extension() const;
  1707. /* Copies the JSON name for this field into the given buffer. Returns the
  1708. * actual size of the JSON name, including the NULL terminator. If the
  1709. * return value is 0, the JSON name is unset. If the return value is
  1710. * greater than len, the JSON name was truncated. The buffer is always
  1711. * NULL-terminated if len > 0.
  1712. *
  1713. * The JSON name always defaults to a camelCased version of the regular
  1714. * name. However if the regular name is unset, the JSON name will be unset
  1715. * also.
  1716. */
  1717. size_t GetJsonName(char* buf, size_t len) const;
  1718. /* Convenience version of the above function which copies the JSON name
  1719. * into the given string, returning false if the name is not set. */
  1720. template <class T>
  1721. bool GetJsonName(T* str) {
  1722. str->resize(GetJsonName(NULL, 0));
  1723. GetJsonName(&(*str)[0], str->size());
  1724. return str->size() > 0;
  1725. }
  1726. /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
  1727. * indicates whether this field should have lazy parsing handlers that yield
  1728. * the unparsed string for the submessage.
  1729. *
  1730. * TODO(haberman): I think we want to move this into a FieldOptions container
  1731. * when we add support for custom options (the FieldOptions struct will
  1732. * contain both regular FieldOptions like "lazy" *and* custom options). */
  1733. bool lazy() const;
  1734. /* For non-string, non-submessage fields, this indicates whether binary
  1735. * protobufs are encoded in packed or non-packed format.
  1736. *
  1737. * TODO(haberman): see note above about putting options like this into a
  1738. * FieldOptions container. */
  1739. bool packed() const;
  1740. /* An integer that can be used as an index into an array of fields for
  1741. * whatever message this field belongs to. Guaranteed to be less than
  1742. * f->containing_type()->field_count(). May only be accessed once the def has
  1743. * been finalized. */
  1744. uint32_t index() const;
  1745. /* The MessageDef to which this field belongs.
  1746. *
  1747. * If this field has been added to a MessageDef, that message can be retrieved
  1748. * directly (this is always the case for frozen FieldDefs).
  1749. *
  1750. * If the field has not yet been added to a MessageDef, you can set the name
  1751. * of the containing type symbolically instead. This is mostly useful for
  1752. * extensions, where the extension is declared separately from the message. */
  1753. const MessageDef* containing_type() const;
  1754. const char* containing_type_name();
  1755. /* The OneofDef to which this field belongs, or NULL if this field is not part
  1756. * of a oneof. */
  1757. const OneofDef* containing_oneof() const;
  1758. /* The field's type according to the enum in descriptor.proto. This is not
  1759. * the same as UPB_TYPE_*, because it distinguishes between (for example)
  1760. * INT32 and SINT32, whereas our "type" enum does not. This return of
  1761. * descriptor_type() is a function of type(), integer_format(), and
  1762. * is_tag_delimited(). Likewise set_descriptor_type() sets all three
  1763. * appropriately. */
  1764. DescriptorType descriptor_type() const;
  1765. /* Convenient field type tests. */
  1766. bool IsSubMessage() const;
  1767. bool IsString() const;
  1768. bool IsSequence() const;
  1769. bool IsPrimitive() const;
  1770. bool IsMap() const;
  1771. /* Returns whether this field explicitly represents presence.
  1772. *
  1773. * For proto2 messages: Returns true for any scalar (non-repeated) field.
  1774. * For proto3 messages: Returns true for scalar submessage or oneof fields. */
  1775. bool HasPresence() const;
  1776. /* How integers are encoded. Only meaningful for integer types.
  1777. * Defaults to UPB_INTFMT_VARIABLE, and is reset when "type" changes. */
  1778. IntegerFormat integer_format() const;
  1779. /* Whether a submessage field is tag-delimited or not (if false, then
  1780. * length-delimited). May only be set when type() == UPB_TYPE_MESSAGE. */
  1781. bool is_tag_delimited() const;
  1782. /* Returns the non-string default value for this fielddef, which may either
  1783. * be something the client set explicitly or the "default default" (0 for
  1784. * numbers, empty for strings). The field's type indicates the type of the
  1785. * returned value, except for enum fields that are still mutable.
  1786. *
  1787. * Requires that the given function matches the field's current type. */
  1788. int64_t default_int64() const;
  1789. int32_t default_int32() const;
  1790. uint64_t default_uint64() const;
  1791. uint32_t default_uint32() const;
  1792. bool default_bool() const;
  1793. float default_float() const;
  1794. double default_double() const;
  1795. /* The resulting string is always NULL-terminated. If non-NULL, the length
  1796. * will be stored in *len. */
  1797. const char *default_string(size_t* len) const;
  1798. /* For frozen UPB_TYPE_ENUM fields, enum defaults can always be read as either
  1799. * string or int32, and both of these methods will always return true.
  1800. *
  1801. * For mutable UPB_TYPE_ENUM fields, the story is a bit more complicated.
  1802. * Enum defaults are unusual. They can be specified either as string or int32,
  1803. * but to be valid the enum must have that value as a member. And if no
  1804. * default is specified, the "default default" comes from the EnumDef.
  1805. *
  1806. * We allow reading the default as either an int32 or a string, but only if
  1807. * we have a meaningful value to report. We have a meaningful value if it was
  1808. * set explicitly, or if we could get the "default default" from the EnumDef.
  1809. * Also if you explicitly set the name and we find the number in the EnumDef */
  1810. bool EnumHasStringDefault() const;
  1811. bool EnumHasInt32Default() const;
  1812. /* Submessage and enum fields must reference a "subdef", which is the
  1813. * upb::MessageDef or upb::EnumDef that defines their type. Note that when
  1814. * the FieldDef is mutable it may not have a subdef *yet*, but this function
  1815. * still returns true to indicate that the field's type requires a subdef. */
  1816. bool HasSubDef() const;
  1817. /* Returns the enum or submessage def for this field, if any. The field's
  1818. * type must match (ie. you may only call enum_subdef() for fields where
  1819. * type() == UPB_TYPE_ENUM). Returns NULL if the subdef has not been set or
  1820. * is currently set symbolically. */
  1821. const EnumDef* enum_subdef() const;
  1822. const MessageDef* message_subdef() const;
  1823. /* Returns the generic subdef for this field. Requires that HasSubDef() (ie.
  1824. * only works for UPB_TYPE_ENUM and UPB_TYPE_MESSAGE fields). */
  1825. const Def* subdef() const;
  1826. /* Returns the symbolic name of the subdef. If the subdef is currently set
  1827. * unresolved (ie. set symbolically) returns the symbolic name. If it has
  1828. * been resolved to a specific subdef, returns the name from that subdef. */
  1829. const char* subdef_name() const;
  1830. /* Setters (non-const methods), only valid for mutable FieldDefs! ***********/
  1831. bool set_full_name(const char* fullname, upb::Status* s);
  1832. bool set_full_name(const std::string& fullname, upb::Status* s);
  1833. /* This may only be called if containing_type() == NULL (ie. the field has not
  1834. * been added to a message yet). */
  1835. bool set_containing_type_name(const char *name, Status* status);
  1836. bool set_containing_type_name(const std::string& name, Status* status);
  1837. /* Defaults to false. When we freeze, we ensure that this can only be true
  1838. * for length-delimited message fields. Prior to freezing this can be true or
  1839. * false with no restrictions. */
  1840. void set_lazy(bool lazy);
  1841. /* Defaults to true. Sets whether this field is encoded in packed format. */
  1842. void set_packed(bool packed);
  1843. /* "type" or "descriptor_type" MUST be set explicitly before the fielddef is
  1844. * finalized. These setters require that the enum value is valid; if the
  1845. * value did not come directly from an enum constant, the caller should
  1846. * validate it first with the functions above (CheckFieldType(), etc). */
  1847. void set_type(Type type);
  1848. void set_label(Label label);
  1849. void set_descriptor_type(DescriptorType type);
  1850. void set_is_extension(bool is_extension);
  1851. /* "number" and "name" must be set before the FieldDef is added to a
  1852. * MessageDef, and may not be set after that.
  1853. *
  1854. * "name" is the same as full_name()/set_full_name(), but since fielddefs
  1855. * most often use simple, non-qualified names, we provide this accessor
  1856. * also. Generally only extensions will want to think of this name as
  1857. * fully-qualified. */
  1858. bool set_number(uint32_t number, upb::Status* s);
  1859. bool set_name(const char* name, upb::Status* s);
  1860. bool set_name(const std::string& name, upb::Status* s);
  1861. /* Sets the JSON name to the given string. */
  1862. /* TODO(haberman): implement. Right now only default json_name (camelCase)
  1863. * is supported. */
  1864. bool set_json_name(const char* json_name, upb::Status* s);
  1865. bool set_json_name(const std::string& name, upb::Status* s);
  1866. /* Clears the JSON name. This will make it revert to its default, which is
  1867. * a camelCased version of the regular field name. */
  1868. void clear_json_name();
  1869. void set_integer_format(IntegerFormat format);
  1870. bool set_tag_delimited(bool tag_delimited, upb::Status* s);
  1871. /* Sets default value for the field. The call must exactly match the type
  1872. * of the field. Enum fields may use either setint32 or setstring to set
  1873. * the default numerically or symbolically, respectively, but symbolic
  1874. * defaults must be resolved before finalizing (see ResolveEnumDefault()).
  1875. *
  1876. * Changing the type of a field will reset its default. */
  1877. void set_default_int64(int64_t val);
  1878. void set_default_int32(int32_t val);
  1879. void set_default_uint64(uint64_t val);
  1880. void set_default_uint32(uint32_t val);
  1881. void set_default_bool(bool val);
  1882. void set_default_float(float val);
  1883. void set_default_double(double val);
  1884. bool set_default_string(const void *str, size_t len, Status *s);
  1885. bool set_default_string(const std::string &str, Status *s);
  1886. void set_default_cstr(const char *str, Status *s);
  1887. /* Before a fielddef is frozen, its subdef may be set either directly (with a
  1888. * upb::Def*) or symbolically. Symbolic refs must be resolved before the
  1889. * containing msgdef can be frozen (see upb_resolve() above). upb always
  1890. * guarantees that any def reachable from a live def will also be kept alive.
  1891. *
  1892. * Both methods require that upb_hassubdef(f) (so the type must be set prior
  1893. * to calling these methods). Returns false if this is not the case, or if
  1894. * the given subdef is not of the correct type. The subdef is reset if the
  1895. * field's type is changed. The subdef can be set to NULL to clear it. */
  1896. bool set_subdef(const Def* subdef, Status* s);
  1897. bool set_enum_subdef(const EnumDef* subdef, Status* s);
  1898. bool set_message_subdef(const MessageDef* subdef, Status* s);
  1899. bool set_subdef_name(const char* name, Status* s);
  1900. bool set_subdef_name(const std::string &name, Status* s);
  1901. private:
  1902. UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef)
  1903. };
  1904. # endif /* defined(__cplusplus) */
  1905. UPB_BEGIN_EXTERN_C
  1906. /* Native C API. */
  1907. upb_fielddef *upb_fielddef_new(const void *owner);
  1908. /* Include upb_refcounted methods like upb_fielddef_ref(). */
  1909. UPB_REFCOUNTED_CMETHODS(upb_fielddef, upb_fielddef_upcast2)
  1910. /* Methods from upb_def. */
  1911. const char *upb_fielddef_fullname(const upb_fielddef *f);
  1912. bool upb_fielddef_setfullname(upb_fielddef *f, const char *fullname,
  1913. upb_status *s);
  1914. bool upb_fielddef_typeisset(const upb_fielddef *f);
  1915. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
  1916. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
  1917. upb_label_t upb_fielddef_label(const upb_fielddef *f);
  1918. uint32_t upb_fielddef_number(const upb_fielddef *f);
  1919. const char *upb_fielddef_name(const upb_fielddef *f);
  1920. bool upb_fielddef_isextension(const upb_fielddef *f);
  1921. bool upb_fielddef_lazy(const upb_fielddef *f);
  1922. bool upb_fielddef_packed(const upb_fielddef *f);
  1923. size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
  1924. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
  1925. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
  1926. upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f);
  1927. const char *upb_fielddef_containingtypename(upb_fielddef *f);
  1928. upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f);
  1929. uint32_t upb_fielddef_index(const upb_fielddef *f);
  1930. bool upb_fielddef_istagdelim(const upb_fielddef *f);
  1931. bool upb_fielddef_issubmsg(const upb_fielddef *f);
  1932. bool upb_fielddef_isstring(const upb_fielddef *f);
  1933. bool upb_fielddef_isseq(const upb_fielddef *f);
  1934. bool upb_fielddef_isprimitive(const upb_fielddef *f);
  1935. bool upb_fielddef_ismap(const upb_fielddef *f);
  1936. bool upb_fielddef_haspresence(const upb_fielddef *f);
  1937. int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
  1938. int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
  1939. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
  1940. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
  1941. bool upb_fielddef_defaultbool(const upb_fielddef *f);
  1942. float upb_fielddef_defaultfloat(const upb_fielddef *f);
  1943. double upb_fielddef_defaultdouble(const upb_fielddef *f);
  1944. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
  1945. bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f);
  1946. bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f);
  1947. bool upb_fielddef_hassubdef(const upb_fielddef *f);
  1948. const upb_def *upb_fielddef_subdef(const upb_fielddef *f);
  1949. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
  1950. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
  1951. const char *upb_fielddef_subdefname(const upb_fielddef *f);
  1952. void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type);
  1953. void upb_fielddef_setdescriptortype(upb_fielddef *f, int type);
  1954. void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label);
  1955. bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s);
  1956. bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s);
  1957. bool upb_fielddef_setjsonname(upb_fielddef *f, const char *name, upb_status *s);
  1958. bool upb_fielddef_clearjsonname(upb_fielddef *f);
  1959. bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
  1960. upb_status *s);
  1961. void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension);
  1962. void upb_fielddef_setlazy(upb_fielddef *f, bool lazy);
  1963. void upb_fielddef_setpacked(upb_fielddef *f, bool packed);
  1964. void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt);
  1965. void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim);
  1966. void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t val);
  1967. void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t val);
  1968. void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t val);
  1969. void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t val);
  1970. void upb_fielddef_setdefaultbool(upb_fielddef *f, bool val);
  1971. void upb_fielddef_setdefaultfloat(upb_fielddef *f, float val);
  1972. void upb_fielddef_setdefaultdouble(upb_fielddef *f, double val);
  1973. bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
  1974. upb_status *s);
  1975. void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
  1976. upb_status *s);
  1977. bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
  1978. upb_status *s);
  1979. bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
  1980. upb_status *s);
  1981. bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
  1982. upb_status *s);
  1983. bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
  1984. upb_status *s);
  1985. bool upb_fielddef_checklabel(int32_t label);
  1986. bool upb_fielddef_checktype(int32_t type);
  1987. bool upb_fielddef_checkdescriptortype(int32_t type);
  1988. bool upb_fielddef_checkintfmt(int32_t fmt);
  1989. UPB_END_EXTERN_C
  1990. /* upb::MessageDef ************************************************************/
  1991. typedef upb_inttable_iter upb_msg_field_iter;
  1992. typedef upb_strtable_iter upb_msg_oneof_iter;
  1993. /* Well-known field tag numbers for map-entry messages. */
  1994. #define UPB_MAPENTRY_KEY 1
  1995. #define UPB_MAPENTRY_VALUE 2
  1996. /* Well-known field tag numbers for Any messages. */
  1997. #define UPB_ANY_TYPE 1
  1998. #define UPB_ANY_VALUE 2
  1999. /* Well-known field tag numbers for timestamp messages. */
  2000. #define UPB_DURATION_SECONDS 1
  2001. #define UPB_DURATION_NANOS 2
  2002. /* Well-known field tag numbers for duration messages. */
  2003. #define UPB_TIMESTAMP_SECONDS 1
  2004. #define UPB_TIMESTAMP_NANOS 2
  2005. #ifdef __cplusplus
  2006. /* Structure that describes a single .proto message type.
  2007. *
  2008. * Its base class is upb::Def (use upb::upcast() to convert). */
  2009. class upb::MessageDef {
  2010. public:
  2011. /* Returns NULL if memory allocation failed. */
  2012. static reffed_ptr<MessageDef> New();
  2013. /* upb::RefCounted methods like Ref()/Unref(). */
  2014. UPB_REFCOUNTED_CPPMETHODS
  2015. /* Functionality from upb::Def. */
  2016. const char* full_name() const;
  2017. const char* name() const;
  2018. bool set_full_name(const char* fullname, Status* s);
  2019. bool set_full_name(const std::string& fullname, Status* s);
  2020. /* Call to freeze this MessageDef.
  2021. * WARNING: this will fail if this message has any unfrozen submessages!
  2022. * Messages with cycles must be frozen as a batch using upb::Def::Freeze(). */
  2023. bool Freeze(Status* s);
  2024. /* The number of fields that belong to the MessageDef. */
  2025. int field_count() const;
  2026. /* The number of oneofs that belong to the MessageDef. */
  2027. int oneof_count() const;
  2028. /* Adds a field (upb_fielddef object) to a msgdef. Requires that the msgdef
  2029. * and the fielddefs are mutable. The fielddef's name and number must be
  2030. * set, and the message may not already contain any field with this name or
  2031. * number, and this fielddef may not be part of another message. In error
  2032. * cases false is returned and the msgdef is unchanged.
  2033. *
  2034. * If the given field is part of a oneof, this call succeeds if and only if
  2035. * that oneof is already part of this msgdef. (Note that adding a oneof to a
  2036. * msgdef automatically adds all of its fields to the msgdef at the time that
  2037. * the oneof is added, so it is usually more idiomatic to add the oneof's
  2038. * fields first then add the oneof to the msgdef. This case is supported for
  2039. * convenience.)
  2040. *
  2041. * If |f| is already part of this MessageDef, this method performs no action
  2042. * and returns true (success). Thus, this method is idempotent. */
  2043. bool AddField(FieldDef* f, Status* s);
  2044. bool AddField(const reffed_ptr<FieldDef>& f, Status* s);
  2045. /* Adds a oneof (upb_oneofdef object) to a msgdef. Requires that the msgdef,
  2046. * oneof, and any fielddefs are mutable, that the fielddefs contained in the
  2047. * oneof do not have any name or number conflicts with existing fields in the
  2048. * msgdef, and that the oneof's name is unique among all oneofs in the msgdef.
  2049. * If the oneof is added successfully, all of its fields will be added
  2050. * directly to the msgdef as well. In error cases, false is returned and the
  2051. * msgdef is unchanged. */
  2052. bool AddOneof(OneofDef* o, Status* s);
  2053. bool AddOneof(const reffed_ptr<OneofDef>& o, Status* s);
  2054. upb_syntax_t syntax() const;
  2055. /* Returns false if we don't support this syntax value. */
  2056. bool set_syntax(upb_syntax_t syntax);
  2057. /* Set this to false to indicate that primitive fields should not have
  2058. * explicit presence information associated with them. This will affect all
  2059. * fields added to this message. Defaults to true. */
  2060. void SetPrimitivesHavePresence(bool have_presence);
  2061. /* These return NULL if the field is not found. */
  2062. FieldDef* FindFieldByNumber(uint32_t number);
  2063. FieldDef* FindFieldByName(const char *name, size_t len);
  2064. const FieldDef* FindFieldByNumber(uint32_t number) const;
  2065. const FieldDef* FindFieldByName(const char* name, size_t len) const;
  2066. FieldDef* FindFieldByName(const char *name) {
  2067. return FindFieldByName(name, strlen(name));
  2068. }
  2069. const FieldDef* FindFieldByName(const char *name) const {
  2070. return FindFieldByName(name, strlen(name));
  2071. }
  2072. template <class T>
  2073. FieldDef* FindFieldByName(const T& str) {
  2074. return FindFieldByName(str.c_str(), str.size());
  2075. }
  2076. template <class T>
  2077. const FieldDef* FindFieldByName(const T& str) const {
  2078. return FindFieldByName(str.c_str(), str.size());
  2079. }
  2080. OneofDef* FindOneofByName(const char* name, size_t len);
  2081. const OneofDef* FindOneofByName(const char* name, size_t len) const;
  2082. OneofDef* FindOneofByName(const char* name) {
  2083. return FindOneofByName(name, strlen(name));
  2084. }
  2085. const OneofDef* FindOneofByName(const char* name) const {
  2086. return FindOneofByName(name, strlen(name));
  2087. }
  2088. template<class T>
  2089. OneofDef* FindOneofByName(const T& str) {
  2090. return FindOneofByName(str.c_str(), str.size());
  2091. }
  2092. template<class T>
  2093. const OneofDef* FindOneofByName(const T& str) const {
  2094. return FindOneofByName(str.c_str(), str.size());
  2095. }
  2096. /* Is this message a map entry? */
  2097. void setmapentry(bool map_entry);
  2098. bool mapentry() const;
  2099. /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
  2100. * non-well-known message. */
  2101. upb_wellknowntype_t wellknowntype() const;
  2102. /* Whether is a number wrapper. */
  2103. bool isnumberwrapper() const;
  2104. /* Iteration over fields. The order is undefined. */
  2105. class field_iterator
  2106. : public std::iterator<std::forward_iterator_tag, FieldDef*> {
  2107. public:
  2108. explicit field_iterator(MessageDef* md);
  2109. static field_iterator end(MessageDef* md);
  2110. void operator++();
  2111. FieldDef* operator*() const;
  2112. bool operator!=(const field_iterator& other) const;
  2113. bool operator==(const field_iterator& other) const;
  2114. private:
  2115. upb_msg_field_iter iter_;
  2116. };
  2117. class const_field_iterator
  2118. : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
  2119. public:
  2120. explicit const_field_iterator(const MessageDef* md);
  2121. static const_field_iterator end(const MessageDef* md);
  2122. void operator++();
  2123. const FieldDef* operator*() const;
  2124. bool operator!=(const const_field_iterator& other) const;
  2125. bool operator==(const const_field_iterator& other) const;
  2126. private:
  2127. upb_msg_field_iter iter_;
  2128. };
  2129. /* Iteration over oneofs. The order is undefined. */
  2130. class oneof_iterator
  2131. : public std::iterator<std::forward_iterator_tag, FieldDef*> {
  2132. public:
  2133. explicit oneof_iterator(MessageDef* md);
  2134. static oneof_iterator end(MessageDef* md);
  2135. void operator++();
  2136. OneofDef* operator*() const;
  2137. bool operator!=(const oneof_iterator& other) const;
  2138. bool operator==(const oneof_iterator& other) const;
  2139. private:
  2140. upb_msg_oneof_iter iter_;
  2141. };
  2142. class const_oneof_iterator
  2143. : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
  2144. public:
  2145. explicit const_oneof_iterator(const MessageDef* md);
  2146. static const_oneof_iterator end(const MessageDef* md);
  2147. void operator++();
  2148. const OneofDef* operator*() const;
  2149. bool operator!=(const const_oneof_iterator& other) const;
  2150. bool operator==(const const_oneof_iterator& other) const;
  2151. private:
  2152. upb_msg_oneof_iter iter_;
  2153. };
  2154. class FieldAccessor {
  2155. public:
  2156. explicit FieldAccessor(MessageDef* msg) : msg_(msg) {}
  2157. field_iterator begin() { return msg_->field_begin(); }
  2158. field_iterator end() { return msg_->field_end(); }
  2159. private:
  2160. MessageDef* msg_;
  2161. };
  2162. class ConstFieldAccessor {
  2163. public:
  2164. explicit ConstFieldAccessor(const MessageDef* msg) : msg_(msg) {}
  2165. const_field_iterator begin() { return msg_->field_begin(); }
  2166. const_field_iterator end() { return msg_->field_end(); }
  2167. private:
  2168. const MessageDef* msg_;
  2169. };
  2170. class OneofAccessor {
  2171. public:
  2172. explicit OneofAccessor(MessageDef* msg) : msg_(msg) {}
  2173. oneof_iterator begin() { return msg_->oneof_begin(); }
  2174. oneof_iterator end() { return msg_->oneof_end(); }
  2175. private:
  2176. MessageDef* msg_;
  2177. };
  2178. class ConstOneofAccessor {
  2179. public:
  2180. explicit ConstOneofAccessor(const MessageDef* msg) : msg_(msg) {}
  2181. const_oneof_iterator begin() { return msg_->oneof_begin(); }
  2182. const_oneof_iterator end() { return msg_->oneof_end(); }
  2183. private:
  2184. const MessageDef* msg_;
  2185. };
  2186. field_iterator field_begin();
  2187. field_iterator field_end();
  2188. const_field_iterator field_begin() const;
  2189. const_field_iterator field_end() const;
  2190. oneof_iterator oneof_begin();
  2191. oneof_iterator oneof_end();
  2192. const_oneof_iterator oneof_begin() const;
  2193. const_oneof_iterator oneof_end() const;
  2194. FieldAccessor fields() { return FieldAccessor(this); }
  2195. ConstFieldAccessor fields() const { return ConstFieldAccessor(this); }
  2196. OneofAccessor oneofs() { return OneofAccessor(this); }
  2197. ConstOneofAccessor oneofs() const { return ConstOneofAccessor(this); }
  2198. private:
  2199. UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef)
  2200. };
  2201. #endif /* __cplusplus */
  2202. UPB_BEGIN_EXTERN_C
  2203. /* Returns NULL if memory allocation failed. */
  2204. upb_msgdef *upb_msgdef_new(const void *owner);
  2205. /* Include upb_refcounted methods like upb_msgdef_ref(). */
  2206. UPB_REFCOUNTED_CMETHODS(upb_msgdef, upb_msgdef_upcast2)
  2207. bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status);
  2208. const char *upb_msgdef_fullname(const upb_msgdef *m);
  2209. const char *upb_msgdef_name(const upb_msgdef *m);
  2210. int upb_msgdef_numoneofs(const upb_msgdef *m);
  2211. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
  2212. bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
  2213. upb_status *s);
  2214. bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
  2215. upb_status *s);
  2216. bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname, upb_status *s);
  2217. void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry);
  2218. bool upb_msgdef_mapentry(const upb_msgdef *m);
  2219. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
  2220. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
  2221. bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax);
  2222. /* Field lookup in a couple of different variations:
  2223. * - itof = int to field
  2224. * - ntof = name to field
  2225. * - ntofz = name to field, null-terminated string. */
  2226. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
  2227. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  2228. size_t len);
  2229. int upb_msgdef_numfields(const upb_msgdef *m);
  2230. UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
  2231. const char *name) {
  2232. return upb_msgdef_ntof(m, name, strlen(name));
  2233. }
  2234. UPB_INLINE upb_fielddef *upb_msgdef_itof_mutable(upb_msgdef *m, uint32_t i) {
  2235. return (upb_fielddef*)upb_msgdef_itof(m, i);
  2236. }
  2237. UPB_INLINE upb_fielddef *upb_msgdef_ntof_mutable(upb_msgdef *m,
  2238. const char *name, size_t len) {
  2239. return (upb_fielddef *)upb_msgdef_ntof(m, name, len);
  2240. }
  2241. /* Oneof lookup:
  2242. * - ntoo = name to oneof
  2243. * - ntooz = name to oneof, null-terminated string. */
  2244. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  2245. size_t len);
  2246. int upb_msgdef_numoneofs(const upb_msgdef *m);
  2247. UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
  2248. const char *name) {
  2249. return upb_msgdef_ntoo(m, name, strlen(name));
  2250. }
  2251. UPB_INLINE upb_oneofdef *upb_msgdef_ntoo_mutable(upb_msgdef *m,
  2252. const char *name, size_t len) {
  2253. return (upb_oneofdef *)upb_msgdef_ntoo(m, name, len);
  2254. }
  2255. /* Lookup of either field or oneof by name. Returns whether either was found.
  2256. * If the return is true, then the found def will be set, and the non-found
  2257. * one set to NULL. */
  2258. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  2259. const upb_fielddef **f, const upb_oneofdef **o);
  2260. UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
  2261. const upb_fielddef **f,
  2262. const upb_oneofdef **o) {
  2263. return upb_msgdef_lookupname(m, name, strlen(name), f, o);
  2264. }
  2265. /* Iteration over fields and oneofs. For example:
  2266. *
  2267. * upb_msg_field_iter i;
  2268. * for(upb_msg_field_begin(&i, m);
  2269. * !upb_msg_field_done(&i);
  2270. * upb_msg_field_next(&i)) {
  2271. * upb_fielddef *f = upb_msg_iter_field(&i);
  2272. * // ...
  2273. * }
  2274. *
  2275. * For C we don't have separate iterators for const and non-const.
  2276. * It is the caller's responsibility to cast the upb_fielddef* to
  2277. * const if the upb_msgdef* is const. */
  2278. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
  2279. void upb_msg_field_next(upb_msg_field_iter *iter);
  2280. bool upb_msg_field_done(const upb_msg_field_iter *iter);
  2281. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
  2282. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
  2283. /* Similar to above, we also support iterating through the oneofs in a
  2284. * msgdef. */
  2285. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m);
  2286. void upb_msg_oneof_next(upb_msg_oneof_iter *iter);
  2287. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
  2288. upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
  2289. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter);
  2290. UPB_END_EXTERN_C
  2291. /* upb::EnumDef ***************************************************************/
  2292. typedef upb_strtable_iter upb_enum_iter;
  2293. #ifdef __cplusplus
  2294. /* Class that represents an enum. Its base class is upb::Def (convert with
  2295. * upb::upcast()). */
  2296. class upb::EnumDef {
  2297. public:
  2298. /* Returns NULL if memory allocation failed. */
  2299. static reffed_ptr<EnumDef> New();
  2300. /* upb::RefCounted methods like Ref()/Unref(). */
  2301. UPB_REFCOUNTED_CPPMETHODS
  2302. /* Functionality from upb::Def. */
  2303. const char* full_name() const;
  2304. const char* name() const;
  2305. bool set_full_name(const char* fullname, Status* s);
  2306. bool set_full_name(const std::string& fullname, Status* s);
  2307. /* Call to freeze this EnumDef. */
  2308. bool Freeze(Status* s);
  2309. /* The value that is used as the default when no field default is specified.
  2310. * If not set explicitly, the first value that was added will be used.
  2311. * The default value must be a member of the enum.
  2312. * Requires that value_count() > 0. */
  2313. int32_t default_value() const;
  2314. /* Sets the default value. If this value is not valid, returns false and an
  2315. * error message in status. */
  2316. bool set_default_value(int32_t val, Status* status);
  2317. /* Returns the number of values currently defined in the enum. Note that
  2318. * multiple names can refer to the same number, so this may be greater than
  2319. * the total number of unique numbers. */
  2320. int value_count() const;
  2321. /* Adds a single name/number pair to the enum. Fails if this name has
  2322. * already been used by another value. */
  2323. bool AddValue(const char* name, int32_t num, Status* status);
  2324. bool AddValue(const std::string& name, int32_t num, Status* status);
  2325. /* Lookups from name to integer, returning true if found. */
  2326. bool FindValueByName(const char* name, int32_t* num) const;
  2327. /* Finds the name corresponding to the given number, or NULL if none was
  2328. * found. If more than one name corresponds to this number, returns the
  2329. * first one that was added. */
  2330. const char* FindValueByNumber(int32_t num) const;
  2331. /* Iteration over name/value pairs. The order is undefined.
  2332. * Adding an enum val invalidates any iterators.
  2333. *
  2334. * TODO: make compatible with range-for, with elements as pairs? */
  2335. class Iterator {
  2336. public:
  2337. explicit Iterator(const EnumDef*);
  2338. int32_t number();
  2339. const char *name();
  2340. bool Done();
  2341. void Next();
  2342. private:
  2343. upb_enum_iter iter_;
  2344. };
  2345. private:
  2346. UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef)
  2347. };
  2348. #endif /* __cplusplus */
  2349. UPB_BEGIN_EXTERN_C
  2350. /* Native C API. */
  2351. upb_enumdef *upb_enumdef_new(const void *owner);
  2352. /* Include upb_refcounted methods like upb_enumdef_ref(). */
  2353. UPB_REFCOUNTED_CMETHODS(upb_enumdef, upb_enumdef_upcast2)
  2354. bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status);
  2355. /* From upb_def. */
  2356. const char *upb_enumdef_fullname(const upb_enumdef *e);
  2357. const char *upb_enumdef_name(const upb_enumdef *e);
  2358. bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
  2359. upb_status *s);
  2360. int32_t upb_enumdef_default(const upb_enumdef *e);
  2361. bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s);
  2362. int upb_enumdef_numvals(const upb_enumdef *e);
  2363. bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
  2364. upb_status *status);
  2365. /* Enum lookups:
  2366. * - ntoi: look up a name with specified length.
  2367. * - ntoiz: look up a name provided as a null-terminated string.
  2368. * - iton: look up an integer, returning the name as a null-terminated
  2369. * string. */
  2370. bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
  2371. int32_t *num);
  2372. UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
  2373. const char *name, int32_t *num) {
  2374. return upb_enumdef_ntoi(e, name, strlen(name), num);
  2375. }
  2376. const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
  2377. /* upb_enum_iter i;
  2378. * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
  2379. * // ...
  2380. * }
  2381. */
  2382. void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
  2383. void upb_enum_next(upb_enum_iter *iter);
  2384. bool upb_enum_done(upb_enum_iter *iter);
  2385. const char *upb_enum_iter_name(upb_enum_iter *iter);
  2386. int32_t upb_enum_iter_number(upb_enum_iter *iter);
  2387. UPB_END_EXTERN_C
  2388. /* upb::OneofDef **************************************************************/
  2389. typedef upb_inttable_iter upb_oneof_iter;
  2390. #ifdef __cplusplus
  2391. /* Class that represents a oneof. */
  2392. class upb::OneofDef {
  2393. public:
  2394. /* Returns NULL if memory allocation failed. */
  2395. static reffed_ptr<OneofDef> New();
  2396. /* upb::RefCounted methods like Ref()/Unref(). */
  2397. UPB_REFCOUNTED_CPPMETHODS
  2398. /* Returns the MessageDef that owns this OneofDef. */
  2399. const MessageDef* containing_type() const;
  2400. /* Returns the name of this oneof. This is the name used to look up the oneof
  2401. * by name once added to a message def. */
  2402. const char* name() const;
  2403. bool set_name(const char* name, Status* s);
  2404. bool set_name(const std::string& name, Status* s);
  2405. /* Returns the number of fields currently defined in the oneof. */
  2406. int field_count() const;
  2407. /* Adds a field to the oneof. The field must not have been added to any other
  2408. * oneof or msgdef. If the oneof is not yet part of a msgdef, then when the
  2409. * oneof is eventually added to a msgdef, all fields added to the oneof will
  2410. * also be added to the msgdef at that time. If the oneof is already part of a
  2411. * msgdef, the field must either be a part of that msgdef already, or must not
  2412. * be a part of any msgdef; in the latter case, the field is added to the
  2413. * msgdef as a part of this operation.
  2414. *
  2415. * The field may only have an OPTIONAL label, never REQUIRED or REPEATED.
  2416. *
  2417. * If |f| is already part of this MessageDef, this method performs no action
  2418. * and returns true (success). Thus, this method is idempotent. */
  2419. bool AddField(FieldDef* field, Status* s);
  2420. bool AddField(const reffed_ptr<FieldDef>& field, Status* s);
  2421. /* Looks up by name. */
  2422. const FieldDef* FindFieldByName(const char* name, size_t len) const;
  2423. FieldDef* FindFieldByName(const char* name, size_t len);
  2424. const FieldDef* FindFieldByName(const char* name) const {
  2425. return FindFieldByName(name, strlen(name));
  2426. }
  2427. FieldDef* FindFieldByName(const char* name) {
  2428. return FindFieldByName(name, strlen(name));
  2429. }
  2430. template <class T>
  2431. FieldDef* FindFieldByName(const T& str) {
  2432. return FindFieldByName(str.c_str(), str.size());
  2433. }
  2434. template <class T>
  2435. const FieldDef* FindFieldByName(const T& str) const {
  2436. return FindFieldByName(str.c_str(), str.size());
  2437. }
  2438. /* Looks up by tag number. */
  2439. const FieldDef* FindFieldByNumber(uint32_t num) const;
  2440. /* Iteration over fields. The order is undefined. */
  2441. class iterator : public std::iterator<std::forward_iterator_tag, FieldDef*> {
  2442. public:
  2443. explicit iterator(OneofDef* md);
  2444. static iterator end(OneofDef* md);
  2445. void operator++();
  2446. FieldDef* operator*() const;
  2447. bool operator!=(const iterator& other) const;
  2448. bool operator==(const iterator& other) const;
  2449. private:
  2450. upb_oneof_iter iter_;
  2451. };
  2452. class const_iterator
  2453. : public std::iterator<std::forward_iterator_tag, const FieldDef*> {
  2454. public:
  2455. explicit const_iterator(const OneofDef* md);
  2456. static const_iterator end(const OneofDef* md);
  2457. void operator++();
  2458. const FieldDef* operator*() const;
  2459. bool operator!=(const const_iterator& other) const;
  2460. bool operator==(const const_iterator& other) const;
  2461. private:
  2462. upb_oneof_iter iter_;
  2463. };
  2464. iterator begin();
  2465. iterator end();
  2466. const_iterator begin() const;
  2467. const_iterator end() const;
  2468. private:
  2469. UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef)
  2470. };
  2471. #endif /* __cplusplus */
  2472. UPB_BEGIN_EXTERN_C
  2473. /* Native C API. */
  2474. upb_oneofdef *upb_oneofdef_new(const void *owner);
  2475. /* Include upb_refcounted methods like upb_oneofdef_ref(). */
  2476. UPB_REFCOUNTED_CMETHODS(upb_oneofdef, upb_oneofdef_upcast)
  2477. const char *upb_oneofdef_name(const upb_oneofdef *o);
  2478. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
  2479. int upb_oneofdef_numfields(const upb_oneofdef *o);
  2480. uint32_t upb_oneofdef_index(const upb_oneofdef *o);
  2481. bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s);
  2482. bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
  2483. const void *ref_donor,
  2484. upb_status *s);
  2485. /* Oneof lookups:
  2486. * - ntof: look up a field by name.
  2487. * - ntofz: look up a field by name (as a null-terminated string).
  2488. * - itof: look up a field by number. */
  2489. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  2490. const char *name, size_t length);
  2491. UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
  2492. const char *name) {
  2493. return upb_oneofdef_ntof(o, name, strlen(name));
  2494. }
  2495. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
  2496. /* upb_oneof_iter i;
  2497. * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
  2498. * // ...
  2499. * }
  2500. */
  2501. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
  2502. void upb_oneof_next(upb_oneof_iter *iter);
  2503. bool upb_oneof_done(upb_oneof_iter *iter);
  2504. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
  2505. void upb_oneof_iter_setdone(upb_oneof_iter *iter);
  2506. UPB_END_EXTERN_C
  2507. /* upb::FileDef ***************************************************************/
  2508. #ifdef __cplusplus
  2509. /* Class that represents a .proto file with some things defined in it.
  2510. *
  2511. * Many users won't care about FileDefs, but they are necessary if you want to
  2512. * read the values of file-level options. */
  2513. class upb::FileDef {
  2514. public:
  2515. /* Returns NULL if memory allocation failed. */
  2516. static reffed_ptr<FileDef> New();
  2517. /* upb::RefCounted methods like Ref()/Unref(). */
  2518. UPB_REFCOUNTED_CPPMETHODS
  2519. /* Get/set name of the file (eg. "foo/bar.proto"). */
  2520. const char* name() const;
  2521. bool set_name(const char* name, Status* s);
  2522. bool set_name(const std::string& name, Status* s);
  2523. /* Package name for definitions inside the file (eg. "foo.bar"). */
  2524. const char* package() const;
  2525. bool set_package(const char* package, Status* s);
  2526. /* Sets the php class prefix which is prepended to all php generated classes
  2527. * from this .proto. Default is empty. */
  2528. const char* phpprefix() const;
  2529. bool set_phpprefix(const char* phpprefix, Status* s);
  2530. /* Use this option to change the namespace of php generated classes. Default
  2531. * is empty. When this option is empty, the package name will be used for
  2532. * determining the namespace. */
  2533. const char* phpnamespace() const;
  2534. bool set_phpnamespace(const char* phpnamespace, Status* s);
  2535. /* Syntax for the file. Defaults to proto2. */
  2536. upb_syntax_t syntax() const;
  2537. void set_syntax(upb_syntax_t syntax);
  2538. /* Get the list of defs from the file. These are returned in the order that
  2539. * they were added to the FileDef. */
  2540. int def_count() const;
  2541. const Def* def(int index) const;
  2542. Def* def(int index);
  2543. /* Get the list of dependencies from the file. These are returned in the
  2544. * order that they were added to the FileDef. */
  2545. int dependency_count() const;
  2546. const FileDef* dependency(int index) const;
  2547. /* Adds defs to this file. The def must not already belong to another
  2548. * file.
  2549. *
  2550. * Note: this does *not* ensure that this def's name is unique in this file!
  2551. * Use a SymbolTable if you want to check this property. Especially since
  2552. * properly checking uniqueness would require a check across *all* files
  2553. * (including dependencies). */
  2554. bool AddDef(Def* def, Status* s);
  2555. bool AddMessage(MessageDef* m, Status* s);
  2556. bool AddEnum(EnumDef* e, Status* s);
  2557. bool AddExtension(FieldDef* f, Status* s);
  2558. /* Adds a dependency of this file. */
  2559. bool AddDependency(const FileDef* file);
  2560. /* Freezes this FileDef and all messages/enums under it. All subdefs must be
  2561. * resolved and all messages/enums must validate. Returns true if this
  2562. * succeeded.
  2563. *
  2564. * TODO(haberman): should we care whether the file's dependencies are frozen
  2565. * already? */
  2566. bool Freeze(Status* s);
  2567. private:
  2568. UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef)
  2569. };
  2570. #endif
  2571. UPB_BEGIN_EXTERN_C
  2572. upb_filedef *upb_filedef_new(const void *owner);
  2573. /* Include upb_refcounted methods like upb_msgdef_ref(). */
  2574. UPB_REFCOUNTED_CMETHODS(upb_filedef, upb_filedef_upcast)
  2575. const char *upb_filedef_name(const upb_filedef *f);
  2576. const char *upb_filedef_package(const upb_filedef *f);
  2577. const char *upb_filedef_phpprefix(const upb_filedef *f);
  2578. const char *upb_filedef_phpnamespace(const upb_filedef *f);
  2579. upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
  2580. size_t upb_filedef_defcount(const upb_filedef *f);
  2581. size_t upb_filedef_depcount(const upb_filedef *f);
  2582. const upb_def *upb_filedef_def(const upb_filedef *f, size_t i);
  2583. const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i);
  2584. bool upb_filedef_freeze(upb_filedef *f, upb_status *s);
  2585. bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s);
  2586. bool upb_filedef_setpackage(upb_filedef *f, const char *package, upb_status *s);
  2587. bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix,
  2588. upb_status *s);
  2589. bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace,
  2590. upb_status *s);
  2591. bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax, upb_status *s);
  2592. bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor,
  2593. upb_status *s);
  2594. bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep);
  2595. UPB_INLINE bool upb_filedef_addmsg(upb_filedef *f, upb_msgdef *m,
  2596. const void *ref_donor, upb_status *s) {
  2597. return upb_filedef_adddef(f, upb_msgdef_upcast_mutable(m), ref_donor, s);
  2598. }
  2599. UPB_INLINE bool upb_filedef_addenum(upb_filedef *f, upb_enumdef *e,
  2600. const void *ref_donor, upb_status *s) {
  2601. return upb_filedef_adddef(f, upb_enumdef_upcast_mutable(e), ref_donor, s);
  2602. }
  2603. UPB_INLINE bool upb_filedef_addext(upb_filedef *file, upb_fielddef *f,
  2604. const void *ref_donor, upb_status *s) {
  2605. return upb_filedef_adddef(file, upb_fielddef_upcast_mutable(f), ref_donor, s);
  2606. }
  2607. UPB_INLINE upb_def *upb_filedef_mutabledef(upb_filedef *f, int i) {
  2608. return (upb_def*)upb_filedef_def(f, i);
  2609. }
  2610. UPB_END_EXTERN_C
  2611. typedef struct {
  2612. UPB_PRIVATE_FOR_CPP
  2613. upb_strtable_iter iter;
  2614. upb_deftype_t type;
  2615. } upb_symtab_iter;
  2616. #ifdef __cplusplus
  2617. /* Non-const methods in upb::SymbolTable are NOT thread-safe. */
  2618. class upb::SymbolTable {
  2619. public:
  2620. /* Returns a new symbol table with a single ref owned by "owner."
  2621. * Returns NULL if memory allocation failed. */
  2622. static SymbolTable* New();
  2623. static void Free(upb::SymbolTable* table);
  2624. /* For all lookup functions, the returned pointer is not owned by the
  2625. * caller; it may be invalidated by any non-const call or unref of the
  2626. * SymbolTable! To protect against this, take a ref if desired. */
  2627. /* Freezes the symbol table: prevents further modification of it.
  2628. * After the Freeze() operation is successful, the SymbolTable must only be
  2629. * accessed via a const pointer.
  2630. *
  2631. * Unlike with upb::MessageDef/upb::EnumDef/etc, freezing a SymbolTable is not
  2632. * a necessary step in using a SymbolTable. If you have no need for it to be
  2633. * immutable, there is no need to freeze it ever. However sometimes it is
  2634. * useful, and SymbolTables that are statically compiled into the binary are
  2635. * always frozen by nature. */
  2636. void Freeze();
  2637. /* Resolves the given symbol using the rules described in descriptor.proto,
  2638. * namely:
  2639. *
  2640. * If the name starts with a '.', it is fully-qualified. Otherwise,
  2641. * C++-like scoping rules are used to find the type (i.e. first the nested
  2642. * types within this message are searched, then within the parent, on up
  2643. * to the root namespace).
  2644. *
  2645. * If not found, returns NULL. */
  2646. const Def* Resolve(const char* base, const char* sym) const;
  2647. /* Finds an entry in the symbol table with this exact name. If not found,
  2648. * returns NULL. */
  2649. const Def* Lookup(const char *sym) const;
  2650. const MessageDef* LookupMessage(const char *sym) const;
  2651. const EnumDef* LookupEnum(const char *sym) const;
  2652. /* TODO: introduce a C++ iterator, but make it nice and templated so that if
  2653. * you ask for an iterator of MessageDef the iterated elements are strongly
  2654. * typed as MessageDef*. */
  2655. /* Adds the given mutable defs to the symtab, resolving all symbols (including
  2656. * enum default values) and finalizing the defs. Only one def per name may be
  2657. * in the list, and the defs may not duplicate any name already in the symtab.
  2658. * All defs must have a name -- anonymous defs are not allowed. Anonymous
  2659. * defs can still be frozen by calling upb_def_freeze() directly.
  2660. *
  2661. * The entire operation either succeeds or fails. If the operation fails,
  2662. * the symtab is unchanged, false is returned, and status indicates the
  2663. * error. The caller passes a ref on all defs to the symtab (even if the
  2664. * operation fails).
  2665. *
  2666. * TODO(haberman): currently failure will leave the symtab unchanged, but may
  2667. * leave the defs themselves partially resolved. Does this matter? If so we
  2668. * could do a prepass that ensures that all symbols are resolvable and bail
  2669. * if not, so we don't mutate anything until we know the operation will
  2670. * succeed. */
  2671. bool Add(Def*const* defs, size_t n, void* ref_donor, Status* status);
  2672. bool Add(const std::vector<Def*>& defs, void *owner, Status* status) {
  2673. return Add((Def*const*)&defs[0], defs.size(), owner, status);
  2674. }
  2675. /* Resolves all subdefs for messages in this file and attempts to freeze the
  2676. * file. If this succeeds, adds all the symbols to this SymbolTable
  2677. * (replacing any existing ones with the same names). */
  2678. bool AddFile(FileDef* file, Status* s);
  2679. private:
  2680. UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable)
  2681. };
  2682. #endif /* __cplusplus */
  2683. UPB_BEGIN_EXTERN_C
  2684. /* Native C API. */
  2685. upb_symtab *upb_symtab_new();
  2686. void upb_symtab_free(upb_symtab* s);
  2687. const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
  2688. const char *sym);
  2689. const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym);
  2690. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
  2691. const upb_msgdef *upb_symtab_lookupmsg2(
  2692. const upb_symtab *s, const char *sym, size_t len);
  2693. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
  2694. bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
  2695. void *ref_donor, upb_status *status);
  2696. bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status* status);
  2697. /* upb_symtab_iter i;
  2698. * for(upb_symtab_begin(&i, s, type); !upb_symtab_done(&i);
  2699. * upb_symtab_next(&i)) {
  2700. * const upb_def *def = upb_symtab_iter_def(&i);
  2701. * // ...
  2702. * }
  2703. *
  2704. * For C we don't have separate iterators for const and non-const.
  2705. * It is the caller's responsibility to cast the upb_fielddef* to
  2706. * const if the upb_msgdef* is const. */
  2707. void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
  2708. upb_deftype_t type);
  2709. void upb_symtab_next(upb_symtab_iter *iter);
  2710. bool upb_symtab_done(const upb_symtab_iter *iter);
  2711. const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter);
  2712. UPB_END_EXTERN_C
  2713. #ifdef __cplusplus
  2714. /* C++ inline wrappers. */
  2715. namespace upb {
  2716. inline SymbolTable* SymbolTable::New() {
  2717. return upb_symtab_new();
  2718. }
  2719. inline void SymbolTable::Free(SymbolTable* s) {
  2720. upb_symtab_free(s);
  2721. }
  2722. inline const Def *SymbolTable::Resolve(const char *base,
  2723. const char *sym) const {
  2724. return upb_symtab_resolve(this, base, sym);
  2725. }
  2726. inline const Def* SymbolTable::Lookup(const char *sym) const {
  2727. return upb_symtab_lookup(this, sym);
  2728. }
  2729. inline const MessageDef *SymbolTable::LookupMessage(const char *sym) const {
  2730. return upb_symtab_lookupmsg(this, sym);
  2731. }
  2732. inline bool SymbolTable::Add(
  2733. Def*const* defs, size_t n, void* ref_donor, Status* status) {
  2734. return upb_symtab_add(this, (upb_def*const*)defs, n, ref_donor, status);
  2735. }
  2736. inline bool SymbolTable::AddFile(FileDef* file, Status* s) {
  2737. return upb_symtab_addfile(this, file, s);
  2738. }
  2739. } /* namespace upb */
  2740. #endif
  2741. #ifdef __cplusplus
  2742. UPB_INLINE const char* upb_safecstr(const std::string& str) {
  2743. UPB_ASSERT(str.size() == std::strlen(str.c_str()));
  2744. return str.c_str();
  2745. }
  2746. /* Inline C++ wrappers. */
  2747. namespace upb {
  2748. inline Def::Type Def::def_type() const { return upb_def_type(this); }
  2749. inline const char* Def::full_name() const { return upb_def_fullname(this); }
  2750. inline const char* Def::name() const { return upb_def_name(this); }
  2751. inline bool Def::set_full_name(const char* fullname, Status* s) {
  2752. return upb_def_setfullname(this, fullname, s);
  2753. }
  2754. inline bool Def::set_full_name(const std::string& fullname, Status* s) {
  2755. return upb_def_setfullname(this, upb_safecstr(fullname), s);
  2756. }
  2757. inline bool Def::Freeze(Def* const* defs, size_t n, Status* status) {
  2758. return upb_def_freeze(defs, n, status);
  2759. }
  2760. inline bool Def::Freeze(const std::vector<Def*>& defs, Status* status) {
  2761. return upb_def_freeze((Def* const*)&defs[0], defs.size(), status);
  2762. }
  2763. inline bool FieldDef::CheckType(int32_t val) {
  2764. return upb_fielddef_checktype(val);
  2765. }
  2766. inline bool FieldDef::CheckLabel(int32_t val) {
  2767. return upb_fielddef_checklabel(val);
  2768. }
  2769. inline bool FieldDef::CheckDescriptorType(int32_t val) {
  2770. return upb_fielddef_checkdescriptortype(val);
  2771. }
  2772. inline bool FieldDef::CheckIntegerFormat(int32_t val) {
  2773. return upb_fielddef_checkintfmt(val);
  2774. }
  2775. inline FieldDef::Type FieldDef::ConvertType(int32_t val) {
  2776. UPB_ASSERT(CheckType(val));
  2777. return static_cast<FieldDef::Type>(val);
  2778. }
  2779. inline FieldDef::Label FieldDef::ConvertLabel(int32_t val) {
  2780. UPB_ASSERT(CheckLabel(val));
  2781. return static_cast<FieldDef::Label>(val);
  2782. }
  2783. inline FieldDef::DescriptorType FieldDef::ConvertDescriptorType(int32_t val) {
  2784. UPB_ASSERT(CheckDescriptorType(val));
  2785. return static_cast<FieldDef::DescriptorType>(val);
  2786. }
  2787. inline FieldDef::IntegerFormat FieldDef::ConvertIntegerFormat(int32_t val) {
  2788. UPB_ASSERT(CheckIntegerFormat(val));
  2789. return static_cast<FieldDef::IntegerFormat>(val);
  2790. }
  2791. inline reffed_ptr<FieldDef> FieldDef::New() {
  2792. upb_fielddef *f = upb_fielddef_new(&f);
  2793. return reffed_ptr<FieldDef>(f, &f);
  2794. }
  2795. inline const char* FieldDef::full_name() const {
  2796. return upb_fielddef_fullname(this);
  2797. }
  2798. inline bool FieldDef::set_full_name(const char* fullname, Status* s) {
  2799. return upb_fielddef_setfullname(this, fullname, s);
  2800. }
  2801. inline bool FieldDef::set_full_name(const std::string& fullname, Status* s) {
  2802. return upb_fielddef_setfullname(this, upb_safecstr(fullname), s);
  2803. }
  2804. inline bool FieldDef::type_is_set() const {
  2805. return upb_fielddef_typeisset(this);
  2806. }
  2807. inline FieldDef::Type FieldDef::type() const { return upb_fielddef_type(this); }
  2808. inline FieldDef::DescriptorType FieldDef::descriptor_type() const {
  2809. return upb_fielddef_descriptortype(this);
  2810. }
  2811. inline FieldDef::Label FieldDef::label() const {
  2812. return upb_fielddef_label(this);
  2813. }
  2814. inline uint32_t FieldDef::number() const { return upb_fielddef_number(this); }
  2815. inline const char* FieldDef::name() const { return upb_fielddef_name(this); }
  2816. inline bool FieldDef::is_extension() const {
  2817. return upb_fielddef_isextension(this);
  2818. }
  2819. inline size_t FieldDef::GetJsonName(char* buf, size_t len) const {
  2820. return upb_fielddef_getjsonname(this, buf, len);
  2821. }
  2822. inline bool FieldDef::lazy() const {
  2823. return upb_fielddef_lazy(this);
  2824. }
  2825. inline void FieldDef::set_lazy(bool lazy) {
  2826. upb_fielddef_setlazy(this, lazy);
  2827. }
  2828. inline bool FieldDef::packed() const {
  2829. return upb_fielddef_packed(this);
  2830. }
  2831. inline uint32_t FieldDef::index() const {
  2832. return upb_fielddef_index(this);
  2833. }
  2834. inline void FieldDef::set_packed(bool packed) {
  2835. upb_fielddef_setpacked(this, packed);
  2836. }
  2837. inline const MessageDef* FieldDef::containing_type() const {
  2838. return upb_fielddef_containingtype(this);
  2839. }
  2840. inline const OneofDef* FieldDef::containing_oneof() const {
  2841. return upb_fielddef_containingoneof(this);
  2842. }
  2843. inline const char* FieldDef::containing_type_name() {
  2844. return upb_fielddef_containingtypename(this);
  2845. }
  2846. inline bool FieldDef::set_number(uint32_t number, Status* s) {
  2847. return upb_fielddef_setnumber(this, number, s);
  2848. }
  2849. inline bool FieldDef::set_name(const char *name, Status* s) {
  2850. return upb_fielddef_setname(this, name, s);
  2851. }
  2852. inline bool FieldDef::set_name(const std::string& name, Status* s) {
  2853. return upb_fielddef_setname(this, upb_safecstr(name), s);
  2854. }
  2855. inline bool FieldDef::set_json_name(const char *name, Status* s) {
  2856. return upb_fielddef_setjsonname(this, name, s);
  2857. }
  2858. inline bool FieldDef::set_json_name(const std::string& name, Status* s) {
  2859. return upb_fielddef_setjsonname(this, upb_safecstr(name), s);
  2860. }
  2861. inline void FieldDef::clear_json_name() {
  2862. upb_fielddef_clearjsonname(this);
  2863. }
  2864. inline bool FieldDef::set_containing_type_name(const char *name, Status* s) {
  2865. return upb_fielddef_setcontainingtypename(this, name, s);
  2866. }
  2867. inline bool FieldDef::set_containing_type_name(const std::string &name,
  2868. Status *s) {
  2869. return upb_fielddef_setcontainingtypename(this, upb_safecstr(name), s);
  2870. }
  2871. inline void FieldDef::set_type(upb_fieldtype_t type) {
  2872. upb_fielddef_settype(this, type);
  2873. }
  2874. inline void FieldDef::set_is_extension(bool is_extension) {
  2875. upb_fielddef_setisextension(this, is_extension);
  2876. }
  2877. inline void FieldDef::set_descriptor_type(FieldDef::DescriptorType type) {
  2878. upb_fielddef_setdescriptortype(this, type);
  2879. }
  2880. inline void FieldDef::set_label(upb_label_t label) {
  2881. upb_fielddef_setlabel(this, label);
  2882. }
  2883. inline bool FieldDef::IsSubMessage() const {
  2884. return upb_fielddef_issubmsg(this);
  2885. }
  2886. inline bool FieldDef::IsString() const { return upb_fielddef_isstring(this); }
  2887. inline bool FieldDef::IsSequence() const { return upb_fielddef_isseq(this); }
  2888. inline bool FieldDef::IsMap() const { return upb_fielddef_ismap(this); }
  2889. inline int64_t FieldDef::default_int64() const {
  2890. return upb_fielddef_defaultint64(this);
  2891. }
  2892. inline int32_t FieldDef::default_int32() const {
  2893. return upb_fielddef_defaultint32(this);
  2894. }
  2895. inline uint64_t FieldDef::default_uint64() const {
  2896. return upb_fielddef_defaultuint64(this);
  2897. }
  2898. inline uint32_t FieldDef::default_uint32() const {
  2899. return upb_fielddef_defaultuint32(this);
  2900. }
  2901. inline bool FieldDef::default_bool() const {
  2902. return upb_fielddef_defaultbool(this);
  2903. }
  2904. inline float FieldDef::default_float() const {
  2905. return upb_fielddef_defaultfloat(this);
  2906. }
  2907. inline double FieldDef::default_double() const {
  2908. return upb_fielddef_defaultdouble(this);
  2909. }
  2910. inline const char* FieldDef::default_string(size_t* len) const {
  2911. return upb_fielddef_defaultstr(this, len);
  2912. }
  2913. inline void FieldDef::set_default_int64(int64_t value) {
  2914. upb_fielddef_setdefaultint64(this, value);
  2915. }
  2916. inline void FieldDef::set_default_int32(int32_t value) {
  2917. upb_fielddef_setdefaultint32(this, value);
  2918. }
  2919. inline void FieldDef::set_default_uint64(uint64_t value) {
  2920. upb_fielddef_setdefaultuint64(this, value);
  2921. }
  2922. inline void FieldDef::set_default_uint32(uint32_t value) {
  2923. upb_fielddef_setdefaultuint32(this, value);
  2924. }
  2925. inline void FieldDef::set_default_bool(bool value) {
  2926. upb_fielddef_setdefaultbool(this, value);
  2927. }
  2928. inline void FieldDef::set_default_float(float value) {
  2929. upb_fielddef_setdefaultfloat(this, value);
  2930. }
  2931. inline void FieldDef::set_default_double(double value) {
  2932. upb_fielddef_setdefaultdouble(this, value);
  2933. }
  2934. inline bool FieldDef::set_default_string(const void *str, size_t len,
  2935. Status *s) {
  2936. return upb_fielddef_setdefaultstr(this, str, len, s);
  2937. }
  2938. inline bool FieldDef::set_default_string(const std::string& str, Status* s) {
  2939. return upb_fielddef_setdefaultstr(this, str.c_str(), str.size(), s);
  2940. }
  2941. inline void FieldDef::set_default_cstr(const char* str, Status* s) {
  2942. return upb_fielddef_setdefaultcstr(this, str, s);
  2943. }
  2944. inline bool FieldDef::HasSubDef() const { return upb_fielddef_hassubdef(this); }
  2945. inline const Def* FieldDef::subdef() const { return upb_fielddef_subdef(this); }
  2946. inline const MessageDef *FieldDef::message_subdef() const {
  2947. return upb_fielddef_msgsubdef(this);
  2948. }
  2949. inline const EnumDef *FieldDef::enum_subdef() const {
  2950. return upb_fielddef_enumsubdef(this);
  2951. }
  2952. inline const char* FieldDef::subdef_name() const {
  2953. return upb_fielddef_subdefname(this);
  2954. }
  2955. inline bool FieldDef::set_subdef(const Def* subdef, Status* s) {
  2956. return upb_fielddef_setsubdef(this, subdef, s);
  2957. }
  2958. inline bool FieldDef::set_enum_subdef(const EnumDef* subdef, Status* s) {
  2959. return upb_fielddef_setenumsubdef(this, subdef, s);
  2960. }
  2961. inline bool FieldDef::set_message_subdef(const MessageDef* subdef, Status* s) {
  2962. return upb_fielddef_setmsgsubdef(this, subdef, s);
  2963. }
  2964. inline bool FieldDef::set_subdef_name(const char* name, Status* s) {
  2965. return upb_fielddef_setsubdefname(this, name, s);
  2966. }
  2967. inline bool FieldDef::set_subdef_name(const std::string& name, Status* s) {
  2968. return upb_fielddef_setsubdefname(this, upb_safecstr(name), s);
  2969. }
  2970. inline reffed_ptr<MessageDef> MessageDef::New() {
  2971. upb_msgdef *m = upb_msgdef_new(&m);
  2972. return reffed_ptr<MessageDef>(m, &m);
  2973. }
  2974. inline const char *MessageDef::full_name() const {
  2975. return upb_msgdef_fullname(this);
  2976. }
  2977. inline const char *MessageDef::name() const {
  2978. return upb_msgdef_name(this);
  2979. }
  2980. inline upb_syntax_t MessageDef::syntax() const {
  2981. return upb_msgdef_syntax(this);
  2982. }
  2983. inline bool MessageDef::set_full_name(const char* fullname, Status* s) {
  2984. return upb_msgdef_setfullname(this, fullname, s);
  2985. }
  2986. inline bool MessageDef::set_full_name(const std::string& fullname, Status* s) {
  2987. return upb_msgdef_setfullname(this, upb_safecstr(fullname), s);
  2988. }
  2989. inline bool MessageDef::set_syntax(upb_syntax_t syntax) {
  2990. return upb_msgdef_setsyntax(this, syntax);
  2991. }
  2992. inline bool MessageDef::Freeze(Status* status) {
  2993. return upb_msgdef_freeze(this, status);
  2994. }
  2995. inline int MessageDef::field_count() const {
  2996. return upb_msgdef_numfields(this);
  2997. }
  2998. inline int MessageDef::oneof_count() const {
  2999. return upb_msgdef_numoneofs(this);
  3000. }
  3001. inline bool MessageDef::AddField(upb_fielddef* f, Status* s) {
  3002. return upb_msgdef_addfield(this, f, NULL, s);
  3003. }
  3004. inline bool MessageDef::AddField(const reffed_ptr<FieldDef>& f, Status* s) {
  3005. return upb_msgdef_addfield(this, f.get(), NULL, s);
  3006. }
  3007. inline bool MessageDef::AddOneof(upb_oneofdef* o, Status* s) {
  3008. return upb_msgdef_addoneof(this, o, NULL, s);
  3009. }
  3010. inline bool MessageDef::AddOneof(const reffed_ptr<OneofDef>& o, Status* s) {
  3011. return upb_msgdef_addoneof(this, o.get(), NULL, s);
  3012. }
  3013. inline FieldDef* MessageDef::FindFieldByNumber(uint32_t number) {
  3014. return upb_msgdef_itof_mutable(this, number);
  3015. }
  3016. inline FieldDef* MessageDef::FindFieldByName(const char* name, size_t len) {
  3017. return upb_msgdef_ntof_mutable(this, name, len);
  3018. }
  3019. inline const FieldDef* MessageDef::FindFieldByNumber(uint32_t number) const {
  3020. return upb_msgdef_itof(this, number);
  3021. }
  3022. inline const FieldDef *MessageDef::FindFieldByName(const char *name,
  3023. size_t len) const {
  3024. return upb_msgdef_ntof(this, name, len);
  3025. }
  3026. inline OneofDef* MessageDef::FindOneofByName(const char* name, size_t len) {
  3027. return upb_msgdef_ntoo_mutable(this, name, len);
  3028. }
  3029. inline const OneofDef* MessageDef::FindOneofByName(const char* name,
  3030. size_t len) const {
  3031. return upb_msgdef_ntoo(this, name, len);
  3032. }
  3033. inline void MessageDef::setmapentry(bool map_entry) {
  3034. upb_msgdef_setmapentry(this, map_entry);
  3035. }
  3036. inline bool MessageDef::mapentry() const {
  3037. return upb_msgdef_mapentry(this);
  3038. }
  3039. inline upb_wellknowntype_t MessageDef::wellknowntype() const {
  3040. return upb_msgdef_wellknowntype(this);
  3041. }
  3042. inline bool MessageDef::isnumberwrapper() const {
  3043. return upb_msgdef_isnumberwrapper(this);
  3044. }
  3045. inline MessageDef::field_iterator MessageDef::field_begin() {
  3046. return field_iterator(this);
  3047. }
  3048. inline MessageDef::field_iterator MessageDef::field_end() {
  3049. return field_iterator::end(this);
  3050. }
  3051. inline MessageDef::const_field_iterator MessageDef::field_begin() const {
  3052. return const_field_iterator(this);
  3053. }
  3054. inline MessageDef::const_field_iterator MessageDef::field_end() const {
  3055. return const_field_iterator::end(this);
  3056. }
  3057. inline MessageDef::oneof_iterator MessageDef::oneof_begin() {
  3058. return oneof_iterator(this);
  3059. }
  3060. inline MessageDef::oneof_iterator MessageDef::oneof_end() {
  3061. return oneof_iterator::end(this);
  3062. }
  3063. inline MessageDef::const_oneof_iterator MessageDef::oneof_begin() const {
  3064. return const_oneof_iterator(this);
  3065. }
  3066. inline MessageDef::const_oneof_iterator MessageDef::oneof_end() const {
  3067. return const_oneof_iterator::end(this);
  3068. }
  3069. inline MessageDef::field_iterator::field_iterator(MessageDef* md) {
  3070. upb_msg_field_begin(&iter_, md);
  3071. }
  3072. inline MessageDef::field_iterator MessageDef::field_iterator::end(
  3073. MessageDef* md) {
  3074. MessageDef::field_iterator iter(md);
  3075. upb_msg_field_iter_setdone(&iter.iter_);
  3076. return iter;
  3077. }
  3078. inline FieldDef* MessageDef::field_iterator::operator*() const {
  3079. return upb_msg_iter_field(&iter_);
  3080. }
  3081. inline void MessageDef::field_iterator::operator++() {
  3082. return upb_msg_field_next(&iter_);
  3083. }
  3084. inline bool MessageDef::field_iterator::operator==(
  3085. const field_iterator &other) const {
  3086. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  3087. }
  3088. inline bool MessageDef::field_iterator::operator!=(
  3089. const field_iterator &other) const {
  3090. return !(*this == other);
  3091. }
  3092. inline MessageDef::const_field_iterator::const_field_iterator(
  3093. const MessageDef* md) {
  3094. upb_msg_field_begin(&iter_, md);
  3095. }
  3096. inline MessageDef::const_field_iterator MessageDef::const_field_iterator::end(
  3097. const MessageDef *md) {
  3098. MessageDef::const_field_iterator iter(md);
  3099. upb_msg_field_iter_setdone(&iter.iter_);
  3100. return iter;
  3101. }
  3102. inline const FieldDef* MessageDef::const_field_iterator::operator*() const {
  3103. return upb_msg_iter_field(&iter_);
  3104. }
  3105. inline void MessageDef::const_field_iterator::operator++() {
  3106. return upb_msg_field_next(&iter_);
  3107. }
  3108. inline bool MessageDef::const_field_iterator::operator==(
  3109. const const_field_iterator &other) const {
  3110. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  3111. }
  3112. inline bool MessageDef::const_field_iterator::operator!=(
  3113. const const_field_iterator &other) const {
  3114. return !(*this == other);
  3115. }
  3116. inline MessageDef::oneof_iterator::oneof_iterator(MessageDef* md) {
  3117. upb_msg_oneof_begin(&iter_, md);
  3118. }
  3119. inline MessageDef::oneof_iterator MessageDef::oneof_iterator::end(
  3120. MessageDef* md) {
  3121. MessageDef::oneof_iterator iter(md);
  3122. upb_msg_oneof_iter_setdone(&iter.iter_);
  3123. return iter;
  3124. }
  3125. inline OneofDef* MessageDef::oneof_iterator::operator*() const {
  3126. return upb_msg_iter_oneof(&iter_);
  3127. }
  3128. inline void MessageDef::oneof_iterator::operator++() {
  3129. return upb_msg_oneof_next(&iter_);
  3130. }
  3131. inline bool MessageDef::oneof_iterator::operator==(
  3132. const oneof_iterator &other) const {
  3133. return upb_strtable_iter_isequal(&iter_, &other.iter_);
  3134. }
  3135. inline bool MessageDef::oneof_iterator::operator!=(
  3136. const oneof_iterator &other) const {
  3137. return !(*this == other);
  3138. }
  3139. inline MessageDef::const_oneof_iterator::const_oneof_iterator(
  3140. const MessageDef* md) {
  3141. upb_msg_oneof_begin(&iter_, md);
  3142. }
  3143. inline MessageDef::const_oneof_iterator MessageDef::const_oneof_iterator::end(
  3144. const MessageDef *md) {
  3145. MessageDef::const_oneof_iterator iter(md);
  3146. upb_msg_oneof_iter_setdone(&iter.iter_);
  3147. return iter;
  3148. }
  3149. inline const OneofDef* MessageDef::const_oneof_iterator::operator*() const {
  3150. return upb_msg_iter_oneof(&iter_);
  3151. }
  3152. inline void MessageDef::const_oneof_iterator::operator++() {
  3153. return upb_msg_oneof_next(&iter_);
  3154. }
  3155. inline bool MessageDef::const_oneof_iterator::operator==(
  3156. const const_oneof_iterator &other) const {
  3157. return upb_strtable_iter_isequal(&iter_, &other.iter_);
  3158. }
  3159. inline bool MessageDef::const_oneof_iterator::operator!=(
  3160. const const_oneof_iterator &other) const {
  3161. return !(*this == other);
  3162. }
  3163. inline reffed_ptr<EnumDef> EnumDef::New() {
  3164. upb_enumdef *e = upb_enumdef_new(&e);
  3165. return reffed_ptr<EnumDef>(e, &e);
  3166. }
  3167. inline const char* EnumDef::full_name() const {
  3168. return upb_enumdef_fullname(this);
  3169. }
  3170. inline const char* EnumDef::name() const {
  3171. return upb_enumdef_name(this);
  3172. }
  3173. inline bool EnumDef::set_full_name(const char* fullname, Status* s) {
  3174. return upb_enumdef_setfullname(this, fullname, s);
  3175. }
  3176. inline bool EnumDef::set_full_name(const std::string& fullname, Status* s) {
  3177. return upb_enumdef_setfullname(this, upb_safecstr(fullname), s);
  3178. }
  3179. inline bool EnumDef::Freeze(Status* status) {
  3180. return upb_enumdef_freeze(this, status);
  3181. }
  3182. inline int32_t EnumDef::default_value() const {
  3183. return upb_enumdef_default(this);
  3184. }
  3185. inline bool EnumDef::set_default_value(int32_t val, Status* status) {
  3186. return upb_enumdef_setdefault(this, val, status);
  3187. }
  3188. inline int EnumDef::value_count() const { return upb_enumdef_numvals(this); }
  3189. inline bool EnumDef::AddValue(const char* name, int32_t num, Status* status) {
  3190. return upb_enumdef_addval(this, name, num, status);
  3191. }
  3192. inline bool EnumDef::AddValue(const std::string& name, int32_t num,
  3193. Status* status) {
  3194. return upb_enumdef_addval(this, upb_safecstr(name), num, status);
  3195. }
  3196. inline bool EnumDef::FindValueByName(const char* name, int32_t *num) const {
  3197. return upb_enumdef_ntoiz(this, name, num);
  3198. }
  3199. inline const char* EnumDef::FindValueByNumber(int32_t num) const {
  3200. return upb_enumdef_iton(this, num);
  3201. }
  3202. inline EnumDef::Iterator::Iterator(const EnumDef* e) {
  3203. upb_enum_begin(&iter_, e);
  3204. }
  3205. inline int32_t EnumDef::Iterator::number() {
  3206. return upb_enum_iter_number(&iter_);
  3207. }
  3208. inline const char* EnumDef::Iterator::name() {
  3209. return upb_enum_iter_name(&iter_);
  3210. }
  3211. inline bool EnumDef::Iterator::Done() { return upb_enum_done(&iter_); }
  3212. inline void EnumDef::Iterator::Next() { return upb_enum_next(&iter_); }
  3213. inline reffed_ptr<OneofDef> OneofDef::New() {
  3214. upb_oneofdef *o = upb_oneofdef_new(&o);
  3215. return reffed_ptr<OneofDef>(o, &o);
  3216. }
  3217. inline const MessageDef* OneofDef::containing_type() const {
  3218. return upb_oneofdef_containingtype(this);
  3219. }
  3220. inline const char* OneofDef::name() const {
  3221. return upb_oneofdef_name(this);
  3222. }
  3223. inline bool OneofDef::set_name(const char* name, Status* s) {
  3224. return upb_oneofdef_setname(this, name, s);
  3225. }
  3226. inline bool OneofDef::set_name(const std::string& name, Status* s) {
  3227. return upb_oneofdef_setname(this, upb_safecstr(name), s);
  3228. }
  3229. inline int OneofDef::field_count() const {
  3230. return upb_oneofdef_numfields(this);
  3231. }
  3232. inline bool OneofDef::AddField(FieldDef* field, Status* s) {
  3233. return upb_oneofdef_addfield(this, field, NULL, s);
  3234. }
  3235. inline bool OneofDef::AddField(const reffed_ptr<FieldDef>& field, Status* s) {
  3236. return upb_oneofdef_addfield(this, field.get(), NULL, s);
  3237. }
  3238. inline const FieldDef* OneofDef::FindFieldByName(const char* name,
  3239. size_t len) const {
  3240. return upb_oneofdef_ntof(this, name, len);
  3241. }
  3242. inline const FieldDef* OneofDef::FindFieldByNumber(uint32_t num) const {
  3243. return upb_oneofdef_itof(this, num);
  3244. }
  3245. inline OneofDef::iterator OneofDef::begin() { return iterator(this); }
  3246. inline OneofDef::iterator OneofDef::end() { return iterator::end(this); }
  3247. inline OneofDef::const_iterator OneofDef::begin() const {
  3248. return const_iterator(this);
  3249. }
  3250. inline OneofDef::const_iterator OneofDef::end() const {
  3251. return const_iterator::end(this);
  3252. }
  3253. inline OneofDef::iterator::iterator(OneofDef* o) {
  3254. upb_oneof_begin(&iter_, o);
  3255. }
  3256. inline OneofDef::iterator OneofDef::iterator::end(OneofDef* o) {
  3257. OneofDef::iterator iter(o);
  3258. upb_oneof_iter_setdone(&iter.iter_);
  3259. return iter;
  3260. }
  3261. inline FieldDef* OneofDef::iterator::operator*() const {
  3262. return upb_oneof_iter_field(&iter_);
  3263. }
  3264. inline void OneofDef::iterator::operator++() { return upb_oneof_next(&iter_); }
  3265. inline bool OneofDef::iterator::operator==(const iterator &other) const {
  3266. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  3267. }
  3268. inline bool OneofDef::iterator::operator!=(const iterator &other) const {
  3269. return !(*this == other);
  3270. }
  3271. inline OneofDef::const_iterator::const_iterator(const OneofDef* md) {
  3272. upb_oneof_begin(&iter_, md);
  3273. }
  3274. inline OneofDef::const_iterator OneofDef::const_iterator::end(
  3275. const OneofDef *md) {
  3276. OneofDef::const_iterator iter(md);
  3277. upb_oneof_iter_setdone(&iter.iter_);
  3278. return iter;
  3279. }
  3280. inline const FieldDef* OneofDef::const_iterator::operator*() const {
  3281. return upb_msg_iter_field(&iter_);
  3282. }
  3283. inline void OneofDef::const_iterator::operator++() {
  3284. return upb_oneof_next(&iter_);
  3285. }
  3286. inline bool OneofDef::const_iterator::operator==(
  3287. const const_iterator &other) const {
  3288. return upb_inttable_iter_isequal(&iter_, &other.iter_);
  3289. }
  3290. inline bool OneofDef::const_iterator::operator!=(
  3291. const const_iterator &other) const {
  3292. return !(*this == other);
  3293. }
  3294. inline reffed_ptr<FileDef> FileDef::New() {
  3295. upb_filedef *f = upb_filedef_new(&f);
  3296. return reffed_ptr<FileDef>(f, &f);
  3297. }
  3298. inline const char* FileDef::name() const {
  3299. return upb_filedef_name(this);
  3300. }
  3301. inline bool FileDef::set_name(const char* name, Status* s) {
  3302. return upb_filedef_setname(this, name, s);
  3303. }
  3304. inline bool FileDef::set_name(const std::string& name, Status* s) {
  3305. return upb_filedef_setname(this, upb_safecstr(name), s);
  3306. }
  3307. inline const char* FileDef::package() const {
  3308. return upb_filedef_package(this);
  3309. }
  3310. inline bool FileDef::set_package(const char* package, Status* s) {
  3311. return upb_filedef_setpackage(this, package, s);
  3312. }
  3313. inline const char* FileDef::phpprefix() const {
  3314. return upb_filedef_phpprefix(this);
  3315. }
  3316. inline bool FileDef::set_phpprefix(const char* phpprefix, Status* s) {
  3317. return upb_filedef_setphpprefix(this, phpprefix, s);
  3318. }
  3319. inline const char* FileDef::phpnamespace() const {
  3320. return upb_filedef_phpnamespace(this);
  3321. }
  3322. inline bool FileDef::set_phpnamespace(const char* phpnamespace, Status* s) {
  3323. return upb_filedef_setphpnamespace(this, phpnamespace, s);
  3324. }
  3325. inline int FileDef::def_count() const {
  3326. return upb_filedef_defcount(this);
  3327. }
  3328. inline const Def* FileDef::def(int index) const {
  3329. return upb_filedef_def(this, index);
  3330. }
  3331. inline Def* FileDef::def(int index) {
  3332. return const_cast<Def*>(upb_filedef_def(this, index));
  3333. }
  3334. inline int FileDef::dependency_count() const {
  3335. return upb_filedef_depcount(this);
  3336. }
  3337. inline const FileDef* FileDef::dependency(int index) const {
  3338. return upb_filedef_dep(this, index);
  3339. }
  3340. inline bool FileDef::AddDef(Def* def, Status* s) {
  3341. return upb_filedef_adddef(this, def, NULL, s);
  3342. }
  3343. inline bool FileDef::AddMessage(MessageDef* m, Status* s) {
  3344. return upb_filedef_addmsg(this, m, NULL, s);
  3345. }
  3346. inline bool FileDef::AddEnum(EnumDef* e, Status* s) {
  3347. return upb_filedef_addenum(this, e, NULL, s);
  3348. }
  3349. inline bool FileDef::AddExtension(FieldDef* f, Status* s) {
  3350. return upb_filedef_addext(this, f, NULL, s);
  3351. }
  3352. inline bool FileDef::AddDependency(const FileDef* file) {
  3353. return upb_filedef_adddep(this, file);
  3354. }
  3355. } /* namespace upb */
  3356. #endif
  3357. #endif /* UPB_DEF_H_ */
  3358. /*
  3359. ** upb::Handlers (upb_handlers)
  3360. **
  3361. ** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the
  3362. ** message can have associated functions that will be called when we are
  3363. ** parsing or visiting a stream of data. This is similar to how handlers work
  3364. ** in SAX (the Simple API for XML).
  3365. **
  3366. ** The handlers have no idea where the data is coming from, so a single set of
  3367. ** handlers could be used with two completely different data sources (for
  3368. ** example, a parser and a visitor over in-memory objects). This decoupling is
  3369. ** the most important feature of upb, because it allows parsers and serializers
  3370. ** to be highly reusable.
  3371. **
  3372. ** This is a mixed C/C++ interface that offers a full API to both languages.
  3373. ** See the top-level README for more information.
  3374. */
  3375. #ifndef UPB_HANDLERS_H
  3376. #define UPB_HANDLERS_H
  3377. #ifdef __cplusplus
  3378. namespace upb {
  3379. class BufferHandle;
  3380. class BytesHandler;
  3381. class HandlerAttributes;
  3382. class Handlers;
  3383. template <class T> class Handler;
  3384. template <class T> struct CanonicalType;
  3385. } /* namespace upb */
  3386. #endif
  3387. UPB_DECLARE_TYPE(upb::BufferHandle, upb_bufhandle)
  3388. UPB_DECLARE_TYPE(upb::BytesHandler, upb_byteshandler)
  3389. UPB_DECLARE_TYPE(upb::HandlerAttributes, upb_handlerattr)
  3390. UPB_DECLARE_DERIVED_TYPE(upb::Handlers, upb::RefCounted,
  3391. upb_handlers, upb_refcounted)
  3392. /* The maximum depth that the handler graph can have. This is a resource limit
  3393. * for the C stack since we sometimes need to recursively traverse the graph.
  3394. * Cycles are ok; the traversal will stop when it detects a cycle, but we must
  3395. * hit the cycle before the maximum depth is reached.
  3396. *
  3397. * If having a single static limit is too inflexible, we can add another variant
  3398. * of Handlers::Freeze that allows specifying this as a parameter. */
  3399. #define UPB_MAX_HANDLER_DEPTH 64
  3400. /* All the different types of handlers that can be registered.
  3401. * Only needed for the advanced functions in upb::Handlers. */
  3402. typedef enum {
  3403. UPB_HANDLER_INT32,
  3404. UPB_HANDLER_INT64,
  3405. UPB_HANDLER_UINT32,
  3406. UPB_HANDLER_UINT64,
  3407. UPB_HANDLER_FLOAT,
  3408. UPB_HANDLER_DOUBLE,
  3409. UPB_HANDLER_BOOL,
  3410. UPB_HANDLER_STARTSTR,
  3411. UPB_HANDLER_STRING,
  3412. UPB_HANDLER_ENDSTR,
  3413. UPB_HANDLER_STARTSUBMSG,
  3414. UPB_HANDLER_ENDSUBMSG,
  3415. UPB_HANDLER_STARTSEQ,
  3416. UPB_HANDLER_ENDSEQ
  3417. } upb_handlertype_t;
  3418. #define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
  3419. #define UPB_BREAK NULL
  3420. /* A convenient definition for when no closure is needed. */
  3421. extern char _upb_noclosure;
  3422. #define UPB_NO_CLOSURE &_upb_noclosure
  3423. /* A selector refers to a specific field handler in the Handlers object
  3424. * (for example: the STARTSUBMSG handler for field "field15"). */
  3425. typedef int32_t upb_selector_t;
  3426. UPB_BEGIN_EXTERN_C
  3427. /* Forward-declares for C inline accessors. We need to declare these here
  3428. * so we can "friend" them in the class declarations in C++. */
  3429. UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
  3430. upb_selector_t s);
  3431. UPB_INLINE const void *upb_handlerattr_handlerdata(const upb_handlerattr *attr);
  3432. UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
  3433. upb_selector_t s);
  3434. UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h);
  3435. UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
  3436. const void *type);
  3437. UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
  3438. size_t ofs);
  3439. UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h);
  3440. UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h);
  3441. UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h);
  3442. UPB_END_EXTERN_C
  3443. /* Static selectors for upb::Handlers. */
  3444. #define UPB_STARTMSG_SELECTOR 0
  3445. #define UPB_ENDMSG_SELECTOR 1
  3446. #define UPB_UNKNOWN_SELECTOR 2
  3447. #define UPB_STATIC_SELECTOR_COUNT 3
  3448. /* Static selectors for upb::BytesHandler. */
  3449. #define UPB_STARTSTR_SELECTOR 0
  3450. #define UPB_STRING_SELECTOR 1
  3451. #define UPB_ENDSTR_SELECTOR 2
  3452. typedef void upb_handlerfree(void *d);
  3453. #ifdef __cplusplus
  3454. /* A set of attributes that accompanies a handler's function pointer. */
  3455. class upb::HandlerAttributes {
  3456. public:
  3457. HandlerAttributes();
  3458. ~HandlerAttributes();
  3459. /* Sets the handler data that will be passed as the second parameter of the
  3460. * handler. To free this pointer when the handlers are freed, call
  3461. * Handlers::AddCleanup(). */
  3462. bool SetHandlerData(const void *handler_data);
  3463. const void* handler_data() const;
  3464. /* Use this to specify the type of the closure. This will be checked against
  3465. * all other closure types for handler that use the same closure.
  3466. * Registration will fail if this does not match all other non-NULL closure
  3467. * types. */
  3468. bool SetClosureType(const void *closure_type);
  3469. const void* closure_type() const;
  3470. /* Use this to specify the type of the returned closure. Only used for
  3471. * Start*{String,SubMessage,Sequence} handlers. This must match the closure
  3472. * type of any handlers that use it (for example, the StringBuf handler must
  3473. * match the closure returned from StartString). */
  3474. bool SetReturnClosureType(const void *return_closure_type);
  3475. const void* return_closure_type() const;
  3476. /* Set to indicate that the handler always returns "ok" (either "true" or a
  3477. * non-NULL closure). This is a hint that can allow code generators to
  3478. * generate more efficient code. */
  3479. bool SetAlwaysOk(bool always_ok);
  3480. bool always_ok() const;
  3481. private:
  3482. friend UPB_INLINE const void * ::upb_handlerattr_handlerdata(
  3483. const upb_handlerattr *attr);
  3484. #else
  3485. struct upb_handlerattr {
  3486. #endif
  3487. const void *handler_data_;
  3488. const void *closure_type_;
  3489. const void *return_closure_type_;
  3490. bool alwaysok_;
  3491. };
  3492. #define UPB_HANDLERATTR_INITIALIZER {NULL, NULL, NULL, false}
  3493. typedef struct {
  3494. upb_func *func;
  3495. /* It is wasteful to include the entire attributes here:
  3496. *
  3497. * * Some of the information is redundant (like storing the closure type
  3498. * separately for each handler that must match).
  3499. * * Some of the info is only needed prior to freeze() (like closure types).
  3500. * * alignment padding wastes a lot of space for alwaysok_.
  3501. *
  3502. * If/when the size and locality of handlers is an issue, we can optimize this
  3503. * not to store the entire attr like this. We do not expose the table's
  3504. * layout to allow this optimization in the future. */
  3505. upb_handlerattr attr;
  3506. } upb_handlers_tabent;
  3507. #ifdef __cplusplus
  3508. /* Extra information about a buffer that is passed to a StringBuf handler.
  3509. * TODO(haberman): allow the handle to be pinned so that it will outlive
  3510. * the handler invocation. */
  3511. class upb::BufferHandle {
  3512. public:
  3513. BufferHandle();
  3514. ~BufferHandle();
  3515. /* The beginning of the buffer. This may be different than the pointer
  3516. * passed to a StringBuf handler because the handler may receive data
  3517. * that is from the middle or end of a larger buffer. */
  3518. const char* buffer() const;
  3519. /* The offset within the attached object where this buffer begins. Only
  3520. * meaningful if there is an attached object. */
  3521. size_t object_offset() const;
  3522. /* Note that object_offset is the offset of "buf" within the attached
  3523. * object. */
  3524. void SetBuffer(const char* buf, size_t object_offset);
  3525. /* The BufferHandle can have an "attached object", which can be used to
  3526. * tunnel through a pointer to the buffer's underlying representation. */
  3527. template <class T>
  3528. void SetAttachedObject(const T* obj);
  3529. /* Returns NULL if the attached object is not of this type. */
  3530. template <class T>
  3531. const T* GetAttachedObject() const;
  3532. private:
  3533. friend UPB_INLINE void ::upb_bufhandle_init(upb_bufhandle *h);
  3534. friend UPB_INLINE void ::upb_bufhandle_setobj(upb_bufhandle *h,
  3535. const void *obj,
  3536. const void *type);
  3537. friend UPB_INLINE void ::upb_bufhandle_setbuf(upb_bufhandle *h,
  3538. const char *buf, size_t ofs);
  3539. friend UPB_INLINE const void* ::upb_bufhandle_obj(const upb_bufhandle *h);
  3540. friend UPB_INLINE const void* ::upb_bufhandle_objtype(
  3541. const upb_bufhandle *h);
  3542. friend UPB_INLINE const char* ::upb_bufhandle_buf(const upb_bufhandle *h);
  3543. #else
  3544. struct upb_bufhandle {
  3545. #endif
  3546. const char *buf_;
  3547. const void *obj_;
  3548. const void *objtype_;
  3549. size_t objofs_;
  3550. };
  3551. #ifdef __cplusplus
  3552. /* A upb::Handlers object represents the set of handlers associated with a
  3553. * message in the graph of messages. You can think of it as a big virtual
  3554. * table with functions corresponding to all the events that can fire while
  3555. * parsing or visiting a message of a specific type.
  3556. *
  3557. * Any handlers that are not set behave as if they had successfully consumed
  3558. * the value. Any unset Start* handlers will propagate their closure to the
  3559. * inner frame.
  3560. *
  3561. * The easiest way to create the *Handler objects needed by the Set* methods is
  3562. * with the UpbBind() and UpbMakeHandler() macros; see below. */
  3563. class upb::Handlers {
  3564. public:
  3565. typedef upb_selector_t Selector;
  3566. typedef upb_handlertype_t Type;
  3567. typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
  3568. typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
  3569. typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
  3570. typedef Handler<bool (*)(void *, const void *, Status*)> EndMessageHandler;
  3571. typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
  3572. typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
  3573. const BufferHandle *)> StringHandler;
  3574. template <class T> struct ValueHandler {
  3575. typedef Handler<bool(*)(void *, const void *, T)> H;
  3576. };
  3577. typedef ValueHandler<int32_t>::H Int32Handler;
  3578. typedef ValueHandler<int64_t>::H Int64Handler;
  3579. typedef ValueHandler<uint32_t>::H UInt32Handler;
  3580. typedef ValueHandler<uint64_t>::H UInt64Handler;
  3581. typedef ValueHandler<float>::H FloatHandler;
  3582. typedef ValueHandler<double>::H DoubleHandler;
  3583. typedef ValueHandler<bool>::H BoolHandler;
  3584. /* Any function pointer can be converted to this and converted back to its
  3585. * correct type. */
  3586. typedef void GenericFunction();
  3587. typedef void HandlersCallback(const void *closure, upb_handlers *h);
  3588. /* Returns a new handlers object for the given frozen msgdef.
  3589. * Returns NULL if memory allocation failed. */
  3590. static reffed_ptr<Handlers> New(const MessageDef *m);
  3591. /* Convenience function for registering a graph of handlers that mirrors the
  3592. * graph of msgdefs for some message. For "m" and all its children a new set
  3593. * of handlers will be created and the given callback will be invoked,
  3594. * allowing the client to register handlers for this message. Note that any
  3595. * subhandlers set by the callback will be overwritten. */
  3596. static reffed_ptr<const Handlers> NewFrozen(const MessageDef *m,
  3597. HandlersCallback *callback,
  3598. const void *closure);
  3599. /* Functionality from upb::RefCounted. */
  3600. UPB_REFCOUNTED_CPPMETHODS
  3601. /* All handler registration functions return bool to indicate success or
  3602. * failure; details about failures are stored in this status object. If a
  3603. * failure does occur, it must be cleared before the Handlers are frozen,
  3604. * otherwise the freeze() operation will fail. The functions may *only* be
  3605. * used while the Handlers are mutable. */
  3606. const Status* status();
  3607. void ClearError();
  3608. /* Call to freeze these Handlers. Requires that any SubHandlers are already
  3609. * frozen. For cycles, you must use the static version below and freeze the
  3610. * whole graph at once. */
  3611. bool Freeze(Status* s);
  3612. /* Freezes the given set of handlers. You may not freeze a handler without
  3613. * also freezing any handlers they point to. */
  3614. static bool Freeze(Handlers*const* handlers, int n, Status* s);
  3615. static bool Freeze(const std::vector<Handlers*>& handlers, Status* s);
  3616. /* Returns the msgdef associated with this handlers object. */
  3617. const MessageDef* message_def() const;
  3618. /* Adds the given pointer and function to the list of cleanup functions that
  3619. * will be run when these handlers are freed. If this pointer has previously
  3620. * been registered, the function returns false and does nothing. */
  3621. bool AddCleanup(void *ptr, upb_handlerfree *cleanup);
  3622. /* Sets the startmsg handler for the message, which is defined as follows:
  3623. *
  3624. * bool startmsg(MyType* closure) {
  3625. * // Called when the message begins. Returns true if processing should
  3626. * // continue.
  3627. * return true;
  3628. * }
  3629. */
  3630. bool SetStartMessageHandler(const StartMessageHandler& handler);
  3631. /* Sets the endmsg handler for the message, which is defined as follows:
  3632. *
  3633. * bool endmsg(MyType* closure, upb_status *status) {
  3634. * // Called when processing of this message ends, whether in success or
  3635. * // failure. "status" indicates the final status of processing, and
  3636. * // can also be modified in-place to update the final status.
  3637. * }
  3638. */
  3639. bool SetEndMessageHandler(const EndMessageHandler& handler);
  3640. /* Sets the value handler for the given field, which is defined as follows
  3641. * (this is for an int32 field; other field types will pass their native
  3642. * C/C++ type for "val"):
  3643. *
  3644. * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3645. * // Called when the field's value is encountered. "d" contains
  3646. * // whatever data was bound to this field when it was registered.
  3647. * // Returns true if processing should continue.
  3648. * return true;
  3649. * }
  3650. *
  3651. * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
  3652. *
  3653. * The value type must exactly match f->type().
  3654. * For example, a handler that takes an int32_t parameter may only be used for
  3655. * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
  3656. *
  3657. * Returns false if the handler failed to register; in this case the cleanup
  3658. * handler (if any) will be called immediately.
  3659. */
  3660. bool SetInt32Handler (const FieldDef* f, const Int32Handler& h);
  3661. bool SetInt64Handler (const FieldDef* f, const Int64Handler& h);
  3662. bool SetUInt32Handler(const FieldDef* f, const UInt32Handler& h);
  3663. bool SetUInt64Handler(const FieldDef* f, const UInt64Handler& h);
  3664. bool SetFloatHandler (const FieldDef* f, const FloatHandler& h);
  3665. bool SetDoubleHandler(const FieldDef* f, const DoubleHandler& h);
  3666. bool SetBoolHandler (const FieldDef* f, const BoolHandler& h);
  3667. /* Like the previous, but templated on the type on the value (ie. int32).
  3668. * This is mostly useful to call from other templates. To call this you must
  3669. * specify the template parameter explicitly, ie:
  3670. * h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */
  3671. template <class T>
  3672. bool SetValueHandler(
  3673. const FieldDef *f,
  3674. const typename ValueHandler<typename CanonicalType<T>::Type>::H& handler);
  3675. /* Sets handlers for a string field, which are defined as follows:
  3676. *
  3677. * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
  3678. * size_t size_hint) {
  3679. * // Called when a string value begins. The return value indicates the
  3680. * // closure for the string. "size_hint" indicates the size of the
  3681. * // string if it is known, however if the string is length-delimited
  3682. * // and the end-of-string is not available size_hint will be zero.
  3683. * // This case is indistinguishable from the case where the size is
  3684. * // known to be zero.
  3685. * //
  3686. * // TODO(haberman): is it important to distinguish these cases?
  3687. * // If we had ssize_t as a type we could make -1 "unknown", but
  3688. * // ssize_t is POSIX (not ANSI) and therefore less portable.
  3689. * // In practice I suspect it won't be important to distinguish.
  3690. * return closure;
  3691. * }
  3692. *
  3693. * size_t str(MyClosure* closure, const MyHandlerData* d,
  3694. * const char *str, size_t len) {
  3695. * // Called for each buffer of string data; the multiple physical buffers
  3696. * // are all part of the same logical string. The return value indicates
  3697. * // how many bytes were consumed. If this number is less than "len",
  3698. * // this will also indicate that processing should be halted for now,
  3699. * // like returning false or UPB_BREAK from any other callback. If
  3700. * // number is greater than "len", the excess bytes will be skipped over
  3701. * // and not passed to the callback.
  3702. * return len;
  3703. * }
  3704. *
  3705. * bool endstr(MyClosure* c, const MyHandlerData* d) {
  3706. * // Called when a string value ends. Return value indicates whether
  3707. * // processing should continue.
  3708. * return true;
  3709. * }
  3710. */
  3711. bool SetStartStringHandler(const FieldDef* f, const StartStringHandler& h);
  3712. bool SetStringHandler(const FieldDef* f, const StringHandler& h);
  3713. bool SetEndStringHandler(const FieldDef* f, const EndFieldHandler& h);
  3714. /* Sets the startseq handler, which is defined as follows:
  3715. *
  3716. * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
  3717. * // Called when a sequence (repeated field) begins. The returned
  3718. * // pointer indicates the closure for the sequence (or UPB_BREAK
  3719. * // to interrupt processing).
  3720. * return closure;
  3721. * }
  3722. *
  3723. * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
  3724. *
  3725. * Returns "false" if "f" does not belong to this message or is not a
  3726. * repeated field.
  3727. */
  3728. bool SetStartSequenceHandler(const FieldDef* f, const StartFieldHandler& h);
  3729. /* Sets the startsubmsg handler for the given field, which is defined as
  3730. * follows:
  3731. *
  3732. * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
  3733. * // Called when a submessage begins. The returned pointer indicates the
  3734. * // closure for the sequence (or UPB_BREAK to interrupt processing).
  3735. * return closure;
  3736. * }
  3737. *
  3738. * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
  3739. * new MyHandlerData(...)));
  3740. *
  3741. * Returns "false" if "f" does not belong to this message or is not a
  3742. * submessage/group field.
  3743. */
  3744. bool SetStartSubMessageHandler(const FieldDef* f, const StartFieldHandler& h);
  3745. /* Sets the endsubmsg handler for the given field, which is defined as
  3746. * follows:
  3747. *
  3748. * bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
  3749. * // Called when a submessage ends. Returns true to continue processing.
  3750. * return true;
  3751. * }
  3752. *
  3753. * Returns "false" if "f" does not belong to this message or is not a
  3754. * submessage/group field.
  3755. */
  3756. bool SetEndSubMessageHandler(const FieldDef *f, const EndFieldHandler &h);
  3757. /* Starts the endsubseq handler for the given field, which is defined as
  3758. * follows:
  3759. *
  3760. * bool endseq(MyClosure* c, const MyHandlerData* d) {
  3761. * // Called when a sequence ends. Returns true continue processing.
  3762. * return true;
  3763. * }
  3764. *
  3765. * Returns "false" if "f" does not belong to this message or is not a
  3766. * repeated field.
  3767. */
  3768. bool SetEndSequenceHandler(const FieldDef* f, const EndFieldHandler& h);
  3769. /* Sets or gets the object that specifies handlers for the given field, which
  3770. * must be a submessage or group. Returns NULL if no handlers are set. */
  3771. bool SetSubHandlers(const FieldDef* f, const Handlers* sub);
  3772. const Handlers* GetSubHandlers(const FieldDef* f) const;
  3773. /* Equivalent to GetSubHandlers, but takes the STARTSUBMSG selector for the
  3774. * field. */
  3775. const Handlers* GetSubHandlers(Selector startsubmsg) const;
  3776. /* A selector refers to a specific field handler in the Handlers object
  3777. * (for example: the STARTSUBMSG handler for field "field15").
  3778. * On success, returns true and stores the selector in "s".
  3779. * If the FieldDef or Type are invalid, returns false.
  3780. * The returned selector is ONLY valid for Handlers whose MessageDef
  3781. * contains this FieldDef. */
  3782. static bool GetSelector(const FieldDef* f, Type type, Selector* s);
  3783. /* Given a START selector of any kind, returns the corresponding END selector. */
  3784. static Selector GetEndSelector(Selector start_selector);
  3785. /* Returns the function pointer for this handler. It is the client's
  3786. * responsibility to cast to the correct function type before calling it. */
  3787. GenericFunction* GetHandler(Selector selector);
  3788. /* Sets the given attributes to the attributes for this selector. */
  3789. bool GetAttributes(Selector selector, HandlerAttributes* attr);
  3790. /* Returns the handler data that was registered with this handler. */
  3791. const void* GetHandlerData(Selector selector);
  3792. /* Could add any of the following functions as-needed, with some minor
  3793. * implementation changes:
  3794. *
  3795. * const FieldDef* GetFieldDef(Selector selector);
  3796. * static bool IsSequence(Selector selector); */
  3797. private:
  3798. UPB_DISALLOW_POD_OPS(Handlers, upb::Handlers)
  3799. friend UPB_INLINE GenericFunction *::upb_handlers_gethandler(
  3800. const upb_handlers *h, upb_selector_t s);
  3801. friend UPB_INLINE const void *::upb_handlers_gethandlerdata(
  3802. const upb_handlers *h, upb_selector_t s);
  3803. #else
  3804. struct upb_handlers {
  3805. #endif
  3806. upb_refcounted base;
  3807. const upb_msgdef *msg;
  3808. const upb_handlers **sub;
  3809. const void *top_closure_type;
  3810. upb_inttable cleanup_;
  3811. upb_status status_; /* Used only when mutable. */
  3812. upb_handlers_tabent table[1]; /* Dynamically-sized field handler array. */
  3813. };
  3814. #ifdef __cplusplus
  3815. namespace upb {
  3816. /* Convenience macros for creating a Handler object that is wrapped with a
  3817. * type-safe wrapper function that converts the "void*" parameters/returns
  3818. * of the underlying C API into nice C++ function.
  3819. *
  3820. * Sample usage:
  3821. * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3822. * // do stuff ...
  3823. * }
  3824. *
  3825. * // Handler that doesn't need any data bound to it.
  3826. * void OnValue2(MyClosure* c, int32_t val) {
  3827. * // do stuff ...
  3828. * }
  3829. *
  3830. * // Handler that returns bool so it can return failure if necessary.
  3831. * bool OnValue3(MyClosure* c, int32_t val) {
  3832. * // do stuff ...
  3833. * return ok;
  3834. * }
  3835. *
  3836. * // Member function handler.
  3837. * class MyClosure {
  3838. * public:
  3839. * void OnValue(int32_t val) {
  3840. * // do stuff ...
  3841. * }
  3842. * };
  3843. *
  3844. * // Takes ownership of the MyHandlerData.
  3845. * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
  3846. * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
  3847. * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
  3848. * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
  3849. */
  3850. #ifdef UPB_CXX11
  3851. /* In C++11, the "template" disambiguator can appear even outside templates,
  3852. * so all calls can safely use this pair of macros. */
  3853. #define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
  3854. /* We have to be careful to only evaluate "d" once. */
  3855. #define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
  3856. #else
  3857. /* Prior to C++11, the "template" disambiguator may only appear inside a
  3858. * template, so the regular macro must not use "template" */
  3859. #define UpbMakeHandler(f) upb::MatchFunc(f).GetFunc<f>()
  3860. #define UpbBind(f, d) upb::MatchFunc(f).GetFunc<f>((d))
  3861. #endif /* UPB_CXX11 */
  3862. /* This macro must be used in C++98 for calls from inside a template. But we
  3863. * define this variant in all cases; code that wants to be compatible with both
  3864. * C++98 and C++11 should always use this macro when calling from a template. */
  3865. #define UpbMakeHandlerT(f) upb::MatchFunc(f).template GetFunc<f>()
  3866. /* We have to be careful to only evaluate "d" once. */
  3867. #define UpbBindT(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
  3868. /* Handler: a struct that contains the (handler, data, deleter) tuple that is
  3869. * used to register all handlers. Users can Make() these directly but it's
  3870. * more convenient to use the UpbMakeHandler/UpbBind macros above. */
  3871. template <class T> class Handler {
  3872. public:
  3873. /* The underlying, handler function signature that upb uses internally. */
  3874. typedef T FuncPtr;
  3875. /* Intentionally implicit. */
  3876. template <class F> Handler(F func);
  3877. ~Handler();
  3878. private:
  3879. void AddCleanup(Handlers* h) const {
  3880. if (cleanup_func_) {
  3881. bool ok = h->AddCleanup(cleanup_data_, cleanup_func_);
  3882. UPB_ASSERT(ok);
  3883. }
  3884. }
  3885. UPB_DISALLOW_COPY_AND_ASSIGN(Handler)
  3886. friend class Handlers;
  3887. FuncPtr handler_;
  3888. mutable HandlerAttributes attr_;
  3889. mutable bool registered_;
  3890. void *cleanup_data_;
  3891. upb_handlerfree *cleanup_func_;
  3892. };
  3893. } /* namespace upb */
  3894. #endif /* __cplusplus */
  3895. UPB_BEGIN_EXTERN_C
  3896. /* Native C API. */
  3897. /* Handler function typedefs. */
  3898. typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf,
  3899. size_t n);
  3900. typedef bool upb_startmsg_handlerfunc(void *c, const void*);
  3901. typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
  3902. typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
  3903. typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
  3904. typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
  3905. typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
  3906. typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
  3907. typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
  3908. typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
  3909. typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
  3910. typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
  3911. typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
  3912. size_t size_hint);
  3913. typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
  3914. size_t n, const upb_bufhandle* handle);
  3915. /* upb_bufhandle */
  3916. size_t upb_bufhandle_objofs(const upb_bufhandle *h);
  3917. /* upb_handlerattr */
  3918. void upb_handlerattr_init(upb_handlerattr *attr);
  3919. void upb_handlerattr_uninit(upb_handlerattr *attr);
  3920. bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd);
  3921. bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type);
  3922. const void *upb_handlerattr_closuretype(const upb_handlerattr *attr);
  3923. bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
  3924. const void *type);
  3925. const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr);
  3926. bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok);
  3927. bool upb_handlerattr_alwaysok(const upb_handlerattr *attr);
  3928. UPB_INLINE const void *upb_handlerattr_handlerdata(
  3929. const upb_handlerattr *attr) {
  3930. return attr->handler_data_;
  3931. }
  3932. /* upb_handlers */
  3933. typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
  3934. upb_handlers *upb_handlers_new(const upb_msgdef *m,
  3935. const void *owner);
  3936. const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
  3937. const void *owner,
  3938. upb_handlers_callback *callback,
  3939. const void *closure);
  3940. /* Include refcounted methods like upb_handlers_ref(). */
  3941. UPB_REFCOUNTED_CMETHODS(upb_handlers, upb_handlers_upcast)
  3942. const upb_status *upb_handlers_status(upb_handlers *h);
  3943. void upb_handlers_clearerr(upb_handlers *h);
  3944. const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
  3945. bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
  3946. bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func,
  3947. upb_handlerattr *attr);
  3948. bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
  3949. upb_handlerattr *attr);
  3950. bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
  3951. upb_handlerattr *attr);
  3952. bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
  3953. upb_int32_handlerfunc *func, upb_handlerattr *attr);
  3954. bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
  3955. upb_int64_handlerfunc *func, upb_handlerattr *attr);
  3956. bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
  3957. upb_uint32_handlerfunc *func,
  3958. upb_handlerattr *attr);
  3959. bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
  3960. upb_uint64_handlerfunc *func,
  3961. upb_handlerattr *attr);
  3962. bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
  3963. upb_float_handlerfunc *func, upb_handlerattr *attr);
  3964. bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
  3965. upb_double_handlerfunc *func,
  3966. upb_handlerattr *attr);
  3967. bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
  3968. upb_bool_handlerfunc *func,
  3969. upb_handlerattr *attr);
  3970. bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
  3971. upb_startstr_handlerfunc *func,
  3972. upb_handlerattr *attr);
  3973. bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
  3974. upb_string_handlerfunc *func,
  3975. upb_handlerattr *attr);
  3976. bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
  3977. upb_endfield_handlerfunc *func,
  3978. upb_handlerattr *attr);
  3979. bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
  3980. upb_startfield_handlerfunc *func,
  3981. upb_handlerattr *attr);
  3982. bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
  3983. upb_startfield_handlerfunc *func,
  3984. upb_handlerattr *attr);
  3985. bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
  3986. upb_endfield_handlerfunc *func,
  3987. upb_handlerattr *attr);
  3988. bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
  3989. upb_endfield_handlerfunc *func,
  3990. upb_handlerattr *attr);
  3991. bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
  3992. const upb_handlers *sub);
  3993. const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
  3994. const upb_fielddef *f);
  3995. const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
  3996. upb_selector_t sel);
  3997. UPB_INLINE upb_func *upb_handlers_gethandler(const upb_handlers *h,
  3998. upb_selector_t s) {
  3999. return (upb_func *)h->table[s].func;
  4000. }
  4001. bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
  4002. upb_handlerattr *attr);
  4003. UPB_INLINE const void *upb_handlers_gethandlerdata(const upb_handlers *h,
  4004. upb_selector_t s) {
  4005. return upb_handlerattr_handlerdata(&h->table[s].attr);
  4006. }
  4007. #ifdef __cplusplus
  4008. /* Handler types for single fields.
  4009. * Right now we only have one for TYPE_BYTES but ones for other types
  4010. * should follow.
  4011. *
  4012. * These follow the same handlers protocol for fields of a message. */
  4013. class upb::BytesHandler {
  4014. public:
  4015. BytesHandler();
  4016. ~BytesHandler();
  4017. #else
  4018. struct upb_byteshandler {
  4019. #endif
  4020. upb_handlers_tabent table[3];
  4021. };
  4022. void upb_byteshandler_init(upb_byteshandler *h);
  4023. /* Caller must ensure that "d" outlives the handlers.
  4024. * TODO(haberman): should this have a "freeze" operation? It's not necessary
  4025. * for memory management, but could be useful to force immutability and provide
  4026. * a convenient moment to verify that all registration succeeded. */
  4027. bool upb_byteshandler_setstartstr(upb_byteshandler *h,
  4028. upb_startstr_handlerfunc *func, void *d);
  4029. bool upb_byteshandler_setstring(upb_byteshandler *h,
  4030. upb_string_handlerfunc *func, void *d);
  4031. bool upb_byteshandler_setendstr(upb_byteshandler *h,
  4032. upb_endfield_handlerfunc *func, void *d);
  4033. /* "Static" methods */
  4034. bool upb_handlers_freeze(upb_handlers *const *handlers, int n, upb_status *s);
  4035. upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
  4036. bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
  4037. upb_selector_t *s);
  4038. UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
  4039. return start + 1;
  4040. }
  4041. /* Internal-only. */
  4042. uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
  4043. uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
  4044. /** Message handlers ******************************************************************/
  4045. /* These are the handlers used internally by upb_msgfactory_getmergehandlers().
  4046. * They write scalar data to a known offset from the message pointer.
  4047. *
  4048. * These would be trivial for anyone to implement themselves, but it's better
  4049. * to use these because some JITs will recognize and specialize these instead
  4050. * of actually calling the function. */
  4051. /* Sets a handler for the given primitive field that will write the data at the
  4052. * given offset. If hasbit > 0, also sets a hasbit at the given bit offset
  4053. * (addressing each byte low to high). */
  4054. bool upb_msg_setscalarhandler(upb_handlers *h,
  4055. const upb_fielddef *f,
  4056. size_t offset,
  4057. int32_t hasbit);
  4058. /* If the given handler is a msghandlers_primitive field, returns true and sets
  4059. * *type, *offset and *hasbit. Otherwise returns false. */
  4060. bool upb_msg_getscalarhandlerdata(const upb_handlers *h,
  4061. upb_selector_t s,
  4062. upb_fieldtype_t *type,
  4063. size_t *offset,
  4064. int32_t *hasbit);
  4065. UPB_END_EXTERN_C
  4066. /*
  4067. ** Inline definitions for handlers.h, which are particularly long and a bit
  4068. ** tricky.
  4069. */
  4070. #ifndef UPB_HANDLERS_INL_H_
  4071. #define UPB_HANDLERS_INL_H_
  4072. #include <limits.h>
  4073. /* C inline methods. */
  4074. /* upb_bufhandle */
  4075. UPB_INLINE void upb_bufhandle_init(upb_bufhandle *h) {
  4076. h->obj_ = NULL;
  4077. h->objtype_ = NULL;
  4078. h->buf_ = NULL;
  4079. h->objofs_ = 0;
  4080. }
  4081. UPB_INLINE void upb_bufhandle_uninit(upb_bufhandle *h) {
  4082. UPB_UNUSED(h);
  4083. }
  4084. UPB_INLINE void upb_bufhandle_setobj(upb_bufhandle *h, const void *obj,
  4085. const void *type) {
  4086. h->obj_ = obj;
  4087. h->objtype_ = type;
  4088. }
  4089. UPB_INLINE void upb_bufhandle_setbuf(upb_bufhandle *h, const char *buf,
  4090. size_t ofs) {
  4091. h->buf_ = buf;
  4092. h->objofs_ = ofs;
  4093. }
  4094. UPB_INLINE const void *upb_bufhandle_obj(const upb_bufhandle *h) {
  4095. return h->obj_;
  4096. }
  4097. UPB_INLINE const void *upb_bufhandle_objtype(const upb_bufhandle *h) {
  4098. return h->objtype_;
  4099. }
  4100. UPB_INLINE const char *upb_bufhandle_buf(const upb_bufhandle *h) {
  4101. return h->buf_;
  4102. }
  4103. #ifdef __cplusplus
  4104. /* Type detection and typedefs for integer types.
  4105. * For platforms where there are multiple 32-bit or 64-bit types, we need to be
  4106. * able to enumerate them so we can properly create overloads for all variants.
  4107. *
  4108. * If any platform existed where there were three integer types with the same
  4109. * size, this would have to become more complicated. For example, short, int,
  4110. * and long could all be 32-bits. Even more diabolically, short, int, long,
  4111. * and long long could all be 64 bits and still be standard-compliant.
  4112. * However, few platforms are this strange, and it's unlikely that upb will be
  4113. * used on the strangest ones. */
  4114. /* Can't count on stdint.h limits like INT32_MAX, because in C++ these are
  4115. * only defined when __STDC_LIMIT_MACROS are defined before the *first* include
  4116. * of stdint.h. We can't guarantee that someone else didn't include these first
  4117. * without defining __STDC_LIMIT_MACROS. */
  4118. #define UPB_INT32_MAX 0x7fffffffLL
  4119. #define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
  4120. #define UPB_INT64_MAX 0x7fffffffffffffffLL
  4121. #define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
  4122. #if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
  4123. #define UPB_INT_IS_32BITS 1
  4124. #endif
  4125. #if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
  4126. #define UPB_LONG_IS_32BITS 1
  4127. #endif
  4128. #if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
  4129. #define UPB_LONG_IS_64BITS 1
  4130. #endif
  4131. #if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
  4132. #define UPB_LLONG_IS_64BITS 1
  4133. #endif
  4134. /* We use macros instead of typedefs so we can undefine them later and avoid
  4135. * leaking them outside this header file. */
  4136. #if UPB_INT_IS_32BITS
  4137. #define UPB_INT32_T int
  4138. #define UPB_UINT32_T unsigned int
  4139. #if UPB_LONG_IS_32BITS
  4140. #define UPB_TWO_32BIT_TYPES 1
  4141. #define UPB_INT32ALT_T long
  4142. #define UPB_UINT32ALT_T unsigned long
  4143. #endif /* UPB_LONG_IS_32BITS */
  4144. #elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */
  4145. #define UPB_INT32_T long
  4146. #define UPB_UINT32_T unsigned long
  4147. #endif /* UPB_INT_IS_32BITS */
  4148. #if UPB_LONG_IS_64BITS
  4149. #define UPB_INT64_T long
  4150. #define UPB_UINT64_T unsigned long
  4151. #if UPB_LLONG_IS_64BITS
  4152. #define UPB_TWO_64BIT_TYPES 1
  4153. #define UPB_INT64ALT_T long long
  4154. #define UPB_UINT64ALT_T unsigned long long
  4155. #endif /* UPB_LLONG_IS_64BITS */
  4156. #elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */
  4157. #define UPB_INT64_T long long
  4158. #define UPB_UINT64_T unsigned long long
  4159. #endif /* UPB_LONG_IS_64BITS */
  4160. #undef UPB_INT32_MAX
  4161. #undef UPB_INT32_MIN
  4162. #undef UPB_INT64_MAX
  4163. #undef UPB_INT64_MIN
  4164. #undef UPB_INT_IS_32BITS
  4165. #undef UPB_LONG_IS_32BITS
  4166. #undef UPB_LONG_IS_64BITS
  4167. #undef UPB_LLONG_IS_64BITS
  4168. namespace upb {
  4169. typedef void CleanupFunc(void *ptr);
  4170. /* Template to remove "const" from "const T*" and just return "T*".
  4171. *
  4172. * We define a nonsense default because otherwise it will fail to instantiate as
  4173. * a function parameter type even in cases where we don't expect any caller to
  4174. * actually match the overload. */
  4175. class CouldntRemoveConst {};
  4176. template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
  4177. template <class T> struct remove_constptr<const T *> { typedef T *type; };
  4178. /* Template that we use below to remove a template specialization from
  4179. * consideration if it matches a specific type. */
  4180. template <class T, class U> struct disable_if_same { typedef void Type; };
  4181. template <class T> struct disable_if_same<T, T> {};
  4182. template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
  4183. template <class T1, class T2>
  4184. struct FirstUnlessVoidOrBool {
  4185. typedef T1 value;
  4186. };
  4187. template <class T2>
  4188. struct FirstUnlessVoidOrBool<void, T2> {
  4189. typedef T2 value;
  4190. };
  4191. template <class T2>
  4192. struct FirstUnlessVoidOrBool<bool, T2> {
  4193. typedef T2 value;
  4194. };
  4195. template<class T, class U>
  4196. struct is_same {
  4197. static bool value;
  4198. };
  4199. template<class T>
  4200. struct is_same<T, T> {
  4201. static bool value;
  4202. };
  4203. template<class T, class U>
  4204. bool is_same<T, U>::value = false;
  4205. template<class T>
  4206. bool is_same<T, T>::value = true;
  4207. /* FuncInfo *******************************************************************/
  4208. /* Info about the user's original, pre-wrapped function. */
  4209. template <class C, class R = void>
  4210. struct FuncInfo {
  4211. /* The type of the closure that the function takes (its first param). */
  4212. typedef C Closure;
  4213. /* The return type. */
  4214. typedef R Return;
  4215. };
  4216. /* Func ***********************************************************************/
  4217. /* Func1, Func2, Func3: Template classes representing a function and its
  4218. * signature.
  4219. *
  4220. * Since the function is a template parameter, calling the function can be
  4221. * inlined at compile-time and does not require a function pointer at runtime.
  4222. * These functions are not bound to a handler data so have no data or cleanup
  4223. * handler. */
  4224. struct UnboundFunc {
  4225. CleanupFunc *GetCleanup() { return NULL; }
  4226. void *GetData() { return NULL; }
  4227. };
  4228. template <class R, class P1, R F(P1), class I>
  4229. struct Func1 : public UnboundFunc {
  4230. typedef R Return;
  4231. typedef I FuncInfo;
  4232. static R Call(P1 p1) { return F(p1); }
  4233. };
  4234. template <class R, class P1, class P2, R F(P1, P2), class I>
  4235. struct Func2 : public UnboundFunc {
  4236. typedef R Return;
  4237. typedef I FuncInfo;
  4238. static R Call(P1 p1, P2 p2) { return F(p1, p2); }
  4239. };
  4240. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4241. struct Func3 : public UnboundFunc {
  4242. typedef R Return;
  4243. typedef I FuncInfo;
  4244. static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
  4245. };
  4246. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4247. class I>
  4248. struct Func4 : public UnboundFunc {
  4249. typedef R Return;
  4250. typedef I FuncInfo;
  4251. static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
  4252. };
  4253. template <class R, class P1, class P2, class P3, class P4, class P5,
  4254. R F(P1, P2, P3, P4, P5), class I>
  4255. struct Func5 : public UnboundFunc {
  4256. typedef R Return;
  4257. typedef I FuncInfo;
  4258. static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  4259. return F(p1, p2, p3, p4, p5);
  4260. }
  4261. };
  4262. /* BoundFunc ******************************************************************/
  4263. /* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
  4264. * shall be bound to the function's second parameter.
  4265. *
  4266. * Note that the second parameter is a const pointer, but our stored bound value
  4267. * is non-const so we can free it when the handlers are destroyed. */
  4268. template <class T>
  4269. struct BoundFunc {
  4270. typedef typename remove_constptr<T>::type MutableP2;
  4271. explicit BoundFunc(MutableP2 data_) : data(data_) {}
  4272. CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
  4273. MutableP2 GetData() { return data; }
  4274. MutableP2 data;
  4275. };
  4276. template <class R, class P1, class P2, R F(P1, P2), class I>
  4277. struct BoundFunc2 : public BoundFunc<P2> {
  4278. typedef BoundFunc<P2> Base;
  4279. typedef I FuncInfo;
  4280. explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
  4281. };
  4282. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4283. struct BoundFunc3 : public BoundFunc<P2> {
  4284. typedef BoundFunc<P2> Base;
  4285. typedef I FuncInfo;
  4286. explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
  4287. };
  4288. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4289. class I>
  4290. struct BoundFunc4 : public BoundFunc<P2> {
  4291. typedef BoundFunc<P2> Base;
  4292. typedef I FuncInfo;
  4293. explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
  4294. };
  4295. template <class R, class P1, class P2, class P3, class P4, class P5,
  4296. R F(P1, P2, P3, P4, P5), class I>
  4297. struct BoundFunc5 : public BoundFunc<P2> {
  4298. typedef BoundFunc<P2> Base;
  4299. typedef I FuncInfo;
  4300. explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
  4301. };
  4302. /* FuncSig ********************************************************************/
  4303. /* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
  4304. * *signature*, but without a specific function attached.
  4305. *
  4306. * These classes contain member functions that can be invoked with a
  4307. * specific function to return a Func/BoundFunc class. */
  4308. template <class R, class P1>
  4309. struct FuncSig1 {
  4310. template <R F(P1)>
  4311. Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
  4312. return Func1<R, P1, F, FuncInfo<P1, R> >();
  4313. }
  4314. };
  4315. template <class R, class P1, class P2>
  4316. struct FuncSig2 {
  4317. template <R F(P1, P2)>
  4318. Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
  4319. return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
  4320. }
  4321. template <R F(P1, P2)>
  4322. BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
  4323. typename remove_constptr<P2>::type param2) {
  4324. return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
  4325. }
  4326. };
  4327. template <class R, class P1, class P2, class P3>
  4328. struct FuncSig3 {
  4329. template <R F(P1, P2, P3)>
  4330. Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
  4331. return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
  4332. }
  4333. template <R F(P1, P2, P3)>
  4334. BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
  4335. typename remove_constptr<P2>::type param2) {
  4336. return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
  4337. }
  4338. };
  4339. template <class R, class P1, class P2, class P3, class P4>
  4340. struct FuncSig4 {
  4341. template <R F(P1, P2, P3, P4)>
  4342. Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
  4343. return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
  4344. }
  4345. template <R F(P1, P2, P3, P4)>
  4346. BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
  4347. typename remove_constptr<P2>::type param2) {
  4348. return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
  4349. }
  4350. };
  4351. template <class R, class P1, class P2, class P3, class P4, class P5>
  4352. struct FuncSig5 {
  4353. template <R F(P1, P2, P3, P4, P5)>
  4354. Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
  4355. return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
  4356. }
  4357. template <R F(P1, P2, P3, P4, P5)>
  4358. BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
  4359. typename remove_constptr<P2>::type param2) {
  4360. return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
  4361. }
  4362. };
  4363. /* Overloaded template function that can construct the appropriate FuncSig*
  4364. * class given a function pointer by deducing the template parameters. */
  4365. template <class R, class P1>
  4366. inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
  4367. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4368. return FuncSig1<R, P1>();
  4369. }
  4370. template <class R, class P1, class P2>
  4371. inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
  4372. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4373. return FuncSig2<R, P1, P2>();
  4374. }
  4375. template <class R, class P1, class P2, class P3>
  4376. inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
  4377. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4378. return FuncSig3<R, P1, P2, P3>();
  4379. }
  4380. template <class R, class P1, class P2, class P3, class P4>
  4381. inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
  4382. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4383. return FuncSig4<R, P1, P2, P3, P4>();
  4384. }
  4385. template <class R, class P1, class P2, class P3, class P4, class P5>
  4386. inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
  4387. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4388. return FuncSig5<R, P1, P2, P3, P4, P5>();
  4389. }
  4390. /* MethodSig ******************************************************************/
  4391. /* CallMethod*: a function template that calls a given method. */
  4392. template <class R, class C, R (C::*F)()>
  4393. R CallMethod0(C *obj) {
  4394. return ((*obj).*F)();
  4395. }
  4396. template <class R, class C, class P1, R (C::*F)(P1)>
  4397. R CallMethod1(C *obj, P1 arg1) {
  4398. return ((*obj).*F)(arg1);
  4399. }
  4400. template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
  4401. R CallMethod2(C *obj, P1 arg1, P2 arg2) {
  4402. return ((*obj).*F)(arg1, arg2);
  4403. }
  4404. template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
  4405. R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
  4406. return ((*obj).*F)(arg1, arg2, arg3);
  4407. }
  4408. template <class R, class C, class P1, class P2, class P3, class P4,
  4409. R (C::*F)(P1, P2, P3, P4)>
  4410. R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
  4411. return ((*obj).*F)(arg1, arg2, arg3, arg4);
  4412. }
  4413. /* MethodSig: like FuncSig, but for member functions.
  4414. *
  4415. * GetFunc() returns a normal FuncN object, so after calling GetFunc() no
  4416. * more logic is required to special-case methods. */
  4417. template <class R, class C>
  4418. struct MethodSig0 {
  4419. template <R (C::*F)()>
  4420. Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
  4421. return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
  4422. }
  4423. };
  4424. template <class R, class C, class P1>
  4425. struct MethodSig1 {
  4426. template <R (C::*F)(P1)>
  4427. Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
  4428. return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
  4429. }
  4430. template <R (C::*F)(P1)>
  4431. BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
  4432. typename remove_constptr<P1>::type param1) {
  4433. return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
  4434. param1);
  4435. }
  4436. };
  4437. template <class R, class C, class P1, class P2>
  4438. struct MethodSig2 {
  4439. template <R (C::*F)(P1, P2)>
  4440. Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4441. GetFunc() {
  4442. return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4443. FuncInfo<C *, R> >();
  4444. }
  4445. template <R (C::*F)(P1, P2)>
  4446. BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4447. GetFunc(typename remove_constptr<P1>::type param1) {
  4448. return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4449. FuncInfo<C *, R> >(param1);
  4450. }
  4451. };
  4452. template <class R, class C, class P1, class P2, class P3>
  4453. struct MethodSig3 {
  4454. template <R (C::*F)(P1, P2, P3)>
  4455. Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
  4456. GetFunc() {
  4457. return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4458. FuncInfo<C *, R> >();
  4459. }
  4460. template <R (C::*F)(P1, P2, P3)>
  4461. BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4462. FuncInfo<C *, R> >
  4463. GetFunc(typename remove_constptr<P1>::type param1) {
  4464. return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4465. FuncInfo<C *, R> >(param1);
  4466. }
  4467. };
  4468. template <class R, class C, class P1, class P2, class P3, class P4>
  4469. struct MethodSig4 {
  4470. template <R (C::*F)(P1, P2, P3, P4)>
  4471. Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4472. FuncInfo<C *, R> >
  4473. GetFunc() {
  4474. return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4475. FuncInfo<C *, R> >();
  4476. }
  4477. template <R (C::*F)(P1, P2, P3, P4)>
  4478. BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4479. FuncInfo<C *, R> >
  4480. GetFunc(typename remove_constptr<P1>::type param1) {
  4481. return BoundFunc5<R, C *, P1, P2, P3, P4,
  4482. CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
  4483. param1);
  4484. }
  4485. };
  4486. template <class R, class C>
  4487. inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
  4488. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4489. return MethodSig0<R, C>();
  4490. }
  4491. template <class R, class C, class P1>
  4492. inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
  4493. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4494. return MethodSig1<R, C, P1>();
  4495. }
  4496. template <class R, class C, class P1, class P2>
  4497. inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
  4498. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4499. return MethodSig2<R, C, P1, P2>();
  4500. }
  4501. template <class R, class C, class P1, class P2, class P3>
  4502. inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
  4503. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4504. return MethodSig3<R, C, P1, P2, P3>();
  4505. }
  4506. template <class R, class C, class P1, class P2, class P3, class P4>
  4507. inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
  4508. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4509. return MethodSig4<R, C, P1, P2, P3, P4>();
  4510. }
  4511. /* MaybeWrapReturn ************************************************************/
  4512. /* Template class that attempts to wrap the return value of the function so it
  4513. * matches the expected type. There are two main adjustments it may make:
  4514. *
  4515. * 1. If the function returns void, make it return the expected type and with
  4516. * a value that always indicates success.
  4517. * 2. If the function returns bool, make it return the expected type with a
  4518. * value that indicates success or failure.
  4519. *
  4520. * The "expected type" for return is:
  4521. * 1. void* for start handlers. If the closure parameter has a different type
  4522. * we will cast it to void* for the return in the success case.
  4523. * 2. size_t for string buffer handlers.
  4524. * 3. bool for everything else. */
  4525. /* Template parameters are FuncN type and desired return type. */
  4526. template <class F, class R, class Enable = void>
  4527. struct MaybeWrapReturn;
  4528. /* If the return type matches, return the given function unwrapped. */
  4529. template <class F>
  4530. struct MaybeWrapReturn<F, typename F::Return> {
  4531. typedef F Func;
  4532. };
  4533. /* Function wrapper that munges the return value from void to (bool)true. */
  4534. template <class P1, class P2, void F(P1, P2)>
  4535. bool ReturnTrue2(P1 p1, P2 p2) {
  4536. F(p1, p2);
  4537. return true;
  4538. }
  4539. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4540. bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
  4541. F(p1, p2, p3);
  4542. return true;
  4543. }
  4544. /* Function wrapper that munges the return value from void to (void*)arg1 */
  4545. template <class P1, class P2, void F(P1, P2)>
  4546. void *ReturnClosure2(P1 p1, P2 p2) {
  4547. F(p1, p2);
  4548. return p1;
  4549. }
  4550. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4551. void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
  4552. F(p1, p2, p3);
  4553. return p1;
  4554. }
  4555. /* Function wrapper that munges the return value from R to void*. */
  4556. template <class R, class P1, class P2, R F(P1, P2)>
  4557. void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
  4558. return F(p1, p2);
  4559. }
  4560. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4561. void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
  4562. return F(p1, p2, p3);
  4563. }
  4564. /* Function wrapper that munges the return value from bool to void*. */
  4565. template <class P1, class P2, bool F(P1, P2)>
  4566. void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
  4567. return F(p1, p2) ? p1 : UPB_BREAK;
  4568. }
  4569. template <class P1, class P2, class P3, bool F(P1, P2, P3)>
  4570. void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
  4571. return F(p1, p2, p3) ? p1 : UPB_BREAK;
  4572. }
  4573. /* For the string callback, which takes five params, returns the size param. */
  4574. template <class P1, class P2,
  4575. void F(P1, P2, const char *, size_t, const BufferHandle *)>
  4576. size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
  4577. const BufferHandle *p5) {
  4578. F(p1, p2, p3, p4, p5);
  4579. return p4;
  4580. }
  4581. /* For the string callback, which takes five params, returns the size param or
  4582. * zero. */
  4583. template <class P1, class P2,
  4584. bool F(P1, P2, const char *, size_t, const BufferHandle *)>
  4585. size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
  4586. const BufferHandle *p5) {
  4587. return F(p1, p2, p3, p4, p5) ? p4 : 0;
  4588. }
  4589. /* If we have a function returning void but want a function returning bool, wrap
  4590. * it in a function that returns true. */
  4591. template <class P1, class P2, void F(P1, P2), class I>
  4592. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
  4593. typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
  4594. };
  4595. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4596. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
  4597. typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
  4598. };
  4599. /* If our function returns void but we want one returning void*, wrap it in a
  4600. * function that returns the first argument. */
  4601. template <class P1, class P2, void F(P1, P2), class I>
  4602. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
  4603. typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
  4604. };
  4605. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4606. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
  4607. typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
  4608. };
  4609. /* If our function returns R* but we want one returning void*, wrap it in a
  4610. * function that casts to void*. */
  4611. template <class R, class P1, class P2, R *F(P1, P2), class I>
  4612. struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
  4613. typename disable_if_same<R *, void *>::Type> {
  4614. typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
  4615. };
  4616. template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
  4617. struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
  4618. typename disable_if_same<R *, void *>::Type> {
  4619. typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
  4620. Func;
  4621. };
  4622. /* If our function returns bool but we want one returning void*, wrap it in a
  4623. * function that returns either the first param or UPB_BREAK. */
  4624. template <class P1, class P2, bool F(P1, P2), class I>
  4625. struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
  4626. typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
  4627. };
  4628. template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
  4629. struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
  4630. typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
  4631. Func;
  4632. };
  4633. /* If our function returns void but we want one returning size_t, wrap it in a
  4634. * function that returns the size argument. */
  4635. template <class P1, class P2,
  4636. void F(P1, P2, const char *, size_t, const BufferHandle *), class I>
  4637. struct MaybeWrapReturn<
  4638. Func5<void, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
  4639. size_t> {
  4640. typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
  4641. ReturnStringLen<P1, P2, F>, I> Func;
  4642. };
  4643. /* If our function returns bool but we want one returning size_t, wrap it in a
  4644. * function that returns either 0 or the buf size. */
  4645. template <class P1, class P2,
  4646. bool F(P1, P2, const char *, size_t, const BufferHandle *), class I>
  4647. struct MaybeWrapReturn<
  4648. Func5<bool, P1, P2, const char *, size_t, const BufferHandle *, F, I>,
  4649. size_t> {
  4650. typedef Func5<size_t, P1, P2, const char *, size_t, const BufferHandle *,
  4651. ReturnNOr0<P1, P2, F>, I> Func;
  4652. };
  4653. /* ConvertParams **************************************************************/
  4654. /* Template class that converts the function parameters if necessary, and
  4655. * ignores the HandlerData parameter if appropriate.
  4656. *
  4657. * Template parameter is the are FuncN function type. */
  4658. template <class F, class T>
  4659. struct ConvertParams;
  4660. /* Function that discards the handler data parameter. */
  4661. template <class R, class P1, R F(P1)>
  4662. R IgnoreHandlerData2(void *p1, const void *hd) {
  4663. UPB_UNUSED(hd);
  4664. return F(static_cast<P1>(p1));
  4665. }
  4666. template <class R, class P1, class P2Wrapper, class P2Wrapped,
  4667. R F(P1, P2Wrapped)>
  4668. R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
  4669. UPB_UNUSED(hd);
  4670. return F(static_cast<P1>(p1), p2);
  4671. }
  4672. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4673. R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
  4674. UPB_UNUSED(hd);
  4675. return F(static_cast<P1>(p1), p2, p3);
  4676. }
  4677. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
  4678. R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
  4679. UPB_UNUSED(hd);
  4680. return F(static_cast<P1>(p1), p2, p3, p4);
  4681. }
  4682. template <class R, class P1, R F(P1, const char*, size_t)>
  4683. R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
  4684. size_t p3, const BufferHandle *handle) {
  4685. UPB_UNUSED(hd);
  4686. UPB_UNUSED(handle);
  4687. return F(static_cast<P1>(p1), p2, p3);
  4688. }
  4689. /* Function that casts the handler data parameter. */
  4690. template <class R, class P1, class P2, R F(P1, P2)>
  4691. R CastHandlerData2(void *c, const void *hd) {
  4692. return F(static_cast<P1>(c), static_cast<P2>(hd));
  4693. }
  4694. template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
  4695. R F(P1, P2, P3Wrapped)>
  4696. R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
  4697. return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
  4698. }
  4699. template <class R, class P1, class P2, class P3, class P4, class P5,
  4700. R F(P1, P2, P3, P4, P5)>
  4701. R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
  4702. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
  4703. }
  4704. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
  4705. R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
  4706. size_t p4, const BufferHandle *handle) {
  4707. UPB_UNUSED(handle);
  4708. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
  4709. }
  4710. /* For unbound functions, ignore the handler data. */
  4711. template <class R, class P1, R F(P1), class I, class T>
  4712. struct ConvertParams<Func1<R, P1, F, I>, T> {
  4713. typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
  4714. };
  4715. template <class R, class P1, class P2, R F(P1, P2), class I,
  4716. class R2, class P1_2, class P2_2, class P3_2>
  4717. struct ConvertParams<Func2<R, P1, P2, F, I>,
  4718. R2 (*)(P1_2, P2_2, P3_2)> {
  4719. typedef Func3<R, void *, const void *, P3_2,
  4720. IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
  4721. };
  4722. /* For StringBuffer only; this ignores both the handler data and the
  4723. * BufferHandle. */
  4724. template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
  4725. struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
  4726. typedef Func5<R, void *, const void *, const char *, size_t,
  4727. const BufferHandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
  4728. I> Func;
  4729. };
  4730. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4731. class I, class T>
  4732. struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
  4733. typedef Func5<R, void *, const void *, P2, P3, P4,
  4734. IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
  4735. };
  4736. /* For bound functions, cast the handler data. */
  4737. template <class R, class P1, class P2, R F(P1, P2), class I, class T>
  4738. struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
  4739. typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
  4740. Func;
  4741. };
  4742. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
  4743. class R2, class P1_2, class P2_2, class P3_2>
  4744. struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
  4745. R2 (*)(P1_2, P2_2, P3_2)> {
  4746. typedef Func3<R, void *, const void *, P3_2,
  4747. CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
  4748. };
  4749. /* For StringBuffer only; this ignores the BufferHandle. */
  4750. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
  4751. class I, class T>
  4752. struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
  4753. typedef Func5<R, void *, const void *, const char *, size_t,
  4754. const BufferHandle *, CastHandlerDataIgnoreHandle<R, P1, P2, F>,
  4755. I> Func;
  4756. };
  4757. template <class R, class P1, class P2, class P3, class P4, class P5,
  4758. R F(P1, P2, P3, P4, P5), class I, class T>
  4759. struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
  4760. typedef Func5<R, void *, const void *, P3, P4, P5,
  4761. CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
  4762. };
  4763. /* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
  4764. * variant C type. */
  4765. #define TYPE_METHODS(utype, ltype, ctype, vtype) \
  4766. template <> struct CanonicalType<vtype> { \
  4767. typedef ctype Type; \
  4768. }; \
  4769. template <> \
  4770. inline bool Handlers::SetValueHandler<vtype>( \
  4771. const FieldDef *f, \
  4772. const Handlers::utype ## Handler& handler) { \
  4773. UPB_ASSERT(!handler.registered_); \
  4774. handler.AddCleanup(this); \
  4775. handler.registered_ = true; \
  4776. return upb_handlers_set##ltype(this, f, handler.handler_, &handler.attr_); \
  4777. } \
  4778. TYPE_METHODS(Double, double, double, double)
  4779. TYPE_METHODS(Float, float, float, float)
  4780. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T)
  4781. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T)
  4782. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T)
  4783. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T)
  4784. TYPE_METHODS(Bool, bool, bool, bool)
  4785. #ifdef UPB_TWO_32BIT_TYPES
  4786. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T)
  4787. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T)
  4788. #endif
  4789. #ifdef UPB_TWO_64BIT_TYPES
  4790. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T)
  4791. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T)
  4792. #endif
  4793. #undef TYPE_METHODS
  4794. template <> struct CanonicalType<Status*> {
  4795. typedef Status* Type;
  4796. };
  4797. /* Type methods that are only one-per-canonical-type and not
  4798. * one-per-cvariant. */
  4799. #define TYPE_METHODS(utype, ctype) \
  4800. inline bool Handlers::Set##utype##Handler(const FieldDef *f, \
  4801. const utype##Handler &h) { \
  4802. return SetValueHandler<ctype>(f, h); \
  4803. } \
  4804. TYPE_METHODS(Double, double)
  4805. TYPE_METHODS(Float, float)
  4806. TYPE_METHODS(UInt64, uint64_t)
  4807. TYPE_METHODS(UInt32, uint32_t)
  4808. TYPE_METHODS(Int64, int64_t)
  4809. TYPE_METHODS(Int32, int32_t)
  4810. TYPE_METHODS(Bool, bool)
  4811. #undef TYPE_METHODS
  4812. template <class F> struct ReturnOf;
  4813. template <class R, class P1, class P2>
  4814. struct ReturnOf<R (*)(P1, P2)> {
  4815. typedef R Return;
  4816. };
  4817. template <class R, class P1, class P2, class P3>
  4818. struct ReturnOf<R (*)(P1, P2, P3)> {
  4819. typedef R Return;
  4820. };
  4821. template <class R, class P1, class P2, class P3, class P4>
  4822. struct ReturnOf<R (*)(P1, P2, P3, P4)> {
  4823. typedef R Return;
  4824. };
  4825. template <class R, class P1, class P2, class P3, class P4, class P5>
  4826. struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
  4827. typedef R Return;
  4828. };
  4829. template<class T> const void *UniquePtrForType() {
  4830. static const char ch = 0;
  4831. return &ch;
  4832. }
  4833. template <class T>
  4834. template <class F>
  4835. inline Handler<T>::Handler(F func)
  4836. : registered_(false),
  4837. cleanup_data_(func.GetData()),
  4838. cleanup_func_(func.GetCleanup()) {
  4839. upb_handlerattr_sethandlerdata(&attr_, func.GetData());
  4840. typedef typename ReturnOf<T>::Return Return;
  4841. typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
  4842. typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
  4843. ReturnWrappedFunc;
  4844. handler_ = ReturnWrappedFunc().Call;
  4845. /* Set attributes based on what templates can statically tell us about the
  4846. * user's function. */
  4847. /* If the original function returns void, then we know that we wrapped it to
  4848. * always return ok. */
  4849. bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
  4850. attr_.SetAlwaysOk(always_ok);
  4851. /* Closure parameter and return type. */
  4852. attr_.SetClosureType(UniquePtrForType<typename F::FuncInfo::Closure>());
  4853. /* We use the closure type (from the first parameter) if the return type is
  4854. * void or bool, since these are the two cases we wrap to return the closure's
  4855. * type anyway.
  4856. *
  4857. * This is all nonsense for non START* handlers, but it doesn't matter because
  4858. * in that case the value will be ignored. */
  4859. typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
  4860. typename F::FuncInfo::Closure>::value
  4861. EffectiveReturn;
  4862. attr_.SetReturnClosureType(UniquePtrForType<EffectiveReturn>());
  4863. }
  4864. template <class T>
  4865. inline Handler<T>::~Handler() {
  4866. UPB_ASSERT(registered_);
  4867. }
  4868. inline HandlerAttributes::HandlerAttributes() { upb_handlerattr_init(this); }
  4869. inline HandlerAttributes::~HandlerAttributes() { upb_handlerattr_uninit(this); }
  4870. inline bool HandlerAttributes::SetHandlerData(const void *hd) {
  4871. return upb_handlerattr_sethandlerdata(this, hd);
  4872. }
  4873. inline const void* HandlerAttributes::handler_data() const {
  4874. return upb_handlerattr_handlerdata(this);
  4875. }
  4876. inline bool HandlerAttributes::SetClosureType(const void *type) {
  4877. return upb_handlerattr_setclosuretype(this, type);
  4878. }
  4879. inline const void* HandlerAttributes::closure_type() const {
  4880. return upb_handlerattr_closuretype(this);
  4881. }
  4882. inline bool HandlerAttributes::SetReturnClosureType(const void *type) {
  4883. return upb_handlerattr_setreturnclosuretype(this, type);
  4884. }
  4885. inline const void* HandlerAttributes::return_closure_type() const {
  4886. return upb_handlerattr_returnclosuretype(this);
  4887. }
  4888. inline bool HandlerAttributes::SetAlwaysOk(bool always_ok) {
  4889. return upb_handlerattr_setalwaysok(this, always_ok);
  4890. }
  4891. inline bool HandlerAttributes::always_ok() const {
  4892. return upb_handlerattr_alwaysok(this);
  4893. }
  4894. inline BufferHandle::BufferHandle() { upb_bufhandle_init(this); }
  4895. inline BufferHandle::~BufferHandle() { upb_bufhandle_uninit(this); }
  4896. inline const char* BufferHandle::buffer() const {
  4897. return upb_bufhandle_buf(this);
  4898. }
  4899. inline size_t BufferHandle::object_offset() const {
  4900. return upb_bufhandle_objofs(this);
  4901. }
  4902. inline void BufferHandle::SetBuffer(const char* buf, size_t ofs) {
  4903. upb_bufhandle_setbuf(this, buf, ofs);
  4904. }
  4905. template <class T>
  4906. void BufferHandle::SetAttachedObject(const T* obj) {
  4907. upb_bufhandle_setobj(this, obj, UniquePtrForType<T>());
  4908. }
  4909. template <class T>
  4910. const T* BufferHandle::GetAttachedObject() const {
  4911. return upb_bufhandle_objtype(this) == UniquePtrForType<T>()
  4912. ? static_cast<const T *>(upb_bufhandle_obj(this))
  4913. : NULL;
  4914. }
  4915. inline reffed_ptr<Handlers> Handlers::New(const MessageDef *m) {
  4916. upb_handlers *h = upb_handlers_new(m, &h);
  4917. return reffed_ptr<Handlers>(h, &h);
  4918. }
  4919. inline reffed_ptr<const Handlers> Handlers::NewFrozen(
  4920. const MessageDef *m, upb_handlers_callback *callback,
  4921. const void *closure) {
  4922. const upb_handlers *h = upb_handlers_newfrozen(m, &h, callback, closure);
  4923. return reffed_ptr<const Handlers>(h, &h);
  4924. }
  4925. inline const Status* Handlers::status() {
  4926. return upb_handlers_status(this);
  4927. }
  4928. inline void Handlers::ClearError() {
  4929. return upb_handlers_clearerr(this);
  4930. }
  4931. inline bool Handlers::Freeze(Status *s) {
  4932. upb::Handlers* h = this;
  4933. return upb_handlers_freeze(&h, 1, s);
  4934. }
  4935. inline bool Handlers::Freeze(Handlers *const *handlers, int n, Status *s) {
  4936. return upb_handlers_freeze(handlers, n, s);
  4937. }
  4938. inline bool Handlers::Freeze(const std::vector<Handlers*>& h, Status* status) {
  4939. return upb_handlers_freeze((Handlers* const*)&h[0], h.size(), status);
  4940. }
  4941. inline const MessageDef *Handlers::message_def() const {
  4942. return upb_handlers_msgdef(this);
  4943. }
  4944. inline bool Handlers::AddCleanup(void *p, upb_handlerfree *func) {
  4945. return upb_handlers_addcleanup(this, p, func);
  4946. }
  4947. inline bool Handlers::SetStartMessageHandler(
  4948. const Handlers::StartMessageHandler &handler) {
  4949. UPB_ASSERT(!handler.registered_);
  4950. handler.registered_ = true;
  4951. handler.AddCleanup(this);
  4952. return upb_handlers_setstartmsg(this, handler.handler_, &handler.attr_);
  4953. }
  4954. inline bool Handlers::SetEndMessageHandler(
  4955. const Handlers::EndMessageHandler &handler) {
  4956. UPB_ASSERT(!handler.registered_);
  4957. handler.registered_ = true;
  4958. handler.AddCleanup(this);
  4959. return upb_handlers_setendmsg(this, handler.handler_, &handler.attr_);
  4960. }
  4961. inline bool Handlers::SetStartStringHandler(const FieldDef *f,
  4962. const StartStringHandler &handler) {
  4963. UPB_ASSERT(!handler.registered_);
  4964. handler.registered_ = true;
  4965. handler.AddCleanup(this);
  4966. return upb_handlers_setstartstr(this, f, handler.handler_, &handler.attr_);
  4967. }
  4968. inline bool Handlers::SetEndStringHandler(const FieldDef *f,
  4969. const EndFieldHandler &handler) {
  4970. UPB_ASSERT(!handler.registered_);
  4971. handler.registered_ = true;
  4972. handler.AddCleanup(this);
  4973. return upb_handlers_setendstr(this, f, handler.handler_, &handler.attr_);
  4974. }
  4975. inline bool Handlers::SetStringHandler(const FieldDef *f,
  4976. const StringHandler& handler) {
  4977. UPB_ASSERT(!handler.registered_);
  4978. handler.registered_ = true;
  4979. handler.AddCleanup(this);
  4980. return upb_handlers_setstring(this, f, handler.handler_, &handler.attr_);
  4981. }
  4982. inline bool Handlers::SetStartSequenceHandler(
  4983. const FieldDef *f, const StartFieldHandler &handler) {
  4984. UPB_ASSERT(!handler.registered_);
  4985. handler.registered_ = true;
  4986. handler.AddCleanup(this);
  4987. return upb_handlers_setstartseq(this, f, handler.handler_, &handler.attr_);
  4988. }
  4989. inline bool Handlers::SetStartSubMessageHandler(
  4990. const FieldDef *f, const StartFieldHandler &handler) {
  4991. UPB_ASSERT(!handler.registered_);
  4992. handler.registered_ = true;
  4993. handler.AddCleanup(this);
  4994. return upb_handlers_setstartsubmsg(this, f, handler.handler_, &handler.attr_);
  4995. }
  4996. inline bool Handlers::SetEndSubMessageHandler(const FieldDef *f,
  4997. const EndFieldHandler &handler) {
  4998. UPB_ASSERT(!handler.registered_);
  4999. handler.registered_ = true;
  5000. handler.AddCleanup(this);
  5001. return upb_handlers_setendsubmsg(this, f, handler.handler_, &handler.attr_);
  5002. }
  5003. inline bool Handlers::SetEndSequenceHandler(const FieldDef *f,
  5004. const EndFieldHandler &handler) {
  5005. UPB_ASSERT(!handler.registered_);
  5006. handler.registered_ = true;
  5007. handler.AddCleanup(this);
  5008. return upb_handlers_setendseq(this, f, handler.handler_, &handler.attr_);
  5009. }
  5010. inline bool Handlers::SetSubHandlers(const FieldDef *f, const Handlers *sub) {
  5011. return upb_handlers_setsubhandlers(this, f, sub);
  5012. }
  5013. inline const Handlers *Handlers::GetSubHandlers(const FieldDef *f) const {
  5014. return upb_handlers_getsubhandlers(this, f);
  5015. }
  5016. inline const Handlers *Handlers::GetSubHandlers(Handlers::Selector sel) const {
  5017. return upb_handlers_getsubhandlers_sel(this, sel);
  5018. }
  5019. inline bool Handlers::GetSelector(const FieldDef *f, Handlers::Type type,
  5020. Handlers::Selector *s) {
  5021. return upb_handlers_getselector(f, type, s);
  5022. }
  5023. inline Handlers::Selector Handlers::GetEndSelector(Handlers::Selector start) {
  5024. return upb_handlers_getendselector(start);
  5025. }
  5026. inline Handlers::GenericFunction *Handlers::GetHandler(
  5027. Handlers::Selector selector) {
  5028. return upb_handlers_gethandler(this, selector);
  5029. }
  5030. inline const void *Handlers::GetHandlerData(Handlers::Selector selector) {
  5031. return upb_handlers_gethandlerdata(this, selector);
  5032. }
  5033. inline BytesHandler::BytesHandler() {
  5034. upb_byteshandler_init(this);
  5035. }
  5036. inline BytesHandler::~BytesHandler() {}
  5037. } /* namespace upb */
  5038. #endif /* __cplusplus */
  5039. #undef UPB_TWO_32BIT_TYPES
  5040. #undef UPB_TWO_64BIT_TYPES
  5041. #undef UPB_INT32_T
  5042. #undef UPB_UINT32_T
  5043. #undef UPB_INT32ALT_T
  5044. #undef UPB_UINT32ALT_T
  5045. #undef UPB_INT64_T
  5046. #undef UPB_UINT64_T
  5047. #undef UPB_INT64ALT_T
  5048. #undef UPB_UINT64ALT_T
  5049. #endif /* UPB_HANDLERS_INL_H_ */
  5050. #endif /* UPB_HANDLERS_H */
  5051. /*
  5052. ** upb::Sink (upb_sink)
  5053. ** upb::BytesSink (upb_bytessink)
  5054. **
  5055. ** A upb_sink is an object that binds a upb_handlers object to some runtime
  5056. ** state. It is the object that can actually receive data via the upb_handlers
  5057. ** interface.
  5058. **
  5059. ** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
  5060. ** thread-safe. You can create as many of them as you want, but each one may
  5061. ** only be used in a single thread at a time.
  5062. **
  5063. ** If we compare with class-based OOP, a you can think of a upb_def as an
  5064. ** abstract base class, a upb_handlers as a concrete derived class, and a
  5065. ** upb_sink as an object (class instance).
  5066. */
  5067. #ifndef UPB_SINK_H
  5068. #define UPB_SINK_H
  5069. #ifdef __cplusplus
  5070. namespace upb {
  5071. class BufferSink;
  5072. class BufferSource;
  5073. class BytesSink;
  5074. class Sink;
  5075. }
  5076. #endif
  5077. UPB_DECLARE_TYPE(upb::BufferSink, upb_bufsink)
  5078. UPB_DECLARE_TYPE(upb::BufferSource, upb_bufsrc)
  5079. UPB_DECLARE_TYPE(upb::BytesSink, upb_bytessink)
  5080. UPB_DECLARE_TYPE(upb::Sink, upb_sink)
  5081. #ifdef __cplusplus
  5082. /* A upb::Sink is an object that binds a upb::Handlers object to some runtime
  5083. * state. It represents an endpoint to which data can be sent.
  5084. *
  5085. * TODO(haberman): right now all of these functions take selectors. Should they
  5086. * take selectorbase instead?
  5087. *
  5088. * ie. instead of calling:
  5089. * sink->StartString(FOO_FIELD_START_STRING, ...)
  5090. * a selector base would let you say:
  5091. * sink->StartString(FOO_FIELD, ...)
  5092. *
  5093. * This would make call sites a little nicer and require emitting fewer selector
  5094. * definitions in .h files.
  5095. *
  5096. * But the current scheme has the benefit that you can retrieve a function
  5097. * pointer for any handler with handlers->GetHandler(selector), without having
  5098. * to have a separate GetHandler() function for each handler type. The JIT
  5099. * compiler uses this. To accommodate we'd have to expose a separate
  5100. * GetHandler() for every handler type.
  5101. *
  5102. * Also to ponder: selectors right now are independent of a specific Handlers
  5103. * instance. In other words, they allocate a number to every possible handler
  5104. * that *could* be registered, without knowing anything about what handlers
  5105. * *are* registered. That means that using selectors as table offsets prohibits
  5106. * us from compacting the handler table at Freeze() time. If the table is very
  5107. * sparse, this could be wasteful.
  5108. *
  5109. * Having another selector-like thing that is specific to a Handlers instance
  5110. * would allow this compacting, but then it would be impossible to write code
  5111. * ahead-of-time that can be bound to any Handlers instance at runtime. For
  5112. * example, a .proto file parser written as straight C will not know what
  5113. * Handlers it will be bound to, so when it calls sink->StartString() what
  5114. * selector will it pass? It needs a selector like we have today, that is
  5115. * independent of any particular upb::Handlers.
  5116. *
  5117. * Is there a way then to allow Handlers table compaction? */
  5118. class upb::Sink {
  5119. public:
  5120. /* Constructor with no initialization; must be Reset() before use. */
  5121. Sink() {}
  5122. /* Constructs a new sink for the given frozen handlers and closure.
  5123. *
  5124. * TODO: once the Handlers know the expected closure type, verify that T
  5125. * matches it. */
  5126. template <class T> Sink(const Handlers* handlers, T* closure);
  5127. /* Resets the value of the sink. */
  5128. template <class T> void Reset(const Handlers* handlers, T* closure);
  5129. /* Returns the top-level object that is bound to this sink.
  5130. *
  5131. * TODO: once the Handlers know the expected closure type, verify that T
  5132. * matches it. */
  5133. template <class T> T* GetObject() const;
  5134. /* Functions for pushing data into the sink.
  5135. *
  5136. * These return false if processing should stop (either due to error or just
  5137. * to suspend).
  5138. *
  5139. * These may not be called from within one of the same sink's handlers (in
  5140. * other words, handlers are not re-entrant). */
  5141. /* Should be called at the start and end of every message; both the top-level
  5142. * message and submessages. This means that submessages should use the
  5143. * following sequence:
  5144. * sink->StartSubMessage(startsubmsg_selector);
  5145. * sink->StartMessage();
  5146. * // ...
  5147. * sink->EndMessage(&status);
  5148. * sink->EndSubMessage(endsubmsg_selector); */
  5149. bool StartMessage();
  5150. bool EndMessage(Status* status);
  5151. /* Putting of individual values. These work for both repeated and
  5152. * non-repeated fields, but for repeated fields you must wrap them in
  5153. * calls to StartSequence()/EndSequence(). */
  5154. bool PutInt32(Handlers::Selector s, int32_t val);
  5155. bool PutInt64(Handlers::Selector s, int64_t val);
  5156. bool PutUInt32(Handlers::Selector s, uint32_t val);
  5157. bool PutUInt64(Handlers::Selector s, uint64_t val);
  5158. bool PutFloat(Handlers::Selector s, float val);
  5159. bool PutDouble(Handlers::Selector s, double val);
  5160. bool PutBool(Handlers::Selector s, bool val);
  5161. /* Putting of string/bytes values. Each string can consist of zero or more
  5162. * non-contiguous buffers of data.
  5163. *
  5164. * For StartString(), the function will write a sink for the string to "sub."
  5165. * The sub-sink must be used for any/all PutStringBuffer() calls. */
  5166. bool StartString(Handlers::Selector s, size_t size_hint, Sink* sub);
  5167. size_t PutStringBuffer(Handlers::Selector s, const char *buf, size_t len,
  5168. const BufferHandle *handle);
  5169. bool EndString(Handlers::Selector s);
  5170. /* For submessage fields.
  5171. *
  5172. * For StartSubMessage(), the function will write a sink for the string to
  5173. * "sub." The sub-sink must be used for any/all handlers called within the
  5174. * submessage. */
  5175. bool StartSubMessage(Handlers::Selector s, Sink* sub);
  5176. bool EndSubMessage(Handlers::Selector s);
  5177. /* For repeated fields of any type, the sequence of values must be wrapped in
  5178. * these calls.
  5179. *
  5180. * For StartSequence(), the function will write a sink for the string to
  5181. * "sub." The sub-sink must be used for any/all handlers called within the
  5182. * sequence. */
  5183. bool StartSequence(Handlers::Selector s, Sink* sub);
  5184. bool EndSequence(Handlers::Selector s);
  5185. /* Copy and assign specifically allowed.
  5186. * We don't even bother making these members private because so many
  5187. * functions need them and this is mainly just a dumb data container anyway.
  5188. */
  5189. #else
  5190. struct upb_sink {
  5191. #endif
  5192. const upb_handlers *handlers;
  5193. void *closure;
  5194. };
  5195. #ifdef __cplusplus
  5196. class upb::BytesSink {
  5197. public:
  5198. BytesSink() {}
  5199. /* Constructs a new sink for the given frozen handlers and closure.
  5200. *
  5201. * TODO(haberman): once the Handlers know the expected closure type, verify
  5202. * that T matches it. */
  5203. template <class T> BytesSink(const BytesHandler* handler, T* closure);
  5204. /* Resets the value of the sink. */
  5205. template <class T> void Reset(const BytesHandler* handler, T* closure);
  5206. bool Start(size_t size_hint, void **subc);
  5207. size_t PutBuffer(void *subc, const char *buf, size_t len,
  5208. const BufferHandle *handle);
  5209. bool End();
  5210. #else
  5211. struct upb_bytessink {
  5212. #endif
  5213. const upb_byteshandler *handler;
  5214. void *closure;
  5215. };
  5216. #ifdef __cplusplus
  5217. /* A class for pushing a flat buffer of data to a BytesSink.
  5218. * You can construct an instance of this to get a resumable source,
  5219. * or just call the static PutBuffer() to do a non-resumable push all in one
  5220. * go. */
  5221. class upb::BufferSource {
  5222. public:
  5223. BufferSource();
  5224. BufferSource(const char* buf, size_t len, BytesSink* sink);
  5225. /* Returns true if the entire buffer was pushed successfully. Otherwise the
  5226. * next call to PutNext() will resume where the previous one left off.
  5227. * TODO(haberman): implement this. */
  5228. bool PutNext();
  5229. /* A static version; with this version is it not possible to resume in the
  5230. * case of failure or a partially-consumed buffer. */
  5231. static bool PutBuffer(const char* buf, size_t len, BytesSink* sink);
  5232. template <class T> static bool PutBuffer(const T& str, BytesSink* sink) {
  5233. return PutBuffer(str.c_str(), str.size(), sink);
  5234. }
  5235. #else
  5236. struct upb_bufsrc {
  5237. char dummy;
  5238. #endif
  5239. };
  5240. UPB_BEGIN_EXTERN_C
  5241. /* A class for accumulating output string data in a flat buffer. */
  5242. upb_bufsink *upb_bufsink_new(upb_env *env);
  5243. void upb_bufsink_free(upb_bufsink *sink);
  5244. upb_bytessink *upb_bufsink_sink(upb_bufsink *sink);
  5245. const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len);
  5246. /* Inline definitions. */
  5247. UPB_INLINE void upb_bytessink_reset(upb_bytessink *s, const upb_byteshandler *h,
  5248. void *closure) {
  5249. s->handler = h;
  5250. s->closure = closure;
  5251. }
  5252. UPB_INLINE bool upb_bytessink_start(upb_bytessink *s, size_t size_hint,
  5253. void **subc) {
  5254. typedef upb_startstr_handlerfunc func;
  5255. func *start;
  5256. *subc = s->closure;
  5257. if (!s->handler) return true;
  5258. start = (func *)s->handler->table[UPB_STARTSTR_SELECTOR].func;
  5259. if (!start) return true;
  5260. *subc = start(s->closure, upb_handlerattr_handlerdata(
  5261. &s->handler->table[UPB_STARTSTR_SELECTOR].attr),
  5262. size_hint);
  5263. return *subc != NULL;
  5264. }
  5265. UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink *s, void *subc,
  5266. const char *buf, size_t size,
  5267. const upb_bufhandle* handle) {
  5268. typedef upb_string_handlerfunc func;
  5269. func *putbuf;
  5270. if (!s->handler) return true;
  5271. putbuf = (func *)s->handler->table[UPB_STRING_SELECTOR].func;
  5272. if (!putbuf) return true;
  5273. return putbuf(subc, upb_handlerattr_handlerdata(
  5274. &s->handler->table[UPB_STRING_SELECTOR].attr),
  5275. buf, size, handle);
  5276. }
  5277. UPB_INLINE bool upb_bytessink_end(upb_bytessink *s) {
  5278. typedef upb_endfield_handlerfunc func;
  5279. func *end;
  5280. if (!s->handler) return true;
  5281. end = (func *)s->handler->table[UPB_ENDSTR_SELECTOR].func;
  5282. if (!end) return true;
  5283. return end(s->closure,
  5284. upb_handlerattr_handlerdata(
  5285. &s->handler->table[UPB_ENDSTR_SELECTOR].attr));
  5286. }
  5287. bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink);
  5288. #define PUTVAL(type, ctype) \
  5289. UPB_INLINE bool upb_sink_put##type(upb_sink *s, upb_selector_t sel, \
  5290. ctype val) { \
  5291. typedef upb_##type##_handlerfunc functype; \
  5292. functype *func; \
  5293. const void *hd; \
  5294. if (!s->handlers) return true; \
  5295. func = (functype *)upb_handlers_gethandler(s->handlers, sel); \
  5296. if (!func) return true; \
  5297. hd = upb_handlers_gethandlerdata(s->handlers, sel); \
  5298. return func(s->closure, hd, val); \
  5299. }
  5300. PUTVAL(int32, int32_t)
  5301. PUTVAL(int64, int64_t)
  5302. PUTVAL(uint32, uint32_t)
  5303. PUTVAL(uint64, uint64_t)
  5304. PUTVAL(float, float)
  5305. PUTVAL(double, double)
  5306. PUTVAL(bool, bool)
  5307. #undef PUTVAL
  5308. UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
  5309. s->handlers = h;
  5310. s->closure = c;
  5311. }
  5312. UPB_INLINE size_t upb_sink_putstring(upb_sink *s, upb_selector_t sel,
  5313. const char *buf, size_t n,
  5314. const upb_bufhandle *handle) {
  5315. typedef upb_string_handlerfunc func;
  5316. func *handler;
  5317. const void *hd;
  5318. if (!s->handlers) return n;
  5319. handler = (func *)upb_handlers_gethandler(s->handlers, sel);
  5320. if (!handler) return n;
  5321. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5322. return handler(s->closure, hd, buf, n, handle);
  5323. }
  5324. UPB_INLINE bool upb_sink_putunknown(upb_sink *s, const char *buf, size_t n) {
  5325. typedef upb_unknown_handlerfunc func;
  5326. func *handler;
  5327. const void *hd;
  5328. if (!s->handlers) return true;
  5329. handler = (func *)upb_handlers_gethandler(s->handlers, UPB_UNKNOWN_SELECTOR);
  5330. if (!handler) return n;
  5331. hd = upb_handlers_gethandlerdata(s->handlers, UPB_UNKNOWN_SELECTOR);
  5332. return handler(s->closure, hd, buf, n);
  5333. }
  5334. UPB_INLINE bool upb_sink_startmsg(upb_sink *s) {
  5335. typedef upb_startmsg_handlerfunc func;
  5336. func *startmsg;
  5337. const void *hd;
  5338. if (!s->handlers) return true;
  5339. startmsg = (func*)upb_handlers_gethandler(s->handlers, UPB_STARTMSG_SELECTOR);
  5340. if (!startmsg) return true;
  5341. hd = upb_handlers_gethandlerdata(s->handlers, UPB_STARTMSG_SELECTOR);
  5342. return startmsg(s->closure, hd);
  5343. }
  5344. UPB_INLINE bool upb_sink_endmsg(upb_sink *s, upb_status *status) {
  5345. typedef upb_endmsg_handlerfunc func;
  5346. func *endmsg;
  5347. const void *hd;
  5348. if (!s->handlers) return true;
  5349. endmsg = (func *)upb_handlers_gethandler(s->handlers, UPB_ENDMSG_SELECTOR);
  5350. if (!endmsg) return true;
  5351. hd = upb_handlers_gethandlerdata(s->handlers, UPB_ENDMSG_SELECTOR);
  5352. return endmsg(s->closure, hd, status);
  5353. }
  5354. UPB_INLINE bool upb_sink_startseq(upb_sink *s, upb_selector_t sel,
  5355. upb_sink *sub) {
  5356. typedef upb_startfield_handlerfunc func;
  5357. func *startseq;
  5358. const void *hd;
  5359. sub->closure = s->closure;
  5360. sub->handlers = s->handlers;
  5361. if (!s->handlers) return true;
  5362. startseq = (func*)upb_handlers_gethandler(s->handlers, sel);
  5363. if (!startseq) return true;
  5364. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5365. sub->closure = startseq(s->closure, hd);
  5366. return sub->closure ? true : false;
  5367. }
  5368. UPB_INLINE bool upb_sink_endseq(upb_sink *s, upb_selector_t sel) {
  5369. typedef upb_endfield_handlerfunc func;
  5370. func *endseq;
  5371. const void *hd;
  5372. if (!s->handlers) return true;
  5373. endseq = (func*)upb_handlers_gethandler(s->handlers, sel);
  5374. if (!endseq) return true;
  5375. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5376. return endseq(s->closure, hd);
  5377. }
  5378. UPB_INLINE bool upb_sink_startstr(upb_sink *s, upb_selector_t sel,
  5379. size_t size_hint, upb_sink *sub) {
  5380. typedef upb_startstr_handlerfunc func;
  5381. func *startstr;
  5382. const void *hd;
  5383. sub->closure = s->closure;
  5384. sub->handlers = s->handlers;
  5385. if (!s->handlers) return true;
  5386. startstr = (func*)upb_handlers_gethandler(s->handlers, sel);
  5387. if (!startstr) return true;
  5388. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5389. sub->closure = startstr(s->closure, hd, size_hint);
  5390. return sub->closure ? true : false;
  5391. }
  5392. UPB_INLINE bool upb_sink_endstr(upb_sink *s, upb_selector_t sel) {
  5393. typedef upb_endfield_handlerfunc func;
  5394. func *endstr;
  5395. const void *hd;
  5396. if (!s->handlers) return true;
  5397. endstr = (func*)upb_handlers_gethandler(s->handlers, sel);
  5398. if (!endstr) return true;
  5399. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5400. return endstr(s->closure, hd);
  5401. }
  5402. UPB_INLINE bool upb_sink_startsubmsg(upb_sink *s, upb_selector_t sel,
  5403. upb_sink *sub) {
  5404. typedef upb_startfield_handlerfunc func;
  5405. func *startsubmsg;
  5406. const void *hd;
  5407. sub->closure = s->closure;
  5408. if (!s->handlers) {
  5409. sub->handlers = NULL;
  5410. return true;
  5411. }
  5412. sub->handlers = upb_handlers_getsubhandlers_sel(s->handlers, sel);
  5413. startsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
  5414. if (!startsubmsg) return true;
  5415. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5416. sub->closure = startsubmsg(s->closure, hd);
  5417. return sub->closure ? true : false;
  5418. }
  5419. UPB_INLINE bool upb_sink_endsubmsg(upb_sink *s, upb_selector_t sel) {
  5420. typedef upb_endfield_handlerfunc func;
  5421. func *endsubmsg;
  5422. const void *hd;
  5423. if (!s->handlers) return true;
  5424. endsubmsg = (func*)upb_handlers_gethandler(s->handlers, sel);
  5425. if (!endsubmsg) return s->closure;
  5426. hd = upb_handlers_gethandlerdata(s->handlers, sel);
  5427. return endsubmsg(s->closure, hd);
  5428. }
  5429. UPB_END_EXTERN_C
  5430. #ifdef __cplusplus
  5431. namespace upb {
  5432. template <class T> Sink::Sink(const Handlers* handlers, T* closure) {
  5433. upb_sink_reset(this, handlers, closure);
  5434. }
  5435. template <class T>
  5436. inline void Sink::Reset(const Handlers* handlers, T* closure) {
  5437. upb_sink_reset(this, handlers, closure);
  5438. }
  5439. inline bool Sink::StartMessage() {
  5440. return upb_sink_startmsg(this);
  5441. }
  5442. inline bool Sink::EndMessage(Status* status) {
  5443. return upb_sink_endmsg(this, status);
  5444. }
  5445. inline bool Sink::PutInt32(Handlers::Selector sel, int32_t val) {
  5446. return upb_sink_putint32(this, sel, val);
  5447. }
  5448. inline bool Sink::PutInt64(Handlers::Selector sel, int64_t val) {
  5449. return upb_sink_putint64(this, sel, val);
  5450. }
  5451. inline bool Sink::PutUInt32(Handlers::Selector sel, uint32_t val) {
  5452. return upb_sink_putuint32(this, sel, val);
  5453. }
  5454. inline bool Sink::PutUInt64(Handlers::Selector sel, uint64_t val) {
  5455. return upb_sink_putuint64(this, sel, val);
  5456. }
  5457. inline bool Sink::PutFloat(Handlers::Selector sel, float val) {
  5458. return upb_sink_putfloat(this, sel, val);
  5459. }
  5460. inline bool Sink::PutDouble(Handlers::Selector sel, double val) {
  5461. return upb_sink_putdouble(this, sel, val);
  5462. }
  5463. inline bool Sink::PutBool(Handlers::Selector sel, bool val) {
  5464. return upb_sink_putbool(this, sel, val);
  5465. }
  5466. inline bool Sink::StartString(Handlers::Selector sel, size_t size_hint,
  5467. Sink *sub) {
  5468. return upb_sink_startstr(this, sel, size_hint, sub);
  5469. }
  5470. inline size_t Sink::PutStringBuffer(Handlers::Selector sel, const char *buf,
  5471. size_t len, const BufferHandle* handle) {
  5472. return upb_sink_putstring(this, sel, buf, len, handle);
  5473. }
  5474. inline bool Sink::EndString(Handlers::Selector sel) {
  5475. return upb_sink_endstr(this, sel);
  5476. }
  5477. inline bool Sink::StartSubMessage(Handlers::Selector sel, Sink* sub) {
  5478. return upb_sink_startsubmsg(this, sel, sub);
  5479. }
  5480. inline bool Sink::EndSubMessage(Handlers::Selector sel) {
  5481. return upb_sink_endsubmsg(this, sel);
  5482. }
  5483. inline bool Sink::StartSequence(Handlers::Selector sel, Sink* sub) {
  5484. return upb_sink_startseq(this, sel, sub);
  5485. }
  5486. inline bool Sink::EndSequence(Handlers::Selector sel) {
  5487. return upb_sink_endseq(this, sel);
  5488. }
  5489. template <class T>
  5490. BytesSink::BytesSink(const BytesHandler* handler, T* closure) {
  5491. Reset(handler, closure);
  5492. }
  5493. template <class T>
  5494. void BytesSink::Reset(const BytesHandler *handler, T *closure) {
  5495. upb_bytessink_reset(this, handler, closure);
  5496. }
  5497. inline bool BytesSink::Start(size_t size_hint, void **subc) {
  5498. return upb_bytessink_start(this, size_hint, subc);
  5499. }
  5500. inline size_t BytesSink::PutBuffer(void *subc, const char *buf, size_t len,
  5501. const BufferHandle *handle) {
  5502. return upb_bytessink_putbuf(this, subc, buf, len, handle);
  5503. }
  5504. inline bool BytesSink::End() {
  5505. return upb_bytessink_end(this);
  5506. }
  5507. inline bool BufferSource::PutBuffer(const char *buf, size_t len,
  5508. BytesSink *sink) {
  5509. return upb_bufsrc_putbuf(buf, len, sink);
  5510. }
  5511. } /* namespace upb */
  5512. #endif
  5513. #endif
  5514. #ifdef __cplusplus
  5515. namespace upb {
  5516. class Array;
  5517. class Map;
  5518. class MapIterator;
  5519. class MessageLayout;
  5520. }
  5521. #endif
  5522. UPB_DECLARE_TYPE(upb::Array, upb_array)
  5523. UPB_DECLARE_TYPE(upb::Map, upb_map)
  5524. UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter)
  5525. /* TODO(haberman): C++ accessors */
  5526. UPB_BEGIN_EXTERN_C
  5527. typedef void upb_msg;
  5528. /** upb_msglayout *************************************************************/
  5529. /* upb_msglayout represents the memory layout of a given upb_msgdef. The
  5530. * members are public so generated code can initialize them, but users MUST NOT
  5531. * read or write any of its members. */
  5532. typedef struct {
  5533. uint32_t number;
  5534. uint16_t offset;
  5535. int16_t presence; /* If >0, hasbit_index+1. If <0, oneof_index+1. */
  5536. uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */
  5537. uint8_t descriptortype;
  5538. uint8_t label;
  5539. } upb_msglayout_field;
  5540. typedef struct upb_msglayout {
  5541. const struct upb_msglayout *const* submsgs;
  5542. const upb_msglayout_field *fields;
  5543. /* Must be aligned to sizeof(void*). Doesn't include internal members like
  5544. * unknown fields, extension dict, pointer to msglayout, etc. */
  5545. uint16_t size;
  5546. uint16_t field_count;
  5547. bool extendable;
  5548. } upb_msglayout;
  5549. /** upb_stringview ************************************************************/
  5550. typedef struct {
  5551. const char *data;
  5552. size_t size;
  5553. } upb_stringview;
  5554. UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) {
  5555. upb_stringview ret;
  5556. ret.data = data;
  5557. ret.size = size;
  5558. return ret;
  5559. }
  5560. #define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len}
  5561. /** upb_msgval ****************************************************************/
  5562. /* A union representing all possible protobuf values. Used for generic get/set
  5563. * operations. */
  5564. typedef union {
  5565. bool b;
  5566. float flt;
  5567. double dbl;
  5568. int32_t i32;
  5569. int64_t i64;
  5570. uint32_t u32;
  5571. uint64_t u64;
  5572. const upb_map* map;
  5573. const upb_msg* msg;
  5574. const upb_array* arr;
  5575. const void* ptr;
  5576. upb_stringview str;
  5577. } upb_msgval;
  5578. #define ACCESSORS(name, membername, ctype) \
  5579. UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \
  5580. return v.membername; \
  5581. } \
  5582. UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \
  5583. v->membername = cval; \
  5584. } \
  5585. UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \
  5586. upb_msgval ret; \
  5587. ret.membername = v; \
  5588. return ret; \
  5589. }
  5590. ACCESSORS(bool, b, bool)
  5591. ACCESSORS(float, flt, float)
  5592. ACCESSORS(double, dbl, double)
  5593. ACCESSORS(int32, i32, int32_t)
  5594. ACCESSORS(int64, i64, int64_t)
  5595. ACCESSORS(uint32, u32, uint32_t)
  5596. ACCESSORS(uint64, u64, uint64_t)
  5597. ACCESSORS(map, map, const upb_map*)
  5598. ACCESSORS(msg, msg, const upb_msg*)
  5599. ACCESSORS(ptr, ptr, const void*)
  5600. ACCESSORS(arr, arr, const upb_array*)
  5601. ACCESSORS(str, str, upb_stringview)
  5602. #undef ACCESSORS
  5603. UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
  5604. return upb_msgval_str(upb_stringview_make(data, size));
  5605. }
  5606. /** upb_msg *******************************************************************/
  5607. /* A upb_msg represents a protobuf message. It always corresponds to a specific
  5608. * upb_msglayout, which describes how it is laid out in memory. */
  5609. /* Creates a new message of the given type/layout in this arena. */
  5610. upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a);
  5611. /* Returns the arena for the given message. */
  5612. upb_arena *upb_msg_arena(const upb_msg *msg);
  5613. void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len);
  5614. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
  5615. /* Read-only message API. Can be safely called by anyone. */
  5616. /* Returns the value associated with this field:
  5617. * - for scalar fields (including strings), the value directly.
  5618. * - return upb_msg*, or upb_map* for msg/map.
  5619. * If the field is unset for these field types, returns NULL.
  5620. *
  5621. * TODO(haberman): should we let users store cached array/map/msg
  5622. * pointers here for fields that are unset? Could be useful for the
  5623. * strongly-owned submessage model (ie. generated C API that doesn't use
  5624. * arenas).
  5625. */
  5626. upb_msgval upb_msg_get(const upb_msg *msg,
  5627. int field_index,
  5628. const upb_msglayout *l);
  5629. /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
  5630. bool upb_msg_has(const upb_msg *msg,
  5631. int field_index,
  5632. const upb_msglayout *l);
  5633. /* Mutable message API. May only be called by the owner of the message who
  5634. * knows its ownership scheme and how to keep it consistent. */
  5635. /* Sets the given field to the given value. Does not perform any memory
  5636. * management: if you overwrite a pointer to a msg/array/map/string without
  5637. * cleaning it up (or using an arena) it will leak.
  5638. */
  5639. void upb_msg_set(upb_msg *msg,
  5640. int field_index,
  5641. upb_msgval val,
  5642. const upb_msglayout *l);
  5643. /* For a primitive field, set it back to its default. For repeated, string, and
  5644. * submessage fields set it back to NULL. This could involve releasing some
  5645. * internal memory (for example, from an extension dictionary), but it is not
  5646. * recursive in any way and will not recover any memory that may be used by
  5647. * arrays/maps/strings/msgs that this field may have pointed to.
  5648. */
  5649. bool upb_msg_clearfield(upb_msg *msg,
  5650. int field_index,
  5651. const upb_msglayout *l);
  5652. /* TODO(haberman): copyfrom()/mergefrom()? */
  5653. /** upb_array *****************************************************************/
  5654. /* A upb_array stores data for a repeated field. The memory management
  5655. * semantics are the same as upb_msg. A upb_array allocates dynamic
  5656. * memory internally for the array elements. */
  5657. upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a);
  5658. upb_fieldtype_t upb_array_type(const upb_array *arr);
  5659. /* Read-only interface. Safe for anyone to call. */
  5660. size_t upb_array_size(const upb_array *arr);
  5661. upb_msgval upb_array_get(const upb_array *arr, size_t i);
  5662. /* Write interface. May only be called by the message's owner who can enforce
  5663. * its memory management invariants. */
  5664. bool upb_array_set(upb_array *arr, size_t i, upb_msgval val);
  5665. /** upb_map *******************************************************************/
  5666. /* A upb_map stores data for a map field. The memory management semantics are
  5667. * the same as upb_msg, with one notable exception. upb_map will internally
  5668. * store a copy of all string keys, but *not* any string values or submessages.
  5669. * So you must ensure that any string or message values outlive the map, and you
  5670. * must delete them manually when they are no longer required. */
  5671. upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
  5672. upb_arena *a);
  5673. /* Read-only interface. Safe for anyone to call. */
  5674. size_t upb_map_size(const upb_map *map);
  5675. upb_fieldtype_t upb_map_keytype(const upb_map *map);
  5676. upb_fieldtype_t upb_map_valuetype(const upb_map *map);
  5677. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
  5678. /* Write interface. May only be called by the message's owner who can enforce
  5679. * its memory management invariants. */
  5680. /* Sets or overwrites an entry in the map. Return value indicates whether
  5681. * the operation succeeded or failed with OOM, and also whether an existing
  5682. * key was replaced or not. */
  5683. bool upb_map_set(upb_map *map,
  5684. upb_msgval key, upb_msgval val,
  5685. upb_msgval *valremoved);
  5686. /* Deletes an entry in the map. Returns true if the key was present. */
  5687. bool upb_map_del(upb_map *map, upb_msgval key);
  5688. /** upb_mapiter ***************************************************************/
  5689. /* For iterating over a map. Map iterators are invalidated by mutations to the
  5690. * map, but an invalidated iterator will never return junk or crash the process.
  5691. * An invalidated iterator may return entries that were already returned though,
  5692. * and if you keep invalidating the iterator during iteration, the program may
  5693. * enter an infinite loop. */
  5694. size_t upb_mapiter_sizeof();
  5695. void upb_mapiter_begin(upb_mapiter *i, const upb_map *t);
  5696. upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a);
  5697. void upb_mapiter_free(upb_mapiter *i, upb_alloc *a);
  5698. void upb_mapiter_next(upb_mapiter *i);
  5699. bool upb_mapiter_done(const upb_mapiter *i);
  5700. upb_msgval upb_mapiter_key(const upb_mapiter *i);
  5701. upb_msgval upb_mapiter_value(const upb_mapiter *i);
  5702. void upb_mapiter_setdone(upb_mapiter *i);
  5703. bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2);
  5704. UPB_END_EXTERN_C
  5705. #endif /* UPB_MSG_H_ */
  5706. /* This file was generated by upbc (the upb compiler) from the input
  5707. * file:
  5708. *
  5709. * google/protobuf/descriptor.proto
  5710. *
  5711. * Do not edit -- your changes will be discarded when the file is
  5712. * regenerated. */
  5713. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
  5714. #define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
  5715. /*
  5716. ** upb_decode: parsing into a upb_msg using a upb_msglayout.
  5717. */
  5718. #ifndef UPB_DECODE_H_
  5719. #define UPB_DECODE_H_
  5720. UPB_BEGIN_EXTERN_C
  5721. bool upb_decode(upb_stringview buf, upb_msg *msg, const upb_msglayout *l);
  5722. UPB_END_EXTERN_C
  5723. #endif /* UPB_DECODE_H_ */
  5724. /*
  5725. ** upb_encode: parsing into a upb_msg using a upb_msglayout.
  5726. */
  5727. #ifndef UPB_ENCODE_H_
  5728. #define UPB_ENCODE_H_
  5729. UPB_BEGIN_EXTERN_C
  5730. char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
  5731. size_t *size);
  5732. UPB_END_EXTERN_C
  5733. #endif /* UPB_ENCODE_H_ */
  5734. UPB_BEGIN_EXTERN_C
  5735. struct google_protobuf_FileDescriptorSet;
  5736. struct google_protobuf_FileDescriptorProto;
  5737. struct google_protobuf_DescriptorProto;
  5738. struct google_protobuf_DescriptorProto_ExtensionRange;
  5739. struct google_protobuf_DescriptorProto_ReservedRange;
  5740. struct google_protobuf_ExtensionRangeOptions;
  5741. struct google_protobuf_FieldDescriptorProto;
  5742. struct google_protobuf_OneofDescriptorProto;
  5743. struct google_protobuf_EnumDescriptorProto;
  5744. struct google_protobuf_EnumDescriptorProto_EnumReservedRange;
  5745. struct google_protobuf_EnumValueDescriptorProto;
  5746. struct google_protobuf_ServiceDescriptorProto;
  5747. struct google_protobuf_MethodDescriptorProto;
  5748. struct google_protobuf_FileOptions;
  5749. struct google_protobuf_MessageOptions;
  5750. struct google_protobuf_FieldOptions;
  5751. struct google_protobuf_OneofOptions;
  5752. struct google_protobuf_EnumOptions;
  5753. struct google_protobuf_EnumValueOptions;
  5754. struct google_protobuf_ServiceOptions;
  5755. struct google_protobuf_MethodOptions;
  5756. struct google_protobuf_UninterpretedOption;
  5757. struct google_protobuf_UninterpretedOption_NamePart;
  5758. struct google_protobuf_SourceCodeInfo;
  5759. struct google_protobuf_SourceCodeInfo_Location;
  5760. struct google_protobuf_GeneratedCodeInfo;
  5761. struct google_protobuf_GeneratedCodeInfo_Annotation;
  5762. typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet;
  5763. typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto;
  5764. typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto;
  5765. typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange;
  5766. typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange;
  5767. typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions;
  5768. typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto;
  5769. typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto;
  5770. typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto;
  5771. typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange;
  5772. typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto;
  5773. typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto;
  5774. typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto;
  5775. typedef struct google_protobuf_FileOptions google_protobuf_FileOptions;
  5776. typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions;
  5777. typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions;
  5778. typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions;
  5779. typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions;
  5780. typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions;
  5781. typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions;
  5782. typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions;
  5783. typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption;
  5784. typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart;
  5785. typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo;
  5786. typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location;
  5787. typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo;
  5788. typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation;
  5789. /* Enums */
  5790. typedef enum {
  5791. google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1,
  5792. google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2,
  5793. google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3
  5794. } google_protobuf_FieldDescriptorProto_Label;
  5795. typedef enum {
  5796. google_protobuf_FieldDescriptorProto_TYPE_DOUBLE = 1,
  5797. google_protobuf_FieldDescriptorProto_TYPE_FLOAT = 2,
  5798. google_protobuf_FieldDescriptorProto_TYPE_INT64 = 3,
  5799. google_protobuf_FieldDescriptorProto_TYPE_UINT64 = 4,
  5800. google_protobuf_FieldDescriptorProto_TYPE_INT32 = 5,
  5801. google_protobuf_FieldDescriptorProto_TYPE_FIXED64 = 6,
  5802. google_protobuf_FieldDescriptorProto_TYPE_FIXED32 = 7,
  5803. google_protobuf_FieldDescriptorProto_TYPE_BOOL = 8,
  5804. google_protobuf_FieldDescriptorProto_TYPE_STRING = 9,
  5805. google_protobuf_FieldDescriptorProto_TYPE_GROUP = 10,
  5806. google_protobuf_FieldDescriptorProto_TYPE_MESSAGE = 11,
  5807. google_protobuf_FieldDescriptorProto_TYPE_BYTES = 12,
  5808. google_protobuf_FieldDescriptorProto_TYPE_UINT32 = 13,
  5809. google_protobuf_FieldDescriptorProto_TYPE_ENUM = 14,
  5810. google_protobuf_FieldDescriptorProto_TYPE_SFIXED32 = 15,
  5811. google_protobuf_FieldDescriptorProto_TYPE_SFIXED64 = 16,
  5812. google_protobuf_FieldDescriptorProto_TYPE_SINT32 = 17,
  5813. google_protobuf_FieldDescriptorProto_TYPE_SINT64 = 18
  5814. } google_protobuf_FieldDescriptorProto_Type;
  5815. typedef enum {
  5816. google_protobuf_FieldOptions_STRING = 0,
  5817. google_protobuf_FieldOptions_CORD = 1,
  5818. google_protobuf_FieldOptions_STRING_PIECE = 2
  5819. } google_protobuf_FieldOptions_CType;
  5820. typedef enum {
  5821. google_protobuf_FieldOptions_JS_NORMAL = 0,
  5822. google_protobuf_FieldOptions_JS_STRING = 1,
  5823. google_protobuf_FieldOptions_JS_NUMBER = 2
  5824. } google_protobuf_FieldOptions_JSType;
  5825. typedef enum {
  5826. google_protobuf_FileOptions_SPEED = 1,
  5827. google_protobuf_FileOptions_CODE_SIZE = 2,
  5828. google_protobuf_FileOptions_LITE_RUNTIME = 3
  5829. } google_protobuf_FileOptions_OptimizeMode;
  5830. typedef enum {
  5831. google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0,
  5832. google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1,
  5833. google_protobuf_MethodOptions_IDEMPOTENT = 2
  5834. } google_protobuf_MethodOptions_IdempotencyLevel;
  5835. /* google.protobuf.FileDescriptorSet */
  5836. extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
  5837. UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) {
  5838. return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena);
  5839. }
  5840. UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_stringview buf, upb_arena *arena) {
  5841. google_protobuf_FileDescriptorSet *ret = google_protobuf_FileDescriptorSet_new(arena);
  5842. return (ret && upb_decode(buf, ret, &google_protobuf_FileDescriptorSet_msginit)) ? ret : NULL;
  5843. }
  5844. UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) {
  5845. return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len);
  5846. }
  5847. UPB_INLINE const upb_array* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(0, 0)); }
  5848. UPB_INLINE void google_protobuf_FileDescriptorSet_set_file(google_protobuf_FileDescriptorSet *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(0, 0)) = value; }
  5849. /* google.protobuf.FileDescriptorProto */
  5850. extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
  5851. UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) {
  5852. return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
  5853. }
  5854. UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  5855. google_protobuf_FileDescriptorProto *ret = google_protobuf_FileDescriptorProto_new(arena);
  5856. return (ret && upb_decode(buf, ret, &google_protobuf_FileDescriptorProto_msginit)) ? ret : NULL;
  5857. }
  5858. UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) {
  5859. return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len);
  5860. }
  5861. UPB_INLINE upb_stringview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  5862. UPB_INLINE upb_stringview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 32)); }
  5863. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(40, 80)); }
  5864. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(44, 88)); }
  5865. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(48, 96)); }
  5866. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(52, 104)); }
  5867. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(56, 112)); }
  5868. UPB_INLINE const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FileOptions*, UPB_SIZE(32, 64)); }
  5869. UPB_INLINE const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_SourceCodeInfo*, UPB_SIZE(36, 72)); }
  5870. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(60, 120)); }
  5871. UPB_INLINE const upb_array* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(64, 128)); }
  5872. UPB_INLINE upb_stringview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(24, 48)); }
  5873. UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  5874. UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 32)) = value; }
  5875. UPB_INLINE void google_protobuf_FileDescriptorProto_set_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(40, 80)) = value; }
  5876. UPB_INLINE void google_protobuf_FileDescriptorProto_set_message_type(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(44, 88)) = value; }
  5877. UPB_INLINE void google_protobuf_FileDescriptorProto_set_enum_type(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(48, 96)) = value; }
  5878. UPB_INLINE void google_protobuf_FileDescriptorProto_set_service(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(52, 104)) = value; }
  5879. UPB_INLINE void google_protobuf_FileDescriptorProto_set_extension(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(56, 112)) = value; }
  5880. UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) { UPB_FIELD_AT(msg, google_protobuf_FileOptions*, UPB_SIZE(32, 64)) = value; }
  5881. UPB_INLINE void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value) { UPB_FIELD_AT(msg, google_protobuf_SourceCodeInfo*, UPB_SIZE(36, 72)) = value; }
  5882. UPB_INLINE void google_protobuf_FileDescriptorProto_set_public_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(60, 120)) = value; }
  5883. UPB_INLINE void google_protobuf_FileDescriptorProto_set_weak_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(64, 128)) = value; }
  5884. UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(24, 48)) = value; }
  5885. /* google.protobuf.DescriptorProto */
  5886. extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
  5887. UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) {
  5888. return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  5889. }
  5890. UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  5891. google_protobuf_DescriptorProto *ret = google_protobuf_DescriptorProto_new(arena);
  5892. return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_msginit)) ? ret : NULL;
  5893. }
  5894. UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) {
  5895. return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len);
  5896. }
  5897. UPB_INLINE upb_stringview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  5898. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(20, 40)); }
  5899. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(24, 48)); }
  5900. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(28, 56)); }
  5901. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(32, 64)); }
  5902. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(36, 72)); }
  5903. UPB_INLINE const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MessageOptions*, UPB_SIZE(16, 32)); }
  5904. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(40, 80)); }
  5905. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(44, 88)); }
  5906. UPB_INLINE const upb_array* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(48, 96)); }
  5907. UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  5908. UPB_INLINE void google_protobuf_DescriptorProto_set_field(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(20, 40)) = value; }
  5909. UPB_INLINE void google_protobuf_DescriptorProto_set_nested_type(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(24, 48)) = value; }
  5910. UPB_INLINE void google_protobuf_DescriptorProto_set_enum_type(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(28, 56)) = value; }
  5911. UPB_INLINE void google_protobuf_DescriptorProto_set_extension_range(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(32, 64)) = value; }
  5912. UPB_INLINE void google_protobuf_DescriptorProto_set_extension(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(36, 72)) = value; }
  5913. UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) { UPB_FIELD_AT(msg, google_protobuf_MessageOptions*, UPB_SIZE(16, 32)) = value; }
  5914. UPB_INLINE void google_protobuf_DescriptorProto_set_oneof_decl(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(40, 80)) = value; }
  5915. UPB_INLINE void google_protobuf_DescriptorProto_set_reserved_range(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(44, 88)) = value; }
  5916. UPB_INLINE void google_protobuf_DescriptorProto_set_reserved_name(google_protobuf_DescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(48, 96)) = value; }
  5917. /* google.protobuf.DescriptorProto.ExtensionRange */
  5918. extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
  5919. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) {
  5920. return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
  5921. }
  5922. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_stringview buf, upb_arena *arena) {
  5923. google_protobuf_DescriptorProto_ExtensionRange *ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena);
  5924. return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_ExtensionRange_msginit)) ? ret : NULL;
  5925. }
  5926. UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) {
  5927. return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len);
  5928. }
  5929. UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  5930. UPB_INLINE int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  5931. UPB_INLINE const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)); }
  5932. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; }
  5933. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; }
  5934. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) { UPB_FIELD_AT(msg, google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)) = value; }
  5935. /* google.protobuf.DescriptorProto.ReservedRange */
  5936. extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
  5937. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) {
  5938. return (google_protobuf_DescriptorProto_ReservedRange *)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
  5939. }
  5940. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_stringview buf, upb_arena *arena) {
  5941. google_protobuf_DescriptorProto_ReservedRange *ret = google_protobuf_DescriptorProto_ReservedRange_new(arena);
  5942. return (ret && upb_decode(buf, ret, &google_protobuf_DescriptorProto_ReservedRange_msginit)) ? ret : NULL;
  5943. }
  5944. UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) {
  5945. return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len);
  5946. }
  5947. UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  5948. UPB_INLINE int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  5949. UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; }
  5950. UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; }
  5951. /* google.protobuf.ExtensionRangeOptions */
  5952. extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
  5953. UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) {
  5954. return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
  5955. }
  5956. UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  5957. google_protobuf_ExtensionRangeOptions *ret = google_protobuf_ExtensionRangeOptions_new(arena);
  5958. return (ret && upb_decode(buf, ret, &google_protobuf_ExtensionRangeOptions_msginit)) ? ret : NULL;
  5959. }
  5960. UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) {
  5961. return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len);
  5962. }
  5963. UPB_INLINE const upb_array* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(0, 0)); }
  5964. UPB_INLINE void google_protobuf_ExtensionRangeOptions_set_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(0, 0)) = value; }
  5965. /* google.protobuf.FieldDescriptorProto */
  5966. extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
  5967. UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) {
  5968. return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  5969. }
  5970. UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  5971. google_protobuf_FieldDescriptorProto *ret = google_protobuf_FieldDescriptorProto_new(arena);
  5972. return (ret && upb_decode(buf, ret, &google_protobuf_FieldDescriptorProto_msginit)) ? ret : NULL;
  5973. }
  5974. UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) {
  5975. return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len);
  5976. }
  5977. UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)); }
  5978. UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)); }
  5979. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); }
  5980. UPB_INLINE google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)); }
  5981. UPB_INLINE google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)); }
  5982. UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)); }
  5983. UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(56, 80)); }
  5984. UPB_INLINE const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_FieldOptions*, UPB_SIZE(72, 112)); }
  5985. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)); }
  5986. UPB_INLINE upb_stringview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(64, 96)); }
  5987. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)) = value; }
  5988. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)) = value; }
  5989. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)) = value; }
  5990. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Label value) { UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Label, UPB_SIZE(8, 8)) = value; }
  5991. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Type value) { UPB_FIELD_AT(msg, google_protobuf_FieldDescriptorProto_Type, UPB_SIZE(16, 16)) = value; }
  5992. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)) = value; }
  5993. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(56, 80)) = value; }
  5994. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) { UPB_FIELD_AT(msg, google_protobuf_FieldOptions*, UPB_SIZE(72, 112)) = value; }
  5995. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)) = value; }
  5996. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(64, 96)) = value; }
  5997. /* google.protobuf.OneofDescriptorProto */
  5998. extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
  5999. UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) {
  6000. return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
  6001. }
  6002. UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  6003. google_protobuf_OneofDescriptorProto *ret = google_protobuf_OneofDescriptorProto_new(arena);
  6004. return (ret && upb_decode(buf, ret, &google_protobuf_OneofDescriptorProto_msginit)) ? ret : NULL;
  6005. }
  6006. UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) {
  6007. return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len);
  6008. }
  6009. UPB_INLINE upb_stringview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6010. UPB_INLINE const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_OneofOptions*, UPB_SIZE(16, 32)); }
  6011. UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6012. UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) { UPB_FIELD_AT(msg, google_protobuf_OneofOptions*, UPB_SIZE(16, 32)) = value; }
  6013. /* google.protobuf.EnumDescriptorProto */
  6014. extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
  6015. UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) {
  6016. return (google_protobuf_EnumDescriptorProto *)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  6017. }
  6018. UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  6019. google_protobuf_EnumDescriptorProto *ret = google_protobuf_EnumDescriptorProto_new(arena);
  6020. return (ret && upb_decode(buf, ret, &google_protobuf_EnumDescriptorProto_msginit)) ? ret : NULL;
  6021. }
  6022. UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) {
  6023. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len);
  6024. }
  6025. UPB_INLINE upb_stringview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6026. UPB_INLINE const upb_array* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(20, 40)); }
  6027. UPB_INLINE const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumOptions*, UPB_SIZE(16, 32)); }
  6028. UPB_INLINE const upb_array* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(24, 48)); }
  6029. UPB_INLINE const upb_array* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(28, 56)); }
  6030. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6031. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_value(google_protobuf_EnumDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(20, 40)) = value; }
  6032. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) { UPB_FIELD_AT(msg, google_protobuf_EnumOptions*, UPB_SIZE(16, 32)) = value; }
  6033. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(24, 48)) = value; }
  6034. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(28, 56)) = value; }
  6035. /* google.protobuf.EnumDescriptorProto.EnumReservedRange */
  6036. extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
  6037. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) {
  6038. return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
  6039. }
  6040. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_stringview buf, upb_arena *arena) {
  6041. google_protobuf_EnumDescriptorProto_EnumReservedRange *ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena);
  6042. return (ret && upb_decode(buf, ret, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit)) ? ret : NULL;
  6043. }
  6044. UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) {
  6045. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len);
  6046. }
  6047. UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  6048. UPB_INLINE int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  6049. UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; }
  6050. UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; }
  6051. /* google.protobuf.EnumValueDescriptorProto */
  6052. extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
  6053. UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) {
  6054. return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
  6055. }
  6056. UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  6057. google_protobuf_EnumValueDescriptorProto *ret = google_protobuf_EnumValueDescriptorProto_new(arena);
  6058. return (ret && upb_decode(buf, ret, &google_protobuf_EnumValueDescriptorProto_msginit)) ? ret : NULL;
  6059. }
  6060. UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) {
  6061. return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len);
  6062. }
  6063. UPB_INLINE upb_stringview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6064. UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  6065. UPB_INLINE const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_EnumValueOptions*, UPB_SIZE(16, 32)); }
  6066. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6067. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; }
  6068. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) { UPB_FIELD_AT(msg, google_protobuf_EnumValueOptions*, UPB_SIZE(16, 32)) = value; }
  6069. /* google.protobuf.ServiceDescriptorProto */
  6070. extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
  6071. UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) {
  6072. return (google_protobuf_ServiceDescriptorProto *)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
  6073. }
  6074. UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  6075. google_protobuf_ServiceDescriptorProto *ret = google_protobuf_ServiceDescriptorProto_new(arena);
  6076. return (ret && upb_decode(buf, ret, &google_protobuf_ServiceDescriptorProto_msginit)) ? ret : NULL;
  6077. }
  6078. UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) {
  6079. return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len);
  6080. }
  6081. UPB_INLINE upb_stringview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6082. UPB_INLINE const upb_array* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(20, 40)); }
  6083. UPB_INLINE const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_ServiceOptions*, UPB_SIZE(16, 32)); }
  6084. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6085. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_method(google_protobuf_ServiceDescriptorProto *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(20, 40)) = value; }
  6086. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) { UPB_FIELD_AT(msg, google_protobuf_ServiceOptions*, UPB_SIZE(16, 32)) = value; }
  6087. /* google.protobuf.MethodDescriptorProto */
  6088. extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
  6089. UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) {
  6090. return (google_protobuf_MethodDescriptorProto *)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
  6091. }
  6092. UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_stringview buf, upb_arena *arena) {
  6093. google_protobuf_MethodDescriptorProto *ret = google_protobuf_MethodDescriptorProto_new(arena);
  6094. return (ret && upb_decode(buf, ret, &google_protobuf_MethodDescriptorProto_msginit)) ? ret : NULL;
  6095. }
  6096. UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) {
  6097. return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len);
  6098. }
  6099. UPB_INLINE upb_stringview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6100. UPB_INLINE upb_stringview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 32)); }
  6101. UPB_INLINE upb_stringview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(24, 48)); }
  6102. UPB_INLINE const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, const google_protobuf_MethodOptions*, UPB_SIZE(32, 64)); }
  6103. UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  6104. UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  6105. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6106. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 32)) = value; }
  6107. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(24, 48)) = value; }
  6108. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) { UPB_FIELD_AT(msg, google_protobuf_MethodOptions*, UPB_SIZE(32, 64)) = value; }
  6109. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; }
  6110. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; }
  6111. /* google.protobuf.FileOptions */
  6112. extern const upb_msglayout google_protobuf_FileOptions_msginit;
  6113. UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) {
  6114. return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
  6115. }
  6116. UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6117. google_protobuf_FileOptions *ret = google_protobuf_FileOptions_new(arena);
  6118. return (ret && upb_decode(buf, ret, &google_protobuf_FileOptions_msginit)) ? ret : NULL;
  6119. }
  6120. UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) {
  6121. return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len);
  6122. }
  6123. UPB_INLINE upb_stringview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)); }
  6124. UPB_INLINE upb_stringview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)); }
  6125. UPB_INLINE google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)); }
  6126. UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
  6127. UPB_INLINE upb_stringview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)); }
  6128. UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); }
  6129. UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); }
  6130. UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); }
  6131. UPB_INLINE bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)); }
  6132. UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); }
  6133. UPB_INLINE bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)); }
  6134. UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); }
  6135. UPB_INLINE upb_stringview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(56, 80)); }
  6136. UPB_INLINE upb_stringview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(64, 96)); }
  6137. UPB_INLINE upb_stringview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(72, 112)); }
  6138. UPB_INLINE upb_stringview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(80, 128)); }
  6139. UPB_INLINE upb_stringview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(88, 144)); }
  6140. UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
  6141. UPB_INLINE const upb_array* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(96, 160)); }
  6142. UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)) = value; }
  6143. UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)) = value; }
  6144. UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, google_protobuf_FileOptions_OptimizeMode value) { UPB_FIELD_AT(msg, google_protobuf_FileOptions_OptimizeMode, UPB_SIZE(8, 8)) = value; }
  6145. UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value; }
  6146. UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)) = value; }
  6147. UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)) = value; }
  6148. UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)) = value; }
  6149. UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)) = value; }
  6150. UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)) = value; }
  6151. UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)) = value; }
  6152. UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)) = value; }
  6153. UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)) = value; }
  6154. UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(56, 80)) = value; }
  6155. UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(64, 96)) = value; }
  6156. UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(72, 112)) = value; }
  6157. UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(80, 128)) = value; }
  6158. UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(88, 144)) = value; }
  6159. UPB_INLINE void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value; }
  6160. UPB_INLINE void google_protobuf_FileOptions_set_uninterpreted_option(google_protobuf_FileOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(96, 160)) = value; }
  6161. /* google.protobuf.MessageOptions */
  6162. extern const upb_msglayout google_protobuf_MessageOptions_msginit;
  6163. UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) {
  6164. return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
  6165. }
  6166. UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6167. google_protobuf_MessageOptions *ret = google_protobuf_MessageOptions_new(arena);
  6168. return (ret && upb_decode(buf, ret, &google_protobuf_MessageOptions_msginit)) ? ret : NULL;
  6169. }
  6170. UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) {
  6171. return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len);
  6172. }
  6173. UPB_INLINE bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  6174. UPB_INLINE bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  6175. UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); }
  6176. UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); }
  6177. UPB_INLINE const upb_array* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(8, 8)); }
  6178. UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; }
  6179. UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; }
  6180. UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)) = value; }
  6181. UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)) = value; }
  6182. UPB_INLINE void google_protobuf_MessageOptions_set_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(8, 8)) = value; }
  6183. /* google.protobuf.FieldOptions */
  6184. extern const upb_msglayout google_protobuf_FieldOptions_msginit;
  6185. UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) {
  6186. return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
  6187. }
  6188. UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6189. google_protobuf_FieldOptions *ret = google_protobuf_FieldOptions_new(arena);
  6190. return (ret && upb_decode(buf, ret, &google_protobuf_FieldOptions_msginit)) ? ret : NULL;
  6191. }
  6192. UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) {
  6193. return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len);
  6194. }
  6195. UPB_INLINE google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)); }
  6196. UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
  6197. UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); }
  6198. UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); }
  6199. UPB_INLINE google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)); }
  6200. UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); }
  6201. UPB_INLINE const upb_array* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(28, 32)); }
  6202. UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_CType value) { UPB_FIELD_AT(msg, google_protobuf_FieldOptions_CType, UPB_SIZE(8, 8)) = value; }
  6203. UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value; }
  6204. UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)) = value; }
  6205. UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)) = value; }
  6206. UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_JSType value) { UPB_FIELD_AT(msg, google_protobuf_FieldOptions_JSType, UPB_SIZE(16, 16)) = value; }
  6207. UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)) = value; }
  6208. UPB_INLINE void google_protobuf_FieldOptions_set_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(28, 32)) = value; }
  6209. /* google.protobuf.OneofOptions */
  6210. extern const upb_msglayout google_protobuf_OneofOptions_msginit;
  6211. UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) {
  6212. return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
  6213. }
  6214. UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6215. google_protobuf_OneofOptions *ret = google_protobuf_OneofOptions_new(arena);
  6216. return (ret && upb_decode(buf, ret, &google_protobuf_OneofOptions_msginit)) ? ret : NULL;
  6217. }
  6218. UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) {
  6219. return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len);
  6220. }
  6221. UPB_INLINE const upb_array* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(0, 0)); }
  6222. UPB_INLINE void google_protobuf_OneofOptions_set_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(0, 0)) = value; }
  6223. /* google.protobuf.EnumOptions */
  6224. extern const upb_msglayout google_protobuf_EnumOptions_msginit;
  6225. UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) {
  6226. return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
  6227. }
  6228. UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6229. google_protobuf_EnumOptions *ret = google_protobuf_EnumOptions_new(arena);
  6230. return (ret && upb_decode(buf, ret, &google_protobuf_EnumOptions_msginit)) ? ret : NULL;
  6231. }
  6232. UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) {
  6233. return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len);
  6234. }
  6235. UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  6236. UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  6237. UPB_INLINE const upb_array* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(4, 8)); }
  6238. UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; }
  6239. UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value; }
  6240. UPB_INLINE void google_protobuf_EnumOptions_set_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(4, 8)) = value; }
  6241. /* google.protobuf.EnumValueOptions */
  6242. extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
  6243. UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) {
  6244. return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
  6245. }
  6246. UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6247. google_protobuf_EnumValueOptions *ret = google_protobuf_EnumValueOptions_new(arena);
  6248. return (ret && upb_decode(buf, ret, &google_protobuf_EnumValueOptions_msginit)) ? ret : NULL;
  6249. }
  6250. UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) {
  6251. return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len);
  6252. }
  6253. UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  6254. UPB_INLINE const upb_array* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(4, 8)); }
  6255. UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; }
  6256. UPB_INLINE void google_protobuf_EnumValueOptions_set_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(4, 8)) = value; }
  6257. /* google.protobuf.ServiceOptions */
  6258. extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
  6259. UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) {
  6260. return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
  6261. }
  6262. UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6263. google_protobuf_ServiceOptions *ret = google_protobuf_ServiceOptions_new(arena);
  6264. return (ret && upb_decode(buf, ret, &google_protobuf_ServiceOptions_msginit)) ? ret : NULL;
  6265. }
  6266. UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) {
  6267. return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len);
  6268. }
  6269. UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  6270. UPB_INLINE const upb_array* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(4, 8)); }
  6271. UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; }
  6272. UPB_INLINE void google_protobuf_ServiceOptions_set_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(4, 8)) = value; }
  6273. /* google.protobuf.MethodOptions */
  6274. extern const upb_msglayout google_protobuf_MethodOptions_msginit;
  6275. UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) {
  6276. return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
  6277. }
  6278. UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_stringview buf, upb_arena *arena) {
  6279. google_protobuf_MethodOptions *ret = google_protobuf_MethodOptions_new(arena);
  6280. return (ret && upb_decode(buf, ret, &google_protobuf_MethodOptions_msginit)) ? ret : NULL;
  6281. }
  6282. UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) {
  6283. return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len);
  6284. }
  6285. UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
  6286. UPB_INLINE google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)); }
  6287. UPB_INLINE const upb_array* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(20, 24)); }
  6288. UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value; }
  6289. UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, google_protobuf_MethodOptions_IdempotencyLevel value) { UPB_FIELD_AT(msg, google_protobuf_MethodOptions_IdempotencyLevel, UPB_SIZE(8, 8)) = value; }
  6290. UPB_INLINE void google_protobuf_MethodOptions_set_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(20, 24)) = value; }
  6291. /* google.protobuf.UninterpretedOption */
  6292. extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
  6293. UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) {
  6294. return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  6295. }
  6296. UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_stringview buf, upb_arena *arena) {
  6297. google_protobuf_UninterpretedOption *ret = google_protobuf_UninterpretedOption_new(arena);
  6298. return (ret && upb_decode(buf, ret, &google_protobuf_UninterpretedOption_msginit)) ? ret : NULL;
  6299. }
  6300. UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) {
  6301. return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len);
  6302. }
  6303. UPB_INLINE const upb_array* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(56, 80)); }
  6304. UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)); }
  6305. UPB_INLINE uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)); }
  6306. UPB_INLINE int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)); }
  6307. UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); }
  6308. UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)); }
  6309. UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)); }
  6310. UPB_INLINE void google_protobuf_UninterpretedOption_set_name(google_protobuf_UninterpretedOption *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(56, 80)) = value; }
  6311. UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(32, 32)) = value; }
  6312. UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) { UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)) = value; }
  6313. UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) { UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)) = value; }
  6314. UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) { UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)) = value; }
  6315. UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(40, 48)) = value; }
  6316. UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(48, 64)) = value; }
  6317. /* google.protobuf.UninterpretedOption.NamePart */
  6318. extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
  6319. UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) {
  6320. return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
  6321. }
  6322. UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_stringview buf, upb_arena *arena) {
  6323. google_protobuf_UninterpretedOption_NamePart *ret = google_protobuf_UninterpretedOption_NamePart_new(arena);
  6324. return (ret && upb_decode(buf, ret, &google_protobuf_UninterpretedOption_NamePart_msginit)) ? ret : NULL;
  6325. }
  6326. UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) {
  6327. return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len);
  6328. }
  6329. UPB_INLINE upb_stringview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6330. UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  6331. UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6332. UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) { UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value; }
  6333. /* google.protobuf.SourceCodeInfo */
  6334. extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
  6335. UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) {
  6336. return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
  6337. }
  6338. UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_stringview buf, upb_arena *arena) {
  6339. google_protobuf_SourceCodeInfo *ret = google_protobuf_SourceCodeInfo_new(arena);
  6340. return (ret && upb_decode(buf, ret, &google_protobuf_SourceCodeInfo_msginit)) ? ret : NULL;
  6341. }
  6342. UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) {
  6343. return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len);
  6344. }
  6345. UPB_INLINE const upb_array* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(0, 0)); }
  6346. UPB_INLINE void google_protobuf_SourceCodeInfo_set_location(google_protobuf_SourceCodeInfo *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(0, 0)) = value; }
  6347. /* google.protobuf.SourceCodeInfo.Location */
  6348. extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
  6349. UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) {
  6350. return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
  6351. }
  6352. UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_stringview buf, upb_arena *arena) {
  6353. google_protobuf_SourceCodeInfo_Location *ret = google_protobuf_SourceCodeInfo_Location_new(arena);
  6354. return (ret && upb_decode(buf, ret, &google_protobuf_SourceCodeInfo_Location_msginit)) ? ret : NULL;
  6355. }
  6356. UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) {
  6357. return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len);
  6358. }
  6359. UPB_INLINE const upb_array* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(24, 48)); }
  6360. UPB_INLINE const upb_array* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(28, 56)); }
  6361. UPB_INLINE upb_stringview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)); }
  6362. UPB_INLINE upb_stringview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 32)); }
  6363. UPB_INLINE const upb_array* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(32, 64)); }
  6364. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_path(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(24, 48)) = value; }
  6365. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_span(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(28, 56)) = value; }
  6366. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(8, 16)) = value; }
  6367. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 32)) = value; }
  6368. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(32, 64)) = value; }
  6369. /* google.protobuf.GeneratedCodeInfo */
  6370. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
  6371. UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) {
  6372. return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena);
  6373. }
  6374. UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_stringview buf, upb_arena *arena) {
  6375. google_protobuf_GeneratedCodeInfo *ret = google_protobuf_GeneratedCodeInfo_new(arena);
  6376. return (ret && upb_decode(buf, ret, &google_protobuf_GeneratedCodeInfo_msginit)) ? ret : NULL;
  6377. }
  6378. UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) {
  6379. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len);
  6380. }
  6381. UPB_INLINE const upb_array* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(0, 0)); }
  6382. UPB_INLINE void google_protobuf_GeneratedCodeInfo_set_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(0, 0)) = value; }
  6383. /* google.protobuf.GeneratedCodeInfo.Annotation */
  6384. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
  6385. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) {
  6386. return (google_protobuf_GeneratedCodeInfo_Annotation *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
  6387. }
  6388. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_stringview buf, upb_arena *arena) {
  6389. google_protobuf_GeneratedCodeInfo_Annotation *ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena);
  6390. return (ret && upb_decode(buf, ret, &google_protobuf_GeneratedCodeInfo_Annotation_msginit)) ? ret : NULL;
  6391. }
  6392. UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) {
  6393. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len);
  6394. }
  6395. UPB_INLINE const upb_array* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, const upb_array*, UPB_SIZE(24, 32)); }
  6396. UPB_INLINE upb_stringview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 16)); }
  6397. UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  6398. UPB_INLINE int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  6399. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_array* value) { UPB_FIELD_AT(msg, upb_array*, UPB_SIZE(24, 32)) = value; }
  6400. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_stringview value) { UPB_FIELD_AT(msg, upb_stringview, UPB_SIZE(16, 16)) = value; }
  6401. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value; }
  6402. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) { UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value; }
  6403. UPB_END_EXTERN_C
  6404. #endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */
  6405. /*
  6406. ** structs.int.h: structures definitions that are internal to upb.
  6407. */
  6408. #ifndef UPB_STRUCTS_H_
  6409. #define UPB_STRUCTS_H_
  6410. struct upb_array {
  6411. upb_fieldtype_t type;
  6412. uint8_t element_size;
  6413. void *data; /* Each element is element_size. */
  6414. size_t len; /* Measured in elements. */
  6415. size_t size; /* Measured in elements. */
  6416. upb_arena *arena;
  6417. };
  6418. #endif /* UPB_STRUCTS_H_ */
  6419. /*
  6420. ** This file contains definitions of structs that should be considered private
  6421. ** and NOT stable across versions of upb.
  6422. **
  6423. ** The only reason they are declared here and not in .c files is to allow upb
  6424. ** and the application (if desired) to embed statically-initialized instances
  6425. ** of structures like defs.
  6426. **
  6427. ** If you include this file, all guarantees of ABI compatibility go out the
  6428. ** window! Any code that includes this file needs to recompile against the
  6429. ** exact same version of upb that they are linking against.
  6430. **
  6431. ** You also need to recompile if you change the value of the UPB_DEBUG_REFS
  6432. ** flag.
  6433. */
  6434. #ifndef UPB_STATICINIT_H_
  6435. #define UPB_STATICINIT_H_
  6436. #ifdef __cplusplus
  6437. /* Because of how we do our typedefs, this header can't be included from C++. */
  6438. #error This file cannot be included from C++
  6439. #endif
  6440. /* upb_refcounted *************************************************************/
  6441. /* upb_def ********************************************************************/
  6442. struct upb_def {
  6443. upb_refcounted base;
  6444. const char *fullname;
  6445. const upb_filedef* file;
  6446. char type; /* A upb_deftype_t (char to save space) */
  6447. /* Used as a flag during the def's mutable stage. Must be false unless
  6448. * it is currently being used by a function on the stack. This allows
  6449. * us to easily determine which defs were passed into the function's
  6450. * current invocation. */
  6451. bool came_from_user;
  6452. };
  6453. #define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \
  6454. { UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false }
  6455. /* upb_fielddef ***************************************************************/
  6456. struct upb_fielddef {
  6457. upb_def base;
  6458. union {
  6459. int64_t sint;
  6460. uint64_t uint;
  6461. double dbl;
  6462. float flt;
  6463. void *bytes;
  6464. } defaultval;
  6465. union {
  6466. const upb_msgdef *def; /* If !msg_is_symbolic. */
  6467. char *name; /* If msg_is_symbolic. */
  6468. } msg;
  6469. union {
  6470. const upb_def *def; /* If !subdef_is_symbolic. */
  6471. char *name; /* If subdef_is_symbolic. */
  6472. } sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */
  6473. bool subdef_is_symbolic;
  6474. bool msg_is_symbolic;
  6475. const upb_oneofdef *oneof;
  6476. bool default_is_string;
  6477. bool type_is_set_; /* False until type is explicitly set. */
  6478. bool is_extension_;
  6479. bool lazy_;
  6480. bool packed_;
  6481. upb_intfmt_t intfmt;
  6482. bool tagdelim;
  6483. upb_fieldtype_t type_;
  6484. upb_label_t label_;
  6485. uint32_t number_;
  6486. uint32_t selector_base; /* Used to index into a upb::Handlers table. */
  6487. uint32_t index_;
  6488. };
  6489. extern const struct upb_refcounted_vtbl upb_fielddef_vtbl;
  6490. #define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
  6491. packed, name, num, msgdef, subdef, selector_base, \
  6492. index, defaultval, refs, ref2s) \
  6493. { \
  6494. UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \
  6495. defaultval, {msgdef}, {subdef}, NULL, false, false, \
  6496. type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
  6497. lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
  6498. }
  6499. /* upb_msgdef *****************************************************************/
  6500. struct upb_msgdef {
  6501. upb_def base;
  6502. size_t selector_count;
  6503. uint32_t submsg_field_count;
  6504. /* Tables for looking up fields by number and name. */
  6505. upb_inttable itof; /* int to field */
  6506. upb_strtable ntof; /* name to field/oneof */
  6507. /* Is this a map-entry message? */
  6508. bool map_entry;
  6509. /* Whether this message has proto2 or proto3 semantics. */
  6510. upb_syntax_t syntax;
  6511. /* Type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
  6512. * non-well-known message. */
  6513. upb_wellknowntype_t well_known_type;
  6514. /* TODO(haberman): proper extension ranges (there can be multiple). */
  6515. };
  6516. extern const struct upb_refcounted_vtbl upb_msgdef_vtbl;
  6517. /* TODO: also support static initialization of the oneofs table. This will be
  6518. * needed if we compile in descriptors that contain oneofs. */
  6519. #define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
  6520. map_entry, syntax, well_known_type, refs, ref2s) \
  6521. { \
  6522. UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \
  6523. selector_count, submsg_field_count, itof, ntof, map_entry, syntax, \
  6524. well_known_type \
  6525. }
  6526. /* upb_enumdef ****************************************************************/
  6527. struct upb_enumdef {
  6528. upb_def base;
  6529. upb_strtable ntoi;
  6530. upb_inttable iton;
  6531. int32_t defaultval;
  6532. };
  6533. extern const struct upb_refcounted_vtbl upb_enumdef_vtbl;
  6534. #define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
  6535. { UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \
  6536. iton, defaultval }
  6537. /* upb_oneofdef ***************************************************************/
  6538. struct upb_oneofdef {
  6539. upb_refcounted base;
  6540. uint32_t index; /* Index within oneofs. */
  6541. const char *name;
  6542. upb_strtable ntof;
  6543. upb_inttable itof;
  6544. const upb_msgdef *parent;
  6545. };
  6546. extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl;
  6547. #define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
  6548. { UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof }
  6549. /* upb_symtab *****************************************************************/
  6550. struct upb_symtab {
  6551. upb_refcounted base;
  6552. upb_strtable symtab;
  6553. };
  6554. struct upb_filedef {
  6555. upb_refcounted base;
  6556. const char *name;
  6557. const char *package;
  6558. const char *phpprefix;
  6559. const char *phpnamespace;
  6560. upb_syntax_t syntax;
  6561. upb_inttable defs;
  6562. upb_inttable deps;
  6563. };
  6564. extern const struct upb_refcounted_vtbl upb_filedef_vtbl;
  6565. #endif /* UPB_STATICINIT_H_ */
  6566. #ifndef UPB_MSGFACTORY_H_
  6567. #define UPB_MSGFACTORY_H_
  6568. UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory)
  6569. /** upb_msgfactory ************************************************************/
  6570. /* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and
  6571. * upb_visitorplan objects. These are the objects necessary to represent,
  6572. * populate, and and visit upb_msg objects.
  6573. *
  6574. * These caches are all populated by upb_msgdef, and lazily created on demand.
  6575. */
  6576. /* Creates and destroys a msgfactory, respectively. The messages for this
  6577. * msgfactory must come from |symtab| (which should outlive the msgfactory). */
  6578. upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab);
  6579. void upb_msgfactory_free(upb_msgfactory *f);
  6580. const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f);
  6581. /* The functions to get cached objects, lazily creating them on demand. These
  6582. * all require:
  6583. *
  6584. * - m is in upb_msgfactory_symtab(f)
  6585. * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts).
  6586. *
  6587. * The returned objects will live for as long as the msgfactory does.
  6588. *
  6589. * TODO(haberman): consider making this thread-safe and take a const
  6590. * upb_msgfactory. */
  6591. const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f,
  6592. const upb_msgdef *m);
  6593. #endif /* UPB_MSGFACTORY_H_ */
  6594. /*
  6595. ** upb::descriptor::Reader (upb_descreader)
  6596. **
  6597. ** Provides a way of building upb::Defs from data in descriptor.proto format.
  6598. */
  6599. #ifndef UPB_DESCRIPTOR_H
  6600. #define UPB_DESCRIPTOR_H
  6601. #ifdef __cplusplus
  6602. namespace upb {
  6603. namespace descriptor {
  6604. class Reader;
  6605. } /* namespace descriptor */
  6606. } /* namespace upb */
  6607. #endif
  6608. UPB_DECLARE_TYPE(upb::descriptor::Reader, upb_descreader)
  6609. #ifdef __cplusplus
  6610. /* Class that receives descriptor data according to the descriptor.proto schema
  6611. * and use it to build upb::Defs corresponding to that schema. */
  6612. class upb::descriptor::Reader {
  6613. public:
  6614. /* These handlers must have come from NewHandlers() and must outlive the
  6615. * Reader.
  6616. *
  6617. * TODO: generate the handlers statically (like we do with the
  6618. * descriptor.proto defs) so that there is no need to pass this parameter (or
  6619. * to build/memory-manage the handlers at runtime at all). Unfortunately this
  6620. * is a bit tricky to implement for Handlers, but necessary to simplify this
  6621. * interface. */
  6622. static Reader* Create(Environment* env, const Handlers* handlers);
  6623. /* The reader's input; this is where descriptor.proto data should be sent. */
  6624. Sink* input();
  6625. /* Use to get the FileDefs that have been parsed. */
  6626. size_t file_count() const;
  6627. FileDef* file(size_t i) const;
  6628. /* Builds and returns handlers for the reader, owned by "owner." */
  6629. static Handlers* NewHandlers(const void* owner);
  6630. private:
  6631. UPB_DISALLOW_POD_OPS(Reader, upb::descriptor::Reader)
  6632. };
  6633. #endif
  6634. UPB_BEGIN_EXTERN_C
  6635. /* C API. */
  6636. upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h);
  6637. upb_sink *upb_descreader_input(upb_descreader *r);
  6638. size_t upb_descreader_filecount(const upb_descreader *r);
  6639. upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i);
  6640. const upb_handlers *upb_descreader_newhandlers(const void *owner);
  6641. UPB_END_EXTERN_C
  6642. #ifdef __cplusplus
  6643. /* C++ implementation details. ************************************************/
  6644. namespace upb {
  6645. namespace descriptor {
  6646. inline Reader* Reader::Create(Environment* e, const Handlers *h) {
  6647. return upb_descreader_create(e, h);
  6648. }
  6649. inline Sink* Reader::input() { return upb_descreader_input(this); }
  6650. inline size_t Reader::file_count() const {
  6651. return upb_descreader_filecount(this);
  6652. }
  6653. inline FileDef* Reader::file(size_t i) const {
  6654. return upb_descreader_file(this, i);
  6655. }
  6656. } /* namespace descriptor */
  6657. } /* namespace upb */
  6658. #endif
  6659. #endif /* UPB_DESCRIPTOR_H */
  6660. /* This file contains accessors for a set of compiled-in defs.
  6661. * Note that unlike Google's protobuf, it does *not* define
  6662. * generated classes or any other kind of data structure for
  6663. * actually storing protobufs. It only contains *defs* which
  6664. * let you reflect over a protobuf *schema*.
  6665. */
  6666. /* This file was generated by upbc (the upb compiler) from the input
  6667. * file:
  6668. *
  6669. * upb/descriptor/descriptor.proto
  6670. *
  6671. * Do not edit -- your changes will be discarded when the file is
  6672. * regenerated. */
  6673. #ifndef UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_
  6674. #define UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_
  6675. UPB_BEGIN_EXTERN_C
  6676. /* MessageDefs: call these functions to get a ref to a msgdef. */
  6677. const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner);
  6678. const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner);
  6679. const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner);
  6680. const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner);
  6681. const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner);
  6682. const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner);
  6683. const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner);
  6684. const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner);
  6685. const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner);
  6686. const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner);
  6687. const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner);
  6688. const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner);
  6689. const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner);
  6690. const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner);
  6691. const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner);
  6692. const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner);
  6693. const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner);
  6694. const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner);
  6695. const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner);
  6696. const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner);
  6697. const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner);
  6698. const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner);
  6699. /* EnumDefs: call these functions to get a ref to an enumdef. */
  6700. const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner);
  6701. const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner);
  6702. const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner);
  6703. const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner);
  6704. const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner);
  6705. /* Functions to test whether this message is of a certain type. */
  6706. UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_is(const upb_msgdef *m) {
  6707. return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto") == 0;
  6708. }
  6709. UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(const upb_msgdef *m) {
  6710. return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ExtensionRange") == 0;
  6711. }
  6712. UPB_INLINE bool upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(const upb_msgdef *m) {
  6713. return strcmp(upb_msgdef_fullname(m), "google.protobuf.DescriptorProto.ReservedRange") == 0;
  6714. }
  6715. UPB_INLINE bool upbdefs_google_protobuf_EnumDescriptorProto_is(const upb_msgdef *m) {
  6716. return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumDescriptorProto") == 0;
  6717. }
  6718. UPB_INLINE bool upbdefs_google_protobuf_EnumOptions_is(const upb_msgdef *m) {
  6719. return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumOptions") == 0;
  6720. }
  6721. UPB_INLINE bool upbdefs_google_protobuf_EnumValueDescriptorProto_is(const upb_msgdef *m) {
  6722. return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueDescriptorProto") == 0;
  6723. }
  6724. UPB_INLINE bool upbdefs_google_protobuf_EnumValueOptions_is(const upb_msgdef *m) {
  6725. return strcmp(upb_msgdef_fullname(m), "google.protobuf.EnumValueOptions") == 0;
  6726. }
  6727. UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_is(const upb_msgdef *m) {
  6728. return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldDescriptorProto") == 0;
  6729. }
  6730. UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_is(const upb_msgdef *m) {
  6731. return strcmp(upb_msgdef_fullname(m), "google.protobuf.FieldOptions") == 0;
  6732. }
  6733. UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorProto_is(const upb_msgdef *m) {
  6734. return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorProto") == 0;
  6735. }
  6736. UPB_INLINE bool upbdefs_google_protobuf_FileDescriptorSet_is(const upb_msgdef *m) {
  6737. return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileDescriptorSet") == 0;
  6738. }
  6739. UPB_INLINE bool upbdefs_google_protobuf_FileOptions_is(const upb_msgdef *m) {
  6740. return strcmp(upb_msgdef_fullname(m), "google.protobuf.FileOptions") == 0;
  6741. }
  6742. UPB_INLINE bool upbdefs_google_protobuf_MessageOptions_is(const upb_msgdef *m) {
  6743. return strcmp(upb_msgdef_fullname(m), "google.protobuf.MessageOptions") == 0;
  6744. }
  6745. UPB_INLINE bool upbdefs_google_protobuf_MethodDescriptorProto_is(const upb_msgdef *m) {
  6746. return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodDescriptorProto") == 0;
  6747. }
  6748. UPB_INLINE bool upbdefs_google_protobuf_MethodOptions_is(const upb_msgdef *m) {
  6749. return strcmp(upb_msgdef_fullname(m), "google.protobuf.MethodOptions") == 0;
  6750. }
  6751. UPB_INLINE bool upbdefs_google_protobuf_OneofDescriptorProto_is(const upb_msgdef *m) {
  6752. return strcmp(upb_msgdef_fullname(m), "google.protobuf.OneofDescriptorProto") == 0;
  6753. }
  6754. UPB_INLINE bool upbdefs_google_protobuf_ServiceDescriptorProto_is(const upb_msgdef *m) {
  6755. return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceDescriptorProto") == 0;
  6756. }
  6757. UPB_INLINE bool upbdefs_google_protobuf_ServiceOptions_is(const upb_msgdef *m) {
  6758. return strcmp(upb_msgdef_fullname(m), "google.protobuf.ServiceOptions") == 0;
  6759. }
  6760. UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_is(const upb_msgdef *m) {
  6761. return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo") == 0;
  6762. }
  6763. UPB_INLINE bool upbdefs_google_protobuf_SourceCodeInfo_Location_is(const upb_msgdef *m) {
  6764. return strcmp(upb_msgdef_fullname(m), "google.protobuf.SourceCodeInfo.Location") == 0;
  6765. }
  6766. UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_is(const upb_msgdef *m) {
  6767. return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption") == 0;
  6768. }
  6769. UPB_INLINE bool upbdefs_google_protobuf_UninterpretedOption_NamePart_is(const upb_msgdef *m) {
  6770. return strcmp(upb_msgdef_fullname(m), "google.protobuf.UninterpretedOption.NamePart") == 0;
  6771. }
  6772. /* Functions to test whether this enum is of a certain type. */
  6773. UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Label_is(const upb_enumdef *e) {
  6774. return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Label") == 0;
  6775. }
  6776. UPB_INLINE bool upbdefs_google_protobuf_FieldDescriptorProto_Type_is(const upb_enumdef *e) {
  6777. return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldDescriptorProto.Type") == 0;
  6778. }
  6779. UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_CType_is(const upb_enumdef *e) {
  6780. return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.CType") == 0;
  6781. }
  6782. UPB_INLINE bool upbdefs_google_protobuf_FieldOptions_JSType_is(const upb_enumdef *e) {
  6783. return strcmp(upb_enumdef_fullname(e), "google.protobuf.FieldOptions.JSType") == 0;
  6784. }
  6785. UPB_INLINE bool upbdefs_google_protobuf_FileOptions_OptimizeMode_is(const upb_enumdef *e) {
  6786. return strcmp(upb_enumdef_fullname(e), "google.protobuf.FileOptions.OptimizeMode") == 0;
  6787. }
  6788. /* Functions to get a fielddef from a msgdef reference. */
  6789. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 2); }
  6790. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m)); return upb_msgdef_itof(m, 1); }
  6791. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_end(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 2); }
  6792. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_f_start(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m)); return upb_msgdef_itof(m, 1); }
  6793. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
  6794. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
  6795. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_extension_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
  6796. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_field(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6797. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6798. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_nested_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6799. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_oneof_decl(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
  6800. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
  6801. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
  6802. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_DescriptorProto_f_reserved_range(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
  6803. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6804. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6805. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumDescriptorProto_f_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6806. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_allow_alias(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 2); }
  6807. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 3); }
  6808. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6809. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6810. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6811. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6812. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 1); }
  6813. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_EnumValueOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6814. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_default_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
  6815. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_extendee(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6816. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_json_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
  6817. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_label(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
  6818. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6819. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_number(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6820. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_oneof_index(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
  6821. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
  6822. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
  6823. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldDescriptorProto_f_type_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
  6824. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_ctype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 1); }
  6825. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 3); }
  6826. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_jstype(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 6); }
  6827. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_lazy(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 5); }
  6828. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_packed(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 2); }
  6829. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6830. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FieldOptions_f_weak(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m)); return upb_msgdef_itof(m, 10); }
  6831. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6832. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_enum_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
  6833. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 7); }
  6834. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_message_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
  6835. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6836. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 8); }
  6837. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6838. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_public_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 10); }
  6839. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_service(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
  6840. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_source_code_info(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 9); }
  6841. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_syntax(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 12); }
  6842. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorProto_f_weak_dependency(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m)); return upb_msgdef_itof(m, 11); }
  6843. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileDescriptorSet_f_file(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m)); return upb_msgdef_itof(m, 1); }
  6844. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_enable_arenas(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 31); }
  6845. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_cc_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 16); }
  6846. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_csharp_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 37); }
  6847. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 23); }
  6848. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_go_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 11); }
  6849. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generate_equals_and_hash(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 20); }
  6850. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 17); }
  6851. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_multiple_files(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 10); }
  6852. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_outer_classname(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 8); }
  6853. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 1); }
  6854. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_java_string_check_utf8(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 27); }
  6855. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_javanano_use_deprecated_package(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 38); }
  6856. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_objc_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 36); }
  6857. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_optimize_for(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 9); }
  6858. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_class_prefix(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 40); }
  6859. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_php_namespace(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 41); }
  6860. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_py_generic_services(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 18); }
  6861. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_FileOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6862. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 3); }
  6863. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_map_entry(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 7); }
  6864. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_message_set_wire_format(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 1); }
  6865. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_no_standard_descriptor_accessor(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 2); }
  6866. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MessageOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6867. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_client_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 5); }
  6868. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_input_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6869. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6870. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 4); }
  6871. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_output_type(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6872. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodDescriptorProto_f_server_streaming(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m)); return upb_msgdef_itof(m, 6); }
  6873. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 33); }
  6874. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_MethodOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6875. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_OneofDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6876. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_method(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 2); }
  6877. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 1); }
  6878. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceDescriptorProto_f_options(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m)); return upb_msgdef_itof(m, 3); }
  6879. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_deprecated(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 33); }
  6880. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_ServiceOptions_f_uninterpreted_option(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m)); return upb_msgdef_itof(m, 999); }
  6881. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 3); }
  6882. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_leading_detached_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 6); }
  6883. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_path(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 1); }
  6884. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_span(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 2); }
  6885. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_Location_f_trailing_comments(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m)); return upb_msgdef_itof(m, 4); }
  6886. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_SourceCodeInfo_f_location(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m)); return upb_msgdef_itof(m, 1); }
  6887. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_is_extension(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 2); }
  6888. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_NamePart_f_name_part(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m)); return upb_msgdef_itof(m, 1); }
  6889. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_aggregate_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 8); }
  6890. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_double_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 6); }
  6891. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_identifier_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 3); }
  6892. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_name(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 2); }
  6893. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_negative_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 5); }
  6894. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_positive_int_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 4); }
  6895. UPB_INLINE const upb_fielddef *upbdefs_google_protobuf_UninterpretedOption_f_string_value(const upb_msgdef *m) { UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m)); return upb_msgdef_itof(m, 7); }
  6896. UPB_END_EXTERN_C
  6897. #ifdef __cplusplus
  6898. namespace upbdefs {
  6899. namespace google {
  6900. namespace protobuf {
  6901. class DescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6902. public:
  6903. DescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6904. : reffed_ptr(m, ref_donor) {
  6905. UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_is(m));
  6906. }
  6907. static DescriptorProto get() {
  6908. const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_get(&m);
  6909. return DescriptorProto(m, &m);
  6910. }
  6911. class ExtensionRange : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6912. public:
  6913. ExtensionRange(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6914. : reffed_ptr(m, ref_donor) {
  6915. UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ExtensionRange_is(m));
  6916. }
  6917. static ExtensionRange get() {
  6918. const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(&m);
  6919. return ExtensionRange(m, &m);
  6920. }
  6921. };
  6922. class ReservedRange : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6923. public:
  6924. ReservedRange(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6925. : reffed_ptr(m, ref_donor) {
  6926. UPB_ASSERT(upbdefs_google_protobuf_DescriptorProto_ReservedRange_is(m));
  6927. }
  6928. static ReservedRange get() {
  6929. const ::upb::MessageDef* m = upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(&m);
  6930. return ReservedRange(m, &m);
  6931. }
  6932. };
  6933. };
  6934. class EnumDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6935. public:
  6936. EnumDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6937. : reffed_ptr(m, ref_donor) {
  6938. UPB_ASSERT(upbdefs_google_protobuf_EnumDescriptorProto_is(m));
  6939. }
  6940. static EnumDescriptorProto get() {
  6941. const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumDescriptorProto_get(&m);
  6942. return EnumDescriptorProto(m, &m);
  6943. }
  6944. };
  6945. class EnumOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6946. public:
  6947. EnumOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6948. : reffed_ptr(m, ref_donor) {
  6949. UPB_ASSERT(upbdefs_google_protobuf_EnumOptions_is(m));
  6950. }
  6951. static EnumOptions get() {
  6952. const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumOptions_get(&m);
  6953. return EnumOptions(m, &m);
  6954. }
  6955. };
  6956. class EnumValueDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6957. public:
  6958. EnumValueDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6959. : reffed_ptr(m, ref_donor) {
  6960. UPB_ASSERT(upbdefs_google_protobuf_EnumValueDescriptorProto_is(m));
  6961. }
  6962. static EnumValueDescriptorProto get() {
  6963. const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueDescriptorProto_get(&m);
  6964. return EnumValueDescriptorProto(m, &m);
  6965. }
  6966. };
  6967. class EnumValueOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6968. public:
  6969. EnumValueOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6970. : reffed_ptr(m, ref_donor) {
  6971. UPB_ASSERT(upbdefs_google_protobuf_EnumValueOptions_is(m));
  6972. }
  6973. static EnumValueOptions get() {
  6974. const ::upb::MessageDef* m = upbdefs_google_protobuf_EnumValueOptions_get(&m);
  6975. return EnumValueOptions(m, &m);
  6976. }
  6977. };
  6978. class FieldDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  6979. public:
  6980. FieldDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  6981. : reffed_ptr(m, ref_donor) {
  6982. UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_is(m));
  6983. }
  6984. static FieldDescriptorProto get() {
  6985. const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldDescriptorProto_get(&m);
  6986. return FieldDescriptorProto(m, &m);
  6987. }
  6988. class Label : public ::upb::reffed_ptr<const ::upb::EnumDef> {
  6989. public:
  6990. Label(const ::upb::EnumDef* e, const void *ref_donor = NULL)
  6991. : reffed_ptr(e, ref_donor) {
  6992. UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Label_is(e));
  6993. }
  6994. static Label get() {
  6995. const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Label_get(&e);
  6996. return Label(e, &e);
  6997. }
  6998. };
  6999. class Type : public ::upb::reffed_ptr<const ::upb::EnumDef> {
  7000. public:
  7001. Type(const ::upb::EnumDef* e, const void *ref_donor = NULL)
  7002. : reffed_ptr(e, ref_donor) {
  7003. UPB_ASSERT(upbdefs_google_protobuf_FieldDescriptorProto_Type_is(e));
  7004. }
  7005. static Type get() {
  7006. const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldDescriptorProto_Type_get(&e);
  7007. return Type(e, &e);
  7008. }
  7009. };
  7010. };
  7011. class FieldOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7012. public:
  7013. FieldOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7014. : reffed_ptr(m, ref_donor) {
  7015. UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_is(m));
  7016. }
  7017. static FieldOptions get() {
  7018. const ::upb::MessageDef* m = upbdefs_google_protobuf_FieldOptions_get(&m);
  7019. return FieldOptions(m, &m);
  7020. }
  7021. class CType : public ::upb::reffed_ptr<const ::upb::EnumDef> {
  7022. public:
  7023. CType(const ::upb::EnumDef* e, const void *ref_donor = NULL)
  7024. : reffed_ptr(e, ref_donor) {
  7025. UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_CType_is(e));
  7026. }
  7027. static CType get() {
  7028. const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_CType_get(&e);
  7029. return CType(e, &e);
  7030. }
  7031. };
  7032. class JSType : public ::upb::reffed_ptr<const ::upb::EnumDef> {
  7033. public:
  7034. JSType(const ::upb::EnumDef* e, const void *ref_donor = NULL)
  7035. : reffed_ptr(e, ref_donor) {
  7036. UPB_ASSERT(upbdefs_google_protobuf_FieldOptions_JSType_is(e));
  7037. }
  7038. static JSType get() {
  7039. const ::upb::EnumDef* e = upbdefs_google_protobuf_FieldOptions_JSType_get(&e);
  7040. return JSType(e, &e);
  7041. }
  7042. };
  7043. };
  7044. class FileDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7045. public:
  7046. FileDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7047. : reffed_ptr(m, ref_donor) {
  7048. UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorProto_is(m));
  7049. }
  7050. static FileDescriptorProto get() {
  7051. const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorProto_get(&m);
  7052. return FileDescriptorProto(m, &m);
  7053. }
  7054. };
  7055. class FileDescriptorSet : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7056. public:
  7057. FileDescriptorSet(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7058. : reffed_ptr(m, ref_donor) {
  7059. UPB_ASSERT(upbdefs_google_protobuf_FileDescriptorSet_is(m));
  7060. }
  7061. static FileDescriptorSet get() {
  7062. const ::upb::MessageDef* m = upbdefs_google_protobuf_FileDescriptorSet_get(&m);
  7063. return FileDescriptorSet(m, &m);
  7064. }
  7065. };
  7066. class FileOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7067. public:
  7068. FileOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7069. : reffed_ptr(m, ref_donor) {
  7070. UPB_ASSERT(upbdefs_google_protobuf_FileOptions_is(m));
  7071. }
  7072. static FileOptions get() {
  7073. const ::upb::MessageDef* m = upbdefs_google_protobuf_FileOptions_get(&m);
  7074. return FileOptions(m, &m);
  7075. }
  7076. class OptimizeMode : public ::upb::reffed_ptr<const ::upb::EnumDef> {
  7077. public:
  7078. OptimizeMode(const ::upb::EnumDef* e, const void *ref_donor = NULL)
  7079. : reffed_ptr(e, ref_donor) {
  7080. UPB_ASSERT(upbdefs_google_protobuf_FileOptions_OptimizeMode_is(e));
  7081. }
  7082. static OptimizeMode get() {
  7083. const ::upb::EnumDef* e = upbdefs_google_protobuf_FileOptions_OptimizeMode_get(&e);
  7084. return OptimizeMode(e, &e);
  7085. }
  7086. };
  7087. };
  7088. class MessageOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7089. public:
  7090. MessageOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7091. : reffed_ptr(m, ref_donor) {
  7092. UPB_ASSERT(upbdefs_google_protobuf_MessageOptions_is(m));
  7093. }
  7094. static MessageOptions get() {
  7095. const ::upb::MessageDef* m = upbdefs_google_protobuf_MessageOptions_get(&m);
  7096. return MessageOptions(m, &m);
  7097. }
  7098. };
  7099. class MethodDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7100. public:
  7101. MethodDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7102. : reffed_ptr(m, ref_donor) {
  7103. UPB_ASSERT(upbdefs_google_protobuf_MethodDescriptorProto_is(m));
  7104. }
  7105. static MethodDescriptorProto get() {
  7106. const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodDescriptorProto_get(&m);
  7107. return MethodDescriptorProto(m, &m);
  7108. }
  7109. };
  7110. class MethodOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7111. public:
  7112. MethodOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7113. : reffed_ptr(m, ref_donor) {
  7114. UPB_ASSERT(upbdefs_google_protobuf_MethodOptions_is(m));
  7115. }
  7116. static MethodOptions get() {
  7117. const ::upb::MessageDef* m = upbdefs_google_protobuf_MethodOptions_get(&m);
  7118. return MethodOptions(m, &m);
  7119. }
  7120. };
  7121. class OneofDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7122. public:
  7123. OneofDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7124. : reffed_ptr(m, ref_donor) {
  7125. UPB_ASSERT(upbdefs_google_protobuf_OneofDescriptorProto_is(m));
  7126. }
  7127. static OneofDescriptorProto get() {
  7128. const ::upb::MessageDef* m = upbdefs_google_protobuf_OneofDescriptorProto_get(&m);
  7129. return OneofDescriptorProto(m, &m);
  7130. }
  7131. };
  7132. class ServiceDescriptorProto : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7133. public:
  7134. ServiceDescriptorProto(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7135. : reffed_ptr(m, ref_donor) {
  7136. UPB_ASSERT(upbdefs_google_protobuf_ServiceDescriptorProto_is(m));
  7137. }
  7138. static ServiceDescriptorProto get() {
  7139. const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceDescriptorProto_get(&m);
  7140. return ServiceDescriptorProto(m, &m);
  7141. }
  7142. };
  7143. class ServiceOptions : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7144. public:
  7145. ServiceOptions(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7146. : reffed_ptr(m, ref_donor) {
  7147. UPB_ASSERT(upbdefs_google_protobuf_ServiceOptions_is(m));
  7148. }
  7149. static ServiceOptions get() {
  7150. const ::upb::MessageDef* m = upbdefs_google_protobuf_ServiceOptions_get(&m);
  7151. return ServiceOptions(m, &m);
  7152. }
  7153. };
  7154. class SourceCodeInfo : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7155. public:
  7156. SourceCodeInfo(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7157. : reffed_ptr(m, ref_donor) {
  7158. UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_is(m));
  7159. }
  7160. static SourceCodeInfo get() {
  7161. const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_get(&m);
  7162. return SourceCodeInfo(m, &m);
  7163. }
  7164. class Location : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7165. public:
  7166. Location(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7167. : reffed_ptr(m, ref_donor) {
  7168. UPB_ASSERT(upbdefs_google_protobuf_SourceCodeInfo_Location_is(m));
  7169. }
  7170. static Location get() {
  7171. const ::upb::MessageDef* m = upbdefs_google_protobuf_SourceCodeInfo_Location_get(&m);
  7172. return Location(m, &m);
  7173. }
  7174. };
  7175. };
  7176. class UninterpretedOption : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7177. public:
  7178. UninterpretedOption(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7179. : reffed_ptr(m, ref_donor) {
  7180. UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_is(m));
  7181. }
  7182. static UninterpretedOption get() {
  7183. const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_get(&m);
  7184. return UninterpretedOption(m, &m);
  7185. }
  7186. class NamePart : public ::upb::reffed_ptr<const ::upb::MessageDef> {
  7187. public:
  7188. NamePart(const ::upb::MessageDef* m, const void *ref_donor = NULL)
  7189. : reffed_ptr(m, ref_donor) {
  7190. UPB_ASSERT(upbdefs_google_protobuf_UninterpretedOption_NamePart_is(m));
  7191. }
  7192. static NamePart get() {
  7193. const ::upb::MessageDef* m = upbdefs_google_protobuf_UninterpretedOption_NamePart_get(&m);
  7194. return NamePart(m, &m);
  7195. }
  7196. };
  7197. };
  7198. } /* namespace protobuf */
  7199. } /* namespace google */
  7200. } /* namespace upbdefs */
  7201. #endif /* __cplusplus */
  7202. #endif /* UPB_DESCRIPTOR_DESCRIPTOR_PROTO_UPB_H_ */
  7203. /*
  7204. ** Internal-only definitions for the decoder.
  7205. */
  7206. #ifndef UPB_DECODER_INT_H_
  7207. #define UPB_DECODER_INT_H_
  7208. /*
  7209. ** upb::pb::Decoder
  7210. **
  7211. ** A high performance, streaming, resumable decoder for the binary protobuf
  7212. ** format.
  7213. **
  7214. ** This interface works the same regardless of what decoder backend is being
  7215. ** used. A client of this class does not need to know whether decoding is using
  7216. ** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
  7217. ** it will always use the fastest available decoder. However, you can call
  7218. ** set_allow_jit(false) to disable any JIT decoder that might be available.
  7219. ** This is primarily useful for testing purposes.
  7220. */
  7221. #ifndef UPB_DECODER_H_
  7222. #define UPB_DECODER_H_
  7223. #ifdef __cplusplus
  7224. namespace upb {
  7225. namespace pb {
  7226. class CodeCache;
  7227. class Decoder;
  7228. class DecoderMethod;
  7229. class DecoderMethodOptions;
  7230. } /* namespace pb */
  7231. } /* namespace upb */
  7232. #endif
  7233. UPB_DECLARE_TYPE(upb::pb::CodeCache, upb_pbcodecache)
  7234. UPB_DECLARE_TYPE(upb::pb::Decoder, upb_pbdecoder)
  7235. UPB_DECLARE_TYPE(upb::pb::DecoderMethodOptions, upb_pbdecodermethodopts)
  7236. UPB_DECLARE_DERIVED_TYPE(upb::pb::DecoderMethod, upb::RefCounted,
  7237. upb_pbdecodermethod, upb_refcounted)
  7238. /* The maximum number of bytes we are required to buffer internally between
  7239. * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
  7240. * varint, less one because we are buffering an incomplete value.
  7241. *
  7242. * Should only be used by unit tests. */
  7243. #define UPB_DECODER_MAX_RESIDUAL_BYTES 14
  7244. #ifdef __cplusplus
  7245. /* The parameters one uses to construct a DecoderMethod.
  7246. * TODO(haberman): move allowjit here? Seems more convenient for users.
  7247. * TODO(haberman): move this to be heap allocated for ABI stability. */
  7248. class upb::pb::DecoderMethodOptions {
  7249. public:
  7250. /* Parameter represents the destination handlers that this method will push
  7251. * to. */
  7252. explicit DecoderMethodOptions(const Handlers* dest_handlers);
  7253. /* Should the decoder push submessages to lazy handlers for fields that have
  7254. * them? The caller should set this iff the lazy handlers expect data that is
  7255. * in protobuf binary format and the caller wishes to lazy parse it. */
  7256. void set_lazy(bool lazy);
  7257. #else
  7258. struct upb_pbdecodermethodopts {
  7259. #endif
  7260. const upb_handlers *handlers;
  7261. bool lazy;
  7262. };
  7263. #ifdef __cplusplus
  7264. /* Represents the code to parse a protobuf according to a destination
  7265. * Handlers. */
  7266. class upb::pb::DecoderMethod {
  7267. public:
  7268. /* Include base methods from upb::ReferenceCounted. */
  7269. UPB_REFCOUNTED_CPPMETHODS
  7270. /* The destination handlers that are statically bound to this method.
  7271. * This method is only capable of outputting to a sink that uses these
  7272. * handlers. */
  7273. const Handlers* dest_handlers() const;
  7274. /* The input handlers for this decoder method. */
  7275. const BytesHandler* input_handler() const;
  7276. /* Whether this method is native. */
  7277. bool is_native() const;
  7278. /* Convenience method for generating a DecoderMethod without explicitly
  7279. * creating a CodeCache. */
  7280. static reffed_ptr<const DecoderMethod> New(const DecoderMethodOptions& opts);
  7281. private:
  7282. UPB_DISALLOW_POD_OPS(DecoderMethod, upb::pb::DecoderMethod)
  7283. };
  7284. #endif
  7285. /* Preallocation hint: decoder won't allocate more bytes than this when first
  7286. * constructed. This hint may be an overestimate for some build configurations.
  7287. * But if the decoder library is upgraded without recompiling the application,
  7288. * it may be an underestimate. */
  7289. #define UPB_PB_DECODER_SIZE 4416
  7290. #ifdef __cplusplus
  7291. /* A Decoder receives binary protobuf data on its input sink and pushes the
  7292. * decoded data to its output sink. */
  7293. class upb::pb::Decoder {
  7294. public:
  7295. /* Constructs a decoder instance for the given method, which must outlive this
  7296. * decoder. Any errors during parsing will be set on the given status, which
  7297. * must also outlive this decoder.
  7298. *
  7299. * The sink must match the given method. */
  7300. static Decoder* Create(Environment* env, const DecoderMethod* method,
  7301. Sink* output);
  7302. /* Returns the DecoderMethod this decoder is parsing from. */
  7303. const DecoderMethod* method() const;
  7304. /* The sink on which this decoder receives input. */
  7305. BytesSink* input();
  7306. /* Returns number of bytes successfully parsed.
  7307. *
  7308. * This can be useful for determining the stream position where an error
  7309. * occurred.
  7310. *
  7311. * This value may not be up-to-date when called from inside a parsing
  7312. * callback. */
  7313. uint64_t BytesParsed() const;
  7314. /* Gets/sets the parsing nexting limit. If the total number of nested
  7315. * submessages and repeated fields hits this limit, parsing will fail. This
  7316. * is a resource limit that controls the amount of memory used by the parsing
  7317. * stack.
  7318. *
  7319. * Setting the limit will fail if the parser is currently suspended at a depth
  7320. * greater than this, or if memory allocation of the stack fails. */
  7321. size_t max_nesting() const;
  7322. bool set_max_nesting(size_t max);
  7323. void Reset();
  7324. static const size_t kSize = UPB_PB_DECODER_SIZE;
  7325. private:
  7326. UPB_DISALLOW_POD_OPS(Decoder, upb::pb::Decoder)
  7327. };
  7328. #endif /* __cplusplus */
  7329. #ifdef __cplusplus
  7330. /* A class for caching protobuf processing code, whether bytecode for the
  7331. * interpreted decoder or machine code for the JIT.
  7332. *
  7333. * This class is not thread-safe.
  7334. *
  7335. * TODO(haberman): move this to be heap allocated for ABI stability. */
  7336. class upb::pb::CodeCache {
  7337. public:
  7338. CodeCache();
  7339. ~CodeCache();
  7340. /* Whether the cache is allowed to generate machine code. Defaults to true.
  7341. * There is no real reason to turn it off except for testing or if you are
  7342. * having a specific problem with the JIT.
  7343. *
  7344. * Note that allow_jit = true does not *guarantee* that the code will be JIT
  7345. * compiled. If this platform is not supported or the JIT was not compiled
  7346. * in, the code may still be interpreted. */
  7347. bool allow_jit() const;
  7348. /* This may only be called when the object is first constructed, and prior to
  7349. * any code generation, otherwise returns false and does nothing. */
  7350. bool set_allow_jit(bool allow);
  7351. /* Returns a DecoderMethod that can push data to the given handlers.
  7352. * If a suitable method already exists, it will be returned from the cache.
  7353. *
  7354. * Specifying the destination handlers here allows the DecoderMethod to be
  7355. * statically bound to the destination handlers if possible, which can allow
  7356. * more efficient decoding. However the returned method may or may not
  7357. * actually be statically bound. But in all cases, the returned method can
  7358. * push data to the given handlers. */
  7359. const DecoderMethod *GetDecoderMethod(const DecoderMethodOptions& opts);
  7360. /* If/when someone needs to explicitly create a dynamically-bound
  7361. * DecoderMethod*, we can add a method to get it here. */
  7362. private:
  7363. UPB_DISALLOW_COPY_AND_ASSIGN(CodeCache)
  7364. #else
  7365. struct upb_pbcodecache {
  7366. #endif
  7367. bool allow_jit_;
  7368. /* Array of mgroups. */
  7369. upb_inttable groups;
  7370. };
  7371. UPB_BEGIN_EXTERN_C
  7372. upb_pbdecoder *upb_pbdecoder_create(upb_env *e,
  7373. const upb_pbdecodermethod *method,
  7374. upb_sink *output);
  7375. const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
  7376. upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d);
  7377. uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
  7378. size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
  7379. bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
  7380. void upb_pbdecoder_reset(upb_pbdecoder *d);
  7381. void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
  7382. const upb_handlers *h);
  7383. void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy);
  7384. /* Include refcounted methods like upb_pbdecodermethod_ref(). */
  7385. UPB_REFCOUNTED_CMETHODS(upb_pbdecodermethod, upb_pbdecodermethod_upcast)
  7386. const upb_handlers *upb_pbdecodermethod_desthandlers(
  7387. const upb_pbdecodermethod *m);
  7388. const upb_byteshandler *upb_pbdecodermethod_inputhandler(
  7389. const upb_pbdecodermethod *m);
  7390. bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
  7391. const upb_pbdecodermethod *upb_pbdecodermethod_new(
  7392. const upb_pbdecodermethodopts *opts, const void *owner);
  7393. void upb_pbcodecache_init(upb_pbcodecache *c);
  7394. void upb_pbcodecache_uninit(upb_pbcodecache *c);
  7395. bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
  7396. bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
  7397. const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
  7398. upb_pbcodecache *c, const upb_pbdecodermethodopts *opts);
  7399. UPB_END_EXTERN_C
  7400. #ifdef __cplusplus
  7401. namespace upb {
  7402. namespace pb {
  7403. /* static */
  7404. inline Decoder* Decoder::Create(Environment* env, const DecoderMethod* m,
  7405. Sink* sink) {
  7406. return upb_pbdecoder_create(env, m, sink);
  7407. }
  7408. inline const DecoderMethod* Decoder::method() const {
  7409. return upb_pbdecoder_method(this);
  7410. }
  7411. inline BytesSink* Decoder::input() {
  7412. return upb_pbdecoder_input(this);
  7413. }
  7414. inline uint64_t Decoder::BytesParsed() const {
  7415. return upb_pbdecoder_bytesparsed(this);
  7416. }
  7417. inline size_t Decoder::max_nesting() const {
  7418. return upb_pbdecoder_maxnesting(this);
  7419. }
  7420. inline bool Decoder::set_max_nesting(size_t max) {
  7421. return upb_pbdecoder_setmaxnesting(this, max);
  7422. }
  7423. inline void Decoder::Reset() { upb_pbdecoder_reset(this); }
  7424. inline DecoderMethodOptions::DecoderMethodOptions(const Handlers* h) {
  7425. upb_pbdecodermethodopts_init(this, h);
  7426. }
  7427. inline void DecoderMethodOptions::set_lazy(bool lazy) {
  7428. upb_pbdecodermethodopts_setlazy(this, lazy);
  7429. }
  7430. inline const Handlers* DecoderMethod::dest_handlers() const {
  7431. return upb_pbdecodermethod_desthandlers(this);
  7432. }
  7433. inline const BytesHandler* DecoderMethod::input_handler() const {
  7434. return upb_pbdecodermethod_inputhandler(this);
  7435. }
  7436. inline bool DecoderMethod::is_native() const {
  7437. return upb_pbdecodermethod_isnative(this);
  7438. }
  7439. /* static */
  7440. inline reffed_ptr<const DecoderMethod> DecoderMethod::New(
  7441. const DecoderMethodOptions &opts) {
  7442. const upb_pbdecodermethod *m = upb_pbdecodermethod_new(&opts, &m);
  7443. return reffed_ptr<const DecoderMethod>(m, &m);
  7444. }
  7445. inline CodeCache::CodeCache() {
  7446. upb_pbcodecache_init(this);
  7447. }
  7448. inline CodeCache::~CodeCache() {
  7449. upb_pbcodecache_uninit(this);
  7450. }
  7451. inline bool CodeCache::allow_jit() const {
  7452. return upb_pbcodecache_allowjit(this);
  7453. }
  7454. inline bool CodeCache::set_allow_jit(bool allow) {
  7455. return upb_pbcodecache_setallowjit(this, allow);
  7456. }
  7457. inline const DecoderMethod *CodeCache::GetDecoderMethod(
  7458. const DecoderMethodOptions& opts) {
  7459. return upb_pbcodecache_getdecodermethod(this, &opts);
  7460. }
  7461. } /* namespace pb */
  7462. } /* namespace upb */
  7463. #endif /* __cplusplus */
  7464. #endif /* UPB_DECODER_H_ */
  7465. /* C++ names are not actually used since this type isn't exposed to users. */
  7466. #ifdef __cplusplus
  7467. namespace upb {
  7468. namespace pb {
  7469. class MessageGroup;
  7470. } /* namespace pb */
  7471. } /* namespace upb */
  7472. #endif
  7473. UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted,
  7474. mgroup, upb_refcounted)
  7475. /* Opcode definitions. The canonical meaning of each opcode is its
  7476. * implementation in the interpreter (the JIT is written to match this).
  7477. *
  7478. * All instructions have the opcode in the low byte.
  7479. * Instruction format for most instructions is:
  7480. *
  7481. * +-------------------+--------+
  7482. * | arg (24) | op (8) |
  7483. * +-------------------+--------+
  7484. *
  7485. * Exceptions are indicated below. A few opcodes are multi-word. */
  7486. typedef enum {
  7487. /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
  7488. * Arg for all of these is the upb selector for this field. */
  7489. #define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
  7490. T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
  7491. T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
  7492. #undef T
  7493. OP_STARTMSG = 9, /* No arg. */
  7494. OP_ENDMSG = 10, /* No arg. */
  7495. OP_STARTSEQ = 11,
  7496. OP_ENDSEQ = 12,
  7497. OP_STARTSUBMSG = 14,
  7498. OP_ENDSUBMSG = 19,
  7499. OP_STARTSTR = 20,
  7500. OP_STRING = 21,
  7501. OP_ENDSTR = 22,
  7502. OP_PUSHTAGDELIM = 23, /* No arg. */
  7503. OP_PUSHLENDELIM = 24, /* No arg. */
  7504. OP_POP = 25, /* No arg. */
  7505. OP_SETDELIM = 26, /* No arg. */
  7506. OP_SETBIGGROUPNUM = 27, /* two words:
  7507. * | unused (24) | opc (8) |
  7508. * | groupnum (32) | */
  7509. OP_CHECKDELIM = 28,
  7510. OP_CALL = 29,
  7511. OP_RET = 30,
  7512. OP_BRANCH = 31,
  7513. /* Different opcodes depending on how many bytes expected. */
  7514. OP_TAG1 = 32, /* | match tag (16) | jump target (8) | opc (8) | */
  7515. OP_TAG2 = 33, /* | match tag (16) | jump target (8) | opc (8) | */
  7516. OP_TAGN = 34, /* three words: */
  7517. /* | unused (16) | jump target(8) | opc (8) | */
  7518. /* | match tag 1 (32) | */
  7519. /* | match tag 2 (32) | */
  7520. OP_SETDISPATCH = 35, /* N words: */
  7521. /* | unused (24) | opc | */
  7522. /* | upb_inttable* (32 or 64) | */
  7523. OP_DISPATCH = 36, /* No arg. */
  7524. OP_HALT = 37 /* No arg. */
  7525. } opcode;
  7526. #define OP_MAX OP_HALT
  7527. UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; }
  7528. /* Method group; represents a set of decoder methods that had their code
  7529. * emitted together, and must therefore be freed together. Immutable once
  7530. * created. It is possible we may want to expose this to users at some point.
  7531. *
  7532. * Overall ownership of Decoder objects looks like this:
  7533. *
  7534. * +----------+
  7535. * | | <---> DecoderMethod
  7536. * | method |
  7537. * CodeCache ---> | group | <---> DecoderMethod
  7538. * | |
  7539. * | (mgroup) | <---> DecoderMethod
  7540. * +----------+
  7541. */
  7542. struct mgroup {
  7543. upb_refcounted base;
  7544. /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. We own refs on the
  7545. * methods. */
  7546. upb_inttable methods;
  7547. /* When we add the ability to link to previously existing mgroups, we'll
  7548. * need an array of mgroups we reference here, and own refs on them. */
  7549. /* The bytecode for our methods, if any exists. Owned by us. */
  7550. uint32_t *bytecode;
  7551. uint32_t *bytecode_end;
  7552. #ifdef UPB_USE_JIT_X64
  7553. /* JIT-generated machine code, if any. */
  7554. upb_string_handlerfunc *jit_code;
  7555. /* The size of the jit_code (required to munmap()). */
  7556. size_t jit_size;
  7557. char *debug_info;
  7558. void *dl;
  7559. #endif
  7560. };
  7561. /* The maximum that any submessages can be nested. Matches proto2's limit.
  7562. * This specifies the size of the decoder's statically-sized array and therefore
  7563. * setting it high will cause the upb::pb::Decoder object to be larger.
  7564. *
  7565. * If necessary we can add a runtime-settable property to Decoder that allow
  7566. * this to be larger than the compile-time setting, but this would add
  7567. * complexity, particularly since we would have to decide how/if to give users
  7568. * the ability to set a custom memory allocation function. */
  7569. #define UPB_DECODER_MAX_NESTING 64
  7570. /* Internal-only struct used by the decoder. */
  7571. typedef struct {
  7572. /* Space optimization note: we store two pointers here that the JIT
  7573. * doesn't need at all; the upb_handlers* inside the sink and
  7574. * the dispatch table pointer. We can optimze so that the JIT uses
  7575. * smaller stack frames than the interpreter. The only thing we need
  7576. * to guarantee is that the fallback routines can find end_ofs. */
  7577. upb_sink sink;
  7578. /* The absolute stream offset of the end-of-frame delimiter.
  7579. * Non-delimited frames (groups and non-packed repeated fields) reuse the
  7580. * delimiter of their parent, even though the frame may not end there.
  7581. *
  7582. * NOTE: the JIT stores a slightly different value here for non-top frames.
  7583. * It stores the value relative to the end of the enclosed message. But the
  7584. * top frame is still stored the same way, which is important for ensuring
  7585. * that calls from the JIT into C work correctly. */
  7586. uint64_t end_ofs;
  7587. const uint32_t *base;
  7588. /* 0 indicates a length-delimited field.
  7589. * A positive number indicates a known group.
  7590. * A negative number indicates an unknown group. */
  7591. int32_t groupnum;
  7592. upb_inttable *dispatch; /* Not used by the JIT. */
  7593. } upb_pbdecoder_frame;
  7594. struct upb_pbdecodermethod {
  7595. upb_refcounted base;
  7596. /* While compiling, the base is relative in "ofs", after compiling it is
  7597. * absolute in "ptr". */
  7598. union {
  7599. uint32_t ofs; /* PC offset of method. */
  7600. void *ptr; /* Pointer to bytecode or machine code for this method. */
  7601. } code_base;
  7602. /* The decoder method group to which this method belongs. We own a ref.
  7603. * Owning a ref on the entire group is more coarse-grained than is strictly
  7604. * necessary; all we truly require is that methods we directly reference
  7605. * outlive us, while the group could contain many other messages we don't
  7606. * require. But the group represents the messages that were
  7607. * allocated+compiled together, so it makes the most sense to free them
  7608. * together also. */
  7609. const upb_refcounted *group;
  7610. /* Whether this method is native code or bytecode. */
  7611. bool is_native_;
  7612. /* The handler one calls to invoke this method. */
  7613. upb_byteshandler input_handler_;
  7614. /* The destination handlers this method is bound to. We own a ref. */
  7615. const upb_handlers *dest_handlers_;
  7616. /* Dispatch table -- used by both bytecode decoder and JIT when encountering a
  7617. * field number that wasn't the one we were expecting to see. See
  7618. * decoder.int.h for the layout of this table. */
  7619. upb_inttable dispatch;
  7620. };
  7621. struct upb_pbdecoder {
  7622. upb_env *env;
  7623. /* Our input sink. */
  7624. upb_bytessink input_;
  7625. /* The decoder method we are parsing with (owned). */
  7626. const upb_pbdecodermethod *method_;
  7627. size_t call_len;
  7628. const uint32_t *pc, *last;
  7629. /* Current input buffer and its stream offset. */
  7630. const char *buf, *ptr, *end, *checkpoint;
  7631. /* End of the delimited region, relative to ptr, NULL if not in this buf. */
  7632. const char *delim_end;
  7633. /* End of the delimited region, relative to ptr, end if not in this buf. */
  7634. const char *data_end;
  7635. /* Overall stream offset of "buf." */
  7636. uint64_t bufstart_ofs;
  7637. /* Buffer for residual bytes not parsed from the previous buffer. */
  7638. char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
  7639. char *residual_end;
  7640. /* Bytes of data that should be discarded from the input beore we start
  7641. * parsing again. We set this when we internally determine that we can
  7642. * safely skip the next N bytes, but this region extends past the current
  7643. * user buffer. */
  7644. size_t skip;
  7645. /* Stores the user buffer passed to our decode function. */
  7646. const char *buf_param;
  7647. size_t size_param;
  7648. const upb_bufhandle *handle;
  7649. /* Our internal stack. */
  7650. upb_pbdecoder_frame *stack, *top, *limit;
  7651. const uint32_t **callstack;
  7652. size_t stack_size;
  7653. upb_status *status;
  7654. #ifdef UPB_USE_JIT_X64
  7655. /* Used momentarily by the generated code to store a value while a user
  7656. * function is called. */
  7657. uint32_t tmp_len;
  7658. const void *saved_rsp;
  7659. #endif
  7660. };
  7661. /* Decoder entry points; used as handlers. */
  7662. void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
  7663. void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
  7664. size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
  7665. size_t size, const upb_bufhandle *handle);
  7666. bool upb_pbdecoder_end(void *closure, const void *handler_data);
  7667. /* Decoder-internal functions that the JIT calls to handle fallback paths. */
  7668. int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
  7669. size_t size, const upb_bufhandle *handle);
  7670. size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
  7671. int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
  7672. uint8_t wire_type);
  7673. int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
  7674. int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
  7675. int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
  7676. int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
  7677. void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
  7678. /* Error messages that are shared between the bytecode and JIT decoders. */
  7679. extern const char *kPbDecoderStackOverflow;
  7680. extern const char *kPbDecoderSubmessageTooLong;
  7681. /* Access to decoderplan members needed by the decoder. */
  7682. const char *upb_pbdecoder_getopname(unsigned int op);
  7683. /* JIT codegen entry point. */
  7684. void upb_pbdecoder_jit(mgroup *group);
  7685. void upb_pbdecoder_freejit(mgroup *group);
  7686. UPB_REFCOUNTED_CMETHODS(mgroup, mgroup_upcast)
  7687. /* A special label that means "do field dispatch for this message and branch to
  7688. * wherever that takes you." */
  7689. #define LABEL_DISPATCH 0
  7690. /* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
  7691. * RET) for branching to when we find an appropriate ENDGROUP tag. */
  7692. #define DISPATCH_ENDMSG 0
  7693. /* It's important to use this invalid wire type instead of 0 (which is a valid
  7694. * wire type). */
  7695. #define NO_WIRE_TYPE 0xff
  7696. /* The dispatch table layout is:
  7697. * [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
  7698. *
  7699. * If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
  7700. * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
  7701. *
  7702. * We need two wire types because of packed/non-packed compatibility. A
  7703. * primitive repeated field can use either wire type and be valid. While we
  7704. * could key the table on fieldnum+wiretype, the table would be 8x sparser.
  7705. *
  7706. * Storing two wire types in the primary value allows us to quickly rule out
  7707. * the second wire type without needing to do a separate lookup (this case is
  7708. * less common than an unknown field). */
  7709. UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
  7710. uint8_t wt2) {
  7711. return (ofs << 16) | (wt2 << 8) | wt1;
  7712. }
  7713. UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
  7714. uint8_t *wt1, uint8_t *wt2) {
  7715. *wt1 = (uint8_t)dispatch;
  7716. *wt2 = (uint8_t)(dispatch >> 8);
  7717. *ofs = dispatch >> 16;
  7718. }
  7719. /* All of the functions in decoder.c that return int32_t return values according
  7720. * to the following scheme:
  7721. * 1. negative values indicate a return code from the following list.
  7722. * 2. positive values indicate that error or end of buffer was hit, and
  7723. * that the decode function should immediately return the given value
  7724. * (the decoder state has already been suspended and is ready to be
  7725. * resumed). */
  7726. #define DECODE_OK -1
  7727. #define DECODE_MISMATCH -2 /* Used only from checktag_slow(). */
  7728. #define DECODE_ENDGROUP -3 /* Used only from checkunknown(). */
  7729. #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
  7730. #endif /* UPB_DECODER_INT_H_ */
  7731. /*
  7732. ** A number of routines for varint manipulation (we keep them all around to
  7733. ** have multiple approaches available for benchmarking).
  7734. */
  7735. #ifndef UPB_VARINT_DECODER_H_
  7736. #define UPB_VARINT_DECODER_H_
  7737. #include <assert.h>
  7738. #include <stdint.h>
  7739. #include <string.h>
  7740. #ifdef __cplusplus
  7741. extern "C" {
  7742. #endif
  7743. #define UPB_MAX_WIRE_TYPE 5
  7744. /* The maximum number of bytes that it takes to encode a 64-bit varint. */
  7745. #define UPB_PB_VARINT_MAX_LEN 10
  7746. /* Array of the "native" (ie. non-packed-repeated) wire type for the given a
  7747. * descriptor type (upb_descriptortype_t). */
  7748. extern const uint8_t upb_pb_native_wire_types[];
  7749. /* Zig-zag encoding/decoding **************************************************/
  7750. UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
  7751. return (n >> 1) ^ -(int32_t)(n & 1);
  7752. }
  7753. UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
  7754. return (n >> 1) ^ -(int64_t)(n & 1);
  7755. }
  7756. UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
  7757. UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
  7758. /* Decoding *******************************************************************/
  7759. /* All decoding functions return this struct by value. */
  7760. typedef struct {
  7761. const char *p; /* NULL if the varint was unterminated. */
  7762. uint64_t val;
  7763. } upb_decoderet;
  7764. UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
  7765. upb_decoderet ret;
  7766. ret.p = p;
  7767. ret.val = val;
  7768. return ret;
  7769. }
  7770. upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
  7771. upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
  7772. /* Template for a function that checks the first two bytes with branching
  7773. * and dispatches 2-10 bytes with a separate function. Note that this may read
  7774. * up to 10 bytes, so it must not be used unless there are at least ten bytes
  7775. * left in the buffer! */
  7776. #define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
  7777. UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
  7778. uint8_t *p = (uint8_t*)_p; \
  7779. upb_decoderet r; \
  7780. if ((*p & 0x80) == 0) { \
  7781. /* Common case: one-byte varint. */ \
  7782. return upb_decoderet_make(_p + 1, *p & 0x7fU); \
  7783. } \
  7784. r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)); \
  7785. if ((*(p + 1) & 0x80) == 0) { \
  7786. /* Two-byte varint. */ \
  7787. return r; \
  7788. } \
  7789. /* Longer varint, fallback to out-of-line function. */ \
  7790. return decode_max8_function(r); \
  7791. }
  7792. UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
  7793. UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
  7794. #undef UPB_VARINT_DECODER_CHECK2
  7795. /* Our canonical functions for decoding varints, based on the currently
  7796. * favored best-performing implementations. */
  7797. UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
  7798. if (sizeof(long) == 8)
  7799. return upb_vdecode_check2_branch64(p);
  7800. else
  7801. return upb_vdecode_check2_branch32(p);
  7802. }
  7803. /* Encoding *******************************************************************/
  7804. UPB_INLINE int upb_value_size(uint64_t val) {
  7805. #ifdef __GNUC__
  7806. int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */
  7807. #else
  7808. int high_bit = 0;
  7809. uint64_t tmp = val;
  7810. while(tmp >>= 1) high_bit++;
  7811. #endif
  7812. return val == 0 ? 1 : high_bit / 8 + 1;
  7813. }
  7814. /* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
  7815. * bytes long), returning how many bytes were used.
  7816. *
  7817. * TODO: benchmark and optimize if necessary. */
  7818. UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
  7819. size_t i;
  7820. if (val == 0) { buf[0] = 0; return 1; }
  7821. i = 0;
  7822. while (val) {
  7823. uint8_t byte = val & 0x7fU;
  7824. val >>= 7;
  7825. if (val) byte |= 0x80U;
  7826. buf[i++] = byte;
  7827. }
  7828. return i;
  7829. }
  7830. UPB_INLINE size_t upb_varint_size(uint64_t val) {
  7831. char buf[UPB_PB_VARINT_MAX_LEN];
  7832. return upb_vencode64(val, buf);
  7833. }
  7834. /* Encodes a 32-bit varint, *not* sign-extended. */
  7835. UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
  7836. char buf[UPB_PB_VARINT_MAX_LEN];
  7837. size_t bytes = upb_vencode64(val, buf);
  7838. uint64_t ret = 0;
  7839. UPB_ASSERT(bytes <= 5);
  7840. memcpy(&ret, buf, bytes);
  7841. UPB_ASSERT(ret <= 0xffffffffffU);
  7842. return ret;
  7843. }
  7844. #ifdef __cplusplus
  7845. } /* extern "C" */
  7846. #endif
  7847. #endif /* UPB_VARINT_DECODER_H_ */
  7848. /*
  7849. ** upb::pb::Encoder (upb_pb_encoder)
  7850. **
  7851. ** Implements a set of upb_handlers that write protobuf data to the binary wire
  7852. ** format.
  7853. **
  7854. ** This encoder implementation does not have any access to any out-of-band or
  7855. ** precomputed lengths for submessages, so it must buffer submessages internally
  7856. ** before it can emit the first byte.
  7857. */
  7858. #ifndef UPB_ENCODER_H_
  7859. #define UPB_ENCODER_H_
  7860. #ifdef __cplusplus
  7861. namespace upb {
  7862. namespace pb {
  7863. class Encoder;
  7864. } /* namespace pb */
  7865. } /* namespace upb */
  7866. #endif
  7867. UPB_DECLARE_TYPE(upb::pb::Encoder, upb_pb_encoder)
  7868. #define UPB_PBENCODER_MAX_NESTING 100
  7869. /* upb::pb::Encoder ***********************************************************/
  7870. /* Preallocation hint: decoder won't allocate more bytes than this when first
  7871. * constructed. This hint may be an overestimate for some build configurations.
  7872. * But if the decoder library is upgraded without recompiling the application,
  7873. * it may be an underestimate. */
  7874. #define UPB_PB_ENCODER_SIZE 768
  7875. #ifdef __cplusplus
  7876. class upb::pb::Encoder {
  7877. public:
  7878. /* Creates a new encoder in the given environment. The Handlers must have
  7879. * come from NewHandlers() below. */
  7880. static Encoder* Create(Environment* env, const Handlers* handlers,
  7881. BytesSink* output);
  7882. /* The input to the encoder. */
  7883. Sink* input();
  7884. /* Creates a new set of handlers for this MessageDef. */
  7885. static reffed_ptr<const Handlers> NewHandlers(const MessageDef* msg);
  7886. static const size_t kSize = UPB_PB_ENCODER_SIZE;
  7887. private:
  7888. UPB_DISALLOW_POD_OPS(Encoder, upb::pb::Encoder)
  7889. };
  7890. #endif
  7891. UPB_BEGIN_EXTERN_C
  7892. const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
  7893. const void *owner);
  7894. upb_sink *upb_pb_encoder_input(upb_pb_encoder *p);
  7895. upb_pb_encoder* upb_pb_encoder_create(upb_env* e, const upb_handlers* h,
  7896. upb_bytessink* output);
  7897. UPB_END_EXTERN_C
  7898. #ifdef __cplusplus
  7899. namespace upb {
  7900. namespace pb {
  7901. inline Encoder* Encoder::Create(Environment* env, const Handlers* handlers,
  7902. BytesSink* output) {
  7903. return upb_pb_encoder_create(env, handlers, output);
  7904. }
  7905. inline Sink* Encoder::input() {
  7906. return upb_pb_encoder_input(this);
  7907. }
  7908. inline reffed_ptr<const Handlers> Encoder::NewHandlers(
  7909. const upb::MessageDef *md) {
  7910. const Handlers* h = upb_pb_encoder_newhandlers(md, &h);
  7911. return reffed_ptr<const Handlers>(h, &h);
  7912. }
  7913. } /* namespace pb */
  7914. } /* namespace upb */
  7915. #endif
  7916. #endif /* UPB_ENCODER_H_ */
  7917. /*
  7918. ** upb's core components like upb_decoder and upb_msg are carefully designed to
  7919. ** avoid depending on each other for maximum orthogonality. In other words,
  7920. ** you can use a upb_decoder to decode into *any* kind of structure; upb_msg is
  7921. ** just one such structure. A upb_msg can be serialized/deserialized into any
  7922. ** format, protobuf binary format is just one such format.
  7923. **
  7924. ** However, for convenience we provide functions here for doing common
  7925. ** operations like deserializing protobuf binary format into a upb_msg. The
  7926. ** compromise is that this file drags in almost all of upb as a dependency,
  7927. ** which could be undesirable if you're trying to use a trimmed-down build of
  7928. ** upb.
  7929. **
  7930. ** While these routines are convenient, they do not reuse any encoding/decoding
  7931. ** state. For example, if a decoder is JIT-based, it will be re-JITted every
  7932. ** time these functions are called. For this reason, if you are parsing lots
  7933. ** of data and efficiency is an issue, these may not be the best functions to
  7934. ** use (though they are useful for prototyping, before optimizing).
  7935. */
  7936. #ifndef UPB_GLUE_H
  7937. #define UPB_GLUE_H
  7938. #include <stdbool.h>
  7939. #ifdef __cplusplus
  7940. #include <vector>
  7941. extern "C" {
  7942. #endif
  7943. /* Loads a binary descriptor and returns a NULL-terminated array of unfrozen
  7944. * filedefs. The caller owns the returned array, which must be freed with
  7945. * upb_gfree(). */
  7946. upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
  7947. upb_status *status);
  7948. #ifdef __cplusplus
  7949. } /* extern "C" */
  7950. namespace upb {
  7951. inline bool LoadDescriptor(const char* buf, size_t n, Status* status,
  7952. std::vector<reffed_ptr<FileDef> >* files) {
  7953. FileDef** parsed_files = upb_loaddescriptor(buf, n, &parsed_files, status);
  7954. if (parsed_files) {
  7955. FileDef** p = parsed_files;
  7956. while (*p) {
  7957. files->push_back(reffed_ptr<FileDef>(*p, &parsed_files));
  7958. ++p;
  7959. }
  7960. free(parsed_files);
  7961. return true;
  7962. } else {
  7963. return false;
  7964. }
  7965. }
  7966. /* Templated so it can accept both string and std::string. */
  7967. template <typename T>
  7968. bool LoadDescriptor(const T& desc, Status* status,
  7969. std::vector<reffed_ptr<FileDef> >* files) {
  7970. return LoadDescriptor(desc.c_str(), desc.size(), status, files);
  7971. }
  7972. } /* namespace upb */
  7973. #endif
  7974. #endif /* UPB_GLUE_H */
  7975. /*
  7976. ** upb::pb::TextPrinter (upb_textprinter)
  7977. **
  7978. ** Handlers for writing to protobuf text format.
  7979. */
  7980. #ifndef UPB_TEXT_H_
  7981. #define UPB_TEXT_H_
  7982. #ifdef __cplusplus
  7983. namespace upb {
  7984. namespace pb {
  7985. class TextPrinter;
  7986. } /* namespace pb */
  7987. } /* namespace upb */
  7988. #endif
  7989. UPB_DECLARE_TYPE(upb::pb::TextPrinter, upb_textprinter)
  7990. #ifdef __cplusplus
  7991. class upb::pb::TextPrinter {
  7992. public:
  7993. /* The given handlers must have come from NewHandlers(). It must outlive the
  7994. * TextPrinter. */
  7995. static TextPrinter *Create(Environment *env, const upb::Handlers *handlers,
  7996. BytesSink *output);
  7997. void SetSingleLineMode(bool single_line);
  7998. Sink* input();
  7999. /* If handler caching becomes a requirement we can add a code cache as in
  8000. * decoder.h */
  8001. static reffed_ptr<const Handlers> NewHandlers(const MessageDef* md);
  8002. };
  8003. #endif
  8004. UPB_BEGIN_EXTERN_C
  8005. /* C API. */
  8006. upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
  8007. upb_bytessink *output);
  8008. void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
  8009. upb_sink *upb_textprinter_input(upb_textprinter *p);
  8010. const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
  8011. const void *owner);
  8012. UPB_END_EXTERN_C
  8013. #ifdef __cplusplus
  8014. namespace upb {
  8015. namespace pb {
  8016. inline TextPrinter *TextPrinter::Create(Environment *env,
  8017. const upb::Handlers *handlers,
  8018. BytesSink *output) {
  8019. return upb_textprinter_create(env, handlers, output);
  8020. }
  8021. inline void TextPrinter::SetSingleLineMode(bool single_line) {
  8022. upb_textprinter_setsingleline(this, single_line);
  8023. }
  8024. inline Sink* TextPrinter::input() {
  8025. return upb_textprinter_input(this);
  8026. }
  8027. inline reffed_ptr<const Handlers> TextPrinter::NewHandlers(
  8028. const MessageDef *md) {
  8029. const Handlers* h = upb_textprinter_newhandlers(md, &h);
  8030. return reffed_ptr<const Handlers>(h, &h);
  8031. }
  8032. } /* namespace pb */
  8033. } /* namespace upb */
  8034. #endif
  8035. #endif /* UPB_TEXT_H_ */
  8036. /*
  8037. ** upb::json::Parser (upb_json_parser)
  8038. **
  8039. ** Parses JSON according to a specific schema.
  8040. ** Support for parsing arbitrary JSON (schema-less) will be added later.
  8041. */
  8042. #ifndef UPB_JSON_PARSER_H_
  8043. #define UPB_JSON_PARSER_H_
  8044. #ifdef __cplusplus
  8045. namespace upb {
  8046. namespace json {
  8047. class Parser;
  8048. class ParserMethod;
  8049. } /* namespace json */
  8050. } /* namespace upb */
  8051. #endif
  8052. UPB_DECLARE_TYPE(upb::json::Parser, upb_json_parser)
  8053. UPB_DECLARE_DERIVED_TYPE(upb::json::ParserMethod, upb::RefCounted,
  8054. upb_json_parsermethod, upb_refcounted)
  8055. /* upb::json::Parser **********************************************************/
  8056. /* Preallocation hint: parser won't allocate more bytes than this when first
  8057. * constructed. This hint may be an overestimate for some build configurations.
  8058. * But if the parser library is upgraded without recompiling the application,
  8059. * it may be an underestimate. */
  8060. #define UPB_JSON_PARSER_SIZE 5712
  8061. #ifdef __cplusplus
  8062. /* Parses an incoming BytesStream, pushing the results to the destination
  8063. * sink. */
  8064. class upb::json::Parser {
  8065. public:
  8066. static Parser* Create(Environment* env, const ParserMethod* method,
  8067. const SymbolTable* symtab,
  8068. Sink* output, bool ignore_json_unknown);
  8069. BytesSink* input();
  8070. private:
  8071. UPB_DISALLOW_POD_OPS(Parser, upb::json::Parser)
  8072. };
  8073. class upb::json::ParserMethod {
  8074. public:
  8075. /* Include base methods from upb::ReferenceCounted. */
  8076. UPB_REFCOUNTED_CPPMETHODS
  8077. /* Returns handlers for parsing according to the specified schema. */
  8078. static reffed_ptr<const ParserMethod> New(const upb::MessageDef* md);
  8079. /* The destination handlers that are statically bound to this method.
  8080. * This method is only capable of outputting to a sink that uses these
  8081. * handlers. */
  8082. const Handlers* dest_handlers() const;
  8083. /* The input handlers for this decoder method. */
  8084. const BytesHandler* input_handler() const;
  8085. private:
  8086. UPB_DISALLOW_POD_OPS(ParserMethod, upb::json::ParserMethod)
  8087. };
  8088. #endif
  8089. UPB_BEGIN_EXTERN_C
  8090. upb_json_parser* upb_json_parser_create(upb_env* e,
  8091. const upb_json_parsermethod* m,
  8092. const upb_symtab* symtab,
  8093. upb_sink* output,
  8094. bool ignore_json_unknown);
  8095. upb_bytessink *upb_json_parser_input(upb_json_parser *p);
  8096. upb_json_parsermethod* upb_json_parsermethod_new(const upb_msgdef* md,
  8097. const void* owner);
  8098. const upb_handlers *upb_json_parsermethod_desthandlers(
  8099. const upb_json_parsermethod *m);
  8100. const upb_byteshandler *upb_json_parsermethod_inputhandler(
  8101. const upb_json_parsermethod *m);
  8102. /* Include refcounted methods like upb_json_parsermethod_ref(). */
  8103. UPB_REFCOUNTED_CMETHODS(upb_json_parsermethod, upb_json_parsermethod_upcast)
  8104. UPB_END_EXTERN_C
  8105. #ifdef __cplusplus
  8106. namespace upb {
  8107. namespace json {
  8108. inline Parser* Parser::Create(Environment* env, const ParserMethod* method,
  8109. const SymbolTable* symtab,
  8110. Sink* output, bool ignore_json_unknown) {
  8111. return upb_json_parser_create(
  8112. env, method, symtab, output, ignore_json_unknown);
  8113. }
  8114. inline BytesSink* Parser::input() {
  8115. return upb_json_parser_input(this);
  8116. }
  8117. inline const Handlers* ParserMethod::dest_handlers() const {
  8118. return upb_json_parsermethod_desthandlers(this);
  8119. }
  8120. inline const BytesHandler* ParserMethod::input_handler() const {
  8121. return upb_json_parsermethod_inputhandler(this);
  8122. }
  8123. /* static */
  8124. inline reffed_ptr<const ParserMethod> ParserMethod::New(
  8125. const MessageDef* md) {
  8126. const upb_json_parsermethod *m = upb_json_parsermethod_new(md, &m);
  8127. return reffed_ptr<const ParserMethod>(m, &m);
  8128. }
  8129. } /* namespace json */
  8130. } /* namespace upb */
  8131. #endif
  8132. #endif /* UPB_JSON_PARSER_H_ */
  8133. /*
  8134. ** upb::json::Printer
  8135. **
  8136. ** Handlers that emit JSON according to a specific protobuf schema.
  8137. */
  8138. #ifndef UPB_JSON_TYPED_PRINTER_H_
  8139. #define UPB_JSON_TYPED_PRINTER_H_
  8140. #ifdef __cplusplus
  8141. namespace upb {
  8142. namespace json {
  8143. class Printer;
  8144. } /* namespace json */
  8145. } /* namespace upb */
  8146. #endif
  8147. UPB_DECLARE_TYPE(upb::json::Printer, upb_json_printer)
  8148. /* upb::json::Printer *********************************************************/
  8149. #define UPB_JSON_PRINTER_SIZE 192
  8150. #ifdef __cplusplus
  8151. /* Prints an incoming stream of data to a BytesSink in JSON format. */
  8152. class upb::json::Printer {
  8153. public:
  8154. static Printer* Create(Environment* env, const upb::Handlers* handlers,
  8155. BytesSink* output);
  8156. /* The input to the printer. */
  8157. Sink* input();
  8158. /* Returns handlers for printing according to the specified schema.
  8159. * If preserve_proto_fieldnames is true, the output JSON will use the
  8160. * original .proto field names (ie. {"my_field":3}) instead of using
  8161. * camelCased names, which is the default: (eg. {"myField":3}). */
  8162. static reffed_ptr<const Handlers> NewHandlers(const upb::MessageDef* md,
  8163. bool preserve_proto_fieldnames);
  8164. static const size_t kSize = UPB_JSON_PRINTER_SIZE;
  8165. private:
  8166. UPB_DISALLOW_POD_OPS(Printer, upb::json::Printer)
  8167. };
  8168. #endif
  8169. UPB_BEGIN_EXTERN_C
  8170. /* Native C API. */
  8171. upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
  8172. upb_bytessink *output);
  8173. upb_sink *upb_json_printer_input(upb_json_printer *p);
  8174. const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
  8175. bool preserve_fieldnames,
  8176. const void *owner);
  8177. UPB_END_EXTERN_C
  8178. #ifdef __cplusplus
  8179. namespace upb {
  8180. namespace json {
  8181. inline Printer* Printer::Create(Environment* env, const upb::Handlers* handlers,
  8182. BytesSink* output) {
  8183. return upb_json_printer_create(env, handlers, output);
  8184. }
  8185. inline Sink* Printer::input() { return upb_json_printer_input(this); }
  8186. inline reffed_ptr<const Handlers> Printer::NewHandlers(
  8187. const upb::MessageDef *md, bool preserve_proto_fieldnames) {
  8188. const Handlers* h = upb_json_printer_newhandlers(
  8189. md, preserve_proto_fieldnames, &h);
  8190. return reffed_ptr<const Handlers>(h, &h);
  8191. }
  8192. } /* namespace json */
  8193. } /* namespace upb */
  8194. #endif
  8195. #endif /* UPB_JSON_TYPED_PRINTER_H_ */
  8196. #undef UPB_SIZE
  8197. #undef UPB_FIELD_AT
  8198. #undef UPB_READ_ONEOF
  8199. #undef UPB_WRITE_ONEOF