upb.h 311 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255
  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. #include <stdint.h>
  8. #ifndef UINTPTR_MAX
  9. #error must include stdint.h first
  10. #endif
  11. #if UINTPTR_MAX == 0xffffffff
  12. #define UPB_SIZE(size32, size64) size32
  13. #else
  14. #define UPB_SIZE(size32, size64) size64
  15. #endif
  16. #define UPB_FIELD_AT(msg, fieldtype, offset) \
  17. *(fieldtype*)((const char*)(msg) + offset)
  18. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  19. UPB_FIELD_AT(msg, int, case_offset) == case_val \
  20. ? UPB_FIELD_AT(msg, fieldtype, offset) \
  21. : default
  22. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  23. UPB_FIELD_AT(msg, int, case_offset) = case_val; \
  24. UPB_FIELD_AT(msg, fieldtype, offset) = value;
  25. /*
  26. ** upb::Message is a representation for protobuf messages.
  27. **
  28. ** However it differs from other common representations like
  29. ** google::protobuf::Message in one key way: it does not prescribe any
  30. ** ownership between messages and submessages, and it relies on the
  31. ** client to ensure that each submessage/array/map outlives its parent.
  32. **
  33. ** All messages, arrays, and maps live in an Arena. If the entire message
  34. ** tree is in the same arena, ensuring proper lifetimes is simple. However
  35. ** the client can mix arenas as long as they ensure that there are no
  36. ** dangling pointers.
  37. **
  38. ** A client can access a upb::Message without knowing anything about
  39. ** ownership semantics, but to create or mutate a message a user needs
  40. ** to implement the memory management themselves.
  41. **
  42. ** TODO: UTF-8 checking?
  43. **/
  44. #ifndef UPB_MSG_H_
  45. #define UPB_MSG_H_
  46. #include <stdint.h>
  47. #include <string.h>
  48. /*
  49. ** This file contains shared definitions that are widely used across upb.
  50. **
  51. ** This is a mixed C/C++ interface that offers a full API to both languages.
  52. ** See the top-level README for more information.
  53. */
  54. #ifndef UPB_H_
  55. #define UPB_H_
  56. #include <assert.h>
  57. #include <stdarg.h>
  58. #include <stdbool.h>
  59. #include <stddef.h>
  60. #include <stdint.h>
  61. #ifdef __cplusplus
  62. #include <memory>
  63. namespace upb {
  64. class Arena;
  65. class Status;
  66. template <int N> class InlinedArena;
  67. }
  68. #endif
  69. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  70. #ifdef __cplusplus
  71. #define UPB_INLINE inline
  72. #elif defined (__GNUC__)
  73. #define UPB_INLINE static __inline__
  74. #else
  75. #define UPB_INLINE static
  76. #endif
  77. /* Hints to the compiler about likely/unlikely branches. */
  78. #define UPB_LIKELY(x) __builtin_expect((x),1)
  79. /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
  80. * doesn't provide these preprocessor symbols. */
  81. #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  82. #define UPB_BIG_ENDIAN
  83. #endif
  84. /* Macros for function attributes on compilers that support them. */
  85. #ifdef __GNUC__
  86. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  87. #define UPB_NOINLINE __attribute__((noinline))
  88. #define UPB_NORETURN __attribute__((__noreturn__))
  89. #else /* !defined(__GNUC__) */
  90. #define UPB_FORCEINLINE
  91. #define UPB_NOINLINE
  92. #define UPB_NORETURN
  93. #endif
  94. #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
  95. /* C99/C++11 versions. */
  96. #include <stdio.h>
  97. #define _upb_snprintf snprintf
  98. #define _upb_vsnprintf vsnprintf
  99. #define _upb_va_copy(a, b) va_copy(a, b)
  100. #elif defined __GNUC__
  101. /* A few hacky workarounds for functions not in C89.
  102. * For internal use only!
  103. * TODO(haberman): fix these by including our own implementations, or finding
  104. * another workaround.
  105. */
  106. #define _upb_snprintf __builtin_snprintf
  107. #define _upb_vsnprintf __builtin_vsnprintf
  108. #define _upb_va_copy(a, b) __va_copy(a, b)
  109. #else
  110. #error Need implementations of [v]snprintf and va_copy
  111. #endif
  112. #ifdef __cplusplus
  113. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
  114. (defined(_MSC_VER) && _MSC_VER >= 1900)
  115. // C++11 is present
  116. #else
  117. #error upb requires C++11 for C++ support
  118. #endif
  119. #endif
  120. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  121. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  122. #define UPB_UNUSED(var) (void)var
  123. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  124. * evaluated. This prevents "unused variable" warnings. */
  125. #ifdef NDEBUG
  126. #define UPB_ASSERT(expr) do {} while (false && (expr))
  127. #else
  128. #define UPB_ASSERT(expr) assert(expr)
  129. #endif
  130. /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
  131. * exist in debug mode. This turns into regular assert. */
  132. #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
  133. #ifdef __GNUC__
  134. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  135. #else
  136. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  137. #endif
  138. /* upb_status *****************************************************************/
  139. /* upb_status represents a success or failure status and error message.
  140. * It owns no resources and allocates no memory, so it should work
  141. * even in OOM situations. */
  142. /* The maximum length of an error message before it will get truncated. */
  143. #define UPB_STATUS_MAX_MESSAGE 127
  144. typedef struct {
  145. bool ok;
  146. char msg[UPB_STATUS_MAX_MESSAGE]; /* Error message; NULL-terminated. */
  147. } upb_status;
  148. #ifdef __cplusplus
  149. extern "C" {
  150. #endif
  151. const char *upb_status_errmsg(const upb_status *status);
  152. bool upb_ok(const upb_status *status);
  153. /* Any of the functions that write to a status object allow status to be NULL,
  154. * to support use cases where the function's caller does not care about the
  155. * status message. */
  156. void upb_status_clear(upb_status *status);
  157. void upb_status_seterrmsg(upb_status *status, const char *msg);
  158. void upb_status_seterrf(upb_status *status, const char *fmt, ...);
  159. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args);
  160. UPB_INLINE void upb_status_setoom(upb_status *status) {
  161. upb_status_seterrmsg(status, "out of memory");
  162. }
  163. #ifdef __cplusplus
  164. } /* extern "C" */
  165. class upb::Status {
  166. public:
  167. Status() { upb_status_clear(&status_); }
  168. upb_status* ptr() { return &status_; }
  169. /* Returns true if there is no error. */
  170. bool ok() const { return upb_ok(&status_); }
  171. /* Guaranteed to be NULL-terminated. */
  172. const char *error_message() const { return upb_status_errmsg(&status_); }
  173. /* The error message will be truncated if it is longer than
  174. * UPB_STATUS_MAX_MESSAGE-4. */
  175. void SetErrorMessage(const char *msg) { upb_status_seterrmsg(&status_, msg); }
  176. void SetFormattedErrorMessage(const char *fmt, ...) {
  177. va_list args;
  178. va_start(args, fmt);
  179. upb_status_vseterrf(&status_, fmt, args);
  180. va_end(args);
  181. }
  182. /* Resets the status to a successful state with no message. */
  183. void Clear() { upb_status_clear(&status_); }
  184. private:
  185. upb_status status_;
  186. };
  187. #endif /* __cplusplus */
  188. /** upb_alloc *****************************************************************/
  189. /* A upb_alloc is a possibly-stateful allocator object.
  190. *
  191. * It could either be an arena allocator (which doesn't require individual
  192. * free() calls) or a regular malloc() (which does). The client must therefore
  193. * free memory unless it knows that the allocator is an arena allocator. */
  194. struct upb_alloc;
  195. typedef struct upb_alloc upb_alloc;
  196. /* A malloc()/free() function.
  197. * If "size" is 0 then the function acts like free(), otherwise it acts like
  198. * realloc(). Only "oldsize" bytes from a previous allocation are preserved. */
  199. typedef void *upb_alloc_func(upb_alloc *alloc, void *ptr, size_t oldsize,
  200. size_t size);
  201. struct upb_alloc {
  202. upb_alloc_func *func;
  203. };
  204. UPB_INLINE void *upb_malloc(upb_alloc *alloc, size_t size) {
  205. UPB_ASSERT(alloc);
  206. return alloc->func(alloc, NULL, 0, size);
  207. }
  208. UPB_INLINE void *upb_realloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  209. size_t size) {
  210. UPB_ASSERT(alloc);
  211. return alloc->func(alloc, ptr, oldsize, size);
  212. }
  213. UPB_INLINE void upb_free(upb_alloc *alloc, void *ptr) {
  214. assert(alloc);
  215. alloc->func(alloc, ptr, 0, 0);
  216. }
  217. /* The global allocator used by upb. Uses the standard malloc()/free(). */
  218. extern upb_alloc upb_alloc_global;
  219. /* Functions that hard-code the global malloc.
  220. *
  221. * We still get benefit because we can put custom logic into our global
  222. * allocator, like injecting out-of-memory faults in debug/testing builds. */
  223. UPB_INLINE void *upb_gmalloc(size_t size) {
  224. return upb_malloc(&upb_alloc_global, size);
  225. }
  226. UPB_INLINE void *upb_grealloc(void *ptr, size_t oldsize, size_t size) {
  227. return upb_realloc(&upb_alloc_global, ptr, oldsize, size);
  228. }
  229. UPB_INLINE void upb_gfree(void *ptr) {
  230. upb_free(&upb_alloc_global, ptr);
  231. }
  232. /* upb_arena ******************************************************************/
  233. /* upb_arena is a specific allocator implementation that uses arena allocation.
  234. * The user provides an allocator that will be used to allocate the underlying
  235. * arena blocks. Arenas by nature do not require the individual allocations
  236. * to be freed. However the Arena does allow users to register cleanup
  237. * functions that will run when the arena is destroyed.
  238. *
  239. * A upb_arena is *not* thread-safe.
  240. *
  241. * You could write a thread-safe arena allocator that satisfies the
  242. * upb_alloc interface, but it would not be as efficient for the
  243. * single-threaded case. */
  244. typedef void upb_cleanup_func(void *ud);
  245. struct upb_arena;
  246. typedef struct upb_arena upb_arena;
  247. #ifdef __cplusplus
  248. extern "C" {
  249. #endif
  250. /* Creates an arena from the given initial block (if any -- n may be 0).
  251. * Additional blocks will be allocated from |alloc|. If |alloc| is NULL, this
  252. * is a fixed-size arena and cannot grow. */
  253. upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc);
  254. void upb_arena_free(upb_arena *a);
  255. bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func);
  256. size_t upb_arena_bytesallocated(const upb_arena *a);
  257. UPB_INLINE upb_alloc *upb_arena_alloc(upb_arena *a) { return (upb_alloc*)a; }
  258. /* Convenience wrappers around upb_alloc functions. */
  259. UPB_INLINE void *upb_arena_malloc(upb_arena *a, size_t size) {
  260. return upb_malloc(upb_arena_alloc(a), size);
  261. }
  262. UPB_INLINE void *upb_arena_realloc(upb_arena *a, void *ptr, size_t oldsize,
  263. size_t size) {
  264. return upb_realloc(upb_arena_alloc(a), ptr, oldsize, size);
  265. }
  266. UPB_INLINE upb_arena *upb_arena_new() {
  267. return upb_arena_init(NULL, 0, &upb_alloc_global);
  268. }
  269. #ifdef __cplusplus
  270. } /* extern "C" */
  271. class upb::Arena {
  272. public:
  273. /* A simple arena with no initial memory block and the default allocator. */
  274. Arena() : ptr_(upb_arena_new(), upb_arena_free) {}
  275. upb_arena* ptr() { return ptr_.get(); }
  276. /* Allows this arena to be used as a generic allocator.
  277. *
  278. * The arena does not need free() calls so when using Arena as an allocator
  279. * it is safe to skip them. However they are no-ops so there is no harm in
  280. * calling free() either. */
  281. upb_alloc *allocator() { return upb_arena_alloc(ptr_.get()); }
  282. /* Add a cleanup function to run when the arena is destroyed.
  283. * Returns false on out-of-memory. */
  284. bool AddCleanup(void *ud, upb_cleanup_func* func) {
  285. return upb_arena_addcleanup(ptr_.get(), ud, func);
  286. }
  287. /* Total number of bytes that have been allocated. It is undefined what
  288. * Realloc() does to &arena_ counter. */
  289. size_t BytesAllocated() const { return upb_arena_bytesallocated(ptr_.get()); }
  290. private:
  291. std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
  292. };
  293. #endif
  294. /* upb::InlinedArena **********************************************************/
  295. /* upb::InlinedArena seeds the arenas with a predefined amount of memory. No
  296. * heap memory will be allocated until the initial block is exceeded.
  297. *
  298. * These types only exist in C++ */
  299. #ifdef __cplusplus
  300. template <int N> class upb::InlinedArena : public upb::Arena {
  301. public:
  302. InlinedArena() : ptr_(upb_arena_new(&initial_block_, N, &upb_alloc_global)) {}
  303. upb_arena* ptr() { return ptr_.get(); }
  304. private:
  305. InlinedArena(const InlinedArena*) = delete;
  306. InlinedArena& operator=(const InlinedArena*) = delete;
  307. std::unique_ptr<upb_arena, decltype(&upb_arena_free)> ptr_;
  308. char initial_block_[N];
  309. };
  310. #endif /* __cplusplus */
  311. /* Constants ******************************************************************/
  312. /* Generic function type. */
  313. typedef void upb_func();
  314. /* A list of types as they are encoded on-the-wire. */
  315. typedef enum {
  316. UPB_WIRE_TYPE_VARINT = 0,
  317. UPB_WIRE_TYPE_64BIT = 1,
  318. UPB_WIRE_TYPE_DELIMITED = 2,
  319. UPB_WIRE_TYPE_START_GROUP = 3,
  320. UPB_WIRE_TYPE_END_GROUP = 4,
  321. UPB_WIRE_TYPE_32BIT = 5
  322. } upb_wiretype_t;
  323. /* The types a field can have. Note that this list is not identical to the
  324. * types defined in descriptor.proto, which gives INT32 and SINT32 separate
  325. * types (we distinguish the two with the "integer encoding" enum below). */
  326. typedef enum {
  327. /* Types stored in 1 byte. */
  328. UPB_TYPE_BOOL = 1,
  329. /* Types stored in 4 bytes. */
  330. UPB_TYPE_FLOAT = 2,
  331. UPB_TYPE_INT32 = 3,
  332. UPB_TYPE_UINT32 = 4,
  333. UPB_TYPE_ENUM = 5, /* Enum values are int32. */
  334. /* Types stored as pointers (probably 4 or 8 bytes). */
  335. UPB_TYPE_STRING = 6,
  336. UPB_TYPE_BYTES = 7,
  337. UPB_TYPE_MESSAGE = 8,
  338. /* Types stored as 8 bytes. */
  339. UPB_TYPE_DOUBLE = 9,
  340. UPB_TYPE_INT64 = 10,
  341. UPB_TYPE_UINT64 = 11
  342. } upb_fieldtype_t;
  343. /* The repeated-ness of each field; this matches descriptor.proto. */
  344. typedef enum {
  345. UPB_LABEL_OPTIONAL = 1,
  346. UPB_LABEL_REQUIRED = 2,
  347. UPB_LABEL_REPEATED = 3
  348. } upb_label_t;
  349. /* Descriptor types, as defined in descriptor.proto. */
  350. typedef enum {
  351. UPB_DESCRIPTOR_TYPE_DOUBLE = 1,
  352. UPB_DESCRIPTOR_TYPE_FLOAT = 2,
  353. UPB_DESCRIPTOR_TYPE_INT64 = 3,
  354. UPB_DESCRIPTOR_TYPE_UINT64 = 4,
  355. UPB_DESCRIPTOR_TYPE_INT32 = 5,
  356. UPB_DESCRIPTOR_TYPE_FIXED64 = 6,
  357. UPB_DESCRIPTOR_TYPE_FIXED32 = 7,
  358. UPB_DESCRIPTOR_TYPE_BOOL = 8,
  359. UPB_DESCRIPTOR_TYPE_STRING = 9,
  360. UPB_DESCRIPTOR_TYPE_GROUP = 10,
  361. UPB_DESCRIPTOR_TYPE_MESSAGE = 11,
  362. UPB_DESCRIPTOR_TYPE_BYTES = 12,
  363. UPB_DESCRIPTOR_TYPE_UINT32 = 13,
  364. UPB_DESCRIPTOR_TYPE_ENUM = 14,
  365. UPB_DESCRIPTOR_TYPE_SFIXED32 = 15,
  366. UPB_DESCRIPTOR_TYPE_SFIXED64 = 16,
  367. UPB_DESCRIPTOR_TYPE_SINT32 = 17,
  368. UPB_DESCRIPTOR_TYPE_SINT64 = 18
  369. } upb_descriptortype_t;
  370. extern const uint8_t upb_desctype_to_fieldtype[];
  371. #endif /* UPB_H_ */
  372. /*
  373. ** structs.int.h: structures definitions that are internal to upb.
  374. */
  375. #ifndef UPB_STRUCTS_H_
  376. #define UPB_STRUCTS_H_
  377. struct upb_array {
  378. upb_fieldtype_t type;
  379. uint8_t element_size;
  380. void *data; /* Each element is element_size. */
  381. size_t len; /* Measured in elements. */
  382. size_t size; /* Measured in elements. */
  383. upb_arena *arena;
  384. };
  385. #endif /* UPB_STRUCTS_H_ */
  386. #ifdef __cplusplus
  387. namespace upb {
  388. class Array;
  389. class Map;
  390. class MapIterator;
  391. class MessageLayout;
  392. }
  393. #endif
  394. /* TODO(haberman): C++ accessors */
  395. #ifdef __cplusplus
  396. extern "C" {
  397. #endif
  398. typedef void upb_msg;
  399. struct upb_array;
  400. typedef struct upb_array upb_array;
  401. struct upb_map;
  402. typedef struct upb_map upb_map;
  403. struct upb_mapiter;
  404. typedef struct upb_mapiter upb_mapiter;
  405. /** upb_msglayout *************************************************************/
  406. /* upb_msglayout represents the memory layout of a given upb_msgdef. The
  407. * members are public so generated code can initialize them, but users MUST NOT
  408. * read or write any of its members. */
  409. typedef struct {
  410. uint32_t number;
  411. uint16_t offset;
  412. int16_t presence; /* If >0, hasbit_index+1. If <0, oneof_index+1. */
  413. uint16_t submsg_index; /* undefined if descriptortype != MESSAGE or GROUP. */
  414. uint8_t descriptortype;
  415. uint8_t label;
  416. } upb_msglayout_field;
  417. typedef struct upb_msglayout {
  418. const struct upb_msglayout *const* submsgs;
  419. const upb_msglayout_field *fields;
  420. /* Must be aligned to sizeof(void*). Doesn't include internal members like
  421. * unknown fields, extension dict, pointer to msglayout, etc. */
  422. uint16_t size;
  423. uint16_t field_count;
  424. bool extendable;
  425. } upb_msglayout;
  426. /** upb_strview ************************************************************/
  427. typedef struct {
  428. const char *data;
  429. size_t size;
  430. } upb_strview;
  431. UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
  432. upb_strview ret;
  433. ret.data = data;
  434. ret.size = size;
  435. return ret;
  436. }
  437. UPB_INLINE upb_strview upb_strview_makez(const char *data) {
  438. return upb_strview_make(data, strlen(data));
  439. }
  440. UPB_INLINE bool upb_strview_eql(upb_strview a, upb_strview b) {
  441. return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
  442. }
  443. #define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
  444. #define UPB_STRVIEW_FORMAT "%.*s"
  445. #define UPB_STRVIEW_ARGS(view) (int)(view).size, (view).data
  446. /** upb_msgval ****************************************************************/
  447. /* A union representing all possible protobuf values. Used for generic get/set
  448. * operations. */
  449. typedef union {
  450. bool b;
  451. float flt;
  452. double dbl;
  453. int32_t i32;
  454. int64_t i64;
  455. uint32_t u32;
  456. uint64_t u64;
  457. const upb_map* map;
  458. const upb_msg* msg;
  459. const upb_array* arr;
  460. const void* ptr;
  461. upb_strview str;
  462. } upb_msgval;
  463. #define ACCESSORS(name, membername, ctype) \
  464. UPB_INLINE ctype upb_msgval_get ## name(upb_msgval v) { \
  465. return v.membername; \
  466. } \
  467. UPB_INLINE void upb_msgval_set ## name(upb_msgval *v, ctype cval) { \
  468. v->membername = cval; \
  469. } \
  470. UPB_INLINE upb_msgval upb_msgval_ ## name(ctype v) { \
  471. upb_msgval ret; \
  472. ret.membername = v; \
  473. return ret; \
  474. }
  475. ACCESSORS(bool, b, bool)
  476. ACCESSORS(float, flt, float)
  477. ACCESSORS(double, dbl, double)
  478. ACCESSORS(int32, i32, int32_t)
  479. ACCESSORS(int64, i64, int64_t)
  480. ACCESSORS(uint32, u32, uint32_t)
  481. ACCESSORS(uint64, u64, uint64_t)
  482. ACCESSORS(map, map, const upb_map*)
  483. ACCESSORS(msg, msg, const upb_msg*)
  484. ACCESSORS(ptr, ptr, const void*)
  485. ACCESSORS(arr, arr, const upb_array*)
  486. ACCESSORS(str, str, upb_strview)
  487. #undef ACCESSORS
  488. UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
  489. return upb_msgval_str(upb_strview_make(data, size));
  490. }
  491. /** upb_msg *******************************************************************/
  492. /* A upb_msg represents a protobuf message. It always corresponds to a specific
  493. * upb_msglayout, which describes how it is laid out in memory. */
  494. /* Creates a new message of the given type/layout in this arena. */
  495. upb_msg *upb_msg_new(const upb_msglayout *l, upb_arena *a);
  496. /* Returns the arena for the given message. */
  497. upb_arena *upb_msg_arena(const upb_msg *msg);
  498. void upb_msg_addunknown(upb_msg *msg, const char *data, size_t len);
  499. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len);
  500. /* Read-only message API. Can be safely called by anyone. */
  501. /* Returns the value associated with this field:
  502. * - for scalar fields (including strings), the value directly.
  503. * - return upb_msg*, or upb_map* for msg/map.
  504. * If the field is unset for these field types, returns NULL.
  505. *
  506. * TODO(haberman): should we let users store cached array/map/msg
  507. * pointers here for fields that are unset? Could be useful for the
  508. * strongly-owned submessage model (ie. generated C API that doesn't use
  509. * arenas).
  510. */
  511. upb_msgval upb_msg_get(const upb_msg *msg,
  512. int field_index,
  513. const upb_msglayout *l);
  514. /* May only be called for fields where upb_fielddef_haspresence(f) == true. */
  515. bool upb_msg_has(const upb_msg *msg,
  516. int field_index,
  517. const upb_msglayout *l);
  518. /* Mutable message API. May only be called by the owner of the message who
  519. * knows its ownership scheme and how to keep it consistent. */
  520. /* Sets the given field to the given value. Does not perform any memory
  521. * management: if you overwrite a pointer to a msg/array/map/string without
  522. * cleaning it up (or using an arena) it will leak.
  523. */
  524. void upb_msg_set(upb_msg *msg,
  525. int field_index,
  526. upb_msgval val,
  527. const upb_msglayout *l);
  528. /* For a primitive field, set it back to its default. For repeated, string, and
  529. * submessage fields set it back to NULL. This could involve releasing some
  530. * internal memory (for example, from an extension dictionary), but it is not
  531. * recursive in any way and will not recover any memory that may be used by
  532. * arrays/maps/strings/msgs that this field may have pointed to.
  533. */
  534. bool upb_msg_clearfield(upb_msg *msg,
  535. int field_index,
  536. const upb_msglayout *l);
  537. /* TODO(haberman): copyfrom()/mergefrom()? */
  538. /** upb_array *****************************************************************/
  539. /* A upb_array stores data for a repeated field. The memory management
  540. * semantics are the same as upb_msg. A upb_array allocates dynamic
  541. * memory internally for the array elements. */
  542. upb_array *upb_array_new(upb_fieldtype_t type, upb_arena *a);
  543. upb_fieldtype_t upb_array_type(const upb_array *arr);
  544. /* Read-only interface. Safe for anyone to call. */
  545. size_t upb_array_size(const upb_array *arr);
  546. upb_msgval upb_array_get(const upb_array *arr, size_t i);
  547. /* Write interface. May only be called by the message's owner who can enforce
  548. * its memory management invariants. */
  549. bool upb_array_set(upb_array *arr, size_t i, upb_msgval val);
  550. /** upb_map *******************************************************************/
  551. /* A upb_map stores data for a map field. The memory management semantics are
  552. * the same as upb_msg, with one notable exception. upb_map will internally
  553. * store a copy of all string keys, but *not* any string values or submessages.
  554. * So you must ensure that any string or message values outlive the map, and you
  555. * must delete them manually when they are no longer required. */
  556. upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
  557. upb_arena *a);
  558. /* Read-only interface. Safe for anyone to call. */
  559. size_t upb_map_size(const upb_map *map);
  560. upb_fieldtype_t upb_map_keytype(const upb_map *map);
  561. upb_fieldtype_t upb_map_valuetype(const upb_map *map);
  562. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val);
  563. /* Write interface. May only be called by the message's owner who can enforce
  564. * its memory management invariants. */
  565. /* Sets or overwrites an entry in the map. Return value indicates whether
  566. * the operation succeeded or failed with OOM, and also whether an existing
  567. * key was replaced or not. */
  568. bool upb_map_set(upb_map *map,
  569. upb_msgval key, upb_msgval val,
  570. upb_msgval *valremoved);
  571. /* Deletes an entry in the map. Returns true if the key was present. */
  572. bool upb_map_del(upb_map *map, upb_msgval key);
  573. /** upb_mapiter ***************************************************************/
  574. /* For iterating over a map. Map iterators are invalidated by mutations to the
  575. * map, but an invalidated iterator will never return junk or crash the process.
  576. * An invalidated iterator may return entries that were already returned though,
  577. * and if you keep invalidating the iterator during iteration, the program may
  578. * enter an infinite loop. */
  579. size_t upb_mapiter_sizeof();
  580. void upb_mapiter_begin(upb_mapiter *i, const upb_map *t);
  581. upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a);
  582. void upb_mapiter_free(upb_mapiter *i, upb_alloc *a);
  583. void upb_mapiter_next(upb_mapiter *i);
  584. bool upb_mapiter_done(const upb_mapiter *i);
  585. upb_msgval upb_mapiter_key(const upb_mapiter *i);
  586. upb_msgval upb_mapiter_value(const upb_mapiter *i);
  587. void upb_mapiter_setdone(upb_mapiter *i);
  588. bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2);
  589. #ifdef __cplusplus
  590. } /* extern "C" */
  591. #endif
  592. #endif /* UPB_MSG_H_ */
  593. /* This file was generated by upbc (the upb compiler) from the input
  594. * file:
  595. *
  596. * google/protobuf/descriptor.proto
  597. *
  598. * Do not edit -- your changes will be discarded when the file is
  599. * regenerated. */
  600. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
  601. #define GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_
  602. /*
  603. ** Functions for use by generated code. These are not public and users must
  604. ** not call them directly.
  605. */
  606. #ifndef UPB_GENERATED_UTIL_H_
  607. #define UPB_GENERATED_UTIL_H_
  608. #include <stdint.h>
  609. #define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
  610. UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
  611. size_t *size) {
  612. const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
  613. if (arr) {
  614. if (size) *size = arr->len;
  615. return arr->data;
  616. } else {
  617. if (size) *size = 0;
  618. return NULL;
  619. }
  620. }
  621. UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
  622. size_t *size) {
  623. upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
  624. if (arr) {
  625. if (size) *size = arr->len;
  626. return arr->data;
  627. } else {
  628. if (size) *size = 0;
  629. return NULL;
  630. }
  631. }
  632. /* TODO(haberman): this is a mess. It will improve when upb_array no longer
  633. * carries reflective state (type, elem_size). */
  634. UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
  635. size_t elem_size,
  636. upb_fieldtype_t type,
  637. upb_arena *arena) {
  638. upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
  639. if (!arr) {
  640. arr = upb_array_new(type, arena);
  641. if (!arr) return NULL;
  642. *PTR_AT(msg, ofs, upb_array*) = arr;
  643. }
  644. if (size > arr->size) {
  645. size_t new_size = UPB_MAX(arr->size, 4);
  646. size_t old_bytes = arr->size * elem_size;
  647. size_t new_bytes;
  648. while (new_size < size) new_size *= 2;
  649. new_bytes = new_size * elem_size;
  650. arr->data = upb_arena_realloc(arena, arr->data, old_bytes, new_bytes);
  651. if (!arr->data) {
  652. return NULL;
  653. }
  654. arr->size = new_size;
  655. }
  656. arr->len = size;
  657. return arr->data;
  658. }
  659. UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
  660. size_t elem_size,
  661. upb_fieldtype_t type,
  662. const void *value,
  663. upb_arena *arena) {
  664. upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
  665. size_t i = arr ? arr->len : 0;
  666. void *data =
  667. _upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
  668. if (!data) return false;
  669. memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
  670. return true;
  671. }
  672. UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
  673. return (*PTR_AT(msg, idx / 8, const char) & (1 << (idx % 8))) != 0;
  674. }
  675. UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
  676. return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8));
  677. }
  678. UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
  679. return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8));
  680. }
  681. UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
  682. return *PTR_AT(msg, case_ofs, int32_t) == num;
  683. }
  684. #undef PTR_AT
  685. #endif /* UPB_GENERATED_UTIL_H_ */
  686. /*
  687. ** upb_decode: parsing into a upb_msg using a upb_msglayout.
  688. */
  689. #ifndef UPB_DECODE_H_
  690. #define UPB_DECODE_H_
  691. #ifdef __cplusplus
  692. extern "C" {
  693. #endif
  694. bool upb_decode(const char *buf, size_t size, upb_msg *msg,
  695. const upb_msglayout *l);
  696. #ifdef __cplusplus
  697. } /* extern "C" */
  698. #endif
  699. #endif /* UPB_DECODE_H_ */
  700. /*
  701. ** upb_encode: parsing into a upb_msg using a upb_msglayout.
  702. */
  703. #ifndef UPB_ENCODE_H_
  704. #define UPB_ENCODE_H_
  705. #ifdef __cplusplus
  706. extern "C" {
  707. #endif
  708. char *upb_encode(const void *msg, const upb_msglayout *l, upb_arena *arena,
  709. size_t *size);
  710. #ifdef __cplusplus
  711. } /* extern "C" */
  712. #endif
  713. #endif /* UPB_ENCODE_H_ */
  714. #ifdef __cplusplus
  715. extern "C" {
  716. #endif
  717. struct google_protobuf_FileDescriptorSet;
  718. struct google_protobuf_FileDescriptorProto;
  719. struct google_protobuf_DescriptorProto;
  720. struct google_protobuf_DescriptorProto_ExtensionRange;
  721. struct google_protobuf_DescriptorProto_ReservedRange;
  722. struct google_protobuf_ExtensionRangeOptions;
  723. struct google_protobuf_FieldDescriptorProto;
  724. struct google_protobuf_OneofDescriptorProto;
  725. struct google_protobuf_EnumDescriptorProto;
  726. struct google_protobuf_EnumDescriptorProto_EnumReservedRange;
  727. struct google_protobuf_EnumValueDescriptorProto;
  728. struct google_protobuf_ServiceDescriptorProto;
  729. struct google_protobuf_MethodDescriptorProto;
  730. struct google_protobuf_FileOptions;
  731. struct google_protobuf_MessageOptions;
  732. struct google_protobuf_FieldOptions;
  733. struct google_protobuf_OneofOptions;
  734. struct google_protobuf_EnumOptions;
  735. struct google_protobuf_EnumValueOptions;
  736. struct google_protobuf_ServiceOptions;
  737. struct google_protobuf_MethodOptions;
  738. struct google_protobuf_UninterpretedOption;
  739. struct google_protobuf_UninterpretedOption_NamePart;
  740. struct google_protobuf_SourceCodeInfo;
  741. struct google_protobuf_SourceCodeInfo_Location;
  742. struct google_protobuf_GeneratedCodeInfo;
  743. struct google_protobuf_GeneratedCodeInfo_Annotation;
  744. typedef struct google_protobuf_FileDescriptorSet google_protobuf_FileDescriptorSet;
  745. typedef struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto;
  746. typedef struct google_protobuf_DescriptorProto google_protobuf_DescriptorProto;
  747. typedef struct google_protobuf_DescriptorProto_ExtensionRange google_protobuf_DescriptorProto_ExtensionRange;
  748. typedef struct google_protobuf_DescriptorProto_ReservedRange google_protobuf_DescriptorProto_ReservedRange;
  749. typedef struct google_protobuf_ExtensionRangeOptions google_protobuf_ExtensionRangeOptions;
  750. typedef struct google_protobuf_FieldDescriptorProto google_protobuf_FieldDescriptorProto;
  751. typedef struct google_protobuf_OneofDescriptorProto google_protobuf_OneofDescriptorProto;
  752. typedef struct google_protobuf_EnumDescriptorProto google_protobuf_EnumDescriptorProto;
  753. typedef struct google_protobuf_EnumDescriptorProto_EnumReservedRange google_protobuf_EnumDescriptorProto_EnumReservedRange;
  754. typedef struct google_protobuf_EnumValueDescriptorProto google_protobuf_EnumValueDescriptorProto;
  755. typedef struct google_protobuf_ServiceDescriptorProto google_protobuf_ServiceDescriptorProto;
  756. typedef struct google_protobuf_MethodDescriptorProto google_protobuf_MethodDescriptorProto;
  757. typedef struct google_protobuf_FileOptions google_protobuf_FileOptions;
  758. typedef struct google_protobuf_MessageOptions google_protobuf_MessageOptions;
  759. typedef struct google_protobuf_FieldOptions google_protobuf_FieldOptions;
  760. typedef struct google_protobuf_OneofOptions google_protobuf_OneofOptions;
  761. typedef struct google_protobuf_EnumOptions google_protobuf_EnumOptions;
  762. typedef struct google_protobuf_EnumValueOptions google_protobuf_EnumValueOptions;
  763. typedef struct google_protobuf_ServiceOptions google_protobuf_ServiceOptions;
  764. typedef struct google_protobuf_MethodOptions google_protobuf_MethodOptions;
  765. typedef struct google_protobuf_UninterpretedOption google_protobuf_UninterpretedOption;
  766. typedef struct google_protobuf_UninterpretedOption_NamePart google_protobuf_UninterpretedOption_NamePart;
  767. typedef struct google_protobuf_SourceCodeInfo google_protobuf_SourceCodeInfo;
  768. typedef struct google_protobuf_SourceCodeInfo_Location google_protobuf_SourceCodeInfo_Location;
  769. typedef struct google_protobuf_GeneratedCodeInfo google_protobuf_GeneratedCodeInfo;
  770. typedef struct google_protobuf_GeneratedCodeInfo_Annotation google_protobuf_GeneratedCodeInfo_Annotation;
  771. extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
  772. extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
  773. extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
  774. extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
  775. extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
  776. extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
  777. extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
  778. extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
  779. extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
  780. extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
  781. extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
  782. extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
  783. extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
  784. extern const upb_msglayout google_protobuf_FileOptions_msginit;
  785. extern const upb_msglayout google_protobuf_MessageOptions_msginit;
  786. extern const upb_msglayout google_protobuf_FieldOptions_msginit;
  787. extern const upb_msglayout google_protobuf_OneofOptions_msginit;
  788. extern const upb_msglayout google_protobuf_EnumOptions_msginit;
  789. extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
  790. extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
  791. extern const upb_msglayout google_protobuf_MethodOptions_msginit;
  792. extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
  793. extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
  794. extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
  795. extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
  796. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
  797. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
  798. /* Enums */
  799. typedef enum {
  800. google_protobuf_FieldDescriptorProto_LABEL_OPTIONAL = 1,
  801. google_protobuf_FieldDescriptorProto_LABEL_REQUIRED = 2,
  802. google_protobuf_FieldDescriptorProto_LABEL_REPEATED = 3
  803. } google_protobuf_FieldDescriptorProto_Label;
  804. typedef enum {
  805. google_protobuf_FieldDescriptorProto_TYPE_DOUBLE = 1,
  806. google_protobuf_FieldDescriptorProto_TYPE_FLOAT = 2,
  807. google_protobuf_FieldDescriptorProto_TYPE_INT64 = 3,
  808. google_protobuf_FieldDescriptorProto_TYPE_UINT64 = 4,
  809. google_protobuf_FieldDescriptorProto_TYPE_INT32 = 5,
  810. google_protobuf_FieldDescriptorProto_TYPE_FIXED64 = 6,
  811. google_protobuf_FieldDescriptorProto_TYPE_FIXED32 = 7,
  812. google_protobuf_FieldDescriptorProto_TYPE_BOOL = 8,
  813. google_protobuf_FieldDescriptorProto_TYPE_STRING = 9,
  814. google_protobuf_FieldDescriptorProto_TYPE_GROUP = 10,
  815. google_protobuf_FieldDescriptorProto_TYPE_MESSAGE = 11,
  816. google_protobuf_FieldDescriptorProto_TYPE_BYTES = 12,
  817. google_protobuf_FieldDescriptorProto_TYPE_UINT32 = 13,
  818. google_protobuf_FieldDescriptorProto_TYPE_ENUM = 14,
  819. google_protobuf_FieldDescriptorProto_TYPE_SFIXED32 = 15,
  820. google_protobuf_FieldDescriptorProto_TYPE_SFIXED64 = 16,
  821. google_protobuf_FieldDescriptorProto_TYPE_SINT32 = 17,
  822. google_protobuf_FieldDescriptorProto_TYPE_SINT64 = 18
  823. } google_protobuf_FieldDescriptorProto_Type;
  824. typedef enum {
  825. google_protobuf_FieldOptions_STRING = 0,
  826. google_protobuf_FieldOptions_CORD = 1,
  827. google_protobuf_FieldOptions_STRING_PIECE = 2
  828. } google_protobuf_FieldOptions_CType;
  829. typedef enum {
  830. google_protobuf_FieldOptions_JS_NORMAL = 0,
  831. google_protobuf_FieldOptions_JS_STRING = 1,
  832. google_protobuf_FieldOptions_JS_NUMBER = 2
  833. } google_protobuf_FieldOptions_JSType;
  834. typedef enum {
  835. google_protobuf_FileOptions_SPEED = 1,
  836. google_protobuf_FileOptions_CODE_SIZE = 2,
  837. google_protobuf_FileOptions_LITE_RUNTIME = 3
  838. } google_protobuf_FileOptions_OptimizeMode;
  839. typedef enum {
  840. google_protobuf_MethodOptions_IDEMPOTENCY_UNKNOWN = 0,
  841. google_protobuf_MethodOptions_NO_SIDE_EFFECTS = 1,
  842. google_protobuf_MethodOptions_IDEMPOTENT = 2
  843. } google_protobuf_MethodOptions_IdempotencyLevel;
  844. /* google.protobuf.FileDescriptorSet */
  845. UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_arena *arena) {
  846. return (google_protobuf_FileDescriptorSet *)upb_msg_new(&google_protobuf_FileDescriptorSet_msginit, arena);
  847. }
  848. UPB_INLINE google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parse(const char *buf, size_t size,
  849. upb_arena *arena) {
  850. google_protobuf_FileDescriptorSet *ret = google_protobuf_FileDescriptorSet_new(arena);
  851. return (ret && upb_decode(buf, size, ret, &google_protobuf_FileDescriptorSet_msginit)) ? ret : NULL;
  852. }
  853. UPB_INLINE char *google_protobuf_FileDescriptorSet_serialize(const google_protobuf_FileDescriptorSet *msg, upb_arena *arena, size_t *len) {
  854. return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, arena, len);
  855. }
  856. UPB_INLINE const google_protobuf_FileDescriptorProto* const* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg, size_t *len) { return (const google_protobuf_FileDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  857. UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_mutable_file(google_protobuf_FileDescriptorSet *msg, size_t *len) {
  858. return (google_protobuf_FileDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  859. }
  860. UPB_INLINE google_protobuf_FileDescriptorProto** google_protobuf_FileDescriptorSet_resize_file(google_protobuf_FileDescriptorSet *msg, size_t len, upb_arena *arena) {
  861. return (google_protobuf_FileDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  862. }
  863. UPB_INLINE struct google_protobuf_FileDescriptorProto* google_protobuf_FileDescriptorSet_add_file(google_protobuf_FileDescriptorSet *msg, upb_arena *arena) {
  864. struct google_protobuf_FileDescriptorProto* sub = (struct google_protobuf_FileDescriptorProto*)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
  865. bool ok = _upb_array_append_accessor(
  866. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  867. if (!ok) return NULL;
  868. return sub;
  869. }
  870. /* google.protobuf.FileDescriptorProto */
  871. UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_arena *arena) {
  872. return (google_protobuf_FileDescriptorProto *)upb_msg_new(&google_protobuf_FileDescriptorProto_msginit, arena);
  873. }
  874. UPB_INLINE google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parse(const char *buf, size_t size,
  875. upb_arena *arena) {
  876. google_protobuf_FileDescriptorProto *ret = google_protobuf_FileDescriptorProto_new(arena);
  877. return (ret && upb_decode(buf, size, ret, &google_protobuf_FileDescriptorProto_msginit)) ? ret : NULL;
  878. }
  879. UPB_INLINE char *google_protobuf_FileDescriptorProto_serialize(const google_protobuf_FileDescriptorProto *msg, upb_arena *arena, size_t *len) {
  880. return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, arena, len);
  881. }
  882. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_name(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  883. UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  884. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_package(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  885. UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
  886. UPB_INLINE upb_strview const* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); }
  887. UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); }
  888. UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); }
  889. UPB_INLINE const google_protobuf_ServiceDescriptorProto* const* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_ServiceDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(48, 96), len); }
  890. UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(52, 104), len); }
  891. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_options(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 4); }
  892. 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(28, 56)); }
  893. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_source_code_info(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 5); }
  894. 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(32, 64)); }
  895. UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(56, 112), len); }
  896. UPB_INLINE int32_t const* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(60, 120), len); }
  897. UPB_INLINE bool google_protobuf_FileDescriptorProto_has_syntax(const google_protobuf_FileDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  898. UPB_INLINE upb_strview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); }
  899. UPB_INLINE void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
  900. _upb_sethas(msg, 1);
  901. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  902. }
  903. UPB_INLINE void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
  904. _upb_sethas(msg, 2);
  905. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
  906. }
  907. UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_mutable_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  908. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len);
  909. }
  910. UPB_INLINE upb_strview* google_protobuf_FileDescriptorProto_resize_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  911. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  912. }
  913. UPB_INLINE bool google_protobuf_FileDescriptorProto_add_dependency(google_protobuf_FileDescriptorProto *msg, upb_strview val, upb_arena *arena) {
  914. return _upb_array_append_accessor(
  915. msg, UPB_SIZE(36, 72), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  916. }
  917. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_mutable_message_type(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  918. return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len);
  919. }
  920. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_FileDescriptorProto_resize_message_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  921. return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  922. }
  923. UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_FileDescriptorProto_add_message_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  924. struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  925. bool ok = _upb_array_append_accessor(
  926. msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  927. if (!ok) return NULL;
  928. return sub;
  929. }
  930. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_mutable_enum_type(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  931. return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len);
  932. }
  933. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_FileDescriptorProto_resize_enum_type(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  934. return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  935. }
  936. UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_FileDescriptorProto_add_enum_type(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  937. struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  938. bool ok = _upb_array_append_accessor(
  939. msg, UPB_SIZE(44, 88), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  940. if (!ok) return NULL;
  941. return sub;
  942. }
  943. UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_mutable_service(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  944. return (google_protobuf_ServiceDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(48, 96), len);
  945. }
  946. UPB_INLINE google_protobuf_ServiceDescriptorProto** google_protobuf_FileDescriptorProto_resize_service(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  947. return (google_protobuf_ServiceDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(48, 96), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  948. }
  949. UPB_INLINE struct google_protobuf_ServiceDescriptorProto* google_protobuf_FileDescriptorProto_add_service(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  950. struct google_protobuf_ServiceDescriptorProto* sub = (struct google_protobuf_ServiceDescriptorProto*)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
  951. bool ok = _upb_array_append_accessor(
  952. msg, UPB_SIZE(48, 96), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  953. if (!ok) return NULL;
  954. return sub;
  955. }
  956. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_mutable_extension(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  957. return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(52, 104), len);
  958. }
  959. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_FileDescriptorProto_resize_extension(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  960. return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(52, 104), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  961. }
  962. UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_FileDescriptorProto_add_extension(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  963. struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  964. bool ok = _upb_array_append_accessor(
  965. msg, UPB_SIZE(52, 104), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  966. if (!ok) return NULL;
  967. return sub;
  968. }
  969. UPB_INLINE void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) {
  970. _upb_sethas(msg, 4);
  971. UPB_FIELD_AT(msg, google_protobuf_FileOptions*, UPB_SIZE(28, 56)) = value;
  972. }
  973. UPB_INLINE struct google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_mutable_options(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  974. struct google_protobuf_FileOptions* sub = (struct google_protobuf_FileOptions*)google_protobuf_FileDescriptorProto_options(msg);
  975. if (sub == NULL) {
  976. sub = (struct google_protobuf_FileOptions*)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
  977. if (!sub) return NULL;
  978. google_protobuf_FileDescriptorProto_set_options(msg, sub);
  979. }
  980. return sub;
  981. }
  982. UPB_INLINE void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value) {
  983. _upb_sethas(msg, 5);
  984. UPB_FIELD_AT(msg, google_protobuf_SourceCodeInfo*, UPB_SIZE(32, 64)) = value;
  985. }
  986. UPB_INLINE struct google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_mutable_source_code_info(google_protobuf_FileDescriptorProto *msg, upb_arena *arena) {
  987. struct google_protobuf_SourceCodeInfo* sub = (struct google_protobuf_SourceCodeInfo*)google_protobuf_FileDescriptorProto_source_code_info(msg);
  988. if (sub == NULL) {
  989. sub = (struct google_protobuf_SourceCodeInfo*)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
  990. if (!sub) return NULL;
  991. google_protobuf_FileDescriptorProto_set_source_code_info(msg, sub);
  992. }
  993. return sub;
  994. }
  995. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  996. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 112), len);
  997. }
  998. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_public_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  999. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(56, 112), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  1000. }
  1001. UPB_INLINE bool google_protobuf_FileDescriptorProto_add_public_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) {
  1002. return _upb_array_append_accessor(
  1003. msg, UPB_SIZE(56, 112), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  1004. }
  1005. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_mutable_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t *len) {
  1006. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(60, 120), len);
  1007. }
  1008. UPB_INLINE int32_t* google_protobuf_FileDescriptorProto_resize_weak_dependency(google_protobuf_FileDescriptorProto *msg, size_t len, upb_arena *arena) {
  1009. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(60, 120), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  1010. }
  1011. UPB_INLINE bool google_protobuf_FileDescriptorProto_add_weak_dependency(google_protobuf_FileDescriptorProto *msg, int32_t val, upb_arena *arena) {
  1012. return _upb_array_append_accessor(
  1013. msg, UPB_SIZE(60, 120), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  1014. }
  1015. UPB_INLINE void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_strview value) {
  1016. _upb_sethas(msg, 3);
  1017. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value;
  1018. }
  1019. /* google.protobuf.DescriptorProto */
  1020. UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_arena *arena) {
  1021. return (google_protobuf_DescriptorProto *)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  1022. }
  1023. UPB_INLINE google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parse(const char *buf, size_t size,
  1024. upb_arena *arena) {
  1025. google_protobuf_DescriptorProto *ret = google_protobuf_DescriptorProto_new(arena);
  1026. return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_msginit)) ? ret : NULL;
  1027. }
  1028. UPB_INLINE char *google_protobuf_DescriptorProto_serialize(const google_protobuf_DescriptorProto *msg, upb_arena *arena, size_t *len) {
  1029. return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, arena, len);
  1030. }
  1031. UPB_INLINE bool google_protobuf_DescriptorProto_has_name(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1032. UPB_INLINE upb_strview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1033. UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
  1034. UPB_INLINE const google_protobuf_DescriptorProto* const* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
  1035. UPB_INLINE const google_protobuf_EnumDescriptorProto* const* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
  1036. UPB_INLINE const google_protobuf_DescriptorProto_ExtensionRange* const* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ExtensionRange* const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); }
  1037. UPB_INLINE const google_protobuf_FieldDescriptorProto* const* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_FieldDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(32, 64), len); }
  1038. UPB_INLINE bool google_protobuf_DescriptorProto_has_options(const google_protobuf_DescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1039. 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(12, 24)); }
  1040. UPB_INLINE const google_protobuf_OneofDescriptorProto* const* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_OneofDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(36, 72), len); }
  1041. UPB_INLINE const google_protobuf_DescriptorProto_ReservedRange* const* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg, size_t *len) { return (const google_protobuf_DescriptorProto_ReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(40, 80), len); }
  1042. UPB_INLINE upb_strview const* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(44, 88), len); }
  1043. UPB_INLINE void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_strview value) {
  1044. _upb_sethas(msg, 1);
  1045. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1046. }
  1047. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_field(google_protobuf_DescriptorProto *msg, size_t *len) {
  1048. return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
  1049. }
  1050. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_field(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1051. return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1052. }
  1053. UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_field(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1054. struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  1055. bool ok = _upb_array_append_accessor(
  1056. msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1057. if (!ok) return NULL;
  1058. return sub;
  1059. }
  1060. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_mutable_nested_type(google_protobuf_DescriptorProto *msg, size_t *len) {
  1061. return (google_protobuf_DescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
  1062. }
  1063. UPB_INLINE google_protobuf_DescriptorProto** google_protobuf_DescriptorProto_resize_nested_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1064. return (google_protobuf_DescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1065. }
  1066. UPB_INLINE struct google_protobuf_DescriptorProto* google_protobuf_DescriptorProto_add_nested_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1067. struct google_protobuf_DescriptorProto* sub = (struct google_protobuf_DescriptorProto*)upb_msg_new(&google_protobuf_DescriptorProto_msginit, arena);
  1068. bool ok = _upb_array_append_accessor(
  1069. msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1070. if (!ok) return NULL;
  1071. return sub;
  1072. }
  1073. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_mutable_enum_type(google_protobuf_DescriptorProto *msg, size_t *len) {
  1074. return (google_protobuf_EnumDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
  1075. }
  1076. UPB_INLINE google_protobuf_EnumDescriptorProto** google_protobuf_DescriptorProto_resize_enum_type(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1077. return (google_protobuf_EnumDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1078. }
  1079. UPB_INLINE struct google_protobuf_EnumDescriptorProto* google_protobuf_DescriptorProto_add_enum_type(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1080. struct google_protobuf_EnumDescriptorProto* sub = (struct google_protobuf_EnumDescriptorProto*)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  1081. bool ok = _upb_array_append_accessor(
  1082. msg, UPB_SIZE(24, 48), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1083. if (!ok) return NULL;
  1084. return sub;
  1085. }
  1086. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_mutable_extension_range(google_protobuf_DescriptorProto *msg, size_t *len) {
  1087. return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len);
  1088. }
  1089. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange** google_protobuf_DescriptorProto_resize_extension_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1090. return (google_protobuf_DescriptorProto_ExtensionRange**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1091. }
  1092. UPB_INLINE struct google_protobuf_DescriptorProto_ExtensionRange* google_protobuf_DescriptorProto_add_extension_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1093. struct google_protobuf_DescriptorProto_ExtensionRange* sub = (struct google_protobuf_DescriptorProto_ExtensionRange*)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
  1094. bool ok = _upb_array_append_accessor(
  1095. msg, UPB_SIZE(28, 56), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1096. if (!ok) return NULL;
  1097. return sub;
  1098. }
  1099. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_mutable_extension(google_protobuf_DescriptorProto *msg, size_t *len) {
  1100. return (google_protobuf_FieldDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(32, 64), len);
  1101. }
  1102. UPB_INLINE google_protobuf_FieldDescriptorProto** google_protobuf_DescriptorProto_resize_extension(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1103. return (google_protobuf_FieldDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(32, 64), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1104. }
  1105. UPB_INLINE struct google_protobuf_FieldDescriptorProto* google_protobuf_DescriptorProto_add_extension(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1106. struct google_protobuf_FieldDescriptorProto* sub = (struct google_protobuf_FieldDescriptorProto*)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  1107. bool ok = _upb_array_append_accessor(
  1108. msg, UPB_SIZE(32, 64), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1109. if (!ok) return NULL;
  1110. return sub;
  1111. }
  1112. UPB_INLINE void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) {
  1113. _upb_sethas(msg, 2);
  1114. UPB_FIELD_AT(msg, google_protobuf_MessageOptions*, UPB_SIZE(12, 24)) = value;
  1115. }
  1116. UPB_INLINE struct google_protobuf_MessageOptions* google_protobuf_DescriptorProto_mutable_options(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1117. struct google_protobuf_MessageOptions* sub = (struct google_protobuf_MessageOptions*)google_protobuf_DescriptorProto_options(msg);
  1118. if (sub == NULL) {
  1119. sub = (struct google_protobuf_MessageOptions*)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
  1120. if (!sub) return NULL;
  1121. google_protobuf_DescriptorProto_set_options(msg, sub);
  1122. }
  1123. return sub;
  1124. }
  1125. UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_mutable_oneof_decl(google_protobuf_DescriptorProto *msg, size_t *len) {
  1126. return (google_protobuf_OneofDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(36, 72), len);
  1127. }
  1128. UPB_INLINE google_protobuf_OneofDescriptorProto** google_protobuf_DescriptorProto_resize_oneof_decl(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1129. return (google_protobuf_OneofDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(36, 72), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1130. }
  1131. UPB_INLINE struct google_protobuf_OneofDescriptorProto* google_protobuf_DescriptorProto_add_oneof_decl(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1132. struct google_protobuf_OneofDescriptorProto* sub = (struct google_protobuf_OneofDescriptorProto*)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
  1133. bool ok = _upb_array_append_accessor(
  1134. msg, UPB_SIZE(36, 72), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1135. if (!ok) return NULL;
  1136. return sub;
  1137. }
  1138. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_mutable_reserved_range(google_protobuf_DescriptorProto *msg, size_t *len) {
  1139. return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(40, 80), len);
  1140. }
  1141. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange** google_protobuf_DescriptorProto_resize_reserved_range(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1142. return (google_protobuf_DescriptorProto_ReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(40, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1143. }
  1144. UPB_INLINE struct google_protobuf_DescriptorProto_ReservedRange* google_protobuf_DescriptorProto_add_reserved_range(google_protobuf_DescriptorProto *msg, upb_arena *arena) {
  1145. struct google_protobuf_DescriptorProto_ReservedRange* sub = (struct google_protobuf_DescriptorProto_ReservedRange*)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
  1146. bool ok = _upb_array_append_accessor(
  1147. msg, UPB_SIZE(40, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1148. if (!ok) return NULL;
  1149. return sub;
  1150. }
  1151. UPB_INLINE upb_strview* google_protobuf_DescriptorProto_mutable_reserved_name(google_protobuf_DescriptorProto *msg, size_t *len) {
  1152. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(44, 88), len);
  1153. }
  1154. UPB_INLINE upb_strview* google_protobuf_DescriptorProto_resize_reserved_name(google_protobuf_DescriptorProto *msg, size_t len, upb_arena *arena) {
  1155. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(44, 88), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  1156. }
  1157. UPB_INLINE bool google_protobuf_DescriptorProto_add_reserved_name(google_protobuf_DescriptorProto *msg, upb_strview val, upb_arena *arena) {
  1158. return _upb_array_append_accessor(
  1159. msg, UPB_SIZE(44, 88), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  1160. }
  1161. /* google.protobuf.DescriptorProto.ExtensionRange */
  1162. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_arena *arena) {
  1163. return (google_protobuf_DescriptorProto_ExtensionRange *)upb_msg_new(&google_protobuf_DescriptorProto_ExtensionRange_msginit, arena);
  1164. }
  1165. UPB_INLINE google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parse(const char *buf, size_t size,
  1166. upb_arena *arena) {
  1167. google_protobuf_DescriptorProto_ExtensionRange *ret = google_protobuf_DescriptorProto_ExtensionRange_new(arena);
  1168. return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_ExtensionRange_msginit)) ? ret : NULL;
  1169. }
  1170. UPB_INLINE char *google_protobuf_DescriptorProto_ExtensionRange_serialize(const google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena, size_t *len) {
  1171. return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, arena, len);
  1172. }
  1173. UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 1); }
  1174. 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)); }
  1175. UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 2); }
  1176. 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)); }
  1177. UPB_INLINE bool google_protobuf_DescriptorProto_ExtensionRange_has_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) { return _upb_has_field(msg, 3); }
  1178. 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)); }
  1179. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
  1180. _upb_sethas(msg, 1);
  1181. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1182. }
  1183. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
  1184. _upb_sethas(msg, 2);
  1185. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1186. }
  1187. UPB_INLINE void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) {
  1188. _upb_sethas(msg, 3);
  1189. UPB_FIELD_AT(msg, google_protobuf_ExtensionRangeOptions*, UPB_SIZE(12, 16)) = value;
  1190. }
  1191. UPB_INLINE struct google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_mutable_options(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_arena *arena) {
  1192. struct google_protobuf_ExtensionRangeOptions* sub = (struct google_protobuf_ExtensionRangeOptions*)google_protobuf_DescriptorProto_ExtensionRange_options(msg);
  1193. if (sub == NULL) {
  1194. sub = (struct google_protobuf_ExtensionRangeOptions*)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
  1195. if (!sub) return NULL;
  1196. google_protobuf_DescriptorProto_ExtensionRange_set_options(msg, sub);
  1197. }
  1198. return sub;
  1199. }
  1200. /* google.protobuf.DescriptorProto.ReservedRange */
  1201. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_arena *arena) {
  1202. return (google_protobuf_DescriptorProto_ReservedRange *)upb_msg_new(&google_protobuf_DescriptorProto_ReservedRange_msginit, arena);
  1203. }
  1204. UPB_INLINE google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parse(const char *buf, size_t size,
  1205. upb_arena *arena) {
  1206. google_protobuf_DescriptorProto_ReservedRange *ret = google_protobuf_DescriptorProto_ReservedRange_new(arena);
  1207. return (ret && upb_decode(buf, size, ret, &google_protobuf_DescriptorProto_ReservedRange_msginit)) ? ret : NULL;
  1208. }
  1209. UPB_INLINE char *google_protobuf_DescriptorProto_ReservedRange_serialize(const google_protobuf_DescriptorProto_ReservedRange *msg, upb_arena *arena, size_t *len) {
  1210. return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, arena, len);
  1211. }
  1212. UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_start(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 1); }
  1213. 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)); }
  1214. UPB_INLINE bool google_protobuf_DescriptorProto_ReservedRange_has_end(const google_protobuf_DescriptorProto_ReservedRange *msg) { return _upb_has_field(msg, 2); }
  1215. 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)); }
  1216. UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
  1217. _upb_sethas(msg, 1);
  1218. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1219. }
  1220. UPB_INLINE void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
  1221. _upb_sethas(msg, 2);
  1222. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1223. }
  1224. /* google.protobuf.ExtensionRangeOptions */
  1225. UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_arena *arena) {
  1226. return (google_protobuf_ExtensionRangeOptions *)upb_msg_new(&google_protobuf_ExtensionRangeOptions_msginit, arena);
  1227. }
  1228. UPB_INLINE google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parse(const char *buf, size_t size,
  1229. upb_arena *arena) {
  1230. google_protobuf_ExtensionRangeOptions *ret = google_protobuf_ExtensionRangeOptions_new(arena);
  1231. return (ret && upb_decode(buf, size, ret, &google_protobuf_ExtensionRangeOptions_msginit)) ? ret : NULL;
  1232. }
  1233. UPB_INLINE char *google_protobuf_ExtensionRangeOptions_serialize(const google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena, size_t *len) {
  1234. return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, arena, len);
  1235. }
  1236. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  1237. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_mutable_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t *len) {
  1238. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  1239. }
  1240. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ExtensionRangeOptions_resize_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, size_t len, upb_arena *arena) {
  1241. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1242. }
  1243. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ExtensionRangeOptions_add_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_arena *arena) {
  1244. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1245. bool ok = _upb_array_append_accessor(
  1246. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1247. if (!ok) return NULL;
  1248. return sub;
  1249. }
  1250. /* google.protobuf.FieldDescriptorProto */
  1251. UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_arena *arena) {
  1252. return (google_protobuf_FieldDescriptorProto *)upb_msg_new(&google_protobuf_FieldDescriptorProto_msginit, arena);
  1253. }
  1254. UPB_INLINE google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parse(const char *buf, size_t size,
  1255. upb_arena *arena) {
  1256. google_protobuf_FieldDescriptorProto *ret = google_protobuf_FieldDescriptorProto_new(arena);
  1257. return (ret && upb_decode(buf, size, ret, &google_protobuf_FieldDescriptorProto_msginit)) ? ret : NULL;
  1258. }
  1259. UPB_INLINE char *google_protobuf_FieldDescriptorProto_serialize(const google_protobuf_FieldDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1260. return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, arena, len);
  1261. }
  1262. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 5); }
  1263. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); }
  1264. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_extendee(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 6); }
  1265. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); }
  1266. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_number(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  1267. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)); }
  1268. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_label(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1269. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1270. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1271. UPB_INLINE int32_t google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)); }
  1272. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_type_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 7); }
  1273. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); }
  1274. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_default_value(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 8); }
  1275. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)); }
  1276. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_options(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 10); }
  1277. 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)); }
  1278. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_oneof_index(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 4); }
  1279. 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)); }
  1280. UPB_INLINE bool google_protobuf_FieldDescriptorProto_has_json_name(const google_protobuf_FieldDescriptorProto *msg) { return _upb_has_field(msg, 9); }
  1281. UPB_INLINE upb_strview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)); }
  1282. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1283. _upb_sethas(msg, 5);
  1284. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value;
  1285. }
  1286. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1287. _upb_sethas(msg, 6);
  1288. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value;
  1289. }
  1290. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1291. _upb_sethas(msg, 3);
  1292. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(24, 24)) = value;
  1293. }
  1294. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1295. _upb_sethas(msg, 1);
  1296. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1297. }
  1298. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1299. _upb_sethas(msg, 2);
  1300. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)) = value;
  1301. }
  1302. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1303. _upb_sethas(msg, 7);
  1304. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value;
  1305. }
  1306. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1307. _upb_sethas(msg, 8);
  1308. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(56, 80)) = value;
  1309. }
  1310. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) {
  1311. _upb_sethas(msg, 10);
  1312. UPB_FIELD_AT(msg, google_protobuf_FieldOptions*, UPB_SIZE(72, 112)) = value;
  1313. }
  1314. UPB_INLINE struct google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_mutable_options(google_protobuf_FieldDescriptorProto *msg, upb_arena *arena) {
  1315. struct google_protobuf_FieldOptions* sub = (struct google_protobuf_FieldOptions*)google_protobuf_FieldDescriptorProto_options(msg);
  1316. if (sub == NULL) {
  1317. sub = (struct google_protobuf_FieldOptions*)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
  1318. if (!sub) return NULL;
  1319. google_protobuf_FieldDescriptorProto_set_options(msg, sub);
  1320. }
  1321. return sub;
  1322. }
  1323. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  1324. _upb_sethas(msg, 4);
  1325. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(28, 28)) = value;
  1326. }
  1327. UPB_INLINE void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_strview value) {
  1328. _upb_sethas(msg, 9);
  1329. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(64, 96)) = value;
  1330. }
  1331. /* google.protobuf.OneofDescriptorProto */
  1332. UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_arena *arena) {
  1333. return (google_protobuf_OneofDescriptorProto *)upb_msg_new(&google_protobuf_OneofDescriptorProto_msginit, arena);
  1334. }
  1335. UPB_INLINE google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parse(const char *buf, size_t size,
  1336. upb_arena *arena) {
  1337. google_protobuf_OneofDescriptorProto *ret = google_protobuf_OneofDescriptorProto_new(arena);
  1338. return (ret && upb_decode(buf, size, ret, &google_protobuf_OneofDescriptorProto_msginit)) ? ret : NULL;
  1339. }
  1340. UPB_INLINE char *google_protobuf_OneofDescriptorProto_serialize(const google_protobuf_OneofDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1341. return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, arena, len);
  1342. }
  1343. UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_name(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1344. UPB_INLINE upb_strview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1345. UPB_INLINE bool google_protobuf_OneofDescriptorProto_has_options(const google_protobuf_OneofDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1346. 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(12, 24)); }
  1347. UPB_INLINE void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_strview value) {
  1348. _upb_sethas(msg, 1);
  1349. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1350. }
  1351. UPB_INLINE void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) {
  1352. _upb_sethas(msg, 2);
  1353. UPB_FIELD_AT(msg, google_protobuf_OneofOptions*, UPB_SIZE(12, 24)) = value;
  1354. }
  1355. UPB_INLINE struct google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_mutable_options(google_protobuf_OneofDescriptorProto *msg, upb_arena *arena) {
  1356. struct google_protobuf_OneofOptions* sub = (struct google_protobuf_OneofOptions*)google_protobuf_OneofDescriptorProto_options(msg);
  1357. if (sub == NULL) {
  1358. sub = (struct google_protobuf_OneofOptions*)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
  1359. if (!sub) return NULL;
  1360. google_protobuf_OneofDescriptorProto_set_options(msg, sub);
  1361. }
  1362. return sub;
  1363. }
  1364. /* google.protobuf.EnumDescriptorProto */
  1365. UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_arena *arena) {
  1366. return (google_protobuf_EnumDescriptorProto *)upb_msg_new(&google_protobuf_EnumDescriptorProto_msginit, arena);
  1367. }
  1368. UPB_INLINE google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parse(const char *buf, size_t size,
  1369. upb_arena *arena) {
  1370. google_protobuf_EnumDescriptorProto *ret = google_protobuf_EnumDescriptorProto_new(arena);
  1371. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumDescriptorProto_msginit)) ? ret : NULL;
  1372. }
  1373. UPB_INLINE char *google_protobuf_EnumDescriptorProto_serialize(const google_protobuf_EnumDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1374. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, arena, len);
  1375. }
  1376. UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_name(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1377. UPB_INLINE upb_strview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1378. UPB_INLINE const google_protobuf_EnumValueDescriptorProto* const* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumValueDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
  1379. UPB_INLINE bool google_protobuf_EnumDescriptorProto_has_options(const google_protobuf_EnumDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1380. 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(12, 24)); }
  1381. UPB_INLINE const google_protobuf_EnumDescriptorProto_EnumReservedRange* const* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (const google_protobuf_EnumDescriptorProto_EnumReservedRange* const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
  1382. UPB_INLINE upb_strview const* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
  1383. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_strview value) {
  1384. _upb_sethas(msg, 1);
  1385. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1386. }
  1387. UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_mutable_value(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
  1388. return (google_protobuf_EnumValueDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
  1389. }
  1390. UPB_INLINE google_protobuf_EnumValueDescriptorProto** google_protobuf_EnumDescriptorProto_resize_value(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
  1391. return (google_protobuf_EnumValueDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1392. }
  1393. UPB_INLINE struct google_protobuf_EnumValueDescriptorProto* google_protobuf_EnumDescriptorProto_add_value(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
  1394. struct google_protobuf_EnumValueDescriptorProto* sub = (struct google_protobuf_EnumValueDescriptorProto*)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
  1395. bool ok = _upb_array_append_accessor(
  1396. msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1397. if (!ok) return NULL;
  1398. return sub;
  1399. }
  1400. UPB_INLINE void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) {
  1401. _upb_sethas(msg, 2);
  1402. UPB_FIELD_AT(msg, google_protobuf_EnumOptions*, UPB_SIZE(12, 24)) = value;
  1403. }
  1404. UPB_INLINE struct google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_mutable_options(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
  1405. struct google_protobuf_EnumOptions* sub = (struct google_protobuf_EnumOptions*)google_protobuf_EnumDescriptorProto_options(msg);
  1406. if (sub == NULL) {
  1407. sub = (struct google_protobuf_EnumOptions*)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
  1408. if (!sub) return NULL;
  1409. google_protobuf_EnumDescriptorProto_set_options(msg, sub);
  1410. }
  1411. return sub;
  1412. }
  1413. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_mutable_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
  1414. return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
  1415. }
  1416. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange** google_protobuf_EnumDescriptorProto_resize_reserved_range(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
  1417. return (google_protobuf_EnumDescriptorProto_EnumReservedRange**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1418. }
  1419. UPB_INLINE struct google_protobuf_EnumDescriptorProto_EnumReservedRange* google_protobuf_EnumDescriptorProto_add_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_arena *arena) {
  1420. struct google_protobuf_EnumDescriptorProto_EnumReservedRange* sub = (struct google_protobuf_EnumDescriptorProto_EnumReservedRange*)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
  1421. bool ok = _upb_array_append_accessor(
  1422. msg, UPB_SIZE(20, 40), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1423. if (!ok) return NULL;
  1424. return sub;
  1425. }
  1426. UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_mutable_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t *len) {
  1427. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
  1428. }
  1429. UPB_INLINE upb_strview* google_protobuf_EnumDescriptorProto_resize_reserved_name(google_protobuf_EnumDescriptorProto *msg, size_t len, upb_arena *arena) {
  1430. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  1431. }
  1432. UPB_INLINE bool google_protobuf_EnumDescriptorProto_add_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_strview val, upb_arena *arena) {
  1433. return _upb_array_append_accessor(
  1434. msg, UPB_SIZE(24, 48), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  1435. }
  1436. /* google.protobuf.EnumDescriptorProto.EnumReservedRange */
  1437. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_arena *arena) {
  1438. return (google_protobuf_EnumDescriptorProto_EnumReservedRange *)upb_msg_new(&google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena);
  1439. }
  1440. UPB_INLINE google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parse(const char *buf, size_t size,
  1441. upb_arena *arena) {
  1442. google_protobuf_EnumDescriptorProto_EnumReservedRange *ret = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(arena);
  1443. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit)) ? ret : NULL;
  1444. }
  1445. UPB_INLINE char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_arena *arena, size_t *len) {
  1446. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, arena, len);
  1447. }
  1448. UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 1); }
  1449. 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)); }
  1450. UPB_INLINE bool google_protobuf_EnumDescriptorProto_EnumReservedRange_has_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) { return _upb_has_field(msg, 2); }
  1451. 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)); }
  1452. UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
  1453. _upb_sethas(msg, 1);
  1454. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1455. }
  1456. UPB_INLINE void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
  1457. _upb_sethas(msg, 2);
  1458. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1459. }
  1460. /* google.protobuf.EnumValueDescriptorProto */
  1461. UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_arena *arena) {
  1462. return (google_protobuf_EnumValueDescriptorProto *)upb_msg_new(&google_protobuf_EnumValueDescriptorProto_msginit, arena);
  1463. }
  1464. UPB_INLINE google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parse(const char *buf, size_t size,
  1465. upb_arena *arena) {
  1466. google_protobuf_EnumValueDescriptorProto *ret = google_protobuf_EnumValueDescriptorProto_new(arena);
  1467. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumValueDescriptorProto_msginit)) ? ret : NULL;
  1468. }
  1469. UPB_INLINE char *google_protobuf_EnumValueDescriptorProto_serialize(const google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1470. return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, arena, len);
  1471. }
  1472. UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_name(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1473. UPB_INLINE upb_strview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)); }
  1474. UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_number(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1475. UPB_INLINE int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)); }
  1476. UPB_INLINE bool google_protobuf_EnumValueDescriptorProto_has_options(const google_protobuf_EnumValueDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  1477. 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, 24)); }
  1478. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_strview value) {
  1479. _upb_sethas(msg, 2);
  1480. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(8, 8)) = value;
  1481. }
  1482. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) {
  1483. _upb_sethas(msg, 1);
  1484. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  1485. }
  1486. UPB_INLINE void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) {
  1487. _upb_sethas(msg, 3);
  1488. UPB_FIELD_AT(msg, google_protobuf_EnumValueOptions*, UPB_SIZE(16, 24)) = value;
  1489. }
  1490. UPB_INLINE struct google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_mutable_options(google_protobuf_EnumValueDescriptorProto *msg, upb_arena *arena) {
  1491. struct google_protobuf_EnumValueOptions* sub = (struct google_protobuf_EnumValueOptions*)google_protobuf_EnumValueDescriptorProto_options(msg);
  1492. if (sub == NULL) {
  1493. sub = (struct google_protobuf_EnumValueOptions*)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
  1494. if (!sub) return NULL;
  1495. google_protobuf_EnumValueDescriptorProto_set_options(msg, sub);
  1496. }
  1497. return sub;
  1498. }
  1499. /* google.protobuf.ServiceDescriptorProto */
  1500. UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_arena *arena) {
  1501. return (google_protobuf_ServiceDescriptorProto *)upb_msg_new(&google_protobuf_ServiceDescriptorProto_msginit, arena);
  1502. }
  1503. UPB_INLINE google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parse(const char *buf, size_t size,
  1504. upb_arena *arena) {
  1505. google_protobuf_ServiceDescriptorProto *ret = google_protobuf_ServiceDescriptorProto_new(arena);
  1506. return (ret && upb_decode(buf, size, ret, &google_protobuf_ServiceDescriptorProto_msginit)) ? ret : NULL;
  1507. }
  1508. UPB_INLINE char *google_protobuf_ServiceDescriptorProto_serialize(const google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1509. return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, arena, len);
  1510. }
  1511. UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_name(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1512. UPB_INLINE upb_strview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1513. UPB_INLINE const google_protobuf_MethodDescriptorProto* const* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg, size_t *len) { return (const google_protobuf_MethodDescriptorProto* const*)_upb_array_accessor(msg, UPB_SIZE(16, 32), len); }
  1514. UPB_INLINE bool google_protobuf_ServiceDescriptorProto_has_options(const google_protobuf_ServiceDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1515. 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(12, 24)); }
  1516. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_strview value) {
  1517. _upb_sethas(msg, 1);
  1518. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1519. }
  1520. UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_mutable_method(google_protobuf_ServiceDescriptorProto *msg, size_t *len) {
  1521. return (google_protobuf_MethodDescriptorProto**)_upb_array_mutable_accessor(msg, UPB_SIZE(16, 32), len);
  1522. }
  1523. UPB_INLINE google_protobuf_MethodDescriptorProto** google_protobuf_ServiceDescriptorProto_resize_method(google_protobuf_ServiceDescriptorProto *msg, size_t len, upb_arena *arena) {
  1524. return (google_protobuf_MethodDescriptorProto**)_upb_array_resize_accessor(msg, UPB_SIZE(16, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1525. }
  1526. UPB_INLINE struct google_protobuf_MethodDescriptorProto* google_protobuf_ServiceDescriptorProto_add_method(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) {
  1527. struct google_protobuf_MethodDescriptorProto* sub = (struct google_protobuf_MethodDescriptorProto*)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
  1528. bool ok = _upb_array_append_accessor(
  1529. msg, UPB_SIZE(16, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1530. if (!ok) return NULL;
  1531. return sub;
  1532. }
  1533. UPB_INLINE void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) {
  1534. _upb_sethas(msg, 2);
  1535. UPB_FIELD_AT(msg, google_protobuf_ServiceOptions*, UPB_SIZE(12, 24)) = value;
  1536. }
  1537. UPB_INLINE struct google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_mutable_options(google_protobuf_ServiceDescriptorProto *msg, upb_arena *arena) {
  1538. struct google_protobuf_ServiceOptions* sub = (struct google_protobuf_ServiceOptions*)google_protobuf_ServiceDescriptorProto_options(msg);
  1539. if (sub == NULL) {
  1540. sub = (struct google_protobuf_ServiceOptions*)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
  1541. if (!sub) return NULL;
  1542. google_protobuf_ServiceDescriptorProto_set_options(msg, sub);
  1543. }
  1544. return sub;
  1545. }
  1546. /* google.protobuf.MethodDescriptorProto */
  1547. UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_arena *arena) {
  1548. return (google_protobuf_MethodDescriptorProto *)upb_msg_new(&google_protobuf_MethodDescriptorProto_msginit, arena);
  1549. }
  1550. UPB_INLINE google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parse(const char *buf, size_t size,
  1551. upb_arena *arena) {
  1552. google_protobuf_MethodDescriptorProto *ret = google_protobuf_MethodDescriptorProto_new(arena);
  1553. return (ret && upb_decode(buf, size, ret, &google_protobuf_MethodDescriptorProto_msginit)) ? ret : NULL;
  1554. }
  1555. UPB_INLINE char *google_protobuf_MethodDescriptorProto_serialize(const google_protobuf_MethodDescriptorProto *msg, upb_arena *arena, size_t *len) {
  1556. return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, arena, len);
  1557. }
  1558. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_name(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 3); }
  1559. UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  1560. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_input_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 4); }
  1561. UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
  1562. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_output_type(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 5); }
  1563. UPB_INLINE upb_strview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)); }
  1564. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_options(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 6); }
  1565. 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(28, 56)); }
  1566. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 1); }
  1567. UPB_INLINE bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1568. UPB_INLINE bool google_protobuf_MethodDescriptorProto_has_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return _upb_has_field(msg, 2); }
  1569. UPB_INLINE bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  1570. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
  1571. _upb_sethas(msg, 3);
  1572. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  1573. }
  1574. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
  1575. _upb_sethas(msg, 4);
  1576. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
  1577. }
  1578. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_strview value) {
  1579. _upb_sethas(msg, 5);
  1580. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(20, 40)) = value;
  1581. }
  1582. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) {
  1583. _upb_sethas(msg, 6);
  1584. UPB_FIELD_AT(msg, google_protobuf_MethodOptions*, UPB_SIZE(28, 56)) = value;
  1585. }
  1586. UPB_INLINE struct google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_mutable_options(google_protobuf_MethodDescriptorProto *msg, upb_arena *arena) {
  1587. struct google_protobuf_MethodOptions* sub = (struct google_protobuf_MethodOptions*)google_protobuf_MethodDescriptorProto_options(msg);
  1588. if (sub == NULL) {
  1589. sub = (struct google_protobuf_MethodOptions*)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
  1590. if (!sub) return NULL;
  1591. google_protobuf_MethodDescriptorProto_set_options(msg, sub);
  1592. }
  1593. return sub;
  1594. }
  1595. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
  1596. _upb_sethas(msg, 1);
  1597. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1598. }
  1599. UPB_INLINE void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
  1600. _upb_sethas(msg, 2);
  1601. UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
  1602. }
  1603. /* google.protobuf.FileOptions */
  1604. UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_arena *arena) {
  1605. return (google_protobuf_FileOptions *)upb_msg_new(&google_protobuf_FileOptions_msginit, arena);
  1606. }
  1607. UPB_INLINE google_protobuf_FileOptions *google_protobuf_FileOptions_parse(const char *buf, size_t size,
  1608. upb_arena *arena) {
  1609. google_protobuf_FileOptions *ret = google_protobuf_FileOptions_new(arena);
  1610. return (ret && upb_decode(buf, size, ret, &google_protobuf_FileOptions_msginit)) ? ret : NULL;
  1611. }
  1612. UPB_INLINE char *google_protobuf_FileOptions_serialize(const google_protobuf_FileOptions *msg, upb_arena *arena, size_t *len) {
  1613. return upb_encode(msg, &google_protobuf_FileOptions_msginit, arena, len);
  1614. }
  1615. UPB_INLINE bool google_protobuf_FileOptions_has_java_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 11); }
  1616. UPB_INLINE upb_strview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)); }
  1617. UPB_INLINE bool google_protobuf_FileOptions_has_java_outer_classname(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 12); }
  1618. UPB_INLINE upb_strview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)); }
  1619. UPB_INLINE bool google_protobuf_FileOptions_has_optimize_for(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 1); }
  1620. UPB_INLINE int32_t google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1621. UPB_INLINE bool google_protobuf_FileOptions_has_java_multiple_files(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 2); }
  1622. UPB_INLINE bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
  1623. UPB_INLINE bool google_protobuf_FileOptions_has_go_package(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 13); }
  1624. UPB_INLINE upb_strview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)); }
  1625. UPB_INLINE bool google_protobuf_FileOptions_has_cc_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 3); }
  1626. UPB_INLINE bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)); }
  1627. UPB_INLINE bool google_protobuf_FileOptions_has_java_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 4); }
  1628. UPB_INLINE bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)); }
  1629. UPB_INLINE bool google_protobuf_FileOptions_has_py_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 5); }
  1630. UPB_INLINE bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)); }
  1631. UPB_INLINE bool google_protobuf_FileOptions_has_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 6); }
  1632. 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)); }
  1633. UPB_INLINE bool google_protobuf_FileOptions_has_deprecated(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 7); }
  1634. UPB_INLINE bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)); }
  1635. UPB_INLINE bool google_protobuf_FileOptions_has_java_string_check_utf8(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 8); }
  1636. 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)); }
  1637. UPB_INLINE bool google_protobuf_FileOptions_has_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 9); }
  1638. UPB_INLINE bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)); }
  1639. UPB_INLINE bool google_protobuf_FileOptions_has_objc_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 14); }
  1640. UPB_INLINE upb_strview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)); }
  1641. UPB_INLINE bool google_protobuf_FileOptions_has_csharp_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 15); }
  1642. UPB_INLINE upb_strview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)); }
  1643. UPB_INLINE bool google_protobuf_FileOptions_has_swift_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 16); }
  1644. UPB_INLINE upb_strview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)); }
  1645. UPB_INLINE bool google_protobuf_FileOptions_has_php_class_prefix(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 17); }
  1646. UPB_INLINE upb_strview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)); }
  1647. UPB_INLINE bool google_protobuf_FileOptions_has_php_namespace(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 18); }
  1648. UPB_INLINE upb_strview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)); }
  1649. UPB_INLINE bool google_protobuf_FileOptions_has_php_generic_services(const google_protobuf_FileOptions *msg) { return _upb_has_field(msg, 10); }
  1650. UPB_INLINE bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
  1651. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(92, 160), len); }
  1652. UPB_INLINE void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_strview value) {
  1653. _upb_sethas(msg, 11);
  1654. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(28, 32)) = value;
  1655. }
  1656. UPB_INLINE void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_strview value) {
  1657. _upb_sethas(msg, 12);
  1658. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(36, 48)) = value;
  1659. }
  1660. UPB_INLINE void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, int32_t value) {
  1661. _upb_sethas(msg, 1);
  1662. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1663. }
  1664. UPB_INLINE void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) {
  1665. _upb_sethas(msg, 2);
  1666. UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value;
  1667. }
  1668. UPB_INLINE void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_strview value) {
  1669. _upb_sethas(msg, 13);
  1670. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(44, 64)) = value;
  1671. }
  1672. UPB_INLINE void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1673. _upb_sethas(msg, 3);
  1674. UPB_FIELD_AT(msg, bool, UPB_SIZE(17, 17)) = value;
  1675. }
  1676. UPB_INLINE void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1677. _upb_sethas(msg, 4);
  1678. UPB_FIELD_AT(msg, bool, UPB_SIZE(18, 18)) = value;
  1679. }
  1680. UPB_INLINE void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1681. _upb_sethas(msg, 5);
  1682. UPB_FIELD_AT(msg, bool, UPB_SIZE(19, 19)) = value;
  1683. }
  1684. UPB_INLINE void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) {
  1685. _upb_sethas(msg, 6);
  1686. UPB_FIELD_AT(msg, bool, UPB_SIZE(20, 20)) = value;
  1687. }
  1688. UPB_INLINE void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) {
  1689. _upb_sethas(msg, 7);
  1690. UPB_FIELD_AT(msg, bool, UPB_SIZE(21, 21)) = value;
  1691. }
  1692. UPB_INLINE void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) {
  1693. _upb_sethas(msg, 8);
  1694. UPB_FIELD_AT(msg, bool, UPB_SIZE(22, 22)) = value;
  1695. }
  1696. UPB_INLINE void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) {
  1697. _upb_sethas(msg, 9);
  1698. UPB_FIELD_AT(msg, bool, UPB_SIZE(23, 23)) = value;
  1699. }
  1700. UPB_INLINE void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
  1701. _upb_sethas(msg, 14);
  1702. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(52, 80)) = value;
  1703. }
  1704. UPB_INLINE void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_strview value) {
  1705. _upb_sethas(msg, 15);
  1706. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(60, 96)) = value;
  1707. }
  1708. UPB_INLINE void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
  1709. _upb_sethas(msg, 16);
  1710. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(68, 112)) = value;
  1711. }
  1712. UPB_INLINE void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_strview value) {
  1713. _upb_sethas(msg, 17);
  1714. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(76, 128)) = value;
  1715. }
  1716. UPB_INLINE void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_strview value) {
  1717. _upb_sethas(msg, 18);
  1718. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(84, 144)) = value;
  1719. }
  1720. UPB_INLINE void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1721. _upb_sethas(msg, 10);
  1722. UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value;
  1723. }
  1724. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_mutable_uninterpreted_option(google_protobuf_FileOptions *msg, size_t *len) {
  1725. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(92, 160), len);
  1726. }
  1727. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FileOptions_resize_uninterpreted_option(google_protobuf_FileOptions *msg, size_t len, upb_arena *arena) {
  1728. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(92, 160), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1729. }
  1730. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FileOptions_add_uninterpreted_option(google_protobuf_FileOptions *msg, upb_arena *arena) {
  1731. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1732. bool ok = _upb_array_append_accessor(
  1733. msg, UPB_SIZE(92, 160), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1734. if (!ok) return NULL;
  1735. return sub;
  1736. }
  1737. /* google.protobuf.MessageOptions */
  1738. UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_arena *arena) {
  1739. return (google_protobuf_MessageOptions *)upb_msg_new(&google_protobuf_MessageOptions_msginit, arena);
  1740. }
  1741. UPB_INLINE google_protobuf_MessageOptions *google_protobuf_MessageOptions_parse(const char *buf, size_t size,
  1742. upb_arena *arena) {
  1743. google_protobuf_MessageOptions *ret = google_protobuf_MessageOptions_new(arena);
  1744. return (ret && upb_decode(buf, size, ret, &google_protobuf_MessageOptions_msginit)) ? ret : NULL;
  1745. }
  1746. UPB_INLINE char *google_protobuf_MessageOptions_serialize(const google_protobuf_MessageOptions *msg, upb_arena *arena, size_t *len) {
  1747. return upb_encode(msg, &google_protobuf_MessageOptions_msginit, arena, len);
  1748. }
  1749. UPB_INLINE bool google_protobuf_MessageOptions_has_message_set_wire_format(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 1); }
  1750. 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)); }
  1751. UPB_INLINE bool google_protobuf_MessageOptions_has_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 2); }
  1752. 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)); }
  1753. UPB_INLINE bool google_protobuf_MessageOptions_has_deprecated(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 3); }
  1754. UPB_INLINE bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)); }
  1755. UPB_INLINE bool google_protobuf_MessageOptions_has_map_entry(const google_protobuf_MessageOptions *msg) { return _upb_has_field(msg, 4); }
  1756. UPB_INLINE bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)); }
  1757. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(8, 8), len); }
  1758. UPB_INLINE void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) {
  1759. _upb_sethas(msg, 1);
  1760. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1761. }
  1762. UPB_INLINE void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) {
  1763. _upb_sethas(msg, 2);
  1764. UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
  1765. }
  1766. UPB_INLINE void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) {
  1767. _upb_sethas(msg, 3);
  1768. UPB_FIELD_AT(msg, bool, UPB_SIZE(3, 3)) = value;
  1769. }
  1770. UPB_INLINE void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) {
  1771. _upb_sethas(msg, 4);
  1772. UPB_FIELD_AT(msg, bool, UPB_SIZE(4, 4)) = value;
  1773. }
  1774. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_mutable_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t *len) {
  1775. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(8, 8), len);
  1776. }
  1777. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MessageOptions_resize_uninterpreted_option(google_protobuf_MessageOptions *msg, size_t len, upb_arena *arena) {
  1778. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(8, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1779. }
  1780. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MessageOptions_add_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_arena *arena) {
  1781. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1782. bool ok = _upb_array_append_accessor(
  1783. msg, UPB_SIZE(8, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1784. if (!ok) return NULL;
  1785. return sub;
  1786. }
  1787. /* google.protobuf.FieldOptions */
  1788. UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_arena *arena) {
  1789. return (google_protobuf_FieldOptions *)upb_msg_new(&google_protobuf_FieldOptions_msginit, arena);
  1790. }
  1791. UPB_INLINE google_protobuf_FieldOptions *google_protobuf_FieldOptions_parse(const char *buf, size_t size,
  1792. upb_arena *arena) {
  1793. google_protobuf_FieldOptions *ret = google_protobuf_FieldOptions_new(arena);
  1794. return (ret && upb_decode(buf, size, ret, &google_protobuf_FieldOptions_msginit)) ? ret : NULL;
  1795. }
  1796. UPB_INLINE char *google_protobuf_FieldOptions_serialize(const google_protobuf_FieldOptions *msg, upb_arena *arena, size_t *len) {
  1797. return upb_encode(msg, &google_protobuf_FieldOptions_msginit, arena, len);
  1798. }
  1799. UPB_INLINE bool google_protobuf_FieldOptions_has_ctype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 1); }
  1800. UPB_INLINE int32_t google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1801. UPB_INLINE bool google_protobuf_FieldOptions_has_packed(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 3); }
  1802. UPB_INLINE bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)); }
  1803. UPB_INLINE bool google_protobuf_FieldOptions_has_deprecated(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 4); }
  1804. UPB_INLINE bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)); }
  1805. UPB_INLINE bool google_protobuf_FieldOptions_has_lazy(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 5); }
  1806. UPB_INLINE bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)); }
  1807. UPB_INLINE bool google_protobuf_FieldOptions_has_jstype(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 2); }
  1808. UPB_INLINE int32_t google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)); }
  1809. UPB_INLINE bool google_protobuf_FieldOptions_has_weak(const google_protobuf_FieldOptions *msg) { return _upb_has_field(msg, 6); }
  1810. UPB_INLINE bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)); }
  1811. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(28, 32), len); }
  1812. UPB_INLINE void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, int32_t value) {
  1813. _upb_sethas(msg, 1);
  1814. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  1815. }
  1816. UPB_INLINE void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) {
  1817. _upb_sethas(msg, 3);
  1818. UPB_FIELD_AT(msg, bool, UPB_SIZE(24, 24)) = value;
  1819. }
  1820. UPB_INLINE void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) {
  1821. _upb_sethas(msg, 4);
  1822. UPB_FIELD_AT(msg, bool, UPB_SIZE(25, 25)) = value;
  1823. }
  1824. UPB_INLINE void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) {
  1825. _upb_sethas(msg, 5);
  1826. UPB_FIELD_AT(msg, bool, UPB_SIZE(26, 26)) = value;
  1827. }
  1828. UPB_INLINE void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, int32_t value) {
  1829. _upb_sethas(msg, 2);
  1830. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(16, 16)) = value;
  1831. }
  1832. UPB_INLINE void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) {
  1833. _upb_sethas(msg, 6);
  1834. UPB_FIELD_AT(msg, bool, UPB_SIZE(27, 27)) = value;
  1835. }
  1836. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_mutable_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t *len) {
  1837. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 32), len);
  1838. }
  1839. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_FieldOptions_resize_uninterpreted_option(google_protobuf_FieldOptions *msg, size_t len, upb_arena *arena) {
  1840. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(28, 32), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1841. }
  1842. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_FieldOptions_add_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_arena *arena) {
  1843. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1844. bool ok = _upb_array_append_accessor(
  1845. msg, UPB_SIZE(28, 32), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1846. if (!ok) return NULL;
  1847. return sub;
  1848. }
  1849. /* google.protobuf.OneofOptions */
  1850. UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_arena *arena) {
  1851. return (google_protobuf_OneofOptions *)upb_msg_new(&google_protobuf_OneofOptions_msginit, arena);
  1852. }
  1853. UPB_INLINE google_protobuf_OneofOptions *google_protobuf_OneofOptions_parse(const char *buf, size_t size,
  1854. upb_arena *arena) {
  1855. google_protobuf_OneofOptions *ret = google_protobuf_OneofOptions_new(arena);
  1856. return (ret && upb_decode(buf, size, ret, &google_protobuf_OneofOptions_msginit)) ? ret : NULL;
  1857. }
  1858. UPB_INLINE char *google_protobuf_OneofOptions_serialize(const google_protobuf_OneofOptions *msg, upb_arena *arena, size_t *len) {
  1859. return upb_encode(msg, &google_protobuf_OneofOptions_msginit, arena, len);
  1860. }
  1861. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  1862. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_mutable_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t *len) {
  1863. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  1864. }
  1865. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_OneofOptions_resize_uninterpreted_option(google_protobuf_OneofOptions *msg, size_t len, upb_arena *arena) {
  1866. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1867. }
  1868. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_OneofOptions_add_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_arena *arena) {
  1869. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1870. bool ok = _upb_array_append_accessor(
  1871. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1872. if (!ok) return NULL;
  1873. return sub;
  1874. }
  1875. /* google.protobuf.EnumOptions */
  1876. UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_arena *arena) {
  1877. return (google_protobuf_EnumOptions *)upb_msg_new(&google_protobuf_EnumOptions_msginit, arena);
  1878. }
  1879. UPB_INLINE google_protobuf_EnumOptions *google_protobuf_EnumOptions_parse(const char *buf, size_t size,
  1880. upb_arena *arena) {
  1881. google_protobuf_EnumOptions *ret = google_protobuf_EnumOptions_new(arena);
  1882. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumOptions_msginit)) ? ret : NULL;
  1883. }
  1884. UPB_INLINE char *google_protobuf_EnumOptions_serialize(const google_protobuf_EnumOptions *msg, upb_arena *arena, size_t *len) {
  1885. return upb_encode(msg, &google_protobuf_EnumOptions_msginit, arena, len);
  1886. }
  1887. UPB_INLINE bool google_protobuf_EnumOptions_has_allow_alias(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 1); }
  1888. UPB_INLINE bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1889. UPB_INLINE bool google_protobuf_EnumOptions_has_deprecated(const google_protobuf_EnumOptions *msg) { return _upb_has_field(msg, 2); }
  1890. UPB_INLINE bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)); }
  1891. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
  1892. UPB_INLINE void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) {
  1893. _upb_sethas(msg, 1);
  1894. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1895. }
  1896. UPB_INLINE void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) {
  1897. _upb_sethas(msg, 2);
  1898. UPB_FIELD_AT(msg, bool, UPB_SIZE(2, 2)) = value;
  1899. }
  1900. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_mutable_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t *len) {
  1901. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
  1902. }
  1903. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumOptions_resize_uninterpreted_option(google_protobuf_EnumOptions *msg, size_t len, upb_arena *arena) {
  1904. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1905. }
  1906. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumOptions_add_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_arena *arena) {
  1907. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1908. bool ok = _upb_array_append_accessor(
  1909. msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1910. if (!ok) return NULL;
  1911. return sub;
  1912. }
  1913. /* google.protobuf.EnumValueOptions */
  1914. UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_arena *arena) {
  1915. return (google_protobuf_EnumValueOptions *)upb_msg_new(&google_protobuf_EnumValueOptions_msginit, arena);
  1916. }
  1917. UPB_INLINE google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parse(const char *buf, size_t size,
  1918. upb_arena *arena) {
  1919. google_protobuf_EnumValueOptions *ret = google_protobuf_EnumValueOptions_new(arena);
  1920. return (ret && upb_decode(buf, size, ret, &google_protobuf_EnumValueOptions_msginit)) ? ret : NULL;
  1921. }
  1922. UPB_INLINE char *google_protobuf_EnumValueOptions_serialize(const google_protobuf_EnumValueOptions *msg, upb_arena *arena, size_t *len) {
  1923. return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, arena, len);
  1924. }
  1925. UPB_INLINE bool google_protobuf_EnumValueOptions_has_deprecated(const google_protobuf_EnumValueOptions *msg) { return _upb_has_field(msg, 1); }
  1926. UPB_INLINE bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1927. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
  1928. UPB_INLINE void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) {
  1929. _upb_sethas(msg, 1);
  1930. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1931. }
  1932. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_mutable_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t *len) {
  1933. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
  1934. }
  1935. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_EnumValueOptions_resize_uninterpreted_option(google_protobuf_EnumValueOptions *msg, size_t len, upb_arena *arena) {
  1936. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1937. }
  1938. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_EnumValueOptions_add_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_arena *arena) {
  1939. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1940. bool ok = _upb_array_append_accessor(
  1941. msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1942. if (!ok) return NULL;
  1943. return sub;
  1944. }
  1945. /* google.protobuf.ServiceOptions */
  1946. UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_arena *arena) {
  1947. return (google_protobuf_ServiceOptions *)upb_msg_new(&google_protobuf_ServiceOptions_msginit, arena);
  1948. }
  1949. UPB_INLINE google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parse(const char *buf, size_t size,
  1950. upb_arena *arena) {
  1951. google_protobuf_ServiceOptions *ret = google_protobuf_ServiceOptions_new(arena);
  1952. return (ret && upb_decode(buf, size, ret, &google_protobuf_ServiceOptions_msginit)) ? ret : NULL;
  1953. }
  1954. UPB_INLINE char *google_protobuf_ServiceOptions_serialize(const google_protobuf_ServiceOptions *msg, upb_arena *arena, size_t *len) {
  1955. return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, arena, len);
  1956. }
  1957. UPB_INLINE bool google_protobuf_ServiceOptions_has_deprecated(const google_protobuf_ServiceOptions *msg) { return _upb_has_field(msg, 1); }
  1958. UPB_INLINE bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)); }
  1959. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(4, 8), len); }
  1960. UPB_INLINE void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) {
  1961. _upb_sethas(msg, 1);
  1962. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  1963. }
  1964. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_mutable_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t *len) {
  1965. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(4, 8), len);
  1966. }
  1967. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_ServiceOptions_resize_uninterpreted_option(google_protobuf_ServiceOptions *msg, size_t len, upb_arena *arena) {
  1968. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(4, 8), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  1969. }
  1970. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_ServiceOptions_add_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_arena *arena) {
  1971. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  1972. bool ok = _upb_array_append_accessor(
  1973. msg, UPB_SIZE(4, 8), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  1974. if (!ok) return NULL;
  1975. return sub;
  1976. }
  1977. /* google.protobuf.MethodOptions */
  1978. UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_arena *arena) {
  1979. return (google_protobuf_MethodOptions *)upb_msg_new(&google_protobuf_MethodOptions_msginit, arena);
  1980. }
  1981. UPB_INLINE google_protobuf_MethodOptions *google_protobuf_MethodOptions_parse(const char *buf, size_t size,
  1982. upb_arena *arena) {
  1983. google_protobuf_MethodOptions *ret = google_protobuf_MethodOptions_new(arena);
  1984. return (ret && upb_decode(buf, size, ret, &google_protobuf_MethodOptions_msginit)) ? ret : NULL;
  1985. }
  1986. UPB_INLINE char *google_protobuf_MethodOptions_serialize(const google_protobuf_MethodOptions *msg, upb_arena *arena, size_t *len) {
  1987. return upb_encode(msg, &google_protobuf_MethodOptions_msginit, arena, len);
  1988. }
  1989. UPB_INLINE bool google_protobuf_MethodOptions_has_deprecated(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 2); }
  1990. UPB_INLINE bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)); }
  1991. UPB_INLINE bool google_protobuf_MethodOptions_has_idempotency_level(const google_protobuf_MethodOptions *msg) { return _upb_has_field(msg, 1); }
  1992. UPB_INLINE int32_t google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) { return UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)); }
  1993. UPB_INLINE const google_protobuf_UninterpretedOption* const* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg, size_t *len) { return (const google_protobuf_UninterpretedOption* const*)_upb_array_accessor(msg, UPB_SIZE(20, 24), len); }
  1994. UPB_INLINE void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) {
  1995. _upb_sethas(msg, 2);
  1996. UPB_FIELD_AT(msg, bool, UPB_SIZE(16, 16)) = value;
  1997. }
  1998. UPB_INLINE void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, int32_t value) {
  1999. _upb_sethas(msg, 1);
  2000. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  2001. }
  2002. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_mutable_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t *len) {
  2003. return (google_protobuf_UninterpretedOption**)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 24), len);
  2004. }
  2005. UPB_INLINE google_protobuf_UninterpretedOption** google_protobuf_MethodOptions_resize_uninterpreted_option(google_protobuf_MethodOptions *msg, size_t len, upb_arena *arena) {
  2006. return (google_protobuf_UninterpretedOption**)_upb_array_resize_accessor(msg, UPB_SIZE(20, 24), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2007. }
  2008. UPB_INLINE struct google_protobuf_UninterpretedOption* google_protobuf_MethodOptions_add_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_arena *arena) {
  2009. struct google_protobuf_UninterpretedOption* sub = (struct google_protobuf_UninterpretedOption*)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  2010. bool ok = _upb_array_append_accessor(
  2011. msg, UPB_SIZE(20, 24), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2012. if (!ok) return NULL;
  2013. return sub;
  2014. }
  2015. /* google.protobuf.UninterpretedOption */
  2016. UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_arena *arena) {
  2017. return (google_protobuf_UninterpretedOption *)upb_msg_new(&google_protobuf_UninterpretedOption_msginit, arena);
  2018. }
  2019. UPB_INLINE google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parse(const char *buf, size_t size,
  2020. upb_arena *arena) {
  2021. google_protobuf_UninterpretedOption *ret = google_protobuf_UninterpretedOption_new(arena);
  2022. return (ret && upb_decode(buf, size, ret, &google_protobuf_UninterpretedOption_msginit)) ? ret : NULL;
  2023. }
  2024. UPB_INLINE char *google_protobuf_UninterpretedOption_serialize(const google_protobuf_UninterpretedOption *msg, upb_arena *arena, size_t *len) {
  2025. return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, arena, len);
  2026. }
  2027. UPB_INLINE const google_protobuf_UninterpretedOption_NamePart* const* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg, size_t *len) { return (const google_protobuf_UninterpretedOption_NamePart* const*)_upb_array_accessor(msg, UPB_SIZE(56, 80), len); }
  2028. UPB_INLINE bool google_protobuf_UninterpretedOption_has_identifier_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 4); }
  2029. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)); }
  2030. UPB_INLINE bool google_protobuf_UninterpretedOption_has_positive_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 1); }
  2031. 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)); }
  2032. UPB_INLINE bool google_protobuf_UninterpretedOption_has_negative_int_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 2); }
  2033. 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)); }
  2034. UPB_INLINE bool google_protobuf_UninterpretedOption_has_double_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 3); }
  2035. UPB_INLINE double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)); }
  2036. UPB_INLINE bool google_protobuf_UninterpretedOption_has_string_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 5); }
  2037. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)); }
  2038. UPB_INLINE bool google_protobuf_UninterpretedOption_has_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return _upb_has_field(msg, 6); }
  2039. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)); }
  2040. UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_mutable_name(google_protobuf_UninterpretedOption *msg, size_t *len) {
  2041. return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_mutable_accessor(msg, UPB_SIZE(56, 80), len);
  2042. }
  2043. UPB_INLINE google_protobuf_UninterpretedOption_NamePart** google_protobuf_UninterpretedOption_resize_name(google_protobuf_UninterpretedOption *msg, size_t len, upb_arena *arena) {
  2044. return (google_protobuf_UninterpretedOption_NamePart**)_upb_array_resize_accessor(msg, UPB_SIZE(56, 80), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2045. }
  2046. UPB_INLINE struct google_protobuf_UninterpretedOption_NamePart* google_protobuf_UninterpretedOption_add_name(google_protobuf_UninterpretedOption *msg, upb_arena *arena) {
  2047. struct google_protobuf_UninterpretedOption_NamePart* sub = (struct google_protobuf_UninterpretedOption_NamePart*)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
  2048. bool ok = _upb_array_append_accessor(
  2049. msg, UPB_SIZE(56, 80), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2050. if (!ok) return NULL;
  2051. return sub;
  2052. }
  2053. UPB_INLINE void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
  2054. _upb_sethas(msg, 4);
  2055. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(32, 32)) = value;
  2056. }
  2057. UPB_INLINE void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) {
  2058. _upb_sethas(msg, 1);
  2059. UPB_FIELD_AT(msg, uint64_t, UPB_SIZE(8, 8)) = value;
  2060. }
  2061. UPB_INLINE void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) {
  2062. _upb_sethas(msg, 2);
  2063. UPB_FIELD_AT(msg, int64_t, UPB_SIZE(16, 16)) = value;
  2064. }
  2065. UPB_INLINE void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) {
  2066. _upb_sethas(msg, 3);
  2067. UPB_FIELD_AT(msg, double, UPB_SIZE(24, 24)) = value;
  2068. }
  2069. UPB_INLINE void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
  2070. _upb_sethas(msg, 5);
  2071. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(40, 48)) = value;
  2072. }
  2073. UPB_INLINE void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_strview value) {
  2074. _upb_sethas(msg, 6);
  2075. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(48, 64)) = value;
  2076. }
  2077. /* google.protobuf.UninterpretedOption.NamePart */
  2078. UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_arena *arena) {
  2079. return (google_protobuf_UninterpretedOption_NamePart *)upb_msg_new(&google_protobuf_UninterpretedOption_NamePart_msginit, arena);
  2080. }
  2081. UPB_INLINE google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parse(const char *buf, size_t size,
  2082. upb_arena *arena) {
  2083. google_protobuf_UninterpretedOption_NamePart *ret = google_protobuf_UninterpretedOption_NamePart_new(arena);
  2084. return (ret && upb_decode(buf, size, ret, &google_protobuf_UninterpretedOption_NamePart_msginit)) ? ret : NULL;
  2085. }
  2086. UPB_INLINE char *google_protobuf_UninterpretedOption_NamePart_serialize(const google_protobuf_UninterpretedOption_NamePart *msg, upb_arena *arena, size_t *len) {
  2087. return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, arena, len);
  2088. }
  2089. UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 2); }
  2090. UPB_INLINE upb_strview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  2091. UPB_INLINE bool google_protobuf_UninterpretedOption_NamePart_has_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) { return _upb_has_field(msg, 1); }
  2092. 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)); }
  2093. UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_strview value) {
  2094. _upb_sethas(msg, 2);
  2095. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  2096. }
  2097. UPB_INLINE void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) {
  2098. _upb_sethas(msg, 1);
  2099. UPB_FIELD_AT(msg, bool, UPB_SIZE(1, 1)) = value;
  2100. }
  2101. /* google.protobuf.SourceCodeInfo */
  2102. UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_arena *arena) {
  2103. return (google_protobuf_SourceCodeInfo *)upb_msg_new(&google_protobuf_SourceCodeInfo_msginit, arena);
  2104. }
  2105. UPB_INLINE google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parse(const char *buf, size_t size,
  2106. upb_arena *arena) {
  2107. google_protobuf_SourceCodeInfo *ret = google_protobuf_SourceCodeInfo_new(arena);
  2108. return (ret && upb_decode(buf, size, ret, &google_protobuf_SourceCodeInfo_msginit)) ? ret : NULL;
  2109. }
  2110. UPB_INLINE char *google_protobuf_SourceCodeInfo_serialize(const google_protobuf_SourceCodeInfo *msg, upb_arena *arena, size_t *len) {
  2111. return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, arena, len);
  2112. }
  2113. UPB_INLINE const google_protobuf_SourceCodeInfo_Location* const* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg, size_t *len) { return (const google_protobuf_SourceCodeInfo_Location* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  2114. UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_mutable_location(google_protobuf_SourceCodeInfo *msg, size_t *len) {
  2115. return (google_protobuf_SourceCodeInfo_Location**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  2116. }
  2117. UPB_INLINE google_protobuf_SourceCodeInfo_Location** google_protobuf_SourceCodeInfo_resize_location(google_protobuf_SourceCodeInfo *msg, size_t len, upb_arena *arena) {
  2118. return (google_protobuf_SourceCodeInfo_Location**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2119. }
  2120. UPB_INLINE struct google_protobuf_SourceCodeInfo_Location* google_protobuf_SourceCodeInfo_add_location(google_protobuf_SourceCodeInfo *msg, upb_arena *arena) {
  2121. struct google_protobuf_SourceCodeInfo_Location* sub = (struct google_protobuf_SourceCodeInfo_Location*)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
  2122. bool ok = _upb_array_append_accessor(
  2123. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2124. if (!ok) return NULL;
  2125. return sub;
  2126. }
  2127. /* google.protobuf.SourceCodeInfo.Location */
  2128. UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_arena *arena) {
  2129. return (google_protobuf_SourceCodeInfo_Location *)upb_msg_new(&google_protobuf_SourceCodeInfo_Location_msginit, arena);
  2130. }
  2131. UPB_INLINE google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parse(const char *buf, size_t size,
  2132. upb_arena *arena) {
  2133. google_protobuf_SourceCodeInfo_Location *ret = google_protobuf_SourceCodeInfo_Location_new(arena);
  2134. return (ret && upb_decode(buf, size, ret, &google_protobuf_SourceCodeInfo_Location_msginit)) ? ret : NULL;
  2135. }
  2136. UPB_INLINE char *google_protobuf_SourceCodeInfo_Location_serialize(const google_protobuf_SourceCodeInfo_Location *msg, upb_arena *arena, size_t *len) {
  2137. return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, arena, len);
  2138. }
  2139. UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 40), len); }
  2140. UPB_INLINE int32_t const* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(24, 48), len); }
  2141. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 1); }
  2142. UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)); }
  2143. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_has_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return _upb_has_field(msg, 2); }
  2144. UPB_INLINE upb_strview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)); }
  2145. UPB_INLINE upb_strview const* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg, size_t *len) { return (upb_strview const*)_upb_array_accessor(msg, UPB_SIZE(28, 56), len); }
  2146. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_path(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
  2147. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 40), len);
  2148. }
  2149. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_path(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
  2150. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 40), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  2151. }
  2152. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_path(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) {
  2153. return _upb_array_append_accessor(
  2154. msg, UPB_SIZE(20, 40), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  2155. }
  2156. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_mutable_span(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
  2157. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(24, 48), len);
  2158. }
  2159. UPB_INLINE int32_t* google_protobuf_SourceCodeInfo_Location_resize_span(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
  2160. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(24, 48), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  2161. }
  2162. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_span(google_protobuf_SourceCodeInfo_Location *msg, int32_t val, upb_arena *arena) {
  2163. return _upb_array_append_accessor(
  2164. msg, UPB_SIZE(24, 48), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  2165. }
  2166. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) {
  2167. _upb_sethas(msg, 1);
  2168. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(4, 8)) = value;
  2169. }
  2170. UPB_INLINE void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview value) {
  2171. _upb_sethas(msg, 2);
  2172. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 24)) = value;
  2173. }
  2174. UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_mutable_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t *len) {
  2175. return (upb_strview*)_upb_array_mutable_accessor(msg, UPB_SIZE(28, 56), len);
  2176. }
  2177. UPB_INLINE upb_strview* google_protobuf_SourceCodeInfo_Location_resize_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, size_t len, upb_arena *arena) {
  2178. return (upb_strview*)_upb_array_resize_accessor(msg, UPB_SIZE(28, 56), len, UPB_SIZE(8, 16), UPB_TYPE_STRING, arena);
  2179. }
  2180. UPB_INLINE bool google_protobuf_SourceCodeInfo_Location_add_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_strview val, upb_arena *arena) {
  2181. return _upb_array_append_accessor(
  2182. msg, UPB_SIZE(28, 56), UPB_SIZE(8, 16), UPB_TYPE_STRING, &val, arena);
  2183. }
  2184. /* google.protobuf.GeneratedCodeInfo */
  2185. UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_arena *arena) {
  2186. return (google_protobuf_GeneratedCodeInfo *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_msginit, arena);
  2187. }
  2188. UPB_INLINE google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parse(const char *buf, size_t size,
  2189. upb_arena *arena) {
  2190. google_protobuf_GeneratedCodeInfo *ret = google_protobuf_GeneratedCodeInfo_new(arena);
  2191. return (ret && upb_decode(buf, size, ret, &google_protobuf_GeneratedCodeInfo_msginit)) ? ret : NULL;
  2192. }
  2193. UPB_INLINE char *google_protobuf_GeneratedCodeInfo_serialize(const google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena, size_t *len) {
  2194. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, arena, len);
  2195. }
  2196. UPB_INLINE const google_protobuf_GeneratedCodeInfo_Annotation* const* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg, size_t *len) { return (const google_protobuf_GeneratedCodeInfo_Annotation* const*)_upb_array_accessor(msg, UPB_SIZE(0, 0), len); }
  2197. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_mutable_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t *len) {
  2198. return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_mutable_accessor(msg, UPB_SIZE(0, 0), len);
  2199. }
  2200. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation** google_protobuf_GeneratedCodeInfo_resize_annotation(google_protobuf_GeneratedCodeInfo *msg, size_t len, upb_arena *arena) {
  2201. return (google_protobuf_GeneratedCodeInfo_Annotation**)_upb_array_resize_accessor(msg, UPB_SIZE(0, 0), len, UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, arena);
  2202. }
  2203. UPB_INLINE struct google_protobuf_GeneratedCodeInfo_Annotation* google_protobuf_GeneratedCodeInfo_add_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_arena *arena) {
  2204. struct google_protobuf_GeneratedCodeInfo_Annotation* sub = (struct google_protobuf_GeneratedCodeInfo_Annotation*)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
  2205. bool ok = _upb_array_append_accessor(
  2206. msg, UPB_SIZE(0, 0), UPB_SIZE(4, 8), UPB_TYPE_MESSAGE, &sub, arena);
  2207. if (!ok) return NULL;
  2208. return sub;
  2209. }
  2210. /* google.protobuf.GeneratedCodeInfo.Annotation */
  2211. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_arena *arena) {
  2212. return (google_protobuf_GeneratedCodeInfo_Annotation *)upb_msg_new(&google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena);
  2213. }
  2214. UPB_INLINE google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parse(const char *buf, size_t size,
  2215. upb_arena *arena) {
  2216. google_protobuf_GeneratedCodeInfo_Annotation *ret = google_protobuf_GeneratedCodeInfo_Annotation_new(arena);
  2217. return (ret && upb_decode(buf, size, ret, &google_protobuf_GeneratedCodeInfo_Annotation_msginit)) ? ret : NULL;
  2218. }
  2219. UPB_INLINE char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(const google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_arena *arena, size_t *len) {
  2220. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, arena, len);
  2221. }
  2222. UPB_INLINE int32_t const* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) { return (int32_t const*)_upb_array_accessor(msg, UPB_SIZE(20, 32), len); }
  2223. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 3); }
  2224. UPB_INLINE upb_strview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)); }
  2225. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 1); }
  2226. 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)); }
  2227. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_has_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) { return _upb_has_field(msg, 2); }
  2228. 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)); }
  2229. UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_mutable_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t *len) {
  2230. return (int32_t*)_upb_array_mutable_accessor(msg, UPB_SIZE(20, 32), len);
  2231. }
  2232. UPB_INLINE int32_t* google_protobuf_GeneratedCodeInfo_Annotation_resize_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, size_t len, upb_arena *arena) {
  2233. return (int32_t*)_upb_array_resize_accessor(msg, UPB_SIZE(20, 32), len, UPB_SIZE(4, 4), UPB_TYPE_INT32, arena);
  2234. }
  2235. UPB_INLINE bool google_protobuf_GeneratedCodeInfo_Annotation_add_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t val, upb_arena *arena) {
  2236. return _upb_array_append_accessor(
  2237. msg, UPB_SIZE(20, 32), UPB_SIZE(4, 4), UPB_TYPE_INT32, &val, arena);
  2238. }
  2239. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_strview value) {
  2240. _upb_sethas(msg, 3);
  2241. UPB_FIELD_AT(msg, upb_strview, UPB_SIZE(12, 16)) = value;
  2242. }
  2243. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
  2244. _upb_sethas(msg, 1);
  2245. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(4, 4)) = value;
  2246. }
  2247. UPB_INLINE void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
  2248. _upb_sethas(msg, 2);
  2249. UPB_FIELD_AT(msg, int32_t, UPB_SIZE(8, 8)) = value;
  2250. }
  2251. #ifdef __cplusplus
  2252. } /* extern "C" */
  2253. #endif
  2254. #endif /* GOOGLE_PROTOBUF_DESCRIPTOR_PROTO_UPB_H_ */
  2255. /*
  2256. ** Defs are upb's internal representation of the constructs that can appear
  2257. ** in a .proto file:
  2258. **
  2259. ** - upb::MessageDefPtr (upb_msgdef): describes a "message" construct.
  2260. ** - upb::FieldDefPtr (upb_fielddef): describes a message field.
  2261. ** - upb::FileDefPtr (upb_filedef): describes a .proto file and its defs.
  2262. ** - upb::EnumDefPtr (upb_enumdef): describes an enum.
  2263. ** - upb::OneofDefPtr (upb_oneofdef): describes a oneof.
  2264. **
  2265. ** TODO: definitions of services.
  2266. **
  2267. ** This is a mixed C/C++ interface that offers a full API to both languages.
  2268. ** See the top-level README for more information.
  2269. */
  2270. #ifndef UPB_DEF_H_
  2271. #define UPB_DEF_H_
  2272. /*
  2273. ** upb_table
  2274. **
  2275. ** This header is INTERNAL-ONLY! Its interfaces are not public or stable!
  2276. ** This file defines very fast int->upb_value (inttable) and string->upb_value
  2277. ** (strtable) hash tables.
  2278. **
  2279. ** The table uses chained scatter with Brent's variation (inspired by the Lua
  2280. ** implementation of hash tables). The hash function for strings is Austin
  2281. ** Appleby's "MurmurHash."
  2282. **
  2283. ** The inttable uses uintptr_t as its key, which guarantees it can be used to
  2284. ** store pointers or integers of at least 32 bits (upb isn't really useful on
  2285. ** systems where sizeof(void*) < 4).
  2286. **
  2287. ** The table must be homogenous (all values of the same type). In debug
  2288. ** mode, we check this on insert and lookup.
  2289. */
  2290. #ifndef UPB_TABLE_H_
  2291. #define UPB_TABLE_H_
  2292. #include <stdint.h>
  2293. #include <string.h>
  2294. #ifdef __cplusplus
  2295. extern "C" {
  2296. #endif
  2297. /* upb_value ******************************************************************/
  2298. /* A tagged union (stored untagged inside the table) so that we can check that
  2299. * clients calling table accessors are correctly typed without having to have
  2300. * an explosion of accessors. */
  2301. typedef enum {
  2302. UPB_CTYPE_INT32 = 1,
  2303. UPB_CTYPE_INT64 = 2,
  2304. UPB_CTYPE_UINT32 = 3,
  2305. UPB_CTYPE_UINT64 = 4,
  2306. UPB_CTYPE_BOOL = 5,
  2307. UPB_CTYPE_CSTR = 6,
  2308. UPB_CTYPE_PTR = 7,
  2309. UPB_CTYPE_CONSTPTR = 8,
  2310. UPB_CTYPE_FPTR = 9,
  2311. UPB_CTYPE_FLOAT = 10,
  2312. UPB_CTYPE_DOUBLE = 11
  2313. } upb_ctype_t;
  2314. typedef struct {
  2315. uint64_t val;
  2316. #ifndef NDEBUG
  2317. /* In debug mode we carry the value type around also so we can check accesses
  2318. * to be sure the right member is being read. */
  2319. upb_ctype_t ctype;
  2320. #endif
  2321. } upb_value;
  2322. #ifdef NDEBUG
  2323. #define SET_TYPE(dest, val) UPB_UNUSED(val)
  2324. #else
  2325. #define SET_TYPE(dest, val) dest = val
  2326. #endif
  2327. /* Like strdup(), which isn't always available since it's not ANSI C. */
  2328. char *upb_strdup(const char *s, upb_alloc *a);
  2329. /* Variant that works with a length-delimited rather than NULL-delimited string,
  2330. * as supported by strtable. */
  2331. char *upb_strdup2(const char *s, size_t len, upb_alloc *a);
  2332. UPB_INLINE char *upb_gstrdup(const char *s) {
  2333. return upb_strdup(s, &upb_alloc_global);
  2334. }
  2335. UPB_INLINE void _upb_value_setval(upb_value *v, uint64_t val,
  2336. upb_ctype_t ctype) {
  2337. v->val = val;
  2338. SET_TYPE(v->ctype, ctype);
  2339. }
  2340. UPB_INLINE upb_value _upb_value_val(uint64_t val, upb_ctype_t ctype) {
  2341. upb_value ret;
  2342. _upb_value_setval(&ret, val, ctype);
  2343. return ret;
  2344. }
  2345. /* For each value ctype, define the following set of functions:
  2346. *
  2347. * // Get/set an int32 from a upb_value.
  2348. * int32_t upb_value_getint32(upb_value val);
  2349. * void upb_value_setint32(upb_value *val, int32_t cval);
  2350. *
  2351. * // Construct a new upb_value from an int32.
  2352. * upb_value upb_value_int32(int32_t val); */
  2353. #define FUNCS(name, membername, type_t, converter, proto_type) \
  2354. UPB_INLINE void upb_value_set ## name(upb_value *val, type_t cval) { \
  2355. val->val = (converter)cval; \
  2356. SET_TYPE(val->ctype, proto_type); \
  2357. } \
  2358. UPB_INLINE upb_value upb_value_ ## name(type_t val) { \
  2359. upb_value ret; \
  2360. upb_value_set ## name(&ret, val); \
  2361. return ret; \
  2362. } \
  2363. UPB_INLINE type_t upb_value_get ## name(upb_value val) { \
  2364. UPB_ASSERT_DEBUGVAR(val.ctype == proto_type); \
  2365. return (type_t)(converter)val.val; \
  2366. }
  2367. FUNCS(int32, int32, int32_t, int32_t, UPB_CTYPE_INT32)
  2368. FUNCS(int64, int64, int64_t, int64_t, UPB_CTYPE_INT64)
  2369. FUNCS(uint32, uint32, uint32_t, uint32_t, UPB_CTYPE_UINT32)
  2370. FUNCS(uint64, uint64, uint64_t, uint64_t, UPB_CTYPE_UINT64)
  2371. FUNCS(bool, _bool, bool, bool, UPB_CTYPE_BOOL)
  2372. FUNCS(cstr, cstr, char*, uintptr_t, UPB_CTYPE_CSTR)
  2373. FUNCS(ptr, ptr, void*, uintptr_t, UPB_CTYPE_PTR)
  2374. FUNCS(constptr, constptr, const void*, uintptr_t, UPB_CTYPE_CONSTPTR)
  2375. FUNCS(fptr, fptr, upb_func*, uintptr_t, UPB_CTYPE_FPTR)
  2376. #undef FUNCS
  2377. UPB_INLINE void upb_value_setfloat(upb_value *val, float cval) {
  2378. memcpy(&val->val, &cval, sizeof(cval));
  2379. SET_TYPE(val->ctype, UPB_CTYPE_FLOAT);
  2380. }
  2381. UPB_INLINE void upb_value_setdouble(upb_value *val, double cval) {
  2382. memcpy(&val->val, &cval, sizeof(cval));
  2383. SET_TYPE(val->ctype, UPB_CTYPE_DOUBLE);
  2384. }
  2385. UPB_INLINE upb_value upb_value_float(float cval) {
  2386. upb_value ret;
  2387. upb_value_setfloat(&ret, cval);
  2388. return ret;
  2389. }
  2390. UPB_INLINE upb_value upb_value_double(double cval) {
  2391. upb_value ret;
  2392. upb_value_setdouble(&ret, cval);
  2393. return ret;
  2394. }
  2395. #undef SET_TYPE
  2396. /* upb_tabkey *****************************************************************/
  2397. /* Either:
  2398. * 1. an actual integer key, or
  2399. * 2. a pointer to a string prefixed by its uint32_t length, owned by us.
  2400. *
  2401. * ...depending on whether this is a string table or an int table. We would
  2402. * make this a union of those two types, but C89 doesn't support statically
  2403. * initializing a non-first union member. */
  2404. typedef uintptr_t upb_tabkey;
  2405. UPB_INLINE char *upb_tabstr(upb_tabkey key, uint32_t *len) {
  2406. char* mem = (char*)key;
  2407. if (len) memcpy(len, mem, sizeof(*len));
  2408. return mem + sizeof(*len);
  2409. }
  2410. /* upb_tabval *****************************************************************/
  2411. typedef struct {
  2412. uint64_t val;
  2413. } upb_tabval;
  2414. #define UPB_TABVALUE_EMPTY_INIT {-1}
  2415. /* upb_table ******************************************************************/
  2416. typedef struct _upb_tabent {
  2417. upb_tabkey key;
  2418. upb_tabval val;
  2419. /* Internal chaining. This is const so we can create static initializers for
  2420. * tables. We cast away const sometimes, but *only* when the containing
  2421. * upb_table is known to be non-const. This requires a bit of care, but
  2422. * the subtlety is confined to table.c. */
  2423. const struct _upb_tabent *next;
  2424. } upb_tabent;
  2425. typedef struct {
  2426. size_t count; /* Number of entries in the hash part. */
  2427. size_t mask; /* Mask to turn hash value -> bucket. */
  2428. upb_ctype_t ctype; /* Type of all values. */
  2429. uint8_t size_lg2; /* Size of the hashtable part is 2^size_lg2 entries. */
  2430. /* Hash table entries.
  2431. * Making this const isn't entirely accurate; what we really want is for it to
  2432. * have the same const-ness as the table it's inside. But there's no way to
  2433. * declare that in C. So we have to make it const so that we can statically
  2434. * initialize const hash tables. Then we cast away const when we have to.
  2435. */
  2436. const upb_tabent *entries;
  2437. #ifndef NDEBUG
  2438. /* This table's allocator. We make the user pass it in to every relevant
  2439. * function and only use this to check it in debug mode. We do this solely
  2440. * to keep upb_table as small as possible. This might seem slightly paranoid
  2441. * but the plan is to use upb_table for all map fields and extension sets in
  2442. * a forthcoming message representation, so there could be a lot of these.
  2443. * If this turns out to be too annoying later, we can change it (since this
  2444. * is an internal-only header file). */
  2445. upb_alloc *alloc;
  2446. #endif
  2447. } upb_table;
  2448. typedef struct {
  2449. upb_table t;
  2450. } upb_strtable;
  2451. typedef struct {
  2452. upb_table t; /* For entries that don't fit in the array part. */
  2453. const upb_tabval *array; /* Array part of the table. See const note above. */
  2454. size_t array_size; /* Array part size. */
  2455. size_t array_count; /* Array part number of elements. */
  2456. } upb_inttable;
  2457. #define UPB_INTTABLE_INIT(count, mask, ctype, size_lg2, ent, a, asize, acount) \
  2458. {UPB_TABLE_INIT(count, mask, ctype, size_lg2, ent), a, asize, acount}
  2459. #define UPB_EMPTY_INTTABLE_INIT(ctype) \
  2460. UPB_INTTABLE_INIT(0, 0, ctype, 0, NULL, NULL, 0, 0)
  2461. #define UPB_ARRAY_EMPTYENT -1
  2462. UPB_INLINE size_t upb_table_size(const upb_table *t) {
  2463. if (t->size_lg2 == 0)
  2464. return 0;
  2465. else
  2466. return 1 << t->size_lg2;
  2467. }
  2468. /* Internal-only functions, in .h file only out of necessity. */
  2469. UPB_INLINE bool upb_tabent_isempty(const upb_tabent *e) {
  2470. return e->key == 0;
  2471. }
  2472. /* Used by some of the unit tests for generic hashing functionality. */
  2473. uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed);
  2474. UPB_INLINE uintptr_t upb_intkey(uintptr_t key) {
  2475. return key;
  2476. }
  2477. UPB_INLINE uint32_t upb_inthash(uintptr_t key) {
  2478. return (uint32_t)key;
  2479. }
  2480. static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
  2481. return t->entries + (hash & t->mask);
  2482. }
  2483. UPB_INLINE bool upb_arrhas(upb_tabval key) {
  2484. return key.val != (uint64_t)-1;
  2485. }
  2486. /* Initialize and uninitialize a table, respectively. If memory allocation
  2487. * failed, false is returned that the table is uninitialized. */
  2488. bool upb_inttable_init2(upb_inttable *table, upb_ctype_t ctype, upb_alloc *a);
  2489. bool upb_strtable_init2(upb_strtable *table, upb_ctype_t ctype, upb_alloc *a);
  2490. void upb_inttable_uninit2(upb_inttable *table, upb_alloc *a);
  2491. void upb_strtable_uninit2(upb_strtable *table, upb_alloc *a);
  2492. UPB_INLINE bool upb_inttable_init(upb_inttable *table, upb_ctype_t ctype) {
  2493. return upb_inttable_init2(table, ctype, &upb_alloc_global);
  2494. }
  2495. UPB_INLINE bool upb_strtable_init(upb_strtable *table, upb_ctype_t ctype) {
  2496. return upb_strtable_init2(table, ctype, &upb_alloc_global);
  2497. }
  2498. UPB_INLINE void upb_inttable_uninit(upb_inttable *table) {
  2499. upb_inttable_uninit2(table, &upb_alloc_global);
  2500. }
  2501. UPB_INLINE void upb_strtable_uninit(upb_strtable *table) {
  2502. upb_strtable_uninit2(table, &upb_alloc_global);
  2503. }
  2504. /* Returns the number of values in the table. */
  2505. size_t upb_inttable_count(const upb_inttable *t);
  2506. UPB_INLINE size_t upb_strtable_count(const upb_strtable *t) {
  2507. return t->t.count;
  2508. }
  2509. void upb_inttable_packedsize(const upb_inttable *t, size_t *size);
  2510. void upb_strtable_packedsize(const upb_strtable *t, size_t *size);
  2511. upb_inttable *upb_inttable_pack(const upb_inttable *t, void *p, size_t *ofs,
  2512. size_t size);
  2513. upb_strtable *upb_strtable_pack(const upb_strtable *t, void *p, size_t *ofs,
  2514. size_t size);
  2515. /* Inserts the given key into the hashtable with the given value. The key must
  2516. * not already exist in the hash table. For string tables, the key must be
  2517. * NULL-terminated, and the table will make an internal copy of the key.
  2518. * Inttables must not insert a value of UINTPTR_MAX.
  2519. *
  2520. * If a table resize was required but memory allocation failed, false is
  2521. * returned and the table is unchanged. */
  2522. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  2523. upb_alloc *a);
  2524. bool upb_strtable_insert3(upb_strtable *t, const char *key, size_t len,
  2525. upb_value val, upb_alloc *a);
  2526. UPB_INLINE bool upb_inttable_insert(upb_inttable *t, uintptr_t key,
  2527. upb_value val) {
  2528. return upb_inttable_insert2(t, key, val, &upb_alloc_global);
  2529. }
  2530. UPB_INLINE bool upb_strtable_insert2(upb_strtable *t, const char *key,
  2531. size_t len, upb_value val) {
  2532. return upb_strtable_insert3(t, key, len, val, &upb_alloc_global);
  2533. }
  2534. /* For NULL-terminated strings. */
  2535. UPB_INLINE bool upb_strtable_insert(upb_strtable *t, const char *key,
  2536. upb_value val) {
  2537. return upb_strtable_insert2(t, key, strlen(key), val);
  2538. }
  2539. /* Looks up key in this table, returning "true" if the key was found.
  2540. * If v is non-NULL, copies the value for this key into *v. */
  2541. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v);
  2542. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  2543. upb_value *v);
  2544. /* For NULL-terminated strings. */
  2545. UPB_INLINE bool upb_strtable_lookup(const upb_strtable *t, const char *key,
  2546. upb_value *v) {
  2547. return upb_strtable_lookup2(t, key, strlen(key), v);
  2548. }
  2549. /* Removes an item from the table. Returns true if the remove was successful,
  2550. * and stores the removed item in *val if non-NULL. */
  2551. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val);
  2552. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  2553. upb_value *val, upb_alloc *alloc);
  2554. UPB_INLINE bool upb_strtable_remove2(upb_strtable *t, const char *key,
  2555. size_t len, upb_value *val) {
  2556. return upb_strtable_remove3(t, key, len, val, &upb_alloc_global);
  2557. }
  2558. /* For NULL-terminated strings. */
  2559. UPB_INLINE bool upb_strtable_remove(upb_strtable *t, const char *key,
  2560. upb_value *v) {
  2561. return upb_strtable_remove2(t, key, strlen(key), v);
  2562. }
  2563. /* Updates an existing entry in an inttable. If the entry does not exist,
  2564. * returns false and does nothing. Unlike insert/remove, this does not
  2565. * invalidate iterators. */
  2566. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val);
  2567. /* Handy routines for treating an inttable like a stack. May not be mixed with
  2568. * other insert/remove calls. */
  2569. bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a);
  2570. upb_value upb_inttable_pop(upb_inttable *t);
  2571. UPB_INLINE bool upb_inttable_push(upb_inttable *t, upb_value val) {
  2572. return upb_inttable_push2(t, val, &upb_alloc_global);
  2573. }
  2574. /* Convenience routines for inttables with pointer keys. */
  2575. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  2576. upb_alloc *a);
  2577. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val);
  2578. bool upb_inttable_lookupptr(
  2579. const upb_inttable *t, const void *key, upb_value *val);
  2580. UPB_INLINE bool upb_inttable_insertptr(upb_inttable *t, const void *key,
  2581. upb_value val) {
  2582. return upb_inttable_insertptr2(t, key, val, &upb_alloc_global);
  2583. }
  2584. /* Optimizes the table for the current set of entries, for both memory use and
  2585. * lookup time. Client should call this after all entries have been inserted;
  2586. * inserting more entries is legal, but will likely require a table resize. */
  2587. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a);
  2588. UPB_INLINE void upb_inttable_compact(upb_inttable *t) {
  2589. upb_inttable_compact2(t, &upb_alloc_global);
  2590. }
  2591. /* A special-case inlinable version of the lookup routine for 32-bit
  2592. * integers. */
  2593. UPB_INLINE bool upb_inttable_lookup32(const upb_inttable *t, uint32_t key,
  2594. upb_value *v) {
  2595. *v = upb_value_int32(0); /* Silence compiler warnings. */
  2596. if (key < t->array_size) {
  2597. upb_tabval arrval = t->array[key];
  2598. if (upb_arrhas(arrval)) {
  2599. _upb_value_setval(v, arrval.val, t->t.ctype);
  2600. return true;
  2601. } else {
  2602. return false;
  2603. }
  2604. } else {
  2605. const upb_tabent *e;
  2606. if (t->t.entries == NULL) return false;
  2607. for (e = upb_getentry(&t->t, upb_inthash(key)); true; e = e->next) {
  2608. if ((uint32_t)e->key == key) {
  2609. _upb_value_setval(v, e->val.val, t->t.ctype);
  2610. return true;
  2611. }
  2612. if (e->next == NULL) return false;
  2613. }
  2614. }
  2615. }
  2616. /* Exposed for testing only. */
  2617. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a);
  2618. /* Iterators ******************************************************************/
  2619. /* Iterators for int and string tables. We are subject to some kind of unusual
  2620. * design constraints:
  2621. *
  2622. * For high-level languages:
  2623. * - we must be able to guarantee that we don't crash or corrupt memory even if
  2624. * the program accesses an invalidated iterator.
  2625. *
  2626. * For C++11 range-based for:
  2627. * - iterators must be copyable
  2628. * - iterators must be comparable
  2629. * - it must be possible to construct an "end" value.
  2630. *
  2631. * Iteration order is undefined.
  2632. *
  2633. * Modifying the table invalidates iterators. upb_{str,int}table_done() is
  2634. * guaranteed to work even on an invalidated iterator, as long as the table it
  2635. * is iterating over has not been freed. Calling next() or accessing data from
  2636. * an invalidated iterator yields unspecified elements from the table, but it is
  2637. * guaranteed not to crash and to return real table elements (except when done()
  2638. * is true). */
  2639. /* upb_strtable_iter **********************************************************/
  2640. /* upb_strtable_iter i;
  2641. * upb_strtable_begin(&i, t);
  2642. * for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  2643. * const char *key = upb_strtable_iter_key(&i);
  2644. * const upb_value val = upb_strtable_iter_value(&i);
  2645. * // ...
  2646. * }
  2647. */
  2648. typedef struct {
  2649. const upb_strtable *t;
  2650. size_t index;
  2651. } upb_strtable_iter;
  2652. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t);
  2653. void upb_strtable_next(upb_strtable_iter *i);
  2654. bool upb_strtable_done(const upb_strtable_iter *i);
  2655. const char *upb_strtable_iter_key(const upb_strtable_iter *i);
  2656. size_t upb_strtable_iter_keylength(const upb_strtable_iter *i);
  2657. upb_value upb_strtable_iter_value(const upb_strtable_iter *i);
  2658. void upb_strtable_iter_setdone(upb_strtable_iter *i);
  2659. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  2660. const upb_strtable_iter *i2);
  2661. /* upb_inttable_iter **********************************************************/
  2662. /* upb_inttable_iter i;
  2663. * upb_inttable_begin(&i, t);
  2664. * for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  2665. * uintptr_t key = upb_inttable_iter_key(&i);
  2666. * upb_value val = upb_inttable_iter_value(&i);
  2667. * // ...
  2668. * }
  2669. */
  2670. typedef struct {
  2671. const upb_inttable *t;
  2672. size_t index;
  2673. bool array_part;
  2674. } upb_inttable_iter;
  2675. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t);
  2676. void upb_inttable_next(upb_inttable_iter *i);
  2677. bool upb_inttable_done(const upb_inttable_iter *i);
  2678. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i);
  2679. upb_value upb_inttable_iter_value(const upb_inttable_iter *i);
  2680. void upb_inttable_iter_setdone(upb_inttable_iter *i);
  2681. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  2682. const upb_inttable_iter *i2);
  2683. #ifdef __cplusplus
  2684. } /* extern "C" */
  2685. #endif
  2686. #endif /* UPB_TABLE_H_ */
  2687. #ifdef __cplusplus
  2688. #include <cstring>
  2689. #include <memory>
  2690. #include <string>
  2691. #include <vector>
  2692. namespace upb {
  2693. class EnumDefPtr;
  2694. class FieldDefPtr;
  2695. class FileDefPtr;
  2696. class MessageDefPtr;
  2697. class OneofDefPtr;
  2698. class SymbolTable;
  2699. }
  2700. #endif
  2701. struct upb_enumdef;
  2702. typedef struct upb_enumdef upb_enumdef;
  2703. struct upb_fielddef;
  2704. typedef struct upb_fielddef upb_fielddef;
  2705. struct upb_filedef;
  2706. typedef struct upb_filedef upb_filedef;
  2707. struct upb_msgdef;
  2708. typedef struct upb_msgdef upb_msgdef;
  2709. struct upb_oneofdef;
  2710. typedef struct upb_oneofdef upb_oneofdef;
  2711. struct upb_symtab;
  2712. typedef struct upb_symtab upb_symtab;
  2713. typedef enum {
  2714. UPB_SYNTAX_PROTO2 = 2,
  2715. UPB_SYNTAX_PROTO3 = 3
  2716. } upb_syntax_t;
  2717. /* All the different kind of well known type messages. For simplicity of check,
  2718. * number wrappers and string wrappers are grouped together. Make sure the
  2719. * order and merber of these groups are not changed.
  2720. */
  2721. typedef enum {
  2722. UPB_WELLKNOWN_UNSPECIFIED,
  2723. UPB_WELLKNOWN_ANY,
  2724. UPB_WELLKNOWN_FIELDMASK,
  2725. UPB_WELLKNOWN_DURATION,
  2726. UPB_WELLKNOWN_TIMESTAMP,
  2727. /* number wrappers */
  2728. UPB_WELLKNOWN_DOUBLEVALUE,
  2729. UPB_WELLKNOWN_FLOATVALUE,
  2730. UPB_WELLKNOWN_INT64VALUE,
  2731. UPB_WELLKNOWN_UINT64VALUE,
  2732. UPB_WELLKNOWN_INT32VALUE,
  2733. UPB_WELLKNOWN_UINT32VALUE,
  2734. /* string wrappers */
  2735. UPB_WELLKNOWN_STRINGVALUE,
  2736. UPB_WELLKNOWN_BYTESVALUE,
  2737. UPB_WELLKNOWN_BOOLVALUE,
  2738. UPB_WELLKNOWN_VALUE,
  2739. UPB_WELLKNOWN_LISTVALUE,
  2740. UPB_WELLKNOWN_STRUCT
  2741. } upb_wellknowntype_t;
  2742. /* upb_fielddef ***************************************************************/
  2743. /* Maximum field number allowed for FieldDefs. This is an inherent limit of the
  2744. * protobuf wire format. */
  2745. #define UPB_MAX_FIELDNUMBER ((1 << 29) - 1)
  2746. #ifdef __cplusplus
  2747. extern "C" {
  2748. #endif
  2749. const char *upb_fielddef_fullname(const upb_fielddef *f);
  2750. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f);
  2751. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f);
  2752. upb_label_t upb_fielddef_label(const upb_fielddef *f);
  2753. uint32_t upb_fielddef_number(const upb_fielddef *f);
  2754. const char *upb_fielddef_name(const upb_fielddef *f);
  2755. bool upb_fielddef_isextension(const upb_fielddef *f);
  2756. bool upb_fielddef_lazy(const upb_fielddef *f);
  2757. bool upb_fielddef_packed(const upb_fielddef *f);
  2758. size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len);
  2759. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f);
  2760. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f);
  2761. uint32_t upb_fielddef_index(const upb_fielddef *f);
  2762. bool upb_fielddef_issubmsg(const upb_fielddef *f);
  2763. bool upb_fielddef_isstring(const upb_fielddef *f);
  2764. bool upb_fielddef_isseq(const upb_fielddef *f);
  2765. bool upb_fielddef_isprimitive(const upb_fielddef *f);
  2766. bool upb_fielddef_ismap(const upb_fielddef *f);
  2767. int64_t upb_fielddef_defaultint64(const upb_fielddef *f);
  2768. int32_t upb_fielddef_defaultint32(const upb_fielddef *f);
  2769. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f);
  2770. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f);
  2771. bool upb_fielddef_defaultbool(const upb_fielddef *f);
  2772. float upb_fielddef_defaultfloat(const upb_fielddef *f);
  2773. double upb_fielddef_defaultdouble(const upb_fielddef *f);
  2774. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len);
  2775. bool upb_fielddef_hassubdef(const upb_fielddef *f);
  2776. bool upb_fielddef_haspresence(const upb_fielddef *f);
  2777. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f);
  2778. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f);
  2779. /* Internal only. */
  2780. uint32_t upb_fielddef_selectorbase(const upb_fielddef *f);
  2781. #ifdef __cplusplus
  2782. } /* extern "C" */
  2783. /* A upb_fielddef describes a single field in a message. It is most often
  2784. * found as a part of a upb_msgdef, but can also stand alone to represent
  2785. * an extension. */
  2786. class upb::FieldDefPtr {
  2787. public:
  2788. FieldDefPtr() : ptr_(nullptr) {}
  2789. explicit FieldDefPtr(const upb_fielddef *ptr) : ptr_(ptr) {}
  2790. const upb_fielddef* ptr() const { return ptr_; }
  2791. explicit operator bool() const { return ptr_ != nullptr; }
  2792. typedef upb_fieldtype_t Type;
  2793. typedef upb_label_t Label;
  2794. typedef upb_descriptortype_t DescriptorType;
  2795. const char* full_name() const { return upb_fielddef_fullname(ptr_); }
  2796. Type type() const { return upb_fielddef_type(ptr_); }
  2797. Label label() const { return upb_fielddef_label(ptr_); }
  2798. const char* name() const { return upb_fielddef_name(ptr_); }
  2799. uint32_t number() const { return upb_fielddef_number(ptr_); }
  2800. bool is_extension() const { return upb_fielddef_isextension(ptr_); }
  2801. /* Copies the JSON name for this field into the given buffer. Returns the
  2802. * actual size of the JSON name, including the NULL terminator. If the
  2803. * return value is 0, the JSON name is unset. If the return value is
  2804. * greater than len, the JSON name was truncated. The buffer is always
  2805. * NULL-terminated if len > 0.
  2806. *
  2807. * The JSON name always defaults to a camelCased version of the regular
  2808. * name. However if the regular name is unset, the JSON name will be unset
  2809. * also.
  2810. */
  2811. size_t GetJsonName(char *buf, size_t len) const {
  2812. return upb_fielddef_getjsonname(ptr_, buf, len);
  2813. }
  2814. /* Convenience version of the above function which copies the JSON name
  2815. * into the given string, returning false if the name is not set. */
  2816. template <class T>
  2817. bool GetJsonName(T* str) {
  2818. str->resize(GetJsonName(NULL, 0));
  2819. GetJsonName(&(*str)[0], str->size());
  2820. return str->size() > 0;
  2821. }
  2822. /* For UPB_TYPE_MESSAGE fields only where is_tag_delimited() == false,
  2823. * indicates whether this field should have lazy parsing handlers that yield
  2824. * the unparsed string for the submessage.
  2825. *
  2826. * TODO(haberman): I think we want to move this into a FieldOptions container
  2827. * when we add support for custom options (the FieldOptions struct will
  2828. * contain both regular FieldOptions like "lazy" *and* custom options). */
  2829. bool lazy() const { return upb_fielddef_lazy(ptr_); }
  2830. /* For non-string, non-submessage fields, this indicates whether binary
  2831. * protobufs are encoded in packed or non-packed format.
  2832. *
  2833. * TODO(haberman): see note above about putting options like this into a
  2834. * FieldOptions container. */
  2835. bool packed() const { return upb_fielddef_packed(ptr_); }
  2836. /* An integer that can be used as an index into an array of fields for
  2837. * whatever message this field belongs to. Guaranteed to be less than
  2838. * f->containing_type()->field_count(). May only be accessed once the def has
  2839. * been finalized. */
  2840. uint32_t index() const { return upb_fielddef_index(ptr_); }
  2841. /* The MessageDef to which this field belongs.
  2842. *
  2843. * If this field has been added to a MessageDef, that message can be retrieved
  2844. * directly (this is always the case for frozen FieldDefs).
  2845. *
  2846. * If the field has not yet been added to a MessageDef, you can set the name
  2847. * of the containing type symbolically instead. This is mostly useful for
  2848. * extensions, where the extension is declared separately from the message. */
  2849. MessageDefPtr containing_type() const;
  2850. /* The OneofDef to which this field belongs, or NULL if this field is not part
  2851. * of a oneof. */
  2852. OneofDefPtr containing_oneof() const;
  2853. /* The field's type according to the enum in descriptor.proto. This is not
  2854. * the same as UPB_TYPE_*, because it distinguishes between (for example)
  2855. * INT32 and SINT32, whereas our "type" enum does not. This return of
  2856. * descriptor_type() is a function of type(), integer_format(), and
  2857. * is_tag_delimited(). */
  2858. DescriptorType descriptor_type() const {
  2859. return upb_fielddef_descriptortype(ptr_);
  2860. }
  2861. /* Convenient field type tests. */
  2862. bool IsSubMessage() const { return upb_fielddef_issubmsg(ptr_); }
  2863. bool IsString() const { return upb_fielddef_isstring(ptr_); }
  2864. bool IsSequence() const { return upb_fielddef_isseq(ptr_); }
  2865. bool IsPrimitive() const { return upb_fielddef_isprimitive(ptr_); }
  2866. bool IsMap() const { return upb_fielddef_ismap(ptr_); }
  2867. /* Returns the non-string default value for this fielddef, which may either
  2868. * be something the client set explicitly or the "default default" (0 for
  2869. * numbers, empty for strings). The field's type indicates the type of the
  2870. * returned value, except for enum fields that are still mutable.
  2871. *
  2872. * Requires that the given function matches the field's current type. */
  2873. int64_t default_int64() const { return upb_fielddef_defaultint64(ptr_); }
  2874. int32_t default_int32() const { return upb_fielddef_defaultint32(ptr_); }
  2875. uint64_t default_uint64() const { return upb_fielddef_defaultuint64(ptr_); }
  2876. uint32_t default_uint32() const { return upb_fielddef_defaultuint32(ptr_); }
  2877. bool default_bool() const { return upb_fielddef_defaultbool(ptr_); }
  2878. float default_float() const { return upb_fielddef_defaultfloat(ptr_); }
  2879. double default_double() const { return upb_fielddef_defaultdouble(ptr_); }
  2880. /* The resulting string is always NULL-terminated. If non-NULL, the length
  2881. * will be stored in *len. */
  2882. const char *default_string(size_t * len) const {
  2883. return upb_fielddef_defaultstr(ptr_, len);
  2884. }
  2885. /* Returns the enum or submessage def for this field, if any. The field's
  2886. * type must match (ie. you may only call enum_subdef() for fields where
  2887. * type() == UPB_TYPE_ENUM). */
  2888. EnumDefPtr enum_subdef() const;
  2889. MessageDefPtr message_subdef() const;
  2890. private:
  2891. const upb_fielddef *ptr_;
  2892. };
  2893. #endif /* __cplusplus */
  2894. /* upb_oneofdef ***************************************************************/
  2895. #ifdef __cplusplus
  2896. extern "C" {
  2897. #endif
  2898. typedef upb_inttable_iter upb_oneof_iter;
  2899. const char *upb_oneofdef_name(const upb_oneofdef *o);
  2900. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o);
  2901. int upb_oneofdef_numfields(const upb_oneofdef *o);
  2902. uint32_t upb_oneofdef_index(const upb_oneofdef *o);
  2903. /* Oneof lookups:
  2904. * - ntof: look up a field by name.
  2905. * - ntofz: look up a field by name (as a null-terminated string).
  2906. * - itof: look up a field by number. */
  2907. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  2908. const char *name, size_t length);
  2909. UPB_INLINE const upb_fielddef *upb_oneofdef_ntofz(const upb_oneofdef *o,
  2910. const char *name) {
  2911. return upb_oneofdef_ntof(o, name, strlen(name));
  2912. }
  2913. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num);
  2914. /* upb_oneof_iter i;
  2915. * for(upb_oneof_begin(&i, e); !upb_oneof_done(&i); upb_oneof_next(&i)) {
  2916. * // ...
  2917. * }
  2918. */
  2919. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o);
  2920. void upb_oneof_next(upb_oneof_iter *iter);
  2921. bool upb_oneof_done(upb_oneof_iter *iter);
  2922. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter);
  2923. void upb_oneof_iter_setdone(upb_oneof_iter *iter);
  2924. bool upb_oneof_iter_isequal(const upb_oneof_iter *iter1,
  2925. const upb_oneof_iter *iter2);
  2926. #ifdef __cplusplus
  2927. } /* extern "C" */
  2928. /* Class that represents a oneof. */
  2929. class upb::OneofDefPtr {
  2930. public:
  2931. OneofDefPtr() : ptr_(nullptr) {}
  2932. explicit OneofDefPtr(const upb_oneofdef *ptr) : ptr_(ptr) {}
  2933. const upb_oneofdef* ptr() const { return ptr_; }
  2934. explicit operator bool() { return ptr_ != nullptr; }
  2935. /* Returns the MessageDef that owns this OneofDef. */
  2936. MessageDefPtr containing_type() const;
  2937. /* Returns the name of this oneof. This is the name used to look up the oneof
  2938. * by name once added to a message def. */
  2939. const char* name() const { return upb_oneofdef_name(ptr_); }
  2940. /* Returns the number of fields currently defined in the oneof. */
  2941. int field_count() const { return upb_oneofdef_numfields(ptr_); }
  2942. /* Looks up by name. */
  2943. FieldDefPtr FindFieldByName(const char *name, size_t len) const {
  2944. return FieldDefPtr(upb_oneofdef_ntof(ptr_, name, len));
  2945. }
  2946. FieldDefPtr FindFieldByName(const char* name) const {
  2947. return FieldDefPtr(upb_oneofdef_ntofz(ptr_, name));
  2948. }
  2949. template <class T>
  2950. FieldDefPtr FindFieldByName(const T& str) const {
  2951. return FindFieldByName(str.c_str(), str.size());
  2952. }
  2953. /* Looks up by tag number. */
  2954. FieldDefPtr FindFieldByNumber(uint32_t num) const {
  2955. return FieldDefPtr(upb_oneofdef_itof(ptr_, num));
  2956. }
  2957. class const_iterator
  2958. : public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
  2959. public:
  2960. void operator++() { upb_oneof_next(&iter_); }
  2961. FieldDefPtr operator*() const {
  2962. return FieldDefPtr(upb_oneof_iter_field(&iter_));
  2963. }
  2964. bool operator!=(const const_iterator& other) const {
  2965. return !upb_oneof_iter_isequal(&iter_, &other.iter_);
  2966. }
  2967. bool operator==(const const_iterator& other) const {
  2968. return upb_oneof_iter_isequal(&iter_, &other.iter_);
  2969. }
  2970. private:
  2971. friend class OneofDefPtr;
  2972. const_iterator() {}
  2973. explicit const_iterator(OneofDefPtr o) {
  2974. upb_oneof_begin(&iter_, o.ptr());
  2975. }
  2976. static const_iterator end() {
  2977. const_iterator iter;
  2978. upb_oneof_iter_setdone(&iter.iter_);
  2979. return iter;
  2980. }
  2981. upb_oneof_iter iter_;
  2982. };
  2983. const_iterator begin() const { return const_iterator(*this); }
  2984. const_iterator end() const { return const_iterator::end(); }
  2985. private:
  2986. const upb_oneofdef *ptr_;
  2987. };
  2988. inline upb::OneofDefPtr upb::FieldDefPtr::containing_oneof() const {
  2989. return OneofDefPtr(upb_fielddef_containingoneof(ptr_));
  2990. }
  2991. #endif /* __cplusplus */
  2992. /* upb_msgdef *****************************************************************/
  2993. typedef upb_inttable_iter upb_msg_field_iter;
  2994. typedef upb_strtable_iter upb_msg_oneof_iter;
  2995. /* Well-known field tag numbers for map-entry messages. */
  2996. #define UPB_MAPENTRY_KEY 1
  2997. #define UPB_MAPENTRY_VALUE 2
  2998. /* Well-known field tag numbers for Any messages. */
  2999. #define UPB_ANY_TYPE 1
  3000. #define UPB_ANY_VALUE 2
  3001. /* Well-known field tag numbers for timestamp messages. */
  3002. #define UPB_DURATION_SECONDS 1
  3003. #define UPB_DURATION_NANOS 2
  3004. /* Well-known field tag numbers for duration messages. */
  3005. #define UPB_TIMESTAMP_SECONDS 1
  3006. #define UPB_TIMESTAMP_NANOS 2
  3007. #ifdef __cplusplus
  3008. extern "C" {
  3009. #endif
  3010. const char *upb_msgdef_fullname(const upb_msgdef *m);
  3011. const upb_filedef *upb_msgdef_file(const upb_msgdef *m);
  3012. const char *upb_msgdef_name(const upb_msgdef *m);
  3013. int upb_msgdef_numoneofs(const upb_msgdef *m);
  3014. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m);
  3015. bool upb_msgdef_mapentry(const upb_msgdef *m);
  3016. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m);
  3017. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m);
  3018. bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax);
  3019. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i);
  3020. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  3021. size_t len);
  3022. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  3023. size_t len);
  3024. int upb_msgdef_numfields(const upb_msgdef *m);
  3025. int upb_msgdef_numoneofs(const upb_msgdef *m);
  3026. UPB_INLINE const upb_oneofdef *upb_msgdef_ntooz(const upb_msgdef *m,
  3027. const char *name) {
  3028. return upb_msgdef_ntoo(m, name, strlen(name));
  3029. }
  3030. UPB_INLINE const upb_fielddef *upb_msgdef_ntofz(const upb_msgdef *m,
  3031. const char *name) {
  3032. return upb_msgdef_ntof(m, name, strlen(name));
  3033. }
  3034. /* Internal-only. */
  3035. size_t upb_msgdef_selectorcount(const upb_msgdef *m);
  3036. uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m);
  3037. /* Lookup of either field or oneof by name. Returns whether either was found.
  3038. * If the return is true, then the found def will be set, and the non-found
  3039. * one set to NULL. */
  3040. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  3041. const upb_fielddef **f, const upb_oneofdef **o);
  3042. UPB_INLINE bool upb_msgdef_lookupnamez(const upb_msgdef *m, const char *name,
  3043. const upb_fielddef **f,
  3044. const upb_oneofdef **o) {
  3045. return upb_msgdef_lookupname(m, name, strlen(name), f, o);
  3046. }
  3047. /* Iteration over fields and oneofs. For example:
  3048. *
  3049. * upb_msg_field_iter i;
  3050. * for(upb_msg_field_begin(&i, m);
  3051. * !upb_msg_field_done(&i);
  3052. * upb_msg_field_next(&i)) {
  3053. * upb_fielddef *f = upb_msg_iter_field(&i);
  3054. * // ...
  3055. * }
  3056. *
  3057. * For C we don't have separate iterators for const and non-const.
  3058. * It is the caller's responsibility to cast the upb_fielddef* to
  3059. * const if the upb_msgdef* is const. */
  3060. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m);
  3061. void upb_msg_field_next(upb_msg_field_iter *iter);
  3062. bool upb_msg_field_done(const upb_msg_field_iter *iter);
  3063. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter);
  3064. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter);
  3065. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  3066. const upb_msg_field_iter * iter2);
  3067. /* Similar to above, we also support iterating through the oneofs in a
  3068. * msgdef. */
  3069. void upb_msg_oneof_begin(upb_msg_oneof_iter * iter, const upb_msgdef *m);
  3070. void upb_msg_oneof_next(upb_msg_oneof_iter * iter);
  3071. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter);
  3072. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter);
  3073. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter * iter);
  3074. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  3075. const upb_msg_oneof_iter *iter2);
  3076. #ifdef __cplusplus
  3077. } /* extern "C" */
  3078. /* Structure that describes a single .proto message type. */
  3079. class upb::MessageDefPtr {
  3080. public:
  3081. MessageDefPtr() : ptr_(nullptr) {}
  3082. explicit MessageDefPtr(const upb_msgdef *ptr) : ptr_(ptr) {}
  3083. const upb_msgdef *ptr() const { return ptr_; }
  3084. explicit operator bool() const { return ptr_ != nullptr; }
  3085. const char* full_name() const { return upb_msgdef_fullname(ptr_); }
  3086. const char* name() const { return upb_msgdef_name(ptr_); }
  3087. /* The number of fields that belong to the MessageDef. */
  3088. int field_count() const { return upb_msgdef_numfields(ptr_); }
  3089. /* The number of oneofs that belong to the MessageDef. */
  3090. int oneof_count() const { return upb_msgdef_numoneofs(ptr_); }
  3091. upb_syntax_t syntax() const { return upb_msgdef_syntax(ptr_); }
  3092. /* These return null pointers if the field is not found. */
  3093. FieldDefPtr FindFieldByNumber(uint32_t number) const {
  3094. return FieldDefPtr(upb_msgdef_itof(ptr_, number));
  3095. }
  3096. FieldDefPtr FindFieldByName(const char* name, size_t len) const {
  3097. return FieldDefPtr(upb_msgdef_ntof(ptr_, name, len));
  3098. }
  3099. FieldDefPtr FindFieldByName(const char *name) const {
  3100. return FieldDefPtr(upb_msgdef_ntofz(ptr_, name));
  3101. }
  3102. template <class T>
  3103. FieldDefPtr FindFieldByName(const T& str) const {
  3104. return FindFieldByName(str.c_str(), str.size());
  3105. }
  3106. OneofDefPtr FindOneofByName(const char* name, size_t len) const {
  3107. return OneofDefPtr(upb_msgdef_ntoo(ptr_, name, len));
  3108. }
  3109. OneofDefPtr FindOneofByName(const char *name) const {
  3110. return OneofDefPtr(upb_msgdef_ntooz(ptr_, name));
  3111. }
  3112. template <class T>
  3113. OneofDefPtr FindOneofByName(const T &str) const {
  3114. return FindOneofByName(str.c_str(), str.size());
  3115. }
  3116. /* Is this message a map entry? */
  3117. bool mapentry() const { return upb_msgdef_mapentry(ptr_); }
  3118. /* Return the type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
  3119. * non-well-known message. */
  3120. upb_wellknowntype_t wellknowntype() const {
  3121. return upb_msgdef_wellknowntype(ptr_);
  3122. }
  3123. /* Whether is a number wrapper. */
  3124. bool isnumberwrapper() const { return upb_msgdef_isnumberwrapper(ptr_); }
  3125. /* Iteration over fields. The order is undefined. */
  3126. class const_field_iterator
  3127. : public std::iterator<std::forward_iterator_tag, FieldDefPtr> {
  3128. public:
  3129. void operator++() { upb_msg_field_next(&iter_); }
  3130. FieldDefPtr operator*() const {
  3131. return FieldDefPtr(upb_msg_iter_field(&iter_));
  3132. }
  3133. bool operator!=(const const_field_iterator &other) const {
  3134. return !upb_msg_field_iter_isequal(&iter_, &other.iter_);
  3135. }
  3136. bool operator==(const const_field_iterator &other) const {
  3137. return upb_msg_field_iter_isequal(&iter_, &other.iter_);
  3138. }
  3139. private:
  3140. friend class MessageDefPtr;
  3141. explicit const_field_iterator() {}
  3142. explicit const_field_iterator(MessageDefPtr msg) {
  3143. upb_msg_field_begin(&iter_, msg.ptr());
  3144. }
  3145. static const_field_iterator end() {
  3146. const_field_iterator iter;
  3147. upb_msg_field_iter_setdone(&iter.iter_);
  3148. return iter;
  3149. }
  3150. upb_msg_field_iter iter_;
  3151. };
  3152. /* Iteration over oneofs. The order is undefined. */
  3153. class const_oneof_iterator
  3154. : public std::iterator<std::forward_iterator_tag, OneofDefPtr> {
  3155. public:
  3156. void operator++() { upb_msg_oneof_next(&iter_); }
  3157. OneofDefPtr operator*() const {
  3158. return OneofDefPtr(upb_msg_iter_oneof(&iter_));
  3159. }
  3160. bool operator!=(const const_oneof_iterator& other) const {
  3161. return !upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
  3162. }
  3163. bool operator==(const const_oneof_iterator &other) const {
  3164. return upb_msg_oneof_iter_isequal(&iter_, &other.iter_);
  3165. }
  3166. private:
  3167. friend class MessageDefPtr;
  3168. const_oneof_iterator() {}
  3169. explicit const_oneof_iterator(MessageDefPtr msg) {
  3170. upb_msg_oneof_begin(&iter_, msg.ptr());
  3171. }
  3172. static const_oneof_iterator end() {
  3173. const_oneof_iterator iter;
  3174. upb_msg_oneof_iter_setdone(&iter.iter_);
  3175. return iter;
  3176. }
  3177. upb_msg_oneof_iter iter_;
  3178. };
  3179. class ConstFieldAccessor {
  3180. public:
  3181. explicit ConstFieldAccessor(const upb_msgdef* md) : md_(md) {}
  3182. const_field_iterator begin() { return MessageDefPtr(md_).field_begin(); }
  3183. const_field_iterator end() { return MessageDefPtr(md_).field_end(); }
  3184. private:
  3185. const upb_msgdef* md_;
  3186. };
  3187. class ConstOneofAccessor {
  3188. public:
  3189. explicit ConstOneofAccessor(const upb_msgdef* md) : md_(md) {}
  3190. const_oneof_iterator begin() { return MessageDefPtr(md_).oneof_begin(); }
  3191. const_oneof_iterator end() { return MessageDefPtr(md_).oneof_end(); }
  3192. private:
  3193. const upb_msgdef* md_;
  3194. };
  3195. const_field_iterator field_begin() const {
  3196. return const_field_iterator(*this);
  3197. }
  3198. const_field_iterator field_end() const { return const_field_iterator::end(); }
  3199. const_oneof_iterator oneof_begin() const {
  3200. return const_oneof_iterator(*this);
  3201. }
  3202. const_oneof_iterator oneof_end() const { return const_oneof_iterator::end(); }
  3203. ConstFieldAccessor fields() const { return ConstFieldAccessor(ptr()); }
  3204. ConstOneofAccessor oneofs() const { return ConstOneofAccessor(ptr()); }
  3205. private:
  3206. const upb_msgdef* ptr_;
  3207. };
  3208. inline upb::MessageDefPtr upb::FieldDefPtr::message_subdef() const {
  3209. return MessageDefPtr(upb_fielddef_msgsubdef(ptr_));
  3210. }
  3211. inline upb::MessageDefPtr upb::FieldDefPtr::containing_type() const {
  3212. return MessageDefPtr(upb_fielddef_containingtype(ptr_));
  3213. }
  3214. inline upb::MessageDefPtr upb::OneofDefPtr::containing_type() const {
  3215. return MessageDefPtr(upb_oneofdef_containingtype(ptr_));
  3216. }
  3217. #endif /* __cplusplus */
  3218. /* upb_enumdef ****************************************************************/
  3219. typedef upb_strtable_iter upb_enum_iter;
  3220. const char *upb_enumdef_fullname(const upb_enumdef *e);
  3221. const char *upb_enumdef_name(const upb_enumdef *e);
  3222. const upb_filedef *upb_enumdef_file(const upb_enumdef *e);
  3223. int32_t upb_enumdef_default(const upb_enumdef *e);
  3224. int upb_enumdef_numvals(const upb_enumdef *e);
  3225. /* Enum lookups:
  3226. * - ntoi: look up a name with specified length.
  3227. * - ntoiz: look up a name provided as a null-terminated string.
  3228. * - iton: look up an integer, returning the name as a null-terminated
  3229. * string. */
  3230. bool upb_enumdef_ntoi(const upb_enumdef *e, const char *name, size_t len,
  3231. int32_t *num);
  3232. UPB_INLINE bool upb_enumdef_ntoiz(const upb_enumdef *e,
  3233. const char *name, int32_t *num) {
  3234. return upb_enumdef_ntoi(e, name, strlen(name), num);
  3235. }
  3236. const char *upb_enumdef_iton(const upb_enumdef *e, int32_t num);
  3237. /* upb_enum_iter i;
  3238. * for(upb_enum_begin(&i, e); !upb_enum_done(&i); upb_enum_next(&i)) {
  3239. * // ...
  3240. * }
  3241. */
  3242. void upb_enum_begin(upb_enum_iter *iter, const upb_enumdef *e);
  3243. void upb_enum_next(upb_enum_iter *iter);
  3244. bool upb_enum_done(upb_enum_iter *iter);
  3245. const char *upb_enum_iter_name(upb_enum_iter *iter);
  3246. int32_t upb_enum_iter_number(upb_enum_iter *iter);
  3247. #ifdef __cplusplus
  3248. class upb::EnumDefPtr {
  3249. public:
  3250. EnumDefPtr() : ptr_(nullptr) {}
  3251. explicit EnumDefPtr(const upb_enumdef* ptr) : ptr_(ptr) {}
  3252. const upb_enumdef* ptr() const { return ptr_; }
  3253. explicit operator bool() const { return ptr_ != nullptr; }
  3254. const char* full_name() const { return upb_enumdef_fullname(ptr_); }
  3255. const char* name() const { return upb_enumdef_name(ptr_); }
  3256. /* The value that is used as the default when no field default is specified.
  3257. * If not set explicitly, the first value that was added will be used.
  3258. * The default value must be a member of the enum.
  3259. * Requires that value_count() > 0. */
  3260. int32_t default_value() const { return upb_enumdef_default(ptr_); }
  3261. /* Returns the number of values currently defined in the enum. Note that
  3262. * multiple names can refer to the same number, so this may be greater than
  3263. * the total number of unique numbers. */
  3264. int value_count() const { return upb_enumdef_numvals(ptr_); }
  3265. /* Lookups from name to integer, returning true if found. */
  3266. bool FindValueByName(const char *name, int32_t *num) const {
  3267. return upb_enumdef_ntoiz(ptr_, name, num);
  3268. }
  3269. /* Finds the name corresponding to the given number, or NULL if none was
  3270. * found. If more than one name corresponds to this number, returns the
  3271. * first one that was added. */
  3272. const char *FindValueByNumber(int32_t num) const {
  3273. return upb_enumdef_iton(ptr_, num);
  3274. }
  3275. /* Iteration over name/value pairs. The order is undefined.
  3276. * Adding an enum val invalidates any iterators.
  3277. *
  3278. * TODO: make compatible with range-for, with elements as pairs? */
  3279. class Iterator {
  3280. public:
  3281. explicit Iterator(EnumDefPtr e) { upb_enum_begin(&iter_, e.ptr()); }
  3282. int32_t number() { return upb_enum_iter_number(&iter_); }
  3283. const char *name() { return upb_enum_iter_name(&iter_); }
  3284. bool Done() { return upb_enum_done(&iter_); }
  3285. void Next() { return upb_enum_next(&iter_); }
  3286. private:
  3287. upb_enum_iter iter_;
  3288. };
  3289. private:
  3290. const upb_enumdef *ptr_;
  3291. };
  3292. inline upb::EnumDefPtr upb::FieldDefPtr::enum_subdef() const {
  3293. return EnumDefPtr(upb_fielddef_enumsubdef(ptr_));
  3294. }
  3295. #endif /* __cplusplus */
  3296. /* upb_filedef ****************************************************************/
  3297. #ifdef __cplusplus
  3298. extern "C" {
  3299. #endif
  3300. const char *upb_filedef_name(const upb_filedef *f);
  3301. const char *upb_filedef_package(const upb_filedef *f);
  3302. const char *upb_filedef_phpprefix(const upb_filedef *f);
  3303. const char *upb_filedef_phpnamespace(const upb_filedef *f);
  3304. upb_syntax_t upb_filedef_syntax(const upb_filedef *f);
  3305. int upb_filedef_depcount(const upb_filedef *f);
  3306. int upb_filedef_msgcount(const upb_filedef *f);
  3307. int upb_filedef_enumcount(const upb_filedef *f);
  3308. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i);
  3309. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i);
  3310. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i);
  3311. #ifdef __cplusplus
  3312. } /* extern "C" */
  3313. /* Class that represents a .proto file with some things defined in it.
  3314. *
  3315. * Many users won't care about FileDefs, but they are necessary if you want to
  3316. * read the values of file-level options. */
  3317. class upb::FileDefPtr {
  3318. public:
  3319. explicit FileDefPtr(const upb_filedef *ptr) : ptr_(ptr) {}
  3320. const upb_filedef* ptr() const { return ptr_; }
  3321. explicit operator bool() const { return ptr_ != nullptr; }
  3322. /* Get/set name of the file (eg. "foo/bar.proto"). */
  3323. const char* name() const { return upb_filedef_name(ptr_); }
  3324. /* Package name for definitions inside the file (eg. "foo.bar"). */
  3325. const char* package() const { return upb_filedef_package(ptr_); }
  3326. /* Sets the php class prefix which is prepended to all php generated classes
  3327. * from this .proto. Default is empty. */
  3328. const char* phpprefix() const { return upb_filedef_phpprefix(ptr_); }
  3329. /* Use this option to change the namespace of php generated classes. Default
  3330. * is empty. When this option is empty, the package name will be used for
  3331. * determining the namespace. */
  3332. const char* phpnamespace() const { return upb_filedef_phpnamespace(ptr_); }
  3333. /* Syntax for the file. Defaults to proto2. */
  3334. upb_syntax_t syntax() const { return upb_filedef_syntax(ptr_); }
  3335. /* Get the list of dependencies from the file. These are returned in the
  3336. * order that they were added to the FileDefPtr. */
  3337. int dependency_count() const { return upb_filedef_depcount(ptr_); }
  3338. const FileDefPtr dependency(int index) const {
  3339. return FileDefPtr(upb_filedef_dep(ptr_, index));
  3340. }
  3341. private:
  3342. const upb_filedef* ptr_;
  3343. };
  3344. #endif /* __cplusplus */
  3345. /* upb_symtab *****************************************************************/
  3346. #ifdef __cplusplus
  3347. extern "C" {
  3348. #endif
  3349. upb_symtab *upb_symtab_new();
  3350. void upb_symtab_free(upb_symtab* s);
  3351. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym);
  3352. const upb_msgdef *upb_symtab_lookupmsg2(
  3353. const upb_symtab *s, const char *sym, size_t len);
  3354. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym);
  3355. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name);
  3356. int upb_symtab_filecount(const upb_symtab *s);
  3357. const upb_filedef *upb_symtab_addfile(
  3358. upb_symtab *s, const google_protobuf_FileDescriptorProto *file,
  3359. upb_status *status);
  3360. /* For generated code only: loads a generated descriptor. */
  3361. typedef struct upb_def_init {
  3362. struct upb_def_init **deps;
  3363. const char *filename;
  3364. upb_strview descriptor;
  3365. } upb_def_init;
  3366. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init);
  3367. #ifdef __cplusplus
  3368. } /* extern "C" */
  3369. /* Non-const methods in upb::SymbolTable are NOT thread-safe. */
  3370. class upb::SymbolTable {
  3371. public:
  3372. SymbolTable() : ptr_(upb_symtab_new(), upb_symtab_free) {}
  3373. explicit SymbolTable(upb_symtab* s) : ptr_(s, upb_symtab_free) {}
  3374. const upb_symtab* ptr() const { return ptr_.get(); }
  3375. upb_symtab* ptr() { return ptr_.get(); }
  3376. /* Finds an entry in the symbol table with this exact name. If not found,
  3377. * returns NULL. */
  3378. MessageDefPtr LookupMessage(const char *sym) const {
  3379. return MessageDefPtr(upb_symtab_lookupmsg(ptr_.get(), sym));
  3380. }
  3381. EnumDefPtr LookupEnum(const char *sym) const {
  3382. return EnumDefPtr(upb_symtab_lookupenum(ptr_.get(), sym));
  3383. }
  3384. FileDefPtr LookupFile(const char *name) const {
  3385. return FileDefPtr(upb_symtab_lookupfile(ptr_.get(), name));
  3386. }
  3387. /* TODO: iteration? */
  3388. /* Adds the given serialized FileDescriptorProto to the pool. */
  3389. FileDefPtr AddFile(const google_protobuf_FileDescriptorProto *file_proto,
  3390. Status *status) {
  3391. return FileDefPtr(
  3392. upb_symtab_addfile(ptr_.get(), file_proto, status->ptr()));
  3393. }
  3394. private:
  3395. std::unique_ptr<upb_symtab, decltype(&upb_symtab_free)> ptr_;
  3396. };
  3397. UPB_INLINE const char* upb_safecstr(const std::string& str) {
  3398. UPB_ASSERT(str.size() == std::strlen(str.c_str()));
  3399. return str.c_str();
  3400. }
  3401. #endif /* __cplusplus */
  3402. #endif /* UPB_DEF_H_ */
  3403. /*
  3404. ** upb::Handlers (upb_handlers)
  3405. **
  3406. ** A upb_handlers is like a virtual table for a upb_msgdef. Each field of the
  3407. ** message can have associated functions that will be called when we are
  3408. ** parsing or visiting a stream of data. This is similar to how handlers work
  3409. ** in SAX (the Simple API for XML).
  3410. **
  3411. ** The handlers have no idea where the data is coming from, so a single set of
  3412. ** handlers could be used with two completely different data sources (for
  3413. ** example, a parser and a visitor over in-memory objects). This decoupling is
  3414. ** the most important feature of upb, because it allows parsers and serializers
  3415. ** to be highly reusable.
  3416. **
  3417. ** This is a mixed C/C++ interface that offers a full API to both languages.
  3418. ** See the top-level README for more information.
  3419. */
  3420. #ifndef UPB_HANDLERS_H
  3421. #define UPB_HANDLERS_H
  3422. #ifdef __cplusplus
  3423. namespace upb {
  3424. class HandlersPtr;
  3425. class HandlerCache;
  3426. template <class T> class Handler;
  3427. template <class T> struct CanonicalType;
  3428. } /* namespace upb */
  3429. #endif
  3430. /* The maximum depth that the handler graph can have. This is a resource limit
  3431. * for the C stack since we sometimes need to recursively traverse the graph.
  3432. * Cycles are ok; the traversal will stop when it detects a cycle, but we must
  3433. * hit the cycle before the maximum depth is reached.
  3434. *
  3435. * If having a single static limit is too inflexible, we can add another variant
  3436. * of Handlers::Freeze that allows specifying this as a parameter. */
  3437. #define UPB_MAX_HANDLER_DEPTH 64
  3438. /* All the different types of handlers that can be registered.
  3439. * Only needed for the advanced functions in upb::Handlers. */
  3440. typedef enum {
  3441. UPB_HANDLER_INT32,
  3442. UPB_HANDLER_INT64,
  3443. UPB_HANDLER_UINT32,
  3444. UPB_HANDLER_UINT64,
  3445. UPB_HANDLER_FLOAT,
  3446. UPB_HANDLER_DOUBLE,
  3447. UPB_HANDLER_BOOL,
  3448. UPB_HANDLER_STARTSTR,
  3449. UPB_HANDLER_STRING,
  3450. UPB_HANDLER_ENDSTR,
  3451. UPB_HANDLER_STARTSUBMSG,
  3452. UPB_HANDLER_ENDSUBMSG,
  3453. UPB_HANDLER_STARTSEQ,
  3454. UPB_HANDLER_ENDSEQ
  3455. } upb_handlertype_t;
  3456. #define UPB_HANDLER_MAX (UPB_HANDLER_ENDSEQ+1)
  3457. #define UPB_BREAK NULL
  3458. /* A convenient definition for when no closure is needed. */
  3459. extern char _upb_noclosure;
  3460. #define UPB_NO_CLOSURE &_upb_noclosure
  3461. /* A selector refers to a specific field handler in the Handlers object
  3462. * (for example: the STARTSUBMSG handler for field "field15"). */
  3463. typedef int32_t upb_selector_t;
  3464. /* Static selectors for upb::Handlers. */
  3465. #define UPB_STARTMSG_SELECTOR 0
  3466. #define UPB_ENDMSG_SELECTOR 1
  3467. #define UPB_UNKNOWN_SELECTOR 2
  3468. #define UPB_STATIC_SELECTOR_COUNT 3
  3469. /* Static selectors for upb::BytesHandler. */
  3470. #define UPB_STARTSTR_SELECTOR 0
  3471. #define UPB_STRING_SELECTOR 1
  3472. #define UPB_ENDSTR_SELECTOR 2
  3473. #ifdef __cplusplus
  3474. template<class T> const void *UniquePtrForType() {
  3475. static const char ch = 0;
  3476. return &ch;
  3477. }
  3478. #endif
  3479. /* upb_handlers ************************************************************/
  3480. /* Handler attributes, to be registered with the handler itself. */
  3481. typedef struct {
  3482. const void *handler_data;
  3483. const void *closure_type;
  3484. const void *return_closure_type;
  3485. bool alwaysok;
  3486. } upb_handlerattr;
  3487. #define UPB_HANDLERATTR_INIT {NULL, NULL, NULL, false}
  3488. /* Bufhandle, data passed along with a buffer to indicate its provenance. */
  3489. typedef struct {
  3490. /* The beginning of the buffer. This may be different than the pointer
  3491. * passed to a StringBuf handler because the handler may receive data
  3492. * that is from the middle or end of a larger buffer. */
  3493. const char *buf;
  3494. /* The offset within the attached object where this buffer begins. Only
  3495. * meaningful if there is an attached object. */
  3496. size_t objofs;
  3497. /* The attached object (if any) and a pointer representing its type. */
  3498. const void *obj;
  3499. const void *objtype;
  3500. #ifdef __cplusplus
  3501. template <class T>
  3502. void SetAttachedObject(const T* _obj) {
  3503. obj = _obj;
  3504. objtype = UniquePtrForType<T>();
  3505. }
  3506. template <class T>
  3507. const T *GetAttachedObject() const {
  3508. return objtype == UniquePtrForType<T>() ? static_cast<const T *>(obj)
  3509. : NULL;
  3510. }
  3511. #endif
  3512. } upb_bufhandle;
  3513. #define UPB_BUFHANDLE_INIT {NULL, 0, NULL, NULL}
  3514. /* Handler function typedefs. */
  3515. typedef void upb_handlerfree(void *d);
  3516. typedef bool upb_unknown_handlerfunc(void *c, const void *hd, const char *buf,
  3517. size_t n);
  3518. typedef bool upb_startmsg_handlerfunc(void *c, const void*);
  3519. typedef bool upb_endmsg_handlerfunc(void *c, const void *, upb_status *status);
  3520. typedef void* upb_startfield_handlerfunc(void *c, const void *hd);
  3521. typedef bool upb_endfield_handlerfunc(void *c, const void *hd);
  3522. typedef bool upb_int32_handlerfunc(void *c, const void *hd, int32_t val);
  3523. typedef bool upb_int64_handlerfunc(void *c, const void *hd, int64_t val);
  3524. typedef bool upb_uint32_handlerfunc(void *c, const void *hd, uint32_t val);
  3525. typedef bool upb_uint64_handlerfunc(void *c, const void *hd, uint64_t val);
  3526. typedef bool upb_float_handlerfunc(void *c, const void *hd, float val);
  3527. typedef bool upb_double_handlerfunc(void *c, const void *hd, double val);
  3528. typedef bool upb_bool_handlerfunc(void *c, const void *hd, bool val);
  3529. typedef void *upb_startstr_handlerfunc(void *c, const void *hd,
  3530. size_t size_hint);
  3531. typedef size_t upb_string_handlerfunc(void *c, const void *hd, const char *buf,
  3532. size_t n, const upb_bufhandle* handle);
  3533. struct upb_handlers;
  3534. typedef struct upb_handlers upb_handlers;
  3535. #ifdef __cplusplus
  3536. extern "C" {
  3537. #endif
  3538. /* Mutating accessors. */
  3539. const upb_status *upb_handlers_status(upb_handlers *h);
  3540. void upb_handlers_clearerr(upb_handlers *h);
  3541. const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h);
  3542. bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *hfree);
  3543. bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func,
  3544. const upb_handlerattr *attr);
  3545. bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
  3546. const upb_handlerattr *attr);
  3547. bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
  3548. const upb_handlerattr *attr);
  3549. bool upb_handlers_setint32(upb_handlers *h, const upb_fielddef *f,
  3550. upb_int32_handlerfunc *func,
  3551. const upb_handlerattr *attr);
  3552. bool upb_handlers_setint64(upb_handlers *h, const upb_fielddef *f,
  3553. upb_int64_handlerfunc *func,
  3554. const upb_handlerattr *attr);
  3555. bool upb_handlers_setuint32(upb_handlers *h, const upb_fielddef *f,
  3556. upb_uint32_handlerfunc *func,
  3557. const upb_handlerattr *attr);
  3558. bool upb_handlers_setuint64(upb_handlers *h, const upb_fielddef *f,
  3559. upb_uint64_handlerfunc *func,
  3560. const upb_handlerattr *attr);
  3561. bool upb_handlers_setfloat(upb_handlers *h, const upb_fielddef *f,
  3562. upb_float_handlerfunc *func,
  3563. const upb_handlerattr *attr);
  3564. bool upb_handlers_setdouble(upb_handlers *h, const upb_fielddef *f,
  3565. upb_double_handlerfunc *func,
  3566. const upb_handlerattr *attr);
  3567. bool upb_handlers_setbool(upb_handlers *h, const upb_fielddef *f,
  3568. upb_bool_handlerfunc *func,
  3569. const upb_handlerattr *attr);
  3570. bool upb_handlers_setstartstr(upb_handlers *h, const upb_fielddef *f,
  3571. upb_startstr_handlerfunc *func,
  3572. const upb_handlerattr *attr);
  3573. bool upb_handlers_setstring(upb_handlers *h, const upb_fielddef *f,
  3574. upb_string_handlerfunc *func,
  3575. const upb_handlerattr *attr);
  3576. bool upb_handlers_setendstr(upb_handlers *h, const upb_fielddef *f,
  3577. upb_endfield_handlerfunc *func,
  3578. const upb_handlerattr *attr);
  3579. bool upb_handlers_setstartseq(upb_handlers *h, const upb_fielddef *f,
  3580. upb_startfield_handlerfunc *func,
  3581. const upb_handlerattr *attr);
  3582. bool upb_handlers_setstartsubmsg(upb_handlers *h, const upb_fielddef *f,
  3583. upb_startfield_handlerfunc *func,
  3584. const upb_handlerattr *attr);
  3585. bool upb_handlers_setendsubmsg(upb_handlers *h, const upb_fielddef *f,
  3586. upb_endfield_handlerfunc *func,
  3587. const upb_handlerattr *attr);
  3588. bool upb_handlers_setendseq(upb_handlers *h, const upb_fielddef *f,
  3589. upb_endfield_handlerfunc *func,
  3590. const upb_handlerattr *attr);
  3591. /* Read-only accessors. */
  3592. const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
  3593. const upb_fielddef *f);
  3594. const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
  3595. upb_selector_t sel);
  3596. upb_func *upb_handlers_gethandler(const upb_handlers *h, upb_selector_t s,
  3597. const void **handler_data);
  3598. bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t s,
  3599. upb_handlerattr *attr);
  3600. /* "Static" methods */
  3601. upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f);
  3602. bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
  3603. upb_selector_t *s);
  3604. UPB_INLINE upb_selector_t upb_handlers_getendselector(upb_selector_t start) {
  3605. return start + 1;
  3606. }
  3607. /* Internal-only. */
  3608. uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f);
  3609. uint32_t upb_handlers_selectorcount(const upb_fielddef *f);
  3610. #ifdef __cplusplus
  3611. } /* extern "C" */
  3612. namespace upb {
  3613. typedef upb_handlers Handlers;
  3614. }
  3615. /* Convenience macros for creating a Handler object that is wrapped with a
  3616. * type-safe wrapper function that converts the "void*" parameters/returns
  3617. * of the underlying C API into nice C++ function.
  3618. *
  3619. * Sample usage:
  3620. * void OnValue1(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3621. * // do stuff ...
  3622. * }
  3623. *
  3624. * // Handler that doesn't need any data bound to it.
  3625. * void OnValue2(MyClosure* c, int32_t val) {
  3626. * // do stuff ...
  3627. * }
  3628. *
  3629. * // Handler that returns bool so it can return failure if necessary.
  3630. * bool OnValue3(MyClosure* c, int32_t val) {
  3631. * // do stuff ...
  3632. * return ok;
  3633. * }
  3634. *
  3635. * // Member function handler.
  3636. * class MyClosure {
  3637. * public:
  3638. * void OnValue(int32_t val) {
  3639. * // do stuff ...
  3640. * }
  3641. * };
  3642. *
  3643. * // Takes ownership of the MyHandlerData.
  3644. * handlers->SetInt32Handler(f1, UpbBind(OnValue1, new MyHandlerData(...)));
  3645. * handlers->SetInt32Handler(f2, UpbMakeHandler(OnValue2));
  3646. * handlers->SetInt32Handler(f1, UpbMakeHandler(OnValue3));
  3647. * handlers->SetInt32Handler(f2, UpbMakeHandler(&MyClosure::OnValue));
  3648. */
  3649. /* In C++11, the "template" disambiguator can appear even outside templates,
  3650. * so all calls can safely use this pair of macros. */
  3651. #define UpbMakeHandler(f) upb::MatchFunc(f).template GetFunc<f>()
  3652. /* We have to be careful to only evaluate "d" once. */
  3653. #define UpbBind(f, d) upb::MatchFunc(f).template GetFunc<f>((d))
  3654. /* Handler: a struct that contains the (handler, data, deleter) tuple that is
  3655. * used to register all handlers. Users can Make() these directly but it's
  3656. * more convenient to use the UpbMakeHandler/UpbBind macros above. */
  3657. template <class T> class upb::Handler {
  3658. public:
  3659. /* The underlying, handler function signature that upb uses internally. */
  3660. typedef T FuncPtr;
  3661. /* Intentionally implicit. */
  3662. template <class F> Handler(F func);
  3663. ~Handler() { UPB_ASSERT(registered_); }
  3664. void AddCleanup(upb_handlers* h) const;
  3665. FuncPtr handler() const { return handler_; }
  3666. const upb_handlerattr& attr() const { return attr_; }
  3667. private:
  3668. Handler(const Handler&) = delete;
  3669. Handler& operator=(const Handler&) = delete;
  3670. FuncPtr handler_;
  3671. mutable upb_handlerattr attr_;
  3672. mutable bool registered_;
  3673. void *cleanup_data_;
  3674. upb_handlerfree *cleanup_func_;
  3675. };
  3676. /* A upb::Handlers object represents the set of handlers associated with a
  3677. * message in the graph of messages. You can think of it as a big virtual
  3678. * table with functions corresponding to all the events that can fire while
  3679. * parsing or visiting a message of a specific type.
  3680. *
  3681. * Any handlers that are not set behave as if they had successfully consumed
  3682. * the value. Any unset Start* handlers will propagate their closure to the
  3683. * inner frame.
  3684. *
  3685. * The easiest way to create the *Handler objects needed by the Set* methods is
  3686. * with the UpbBind() and UpbMakeHandler() macros; see below. */
  3687. class upb::HandlersPtr {
  3688. public:
  3689. HandlersPtr(upb_handlers* ptr) : ptr_(ptr) {}
  3690. upb_handlers* ptr() const { return ptr_; }
  3691. typedef upb_selector_t Selector;
  3692. typedef upb_handlertype_t Type;
  3693. typedef Handler<void *(*)(void *, const void *)> StartFieldHandler;
  3694. typedef Handler<bool (*)(void *, const void *)> EndFieldHandler;
  3695. typedef Handler<bool (*)(void *, const void *)> StartMessageHandler;
  3696. typedef Handler<bool (*)(void *, const void *, upb_status *)>
  3697. EndMessageHandler;
  3698. typedef Handler<void *(*)(void *, const void *, size_t)> StartStringHandler;
  3699. typedef Handler<size_t (*)(void *, const void *, const char *, size_t,
  3700. const upb_bufhandle *)>
  3701. StringHandler;
  3702. template <class T> struct ValueHandler {
  3703. typedef Handler<bool(*)(void *, const void *, T)> H;
  3704. };
  3705. typedef ValueHandler<int32_t>::H Int32Handler;
  3706. typedef ValueHandler<int64_t>::H Int64Handler;
  3707. typedef ValueHandler<uint32_t>::H UInt32Handler;
  3708. typedef ValueHandler<uint64_t>::H UInt64Handler;
  3709. typedef ValueHandler<float>::H FloatHandler;
  3710. typedef ValueHandler<double>::H DoubleHandler;
  3711. typedef ValueHandler<bool>::H BoolHandler;
  3712. /* Any function pointer can be converted to this and converted back to its
  3713. * correct type. */
  3714. typedef void GenericFunction();
  3715. typedef void HandlersCallback(const void *closure, upb_handlers *h);
  3716. /* Returns the msgdef associated with this handlers object. */
  3717. MessageDefPtr message_def() const {
  3718. return MessageDefPtr(upb_handlers_msgdef(ptr()));
  3719. }
  3720. /* Adds the given pointer and function to the list of cleanup functions that
  3721. * will be run when these handlers are freed. If this pointer has previously
  3722. * been registered, the function returns false and does nothing. */
  3723. bool AddCleanup(void *ptr, upb_handlerfree *cleanup) {
  3724. return upb_handlers_addcleanup(ptr_, ptr, cleanup);
  3725. }
  3726. /* Sets the startmsg handler for the message, which is defined as follows:
  3727. *
  3728. * bool startmsg(MyType* closure) {
  3729. * // Called when the message begins. Returns true if processing should
  3730. * // continue.
  3731. * return true;
  3732. * }
  3733. */
  3734. bool SetStartMessageHandler(const StartMessageHandler &h) {
  3735. h.AddCleanup(ptr());
  3736. return upb_handlers_setstartmsg(ptr(), h.handler(), &h.attr());
  3737. }
  3738. /* Sets the endmsg handler for the message, which is defined as follows:
  3739. *
  3740. * bool endmsg(MyType* closure, upb_status *status) {
  3741. * // Called when processing of this message ends, whether in success or
  3742. * // failure. "status" indicates the final status of processing, and
  3743. * // can also be modified in-place to update the final status.
  3744. * }
  3745. */
  3746. bool SetEndMessageHandler(const EndMessageHandler& h) {
  3747. h.AddCleanup(ptr());
  3748. return upb_handlers_setendmsg(ptr(), h.handler(), &h.attr());
  3749. }
  3750. /* Sets the value handler for the given field, which is defined as follows
  3751. * (this is for an int32 field; other field types will pass their native
  3752. * C/C++ type for "val"):
  3753. *
  3754. * bool OnValue(MyClosure* c, const MyHandlerData* d, int32_t val) {
  3755. * // Called when the field's value is encountered. "d" contains
  3756. * // whatever data was bound to this field when it was registered.
  3757. * // Returns true if processing should continue.
  3758. * return true;
  3759. * }
  3760. *
  3761. * handers->SetInt32Handler(f, UpbBind(OnValue, new MyHandlerData(...)));
  3762. *
  3763. * The value type must exactly match f->type().
  3764. * For example, a handler that takes an int32_t parameter may only be used for
  3765. * fields of type UPB_TYPE_INT32 and UPB_TYPE_ENUM.
  3766. *
  3767. * Returns false if the handler failed to register; in this case the cleanup
  3768. * handler (if any) will be called immediately.
  3769. */
  3770. bool SetInt32Handler(FieldDefPtr f, const Int32Handler &h) {
  3771. h.AddCleanup(ptr());
  3772. return upb_handlers_setint32(ptr(), f.ptr(), h.handler(), &h.attr());
  3773. }
  3774. bool SetInt64Handler (FieldDefPtr f, const Int64Handler& h) {
  3775. h.AddCleanup(ptr());
  3776. return upb_handlers_setint64(ptr(), f.ptr(), h.handler(), &h.attr());
  3777. }
  3778. bool SetUInt32Handler(FieldDefPtr f, const UInt32Handler& h) {
  3779. h.AddCleanup(ptr());
  3780. return upb_handlers_setuint32(ptr(), f.ptr(), h.handler(), &h.attr());
  3781. }
  3782. bool SetUInt64Handler(FieldDefPtr f, const UInt64Handler& h) {
  3783. h.AddCleanup(ptr());
  3784. return upb_handlers_setuint64(ptr(), f.ptr(), h.handler(), &h.attr());
  3785. }
  3786. bool SetFloatHandler (FieldDefPtr f, const FloatHandler& h) {
  3787. h.AddCleanup(ptr());
  3788. return upb_handlers_setfloat(ptr(), f.ptr(), h.handler(), &h.attr());
  3789. }
  3790. bool SetDoubleHandler(FieldDefPtr f, const DoubleHandler& h) {
  3791. h.AddCleanup(ptr());
  3792. return upb_handlers_setdouble(ptr(), f.ptr(), h.handler(), &h.attr());
  3793. }
  3794. bool SetBoolHandler(FieldDefPtr f, const BoolHandler &h) {
  3795. h.AddCleanup(ptr());
  3796. return upb_handlers_setbool(ptr(), f.ptr(), h.handler(), &h.attr());
  3797. }
  3798. /* Like the previous, but templated on the type on the value (ie. int32).
  3799. * This is mostly useful to call from other templates. To call this you must
  3800. * specify the template parameter explicitly, ie:
  3801. * h->SetValueHandler<T>(f, UpbBind(MyHandler<T>, MyData)); */
  3802. template <class T>
  3803. bool SetValueHandler(
  3804. FieldDefPtr f,
  3805. const typename ValueHandler<typename CanonicalType<T>::Type>::H &handler);
  3806. /* Sets handlers for a string field, which are defined as follows:
  3807. *
  3808. * MySubClosure* startstr(MyClosure* c, const MyHandlerData* d,
  3809. * size_t size_hint) {
  3810. * // Called when a string value begins. The return value indicates the
  3811. * // closure for the string. "size_hint" indicates the size of the
  3812. * // string if it is known, however if the string is length-delimited
  3813. * // and the end-of-string is not available size_hint will be zero.
  3814. * // This case is indistinguishable from the case where the size is
  3815. * // known to be zero.
  3816. * //
  3817. * // TODO(haberman): is it important to distinguish these cases?
  3818. * // If we had ssize_t as a type we could make -1 "unknown", but
  3819. * // ssize_t is POSIX (not ANSI) and therefore less portable.
  3820. * // In practice I suspect it won't be important to distinguish.
  3821. * return closure;
  3822. * }
  3823. *
  3824. * size_t str(MyClosure* closure, const MyHandlerData* d,
  3825. * const char *str, size_t len) {
  3826. * // Called for each buffer of string data; the multiple physical buffers
  3827. * // are all part of the same logical string. The return value indicates
  3828. * // how many bytes were consumed. If this number is less than "len",
  3829. * // this will also indicate that processing should be halted for now,
  3830. * // like returning false or UPB_BREAK from any other callback. If
  3831. * // number is greater than "len", the excess bytes will be skipped over
  3832. * // and not passed to the callback.
  3833. * return len;
  3834. * }
  3835. *
  3836. * bool endstr(MyClosure* c, const MyHandlerData* d) {
  3837. * // Called when a string value ends. Return value indicates whether
  3838. * // processing should continue.
  3839. * return true;
  3840. * }
  3841. */
  3842. bool SetStartStringHandler(FieldDefPtr f, const StartStringHandler &h) {
  3843. h.AddCleanup(ptr());
  3844. return upb_handlers_setstartstr(ptr(), f.ptr(), h.handler(), &h.attr());
  3845. }
  3846. bool SetStringHandler(FieldDefPtr f, const StringHandler& h) {
  3847. h.AddCleanup(ptr());
  3848. return upb_handlers_setstring(ptr(), f.ptr(), h.handler(), &h.attr());
  3849. }
  3850. bool SetEndStringHandler(FieldDefPtr f, const EndFieldHandler& h) {
  3851. h.AddCleanup(ptr());
  3852. return upb_handlers_setendstr(ptr(), f.ptr(), h.handler(), &h.attr());
  3853. }
  3854. /* Sets the startseq handler, which is defined as follows:
  3855. *
  3856. * MySubClosure *startseq(MyClosure* c, const MyHandlerData* d) {
  3857. * // Called when a sequence (repeated field) begins. The returned
  3858. * // pointer indicates the closure for the sequence (or UPB_BREAK
  3859. * // to interrupt processing).
  3860. * return closure;
  3861. * }
  3862. *
  3863. * h->SetStartSequenceHandler(f, UpbBind(startseq, new MyHandlerData(...)));
  3864. *
  3865. * Returns "false" if "f" does not belong to this message or is not a
  3866. * repeated field.
  3867. */
  3868. bool SetStartSequenceHandler(FieldDefPtr f, const StartFieldHandler &h) {
  3869. h.AddCleanup(ptr());
  3870. return upb_handlers_setstartseq(ptr(), f.ptr(), h.handler(), &h.attr());
  3871. }
  3872. /* Sets the startsubmsg handler for the given field, which is defined as
  3873. * follows:
  3874. *
  3875. * MySubClosure* startsubmsg(MyClosure* c, const MyHandlerData* d) {
  3876. * // Called when a submessage begins. The returned pointer indicates the
  3877. * // closure for the sequence (or UPB_BREAK to interrupt processing).
  3878. * return closure;
  3879. * }
  3880. *
  3881. * h->SetStartSubMessageHandler(f, UpbBind(startsubmsg,
  3882. * new MyHandlerData(...)));
  3883. *
  3884. * Returns "false" if "f" does not belong to this message or is not a
  3885. * submessage/group field.
  3886. */
  3887. bool SetStartSubMessageHandler(FieldDefPtr f, const StartFieldHandler& h) {
  3888. h.AddCleanup(ptr());
  3889. return upb_handlers_setstartsubmsg(ptr(), f.ptr(), h.handler(), &h.attr());
  3890. }
  3891. /* Sets the endsubmsg handler for the given field, which is defined as
  3892. * follows:
  3893. *
  3894. * bool endsubmsg(MyClosure* c, const MyHandlerData* d) {
  3895. * // Called when a submessage ends. Returns true to continue processing.
  3896. * return true;
  3897. * }
  3898. *
  3899. * Returns "false" if "f" does not belong to this message or is not a
  3900. * submessage/group field.
  3901. */
  3902. bool SetEndSubMessageHandler(FieldDefPtr f, const EndFieldHandler &h) {
  3903. h.AddCleanup(ptr());
  3904. return upb_handlers_setendsubmsg(ptr(), f.ptr(), h.handler(), &h.attr());
  3905. }
  3906. /* Starts the endsubseq handler for the given field, which is defined as
  3907. * follows:
  3908. *
  3909. * bool endseq(MyClosure* c, const MyHandlerData* d) {
  3910. * // Called when a sequence ends. Returns true continue processing.
  3911. * return true;
  3912. * }
  3913. *
  3914. * Returns "false" if "f" does not belong to this message or is not a
  3915. * repeated field.
  3916. */
  3917. bool SetEndSequenceHandler(FieldDefPtr f, const EndFieldHandler &h) {
  3918. h.AddCleanup(ptr());
  3919. return upb_handlers_setendseq(ptr(), f.ptr(), h.handler(), &h.attr());
  3920. }
  3921. private:
  3922. upb_handlers* ptr_;
  3923. };
  3924. #endif /* __cplusplus */
  3925. /* upb_handlercache ***********************************************************/
  3926. /* A upb_handlercache lazily builds and caches upb_handlers. You pass it a
  3927. * function (with optional closure) that can build handlers for a given
  3928. * message on-demand, and the cache maintains a map of msgdef->handlers. */
  3929. #ifdef __cplusplus
  3930. extern "C" {
  3931. #endif
  3932. struct upb_handlercache;
  3933. typedef struct upb_handlercache upb_handlercache;
  3934. typedef void upb_handlers_callback(const void *closure, upb_handlers *h);
  3935. upb_handlercache *upb_handlercache_new(upb_handlers_callback *callback,
  3936. const void *closure);
  3937. void upb_handlercache_free(upb_handlercache *cache);
  3938. const upb_handlers *upb_handlercache_get(upb_handlercache *cache,
  3939. const upb_msgdef *md);
  3940. bool upb_handlercache_addcleanup(upb_handlercache *h, void *p,
  3941. upb_handlerfree *hfree);
  3942. #ifdef __cplusplus
  3943. } /* extern "C" */
  3944. class upb::HandlerCache {
  3945. public:
  3946. HandlerCache(upb_handlers_callback *callback, const void *closure)
  3947. : ptr_(upb_handlercache_new(callback, closure), upb_handlercache_free) {}
  3948. HandlerCache(HandlerCache&&) = default;
  3949. HandlerCache& operator=(HandlerCache&&) = default;
  3950. HandlerCache(upb_handlercache* c) : ptr_(c, upb_handlercache_free) {}
  3951. upb_handlercache* ptr() { return ptr_.get(); }
  3952. const upb_handlers *Get(MessageDefPtr md) {
  3953. return upb_handlercache_get(ptr_.get(), md.ptr());
  3954. }
  3955. private:
  3956. std::unique_ptr<upb_handlercache, decltype(&upb_handlercache_free)> ptr_;
  3957. };
  3958. #endif /* __cplusplus */
  3959. /* upb_byteshandler ***********************************************************/
  3960. typedef struct {
  3961. upb_func *func;
  3962. /* It is wasteful to include the entire attributes here:
  3963. *
  3964. * * Some of the information is redundant (like storing the closure type
  3965. * separately for each handler that must match).
  3966. * * Some of the info is only needed prior to freeze() (like closure types).
  3967. * * alignment padding wastes a lot of space for alwaysok_.
  3968. *
  3969. * If/when the size and locality of handlers is an issue, we can optimize this
  3970. * not to store the entire attr like this. We do not expose the table's
  3971. * layout to allow this optimization in the future. */
  3972. upb_handlerattr attr;
  3973. } upb_handlers_tabent;
  3974. #define UPB_TABENT_INIT {NULL, UPB_HANDLERATTR_INIT}
  3975. typedef struct {
  3976. upb_handlers_tabent table[3];
  3977. } upb_byteshandler;
  3978. #define UPB_BYTESHANDLER_INIT \
  3979. { \
  3980. { UPB_TABENT_INIT, UPB_TABENT_INIT, UPB_TABENT_INIT } \
  3981. }
  3982. UPB_INLINE void upb_byteshandler_init(upb_byteshandler *handler) {
  3983. upb_byteshandler init = UPB_BYTESHANDLER_INIT;
  3984. *handler = init;
  3985. }
  3986. #ifdef __cplusplus
  3987. extern "C" {
  3988. #endif
  3989. /* Caller must ensure that "d" outlives the handlers. */
  3990. bool upb_byteshandler_setstartstr(upb_byteshandler *h,
  3991. upb_startstr_handlerfunc *func, void *d);
  3992. bool upb_byteshandler_setstring(upb_byteshandler *h,
  3993. upb_string_handlerfunc *func, void *d);
  3994. bool upb_byteshandler_setendstr(upb_byteshandler *h,
  3995. upb_endfield_handlerfunc *func, void *d);
  3996. #ifdef __cplusplus
  3997. } /* extern "C" */
  3998. namespace upb {
  3999. typedef upb_byteshandler BytesHandler;
  4000. }
  4001. #endif
  4002. /** Message handlers ******************************************************************/
  4003. #ifdef __cplusplus
  4004. extern "C" {
  4005. #endif
  4006. /* These are the handlers used internally by upb_msgfactory_getmergehandlers().
  4007. * They write scalar data to a known offset from the message pointer.
  4008. *
  4009. * These would be trivial for anyone to implement themselves, but it's better
  4010. * to use these because some JITs will recognize and specialize these instead
  4011. * of actually calling the function. */
  4012. /* Sets a handler for the given primitive field that will write the data at the
  4013. * given offset. If hasbit > 0, also sets a hasbit at the given bit offset
  4014. * (addressing each byte low to high). */
  4015. bool upb_msg_setscalarhandler(upb_handlers *h,
  4016. const upb_fielddef *f,
  4017. size_t offset,
  4018. int32_t hasbit);
  4019. /* If the given handler is a msghandlers_primitive field, returns true and sets
  4020. * *type, *offset and *hasbit. Otherwise returns false. */
  4021. bool upb_msg_getscalarhandlerdata(const upb_handlers *h,
  4022. upb_selector_t s,
  4023. upb_fieldtype_t *type,
  4024. size_t *offset,
  4025. int32_t *hasbit);
  4026. #ifdef __cplusplus
  4027. } /* extern "C" */
  4028. #endif
  4029. /*
  4030. ** Inline definitions for handlers.h, which are particularly long and a bit
  4031. ** tricky.
  4032. */
  4033. #ifndef UPB_HANDLERS_INL_H_
  4034. #define UPB_HANDLERS_INL_H_
  4035. #include <limits.h>
  4036. #include <stddef.h>
  4037. #ifdef __cplusplus
  4038. /* Type detection and typedefs for integer types.
  4039. * For platforms where there are multiple 32-bit or 64-bit types, we need to be
  4040. * able to enumerate them so we can properly create overloads for all variants.
  4041. *
  4042. * If any platform existed where there were three integer types with the same
  4043. * size, this would have to become more complicated. For example, short, int,
  4044. * and long could all be 32-bits. Even more diabolically, short, int, long,
  4045. * and long long could all be 64 bits and still be standard-compliant.
  4046. * However, few platforms are this strange, and it's unlikely that upb will be
  4047. * used on the strangest ones. */
  4048. /* Can't count on stdint.h limits like INT32_MAX, because in C++ these are
  4049. * only defined when __STDC_LIMIT_MACROS are defined before the *first* include
  4050. * of stdint.h. We can't guarantee that someone else didn't include these first
  4051. * without defining __STDC_LIMIT_MACROS. */
  4052. #define UPB_INT32_MAX 0x7fffffffLL
  4053. #define UPB_INT32_MIN (-UPB_INT32_MAX - 1)
  4054. #define UPB_INT64_MAX 0x7fffffffffffffffLL
  4055. #define UPB_INT64_MIN (-UPB_INT64_MAX - 1)
  4056. #if INT_MAX == UPB_INT32_MAX && INT_MIN == UPB_INT32_MIN
  4057. #define UPB_INT_IS_32BITS 1
  4058. #endif
  4059. #if LONG_MAX == UPB_INT32_MAX && LONG_MIN == UPB_INT32_MIN
  4060. #define UPB_LONG_IS_32BITS 1
  4061. #endif
  4062. #if LONG_MAX == UPB_INT64_MAX && LONG_MIN == UPB_INT64_MIN
  4063. #define UPB_LONG_IS_64BITS 1
  4064. #endif
  4065. #if LLONG_MAX == UPB_INT64_MAX && LLONG_MIN == UPB_INT64_MIN
  4066. #define UPB_LLONG_IS_64BITS 1
  4067. #endif
  4068. /* We use macros instead of typedefs so we can undefine them later and avoid
  4069. * leaking them outside this header file. */
  4070. #if UPB_INT_IS_32BITS
  4071. #define UPB_INT32_T int
  4072. #define UPB_UINT32_T unsigned int
  4073. #if UPB_LONG_IS_32BITS
  4074. #define UPB_TWO_32BIT_TYPES 1
  4075. #define UPB_INT32ALT_T long
  4076. #define UPB_UINT32ALT_T unsigned long
  4077. #endif /* UPB_LONG_IS_32BITS */
  4078. #elif UPB_LONG_IS_32BITS /* && !UPB_INT_IS_32BITS */
  4079. #define UPB_INT32_T long
  4080. #define UPB_UINT32_T unsigned long
  4081. #endif /* UPB_INT_IS_32BITS */
  4082. #if UPB_LONG_IS_64BITS
  4083. #define UPB_INT64_T long
  4084. #define UPB_UINT64_T unsigned long
  4085. #if UPB_LLONG_IS_64BITS
  4086. #define UPB_TWO_64BIT_TYPES 1
  4087. #define UPB_INT64ALT_T long long
  4088. #define UPB_UINT64ALT_T unsigned long long
  4089. #endif /* UPB_LLONG_IS_64BITS */
  4090. #elif UPB_LLONG_IS_64BITS /* && !UPB_LONG_IS_64BITS */
  4091. #define UPB_INT64_T long long
  4092. #define UPB_UINT64_T unsigned long long
  4093. #endif /* UPB_LONG_IS_64BITS */
  4094. #undef UPB_INT32_MAX
  4095. #undef UPB_INT32_MIN
  4096. #undef UPB_INT64_MAX
  4097. #undef UPB_INT64_MIN
  4098. #undef UPB_INT_IS_32BITS
  4099. #undef UPB_LONG_IS_32BITS
  4100. #undef UPB_LONG_IS_64BITS
  4101. #undef UPB_LLONG_IS_64BITS
  4102. namespace upb {
  4103. typedef void CleanupFunc(void *ptr);
  4104. /* Template to remove "const" from "const T*" and just return "T*".
  4105. *
  4106. * We define a nonsense default because otherwise it will fail to instantiate as
  4107. * a function parameter type even in cases where we don't expect any caller to
  4108. * actually match the overload. */
  4109. class CouldntRemoveConst {};
  4110. template <class T> struct remove_constptr { typedef CouldntRemoveConst type; };
  4111. template <class T> struct remove_constptr<const T *> { typedef T *type; };
  4112. /* Template that we use below to remove a template specialization from
  4113. * consideration if it matches a specific type. */
  4114. template <class T, class U> struct disable_if_same { typedef void Type; };
  4115. template <class T> struct disable_if_same<T, T> {};
  4116. template <class T> void DeletePointer(void *p) { delete static_cast<T>(p); }
  4117. template <class T1, class T2>
  4118. struct FirstUnlessVoidOrBool {
  4119. typedef T1 value;
  4120. };
  4121. template <class T2>
  4122. struct FirstUnlessVoidOrBool<void, T2> {
  4123. typedef T2 value;
  4124. };
  4125. template <class T2>
  4126. struct FirstUnlessVoidOrBool<bool, T2> {
  4127. typedef T2 value;
  4128. };
  4129. template<class T, class U>
  4130. struct is_same {
  4131. static bool value;
  4132. };
  4133. template<class T>
  4134. struct is_same<T, T> {
  4135. static bool value;
  4136. };
  4137. template<class T, class U>
  4138. bool is_same<T, U>::value = false;
  4139. template<class T>
  4140. bool is_same<T, T>::value = true;
  4141. /* FuncInfo *******************************************************************/
  4142. /* Info about the user's original, pre-wrapped function. */
  4143. template <class C, class R = void>
  4144. struct FuncInfo {
  4145. /* The type of the closure that the function takes (its first param). */
  4146. typedef C Closure;
  4147. /* The return type. */
  4148. typedef R Return;
  4149. };
  4150. /* Func ***********************************************************************/
  4151. /* Func1, Func2, Func3: Template classes representing a function and its
  4152. * signature.
  4153. *
  4154. * Since the function is a template parameter, calling the function can be
  4155. * inlined at compile-time and does not require a function pointer at runtime.
  4156. * These functions are not bound to a handler data so have no data or cleanup
  4157. * handler. */
  4158. struct UnboundFunc {
  4159. CleanupFunc *GetCleanup() { return nullptr; }
  4160. void *GetData() { return nullptr; }
  4161. };
  4162. template <class R, class P1, R F(P1), class I>
  4163. struct Func1 : public UnboundFunc {
  4164. typedef R Return;
  4165. typedef I FuncInfo;
  4166. static R Call(P1 p1) { return F(p1); }
  4167. };
  4168. template <class R, class P1, class P2, R F(P1, P2), class I>
  4169. struct Func2 : public UnboundFunc {
  4170. typedef R Return;
  4171. typedef I FuncInfo;
  4172. static R Call(P1 p1, P2 p2) { return F(p1, p2); }
  4173. };
  4174. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4175. struct Func3 : public UnboundFunc {
  4176. typedef R Return;
  4177. typedef I FuncInfo;
  4178. static R Call(P1 p1, P2 p2, P3 p3) { return F(p1, p2, p3); }
  4179. };
  4180. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4181. class I>
  4182. struct Func4 : public UnboundFunc {
  4183. typedef R Return;
  4184. typedef I FuncInfo;
  4185. static R Call(P1 p1, P2 p2, P3 p3, P4 p4) { return F(p1, p2, p3, p4); }
  4186. };
  4187. template <class R, class P1, class P2, class P3, class P4, class P5,
  4188. R F(P1, P2, P3, P4, P5), class I>
  4189. struct Func5 : public UnboundFunc {
  4190. typedef R Return;
  4191. typedef I FuncInfo;
  4192. static R Call(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) {
  4193. return F(p1, p2, p3, p4, p5);
  4194. }
  4195. };
  4196. /* BoundFunc ******************************************************************/
  4197. /* BoundFunc2, BoundFunc3: Like Func2/Func3 except also contains a value that
  4198. * shall be bound to the function's second parameter.
  4199. *
  4200. * Note that the second parameter is a const pointer, but our stored bound value
  4201. * is non-const so we can free it when the handlers are destroyed. */
  4202. template <class T>
  4203. struct BoundFunc {
  4204. typedef typename remove_constptr<T>::type MutableP2;
  4205. explicit BoundFunc(MutableP2 data_) : data(data_) {}
  4206. CleanupFunc *GetCleanup() { return &DeletePointer<MutableP2>; }
  4207. MutableP2 GetData() { return data; }
  4208. MutableP2 data;
  4209. };
  4210. template <class R, class P1, class P2, R F(P1, P2), class I>
  4211. struct BoundFunc2 : public BoundFunc<P2> {
  4212. typedef BoundFunc<P2> Base;
  4213. typedef I FuncInfo;
  4214. explicit BoundFunc2(typename Base::MutableP2 arg) : Base(arg) {}
  4215. };
  4216. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I>
  4217. struct BoundFunc3 : public BoundFunc<P2> {
  4218. typedef BoundFunc<P2> Base;
  4219. typedef I FuncInfo;
  4220. explicit BoundFunc3(typename Base::MutableP2 arg) : Base(arg) {}
  4221. };
  4222. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4223. class I>
  4224. struct BoundFunc4 : public BoundFunc<P2> {
  4225. typedef BoundFunc<P2> Base;
  4226. typedef I FuncInfo;
  4227. explicit BoundFunc4(typename Base::MutableP2 arg) : Base(arg) {}
  4228. };
  4229. template <class R, class P1, class P2, class P3, class P4, class P5,
  4230. R F(P1, P2, P3, P4, P5), class I>
  4231. struct BoundFunc5 : public BoundFunc<P2> {
  4232. typedef BoundFunc<P2> Base;
  4233. typedef I FuncInfo;
  4234. explicit BoundFunc5(typename Base::MutableP2 arg) : Base(arg) {}
  4235. };
  4236. /* FuncSig ********************************************************************/
  4237. /* FuncSig1, FuncSig2, FuncSig3: template classes reflecting a function
  4238. * *signature*, but without a specific function attached.
  4239. *
  4240. * These classes contain member functions that can be invoked with a
  4241. * specific function to return a Func/BoundFunc class. */
  4242. template <class R, class P1>
  4243. struct FuncSig1 {
  4244. template <R F(P1)>
  4245. Func1<R, P1, F, FuncInfo<P1, R> > GetFunc() {
  4246. return Func1<R, P1, F, FuncInfo<P1, R> >();
  4247. }
  4248. };
  4249. template <class R, class P1, class P2>
  4250. struct FuncSig2 {
  4251. template <R F(P1, P2)>
  4252. Func2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc() {
  4253. return Func2<R, P1, P2, F, FuncInfo<P1, R> >();
  4254. }
  4255. template <R F(P1, P2)>
  4256. BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> > GetFunc(
  4257. typename remove_constptr<P2>::type param2) {
  4258. return BoundFunc2<R, P1, P2, F, FuncInfo<P1, R> >(param2);
  4259. }
  4260. };
  4261. template <class R, class P1, class P2, class P3>
  4262. struct FuncSig3 {
  4263. template <R F(P1, P2, P3)>
  4264. Func3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc() {
  4265. return Func3<R, P1, P2, P3, F, FuncInfo<P1, R> >();
  4266. }
  4267. template <R F(P1, P2, P3)>
  4268. BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> > GetFunc(
  4269. typename remove_constptr<P2>::type param2) {
  4270. return BoundFunc3<R, P1, P2, P3, F, FuncInfo<P1, R> >(param2);
  4271. }
  4272. };
  4273. template <class R, class P1, class P2, class P3, class P4>
  4274. struct FuncSig4 {
  4275. template <R F(P1, P2, P3, P4)>
  4276. Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc() {
  4277. return Func4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >();
  4278. }
  4279. template <R F(P1, P2, P3, P4)>
  4280. BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> > GetFunc(
  4281. typename remove_constptr<P2>::type param2) {
  4282. return BoundFunc4<R, P1, P2, P3, P4, F, FuncInfo<P1, R> >(param2);
  4283. }
  4284. };
  4285. template <class R, class P1, class P2, class P3, class P4, class P5>
  4286. struct FuncSig5 {
  4287. template <R F(P1, P2, P3, P4, P5)>
  4288. Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc() {
  4289. return Func5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >();
  4290. }
  4291. template <R F(P1, P2, P3, P4, P5)>
  4292. BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> > GetFunc(
  4293. typename remove_constptr<P2>::type param2) {
  4294. return BoundFunc5<R, P1, P2, P3, P4, P5, F, FuncInfo<P1, R> >(param2);
  4295. }
  4296. };
  4297. /* Overloaded template function that can construct the appropriate FuncSig*
  4298. * class given a function pointer by deducing the template parameters. */
  4299. template <class R, class P1>
  4300. inline FuncSig1<R, P1> MatchFunc(R (*f)(P1)) {
  4301. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4302. return FuncSig1<R, P1>();
  4303. }
  4304. template <class R, class P1, class P2>
  4305. inline FuncSig2<R, P1, P2> MatchFunc(R (*f)(P1, P2)) {
  4306. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4307. return FuncSig2<R, P1, P2>();
  4308. }
  4309. template <class R, class P1, class P2, class P3>
  4310. inline FuncSig3<R, P1, P2, P3> MatchFunc(R (*f)(P1, P2, P3)) {
  4311. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4312. return FuncSig3<R, P1, P2, P3>();
  4313. }
  4314. template <class R, class P1, class P2, class P3, class P4>
  4315. inline FuncSig4<R, P1, P2, P3, P4> MatchFunc(R (*f)(P1, P2, P3, P4)) {
  4316. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4317. return FuncSig4<R, P1, P2, P3, P4>();
  4318. }
  4319. template <class R, class P1, class P2, class P3, class P4, class P5>
  4320. inline FuncSig5<R, P1, P2, P3, P4, P5> MatchFunc(R (*f)(P1, P2, P3, P4, P5)) {
  4321. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4322. return FuncSig5<R, P1, P2, P3, P4, P5>();
  4323. }
  4324. /* MethodSig ******************************************************************/
  4325. /* CallMethod*: a function template that calls a given method. */
  4326. template <class R, class C, R (C::*F)()>
  4327. R CallMethod0(C *obj) {
  4328. return ((*obj).*F)();
  4329. }
  4330. template <class R, class C, class P1, R (C::*F)(P1)>
  4331. R CallMethod1(C *obj, P1 arg1) {
  4332. return ((*obj).*F)(arg1);
  4333. }
  4334. template <class R, class C, class P1, class P2, R (C::*F)(P1, P2)>
  4335. R CallMethod2(C *obj, P1 arg1, P2 arg2) {
  4336. return ((*obj).*F)(arg1, arg2);
  4337. }
  4338. template <class R, class C, class P1, class P2, class P3, R (C::*F)(P1, P2, P3)>
  4339. R CallMethod3(C *obj, P1 arg1, P2 arg2, P3 arg3) {
  4340. return ((*obj).*F)(arg1, arg2, arg3);
  4341. }
  4342. template <class R, class C, class P1, class P2, class P3, class P4,
  4343. R (C::*F)(P1, P2, P3, P4)>
  4344. R CallMethod4(C *obj, P1 arg1, P2 arg2, P3 arg3, P4 arg4) {
  4345. return ((*obj).*F)(arg1, arg2, arg3, arg4);
  4346. }
  4347. /* MethodSig: like FuncSig, but for member functions.
  4348. *
  4349. * GetFunc() returns a normal FuncN object, so after calling GetFunc() no
  4350. * more logic is required to special-case methods. */
  4351. template <class R, class C>
  4352. struct MethodSig0 {
  4353. template <R (C::*F)()>
  4354. Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> > GetFunc() {
  4355. return Func1<R, C *, CallMethod0<R, C, F>, FuncInfo<C *, R> >();
  4356. }
  4357. };
  4358. template <class R, class C, class P1>
  4359. struct MethodSig1 {
  4360. template <R (C::*F)(P1)>
  4361. Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc() {
  4362. return Func2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >();
  4363. }
  4364. template <R (C::*F)(P1)>
  4365. BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> > GetFunc(
  4366. typename remove_constptr<P1>::type param1) {
  4367. return BoundFunc2<R, C *, P1, CallMethod1<R, C, P1, F>, FuncInfo<C *, R> >(
  4368. param1);
  4369. }
  4370. };
  4371. template <class R, class C, class P1, class P2>
  4372. struct MethodSig2 {
  4373. template <R (C::*F)(P1, P2)>
  4374. Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4375. GetFunc() {
  4376. return Func3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4377. FuncInfo<C *, R> >();
  4378. }
  4379. template <R (C::*F)(P1, P2)>
  4380. BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>, FuncInfo<C *, R> >
  4381. GetFunc(typename remove_constptr<P1>::type param1) {
  4382. return BoundFunc3<R, C *, P1, P2, CallMethod2<R, C, P1, P2, F>,
  4383. FuncInfo<C *, R> >(param1);
  4384. }
  4385. };
  4386. template <class R, class C, class P1, class P2, class P3>
  4387. struct MethodSig3 {
  4388. template <R (C::*F)(P1, P2, P3)>
  4389. Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>, FuncInfo<C *, R> >
  4390. GetFunc() {
  4391. return Func4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4392. FuncInfo<C *, R> >();
  4393. }
  4394. template <R (C::*F)(P1, P2, P3)>
  4395. BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4396. FuncInfo<C *, R> >
  4397. GetFunc(typename remove_constptr<P1>::type param1) {
  4398. return BoundFunc4<R, C *, P1, P2, P3, CallMethod3<R, C, P1, P2, P3, F>,
  4399. FuncInfo<C *, R> >(param1);
  4400. }
  4401. };
  4402. template <class R, class C, class P1, class P2, class P3, class P4>
  4403. struct MethodSig4 {
  4404. template <R (C::*F)(P1, P2, P3, P4)>
  4405. Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4406. FuncInfo<C *, R> >
  4407. GetFunc() {
  4408. return Func5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4409. FuncInfo<C *, R> >();
  4410. }
  4411. template <R (C::*F)(P1, P2, P3, P4)>
  4412. BoundFunc5<R, C *, P1, P2, P3, P4, CallMethod4<R, C, P1, P2, P3, P4, F>,
  4413. FuncInfo<C *, R> >
  4414. GetFunc(typename remove_constptr<P1>::type param1) {
  4415. return BoundFunc5<R, C *, P1, P2, P3, P4,
  4416. CallMethod4<R, C, P1, P2, P3, P4, F>, FuncInfo<C *, R> >(
  4417. param1);
  4418. }
  4419. };
  4420. template <class R, class C>
  4421. inline MethodSig0<R, C> MatchFunc(R (C::*f)()) {
  4422. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4423. return MethodSig0<R, C>();
  4424. }
  4425. template <class R, class C, class P1>
  4426. inline MethodSig1<R, C, P1> MatchFunc(R (C::*f)(P1)) {
  4427. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4428. return MethodSig1<R, C, P1>();
  4429. }
  4430. template <class R, class C, class P1, class P2>
  4431. inline MethodSig2<R, C, P1, P2> MatchFunc(R (C::*f)(P1, P2)) {
  4432. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4433. return MethodSig2<R, C, P1, P2>();
  4434. }
  4435. template <class R, class C, class P1, class P2, class P3>
  4436. inline MethodSig3<R, C, P1, P2, P3> MatchFunc(R (C::*f)(P1, P2, P3)) {
  4437. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4438. return MethodSig3<R, C, P1, P2, P3>();
  4439. }
  4440. template <class R, class C, class P1, class P2, class P3, class P4>
  4441. inline MethodSig4<R, C, P1, P2, P3, P4> MatchFunc(R (C::*f)(P1, P2, P3, P4)) {
  4442. UPB_UNUSED(f); /* Only used for template parameter deduction. */
  4443. return MethodSig4<R, C, P1, P2, P3, P4>();
  4444. }
  4445. /* MaybeWrapReturn ************************************************************/
  4446. /* Template class that attempts to wrap the return value of the function so it
  4447. * matches the expected type. There are two main adjustments it may make:
  4448. *
  4449. * 1. If the function returns void, make it return the expected type and with
  4450. * a value that always indicates success.
  4451. * 2. If the function returns bool, make it return the expected type with a
  4452. * value that indicates success or failure.
  4453. *
  4454. * The "expected type" for return is:
  4455. * 1. void* for start handlers. If the closure parameter has a different type
  4456. * we will cast it to void* for the return in the success case.
  4457. * 2. size_t for string buffer handlers.
  4458. * 3. bool for everything else. */
  4459. /* Template parameters are FuncN type and desired return type. */
  4460. template <class F, class R, class Enable = void>
  4461. struct MaybeWrapReturn;
  4462. /* If the return type matches, return the given function unwrapped. */
  4463. template <class F>
  4464. struct MaybeWrapReturn<F, typename F::Return> {
  4465. typedef F Func;
  4466. };
  4467. /* Function wrapper that munges the return value from void to (bool)true. */
  4468. template <class P1, class P2, void F(P1, P2)>
  4469. bool ReturnTrue2(P1 p1, P2 p2) {
  4470. F(p1, p2);
  4471. return true;
  4472. }
  4473. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4474. bool ReturnTrue3(P1 p1, P2 p2, P3 p3) {
  4475. F(p1, p2, p3);
  4476. return true;
  4477. }
  4478. /* Function wrapper that munges the return value from void to (void*)arg1 */
  4479. template <class P1, class P2, void F(P1, P2)>
  4480. void *ReturnClosure2(P1 p1, P2 p2) {
  4481. F(p1, p2);
  4482. return p1;
  4483. }
  4484. template <class P1, class P2, class P3, void F(P1, P2, P3)>
  4485. void *ReturnClosure3(P1 p1, P2 p2, P3 p3) {
  4486. F(p1, p2, p3);
  4487. return p1;
  4488. }
  4489. /* Function wrapper that munges the return value from R to void*. */
  4490. template <class R, class P1, class P2, R F(P1, P2)>
  4491. void *CastReturnToVoidPtr2(P1 p1, P2 p2) {
  4492. return F(p1, p2);
  4493. }
  4494. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4495. void *CastReturnToVoidPtr3(P1 p1, P2 p2, P3 p3) {
  4496. return F(p1, p2, p3);
  4497. }
  4498. /* Function wrapper that munges the return value from bool to void*. */
  4499. template <class P1, class P2, bool F(P1, P2)>
  4500. void *ReturnClosureOrBreak2(P1 p1, P2 p2) {
  4501. return F(p1, p2) ? p1 : UPB_BREAK;
  4502. }
  4503. template <class P1, class P2, class P3, bool F(P1, P2, P3)>
  4504. void *ReturnClosureOrBreak3(P1 p1, P2 p2, P3 p3) {
  4505. return F(p1, p2, p3) ? p1 : UPB_BREAK;
  4506. }
  4507. /* For the string callback, which takes five params, returns the size param. */
  4508. template <class P1, class P2,
  4509. void F(P1, P2, const char *, size_t, const upb_bufhandle *)>
  4510. size_t ReturnStringLen(P1 p1, P2 p2, const char *p3, size_t p4,
  4511. const upb_bufhandle *p5) {
  4512. F(p1, p2, p3, p4, p5);
  4513. return p4;
  4514. }
  4515. /* For the string callback, which takes five params, returns the size param or
  4516. * zero. */
  4517. template <class P1, class P2,
  4518. bool F(P1, P2, const char *, size_t, const upb_bufhandle *)>
  4519. size_t ReturnNOr0(P1 p1, P2 p2, const char *p3, size_t p4,
  4520. const upb_bufhandle *p5) {
  4521. return F(p1, p2, p3, p4, p5) ? p4 : 0;
  4522. }
  4523. /* If we have a function returning void but want a function returning bool, wrap
  4524. * it in a function that returns true. */
  4525. template <class P1, class P2, void F(P1, P2), class I>
  4526. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, bool> {
  4527. typedef Func2<bool, P1, P2, ReturnTrue2<P1, P2, F>, I> Func;
  4528. };
  4529. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4530. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, bool> {
  4531. typedef Func3<bool, P1, P2, P3, ReturnTrue3<P1, P2, P3, F>, I> Func;
  4532. };
  4533. /* If our function returns void but we want one returning void*, wrap it in a
  4534. * function that returns the first argument. */
  4535. template <class P1, class P2, void F(P1, P2), class I>
  4536. struct MaybeWrapReturn<Func2<void, P1, P2, F, I>, void *> {
  4537. typedef Func2<void *, P1, P2, ReturnClosure2<P1, P2, F>, I> Func;
  4538. };
  4539. template <class P1, class P2, class P3, void F(P1, P2, P3), class I>
  4540. struct MaybeWrapReturn<Func3<void, P1, P2, P3, F, I>, void *> {
  4541. typedef Func3<void *, P1, P2, P3, ReturnClosure3<P1, P2, P3, F>, I> Func;
  4542. };
  4543. /* If our function returns R* but we want one returning void*, wrap it in a
  4544. * function that casts to void*. */
  4545. template <class R, class P1, class P2, R *F(P1, P2), class I>
  4546. struct MaybeWrapReturn<Func2<R *, P1, P2, F, I>, void *,
  4547. typename disable_if_same<R *, void *>::Type> {
  4548. typedef Func2<void *, P1, P2, CastReturnToVoidPtr2<R *, P1, P2, F>, I> Func;
  4549. };
  4550. template <class R, class P1, class P2, class P3, R *F(P1, P2, P3), class I>
  4551. struct MaybeWrapReturn<Func3<R *, P1, P2, P3, F, I>, void *,
  4552. typename disable_if_same<R *, void *>::Type> {
  4553. typedef Func3<void *, P1, P2, P3, CastReturnToVoidPtr3<R *, P1, P2, P3, F>, I>
  4554. Func;
  4555. };
  4556. /* If our function returns bool but we want one returning void*, wrap it in a
  4557. * function that returns either the first param or UPB_BREAK. */
  4558. template <class P1, class P2, bool F(P1, P2), class I>
  4559. struct MaybeWrapReturn<Func2<bool, P1, P2, F, I>, void *> {
  4560. typedef Func2<void *, P1, P2, ReturnClosureOrBreak2<P1, P2, F>, I> Func;
  4561. };
  4562. template <class P1, class P2, class P3, bool F(P1, P2, P3), class I>
  4563. struct MaybeWrapReturn<Func3<bool, P1, P2, P3, F, I>, void *> {
  4564. typedef Func3<void *, P1, P2, P3, ReturnClosureOrBreak3<P1, P2, P3, F>, I>
  4565. Func;
  4566. };
  4567. /* If our function returns void but we want one returning size_t, wrap it in a
  4568. * function that returns the size argument. */
  4569. template <class P1, class P2,
  4570. void F(P1, P2, const char *, size_t, const upb_bufhandle *), class I>
  4571. struct MaybeWrapReturn<
  4572. Func5<void, P1, P2, const char *, size_t, const upb_bufhandle *, F, I>,
  4573. size_t> {
  4574. typedef Func5<size_t, P1, P2, const char *, size_t, const upb_bufhandle *,
  4575. ReturnStringLen<P1, P2, F>, I> Func;
  4576. };
  4577. /* If our function returns bool but we want one returning size_t, wrap it in a
  4578. * function that returns either 0 or the buf size. */
  4579. template <class P1, class P2,
  4580. bool F(P1, P2, const char *, size_t, const upb_bufhandle *), class I>
  4581. struct MaybeWrapReturn<
  4582. Func5<bool, P1, P2, const char *, size_t, const upb_bufhandle *, F, I>,
  4583. size_t> {
  4584. typedef Func5<size_t, P1, P2, const char *, size_t, const upb_bufhandle *,
  4585. ReturnNOr0<P1, P2, F>, I> Func;
  4586. };
  4587. /* ConvertParams **************************************************************/
  4588. /* Template class that converts the function parameters if necessary, and
  4589. * ignores the HandlerData parameter if appropriate.
  4590. *
  4591. * Template parameter is the are FuncN function type. */
  4592. template <class F, class T>
  4593. struct ConvertParams;
  4594. /* Function that discards the handler data parameter. */
  4595. template <class R, class P1, R F(P1)>
  4596. R IgnoreHandlerData2(void *p1, const void *hd) {
  4597. UPB_UNUSED(hd);
  4598. return F(static_cast<P1>(p1));
  4599. }
  4600. template <class R, class P1, class P2Wrapper, class P2Wrapped,
  4601. R F(P1, P2Wrapped)>
  4602. R IgnoreHandlerData3(void *p1, const void *hd, P2Wrapper p2) {
  4603. UPB_UNUSED(hd);
  4604. return F(static_cast<P1>(p1), p2);
  4605. }
  4606. template <class R, class P1, class P2, class P3, R F(P1, P2, P3)>
  4607. R IgnoreHandlerData4(void *p1, const void *hd, P2 p2, P3 p3) {
  4608. UPB_UNUSED(hd);
  4609. return F(static_cast<P1>(p1), p2, p3);
  4610. }
  4611. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4)>
  4612. R IgnoreHandlerData5(void *p1, const void *hd, P2 p2, P3 p3, P4 p4) {
  4613. UPB_UNUSED(hd);
  4614. return F(static_cast<P1>(p1), p2, p3, p4);
  4615. }
  4616. template <class R, class P1, R F(P1, const char*, size_t)>
  4617. R IgnoreHandlerDataIgnoreHandle(void *p1, const void *hd, const char *p2,
  4618. size_t p3, const upb_bufhandle *handle) {
  4619. UPB_UNUSED(hd);
  4620. UPB_UNUSED(handle);
  4621. return F(static_cast<P1>(p1), p2, p3);
  4622. }
  4623. /* Function that casts the handler data parameter. */
  4624. template <class R, class P1, class P2, R F(P1, P2)>
  4625. R CastHandlerData2(void *c, const void *hd) {
  4626. return F(static_cast<P1>(c), static_cast<P2>(hd));
  4627. }
  4628. template <class R, class P1, class P2, class P3Wrapper, class P3Wrapped,
  4629. R F(P1, P2, P3Wrapped)>
  4630. R CastHandlerData3(void *c, const void *hd, P3Wrapper p3) {
  4631. return F(static_cast<P1>(c), static_cast<P2>(hd), p3);
  4632. }
  4633. template <class R, class P1, class P2, class P3, class P4, class P5,
  4634. R F(P1, P2, P3, P4, P5)>
  4635. R CastHandlerData5(void *c, const void *hd, P3 p3, P4 p4, P5 p5) {
  4636. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4, p5);
  4637. }
  4638. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t)>
  4639. R CastHandlerDataIgnoreHandle(void *c, const void *hd, const char *p3,
  4640. size_t p4, const upb_bufhandle *handle) {
  4641. UPB_UNUSED(handle);
  4642. return F(static_cast<P1>(c), static_cast<P2>(hd), p3, p4);
  4643. }
  4644. /* For unbound functions, ignore the handler data. */
  4645. template <class R, class P1, R F(P1), class I, class T>
  4646. struct ConvertParams<Func1<R, P1, F, I>, T> {
  4647. typedef Func2<R, void *, const void *, IgnoreHandlerData2<R, P1, F>, I> Func;
  4648. };
  4649. template <class R, class P1, class P2, R F(P1, P2), class I,
  4650. class R2, class P1_2, class P2_2, class P3_2>
  4651. struct ConvertParams<Func2<R, P1, P2, F, I>,
  4652. R2 (*)(P1_2, P2_2, P3_2)> {
  4653. typedef Func3<R, void *, const void *, P3_2,
  4654. IgnoreHandlerData3<R, P1, P3_2, P2, F>, I> Func;
  4655. };
  4656. /* For StringBuffer only; this ignores both the handler data and the
  4657. * upb_bufhandle. */
  4658. template <class R, class P1, R F(P1, const char *, size_t), class I, class T>
  4659. struct ConvertParams<Func3<R, P1, const char *, size_t, F, I>, T> {
  4660. typedef Func5<R, void *, const void *, const char *, size_t,
  4661. const upb_bufhandle *, IgnoreHandlerDataIgnoreHandle<R, P1, F>,
  4662. I> Func;
  4663. };
  4664. template <class R, class P1, class P2, class P3, class P4, R F(P1, P2, P3, P4),
  4665. class I, class T>
  4666. struct ConvertParams<Func4<R, P1, P2, P3, P4, F, I>, T> {
  4667. typedef Func5<R, void *, const void *, P2, P3, P4,
  4668. IgnoreHandlerData5<R, P1, P2, P3, P4, F>, I> Func;
  4669. };
  4670. /* For bound functions, cast the handler data. */
  4671. template <class R, class P1, class P2, R F(P1, P2), class I, class T>
  4672. struct ConvertParams<BoundFunc2<R, P1, P2, F, I>, T> {
  4673. typedef Func2<R, void *, const void *, CastHandlerData2<R, P1, P2, F>, I>
  4674. Func;
  4675. };
  4676. template <class R, class P1, class P2, class P3, R F(P1, P2, P3), class I,
  4677. class R2, class P1_2, class P2_2, class P3_2>
  4678. struct ConvertParams<BoundFunc3<R, P1, P2, P3, F, I>,
  4679. R2 (*)(P1_2, P2_2, P3_2)> {
  4680. typedef Func3<R, void *, const void *, P3_2,
  4681. CastHandlerData3<R, P1, P2, P3_2, P3, F>, I> Func;
  4682. };
  4683. /* For StringBuffer only; this ignores the upb_bufhandle. */
  4684. template <class R, class P1, class P2, R F(P1, P2, const char *, size_t),
  4685. class I, class T>
  4686. struct ConvertParams<BoundFunc4<R, P1, P2, const char *, size_t, F, I>, T> {
  4687. typedef Func5<R, void *, const void *, const char *, size_t,
  4688. const upb_bufhandle *,
  4689. CastHandlerDataIgnoreHandle<R, P1, P2, F>, I>
  4690. Func;
  4691. };
  4692. template <class R, class P1, class P2, class P3, class P4, class P5,
  4693. R F(P1, P2, P3, P4, P5), class I, class T>
  4694. struct ConvertParams<BoundFunc5<R, P1, P2, P3, P4, P5, F, I>, T> {
  4695. typedef Func5<R, void *, const void *, P3, P4, P5,
  4696. CastHandlerData5<R, P1, P2, P3, P4, P5, F>, I> Func;
  4697. };
  4698. /* utype/ltype are upper/lower-case, ctype is canonical C type, vtype is
  4699. * variant C type. */
  4700. #define TYPE_METHODS(utype, ltype, ctype, vtype) \
  4701. template <> \
  4702. struct CanonicalType<vtype> { \
  4703. typedef ctype Type; \
  4704. }; \
  4705. template <> \
  4706. inline bool HandlersPtr::SetValueHandler<vtype>( \
  4707. FieldDefPtr f, const HandlersPtr::utype##Handler &handler) { \
  4708. handler.AddCleanup(ptr()); \
  4709. return upb_handlers_set##ltype(ptr(), f.ptr(), handler.handler(), \
  4710. &handler.attr()); \
  4711. }
  4712. TYPE_METHODS(Double, double, double, double)
  4713. TYPE_METHODS(Float, float, float, float)
  4714. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64_T)
  4715. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32_T)
  4716. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64_T)
  4717. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32_T)
  4718. TYPE_METHODS(Bool, bool, bool, bool)
  4719. #ifdef UPB_TWO_32BIT_TYPES
  4720. TYPE_METHODS(Int32, int32, int32_t, UPB_INT32ALT_T)
  4721. TYPE_METHODS(UInt32, uint32, uint32_t, UPB_UINT32ALT_T)
  4722. #endif
  4723. #ifdef UPB_TWO_64BIT_TYPES
  4724. TYPE_METHODS(Int64, int64, int64_t, UPB_INT64ALT_T)
  4725. TYPE_METHODS(UInt64, uint64, uint64_t, UPB_UINT64ALT_T)
  4726. #endif
  4727. #undef TYPE_METHODS
  4728. template <> struct CanonicalType<Status*> {
  4729. typedef Status* Type;
  4730. };
  4731. template <class F> struct ReturnOf;
  4732. template <class R, class P1, class P2>
  4733. struct ReturnOf<R (*)(P1, P2)> {
  4734. typedef R Return;
  4735. };
  4736. template <class R, class P1, class P2, class P3>
  4737. struct ReturnOf<R (*)(P1, P2, P3)> {
  4738. typedef R Return;
  4739. };
  4740. template <class R, class P1, class P2, class P3, class P4>
  4741. struct ReturnOf<R (*)(P1, P2, P3, P4)> {
  4742. typedef R Return;
  4743. };
  4744. template <class R, class P1, class P2, class P3, class P4, class P5>
  4745. struct ReturnOf<R (*)(P1, P2, P3, P4, P5)> {
  4746. typedef R Return;
  4747. };
  4748. template <class T>
  4749. template <class F>
  4750. inline Handler<T>::Handler(F func)
  4751. : registered_(false),
  4752. cleanup_data_(func.GetData()),
  4753. cleanup_func_(func.GetCleanup()) {
  4754. attr_.handler_data = func.GetData();
  4755. typedef typename ReturnOf<T>::Return Return;
  4756. typedef typename ConvertParams<F, T>::Func ConvertedParamsFunc;
  4757. typedef typename MaybeWrapReturn<ConvertedParamsFunc, Return>::Func
  4758. ReturnWrappedFunc;
  4759. handler_ = ReturnWrappedFunc().Call;
  4760. /* Set attributes based on what templates can statically tell us about the
  4761. * user's function. */
  4762. /* If the original function returns void, then we know that we wrapped it to
  4763. * always return ok. */
  4764. bool always_ok = is_same<typename F::FuncInfo::Return, void>::value;
  4765. attr_.alwaysok = always_ok;
  4766. /* Closure parameter and return type. */
  4767. attr_.closure_type = UniquePtrForType<typename F::FuncInfo::Closure>();
  4768. /* We use the closure type (from the first parameter) if the return type is
  4769. * void or bool, since these are the two cases we wrap to return the closure's
  4770. * type anyway.
  4771. *
  4772. * This is all nonsense for non START* handlers, but it doesn't matter because
  4773. * in that case the value will be ignored. */
  4774. typedef typename FirstUnlessVoidOrBool<typename F::FuncInfo::Return,
  4775. typename F::FuncInfo::Closure>::value
  4776. EffectiveReturn;
  4777. attr_.return_closure_type = UniquePtrForType<EffectiveReturn>();
  4778. }
  4779. template <class T>
  4780. inline void Handler<T>::AddCleanup(upb_handlers* h) const {
  4781. UPB_ASSERT(!registered_);
  4782. registered_ = true;
  4783. if (cleanup_func_) {
  4784. bool ok = upb_handlers_addcleanup(h, cleanup_data_, cleanup_func_);
  4785. UPB_ASSERT(ok);
  4786. }
  4787. }
  4788. } /* namespace upb */
  4789. #endif /* __cplusplus */
  4790. #undef UPB_TWO_32BIT_TYPES
  4791. #undef UPB_TWO_64BIT_TYPES
  4792. #undef UPB_INT32_T
  4793. #undef UPB_UINT32_T
  4794. #undef UPB_INT32ALT_T
  4795. #undef UPB_UINT32ALT_T
  4796. #undef UPB_INT64_T
  4797. #undef UPB_UINT64_T
  4798. #undef UPB_INT64ALT_T
  4799. #undef UPB_UINT64ALT_T
  4800. #endif /* UPB_HANDLERS_INL_H_ */
  4801. #endif /* UPB_HANDLERS_H */
  4802. /*
  4803. ** upb::Sink (upb_sink)
  4804. ** upb::BytesSink (upb_bytessink)
  4805. **
  4806. ** A upb_sink is an object that binds a upb_handlers object to some runtime
  4807. ** state. It is the object that can actually receive data via the upb_handlers
  4808. ** interface.
  4809. **
  4810. ** Unlike upb_def and upb_handlers, upb_sink is never frozen, immutable, or
  4811. ** thread-safe. You can create as many of them as you want, but each one may
  4812. ** only be used in a single thread at a time.
  4813. **
  4814. ** If we compare with class-based OOP, a you can think of a upb_def as an
  4815. ** abstract base class, a upb_handlers as a concrete derived class, and a
  4816. ** upb_sink as an object (class instance).
  4817. */
  4818. #ifndef UPB_SINK_H
  4819. #define UPB_SINK_H
  4820. #ifdef __cplusplus
  4821. namespace upb {
  4822. class BytesSink;
  4823. class Sink;
  4824. }
  4825. #endif
  4826. /* upb_sink *******************************************************************/
  4827. #ifdef __cplusplus
  4828. extern "C" {
  4829. #endif
  4830. typedef struct {
  4831. const upb_handlers *handlers;
  4832. void *closure;
  4833. } upb_sink;
  4834. #define PUTVAL(type, ctype) \
  4835. UPB_INLINE bool upb_sink_put##type(upb_sink s, upb_selector_t sel, \
  4836. ctype val) { \
  4837. typedef upb_##type##_handlerfunc functype; \
  4838. functype *func; \
  4839. const void *hd; \
  4840. if (!s.handlers) return true; \
  4841. func = (functype *)upb_handlers_gethandler(s.handlers, sel, &hd); \
  4842. if (!func) return true; \
  4843. return func(s.closure, hd, val); \
  4844. }
  4845. PUTVAL(int32, int32_t)
  4846. PUTVAL(int64, int64_t)
  4847. PUTVAL(uint32, uint32_t)
  4848. PUTVAL(uint64, uint64_t)
  4849. PUTVAL(float, float)
  4850. PUTVAL(double, double)
  4851. PUTVAL(bool, bool)
  4852. #undef PUTVAL
  4853. UPB_INLINE void upb_sink_reset(upb_sink *s, const upb_handlers *h, void *c) {
  4854. s->handlers = h;
  4855. s->closure = c;
  4856. }
  4857. UPB_INLINE size_t upb_sink_putstring(upb_sink s, upb_selector_t sel,
  4858. const char *buf, size_t n,
  4859. const upb_bufhandle *handle) {
  4860. typedef upb_string_handlerfunc func;
  4861. func *handler;
  4862. const void *hd;
  4863. if (!s.handlers) return n;
  4864. handler = (func *)upb_handlers_gethandler(s.handlers, sel, &hd);
  4865. if (!handler) return n;
  4866. return handler(s.closure, hd, buf, n, handle);
  4867. }
  4868. UPB_INLINE bool upb_sink_putunknown(upb_sink s, const char *buf, size_t n) {
  4869. typedef upb_unknown_handlerfunc func;
  4870. func *handler;
  4871. const void *hd;
  4872. if (!s.handlers) return true;
  4873. handler =
  4874. (func *)upb_handlers_gethandler(s.handlers, UPB_UNKNOWN_SELECTOR, &hd);
  4875. if (!handler) return n;
  4876. return handler(s.closure, hd, buf, n);
  4877. }
  4878. UPB_INLINE bool upb_sink_startmsg(upb_sink s) {
  4879. typedef upb_startmsg_handlerfunc func;
  4880. func *startmsg;
  4881. const void *hd;
  4882. if (!s.handlers) return true;
  4883. startmsg =
  4884. (func *)upb_handlers_gethandler(s.handlers, UPB_STARTMSG_SELECTOR, &hd);
  4885. if (!startmsg) return true;
  4886. return startmsg(s.closure, hd);
  4887. }
  4888. UPB_INLINE bool upb_sink_endmsg(upb_sink s, upb_status *status) {
  4889. typedef upb_endmsg_handlerfunc func;
  4890. func *endmsg;
  4891. const void *hd;
  4892. if (!s.handlers) return true;
  4893. endmsg =
  4894. (func *)upb_handlers_gethandler(s.handlers, UPB_ENDMSG_SELECTOR, &hd);
  4895. if (!endmsg) return true;
  4896. return endmsg(s.closure, hd, status);
  4897. }
  4898. UPB_INLINE bool upb_sink_startseq(upb_sink s, upb_selector_t sel,
  4899. upb_sink *sub) {
  4900. typedef upb_startfield_handlerfunc func;
  4901. func *startseq;
  4902. const void *hd;
  4903. sub->closure = s.closure;
  4904. sub->handlers = s.handlers;
  4905. if (!s.handlers) return true;
  4906. startseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4907. if (!startseq) return true;
  4908. sub->closure = startseq(s.closure, hd);
  4909. return sub->closure ? true : false;
  4910. }
  4911. UPB_INLINE bool upb_sink_endseq(upb_sink s, upb_selector_t sel) {
  4912. typedef upb_endfield_handlerfunc func;
  4913. func *endseq;
  4914. const void *hd;
  4915. if (!s.handlers) return true;
  4916. endseq = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4917. if (!endseq) return true;
  4918. return endseq(s.closure, hd);
  4919. }
  4920. UPB_INLINE bool upb_sink_startstr(upb_sink s, upb_selector_t sel,
  4921. size_t size_hint, upb_sink *sub) {
  4922. typedef upb_startstr_handlerfunc func;
  4923. func *startstr;
  4924. const void *hd;
  4925. sub->closure = s.closure;
  4926. sub->handlers = s.handlers;
  4927. if (!s.handlers) return true;
  4928. startstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4929. if (!startstr) return true;
  4930. sub->closure = startstr(s.closure, hd, size_hint);
  4931. return sub->closure ? true : false;
  4932. }
  4933. UPB_INLINE bool upb_sink_endstr(upb_sink s, upb_selector_t sel) {
  4934. typedef upb_endfield_handlerfunc func;
  4935. func *endstr;
  4936. const void *hd;
  4937. if (!s.handlers) return true;
  4938. endstr = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4939. if (!endstr) return true;
  4940. return endstr(s.closure, hd);
  4941. }
  4942. UPB_INLINE bool upb_sink_startsubmsg(upb_sink s, upb_selector_t sel,
  4943. upb_sink *sub) {
  4944. typedef upb_startfield_handlerfunc func;
  4945. func *startsubmsg;
  4946. const void *hd;
  4947. sub->closure = s.closure;
  4948. if (!s.handlers) {
  4949. sub->handlers = NULL;
  4950. return true;
  4951. }
  4952. sub->handlers = upb_handlers_getsubhandlers_sel(s.handlers, sel);
  4953. startsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4954. if (!startsubmsg) return true;
  4955. sub->closure = startsubmsg(s.closure, hd);
  4956. return sub->closure ? true : false;
  4957. }
  4958. UPB_INLINE bool upb_sink_endsubmsg(upb_sink s, upb_selector_t sel) {
  4959. typedef upb_endfield_handlerfunc func;
  4960. func *endsubmsg;
  4961. const void *hd;
  4962. if (!s.handlers) return true;
  4963. endsubmsg = (func*)upb_handlers_gethandler(s.handlers, sel, &hd);
  4964. if (!endsubmsg) return s.closure;
  4965. return endsubmsg(s.closure, hd);
  4966. }
  4967. #ifdef __cplusplus
  4968. } /* extern "C" */
  4969. /* A upb::Sink is an object that binds a upb::Handlers object to some runtime
  4970. * state. It represents an endpoint to which data can be sent.
  4971. *
  4972. * TODO(haberman): right now all of these functions take selectors. Should they
  4973. * take selectorbase instead?
  4974. *
  4975. * ie. instead of calling:
  4976. * sink->StartString(FOO_FIELD_START_STRING, ...)
  4977. * a selector base would let you say:
  4978. * sink->StartString(FOO_FIELD, ...)
  4979. *
  4980. * This would make call sites a little nicer and require emitting fewer selector
  4981. * definitions in .h files.
  4982. *
  4983. * But the current scheme has the benefit that you can retrieve a function
  4984. * pointer for any handler with handlers->GetHandler(selector), without having
  4985. * to have a separate GetHandler() function for each handler type. The JIT
  4986. * compiler uses this. To accommodate we'd have to expose a separate
  4987. * GetHandler() for every handler type.
  4988. *
  4989. * Also to ponder: selectors right now are independent of a specific Handlers
  4990. * instance. In other words, they allocate a number to every possible handler
  4991. * that *could* be registered, without knowing anything about what handlers
  4992. * *are* registered. That means that using selectors as table offsets prohibits
  4993. * us from compacting the handler table at Freeze() time. If the table is very
  4994. * sparse, this could be wasteful.
  4995. *
  4996. * Having another selector-like thing that is specific to a Handlers instance
  4997. * would allow this compacting, but then it would be impossible to write code
  4998. * ahead-of-time that can be bound to any Handlers instance at runtime. For
  4999. * example, a .proto file parser written as straight C will not know what
  5000. * Handlers it will be bound to, so when it calls sink->StartString() what
  5001. * selector will it pass? It needs a selector like we have today, that is
  5002. * independent of any particular upb::Handlers.
  5003. *
  5004. * Is there a way then to allow Handlers table compaction? */
  5005. class upb::Sink {
  5006. public:
  5007. /* Constructor with no initialization; must be Reset() before use. */
  5008. Sink() {}
  5009. Sink(const Sink&) = default;
  5010. Sink& operator=(const Sink&) = default;
  5011. Sink(const upb_sink& sink) : sink_(sink) {}
  5012. Sink &operator=(const upb_sink &sink) {
  5013. sink_ = sink;
  5014. return *this;
  5015. }
  5016. upb_sink sink() { return sink_; }
  5017. /* Constructs a new sink for the given frozen handlers and closure.
  5018. *
  5019. * TODO: once the Handlers know the expected closure type, verify that T
  5020. * matches it. */
  5021. template <class T> Sink(const upb_handlers* handlers, T* closure) {
  5022. Reset(handlers, closure);
  5023. }
  5024. upb_sink* ptr() { return &sink_; }
  5025. /* Resets the value of the sink. */
  5026. template <class T> void Reset(const upb_handlers* handlers, T* closure) {
  5027. upb_sink_reset(&sink_, handlers, closure);
  5028. }
  5029. /* Returns the top-level object that is bound to this sink.
  5030. *
  5031. * TODO: once the Handlers know the expected closure type, verify that T
  5032. * matches it. */
  5033. template <class T> T* GetObject() const {
  5034. return static_cast<T*>(sink_.closure);
  5035. }
  5036. /* Functions for pushing data into the sink.
  5037. *
  5038. * These return false if processing should stop (either due to error or just
  5039. * to suspend).
  5040. *
  5041. * These may not be called from within one of the same sink's handlers (in
  5042. * other words, handlers are not re-entrant). */
  5043. /* Should be called at the start and end of every message; both the top-level
  5044. * message and submessages. This means that submessages should use the
  5045. * following sequence:
  5046. * sink->StartSubMessage(startsubmsg_selector);
  5047. * sink->StartMessage();
  5048. * // ...
  5049. * sink->EndMessage(&status);
  5050. * sink->EndSubMessage(endsubmsg_selector); */
  5051. bool StartMessage() { return upb_sink_startmsg(sink_); }
  5052. bool EndMessage(upb_status *status) {
  5053. return upb_sink_endmsg(sink_, status);
  5054. }
  5055. /* Putting of individual values. These work for both repeated and
  5056. * non-repeated fields, but for repeated fields you must wrap them in
  5057. * calls to StartSequence()/EndSequence(). */
  5058. bool PutInt32(HandlersPtr::Selector s, int32_t val) {
  5059. return upb_sink_putint32(sink_, s, val);
  5060. }
  5061. bool PutInt64(HandlersPtr::Selector s, int64_t val) {
  5062. return upb_sink_putint64(sink_, s, val);
  5063. }
  5064. bool PutUInt32(HandlersPtr::Selector s, uint32_t val) {
  5065. return upb_sink_putuint32(sink_, s, val);
  5066. }
  5067. bool PutUInt64(HandlersPtr::Selector s, uint64_t val) {
  5068. return upb_sink_putuint64(sink_, s, val);
  5069. }
  5070. bool PutFloat(HandlersPtr::Selector s, float val) {
  5071. return upb_sink_putfloat(sink_, s, val);
  5072. }
  5073. bool PutDouble(HandlersPtr::Selector s, double val) {
  5074. return upb_sink_putdouble(sink_, s, val);
  5075. }
  5076. bool PutBool(HandlersPtr::Selector s, bool val) {
  5077. return upb_sink_putbool(sink_, s, val);
  5078. }
  5079. /* Putting of string/bytes values. Each string can consist of zero or more
  5080. * non-contiguous buffers of data.
  5081. *
  5082. * For StartString(), the function will write a sink for the string to "sub."
  5083. * The sub-sink must be used for any/all PutStringBuffer() calls. */
  5084. bool StartString(HandlersPtr::Selector s, size_t size_hint, Sink* sub) {
  5085. upb_sink sub_c;
  5086. bool ret = upb_sink_startstr(sink_, s, size_hint, &sub_c);
  5087. *sub = sub_c;
  5088. return ret;
  5089. }
  5090. size_t PutStringBuffer(HandlersPtr::Selector s, const char *buf, size_t len,
  5091. const upb_bufhandle *handle) {
  5092. return upb_sink_putstring(sink_, s, buf, len, handle);
  5093. }
  5094. bool EndString(HandlersPtr::Selector s) {
  5095. return upb_sink_endstr(sink_, s);
  5096. }
  5097. /* For submessage fields.
  5098. *
  5099. * For StartSubMessage(), the function will write a sink for the string to
  5100. * "sub." The sub-sink must be used for any/all handlers called within the
  5101. * submessage. */
  5102. bool StartSubMessage(HandlersPtr::Selector s, Sink* sub) {
  5103. upb_sink sub_c;
  5104. bool ret = upb_sink_startsubmsg(sink_, s, &sub_c);
  5105. *sub = sub_c;
  5106. return ret;
  5107. }
  5108. bool EndSubMessage(HandlersPtr::Selector s) {
  5109. return upb_sink_endsubmsg(sink_, s);
  5110. }
  5111. /* For repeated fields of any type, the sequence of values must be wrapped in
  5112. * these calls.
  5113. *
  5114. * For StartSequence(), the function will write a sink for the string to
  5115. * "sub." The sub-sink must be used for any/all handlers called within the
  5116. * sequence. */
  5117. bool StartSequence(HandlersPtr::Selector s, Sink* sub) {
  5118. upb_sink sub_c;
  5119. bool ret = upb_sink_startseq(sink_, s, &sub_c);
  5120. *sub = sub_c;
  5121. return ret;
  5122. }
  5123. bool EndSequence(HandlersPtr::Selector s) {
  5124. return upb_sink_endseq(sink_, s);
  5125. }
  5126. /* Copy and assign specifically allowed.
  5127. * We don't even bother making these members private because so many
  5128. * functions need them and this is mainly just a dumb data container anyway.
  5129. */
  5130. private:
  5131. upb_sink sink_;
  5132. };
  5133. #endif /* __cplusplus */
  5134. /* upb_bytessink **************************************************************/
  5135. typedef struct {
  5136. const upb_byteshandler *handler;
  5137. void *closure;
  5138. } upb_bytessink ;
  5139. UPB_INLINE void upb_bytessink_reset(upb_bytessink* s, const upb_byteshandler *h,
  5140. void *closure) {
  5141. s->handler = h;
  5142. s->closure = closure;
  5143. }
  5144. UPB_INLINE bool upb_bytessink_start(upb_bytessink s, size_t size_hint,
  5145. void **subc) {
  5146. typedef upb_startstr_handlerfunc func;
  5147. func *start;
  5148. *subc = s.closure;
  5149. if (!s.handler) return true;
  5150. start = (func *)s.handler->table[UPB_STARTSTR_SELECTOR].func;
  5151. if (!start) return true;
  5152. *subc = start(s.closure,
  5153. s.handler->table[UPB_STARTSTR_SELECTOR].attr.handler_data,
  5154. size_hint);
  5155. return *subc != NULL;
  5156. }
  5157. UPB_INLINE size_t upb_bytessink_putbuf(upb_bytessink s, void *subc,
  5158. const char *buf, size_t size,
  5159. const upb_bufhandle* handle) {
  5160. typedef upb_string_handlerfunc func;
  5161. func *putbuf;
  5162. if (!s.handler) return true;
  5163. putbuf = (func *)s.handler->table[UPB_STRING_SELECTOR].func;
  5164. if (!putbuf) return true;
  5165. return putbuf(subc, s.handler->table[UPB_STRING_SELECTOR].attr.handler_data,
  5166. buf, size, handle);
  5167. }
  5168. UPB_INLINE bool upb_bytessink_end(upb_bytessink s) {
  5169. typedef upb_endfield_handlerfunc func;
  5170. func *end;
  5171. if (!s.handler) return true;
  5172. end = (func *)s.handler->table[UPB_ENDSTR_SELECTOR].func;
  5173. if (!end) return true;
  5174. return end(s.closure,
  5175. s.handler->table[UPB_ENDSTR_SELECTOR].attr.handler_data);
  5176. }
  5177. #ifdef __cplusplus
  5178. class upb::BytesSink {
  5179. public:
  5180. BytesSink() {}
  5181. BytesSink(const BytesSink&) = default;
  5182. BytesSink& operator=(const BytesSink&) = default;
  5183. BytesSink(const upb_bytessink& sink) : sink_(sink) {}
  5184. BytesSink &operator=(const upb_bytessink &sink) {
  5185. sink_ = sink;
  5186. return *this;
  5187. }
  5188. upb_bytessink sink() { return sink_; }
  5189. /* Constructs a new sink for the given frozen handlers and closure.
  5190. *
  5191. * TODO(haberman): once the Handlers know the expected closure type, verify
  5192. * that T matches it. */
  5193. template <class T> BytesSink(const upb_byteshandler* handler, T* closure) {
  5194. upb_bytessink_reset(sink_, handler, closure);
  5195. }
  5196. /* Resets the value of the sink. */
  5197. template <class T> void Reset(const upb_byteshandler* handler, T* closure) {
  5198. upb_bytessink_reset(&sink_, handler, closure);
  5199. }
  5200. bool Start(size_t size_hint, void **subc) {
  5201. return upb_bytessink_start(sink_, size_hint, subc);
  5202. }
  5203. size_t PutBuffer(void *subc, const char *buf, size_t len,
  5204. const upb_bufhandle *handle) {
  5205. return upb_bytessink_putbuf(sink_, subc, buf, len, handle);
  5206. }
  5207. bool End() {
  5208. return upb_bytessink_end(sink_);
  5209. }
  5210. private:
  5211. upb_bytessink sink_;
  5212. };
  5213. #endif /* __cplusplus */
  5214. /* upb_bufsrc *****************************************************************/
  5215. #ifdef __cplusplus
  5216. extern "C" {
  5217. #endif
  5218. bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink sink);
  5219. #ifdef __cplusplus
  5220. } /* extern "C" */
  5221. namespace upb {
  5222. template <class T> bool PutBuffer(const T& str, BytesSink sink) {
  5223. return upb_bufsrc_putbuf(str.data(), str.size(), sink.sink());
  5224. }
  5225. }
  5226. #endif /* __cplusplus */
  5227. #endif
  5228. #ifndef UPB_MSGFACTORY_H_
  5229. #define UPB_MSGFACTORY_H_
  5230. /** upb_msgfactory ************************************************************/
  5231. struct upb_msgfactory;
  5232. typedef struct upb_msgfactory upb_msgfactory;
  5233. #ifdef __cplusplus
  5234. extern "C" {
  5235. #endif
  5236. /* A upb_msgfactory contains a cache of upb_msglayout, upb_handlers, and
  5237. * upb_visitorplan objects. These are the objects necessary to represent,
  5238. * populate, and and visit upb_msg objects.
  5239. *
  5240. * These caches are all populated by upb_msgdef, and lazily created on demand.
  5241. */
  5242. /* Creates and destroys a msgfactory, respectively. The messages for this
  5243. * msgfactory must come from |symtab| (which should outlive the msgfactory). */
  5244. upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab);
  5245. void upb_msgfactory_free(upb_msgfactory *f);
  5246. const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f);
  5247. /* The functions to get cached objects, lazily creating them on demand. These
  5248. * all require:
  5249. *
  5250. * - m is in upb_msgfactory_symtab(f)
  5251. * - upb_msgdef_mapentry(m) == false (since map messages can't have layouts).
  5252. *
  5253. * The returned objects will live for as long as the msgfactory does.
  5254. *
  5255. * TODO(haberman): consider making this thread-safe and take a const
  5256. * upb_msgfactory. */
  5257. const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f,
  5258. const upb_msgdef *m);
  5259. #ifdef __cplusplus
  5260. } /* extern "C" */
  5261. #endif
  5262. #endif /* UPB_MSGFACTORY_H_ */
  5263. /*
  5264. ** Internal-only definitions for the decoder.
  5265. */
  5266. #ifndef UPB_DECODER_INT_H_
  5267. #define UPB_DECODER_INT_H_
  5268. /*
  5269. ** upb::pb::Decoder
  5270. **
  5271. ** A high performance, streaming, resumable decoder for the binary protobuf
  5272. ** format.
  5273. **
  5274. ** This interface works the same regardless of what decoder backend is being
  5275. ** used. A client of this class does not need to know whether decoding is using
  5276. ** a JITted decoder (DynASM, LLVM, etc) or an interpreted decoder. By default,
  5277. ** it will always use the fastest available decoder. However, you can call
  5278. ** set_allow_jit(false) to disable any JIT decoder that might be available.
  5279. ** This is primarily useful for testing purposes.
  5280. */
  5281. #ifndef UPB_DECODER_H_
  5282. #define UPB_DECODER_H_
  5283. #ifdef __cplusplus
  5284. namespace upb {
  5285. namespace pb {
  5286. class CodeCache;
  5287. class DecoderPtr;
  5288. class DecoderMethodPtr;
  5289. class DecoderMethodOptions;
  5290. } /* namespace pb */
  5291. } /* namespace upb */
  5292. #endif
  5293. /* The maximum number of bytes we are required to buffer internally between
  5294. * calls to the decoder. The value is 14: a 5 byte unknown tag plus ten-byte
  5295. * varint, less one because we are buffering an incomplete value.
  5296. *
  5297. * Should only be used by unit tests. */
  5298. #define UPB_DECODER_MAX_RESIDUAL_BYTES 14
  5299. /* upb_pbdecodermethod ********************************************************/
  5300. struct upb_pbdecodermethod;
  5301. typedef struct upb_pbdecodermethod upb_pbdecodermethod;
  5302. #ifdef __cplusplus
  5303. extern "C" {
  5304. #endif
  5305. const upb_handlers *upb_pbdecodermethod_desthandlers(
  5306. const upb_pbdecodermethod *m);
  5307. const upb_byteshandler *upb_pbdecodermethod_inputhandler(
  5308. const upb_pbdecodermethod *m);
  5309. bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m);
  5310. #ifdef __cplusplus
  5311. } /* extern "C" */
  5312. /* Represents the code to parse a protobuf according to a destination
  5313. * Handlers. */
  5314. class upb::pb::DecoderMethodPtr {
  5315. public:
  5316. DecoderMethodPtr() : ptr_(nullptr) {}
  5317. DecoderMethodPtr(const upb_pbdecodermethod* ptr) : ptr_(ptr) {}
  5318. const upb_pbdecodermethod* ptr() { return ptr_; }
  5319. /* The destination handlers that are statically bound to this method.
  5320. * This method is only capable of outputting to a sink that uses these
  5321. * handlers. */
  5322. const Handlers *dest_handlers() const {
  5323. return upb_pbdecodermethod_desthandlers(ptr_);
  5324. }
  5325. /* The input handlers for this decoder method. */
  5326. const BytesHandler* input_handler() const {
  5327. return upb_pbdecodermethod_inputhandler(ptr_);
  5328. }
  5329. /* Whether this method is native. */
  5330. bool is_native() const {
  5331. return upb_pbdecodermethod_isnative(ptr_);
  5332. }
  5333. private:
  5334. const upb_pbdecodermethod* ptr_;
  5335. };
  5336. #endif
  5337. /* upb_pbdecoder **************************************************************/
  5338. /* Preallocation hint: decoder won't allocate more bytes than this when first
  5339. * constructed. This hint may be an overestimate for some build configurations.
  5340. * But if the decoder library is upgraded without recompiling the application,
  5341. * it may be an underestimate. */
  5342. #define UPB_PB_DECODER_SIZE 4416
  5343. struct upb_pbdecoder;
  5344. typedef struct upb_pbdecoder upb_pbdecoder;
  5345. #ifdef __cplusplus
  5346. extern "C" {
  5347. #endif
  5348. upb_pbdecoder *upb_pbdecoder_create(upb_arena *arena,
  5349. const upb_pbdecodermethod *method,
  5350. upb_sink output, upb_status *status);
  5351. const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d);
  5352. upb_bytessink upb_pbdecoder_input(upb_pbdecoder *d);
  5353. uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d);
  5354. size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d);
  5355. bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max);
  5356. void upb_pbdecoder_reset(upb_pbdecoder *d);
  5357. #ifdef __cplusplus
  5358. } /* extern "C" */
  5359. /* A Decoder receives binary protobuf data on its input sink and pushes the
  5360. * decoded data to its output sink. */
  5361. class upb::pb::DecoderPtr {
  5362. public:
  5363. DecoderPtr() : ptr_(nullptr) {}
  5364. DecoderPtr(upb_pbdecoder* ptr) : ptr_(ptr) {}
  5365. upb_pbdecoder* ptr() { return ptr_; }
  5366. /* Constructs a decoder instance for the given method, which must outlive this
  5367. * decoder. Any errors during parsing will be set on the given status, which
  5368. * must also outlive this decoder.
  5369. *
  5370. * The sink must match the given method. */
  5371. static DecoderPtr Create(Arena *arena, DecoderMethodPtr method,
  5372. upb::Sink output, Status *status) {
  5373. return DecoderPtr(upb_pbdecoder_create(arena->ptr(), method.ptr(),
  5374. output.sink(), status->ptr()));
  5375. }
  5376. /* Returns the DecoderMethod this decoder is parsing from. */
  5377. const DecoderMethodPtr method() const {
  5378. return DecoderMethodPtr(upb_pbdecoder_method(ptr_));
  5379. }
  5380. /* The sink on which this decoder receives input. */
  5381. BytesSink input() { return BytesSink(upb_pbdecoder_input(ptr())); }
  5382. /* Returns number of bytes successfully parsed.
  5383. *
  5384. * This can be useful for determining the stream position where an error
  5385. * occurred.
  5386. *
  5387. * This value may not be up-to-date when called from inside a parsing
  5388. * callback. */
  5389. uint64_t BytesParsed() { return upb_pbdecoder_bytesparsed(ptr()); }
  5390. /* Gets/sets the parsing nexting limit. If the total number of nested
  5391. * submessages and repeated fields hits this limit, parsing will fail. This
  5392. * is a resource limit that controls the amount of memory used by the parsing
  5393. * stack.
  5394. *
  5395. * Setting the limit will fail if the parser is currently suspended at a depth
  5396. * greater than this, or if memory allocation of the stack fails. */
  5397. size_t max_nesting() { return upb_pbdecoder_maxnesting(ptr()); }
  5398. bool set_max_nesting(size_t max) { return upb_pbdecoder_maxnesting(ptr()); }
  5399. void Reset() { upb_pbdecoder_reset(ptr()); }
  5400. static const size_t kSize = UPB_PB_DECODER_SIZE;
  5401. private:
  5402. upb_pbdecoder *ptr_;
  5403. };
  5404. #endif /* __cplusplus */
  5405. /* upb_pbcodecache ************************************************************/
  5406. /* Lazily builds and caches decoder methods that will push data to the given
  5407. * handlers. The destination handlercache must outlive this object. */
  5408. struct upb_pbcodecache;
  5409. typedef struct upb_pbcodecache upb_pbcodecache;
  5410. #ifdef __cplusplus
  5411. extern "C" {
  5412. #endif
  5413. upb_pbcodecache *upb_pbcodecache_new(upb_handlercache *dest);
  5414. void upb_pbcodecache_free(upb_pbcodecache *c);
  5415. bool upb_pbcodecache_allowjit(const upb_pbcodecache *c);
  5416. void upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow);
  5417. void upb_pbcodecache_setlazy(upb_pbcodecache *c, bool lazy);
  5418. const upb_pbdecodermethod *upb_pbcodecache_get(upb_pbcodecache *c,
  5419. const upb_msgdef *md);
  5420. #ifdef __cplusplus
  5421. } /* extern "C" */
  5422. /* A class for caching protobuf processing code, whether bytecode for the
  5423. * interpreted decoder or machine code for the JIT.
  5424. *
  5425. * This class is not thread-safe. */
  5426. class upb::pb::CodeCache {
  5427. public:
  5428. CodeCache(upb::HandlerCache *dest)
  5429. : ptr_(upb_pbcodecache_new(dest->ptr()), upb_pbcodecache_free) {}
  5430. CodeCache(CodeCache&&) = default;
  5431. CodeCache& operator=(CodeCache&&) = default;
  5432. upb_pbcodecache* ptr() { return ptr_.get(); }
  5433. const upb_pbcodecache* ptr() const { return ptr_.get(); }
  5434. /* Whether the cache is allowed to generate machine code. Defaults to true.
  5435. * There is no real reason to turn it off except for testing or if you are
  5436. * having a specific problem with the JIT.
  5437. *
  5438. * Note that allow_jit = true does not *guarantee* that the code will be JIT
  5439. * compiled. If this platform is not supported or the JIT was not compiled
  5440. * in, the code may still be interpreted. */
  5441. bool allow_jit() const { return upb_pbcodecache_allowjit(ptr()); }
  5442. /* This may only be called when the object is first constructed, and prior to
  5443. * any code generation. */
  5444. void set_allow_jit(bool allow) { upb_pbcodecache_setallowjit(ptr(), allow); }
  5445. /* Should the decoder push submessages to lazy handlers for fields that have
  5446. * them? The caller should set this iff the lazy handlers expect data that is
  5447. * in protobuf binary format and the caller wishes to lazy parse it. */
  5448. void set_lazy(bool lazy) { upb_pbcodecache_setlazy(ptr(), lazy); }
  5449. /* Returns a DecoderMethod that can push data to the given handlers.
  5450. * If a suitable method already exists, it will be returned from the cache. */
  5451. const DecoderMethodPtr Get(MessageDefPtr md) {
  5452. return DecoderMethodPtr(upb_pbcodecache_get(ptr(), md.ptr()));
  5453. }
  5454. private:
  5455. std::unique_ptr<upb_pbcodecache, decltype(&upb_pbcodecache_free)> ptr_;
  5456. };
  5457. #endif /* __cplusplus */
  5458. #endif /* UPB_DECODER_H_ */
  5459. /* Opcode definitions. The canonical meaning of each opcode is its
  5460. * implementation in the interpreter (the JIT is written to match this).
  5461. *
  5462. * All instructions have the opcode in the low byte.
  5463. * Instruction format for most instructions is:
  5464. *
  5465. * +-------------------+--------+
  5466. * | arg (24) | op (8) |
  5467. * +-------------------+--------+
  5468. *
  5469. * Exceptions are indicated below. A few opcodes are multi-word. */
  5470. typedef enum {
  5471. /* Opcodes 1-8, 13, 15-18 parse their respective descriptor types.
  5472. * Arg for all of these is the upb selector for this field. */
  5473. #define T(type) OP_PARSE_ ## type = UPB_DESCRIPTOR_TYPE_ ## type
  5474. T(DOUBLE), T(FLOAT), T(INT64), T(UINT64), T(INT32), T(FIXED64), T(FIXED32),
  5475. T(BOOL), T(UINT32), T(SFIXED32), T(SFIXED64), T(SINT32), T(SINT64),
  5476. #undef T
  5477. OP_STARTMSG = 9, /* No arg. */
  5478. OP_ENDMSG = 10, /* No arg. */
  5479. OP_STARTSEQ = 11,
  5480. OP_ENDSEQ = 12,
  5481. OP_STARTSUBMSG = 14,
  5482. OP_ENDSUBMSG = 19,
  5483. OP_STARTSTR = 20,
  5484. OP_STRING = 21,
  5485. OP_ENDSTR = 22,
  5486. OP_PUSHTAGDELIM = 23, /* No arg. */
  5487. OP_PUSHLENDELIM = 24, /* No arg. */
  5488. OP_POP = 25, /* No arg. */
  5489. OP_SETDELIM = 26, /* No arg. */
  5490. OP_SETBIGGROUPNUM = 27, /* two words:
  5491. * | unused (24) | opc (8) |
  5492. * | groupnum (32) | */
  5493. OP_CHECKDELIM = 28,
  5494. OP_CALL = 29,
  5495. OP_RET = 30,
  5496. OP_BRANCH = 31,
  5497. /* Different opcodes depending on how many bytes expected. */
  5498. OP_TAG1 = 32, /* | match tag (16) | jump target (8) | opc (8) | */
  5499. OP_TAG2 = 33, /* | match tag (16) | jump target (8) | opc (8) | */
  5500. OP_TAGN = 34, /* three words: */
  5501. /* | unused (16) | jump target(8) | opc (8) | */
  5502. /* | match tag 1 (32) | */
  5503. /* | match tag 2 (32) | */
  5504. OP_SETDISPATCH = 35, /* N words: */
  5505. /* | unused (24) | opc | */
  5506. /* | upb_inttable* (32 or 64) | */
  5507. OP_DISPATCH = 36, /* No arg. */
  5508. OP_HALT = 37 /* No arg. */
  5509. } opcode;
  5510. #define OP_MAX OP_HALT
  5511. UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); }
  5512. struct upb_pbcodecache {
  5513. upb_arena *arena;
  5514. upb_handlercache *dest;
  5515. bool allow_jit;
  5516. bool lazy;
  5517. /* Array of mgroups. */
  5518. upb_inttable groups;
  5519. };
  5520. /* Method group; represents a set of decoder methods that had their code
  5521. * emitted together. Immutable once created. */
  5522. typedef struct {
  5523. /* Maps upb_msgdef/upb_handlers -> upb_pbdecodermethod. Owned by us.
  5524. *
  5525. * Ideally this would be on pbcodecache (if we were actually caching code).
  5526. * Right now we don't actually cache anything, which is wasteful. */
  5527. upb_inttable methods;
  5528. /* The bytecode for our methods, if any exists. Owned by us. */
  5529. uint32_t *bytecode;
  5530. uint32_t *bytecode_end;
  5531. #ifdef UPB_USE_JIT_X64
  5532. /* JIT-generated machine code, if any. */
  5533. upb_string_handlerfunc *jit_code;
  5534. /* The size of the jit_code (required to munmap()). */
  5535. size_t jit_size;
  5536. char *debug_info;
  5537. void *dl;
  5538. #endif
  5539. } mgroup;
  5540. /* The maximum that any submessages can be nested. Matches proto2's limit.
  5541. * This specifies the size of the decoder's statically-sized array and therefore
  5542. * setting it high will cause the upb::pb::Decoder object to be larger.
  5543. *
  5544. * If necessary we can add a runtime-settable property to Decoder that allow
  5545. * this to be larger than the compile-time setting, but this would add
  5546. * complexity, particularly since we would have to decide how/if to give users
  5547. * the ability to set a custom memory allocation function. */
  5548. #define UPB_DECODER_MAX_NESTING 64
  5549. /* Internal-only struct used by the decoder. */
  5550. typedef struct {
  5551. /* Space optimization note: we store two pointers here that the JIT
  5552. * doesn't need at all; the upb_handlers* inside the sink and
  5553. * the dispatch table pointer. We can optimze so that the JIT uses
  5554. * smaller stack frames than the interpreter. The only thing we need
  5555. * to guarantee is that the fallback routines can find end_ofs. */
  5556. upb_sink sink;
  5557. /* The absolute stream offset of the end-of-frame delimiter.
  5558. * Non-delimited frames (groups and non-packed repeated fields) reuse the
  5559. * delimiter of their parent, even though the frame may not end there.
  5560. *
  5561. * NOTE: the JIT stores a slightly different value here for non-top frames.
  5562. * It stores the value relative to the end of the enclosed message. But the
  5563. * top frame is still stored the same way, which is important for ensuring
  5564. * that calls from the JIT into C work correctly. */
  5565. uint64_t end_ofs;
  5566. const uint32_t *base;
  5567. /* 0 indicates a length-delimited field.
  5568. * A positive number indicates a known group.
  5569. * A negative number indicates an unknown group. */
  5570. int32_t groupnum;
  5571. upb_inttable *dispatch; /* Not used by the JIT. */
  5572. } upb_pbdecoder_frame;
  5573. struct upb_pbdecodermethod {
  5574. /* While compiling, the base is relative in "ofs", after compiling it is
  5575. * absolute in "ptr". */
  5576. union {
  5577. uint32_t ofs; /* PC offset of method. */
  5578. void *ptr; /* Pointer to bytecode or machine code for this method. */
  5579. } code_base;
  5580. /* The decoder method group to which this method belongs. */
  5581. const mgroup *group;
  5582. /* Whether this method is native code or bytecode. */
  5583. bool is_native_;
  5584. /* The handler one calls to invoke this method. */
  5585. upb_byteshandler input_handler_;
  5586. /* The destination handlers this method is bound to. We own a ref. */
  5587. const upb_handlers *dest_handlers_;
  5588. /* Dispatch table -- used by both bytecode decoder and JIT when encountering a
  5589. * field number that wasn't the one we were expecting to see. See
  5590. * decoder.int.h for the layout of this table. */
  5591. upb_inttable dispatch;
  5592. };
  5593. struct upb_pbdecoder {
  5594. upb_arena *arena;
  5595. /* Our input sink. */
  5596. upb_bytessink input_;
  5597. /* The decoder method we are parsing with (owned). */
  5598. const upb_pbdecodermethod *method_;
  5599. size_t call_len;
  5600. const uint32_t *pc, *last;
  5601. /* Current input buffer and its stream offset. */
  5602. const char *buf, *ptr, *end, *checkpoint;
  5603. /* End of the delimited region, relative to ptr, NULL if not in this buf. */
  5604. const char *delim_end;
  5605. /* End of the delimited region, relative to ptr, end if not in this buf. */
  5606. const char *data_end;
  5607. /* Overall stream offset of "buf." */
  5608. uint64_t bufstart_ofs;
  5609. /* Buffer for residual bytes not parsed from the previous buffer. */
  5610. char residual[UPB_DECODER_MAX_RESIDUAL_BYTES];
  5611. char *residual_end;
  5612. /* Bytes of data that should be discarded from the input beore we start
  5613. * parsing again. We set this when we internally determine that we can
  5614. * safely skip the next N bytes, but this region extends past the current
  5615. * user buffer. */
  5616. size_t skip;
  5617. /* Stores the user buffer passed to our decode function. */
  5618. const char *buf_param;
  5619. size_t size_param;
  5620. const upb_bufhandle *handle;
  5621. /* Our internal stack. */
  5622. upb_pbdecoder_frame *stack, *top, *limit;
  5623. const uint32_t **callstack;
  5624. size_t stack_size;
  5625. upb_status *status;
  5626. #ifdef UPB_USE_JIT_X64
  5627. /* Used momentarily by the generated code to store a value while a user
  5628. * function is called. */
  5629. uint32_t tmp_len;
  5630. const void *saved_rsp;
  5631. #endif
  5632. };
  5633. /* Decoder entry points; used as handlers. */
  5634. void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint);
  5635. void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint);
  5636. size_t upb_pbdecoder_decode(void *closure, const void *hd, const char *buf,
  5637. size_t size, const upb_bufhandle *handle);
  5638. bool upb_pbdecoder_end(void *closure, const void *handler_data);
  5639. /* Decoder-internal functions that the JIT calls to handle fallback paths. */
  5640. int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
  5641. size_t size, const upb_bufhandle *handle);
  5642. size_t upb_pbdecoder_suspend(upb_pbdecoder *d);
  5643. int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
  5644. uint8_t wire_type);
  5645. int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d, uint64_t expected);
  5646. int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d, uint64_t *u64);
  5647. int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32);
  5648. int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64);
  5649. void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg);
  5650. /* Error messages that are shared between the bytecode and JIT decoders. */
  5651. extern const char *kPbDecoderStackOverflow;
  5652. extern const char *kPbDecoderSubmessageTooLong;
  5653. /* Access to decoderplan members needed by the decoder. */
  5654. const char *upb_pbdecoder_getopname(unsigned int op);
  5655. /* JIT codegen entry point. */
  5656. void upb_pbdecoder_jit(mgroup *group);
  5657. void upb_pbdecoder_freejit(mgroup *group);
  5658. /* A special label that means "do field dispatch for this message and branch to
  5659. * wherever that takes you." */
  5660. #define LABEL_DISPATCH 0
  5661. /* A special slot in the dispatch table that stores the epilogue (ENDMSG and/or
  5662. * RET) for branching to when we find an appropriate ENDGROUP tag. */
  5663. #define DISPATCH_ENDMSG 0
  5664. /* It's important to use this invalid wire type instead of 0 (which is a valid
  5665. * wire type). */
  5666. #define NO_WIRE_TYPE 0xff
  5667. /* The dispatch table layout is:
  5668. * [field number] -> [ 48-bit offset ][ 8-bit wt2 ][ 8-bit wt1 ]
  5669. *
  5670. * If wt1 matches, jump to the 48-bit offset. If wt2 matches, lookup
  5671. * (UPB_MAX_FIELDNUMBER + fieldnum) and jump there.
  5672. *
  5673. * We need two wire types because of packed/non-packed compatibility. A
  5674. * primitive repeated field can use either wire type and be valid. While we
  5675. * could key the table on fieldnum+wiretype, the table would be 8x sparser.
  5676. *
  5677. * Storing two wire types in the primary value allows us to quickly rule out
  5678. * the second wire type without needing to do a separate lookup (this case is
  5679. * less common than an unknown field). */
  5680. UPB_INLINE uint64_t upb_pbdecoder_packdispatch(uint64_t ofs, uint8_t wt1,
  5681. uint8_t wt2) {
  5682. return (ofs << 16) | (wt2 << 8) | wt1;
  5683. }
  5684. UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
  5685. uint8_t *wt1, uint8_t *wt2) {
  5686. *wt1 = (uint8_t)dispatch;
  5687. *wt2 = (uint8_t)(dispatch >> 8);
  5688. *ofs = dispatch >> 16;
  5689. }
  5690. /* All of the functions in decoder.c that return int32_t return values according
  5691. * to the following scheme:
  5692. * 1. negative values indicate a return code from the following list.
  5693. * 2. positive values indicate that error or end of buffer was hit, and
  5694. * that the decode function should immediately return the given value
  5695. * (the decoder state has already been suspended and is ready to be
  5696. * resumed). */
  5697. #define DECODE_OK -1
  5698. #define DECODE_MISMATCH -2 /* Used only from checktag_slow(). */
  5699. #define DECODE_ENDGROUP -3 /* Used only from checkunknown(). */
  5700. #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
  5701. #endif /* UPB_DECODER_INT_H_ */
  5702. /*
  5703. ** A number of routines for varint manipulation (we keep them all around to
  5704. ** have multiple approaches available for benchmarking).
  5705. */
  5706. #ifndef UPB_VARINT_DECODER_H_
  5707. #define UPB_VARINT_DECODER_H_
  5708. #include <assert.h>
  5709. #include <stdint.h>
  5710. #include <string.h>
  5711. #ifdef __cplusplus
  5712. extern "C" {
  5713. #endif
  5714. #define UPB_MAX_WIRE_TYPE 5
  5715. /* The maximum number of bytes that it takes to encode a 64-bit varint. */
  5716. #define UPB_PB_VARINT_MAX_LEN 10
  5717. /* Array of the "native" (ie. non-packed-repeated) wire type for the given a
  5718. * descriptor type (upb_descriptortype_t). */
  5719. extern const uint8_t upb_pb_native_wire_types[];
  5720. UPB_INLINE uint64_t byteswap64(uint64_t val)
  5721. {
  5722. return ((((val) & 0xff00000000000000ull) >> 56)
  5723. | (((val) & 0x00ff000000000000ull) >> 40)
  5724. | (((val) & 0x0000ff0000000000ull) >> 24)
  5725. | (((val) & 0x000000ff00000000ull) >> 8)
  5726. | (((val) & 0x00000000ff000000ull) << 8)
  5727. | (((val) & 0x0000000000ff0000ull) << 24)
  5728. | (((val) & 0x000000000000ff00ull) << 40)
  5729. | (((val) & 0x00000000000000ffull) << 56));
  5730. }
  5731. /* Zig-zag encoding/decoding **************************************************/
  5732. UPB_INLINE int32_t upb_zzdec_32(uint32_t n) {
  5733. return (n >> 1) ^ -(int32_t)(n & 1);
  5734. }
  5735. UPB_INLINE int64_t upb_zzdec_64(uint64_t n) {
  5736. return (n >> 1) ^ -(int64_t)(n & 1);
  5737. }
  5738. UPB_INLINE uint32_t upb_zzenc_32(int32_t n) { return (n << 1) ^ (n >> 31); }
  5739. UPB_INLINE uint64_t upb_zzenc_64(int64_t n) { return (n << 1) ^ (n >> 63); }
  5740. /* Decoding *******************************************************************/
  5741. /* All decoding functions return this struct by value. */
  5742. typedef struct {
  5743. const char *p; /* NULL if the varint was unterminated. */
  5744. uint64_t val;
  5745. } upb_decoderet;
  5746. UPB_INLINE upb_decoderet upb_decoderet_make(const char *p, uint64_t val) {
  5747. upb_decoderet ret;
  5748. ret.p = p;
  5749. ret.val = val;
  5750. return ret;
  5751. }
  5752. upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r);
  5753. upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r);
  5754. /* Template for a function that checks the first two bytes with branching
  5755. * and dispatches 2-10 bytes with a separate function. Note that this may read
  5756. * up to 10 bytes, so it must not be used unless there are at least ten bytes
  5757. * left in the buffer! */
  5758. #define UPB_VARINT_DECODER_CHECK2(name, decode_max8_function) \
  5759. UPB_INLINE upb_decoderet upb_vdecode_check2_ ## name(const char *_p) { \
  5760. uint8_t *p = (uint8_t*)_p; \
  5761. upb_decoderet r; \
  5762. if ((*p & 0x80) == 0) { \
  5763. /* Common case: one-byte varint. */ \
  5764. return upb_decoderet_make(_p + 1, *p & 0x7fU); \
  5765. } \
  5766. r = upb_decoderet_make(_p + 2, (*p & 0x7fU) | ((*(p + 1) & 0x7fU) << 7)); \
  5767. if ((*(p + 1) & 0x80) == 0) { \
  5768. /* Two-byte varint. */ \
  5769. return r; \
  5770. } \
  5771. /* Longer varint, fallback to out-of-line function. */ \
  5772. return decode_max8_function(r); \
  5773. }
  5774. UPB_VARINT_DECODER_CHECK2(branch32, upb_vdecode_max8_branch32)
  5775. UPB_VARINT_DECODER_CHECK2(branch64, upb_vdecode_max8_branch64)
  5776. #undef UPB_VARINT_DECODER_CHECK2
  5777. /* Our canonical functions for decoding varints, based on the currently
  5778. * favored best-performing implementations. */
  5779. UPB_INLINE upb_decoderet upb_vdecode_fast(const char *p) {
  5780. if (sizeof(long) == 8)
  5781. return upb_vdecode_check2_branch64(p);
  5782. else
  5783. return upb_vdecode_check2_branch32(p);
  5784. }
  5785. /* Encoding *******************************************************************/
  5786. UPB_INLINE int upb_value_size(uint64_t val) {
  5787. #ifdef __GNUC__
  5788. int high_bit = 63 - __builtin_clzll(val); /* 0-based, undef if val == 0. */
  5789. #else
  5790. int high_bit = 0;
  5791. uint64_t tmp = val;
  5792. while(tmp >>= 1) high_bit++;
  5793. #endif
  5794. return val == 0 ? 1 : high_bit / 8 + 1;
  5795. }
  5796. /* Encodes a 64-bit varint into buf (which must be >=UPB_PB_VARINT_MAX_LEN
  5797. * bytes long), returning how many bytes were used.
  5798. *
  5799. * TODO: benchmark and optimize if necessary. */
  5800. UPB_INLINE size_t upb_vencode64(uint64_t val, char *buf) {
  5801. size_t i;
  5802. if (val == 0) { buf[0] = 0; return 1; }
  5803. i = 0;
  5804. while (val) {
  5805. uint8_t byte = val & 0x7fU;
  5806. val >>= 7;
  5807. if (val) byte |= 0x80U;
  5808. buf[i++] = byte;
  5809. }
  5810. return i;
  5811. }
  5812. UPB_INLINE size_t upb_varint_size(uint64_t val) {
  5813. char buf[UPB_PB_VARINT_MAX_LEN];
  5814. return upb_vencode64(val, buf);
  5815. }
  5816. /* Encodes a 32-bit varint, *not* sign-extended. */
  5817. UPB_INLINE uint64_t upb_vencode32(uint32_t val) {
  5818. char buf[UPB_PB_VARINT_MAX_LEN];
  5819. size_t bytes = upb_vencode64(val, buf);
  5820. uint64_t ret = 0;
  5821. UPB_ASSERT(bytes <= 5);
  5822. memcpy(&ret, buf, bytes);
  5823. #ifdef UPB_BIG_ENDIAN
  5824. ret = byteswap64(ret);
  5825. #endif
  5826. UPB_ASSERT(ret <= 0xffffffffffU);
  5827. return ret;
  5828. }
  5829. #ifdef __cplusplus
  5830. } /* extern "C" */
  5831. #endif
  5832. #endif /* UPB_VARINT_DECODER_H_ */
  5833. /*
  5834. ** upb::pb::Encoder (upb_pb_encoder)
  5835. **
  5836. ** Implements a set of upb_handlers that write protobuf data to the binary wire
  5837. ** format.
  5838. **
  5839. ** This encoder implementation does not have any access to any out-of-band or
  5840. ** precomputed lengths for submessages, so it must buffer submessages internally
  5841. ** before it can emit the first byte.
  5842. */
  5843. #ifndef UPB_ENCODER_H_
  5844. #define UPB_ENCODER_H_
  5845. #ifdef __cplusplus
  5846. namespace upb {
  5847. namespace pb {
  5848. class EncoderPtr;
  5849. } /* namespace pb */
  5850. } /* namespace upb */
  5851. #endif
  5852. #define UPB_PBENCODER_MAX_NESTING 100
  5853. /* upb_pb_encoder *************************************************************/
  5854. /* Preallocation hint: decoder won't allocate more bytes than this when first
  5855. * constructed. This hint may be an overestimate for some build configurations.
  5856. * But if the decoder library is upgraded without recompiling the application,
  5857. * it may be an underestimate. */
  5858. #define UPB_PB_ENCODER_SIZE 784
  5859. struct upb_pb_encoder;
  5860. typedef struct upb_pb_encoder upb_pb_encoder;
  5861. #ifdef __cplusplus
  5862. extern "C" {
  5863. #endif
  5864. upb_sink upb_pb_encoder_input(upb_pb_encoder *p);
  5865. upb_pb_encoder* upb_pb_encoder_create(upb_arena* a, const upb_handlers* h,
  5866. upb_bytessink output);
  5867. /* Lazily builds and caches handlers that will push encoded data to a bytessink.
  5868. * Any msgdef objects used with this object must outlive it. */
  5869. upb_handlercache *upb_pb_encoder_newcache();
  5870. #ifdef __cplusplus
  5871. } /* extern "C" { */
  5872. class upb::pb::EncoderPtr {
  5873. public:
  5874. EncoderPtr(upb_pb_encoder* ptr) : ptr_(ptr) {}
  5875. upb_pb_encoder* ptr() { return ptr_; }
  5876. /* Creates a new encoder in the given environment. The Handlers must have
  5877. * come from NewHandlers() below. */
  5878. static EncoderPtr Create(Arena* arena, const Handlers* handlers,
  5879. BytesSink output) {
  5880. return EncoderPtr(
  5881. upb_pb_encoder_create(arena->ptr(), handlers, output.sink()));
  5882. }
  5883. /* The input to the encoder. */
  5884. upb::Sink input() { return upb_pb_encoder_input(ptr()); }
  5885. /* Creates a new set of handlers for this MessageDef. */
  5886. static HandlerCache NewCache() {
  5887. return HandlerCache(upb_pb_encoder_newcache());
  5888. }
  5889. static const size_t kSize = UPB_PB_ENCODER_SIZE;
  5890. private:
  5891. upb_pb_encoder* ptr_;
  5892. };
  5893. #endif /* __cplusplus */
  5894. #endif /* UPB_ENCODER_H_ */
  5895. /*
  5896. ** upb::pb::TextPrinter (upb_textprinter)
  5897. **
  5898. ** Handlers for writing to protobuf text format.
  5899. */
  5900. #ifndef UPB_TEXT_H_
  5901. #define UPB_TEXT_H_
  5902. #ifdef __cplusplus
  5903. namespace upb {
  5904. namespace pb {
  5905. class TextPrinterPtr;
  5906. } /* namespace pb */
  5907. } /* namespace upb */
  5908. #endif
  5909. /* upb_textprinter ************************************************************/
  5910. struct upb_textprinter;
  5911. typedef struct upb_textprinter upb_textprinter;
  5912. #ifdef __cplusplus
  5913. extern "C" {
  5914. #endif
  5915. /* C API. */
  5916. upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h,
  5917. upb_bytessink output);
  5918. void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line);
  5919. upb_sink upb_textprinter_input(upb_textprinter *p);
  5920. upb_handlercache *upb_textprinter_newcache();
  5921. #ifdef __cplusplus
  5922. } /* extern "C" */
  5923. class upb::pb::TextPrinterPtr {
  5924. public:
  5925. TextPrinterPtr(upb_textprinter* ptr) : ptr_(ptr) {}
  5926. /* The given handlers must have come from NewHandlers(). It must outlive the
  5927. * TextPrinter. */
  5928. static TextPrinterPtr Create(Arena *arena, upb::HandlersPtr *handlers,
  5929. BytesSink output) {
  5930. return TextPrinterPtr(
  5931. upb_textprinter_create(arena->ptr(), handlers->ptr(), output.sink()));
  5932. }
  5933. void SetSingleLineMode(bool single_line) {
  5934. upb_textprinter_setsingleline(ptr_, single_line);
  5935. }
  5936. Sink input() { return upb_textprinter_input(ptr_); }
  5937. /* If handler caching becomes a requirement we can add a code cache as in
  5938. * decoder.h */
  5939. static HandlerCache NewCache() {
  5940. return HandlerCache(upb_textprinter_newcache());
  5941. }
  5942. private:
  5943. upb_textprinter* ptr_;
  5944. };
  5945. #endif
  5946. #endif /* UPB_TEXT_H_ */
  5947. /*
  5948. ** upb::json::Parser (upb_json_parser)
  5949. **
  5950. ** Parses JSON according to a specific schema.
  5951. ** Support for parsing arbitrary JSON (schema-less) will be added later.
  5952. */
  5953. #ifndef UPB_JSON_PARSER_H_
  5954. #define UPB_JSON_PARSER_H_
  5955. #ifdef __cplusplus
  5956. namespace upb {
  5957. namespace json {
  5958. class CodeCache;
  5959. class ParserPtr;
  5960. class ParserMethodPtr;
  5961. } /* namespace json */
  5962. } /* namespace upb */
  5963. #endif
  5964. /* upb_json_parsermethod ******************************************************/
  5965. struct upb_json_parsermethod;
  5966. typedef struct upb_json_parsermethod upb_json_parsermethod;
  5967. #ifdef __cplusplus
  5968. extern "C" {
  5969. #endif
  5970. const upb_byteshandler* upb_json_parsermethod_inputhandler(
  5971. const upb_json_parsermethod* m);
  5972. #ifdef __cplusplus
  5973. } /* extern "C" */
  5974. class upb::json::ParserMethodPtr {
  5975. public:
  5976. ParserMethodPtr() : ptr_(nullptr) {}
  5977. ParserMethodPtr(const upb_json_parsermethod* ptr) : ptr_(ptr) {}
  5978. const upb_json_parsermethod* ptr() const { return ptr_; }
  5979. const BytesHandler* input_handler() const {
  5980. return upb_json_parsermethod_inputhandler(ptr());
  5981. }
  5982. private:
  5983. const upb_json_parsermethod* ptr_;
  5984. };
  5985. #endif /* __cplusplus */
  5986. /* upb_json_parser ************************************************************/
  5987. /* Preallocation hint: parser won't allocate more bytes than this when first
  5988. * constructed. This hint may be an overestimate for some build configurations.
  5989. * But if the parser library is upgraded without recompiling the application,
  5990. * it may be an underestimate. */
  5991. #define UPB_JSON_PARSER_SIZE 5712
  5992. struct upb_json_parser;
  5993. typedef struct upb_json_parser upb_json_parser;
  5994. #ifdef __cplusplus
  5995. extern "C" {
  5996. #endif
  5997. upb_json_parser* upb_json_parser_create(upb_arena* a,
  5998. const upb_json_parsermethod* m,
  5999. const upb_symtab* symtab,
  6000. upb_sink output,
  6001. upb_status *status,
  6002. bool ignore_json_unknown);
  6003. upb_bytessink upb_json_parser_input(upb_json_parser* p);
  6004. #ifdef __cplusplus
  6005. } /* extern "C" */
  6006. /* Parses an incoming BytesStream, pushing the results to the destination
  6007. * sink. */
  6008. class upb::json::ParserPtr {
  6009. public:
  6010. ParserPtr(upb_json_parser* ptr) : ptr_(ptr) {}
  6011. static ParserPtr Create(Arena* arena, ParserMethodPtr method,
  6012. SymbolTable* symtab, Sink output, Status* status,
  6013. bool ignore_json_unknown) {
  6014. upb_symtab* symtab_ptr = symtab ? symtab->ptr() : nullptr;
  6015. return ParserPtr(upb_json_parser_create(
  6016. arena->ptr(), method.ptr(), symtab_ptr, output.sink(), status->ptr(),
  6017. ignore_json_unknown));
  6018. }
  6019. BytesSink input() { return upb_json_parser_input(ptr_); }
  6020. private:
  6021. upb_json_parser* ptr_;
  6022. };
  6023. #endif /* __cplusplus */
  6024. /* upb_json_codecache *********************************************************/
  6025. /* Lazily builds and caches decoder methods that will push data to the given
  6026. * handlers. The upb_symtab object(s) must outlive this object. */
  6027. struct upb_json_codecache;
  6028. typedef struct upb_json_codecache upb_json_codecache;
  6029. #ifdef __cplusplus
  6030. extern "C" {
  6031. #endif
  6032. upb_json_codecache *upb_json_codecache_new();
  6033. void upb_json_codecache_free(upb_json_codecache *cache);
  6034. const upb_json_parsermethod* upb_json_codecache_get(upb_json_codecache* cache,
  6035. const upb_msgdef* md);
  6036. #ifdef __cplusplus
  6037. } /* extern "C" */
  6038. class upb::json::CodeCache {
  6039. public:
  6040. CodeCache() : ptr_(upb_json_codecache_new(), upb_json_codecache_free) {}
  6041. /* Returns a DecoderMethod that can push data to the given handlers.
  6042. * If a suitable method already exists, it will be returned from the cache. */
  6043. ParserMethodPtr Get(MessageDefPtr md) {
  6044. return upb_json_codecache_get(ptr_.get(), md.ptr());
  6045. }
  6046. private:
  6047. std::unique_ptr<upb_json_codecache, decltype(&upb_json_codecache_free)> ptr_;
  6048. };
  6049. #endif
  6050. #endif /* UPB_JSON_PARSER_H_ */
  6051. /*
  6052. ** upb::json::Printer
  6053. **
  6054. ** Handlers that emit JSON according to a specific protobuf schema.
  6055. */
  6056. #ifndef UPB_JSON_TYPED_PRINTER_H_
  6057. #define UPB_JSON_TYPED_PRINTER_H_
  6058. #ifdef __cplusplus
  6059. namespace upb {
  6060. namespace json {
  6061. class PrinterPtr;
  6062. } /* namespace json */
  6063. } /* namespace upb */
  6064. #endif
  6065. /* upb_json_printer ***********************************************************/
  6066. #define UPB_JSON_PRINTER_SIZE 192
  6067. struct upb_json_printer;
  6068. typedef struct upb_json_printer upb_json_printer;
  6069. #ifdef __cplusplus
  6070. extern "C" {
  6071. #endif
  6072. /* Native C API. */
  6073. upb_json_printer *upb_json_printer_create(upb_arena *a, const upb_handlers *h,
  6074. upb_bytessink output);
  6075. upb_sink upb_json_printer_input(upb_json_printer *p);
  6076. const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
  6077. bool preserve_fieldnames,
  6078. const void *owner);
  6079. /* Lazily builds and caches handlers that will push encoded data to a bytessink.
  6080. * Any msgdef objects used with this object must outlive it. */
  6081. upb_handlercache *upb_json_printer_newcache(bool preserve_proto_fieldnames);
  6082. #ifdef __cplusplus
  6083. } /* extern "C" */
  6084. /* Prints an incoming stream of data to a BytesSink in JSON format. */
  6085. class upb::json::PrinterPtr {
  6086. public:
  6087. PrinterPtr(upb_json_printer* ptr) : ptr_(ptr) {}
  6088. static PrinterPtr Create(Arena *arena, const upb::Handlers *handlers,
  6089. BytesSink output) {
  6090. return PrinterPtr(
  6091. upb_json_printer_create(arena->ptr(), handlers, output.sink()));
  6092. }
  6093. /* The input to the printer. */
  6094. Sink input() { return upb_json_printer_input(ptr_); }
  6095. static const size_t kSize = UPB_JSON_PRINTER_SIZE;
  6096. static HandlerCache NewCache(bool preserve_proto_fieldnames) {
  6097. return upb_json_printer_newcache(preserve_proto_fieldnames);
  6098. }
  6099. private:
  6100. upb_json_printer* ptr_;
  6101. };
  6102. #endif /* __cplusplus */
  6103. #endif /* UPB_JSON_TYPED_PRINTER_H_ */
  6104. #undef UPB_SIZE
  6105. #undef UPB_FIELD_AT
  6106. #undef UPB_READ_ONEOF
  6107. #undef UPB_WRITE_ONEOF