upb.h 320 KB

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