upb.h 315 KB

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