upb.h 332 KB

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