upb.h 295 KB

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