upb.h 405 KB

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