typegen.c 181 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167
  1. /*
  2. * Format String Generator for IDL Compiler
  3. *
  4. * Copyright 2005-2006 Eric Kohl
  5. * Copyright 2005-2006 Robert Shearman
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <assert.h>
  26. #include <ctype.h>
  27. #include <limits.h>
  28. #include "widl.h"
  29. #include "utils.h"
  30. #include "parser.h"
  31. #include "header.h"
  32. #include "typetree.h"
  33. #include "typegen.h"
  34. #include "expr.h"
  35. /* round size up to multiple of alignment */
  36. #define ROUND_SIZE(size, alignment) (((size) + ((alignment) - 1)) & ~((alignment) - 1))
  37. /* value to add on to round size up to a multiple of alignment */
  38. #define ROUNDING(size, alignment) (((alignment) - 1) - (((size) + ((alignment) - 1)) & ((alignment) - 1)))
  39. static const type_t *current_structure;
  40. static const var_t *current_func;
  41. static const type_t *current_iface;
  42. static struct list expr_eval_routines = LIST_INIT(expr_eval_routines);
  43. struct expr_eval_routine
  44. {
  45. struct list entry;
  46. const type_t *iface;
  47. const type_t *cont_type;
  48. char *name;
  49. unsigned int baseoff;
  50. const expr_t *expr;
  51. };
  52. enum type_context
  53. {
  54. TYPE_CONTEXT_TOPLEVELPARAM,
  55. TYPE_CONTEXT_PARAM,
  56. TYPE_CONTEXT_CONTAINER,
  57. TYPE_CONTEXT_CONTAINER_NO_POINTERS,
  58. TYPE_CONTEXT_RETVAL,
  59. };
  60. /* parameter flags in Oif mode */
  61. static const unsigned short MustSize = 0x0001;
  62. static const unsigned short MustFree = 0x0002;
  63. static const unsigned short IsPipe = 0x0004;
  64. static const unsigned short IsIn = 0x0008;
  65. static const unsigned short IsOut = 0x0010;
  66. static const unsigned short IsReturn = 0x0020;
  67. static const unsigned short IsBasetype = 0x0040;
  68. static const unsigned short IsByValue = 0x0080;
  69. static const unsigned short IsSimpleRef = 0x0100;
  70. /* static const unsigned short IsDontCallFreeInst = 0x0200; */
  71. /* static const unsigned short SaveForAsyncFinish = 0x0400; */
  72. static unsigned int field_memsize(const type_t *type, unsigned int *offset);
  73. static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align);
  74. static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
  75. const char *name, unsigned int *typestring_offset);
  76. static unsigned int write_struct_tfs(FILE *file, type_t *type, const char *name, unsigned int *tfsoff);
  77. static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
  78. const char *name, int write_ptr, unsigned int *tfsoff);
  79. static const var_t *find_array_or_string_in_struct(const type_t *type);
  80. static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
  81. type_t *type, enum type_context context,
  82. const char *name, unsigned int *typestring_offset);
  83. static unsigned int get_required_buffer_size_type( const type_t *type, const char *name,
  84. const attr_list_t *attrs, int toplevel_param,
  85. unsigned int *alignment );
  86. static unsigned int get_function_buffer_size( const var_t *func, enum pass pass );
  87. static const char *string_of_type(unsigned char type)
  88. {
  89. switch (type)
  90. {
  91. case FC_BYTE: return "FC_BYTE";
  92. case FC_CHAR: return "FC_CHAR";
  93. case FC_SMALL: return "FC_SMALL";
  94. case FC_USMALL: return "FC_USMALL";
  95. case FC_WCHAR: return "FC_WCHAR";
  96. case FC_SHORT: return "FC_SHORT";
  97. case FC_USHORT: return "FC_USHORT";
  98. case FC_LONG: return "FC_LONG";
  99. case FC_ULONG: return "FC_ULONG";
  100. case FC_FLOAT: return "FC_FLOAT";
  101. case FC_HYPER: return "FC_HYPER";
  102. case FC_DOUBLE: return "FC_DOUBLE";
  103. case FC_ENUM16: return "FC_ENUM16";
  104. case FC_ENUM32: return "FC_ENUM32";
  105. case FC_IGNORE: return "FC_IGNORE";
  106. case FC_ERROR_STATUS_T: return "FC_ERROR_STATUS_T";
  107. case FC_RP: return "FC_RP";
  108. case FC_UP: return "FC_UP";
  109. case FC_OP: return "FC_OP";
  110. case FC_FP: return "FC_FP";
  111. case FC_ENCAPSULATED_UNION: return "FC_ENCAPSULATED_UNION";
  112. case FC_NON_ENCAPSULATED_UNION: return "FC_NON_ENCAPSULATED_UNION";
  113. case FC_STRUCT: return "FC_STRUCT";
  114. case FC_PSTRUCT: return "FC_PSTRUCT";
  115. case FC_CSTRUCT: return "FC_CSTRUCT";
  116. case FC_CPSTRUCT: return "FC_CPSTRUCT";
  117. case FC_CVSTRUCT: return "FC_CVSTRUCT";
  118. case FC_BOGUS_STRUCT: return "FC_BOGUS_STRUCT";
  119. case FC_SMFARRAY: return "FC_SMFARRAY";
  120. case FC_LGFARRAY: return "FC_LGFARRAY";
  121. case FC_SMVARRAY: return "FC_SMVARRAY";
  122. case FC_LGVARRAY: return "FC_LGVARRAY";
  123. case FC_CARRAY: return "FC_CARRAY";
  124. case FC_CVARRAY: return "FC_CVARRAY";
  125. case FC_BOGUS_ARRAY: return "FC_BOGUS_ARRAY";
  126. case FC_ALIGNM2: return "FC_ALIGNM2";
  127. case FC_ALIGNM4: return "FC_ALIGNM4";
  128. case FC_ALIGNM8: return "FC_ALIGNM8";
  129. case FC_POINTER: return "FC_POINTER";
  130. case FC_C_CSTRING: return "FC_C_CSTRING";
  131. case FC_C_WSTRING: return "FC_C_WSTRING";
  132. case FC_CSTRING: return "FC_CSTRING";
  133. case FC_WSTRING: return "FC_WSTRING";
  134. case FC_BYTE_COUNT_POINTER: return "FC_BYTE_COUNT_POINTER";
  135. case FC_TRANSMIT_AS: return "FC_TRANSMIT_AS";
  136. case FC_REPRESENT_AS: return "FC_REPRESENT_AS";
  137. case FC_IP: return "FC_IP";
  138. case FC_BIND_CONTEXT: return "FC_BIND_CONTEXT";
  139. case FC_BIND_GENERIC: return "FC_BIND_GENERIC";
  140. case FC_BIND_PRIMITIVE: return "FC_BIND_PRIMITIVE";
  141. case FC_AUTO_HANDLE: return "FC_AUTO_HANDLE";
  142. case FC_CALLBACK_HANDLE: return "FC_CALLBACK_HANDLE";
  143. case FC_STRUCTPAD1: return "FC_STRUCTPAD1";
  144. case FC_STRUCTPAD2: return "FC_STRUCTPAD2";
  145. case FC_STRUCTPAD3: return "FC_STRUCTPAD3";
  146. case FC_STRUCTPAD4: return "FC_STRUCTPAD4";
  147. case FC_STRUCTPAD5: return "FC_STRUCTPAD5";
  148. case FC_STRUCTPAD6: return "FC_STRUCTPAD6";
  149. case FC_STRUCTPAD7: return "FC_STRUCTPAD7";
  150. case FC_STRING_SIZED: return "FC_STRING_SIZED";
  151. case FC_NO_REPEAT: return "FC_NO_REPEAT";
  152. case FC_FIXED_REPEAT: return "FC_FIXED_REPEAT";
  153. case FC_VARIABLE_REPEAT: return "FC_VARIABLE_REPEAT";
  154. case FC_FIXED_OFFSET: return "FC_FIXED_OFFSET";
  155. case FC_VARIABLE_OFFSET: return "FC_VARIABLE_OFFSET";
  156. case FC_PP: return "FC_PP";
  157. case FC_EMBEDDED_COMPLEX: return "FC_EMBEDDED_COMPLEX";
  158. case FC_DEREFERENCE: return "FC_DEREFERENCE";
  159. case FC_DIV_2: return "FC_DIV_2";
  160. case FC_MULT_2: return "FC_MULT_2";
  161. case FC_ADD_1: return "FC_ADD_1";
  162. case FC_SUB_1: return "FC_SUB_1";
  163. case FC_CALLBACK: return "FC_CALLBACK";
  164. case FC_CONSTANT_IID: return "FC_CONSTANT_IID";
  165. case FC_END: return "FC_END";
  166. case FC_PAD: return "FC_PAD";
  167. case FC_USER_MARSHAL: return "FC_USER_MARSHAL";
  168. case FC_RANGE: return "FC_RANGE";
  169. case FC_INT3264: return "FC_INT3264";
  170. case FC_UINT3264: return "FC_UINT3264";
  171. default:
  172. error("string_of_type: unknown type 0x%02x\n", type);
  173. return NULL;
  174. }
  175. }
  176. static void *get_aliaschain_attrp(const type_t *type, enum attr_type attr)
  177. {
  178. const type_t *t = type;
  179. for (;;)
  180. {
  181. if (is_attr(t->attrs, attr))
  182. return get_attrp(t->attrs, attr);
  183. else if (type_is_alias(t))
  184. t = type_alias_get_aliasee_type(t);
  185. else return NULL;
  186. }
  187. }
  188. unsigned char get_basic_fc(const type_t *type)
  189. {
  190. int sign = type_basic_get_sign(type);
  191. switch (type_basic_get_type(type))
  192. {
  193. case TYPE_BASIC_INT8: return (sign <= 0 ? FC_SMALL : FC_USMALL);
  194. case TYPE_BASIC_INT16: return (sign <= 0 ? FC_SHORT : FC_USHORT);
  195. case TYPE_BASIC_INT32:
  196. case TYPE_BASIC_LONG: return (sign <= 0 ? FC_LONG : FC_ULONG);
  197. case TYPE_BASIC_INT64: return FC_HYPER;
  198. case TYPE_BASIC_INT: return (sign <= 0 ? FC_LONG : FC_ULONG);
  199. case TYPE_BASIC_INT3264: return (sign <= 0 ? FC_INT3264 : FC_UINT3264);
  200. case TYPE_BASIC_BYTE: return FC_BYTE;
  201. case TYPE_BASIC_CHAR: return FC_CHAR;
  202. case TYPE_BASIC_WCHAR: return FC_WCHAR;
  203. case TYPE_BASIC_HYPER: return FC_HYPER;
  204. case TYPE_BASIC_FLOAT: return FC_FLOAT;
  205. case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
  206. case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
  207. case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
  208. }
  209. return 0;
  210. }
  211. static unsigned char get_basic_fc_signed(const type_t *type)
  212. {
  213. switch (type_basic_get_type(type))
  214. {
  215. case TYPE_BASIC_INT8: return FC_SMALL;
  216. case TYPE_BASIC_INT16: return FC_SHORT;
  217. case TYPE_BASIC_INT32: return FC_LONG;
  218. case TYPE_BASIC_INT64: return FC_HYPER;
  219. case TYPE_BASIC_INT: return FC_LONG;
  220. case TYPE_BASIC_INT3264: return FC_INT3264;
  221. case TYPE_BASIC_LONG: return FC_LONG;
  222. case TYPE_BASIC_BYTE: return FC_BYTE;
  223. case TYPE_BASIC_CHAR: return FC_CHAR;
  224. case TYPE_BASIC_WCHAR: return FC_WCHAR;
  225. case TYPE_BASIC_HYPER: return FC_HYPER;
  226. case TYPE_BASIC_FLOAT: return FC_FLOAT;
  227. case TYPE_BASIC_DOUBLE: return FC_DOUBLE;
  228. case TYPE_BASIC_ERROR_STATUS_T: return FC_ERROR_STATUS_T;
  229. case TYPE_BASIC_HANDLE: return FC_BIND_PRIMITIVE;
  230. }
  231. return 0;
  232. }
  233. static inline unsigned int clamp_align(unsigned int align)
  234. {
  235. unsigned int packing = (pointer_size == 4) ? win32_packing : win64_packing;
  236. if(align > packing) align = packing;
  237. return align;
  238. }
  239. unsigned char get_pointer_fc(const type_t *type, const attr_list_t *attrs, int toplevel_param)
  240. {
  241. const type_t *t;
  242. int pointer_type;
  243. assert(is_ptr(type) || is_array(type));
  244. pointer_type = get_attrv(attrs, ATTR_POINTERTYPE);
  245. if (pointer_type)
  246. return pointer_type;
  247. for (t = type; type_is_alias(t); t = type_alias_get_aliasee_type(t))
  248. {
  249. pointer_type = get_attrv(t->attrs, ATTR_POINTERTYPE);
  250. if (pointer_type)
  251. return pointer_type;
  252. }
  253. if (toplevel_param)
  254. return FC_RP;
  255. if ((pointer_type = get_attrv(current_iface->attrs, ATTR_POINTERDEFAULT)))
  256. return pointer_type;
  257. return FC_UP;
  258. }
  259. static unsigned char get_pointer_fc_context( const type_t *type, const attr_list_t *attrs,
  260. enum type_context context )
  261. {
  262. int pointer_fc = get_pointer_fc(type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM);
  263. if (pointer_fc == FC_UP && is_attr( attrs, ATTR_OUT ) &&
  264. (context == TYPE_CONTEXT_PARAM || context == TYPE_CONTEXT_RETVAL) && is_object( current_iface ))
  265. pointer_fc = FC_OP;
  266. return pointer_fc;
  267. }
  268. static unsigned char get_enum_fc(const type_t *type)
  269. {
  270. assert(type_get_type(type) == TYPE_ENUM);
  271. if (is_aliaschain_attr(type, ATTR_V1ENUM))
  272. return FC_ENUM32;
  273. else
  274. return FC_ENUM16;
  275. }
  276. static type_t *get_user_type(const type_t *t, const char **pname)
  277. {
  278. for (;;)
  279. {
  280. type_t *ut = get_attrp(t->attrs, ATTR_WIREMARSHAL);
  281. if (ut)
  282. {
  283. if (pname)
  284. *pname = t->name;
  285. return ut;
  286. }
  287. if (type_is_alias(t))
  288. t = type_alias_get_aliasee_type(t);
  289. else
  290. return NULL;
  291. }
  292. }
  293. static int is_user_type(const type_t *t)
  294. {
  295. return get_user_type(t, NULL) != NULL;
  296. }
  297. enum typegen_type typegen_detect_type(const type_t *type, const attr_list_t *attrs, unsigned int flags)
  298. {
  299. if (is_user_type(type))
  300. return TGT_USER_TYPE;
  301. if (is_aliaschain_attr(type, ATTR_CONTEXTHANDLE))
  302. return TGT_CTXT_HANDLE;
  303. if (!(flags & TDT_IGNORE_STRINGS) && is_string_type(attrs, type))
  304. return TGT_STRING;
  305. switch (type_get_type(type))
  306. {
  307. case TYPE_BASIC:
  308. if (!(flags & TDT_IGNORE_RANGES) &&
  309. (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
  310. return TGT_RANGE;
  311. return TGT_BASIC;
  312. case TYPE_ENUM:
  313. if (!(flags & TDT_IGNORE_RANGES) &&
  314. (is_attr(attrs, ATTR_RANGE) || is_aliaschain_attr(type, ATTR_RANGE)))
  315. return TGT_RANGE;
  316. return TGT_ENUM;
  317. case TYPE_POINTER:
  318. if (type_get_type(type_pointer_get_ref_type(type)) == TYPE_INTERFACE ||
  319. type_get_type(type_pointer_get_ref_type(type)) == TYPE_RUNTIMECLASS ||
  320. type_get_type(type_pointer_get_ref_type(type)) == TYPE_DELEGATE ||
  321. (type_get_type(type_pointer_get_ref_type(type)) == TYPE_VOID && is_attr(attrs, ATTR_IIDIS)))
  322. return TGT_IFACE_POINTER;
  323. else if (is_aliaschain_attr(type_pointer_get_ref_type(type), ATTR_CONTEXTHANDLE))
  324. return TGT_CTXT_HANDLE_POINTER;
  325. else
  326. return TGT_POINTER;
  327. case TYPE_STRUCT:
  328. return TGT_STRUCT;
  329. case TYPE_ENCAPSULATED_UNION:
  330. case TYPE_UNION:
  331. return TGT_UNION;
  332. case TYPE_ARRAY:
  333. return TGT_ARRAY;
  334. case TYPE_FUNCTION:
  335. case TYPE_COCLASS:
  336. case TYPE_INTERFACE:
  337. case TYPE_MODULE:
  338. case TYPE_VOID:
  339. case TYPE_ALIAS:
  340. case TYPE_BITFIELD:
  341. case TYPE_RUNTIMECLASS:
  342. case TYPE_DELEGATE:
  343. break;
  344. case TYPE_APICONTRACT:
  345. case TYPE_PARAMETERIZED_TYPE:
  346. case TYPE_PARAMETER:
  347. /* not supposed to be here */
  348. assert(0);
  349. break;
  350. }
  351. return TGT_INVALID;
  352. }
  353. static int cant_be_null(const var_t *v)
  354. {
  355. switch (typegen_detect_type(v->declspec.type, v->attrs, TDT_IGNORE_STRINGS))
  356. {
  357. case TGT_ARRAY:
  358. if (!type_array_is_decl_as_ptr( v->declspec.type )) return 0;
  359. /* fall through */
  360. case TGT_POINTER:
  361. return (get_pointer_fc(v->declspec.type, v->attrs, TRUE) == FC_RP);
  362. case TGT_CTXT_HANDLE_POINTER:
  363. return TRUE;
  364. default:
  365. return 0;
  366. }
  367. }
  368. static int get_padding(const var_list_t *fields)
  369. {
  370. unsigned short offset = 0;
  371. unsigned int salign = 1;
  372. const var_t *f;
  373. if (!fields)
  374. return 0;
  375. LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
  376. {
  377. type_t *ft = f->declspec.type;
  378. unsigned int align = 0;
  379. unsigned int size = type_memsize_and_alignment(ft, &align);
  380. align = clamp_align(align);
  381. if (align > salign) salign = align;
  382. offset = ROUND_SIZE(offset, align);
  383. offset += size;
  384. }
  385. return ROUNDING(offset, salign);
  386. }
  387. static unsigned int get_stack_size( const var_t *var, int *by_value )
  388. {
  389. unsigned int stack_size;
  390. int by_val;
  391. switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
  392. {
  393. case TGT_BASIC:
  394. case TGT_ENUM:
  395. case TGT_RANGE:
  396. case TGT_STRUCT:
  397. case TGT_UNION:
  398. case TGT_USER_TYPE:
  399. stack_size = type_memsize( var->declspec.type );
  400. by_val = (pointer_size < 8 || stack_size <= pointer_size); /* FIXME: should be platform-specific */
  401. break;
  402. default:
  403. by_val = 0;
  404. break;
  405. }
  406. if (!by_val) stack_size = pointer_size;
  407. if (by_value) *by_value = by_val;
  408. return ROUND_SIZE( stack_size, pointer_size );
  409. }
  410. static unsigned char get_contexthandle_flags( const type_t *iface, const attr_list_t *attrs,
  411. const type_t *type, int is_return )
  412. {
  413. unsigned char flags = 0;
  414. int is_out;
  415. if (is_attr(iface->attrs, ATTR_STRICTCONTEXTHANDLE)) flags |= NDR_STRICT_CONTEXT_HANDLE;
  416. if (is_ptr(type) &&
  417. !is_attr( type->attrs, ATTR_CONTEXTHANDLE ) &&
  418. !is_attr( attrs, ATTR_CONTEXTHANDLE ))
  419. flags |= HANDLE_PARAM_IS_VIA_PTR;
  420. if (is_return) return flags | HANDLE_PARAM_IS_OUT | HANDLE_PARAM_IS_RETURN;
  421. is_out = is_attr(attrs, ATTR_OUT);
  422. if (is_attr(attrs, ATTR_IN) || !is_out)
  423. {
  424. flags |= HANDLE_PARAM_IS_IN;
  425. if (!is_out) flags |= NDR_CONTEXT_HANDLE_CANNOT_BE_NULL;
  426. }
  427. if (is_out) flags |= HANDLE_PARAM_IS_OUT;
  428. return flags;
  429. }
  430. static unsigned int get_rpc_flags( const attr_list_t *attrs )
  431. {
  432. unsigned int flags = 0;
  433. if (is_attr( attrs, ATTR_IDEMPOTENT )) flags |= 0x0001;
  434. if (is_attr( attrs, ATTR_BROADCAST )) flags |= 0x0002;
  435. if (is_attr( attrs, ATTR_MAYBE )) flags |= 0x0004;
  436. if (is_attr( attrs, ATTR_MESSAGE )) flags |= 0x0100;
  437. if (is_attr( attrs, ATTR_ASYNC )) flags |= 0x4000;
  438. return flags;
  439. }
  440. unsigned char get_struct_fc(const type_t *type)
  441. {
  442. int has_pointer = 0;
  443. int has_conformance = 0;
  444. int has_variance = 0;
  445. var_t *field;
  446. var_list_t *fields;
  447. fields = type_struct_get_fields(type);
  448. if (get_padding(fields))
  449. return FC_BOGUS_STRUCT;
  450. if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
  451. {
  452. type_t *t = field->declspec.type;
  453. enum typegen_type typegen_type;
  454. typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
  455. if (typegen_type == TGT_ARRAY && !type_array_is_decl_as_ptr(t))
  456. {
  457. if (is_string_type(field->attrs, field->declspec.type))
  458. {
  459. if (is_conformant_array(t))
  460. has_conformance = 1;
  461. has_variance = 1;
  462. continue;
  463. }
  464. if (is_array(type_array_get_element_type(field->declspec.type)))
  465. return FC_BOGUS_STRUCT;
  466. if (type_array_has_conformance(field->declspec.type))
  467. {
  468. has_conformance = 1;
  469. if (list_next(fields, &field->entry))
  470. error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
  471. field->name);
  472. }
  473. if (type_array_has_variance(t))
  474. has_variance = 1;
  475. t = type_array_get_element_type(t);
  476. typegen_type = typegen_detect_type(t, field->attrs, TDT_IGNORE_STRINGS);
  477. }
  478. switch (typegen_type)
  479. {
  480. case TGT_USER_TYPE:
  481. case TGT_IFACE_POINTER:
  482. return FC_BOGUS_STRUCT;
  483. case TGT_BASIC:
  484. if (type_basic_get_type(t) == TYPE_BASIC_INT3264 && pointer_size != 4)
  485. return FC_BOGUS_STRUCT;
  486. break;
  487. case TGT_ENUM:
  488. if (get_enum_fc(t) == FC_ENUM16)
  489. return FC_BOGUS_STRUCT;
  490. break;
  491. case TGT_POINTER:
  492. case TGT_ARRAY:
  493. if (get_pointer_fc(t, field->attrs, FALSE) == FC_RP || pointer_size != 4)
  494. return FC_BOGUS_STRUCT;
  495. has_pointer = 1;
  496. break;
  497. case TGT_UNION:
  498. return FC_BOGUS_STRUCT;
  499. case TGT_STRUCT:
  500. {
  501. unsigned char fc = get_struct_fc(t);
  502. switch (fc)
  503. {
  504. case FC_STRUCT:
  505. break;
  506. case FC_CVSTRUCT:
  507. has_conformance = 1;
  508. has_variance = 1;
  509. has_pointer = 1;
  510. break;
  511. case FC_CPSTRUCT:
  512. has_conformance = 1;
  513. if (list_next( fields, &field->entry ))
  514. error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
  515. field->name);
  516. has_pointer = 1;
  517. break;
  518. case FC_CSTRUCT:
  519. has_conformance = 1;
  520. if (list_next( fields, &field->entry ))
  521. error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
  522. field->name);
  523. break;
  524. case FC_PSTRUCT:
  525. has_pointer = 1;
  526. break;
  527. default:
  528. error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, fc);
  529. /* fallthru - treat it as complex */
  530. /* as soon as we see one of these these members, it's bogus... */
  531. case FC_BOGUS_STRUCT:
  532. return FC_BOGUS_STRUCT;
  533. }
  534. break;
  535. }
  536. case TGT_RANGE:
  537. return FC_BOGUS_STRUCT;
  538. case TGT_STRING:
  539. /* shouldn't get here because of TDT_IGNORE_STRINGS above. fall through */
  540. case TGT_INVALID:
  541. case TGT_CTXT_HANDLE:
  542. case TGT_CTXT_HANDLE_POINTER:
  543. /* checking after parsing should mean that we don't get here. if we do,
  544. * it's a checker bug */
  545. assert(0);
  546. }
  547. }
  548. if( has_variance )
  549. {
  550. if ( has_conformance )
  551. return FC_CVSTRUCT;
  552. else
  553. return FC_BOGUS_STRUCT;
  554. }
  555. if( has_conformance && has_pointer )
  556. return FC_CPSTRUCT;
  557. if( has_conformance )
  558. return FC_CSTRUCT;
  559. if( has_pointer )
  560. return FC_PSTRUCT;
  561. return FC_STRUCT;
  562. }
  563. static unsigned char get_array_fc(const type_t *type)
  564. {
  565. unsigned char fc;
  566. const expr_t *size_is;
  567. const type_t *elem_type;
  568. elem_type = type_array_get_element_type(type);
  569. size_is = type_array_get_conformance(type);
  570. if (!size_is)
  571. {
  572. unsigned int size = type_memsize(elem_type);
  573. if (size * type_array_get_dim(type) > 0xffffuL)
  574. fc = FC_LGFARRAY;
  575. else
  576. fc = FC_SMFARRAY;
  577. }
  578. else
  579. fc = FC_CARRAY;
  580. if (type_array_has_variance(type))
  581. {
  582. if (fc == FC_SMFARRAY)
  583. fc = FC_SMVARRAY;
  584. else if (fc == FC_LGFARRAY)
  585. fc = FC_LGVARRAY;
  586. else if (fc == FC_CARRAY)
  587. fc = FC_CVARRAY;
  588. }
  589. switch (typegen_detect_type(elem_type, NULL, TDT_IGNORE_STRINGS))
  590. {
  591. case TGT_USER_TYPE:
  592. fc = FC_BOGUS_ARRAY;
  593. break;
  594. case TGT_BASIC:
  595. if (type_basic_get_type(elem_type) == TYPE_BASIC_INT3264 &&
  596. pointer_size != 4)
  597. fc = FC_BOGUS_ARRAY;
  598. break;
  599. case TGT_STRUCT:
  600. switch (get_struct_fc(elem_type))
  601. {
  602. case FC_BOGUS_STRUCT:
  603. fc = FC_BOGUS_ARRAY;
  604. break;
  605. }
  606. break;
  607. case TGT_ENUM:
  608. /* is 16-bit enum - if so, wire size differs from mem size and so
  609. * the array cannot be block copied, which means the array is complex */
  610. if (get_enum_fc(elem_type) == FC_ENUM16)
  611. fc = FC_BOGUS_ARRAY;
  612. break;
  613. case TGT_UNION:
  614. case TGT_IFACE_POINTER:
  615. fc = FC_BOGUS_ARRAY;
  616. break;
  617. case TGT_POINTER:
  618. /* ref pointers cannot just be block copied. unique pointers to
  619. * interfaces need special treatment. either case means the array is
  620. * complex */
  621. if (get_pointer_fc(elem_type, NULL, FALSE) == FC_RP || pointer_size != 4)
  622. fc = FC_BOGUS_ARRAY;
  623. break;
  624. case TGT_RANGE:
  625. fc = FC_BOGUS_ARRAY;
  626. break;
  627. case TGT_CTXT_HANDLE:
  628. case TGT_CTXT_HANDLE_POINTER:
  629. case TGT_STRING:
  630. case TGT_INVALID:
  631. case TGT_ARRAY:
  632. /* nothing to do for everything else */
  633. break;
  634. }
  635. return fc;
  636. }
  637. static int is_non_complex_struct(const type_t *type)
  638. {
  639. return (type_get_type(type) == TYPE_STRUCT &&
  640. get_struct_fc(type) != FC_BOGUS_STRUCT);
  641. }
  642. static int type_has_pointers(const type_t *type)
  643. {
  644. switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
  645. {
  646. case TGT_USER_TYPE:
  647. return FALSE;
  648. case TGT_POINTER:
  649. return TRUE;
  650. case TGT_ARRAY:
  651. return type_array_is_decl_as_ptr(type) || type_has_pointers(type_array_get_element_type(type));
  652. case TGT_STRUCT:
  653. {
  654. var_list_t *fields = type_struct_get_fields(type);
  655. const var_t *field;
  656. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  657. {
  658. if (type_has_pointers(field->declspec.type))
  659. return TRUE;
  660. }
  661. break;
  662. }
  663. case TGT_UNION:
  664. {
  665. var_list_t *fields;
  666. const var_t *field;
  667. fields = type_union_get_cases(type);
  668. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  669. {
  670. if (field->declspec.type && type_has_pointers(field->declspec.type))
  671. return TRUE;
  672. }
  673. break;
  674. }
  675. case TGT_CTXT_HANDLE:
  676. case TGT_CTXT_HANDLE_POINTER:
  677. case TGT_STRING:
  678. case TGT_IFACE_POINTER:
  679. case TGT_BASIC:
  680. case TGT_ENUM:
  681. case TGT_RANGE:
  682. case TGT_INVALID:
  683. break;
  684. }
  685. return FALSE;
  686. }
  687. static int type_has_full_pointer(const type_t *type, const attr_list_t *attrs,
  688. int toplevel_param)
  689. {
  690. switch (typegen_detect_type(type, NULL, TDT_IGNORE_STRINGS))
  691. {
  692. case TGT_USER_TYPE:
  693. return FALSE;
  694. case TGT_POINTER:
  695. if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
  696. return TRUE;
  697. else
  698. return FALSE;
  699. case TGT_ARRAY:
  700. if (get_pointer_fc(type, attrs, toplevel_param) == FC_FP)
  701. return TRUE;
  702. else
  703. return type_has_full_pointer(type_array_get_element_type(type), NULL, FALSE);
  704. case TGT_STRUCT:
  705. {
  706. var_list_t *fields = type_struct_get_fields(type);
  707. const var_t *field;
  708. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  709. {
  710. if (type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
  711. return TRUE;
  712. }
  713. break;
  714. }
  715. case TGT_UNION:
  716. {
  717. var_list_t *fields;
  718. const var_t *field;
  719. fields = type_union_get_cases(type);
  720. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  721. {
  722. if (field->declspec.type && type_has_full_pointer(field->declspec.type, field->attrs, FALSE))
  723. return TRUE;
  724. }
  725. break;
  726. }
  727. case TGT_CTXT_HANDLE:
  728. case TGT_CTXT_HANDLE_POINTER:
  729. case TGT_STRING:
  730. case TGT_IFACE_POINTER:
  731. case TGT_BASIC:
  732. case TGT_ENUM:
  733. case TGT_RANGE:
  734. case TGT_INVALID:
  735. break;
  736. }
  737. return FALSE;
  738. }
  739. static unsigned short user_type_offset(const char *name)
  740. {
  741. user_type_t *ut;
  742. unsigned short off = 0;
  743. LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
  744. {
  745. if (strcmp(name, ut->name) == 0)
  746. return off;
  747. ++off;
  748. }
  749. error("user_type_offset: couldn't find type (%s)\n", name);
  750. return 0;
  751. }
  752. static void update_tfsoff(type_t *type, unsigned int offset, FILE *file)
  753. {
  754. type->typestring_offset = offset;
  755. if (file) type->tfswrite = FALSE;
  756. }
  757. static void guard_rec(type_t *type)
  758. {
  759. /* types that contain references to themselves (like a linked list),
  760. need to be shielded from infinite recursion when writing embedded
  761. types */
  762. if (type->typestring_offset)
  763. type->tfswrite = FALSE;
  764. else
  765. type->typestring_offset = 1;
  766. }
  767. static int is_embedded_complex(const type_t *type)
  768. {
  769. switch (typegen_detect_type(type, NULL, TDT_ALL_TYPES))
  770. {
  771. case TGT_USER_TYPE:
  772. case TGT_STRUCT:
  773. case TGT_UNION:
  774. case TGT_ARRAY:
  775. case TGT_IFACE_POINTER:
  776. return TRUE;
  777. default:
  778. return FALSE;
  779. }
  780. }
  781. static const char *get_context_handle_type_name(const type_t *type)
  782. {
  783. const type_t *t;
  784. for (t = type;
  785. is_ptr(t) || type_is_alias(t);
  786. t = type_is_alias(t) ? type_alias_get_aliasee_type(t) : type_pointer_get_ref_type(t))
  787. if (is_attr(t->attrs, ATTR_CONTEXTHANDLE))
  788. return t->name;
  789. assert(0);
  790. return NULL;
  791. }
  792. #define WRITE_FCTYPE(file, fctype, typestring_offset) \
  793. do { \
  794. if (file) \
  795. fprintf(file, "/* %2u */\n", typestring_offset); \
  796. print_file((file), 2, "0x%02x,\t/* " #fctype " */\n", fctype); \
  797. } \
  798. while (0)
  799. static void print_file(FILE *file, int indent, const char *format, ...) __attribute__((format (printf, 3, 4)));
  800. static void print_file(FILE *file, int indent, const char *format, ...)
  801. {
  802. va_list va;
  803. va_start(va, format);
  804. print(file, indent, format, va);
  805. va_end(va);
  806. }
  807. void print(FILE *file, int indent, const char *format, va_list va)
  808. {
  809. if (file)
  810. {
  811. if (format[0] != '\n')
  812. while (0 < indent--)
  813. fprintf(file, " ");
  814. vfprintf(file, format, va);
  815. }
  816. }
  817. static void write_var_init(FILE *file, int indent, const type_t *t, const char *n, const char *local_var_prefix)
  818. {
  819. if (decl_indirect(t))
  820. {
  821. print_file(file, indent, "MIDL_memset(&%s%s, 0, sizeof(%s%s));\n",
  822. local_var_prefix, n, local_var_prefix, n);
  823. print_file(file, indent, "%s_p_%s = &%s%s;\n", local_var_prefix, n, local_var_prefix, n);
  824. }
  825. else if (is_ptr(t) || is_array(t))
  826. print_file(file, indent, "%s%s = 0;\n", local_var_prefix, n);
  827. }
  828. void write_parameters_init(FILE *file, int indent, const var_t *func, const char *local_var_prefix)
  829. {
  830. const var_t *var = type_function_get_retval(func->declspec.type);
  831. if (!is_void(var->declspec.type))
  832. write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
  833. if (!type_function_get_args(func->declspec.type))
  834. return;
  835. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  836. write_var_init(file, indent, var->declspec.type, var->name, local_var_prefix);
  837. fprintf(file, "\n");
  838. }
  839. static void write_formatdesc(FILE *f, int indent, const char *str)
  840. {
  841. print_file(f, indent, "typedef struct _MIDL_%s_FORMAT_STRING\n", str);
  842. print_file(f, indent, "{\n");
  843. print_file(f, indent + 1, "short Pad;\n");
  844. print_file(f, indent + 1, "unsigned char Format[%s_FORMAT_STRING_SIZE];\n", str);
  845. print_file(f, indent, "} MIDL_%s_FORMAT_STRING;\n", str);
  846. print_file(f, indent, "\n");
  847. }
  848. void write_formatstringsdecl(FILE *f, int indent, const statement_list_t *stmts, type_pred_t pred)
  849. {
  850. clear_all_offsets();
  851. print_file(f, indent, "#define TYPE_FORMAT_STRING_SIZE %d\n",
  852. get_size_typeformatstring(stmts, pred));
  853. print_file(f, indent, "#define PROC_FORMAT_STRING_SIZE %d\n",
  854. get_size_procformatstring(stmts, pred));
  855. fprintf(f, "\n");
  856. write_formatdesc(f, indent, "TYPE");
  857. write_formatdesc(f, indent, "PROC");
  858. fprintf(f, "\n");
  859. print_file(f, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString;\n");
  860. print_file(f, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString;\n");
  861. print_file(f, indent, "\n");
  862. }
  863. int decl_indirect(const type_t *t)
  864. {
  865. if (is_user_type(t))
  866. return TRUE;
  867. return (type_get_type(t) != TYPE_BASIC &&
  868. type_get_type(t) != TYPE_ENUM &&
  869. type_get_type(t) != TYPE_POINTER &&
  870. type_get_type(t) != TYPE_ARRAY);
  871. }
  872. static unsigned char get_parameter_fc( const var_t *var, int is_return, unsigned short *flags,
  873. unsigned int *stack_size, unsigned int *typestring_offset )
  874. {
  875. unsigned int alignment, server_size = 0, buffer_size = 0;
  876. unsigned char fc = 0;
  877. int is_byval;
  878. int is_in = is_attr(var->attrs, ATTR_IN);
  879. int is_out = is_attr(var->attrs, ATTR_OUT);
  880. if (is_return) is_out = TRUE;
  881. else if (!is_in && !is_out) is_in = TRUE;
  882. *flags = 0;
  883. *stack_size = get_stack_size( var, &is_byval );
  884. *typestring_offset = var->typestring_offset;
  885. if (is_in) *flags |= IsIn;
  886. if (is_out) *flags |= IsOut;
  887. if (is_return) *flags |= IsReturn;
  888. if (!is_string_type( var->attrs, var->declspec.type ))
  889. buffer_size = get_required_buffer_size_type( var->declspec.type, NULL, var->attrs, TRUE, &alignment );
  890. switch (typegen_detect_type( var->declspec.type, var->attrs, TDT_ALL_TYPES ))
  891. {
  892. case TGT_BASIC:
  893. *flags |= IsBasetype;
  894. fc = get_basic_fc_signed( var->declspec.type );
  895. if (fc == FC_BIND_PRIMITIVE)
  896. {
  897. buffer_size = 4; /* actually 0 but avoids setting MustSize */
  898. fc = FC_LONG;
  899. }
  900. break;
  901. case TGT_ENUM:
  902. *flags |= IsBasetype;
  903. fc = get_enum_fc( var->declspec.type );
  904. break;
  905. case TGT_RANGE:
  906. *flags |= IsByValue;
  907. break;
  908. case TGT_STRUCT:
  909. case TGT_UNION:
  910. case TGT_USER_TYPE:
  911. *flags |= MustFree | (is_byval ? IsByValue : IsSimpleRef);
  912. break;
  913. case TGT_IFACE_POINTER:
  914. *flags |= MustFree;
  915. break;
  916. case TGT_ARRAY:
  917. *flags |= MustFree;
  918. if (type_array_is_decl_as_ptr(var->declspec.type)
  919. && type_array_get_ptr_tfsoff(var->declspec.type)
  920. && get_pointer_fc(var->declspec.type, var->attrs, !is_return) == FC_RP)
  921. {
  922. *typestring_offset = var->declspec.type->typestring_offset;
  923. *flags |= IsSimpleRef;
  924. }
  925. break;
  926. case TGT_STRING:
  927. *flags |= MustFree;
  928. if (is_declptr( var->declspec.type ) && get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
  929. {
  930. /* skip over pointer description straight to string description */
  931. if (is_conformant_array( var->declspec.type )) *typestring_offset += 4;
  932. else *typestring_offset += 2;
  933. *flags |= IsSimpleRef;
  934. }
  935. break;
  936. case TGT_CTXT_HANDLE_POINTER:
  937. *flags |= IsSimpleRef;
  938. *typestring_offset += 4;
  939. /* fall through */
  940. case TGT_CTXT_HANDLE:
  941. buffer_size = 20;
  942. break;
  943. case TGT_POINTER:
  944. if (get_pointer_fc( var->declspec.type, var->attrs, !is_return ) == FC_RP)
  945. {
  946. const type_t *ref = type_pointer_get_ref_type( var->declspec.type );
  947. if (!is_string_type( var->attrs, ref ))
  948. buffer_size = get_required_buffer_size_type( ref, NULL, NULL, TRUE, &alignment );
  949. switch (typegen_detect_type( ref, NULL, TDT_ALL_TYPES ))
  950. {
  951. case TGT_BASIC:
  952. *flags |= IsSimpleRef | IsBasetype;
  953. fc = get_basic_fc( ref );
  954. if (!is_in && is_out) server_size = pointer_size;
  955. break;
  956. case TGT_ENUM:
  957. if ((fc = get_enum_fc( ref )) == FC_ENUM32)
  958. {
  959. *flags |= IsSimpleRef | IsBasetype;
  960. if (!is_in && is_out) server_size = pointer_size;
  961. }
  962. else
  963. {
  964. server_size = pointer_size;
  965. }
  966. break;
  967. case TGT_UNION:
  968. case TGT_USER_TYPE:
  969. case TGT_RANGE:
  970. *flags |= MustFree | IsSimpleRef;
  971. *typestring_offset = ref->typestring_offset;
  972. if (!is_in && is_out) server_size = type_memsize( ref );
  973. break;
  974. case TGT_ARRAY:
  975. *flags |= MustFree;
  976. if (!type_array_is_decl_as_ptr(ref))
  977. {
  978. *flags |= IsSimpleRef;
  979. *typestring_offset = ref->typestring_offset;
  980. }
  981. if (!is_in && is_out) server_size = type_memsize( ref );
  982. break;
  983. case TGT_STRING:
  984. case TGT_POINTER:
  985. case TGT_CTXT_HANDLE:
  986. case TGT_CTXT_HANDLE_POINTER:
  987. *flags |= MustFree;
  988. server_size = pointer_size;
  989. break;
  990. case TGT_IFACE_POINTER:
  991. *flags |= MustFree;
  992. if (is_in && is_out) server_size = pointer_size;
  993. break;
  994. case TGT_STRUCT:
  995. *flags |= IsSimpleRef | MustFree;
  996. *typestring_offset = ref->typestring_offset;
  997. switch (get_struct_fc(ref))
  998. {
  999. case FC_STRUCT:
  1000. case FC_PSTRUCT:
  1001. case FC_BOGUS_STRUCT:
  1002. if (!is_in && is_out) server_size = type_memsize( ref );
  1003. break;
  1004. default:
  1005. break;
  1006. }
  1007. break;
  1008. case TGT_INVALID:
  1009. assert(0);
  1010. }
  1011. }
  1012. else /* not ref pointer */
  1013. {
  1014. *flags |= MustFree;
  1015. }
  1016. break;
  1017. case TGT_INVALID:
  1018. assert(0);
  1019. }
  1020. if (!buffer_size) *flags |= MustSize;
  1021. if (server_size)
  1022. {
  1023. server_size = (server_size + 7) / 8;
  1024. if (server_size < 8) *flags |= server_size << 13;
  1025. }
  1026. return fc;
  1027. }
  1028. static unsigned char get_func_oi2_flags( const var_t *func )
  1029. {
  1030. const var_t *var;
  1031. var_list_t *args = type_function_get_args( func->declspec.type );
  1032. var_t *retval = type_function_get_retval( func->declspec.type );
  1033. unsigned char oi2_flags = 0x40; /* HasExtensions */
  1034. unsigned short flags;
  1035. unsigned int stack_size, typestring_offset;
  1036. if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
  1037. {
  1038. get_parameter_fc( var, 0, &flags, &stack_size, &typestring_offset );
  1039. if (flags & MustSize)
  1040. {
  1041. if (flags & IsIn) oi2_flags |= 0x02; /* ClientMustSize */
  1042. if (flags & IsOut) oi2_flags |= 0x01; /* ServerMustSize */
  1043. }
  1044. }
  1045. if (!is_void( retval->declspec.type ))
  1046. {
  1047. oi2_flags |= 0x04; /* HasRet */
  1048. get_parameter_fc( retval, 1, &flags, &stack_size, &typestring_offset );
  1049. if (flags & MustSize) oi2_flags |= 0x01; /* ServerMustSize */
  1050. }
  1051. return oi2_flags;
  1052. }
  1053. static unsigned int write_new_procformatstring_type(FILE *file, int indent, const var_t *var,
  1054. int is_return, unsigned int *stack_offset)
  1055. {
  1056. char buffer[128];
  1057. unsigned int stack_size, typestring_offset;
  1058. unsigned short flags;
  1059. unsigned char fc = get_parameter_fc( var, is_return, &flags, &stack_size, &typestring_offset );
  1060. strcpy( buffer, "/* flags:" );
  1061. if (flags & MustSize) strcat( buffer, " must size," );
  1062. if (flags & MustFree) strcat( buffer, " must free," );
  1063. if (flags & IsPipe) strcat( buffer, " pipe," );
  1064. if (flags & IsIn) strcat( buffer, " in," );
  1065. if (flags & IsOut) strcat( buffer, " out," );
  1066. if (flags & IsReturn) strcat( buffer, " return," );
  1067. if (flags & IsBasetype) strcat( buffer, " base type," );
  1068. if (flags & IsByValue) strcat( buffer, " by value," );
  1069. if (flags & IsSimpleRef) strcat( buffer, " simple ref," );
  1070. if (flags >> 13) sprintf( buffer + strlen(buffer), " srv size=%u,", (flags >> 13) * 8 );
  1071. strcpy( buffer + strlen( buffer ) - 1, " */" );
  1072. print_file( file, indent, "NdrFcShort(0x%hx),\t%s\n", flags, buffer );
  1073. print_file( file, indent, "NdrFcShort(0x%x), /* stack offset = %u */\n",
  1074. *stack_offset, *stack_offset );
  1075. if (flags & IsBasetype)
  1076. {
  1077. print_file( file, indent, "0x%02x, /* %s */\n", fc, string_of_type(fc) );
  1078. print_file( file, indent, "0x0,\n" );
  1079. }
  1080. else
  1081. print_file( file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n",
  1082. typestring_offset, typestring_offset );
  1083. *stack_offset += max( stack_size, pointer_size );
  1084. return 6;
  1085. }
  1086. static unsigned int write_old_procformatstring_type(FILE *file, int indent, const var_t *var,
  1087. int is_return, int is_interpreted)
  1088. {
  1089. unsigned int size;
  1090. int is_in = is_attr(var->attrs, ATTR_IN);
  1091. int is_out = is_attr(var->attrs, ATTR_OUT);
  1092. if (!is_in && !is_out) is_in = TRUE;
  1093. if (type_get_type(var->declspec.type) == TYPE_BASIC ||
  1094. type_get_type(var->declspec.type) == TYPE_ENUM)
  1095. {
  1096. unsigned char fc;
  1097. if (is_return)
  1098. print_file(file, indent, "0x53, /* FC_RETURN_PARAM_BASETYPE */\n");
  1099. else
  1100. print_file(file, indent, "0x4e, /* FC_IN_PARAM_BASETYPE */\n");
  1101. if (type_get_type(var->declspec.type) == TYPE_ENUM)
  1102. {
  1103. fc = get_enum_fc(var->declspec.type);
  1104. }
  1105. else
  1106. {
  1107. fc = get_basic_fc_signed(var->declspec.type);
  1108. if (fc == FC_BIND_PRIMITIVE)
  1109. fc = FC_IGNORE;
  1110. }
  1111. print_file(file, indent, "0x%02x, /* %s */\n",
  1112. fc, string_of_type(fc));
  1113. size = 2; /* includes param type prefix */
  1114. }
  1115. else
  1116. {
  1117. unsigned short offset = var->typestring_offset;
  1118. if (!is_interpreted && is_array(var->declspec.type)
  1119. && type_array_is_decl_as_ptr(var->declspec.type)
  1120. && type_array_get_ptr_tfsoff(var->declspec.type))
  1121. offset = var->declspec.type->typestring_offset;
  1122. if (is_return)
  1123. print_file(file, indent, "0x52, /* FC_RETURN_PARAM */\n");
  1124. else if (is_in && is_out)
  1125. print_file(file, indent, "0x50, /* FC_IN_OUT_PARAM */\n");
  1126. else if (is_out)
  1127. print_file(file, indent, "0x51, /* FC_OUT_PARAM */\n");
  1128. else
  1129. print_file(file, indent, "0x4d, /* FC_IN_PARAM */\n");
  1130. size = get_stack_size( var, NULL );
  1131. print_file(file, indent, "0x%02x,\n", size / pointer_size );
  1132. print_file(file, indent, "NdrFcShort(0x%x), /* type offset = %u */\n", offset, offset);
  1133. size = 4; /* includes param type prefix */
  1134. }
  1135. return size;
  1136. }
  1137. int is_interpreted_func( const type_t *iface, const var_t *func )
  1138. {
  1139. const char *str;
  1140. const var_t *var;
  1141. const var_list_t *args = type_function_get_args( func->declspec.type );
  1142. const type_t *ret_type = type_function_get_rettype( func->declspec.type );
  1143. if (type_get_type( ret_type ) == TYPE_BASIC)
  1144. {
  1145. switch (type_basic_get_type( ret_type ))
  1146. {
  1147. case TYPE_BASIC_INT64:
  1148. case TYPE_BASIC_HYPER:
  1149. /* return value must fit in a long_ptr */
  1150. if (pointer_size < 8) return 0;
  1151. break;
  1152. case TYPE_BASIC_FLOAT:
  1153. case TYPE_BASIC_DOUBLE:
  1154. /* floating point values can't be returned */
  1155. return 0;
  1156. default:
  1157. break;
  1158. }
  1159. }
  1160. if (get_stub_mode() != MODE_Oif && args)
  1161. {
  1162. LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
  1163. switch (type_get_type( var->declspec.type ))
  1164. {
  1165. case TYPE_BASIC:
  1166. switch (type_basic_get_type( var->declspec.type ))
  1167. {
  1168. /* floating point arguments are not supported in Oi mode */
  1169. case TYPE_BASIC_FLOAT: return 0;
  1170. case TYPE_BASIC_DOUBLE: return 0;
  1171. default: break;
  1172. }
  1173. break;
  1174. /* unions passed by value are not supported in Oi mode */
  1175. case TYPE_UNION: return 0;
  1176. case TYPE_ENCAPSULATED_UNION: return 0;
  1177. default: break;
  1178. }
  1179. }
  1180. if ((str = get_attrp( func->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
  1181. if ((str = get_attrp( iface->attrs, ATTR_OPTIMIZE ))) return !strcmp( str, "i" );
  1182. return (get_stub_mode() != MODE_Os);
  1183. }
  1184. static void write_proc_func_header( FILE *file, int indent, const type_t *iface,
  1185. const var_t *func, unsigned int *offset,
  1186. unsigned short num_proc )
  1187. {
  1188. var_t *var;
  1189. var_list_t *args = type_function_get_args( func->declspec.type );
  1190. unsigned char explicit_fc, implicit_fc;
  1191. unsigned char handle_flags;
  1192. const var_t *handle_var = get_func_handle_var( iface, func, &explicit_fc, &implicit_fc );
  1193. unsigned char oi_flags = Oi_HAS_RPCFLAGS | Oi_USE_NEW_INIT_ROUTINES;
  1194. unsigned int rpc_flags = get_rpc_flags( func->attrs );
  1195. unsigned int nb_args = 0;
  1196. unsigned int stack_size = 0;
  1197. unsigned short param_num = 0;
  1198. unsigned short handle_stack_offset = 0;
  1199. unsigned short handle_param_num = 0;
  1200. if (is_full_pointer_function( func )) oi_flags |= Oi_FULL_PTR_USED;
  1201. if (is_object( iface ))
  1202. {
  1203. oi_flags |= Oi_OBJECT_PROC;
  1204. if (get_stub_mode() == MODE_Oif) oi_flags |= Oi_OBJ_USE_V2_INTERPRETER;
  1205. stack_size += pointer_size;
  1206. }
  1207. if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
  1208. {
  1209. if (var == handle_var)
  1210. {
  1211. handle_stack_offset = stack_size;
  1212. handle_param_num = param_num;
  1213. }
  1214. stack_size += get_stack_size( var, NULL );
  1215. param_num++;
  1216. nb_args++;
  1217. }
  1218. if (!is_void( type_function_get_rettype( func->declspec.type )))
  1219. {
  1220. stack_size += pointer_size;
  1221. nb_args++;
  1222. }
  1223. print_file( file, 0, "/* %u (procedure %s::%s) */\n", *offset, iface->name, func->name );
  1224. print_file( file, indent, "0x%02x,\t/* %s */\n", implicit_fc,
  1225. implicit_fc ? string_of_type(implicit_fc) : "explicit handle" );
  1226. print_file( file, indent, "0x%02x,\n", oi_flags );
  1227. print_file( file, indent, "NdrFcLong(0x%x),\n", rpc_flags );
  1228. print_file( file, indent, "NdrFcShort(0x%hx),\t/* method %hu */\n", num_proc, num_proc );
  1229. print_file( file, indent, "NdrFcShort(0x%x),\t/* stack size = %u */\n", stack_size, stack_size );
  1230. *offset += 10;
  1231. if (!implicit_fc)
  1232. {
  1233. switch (explicit_fc)
  1234. {
  1235. case FC_BIND_PRIMITIVE:
  1236. handle_flags = 0;
  1237. print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
  1238. print_file( file, indent, "0x%02x,\n", handle_flags );
  1239. print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
  1240. handle_stack_offset, handle_stack_offset );
  1241. *offset += 4;
  1242. break;
  1243. case FC_BIND_GENERIC:
  1244. handle_flags = type_memsize( handle_var->declspec.type );
  1245. print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
  1246. print_file( file, indent, "0x%02x,\n", handle_flags );
  1247. print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
  1248. handle_stack_offset, handle_stack_offset );
  1249. print_file( file, indent, "0x%02x,\n", get_generic_handle_offset( handle_var->declspec.type ) );
  1250. print_file( file, indent, "0x%x,\t/* FC_PAD */\n", FC_PAD);
  1251. *offset += 6;
  1252. break;
  1253. case FC_BIND_CONTEXT:
  1254. handle_flags = get_contexthandle_flags( iface, handle_var->attrs, handle_var->declspec.type, 0 );
  1255. print_file( file, indent, "0x%02x,\t/* %s */\n", explicit_fc, string_of_type(explicit_fc) );
  1256. print_file( file, indent, "0x%02x,\n", handle_flags );
  1257. print_file( file, indent, "NdrFcShort(0x%hx),\t/* stack offset = %hu */\n",
  1258. handle_stack_offset, handle_stack_offset );
  1259. print_file( file, indent, "0x%02x,\n", get_context_handle_offset( handle_var->declspec.type ) );
  1260. print_file( file, indent, "0x%02x,\t/* param %hu */\n", handle_param_num, handle_param_num );
  1261. *offset += 6;
  1262. break;
  1263. }
  1264. }
  1265. if (get_stub_mode() == MODE_Oif)
  1266. {
  1267. unsigned char oi2_flags = get_func_oi2_flags( func );
  1268. unsigned char ext_flags = 0;
  1269. unsigned int size;
  1270. if (is_attr( func->attrs, ATTR_NOTIFY )) ext_flags |= 0x08; /* HasNotify */
  1271. if (is_attr( func->attrs, ATTR_NOTIFYFLAG )) ext_flags |= 0x10; /* HasNotify2 */
  1272. if (iface == type_iface_get_async_iface(iface)) oi2_flags |= 0x20;
  1273. size = get_function_buffer_size( func, PASS_IN );
  1274. print_file( file, indent, "NdrFcShort(0x%x),\t/* client buffer = %u */\n", size, size );
  1275. size = get_function_buffer_size( func, PASS_OUT );
  1276. print_file( file, indent, "NdrFcShort(0x%x),\t/* server buffer = %u */\n", size, size );
  1277. print_file( file, indent, "0x%02x,\n", oi2_flags );
  1278. print_file( file, indent, "0x%02x,\t/* %u params */\n", nb_args, nb_args );
  1279. print_file( file, indent, "0x%02x,\n", pointer_size == 8 ? 10 : 8 );
  1280. print_file( file, indent, "0x%02x,\n", ext_flags );
  1281. print_file( file, indent, "NdrFcShort(0x0),\n" ); /* server corr hint */
  1282. print_file( file, indent, "NdrFcShort(0x0),\n" ); /* client corr hint */
  1283. print_file( file, indent, "NdrFcShort(0x0),\n" ); /* FIXME: notify index */
  1284. *offset += 14;
  1285. if (pointer_size == 8)
  1286. {
  1287. unsigned short pos = 0, fpu_mask = 0;
  1288. if (is_object( iface )) pos += 2;
  1289. if (args) LIST_FOR_EACH_ENTRY( var, args, var_t, entry )
  1290. {
  1291. if (type_get_type( var->declspec.type ) == TYPE_BASIC)
  1292. {
  1293. switch (type_basic_get_type( var->declspec.type ))
  1294. {
  1295. case TYPE_BASIC_FLOAT: fpu_mask |= 1 << pos; break;
  1296. case TYPE_BASIC_DOUBLE: fpu_mask |= 2 << pos; break;
  1297. default: break;
  1298. }
  1299. }
  1300. pos += 2;
  1301. if (pos >= 16) break;
  1302. }
  1303. print_file( file, indent, "NdrFcShort(0x%x),\n", fpu_mask ); /* floating point mask */
  1304. *offset += 2;
  1305. }
  1306. }
  1307. }
  1308. static void write_procformatstring_func( FILE *file, int indent, const type_t *iface,
  1309. const var_t *func, unsigned int *offset,
  1310. unsigned short num_proc )
  1311. {
  1312. unsigned int stack_offset = is_object( iface ) ? pointer_size : 0;
  1313. int is_interpreted = is_interpreted_func( iface, func );
  1314. int is_new_style = is_interpreted && (get_stub_mode() == MODE_Oif);
  1315. var_t *retval = type_function_get_retval( func->declspec.type );
  1316. if (is_interpreted) write_proc_func_header( file, indent, iface, func, offset, num_proc );
  1317. /* emit argument data */
  1318. if (type_function_get_args(func->declspec.type))
  1319. {
  1320. const var_t *var;
  1321. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  1322. {
  1323. print_file( file, 0, "/* %u (parameter %s) */\n", *offset, var->name );
  1324. if (is_new_style)
  1325. *offset += write_new_procformatstring_type(file, indent, var, FALSE, &stack_offset);
  1326. else
  1327. *offset += write_old_procformatstring_type(file, indent, var, FALSE, is_interpreted);
  1328. }
  1329. }
  1330. /* emit return value data */
  1331. if (is_void(retval->declspec.type))
  1332. {
  1333. if (!is_new_style)
  1334. {
  1335. print_file(file, 0, "/* %u (void) */\n", *offset);
  1336. print_file(file, indent, "0x5b,\t/* FC_END */\n");
  1337. print_file(file, indent, "0x5c,\t/* FC_PAD */\n");
  1338. *offset += 2;
  1339. }
  1340. }
  1341. else
  1342. {
  1343. print_file( file, 0, "/* %u (return value) */\n", *offset );
  1344. if (is_new_style)
  1345. *offset += write_new_procformatstring_type(file, indent, retval, TRUE, &stack_offset);
  1346. else
  1347. *offset += write_old_procformatstring_type(file, indent, retval, TRUE, is_interpreted);
  1348. }
  1349. }
  1350. static void for_each_iface(const statement_list_t *stmts,
  1351. void (*proc)(type_t *iface, FILE *file, int indent, unsigned int *offset),
  1352. type_pred_t pred, FILE *file, int indent, unsigned int *offset)
  1353. {
  1354. const statement_t *stmt;
  1355. type_t *iface;
  1356. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, const statement_t, entry )
  1357. {
  1358. if (stmt->type != STMT_TYPE || type_get_type(stmt->u.type) != TYPE_INTERFACE)
  1359. continue;
  1360. iface = stmt->u.type;
  1361. if (!pred(iface)) continue;
  1362. proc(iface, file, indent, offset);
  1363. if (type_iface_get_async_iface(iface))
  1364. proc(type_iface_get_async_iface(iface), file, indent, offset);
  1365. }
  1366. }
  1367. static void write_iface_procformatstring(type_t *iface, FILE *file, int indent, unsigned int *offset)
  1368. {
  1369. const statement_t *stmt;
  1370. const type_t *parent = type_iface_get_inherit( iface );
  1371. int count = parent ? count_methods( parent ) : 0;
  1372. STATEMENTS_FOR_EACH_FUNC(stmt, type_iface_get_stmts(iface))
  1373. {
  1374. var_t *func = stmt->u.var;
  1375. if (is_local(func->attrs)) continue;
  1376. write_procformatstring_func( file, indent, iface, func, offset, count++ );
  1377. }
  1378. }
  1379. void write_procformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
  1380. {
  1381. int indent = 0;
  1382. unsigned int offset = 0;
  1383. print_file(file, indent, "static const MIDL_PROC_FORMAT_STRING __MIDL_ProcFormatString =\n");
  1384. print_file(file, indent, "{\n");
  1385. indent++;
  1386. print_file(file, indent, "0,\n");
  1387. print_file(file, indent, "{\n");
  1388. indent++;
  1389. for_each_iface(stmts, write_iface_procformatstring, pred, file, indent, &offset);
  1390. print_file(file, indent, "0x0\n");
  1391. indent--;
  1392. print_file(file, indent, "}\n");
  1393. indent--;
  1394. print_file(file, indent, "};\n");
  1395. print_file(file, indent, "\n");
  1396. }
  1397. void write_procformatstring_offsets( FILE *file, const type_t *iface )
  1398. {
  1399. const statement_t *stmt;
  1400. int indent = 0;
  1401. print_file( file, indent, "static const unsigned short %s_FormatStringOffsetTable[] =\n",
  1402. iface->name );
  1403. print_file( file, indent, "{\n" );
  1404. indent++;
  1405. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  1406. {
  1407. var_t *func = stmt->u.var;
  1408. if (is_local( func->attrs )) continue;
  1409. print_file( file, indent, "%u, /* %s */\n", func->procstring_offset, func->name );
  1410. }
  1411. indent--;
  1412. print_file( file, indent, "};\n\n" );
  1413. }
  1414. static int write_base_type(FILE *file, const type_t *type, unsigned int *typestring_offset)
  1415. {
  1416. unsigned char fc;
  1417. if (type_get_type(type) == TYPE_BASIC)
  1418. fc = get_basic_fc_signed(type);
  1419. else if (type_get_type(type) == TYPE_ENUM)
  1420. fc = get_enum_fc(type);
  1421. else
  1422. return 0;
  1423. print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
  1424. *typestring_offset += 1;
  1425. return 1;
  1426. }
  1427. /* write conformance / variance descriptor */
  1428. static unsigned int write_conf_or_var_desc(FILE *file, const type_t *cont_type,
  1429. unsigned int baseoff, const type_t *type,
  1430. const expr_t *expr)
  1431. {
  1432. unsigned char operator_type = 0;
  1433. unsigned char conftype = FC_NORMAL_CONFORMANCE;
  1434. const char *conftype_string = "field";
  1435. const expr_t *subexpr;
  1436. const type_t *iface = NULL;
  1437. const char *name;
  1438. if (!expr)
  1439. {
  1440. print_file(file, 2, "NdrFcLong(0xffffffff),\t/* -1 */\n");
  1441. return 4;
  1442. }
  1443. if (expr->is_const)
  1444. {
  1445. if (expr->cval > UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX)
  1446. error("write_conf_or_var_desc: constant value %d is greater than "
  1447. "the maximum constant size of %d\n", expr->cval,
  1448. UCHAR_MAX * (USHRT_MAX + 1) + USHRT_MAX);
  1449. print_file(file, 2, "0x%x, /* Corr desc: constant, val = %d */\n",
  1450. FC_CONSTANT_CONFORMANCE, expr->cval);
  1451. print_file(file, 2, "0x%x,\n", expr->cval >> 16);
  1452. print_file(file, 2, "NdrFcShort(0x%hx),\n", (unsigned short)expr->cval);
  1453. return 4;
  1454. }
  1455. if (!cont_type) /* top-level conformance */
  1456. {
  1457. conftype = FC_TOP_LEVEL_CONFORMANCE;
  1458. conftype_string = "parameter";
  1459. cont_type = current_func->declspec.type;
  1460. name = current_func->name;
  1461. iface = current_iface;
  1462. }
  1463. else
  1464. {
  1465. name = cont_type->name;
  1466. if (is_ptr(type) || (is_array(type) && type_array_is_decl_as_ptr(type)))
  1467. {
  1468. conftype = FC_POINTER_CONFORMANCE;
  1469. conftype_string = "field pointer";
  1470. }
  1471. }
  1472. subexpr = expr;
  1473. switch (subexpr->type)
  1474. {
  1475. case EXPR_PPTR:
  1476. subexpr = subexpr->ref;
  1477. operator_type = FC_DEREFERENCE;
  1478. break;
  1479. case EXPR_DIV:
  1480. if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
  1481. {
  1482. subexpr = subexpr->ref;
  1483. operator_type = FC_DIV_2;
  1484. }
  1485. break;
  1486. case EXPR_MUL:
  1487. if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 2))
  1488. {
  1489. subexpr = subexpr->ref;
  1490. operator_type = FC_MULT_2;
  1491. }
  1492. break;
  1493. case EXPR_SUB:
  1494. if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
  1495. {
  1496. subexpr = subexpr->ref;
  1497. operator_type = FC_SUB_1;
  1498. }
  1499. break;
  1500. case EXPR_ADD:
  1501. if (subexpr->u.ext->is_const && (subexpr->u.ext->cval == 1))
  1502. {
  1503. subexpr = subexpr->ref;
  1504. operator_type = FC_ADD_1;
  1505. }
  1506. break;
  1507. default:
  1508. break;
  1509. }
  1510. if (subexpr->type == EXPR_IDENTIFIER)
  1511. {
  1512. const type_t *correlation_variable = NULL;
  1513. unsigned char param_type = 0;
  1514. unsigned int offset = 0;
  1515. const var_t *var;
  1516. struct expr_loc expr_loc;
  1517. if (type_get_type(cont_type) == TYPE_FUNCTION)
  1518. {
  1519. var_list_t *args = type_function_get_args( cont_type );
  1520. if (is_object( iface )) offset += pointer_size;
  1521. if (args) LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
  1522. {
  1523. if (var->name && !strcmp(var->name, subexpr->u.sval))
  1524. {
  1525. expr_loc.v = var;
  1526. correlation_variable = var->declspec.type;
  1527. break;
  1528. }
  1529. offset += get_stack_size( var, NULL );
  1530. }
  1531. }
  1532. else
  1533. {
  1534. var_list_t *fields = type_struct_get_fields( cont_type );
  1535. if (fields) LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
  1536. {
  1537. unsigned int size = field_memsize( var->declspec.type, &offset );
  1538. if (var->name && !strcmp(var->name, subexpr->u.sval))
  1539. {
  1540. expr_loc.v = var;
  1541. correlation_variable = var->declspec.type;
  1542. break;
  1543. }
  1544. offset += size;
  1545. }
  1546. }
  1547. if (!correlation_variable)
  1548. error("write_conf_or_var_desc: couldn't find variable %s in %s\n", subexpr->u.sval, name);
  1549. expr_loc.attr = NULL;
  1550. correlation_variable = expr_resolve_type(&expr_loc, cont_type, expr);
  1551. offset -= baseoff;
  1552. if (type_get_type(correlation_variable) == TYPE_BASIC)
  1553. {
  1554. switch (get_basic_fc(correlation_variable))
  1555. {
  1556. case FC_CHAR:
  1557. case FC_SMALL:
  1558. param_type = FC_SMALL;
  1559. break;
  1560. case FC_BYTE:
  1561. case FC_USMALL:
  1562. param_type = FC_USMALL;
  1563. break;
  1564. case FC_WCHAR:
  1565. case FC_SHORT:
  1566. param_type = FC_SHORT;
  1567. break;
  1568. case FC_USHORT:
  1569. param_type = FC_USHORT;
  1570. break;
  1571. case FC_LONG:
  1572. param_type = FC_LONG;
  1573. break;
  1574. case FC_ULONG:
  1575. param_type = FC_ULONG;
  1576. break;
  1577. default:
  1578. error("write_conf_or_var_desc: conformance variable type not supported 0x%x\n",
  1579. get_basic_fc(correlation_variable));
  1580. }
  1581. }
  1582. else if (type_get_type(correlation_variable) == TYPE_ENUM)
  1583. {
  1584. if (get_enum_fc(correlation_variable) == FC_ENUM32)
  1585. param_type = FC_LONG;
  1586. else
  1587. param_type = FC_SHORT;
  1588. }
  1589. else if (type_get_type(correlation_variable) == TYPE_POINTER)
  1590. {
  1591. if (pointer_size == 8)
  1592. param_type = FC_HYPER;
  1593. else
  1594. param_type = FC_LONG;
  1595. }
  1596. else
  1597. {
  1598. error("write_conf_or_var_desc: non-arithmetic type used as correlation variable %s\n",
  1599. subexpr->u.sval);
  1600. return 0;
  1601. }
  1602. print_file(file, 2, "0x%x,\t/* Corr desc: %s %s, %s */\n",
  1603. conftype | param_type, conftype_string, subexpr->u.sval, string_of_type(param_type));
  1604. print_file(file, 2, "0x%x,\t/* %s */\n", operator_type,
  1605. operator_type ? string_of_type(operator_type) : "no operators");
  1606. print_file(file, 2, "NdrFcShort(0x%hx),\t/* offset = %d */\n",
  1607. (unsigned short)offset, offset);
  1608. }
  1609. else if (!iface || is_interpreted_func( iface, current_func ))
  1610. {
  1611. unsigned int callback_offset = 0;
  1612. struct expr_eval_routine *eval;
  1613. int found = 0;
  1614. LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
  1615. {
  1616. if (eval->cont_type == cont_type ||
  1617. (type_get_type( eval->cont_type ) == type_get_type( cont_type ) &&
  1618. eval->iface == iface &&
  1619. eval->name && name && !strcmp(eval->name, name) &&
  1620. !compare_expr(eval->expr, expr)))
  1621. {
  1622. found = 1;
  1623. break;
  1624. }
  1625. callback_offset++;
  1626. }
  1627. if (!found)
  1628. {
  1629. eval = xmalloc (sizeof(*eval));
  1630. eval->iface = iface;
  1631. eval->cont_type = cont_type;
  1632. eval->name = xstrdup( name );
  1633. eval->baseoff = baseoff;
  1634. eval->expr = expr;
  1635. list_add_tail (&expr_eval_routines, &eval->entry);
  1636. }
  1637. if (callback_offset > USHRT_MAX)
  1638. error("Maximum number of callback routines reached\n");
  1639. print_file(file, 2, "0x%x,\t/* Corr desc: %s in %s */\n", conftype, conftype_string, name);
  1640. print_file(file, 2, "0x%x,\t/* %s */\n", FC_CALLBACK, "FC_CALLBACK");
  1641. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)callback_offset, callback_offset);
  1642. }
  1643. else /* output a dummy corr desc that isn't used */
  1644. {
  1645. print_file(file, 2, "0x%x,\t/* Corr desc: unused for %s */\n", conftype, name);
  1646. print_file(file, 2, "0x0,\n" );
  1647. print_file(file, 2, "NdrFcShort(0x0),\n" );
  1648. }
  1649. return 4;
  1650. }
  1651. /* return size and start offset of a data field based on current offset */
  1652. static unsigned int field_memsize(const type_t *type, unsigned int *offset)
  1653. {
  1654. unsigned int align = 0;
  1655. unsigned int size = type_memsize_and_alignment( type, &align );
  1656. *offset = ROUND_SIZE( *offset, align );
  1657. return size;
  1658. }
  1659. static unsigned int fields_memsize(const var_list_t *fields, unsigned int *align)
  1660. {
  1661. unsigned int size = 0;
  1662. unsigned int max_align;
  1663. const var_t *v;
  1664. if (!fields) return 0;
  1665. LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
  1666. {
  1667. unsigned int falign = 0;
  1668. unsigned int fsize = type_memsize_and_alignment(v->declspec.type, &falign);
  1669. if (*align < falign) *align = falign;
  1670. falign = clamp_align(falign);
  1671. size = ROUND_SIZE(size, falign);
  1672. size += fsize;
  1673. }
  1674. max_align = clamp_align(*align);
  1675. size = ROUND_SIZE(size, max_align);
  1676. return size;
  1677. }
  1678. static unsigned int union_memsize(const var_list_t *fields, unsigned int *pmaxa)
  1679. {
  1680. unsigned int size, maxs = 0;
  1681. unsigned int align = *pmaxa;
  1682. const var_t *v;
  1683. if (fields) LIST_FOR_EACH_ENTRY( v, fields, const var_t, entry )
  1684. {
  1685. /* we could have an empty default field with NULL type */
  1686. if (v->declspec.type)
  1687. {
  1688. size = type_memsize_and_alignment(v->declspec.type, &align);
  1689. if (maxs < size) maxs = size;
  1690. if (*pmaxa < align) *pmaxa = align;
  1691. }
  1692. }
  1693. return maxs;
  1694. }
  1695. unsigned int type_memsize_and_alignment(const type_t *t, unsigned int *align)
  1696. {
  1697. unsigned int size = 0;
  1698. switch (type_get_type(t))
  1699. {
  1700. case TYPE_BASIC:
  1701. switch (get_basic_fc(t))
  1702. {
  1703. case FC_BYTE:
  1704. case FC_CHAR:
  1705. case FC_USMALL:
  1706. case FC_SMALL:
  1707. size = 1;
  1708. if (size > *align) *align = size;
  1709. break;
  1710. case FC_WCHAR:
  1711. case FC_USHORT:
  1712. case FC_SHORT:
  1713. size = 2;
  1714. if (size > *align) *align = size;
  1715. break;
  1716. case FC_ULONG:
  1717. case FC_LONG:
  1718. case FC_ERROR_STATUS_T:
  1719. case FC_FLOAT:
  1720. size = 4;
  1721. if (size > *align) *align = size;
  1722. break;
  1723. case FC_HYPER:
  1724. case FC_DOUBLE:
  1725. size = 8;
  1726. if (size > *align) *align = size;
  1727. break;
  1728. case FC_INT3264:
  1729. case FC_UINT3264:
  1730. case FC_BIND_PRIMITIVE:
  1731. assert( pointer_size );
  1732. size = pointer_size;
  1733. if (size > *align) *align = size;
  1734. break;
  1735. default:
  1736. error("type_memsize: Unknown type 0x%x\n", get_basic_fc(t));
  1737. size = 0;
  1738. }
  1739. break;
  1740. case TYPE_ENUM:
  1741. switch (get_enum_fc(t))
  1742. {
  1743. case FC_ENUM16:
  1744. case FC_ENUM32:
  1745. size = 4;
  1746. if (size > *align) *align = size;
  1747. break;
  1748. default:
  1749. error("type_memsize: Unknown enum type\n");
  1750. size = 0;
  1751. }
  1752. break;
  1753. case TYPE_STRUCT:
  1754. size = fields_memsize(type_struct_get_fields(t), align);
  1755. break;
  1756. case TYPE_ENCAPSULATED_UNION:
  1757. size = fields_memsize(type_encapsulated_union_get_fields(t), align);
  1758. break;
  1759. case TYPE_UNION:
  1760. size = union_memsize(type_union_get_cases(t), align);
  1761. break;
  1762. case TYPE_POINTER:
  1763. case TYPE_INTERFACE:
  1764. assert( pointer_size );
  1765. size = pointer_size;
  1766. if (size > *align) *align = size;
  1767. break;
  1768. case TYPE_ARRAY:
  1769. if (!type_array_is_decl_as_ptr(t))
  1770. {
  1771. if (is_conformant_array(t))
  1772. {
  1773. type_memsize_and_alignment(type_array_get_element_type(t), align);
  1774. size = 0;
  1775. }
  1776. else
  1777. size = type_array_get_dim(t) *
  1778. type_memsize_and_alignment(type_array_get_element_type(t), align);
  1779. }
  1780. else /* declared as a pointer */
  1781. {
  1782. assert( pointer_size );
  1783. size = pointer_size;
  1784. if (size > *align) *align = size;
  1785. }
  1786. break;
  1787. case TYPE_ALIAS:
  1788. case TYPE_VOID:
  1789. case TYPE_COCLASS:
  1790. case TYPE_MODULE:
  1791. case TYPE_FUNCTION:
  1792. case TYPE_BITFIELD:
  1793. case TYPE_APICONTRACT:
  1794. case TYPE_RUNTIMECLASS:
  1795. case TYPE_PARAMETERIZED_TYPE:
  1796. case TYPE_PARAMETER:
  1797. case TYPE_DELEGATE:
  1798. /* these types should not be encountered here due to language
  1799. * restrictions (interface, void, coclass, module), logical
  1800. * restrictions (alias - due to type_get_type call above) or
  1801. * checking restrictions (function, bitfield). */
  1802. assert(0);
  1803. }
  1804. return size;
  1805. }
  1806. unsigned int type_memsize(const type_t *t)
  1807. {
  1808. unsigned int align = 0;
  1809. return type_memsize_and_alignment( t, &align );
  1810. }
  1811. static unsigned int type_buffer_alignment(const type_t *t)
  1812. {
  1813. const var_list_t *fields;
  1814. const var_t *var;
  1815. unsigned int max = 0, align;
  1816. switch (type_get_type(t))
  1817. {
  1818. case TYPE_BASIC:
  1819. switch (get_basic_fc(t))
  1820. {
  1821. case FC_BYTE:
  1822. case FC_CHAR:
  1823. case FC_USMALL:
  1824. case FC_SMALL:
  1825. return 1;
  1826. case FC_WCHAR:
  1827. case FC_USHORT:
  1828. case FC_SHORT:
  1829. return 2;
  1830. case FC_ULONG:
  1831. case FC_LONG:
  1832. case FC_ERROR_STATUS_T:
  1833. case FC_FLOAT:
  1834. case FC_INT3264:
  1835. case FC_UINT3264:
  1836. return 4;
  1837. case FC_HYPER:
  1838. case FC_DOUBLE:
  1839. return 8;
  1840. default:
  1841. error("type_buffer_alignment: Unknown type 0x%x\n", get_basic_fc(t));
  1842. }
  1843. break;
  1844. case TYPE_ENUM:
  1845. switch (get_enum_fc(t))
  1846. {
  1847. case FC_ENUM16:
  1848. return 2;
  1849. case FC_ENUM32:
  1850. return 4;
  1851. default:
  1852. error("type_buffer_alignment: Unknown enum type\n");
  1853. }
  1854. break;
  1855. case TYPE_STRUCT:
  1856. if (!(fields = type_struct_get_fields(t))) break;
  1857. LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
  1858. {
  1859. if (!var->declspec.type) continue;
  1860. align = type_buffer_alignment( var->declspec.type );
  1861. if (max < align) max = align;
  1862. }
  1863. break;
  1864. case TYPE_ENCAPSULATED_UNION:
  1865. if (!(fields = type_encapsulated_union_get_fields(t))) break;
  1866. LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
  1867. {
  1868. if (!var->declspec.type) continue;
  1869. align = type_buffer_alignment( var->declspec.type );
  1870. if (max < align) max = align;
  1871. }
  1872. break;
  1873. case TYPE_UNION:
  1874. if (!(fields = type_union_get_cases(t))) break;
  1875. LIST_FOR_EACH_ENTRY( var, fields, const var_t, entry )
  1876. {
  1877. if (!var->declspec.type) continue;
  1878. align = type_buffer_alignment( var->declspec.type );
  1879. if (max < align) max = align;
  1880. }
  1881. break;
  1882. case TYPE_ARRAY:
  1883. if (!type_array_is_decl_as_ptr(t))
  1884. return type_buffer_alignment( type_array_get_element_type(t) );
  1885. /* else fall through */
  1886. case TYPE_POINTER:
  1887. return 4;
  1888. case TYPE_INTERFACE:
  1889. case TYPE_ALIAS:
  1890. case TYPE_VOID:
  1891. case TYPE_COCLASS:
  1892. case TYPE_MODULE:
  1893. case TYPE_FUNCTION:
  1894. case TYPE_BITFIELD:
  1895. case TYPE_APICONTRACT:
  1896. case TYPE_RUNTIMECLASS:
  1897. case TYPE_PARAMETERIZED_TYPE:
  1898. case TYPE_PARAMETER:
  1899. case TYPE_DELEGATE:
  1900. /* these types should not be encountered here due to language
  1901. * restrictions (interface, void, coclass, module), logical
  1902. * restrictions (alias - due to type_get_type call above) or
  1903. * checking restrictions (function, bitfield). */
  1904. assert(0);
  1905. }
  1906. return max;
  1907. }
  1908. int is_full_pointer_function(const var_t *func)
  1909. {
  1910. const var_t *var;
  1911. if (type_has_full_pointer(type_function_get_rettype(func->declspec.type), func->attrs, TRUE))
  1912. return TRUE;
  1913. if (!type_function_get_args(func->declspec.type))
  1914. return FALSE;
  1915. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  1916. if (type_has_full_pointer( var->declspec.type, var->attrs, TRUE ))
  1917. return TRUE;
  1918. return FALSE;
  1919. }
  1920. void write_full_pointer_init(FILE *file, int indent, const var_t *func, int is_server)
  1921. {
  1922. print_file(file, indent, "__frame->_StubMsg.FullPtrXlatTables = NdrFullPointerXlatInit(0,%s);\n",
  1923. is_server ? "XLAT_SERVER" : "XLAT_CLIENT");
  1924. fprintf(file, "\n");
  1925. }
  1926. void write_full_pointer_free(FILE *file, int indent, const var_t *func)
  1927. {
  1928. print_file(file, indent, "NdrFullPointerXlatFree(__frame->_StubMsg.FullPtrXlatTables);\n");
  1929. fprintf(file, "\n");
  1930. }
  1931. static unsigned int write_nonsimple_pointer(FILE *file, const attr_list_t *attrs,
  1932. const type_t *type,
  1933. enum type_context context,
  1934. unsigned int offset,
  1935. unsigned int *typeformat_offset)
  1936. {
  1937. unsigned int start_offset = *typeformat_offset;
  1938. short reloff = offset - (*typeformat_offset + 2);
  1939. int in_attr, out_attr;
  1940. int pointer_type;
  1941. unsigned char flags = 0;
  1942. pointer_type = get_pointer_fc_context(type, attrs, context);
  1943. in_attr = is_attr(attrs, ATTR_IN);
  1944. out_attr = is_attr(attrs, ATTR_OUT);
  1945. if (!in_attr && !out_attr) in_attr = 1;
  1946. if (!is_interpreted_func(current_iface, current_func))
  1947. {
  1948. if (out_attr && !in_attr && pointer_type == FC_RP)
  1949. flags |= FC_ALLOCED_ON_STACK;
  1950. }
  1951. else if (get_stub_mode() == MODE_Oif)
  1952. {
  1953. if (context == TYPE_CONTEXT_TOPLEVELPARAM && is_ptr(type) && pointer_type == FC_RP)
  1954. {
  1955. switch (typegen_detect_type(type_pointer_get_ref_type(type), NULL, TDT_ALL_TYPES))
  1956. {
  1957. case TGT_STRING:
  1958. case TGT_POINTER:
  1959. case TGT_CTXT_HANDLE:
  1960. case TGT_CTXT_HANDLE_POINTER:
  1961. case TGT_ARRAY:
  1962. flags |= FC_ALLOCED_ON_STACK;
  1963. break;
  1964. case TGT_IFACE_POINTER:
  1965. if (in_attr && out_attr)
  1966. flags |= FC_ALLOCED_ON_STACK;
  1967. break;
  1968. default:
  1969. break;
  1970. }
  1971. }
  1972. }
  1973. if (is_ptr(type))
  1974. {
  1975. type_t *ref = type_pointer_get_ref_type(type);
  1976. if(is_declptr(ref) && !is_user_type(ref))
  1977. flags |= FC_POINTER_DEREF;
  1978. if (pointer_type != FC_RP) {
  1979. flags |= get_attrv(type->attrs, ATTR_ALLOCATE);
  1980. }
  1981. }
  1982. print_file(file, 2, "0x%x, 0x%x,\t\t/* %s",
  1983. pointer_type,
  1984. flags,
  1985. string_of_type(pointer_type));
  1986. if (file)
  1987. {
  1988. if (flags & FC_ALLOCED_ON_STACK)
  1989. fprintf(file, " [allocated_on_stack]");
  1990. if (flags & FC_POINTER_DEREF)
  1991. fprintf(file, " [pointer_deref]");
  1992. if (flags & FC_DONT_FREE)
  1993. fprintf(file, " [dont_free]");
  1994. if (flags & FC_ALLOCATE_ALL_NODES)
  1995. fprintf(file, " [all_nodes]");
  1996. fprintf(file, " */\n");
  1997. }
  1998. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, offset);
  1999. *typeformat_offset += 4;
  2000. return start_offset;
  2001. }
  2002. static unsigned int write_simple_pointer(FILE *file, const attr_list_t *attrs,
  2003. const type_t *type, enum type_context context)
  2004. {
  2005. unsigned char fc;
  2006. unsigned char pointer_fc;
  2007. const type_t *ref;
  2008. int in_attr = is_attr(attrs, ATTR_IN);
  2009. int out_attr = is_attr(attrs, ATTR_OUT);
  2010. unsigned char flags = FC_SIMPLE_POINTER;
  2011. /* for historical reasons, write_simple_pointer also handled string types,
  2012. * but no longer does. catch bad uses of the function with this check */
  2013. if (is_string_type(attrs, type))
  2014. error("write_simple_pointer: can't handle type %s which is a string type\n", type->name);
  2015. pointer_fc = get_pointer_fc_context(type, attrs, context);
  2016. ref = type_pointer_get_ref_type(type);
  2017. if (type_get_type(ref) == TYPE_ENUM)
  2018. fc = get_enum_fc(ref);
  2019. else
  2020. fc = get_basic_fc(ref);
  2021. if (!is_interpreted_func(current_iface, current_func))
  2022. {
  2023. if (out_attr && !in_attr && pointer_fc == FC_RP)
  2024. flags |= FC_ALLOCED_ON_STACK;
  2025. }
  2026. else if (get_stub_mode() == MODE_Oif)
  2027. {
  2028. if (context == TYPE_CONTEXT_TOPLEVELPARAM && fc == FC_ENUM16 && pointer_fc == FC_RP)
  2029. flags |= FC_ALLOCED_ON_STACK;
  2030. }
  2031. print_file(file, 2, "0x%02x, 0x%x,\t/* %s %s[simple_pointer] */\n",
  2032. pointer_fc, flags, string_of_type(pointer_fc),
  2033. flags & FC_ALLOCED_ON_STACK ? "[allocated_on_stack] " : "");
  2034. print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
  2035. print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
  2036. return 4;
  2037. }
  2038. static void print_start_tfs_comment(FILE *file, type_t *t, unsigned int tfsoff)
  2039. {
  2040. const decl_spec_t ds = {.type = t};
  2041. print_file(file, 0, "/* %u (", tfsoff);
  2042. write_type_decl(file, &ds, NULL);
  2043. print_file(file, 0, ") */\n");
  2044. }
  2045. static unsigned int write_pointer_tfs(FILE *file, const attr_list_t *attrs,
  2046. type_t *type, unsigned int ref_offset,
  2047. enum type_context context,
  2048. unsigned int *typestring_offset)
  2049. {
  2050. unsigned int offset = *typestring_offset;
  2051. type_t *ref = type_pointer_get_ref_type(type);
  2052. print_start_tfs_comment(file, type, offset);
  2053. update_tfsoff(type, offset, file);
  2054. switch (typegen_detect_type(ref, attrs, TDT_ALL_TYPES))
  2055. {
  2056. case TGT_BASIC:
  2057. case TGT_ENUM:
  2058. *typestring_offset += write_simple_pointer(file, attrs, type, context);
  2059. break;
  2060. default:
  2061. if (ref_offset)
  2062. write_nonsimple_pointer(file, attrs, type, context, ref_offset, typestring_offset);
  2063. break;
  2064. }
  2065. return offset;
  2066. }
  2067. static int processed(const type_t *type)
  2068. {
  2069. return type->typestring_offset && !type->tfswrite;
  2070. }
  2071. static int user_type_has_variable_size(const type_t *t)
  2072. {
  2073. if (is_ptr(t))
  2074. return TRUE;
  2075. else if (type_get_type(t) == TYPE_STRUCT)
  2076. {
  2077. switch (get_struct_fc(t))
  2078. {
  2079. case FC_PSTRUCT:
  2080. case FC_CSTRUCT:
  2081. case FC_CPSTRUCT:
  2082. case FC_CVSTRUCT:
  2083. return TRUE;
  2084. }
  2085. }
  2086. /* Note: Since this only applies to user types, we can't have a conformant
  2087. array here, and strings should get filed under pointer in this case. */
  2088. return FALSE;
  2089. }
  2090. static unsigned int write_user_tfs(FILE *file, type_t *type, unsigned int *tfsoff)
  2091. {
  2092. unsigned int start, absoff, flags;
  2093. const char *name = NULL;
  2094. type_t *utype = get_user_type(type, &name);
  2095. unsigned int usize = type_memsize(utype);
  2096. unsigned int ualign = type_buffer_alignment(utype);
  2097. unsigned int size = type_memsize(type);
  2098. unsigned short funoff = user_type_offset(name);
  2099. short reloff;
  2100. if (processed(type)) return type->typestring_offset;
  2101. guard_rec(type);
  2102. if(user_type_has_variable_size(utype)) usize = 0;
  2103. if (type_get_type(utype) == TYPE_BASIC ||
  2104. type_get_type(utype) == TYPE_ENUM)
  2105. {
  2106. unsigned char fc;
  2107. if (type_get_type(utype) == TYPE_ENUM)
  2108. fc = get_enum_fc(utype);
  2109. else
  2110. fc = get_basic_fc(utype);
  2111. absoff = *tfsoff;
  2112. print_start_tfs_comment(file, utype, absoff);
  2113. print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
  2114. print_file(file, 2, "0x5c,\t/* FC_PAD */\n");
  2115. *tfsoff += 2;
  2116. }
  2117. else
  2118. {
  2119. if (!processed(utype))
  2120. write_embedded_types(file, NULL, utype, utype->name, TRUE, tfsoff);
  2121. absoff = utype->typestring_offset;
  2122. }
  2123. if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_RP)
  2124. flags = 0x40;
  2125. else if (type_get_type(utype) == TYPE_POINTER && get_pointer_fc(utype, NULL, FALSE) == FC_UP)
  2126. flags = 0x80;
  2127. else
  2128. flags = 0;
  2129. start = *tfsoff;
  2130. update_tfsoff(type, start, file);
  2131. print_start_tfs_comment(file, type, start);
  2132. print_file(file, 2, "0x%x,\t/* FC_USER_MARSHAL */\n", FC_USER_MARSHAL);
  2133. print_file(file, 2, "0x%x,\t/* Alignment= %d, Flags= %02x */\n",
  2134. flags | (ualign - 1), ualign - 1, flags);
  2135. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Function offset= %hu */\n", funoff, funoff);
  2136. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
  2137. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)usize, usize);
  2138. *tfsoff += 8;
  2139. reloff = absoff - *tfsoff;
  2140. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n", reloff, reloff, absoff);
  2141. *tfsoff += 2;
  2142. return start;
  2143. }
  2144. static void write_member_type(FILE *file, const type_t *cont,
  2145. int cont_is_complex, const attr_list_t *attrs,
  2146. const type_t *type, unsigned int *corroff,
  2147. unsigned int *tfsoff)
  2148. {
  2149. if (is_embedded_complex(type) && !is_conformant_array(type))
  2150. {
  2151. unsigned int absoff;
  2152. short reloff;
  2153. if (type_get_type(type) == TYPE_UNION && is_attr(attrs, ATTR_SWITCHIS))
  2154. {
  2155. absoff = *corroff;
  2156. *corroff += 8;
  2157. }
  2158. else
  2159. {
  2160. absoff = type->typestring_offset;
  2161. }
  2162. reloff = absoff - (*tfsoff + 2);
  2163. print_file(file, 2, "0x4c,\t/* FC_EMBEDDED_COMPLEX */\n");
  2164. /* padding is represented using FC_STRUCTPAD* types, so presumably
  2165. * this is left over in the format for historical purposes in MIDL
  2166. * or rpcrt4. */
  2167. print_file(file, 2, "0x0,\n");
  2168. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
  2169. reloff, reloff, absoff);
  2170. *tfsoff += 4;
  2171. }
  2172. else if (is_ptr(type) || is_conformant_array(type))
  2173. {
  2174. unsigned char fc = cont_is_complex ? FC_POINTER : FC_LONG;
  2175. print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
  2176. *tfsoff += 1;
  2177. }
  2178. else if (!write_base_type(file, type, tfsoff))
  2179. error("Unsupported member type %d\n", type_get_type(type));
  2180. }
  2181. static void write_array_element_type(FILE *file, const attr_list_t *attrs, const type_t *type,
  2182. int cont_is_complex, unsigned int *tfsoff)
  2183. {
  2184. type_t *elem = type_array_get_element_type(type);
  2185. if (!is_embedded_complex(elem) && is_ptr(elem))
  2186. {
  2187. type_t *ref = type_pointer_get_ref_type(elem);
  2188. if (processed(ref))
  2189. {
  2190. write_nonsimple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER,
  2191. ref->typestring_offset, tfsoff);
  2192. return;
  2193. }
  2194. if (cont_is_complex && is_string_type(attrs, elem))
  2195. {
  2196. write_string_tfs(file, NULL, elem, TYPE_CONTEXT_CONTAINER, NULL, tfsoff);
  2197. return;
  2198. }
  2199. if (!is_string_type(NULL, elem) &&
  2200. (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM))
  2201. {
  2202. *tfsoff += write_simple_pointer(file, NULL, elem, TYPE_CONTEXT_CONTAINER);
  2203. return;
  2204. }
  2205. }
  2206. write_member_type(file, type, cont_is_complex, NULL, elem, NULL, tfsoff);
  2207. }
  2208. static void write_end(FILE *file, unsigned int *tfsoff)
  2209. {
  2210. if (*tfsoff % 2 == 0)
  2211. {
  2212. print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
  2213. *tfsoff += 1;
  2214. }
  2215. print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
  2216. *tfsoff += 1;
  2217. }
  2218. static void write_descriptors(FILE *file, type_t *type, unsigned int *tfsoff)
  2219. {
  2220. unsigned int offset = 0;
  2221. var_list_t *fs = type_struct_get_fields(type);
  2222. var_t *f;
  2223. if (fs) LIST_FOR_EACH_ENTRY(f, fs, var_t, entry)
  2224. {
  2225. type_t *ft = f->declspec.type;
  2226. unsigned int size = field_memsize( ft, &offset );
  2227. if (type_get_type(ft) == TYPE_UNION && is_attr(f->attrs, ATTR_SWITCHIS))
  2228. {
  2229. short reloff;
  2230. unsigned int absoff = ft->typestring_offset;
  2231. if (is_attr(ft->attrs, ATTR_SWITCHTYPE))
  2232. absoff += 8; /* we already have a corr descr, skip it */
  2233. reloff = absoff - (*tfsoff + 6);
  2234. print_file(file, 0, "/* %d */\n", *tfsoff);
  2235. print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
  2236. print_file(file, 2, "0x%x,\t/* FIXME: always FC_LONG */\n", FC_LONG);
  2237. write_conf_or_var_desc(file, current_structure, offset, ft,
  2238. get_attrp(f->attrs, ATTR_SWITCHIS));
  2239. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
  2240. (unsigned short)reloff, reloff, absoff);
  2241. *tfsoff += 8;
  2242. }
  2243. offset += size;
  2244. }
  2245. }
  2246. static int write_pointer_description_offsets(
  2247. FILE *file, const attr_list_t *attrs, type_t *type,
  2248. unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
  2249. unsigned int *typestring_offset)
  2250. {
  2251. int written = 0;
  2252. if ((is_ptr(type) && type_get_type(type_pointer_get_ref_type(type)) != TYPE_INTERFACE) ||
  2253. (is_array(type) && type_array_is_decl_as_ptr(type)))
  2254. {
  2255. if (offset_in_memory && offset_in_buffer)
  2256. {
  2257. unsigned int memsize;
  2258. /* pointer instance
  2259. *
  2260. * note that MSDN states that for pointer layouts in structures,
  2261. * this is a negative offset from the end of the structure, but
  2262. * this statement is incorrect. all offsets are positive */
  2263. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Memory offset = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
  2264. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Buffer offset = %d */\n", (unsigned short)*offset_in_buffer, *offset_in_buffer);
  2265. memsize = type_memsize(type);
  2266. *offset_in_memory += memsize;
  2267. /* increment these separately as in the case of conformant (varying)
  2268. * structures these start at different values */
  2269. *offset_in_buffer += memsize;
  2270. }
  2271. *typestring_offset += 4;
  2272. if (is_ptr(type))
  2273. {
  2274. type_t *ref = type_pointer_get_ref_type(type);
  2275. if (is_string_type(attrs, type))
  2276. write_string_tfs(file, attrs, type, TYPE_CONTEXT_CONTAINER, NULL, typestring_offset);
  2277. else if (processed(ref))
  2278. write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER,
  2279. ref->typestring_offset, typestring_offset);
  2280. else if (type_get_type(ref) == TYPE_BASIC || type_get_type(ref) == TYPE_ENUM)
  2281. *typestring_offset += write_simple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER);
  2282. else
  2283. error("write_pointer_description_offsets: type format string unknown\n");
  2284. }
  2285. else
  2286. {
  2287. unsigned int offset = type->typestring_offset;
  2288. /* skip over the pointer that is written for strings, since a
  2289. * pointer has to be written in-place here */
  2290. if (is_string_type(attrs, type))
  2291. offset += 4;
  2292. write_nonsimple_pointer(file, attrs, type, TYPE_CONTEXT_CONTAINER, offset, typestring_offset);
  2293. }
  2294. return 1;
  2295. }
  2296. if (is_array(type))
  2297. {
  2298. return write_pointer_description_offsets(
  2299. file, attrs, type_array_get_element_type(type), offset_in_memory,
  2300. offset_in_buffer, typestring_offset);
  2301. }
  2302. else if (is_non_complex_struct(type))
  2303. {
  2304. /* otherwise search for interesting fields to parse */
  2305. const var_t *v;
  2306. LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
  2307. {
  2308. if (offset_in_memory && offset_in_buffer)
  2309. {
  2310. unsigned int padding;
  2311. unsigned int align = 0;
  2312. type_memsize_and_alignment(v->declspec.type, &align);
  2313. padding = ROUNDING(*offset_in_memory, align);
  2314. *offset_in_memory += padding;
  2315. *offset_in_buffer += padding;
  2316. }
  2317. written += write_pointer_description_offsets(
  2318. file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
  2319. typestring_offset);
  2320. }
  2321. }
  2322. else
  2323. {
  2324. if (offset_in_memory && offset_in_buffer)
  2325. {
  2326. unsigned int memsize = type_memsize(type);
  2327. *offset_in_memory += memsize;
  2328. /* increment these separately as in the case of conformant (varying)
  2329. * structures these start at different values */
  2330. *offset_in_buffer += memsize;
  2331. }
  2332. }
  2333. return written;
  2334. }
  2335. static int write_no_repeat_pointer_descriptions(
  2336. FILE *file, const attr_list_t *attrs, type_t *type,
  2337. unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
  2338. unsigned int *typestring_offset)
  2339. {
  2340. int written = 0;
  2341. if (is_ptr(type) ||
  2342. (is_conformant_array(type) && type_array_is_decl_as_ptr(type)))
  2343. {
  2344. print_file(file, 2, "0x%02x, /* FC_NO_REPEAT */\n", FC_NO_REPEAT);
  2345. print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
  2346. *typestring_offset += 2;
  2347. return write_pointer_description_offsets(file, attrs, type,
  2348. offset_in_memory, offset_in_buffer, typestring_offset);
  2349. }
  2350. if (is_non_complex_struct(type))
  2351. {
  2352. const var_t *v;
  2353. LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
  2354. {
  2355. if (offset_in_memory && offset_in_buffer)
  2356. {
  2357. unsigned int padding;
  2358. unsigned int align = 0;
  2359. type_memsize_and_alignment(v->declspec.type, &align);
  2360. padding = ROUNDING(*offset_in_memory, align);
  2361. *offset_in_memory += padding;
  2362. *offset_in_buffer += padding;
  2363. }
  2364. written += write_no_repeat_pointer_descriptions(
  2365. file, v->attrs, v->declspec.type,
  2366. offset_in_memory, offset_in_buffer, typestring_offset);
  2367. }
  2368. }
  2369. else
  2370. {
  2371. unsigned int memsize = type_memsize(type);
  2372. *offset_in_memory += memsize;
  2373. /* increment these separately as in the case of conformant (varying)
  2374. * structures these start at different values */
  2375. *offset_in_buffer += memsize;
  2376. }
  2377. return written;
  2378. }
  2379. /* Note: if file is NULL return value is number of pointers to write, else
  2380. * it is the number of type format characters written */
  2381. static int write_fixed_array_pointer_descriptions(
  2382. FILE *file, const attr_list_t *attrs, type_t *type,
  2383. unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
  2384. unsigned int *typestring_offset)
  2385. {
  2386. int pointer_count = 0;
  2387. if (type_get_type(type) == TYPE_ARRAY &&
  2388. !type_array_has_conformance(type) && !type_array_has_variance(type))
  2389. {
  2390. unsigned int temp = 0;
  2391. /* unfortunately, this needs to be done in two passes to avoid
  2392. * writing out redundant FC_FIXED_REPEAT descriptions */
  2393. pointer_count = write_pointer_description_offsets(
  2394. NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
  2395. if (pointer_count > 0)
  2396. {
  2397. unsigned int increment_size;
  2398. unsigned int offset_of_array_pointer_mem = 0;
  2399. unsigned int offset_of_array_pointer_buf = 0;
  2400. increment_size = type_memsize(type_array_get_element_type(type));
  2401. print_file(file, 2, "0x%02x, /* FC_FIXED_REPEAT */\n", FC_FIXED_REPEAT);
  2402. print_file(file, 2, "0x%02x, /* FC_PAD */\n", FC_PAD);
  2403. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Iterations = %d */\n", (unsigned short)type_array_get_dim(type), type_array_get_dim(type));
  2404. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
  2405. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
  2406. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
  2407. *typestring_offset += 10;
  2408. pointer_count = write_pointer_description_offsets(
  2409. file, attrs, type, &offset_of_array_pointer_mem,
  2410. &offset_of_array_pointer_buf, typestring_offset);
  2411. }
  2412. }
  2413. else if (type_get_type(type) == TYPE_STRUCT)
  2414. {
  2415. const var_t *v;
  2416. LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
  2417. {
  2418. if (offset_in_memory && offset_in_buffer)
  2419. {
  2420. unsigned int padding;
  2421. unsigned int align = 0;
  2422. type_memsize_and_alignment(v->declspec.type, &align);
  2423. padding = ROUNDING(*offset_in_memory, align);
  2424. *offset_in_memory += padding;
  2425. *offset_in_buffer += padding;
  2426. }
  2427. pointer_count += write_fixed_array_pointer_descriptions(
  2428. file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
  2429. typestring_offset);
  2430. }
  2431. }
  2432. else
  2433. {
  2434. if (offset_in_memory && offset_in_buffer)
  2435. {
  2436. unsigned int memsize;
  2437. memsize = type_memsize(type);
  2438. *offset_in_memory += memsize;
  2439. /* increment these separately as in the case of conformant (varying)
  2440. * structures these start at different values */
  2441. *offset_in_buffer += memsize;
  2442. }
  2443. }
  2444. return pointer_count;
  2445. }
  2446. /* Note: if file is NULL return value is number of pointers to write, else
  2447. * it is the number of type format characters written */
  2448. static int write_conformant_array_pointer_descriptions(
  2449. FILE *file, const attr_list_t *attrs, type_t *type,
  2450. unsigned int offset_in_memory, unsigned int *typestring_offset)
  2451. {
  2452. int pointer_count = 0;
  2453. if (is_conformant_array(type) && !type_array_has_variance(type))
  2454. {
  2455. unsigned int temp = 0;
  2456. /* unfortunately, this needs to be done in two passes to avoid
  2457. * writing out redundant FC_VARIABLE_REPEAT descriptions */
  2458. pointer_count = write_pointer_description_offsets(
  2459. NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
  2460. if (pointer_count > 0)
  2461. {
  2462. unsigned int increment_size;
  2463. unsigned int offset_of_array_pointer_mem = offset_in_memory;
  2464. unsigned int offset_of_array_pointer_buf = offset_in_memory;
  2465. increment_size = type_memsize(type_array_get_element_type(type));
  2466. if (increment_size > USHRT_MAX)
  2467. error("array size of %u bytes is too large\n", increment_size);
  2468. print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
  2469. print_file(file, 2, "0x%02x, /* FC_FIXED_OFFSET */\n", FC_FIXED_OFFSET);
  2470. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
  2471. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)offset_in_memory, offset_in_memory);
  2472. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
  2473. *typestring_offset += 8;
  2474. pointer_count = write_pointer_description_offsets(
  2475. file, attrs, type_array_get_element_type(type),
  2476. &offset_of_array_pointer_mem, &offset_of_array_pointer_buf,
  2477. typestring_offset);
  2478. }
  2479. }
  2480. return pointer_count;
  2481. }
  2482. /* Note: if file is NULL return value is number of pointers to write, else
  2483. * it is the number of type format characters written */
  2484. static int write_varying_array_pointer_descriptions(
  2485. FILE *file, const attr_list_t *attrs, type_t *type,
  2486. unsigned int *offset_in_memory, unsigned int *offset_in_buffer,
  2487. unsigned int *typestring_offset)
  2488. {
  2489. int pointer_count = 0;
  2490. if (is_array(type) && type_array_has_variance(type))
  2491. {
  2492. unsigned int temp = 0;
  2493. /* unfortunately, this needs to be done in two passes to avoid
  2494. * writing out redundant FC_VARIABLE_REPEAT descriptions */
  2495. pointer_count = write_pointer_description_offsets(
  2496. NULL, attrs, type_array_get_element_type(type), NULL, NULL, &temp);
  2497. if (pointer_count > 0)
  2498. {
  2499. unsigned int increment_size;
  2500. increment_size = type_memsize(type_array_get_element_type(type));
  2501. if (increment_size > USHRT_MAX)
  2502. error("array size of %u bytes is too large\n", increment_size);
  2503. print_file(file, 2, "0x%02x, /* FC_VARIABLE_REPEAT */\n", FC_VARIABLE_REPEAT);
  2504. print_file(file, 2, "0x%02x, /* FC_VARIABLE_OFFSET */\n", FC_VARIABLE_OFFSET);
  2505. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Increment = %d */\n", (unsigned short)increment_size, increment_size);
  2506. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset to array = %d */\n", (unsigned short)*offset_in_memory, *offset_in_memory);
  2507. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Number of pointers = %d */\n", (unsigned short)pointer_count, pointer_count);
  2508. *typestring_offset += 8;
  2509. pointer_count = write_pointer_description_offsets(
  2510. file, attrs, type_array_get_element_type(type), offset_in_memory,
  2511. offset_in_buffer, typestring_offset);
  2512. }
  2513. }
  2514. else if (type_get_type(type) == TYPE_STRUCT)
  2515. {
  2516. const var_t *v;
  2517. LIST_FOR_EACH_ENTRY( v, type_struct_get_fields(type), const var_t, entry )
  2518. {
  2519. if (offset_in_memory && offset_in_buffer)
  2520. {
  2521. unsigned int align = 0, padding;
  2522. if (is_array(v->declspec.type) && type_array_has_variance(v->declspec.type))
  2523. {
  2524. *offset_in_buffer = ROUND_SIZE(*offset_in_buffer, 4);
  2525. /* skip over variance and offset in buffer */
  2526. *offset_in_buffer += 8;
  2527. }
  2528. type_memsize_and_alignment(v->declspec.type, &align);
  2529. padding = ROUNDING(*offset_in_memory, align);
  2530. *offset_in_memory += padding;
  2531. *offset_in_buffer += padding;
  2532. }
  2533. pointer_count += write_varying_array_pointer_descriptions(
  2534. file, v->attrs, v->declspec.type, offset_in_memory, offset_in_buffer,
  2535. typestring_offset);
  2536. }
  2537. }
  2538. else
  2539. {
  2540. if (offset_in_memory && offset_in_buffer)
  2541. {
  2542. unsigned int memsize = type_memsize(type);
  2543. *offset_in_memory += memsize;
  2544. /* increment these separately as in the case of conformant (varying)
  2545. * structures these start at different values */
  2546. *offset_in_buffer += memsize;
  2547. }
  2548. }
  2549. return pointer_count;
  2550. }
  2551. static void write_pointer_description(FILE *file, const attr_list_t *attrs, type_t *type,
  2552. unsigned int *typestring_offset)
  2553. {
  2554. unsigned int offset_in_buffer;
  2555. unsigned int offset_in_memory;
  2556. /* pass 1: search for single instance of a pointer (i.e. don't descend
  2557. * into arrays) */
  2558. if (!is_array(type))
  2559. {
  2560. offset_in_memory = 0;
  2561. offset_in_buffer = 0;
  2562. write_no_repeat_pointer_descriptions(
  2563. file, NULL, type,
  2564. &offset_in_memory, &offset_in_buffer, typestring_offset);
  2565. }
  2566. /* pass 2: search for pointers in fixed arrays */
  2567. offset_in_memory = 0;
  2568. offset_in_buffer = 0;
  2569. write_fixed_array_pointer_descriptions(
  2570. file, NULL, type,
  2571. &offset_in_memory, &offset_in_buffer, typestring_offset);
  2572. /* pass 3: search for pointers in conformant only arrays (but don't descend
  2573. * into conformant varying or varying arrays) */
  2574. if (is_conformant_array(type) &&
  2575. (type_array_is_decl_as_ptr(type) || !current_structure))
  2576. write_conformant_array_pointer_descriptions(
  2577. file, attrs, type, 0, typestring_offset);
  2578. else if (type_get_type(type) == TYPE_STRUCT &&
  2579. get_struct_fc(type) == FC_CPSTRUCT)
  2580. {
  2581. type_t *carray = find_array_or_string_in_struct(type)->declspec.type;
  2582. write_conformant_array_pointer_descriptions( file, NULL, carray,
  2583. type_memsize(type), typestring_offset);
  2584. }
  2585. /* pass 4: search for pointers in varying arrays */
  2586. offset_in_memory = 0;
  2587. offset_in_buffer = 0;
  2588. write_varying_array_pointer_descriptions(
  2589. file, NULL, type,
  2590. &offset_in_memory, &offset_in_buffer, typestring_offset);
  2591. }
  2592. static unsigned int write_string_tfs(FILE *file, const attr_list_t *attrs,
  2593. type_t *type, enum type_context context,
  2594. const char *name, unsigned int *typestring_offset)
  2595. {
  2596. unsigned int start_offset;
  2597. unsigned char rtype;
  2598. type_t *elem_type;
  2599. int is_processed = processed(type);
  2600. start_offset = *typestring_offset;
  2601. if (is_declptr(type))
  2602. {
  2603. unsigned char flag = is_conformant_array(type) ? 0 : FC_SIMPLE_POINTER;
  2604. int pointer_type = get_pointer_fc_context(type, attrs, context);
  2605. if (!pointer_type)
  2606. pointer_type = FC_RP;
  2607. print_start_tfs_comment(file, type, *typestring_offset);
  2608. print_file(file, 2,"0x%x, 0x%x,\t/* %s%s */\n",
  2609. pointer_type, flag, string_of_type(pointer_type),
  2610. flag ? " [simple_pointer]" : "");
  2611. *typestring_offset += 2;
  2612. if (!flag)
  2613. {
  2614. print_file(file, 2, "NdrFcShort(0x2),\n");
  2615. *typestring_offset += 2;
  2616. }
  2617. is_processed = FALSE;
  2618. }
  2619. if (is_array(type))
  2620. elem_type = type_array_get_element_type(type);
  2621. else
  2622. elem_type = type_pointer_get_ref_type(type);
  2623. if (type_get_type(elem_type) == TYPE_POINTER && is_array(type))
  2624. return write_array_tfs(file, attrs, type, name, typestring_offset);
  2625. if (type_get_type(elem_type) != TYPE_BASIC)
  2626. {
  2627. error("write_string_tfs: Unimplemented for non-basic type %s\n", name);
  2628. return start_offset;
  2629. }
  2630. rtype = get_basic_fc(elem_type);
  2631. if ((rtype != FC_BYTE) && (rtype != FC_CHAR) && (rtype != FC_WCHAR))
  2632. {
  2633. error("write_string_tfs: Unimplemented for type 0x%x of name: %s\n", rtype, name);
  2634. return start_offset;
  2635. }
  2636. if (type_get_type(type) == TYPE_ARRAY && !type_array_has_conformance(type))
  2637. {
  2638. unsigned int dim = type_array_get_dim(type);
  2639. if (is_processed) return start_offset;
  2640. /* FIXME: multi-dimensional array */
  2641. if (0xffffu < dim)
  2642. error("array size for parameter %s exceeds %u bytes by %u bytes\n",
  2643. name, 0xffffu, dim - 0xffffu);
  2644. if (rtype == FC_WCHAR)
  2645. WRITE_FCTYPE(file, FC_WSTRING, *typestring_offset);
  2646. else
  2647. WRITE_FCTYPE(file, FC_CSTRING, *typestring_offset);
  2648. print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
  2649. *typestring_offset += 2;
  2650. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)dim, dim);
  2651. *typestring_offset += 2;
  2652. update_tfsoff(type, start_offset, file);
  2653. return start_offset;
  2654. }
  2655. else if (is_conformant_array(type))
  2656. {
  2657. if (rtype == FC_WCHAR)
  2658. WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
  2659. else
  2660. WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
  2661. print_file(file, 2, "0x%x,\t/* FC_STRING_SIZED */\n", FC_STRING_SIZED);
  2662. *typestring_offset += 2;
  2663. *typestring_offset += write_conf_or_var_desc(
  2664. file, current_structure,
  2665. (!type_array_is_decl_as_ptr(type) && current_structure
  2666. ? type_memsize(current_structure)
  2667. : 0),
  2668. type, type_array_get_conformance(type));
  2669. update_tfsoff(type, start_offset, file);
  2670. return start_offset;
  2671. }
  2672. else
  2673. {
  2674. if (is_processed) return start_offset;
  2675. if (rtype == FC_WCHAR)
  2676. WRITE_FCTYPE(file, FC_C_WSTRING, *typestring_offset);
  2677. else
  2678. WRITE_FCTYPE(file, FC_C_CSTRING, *typestring_offset);
  2679. print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
  2680. *typestring_offset += 2;
  2681. update_tfsoff(type, start_offset, file);
  2682. return start_offset;
  2683. }
  2684. }
  2685. static unsigned int write_array_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
  2686. const char *name, unsigned int *typestring_offset)
  2687. {
  2688. const expr_t *length_is = type_array_get_variance(type);
  2689. const expr_t *size_is = type_array_get_conformance(type);
  2690. unsigned int align;
  2691. unsigned int size;
  2692. unsigned int start_offset;
  2693. unsigned char fc;
  2694. unsigned int baseoff
  2695. = !type_array_is_decl_as_ptr(type) && current_structure
  2696. ? type_memsize(current_structure)
  2697. : 0;
  2698. if (!is_string_type(attrs, type_array_get_element_type(type)))
  2699. write_embedded_types(file, attrs, type_array_get_element_type(type), name, FALSE, typestring_offset);
  2700. size = type_memsize(is_conformant_array(type) ? type_array_get_element_type(type) : type);
  2701. align = type_buffer_alignment(is_conformant_array(type) ? type_array_get_element_type(type) : type);
  2702. fc = get_array_fc(type);
  2703. start_offset = *typestring_offset;
  2704. update_tfsoff(type, start_offset, file);
  2705. print_start_tfs_comment(file, type, start_offset);
  2706. print_file(file, 2, "0x%02x,\t/* %s */\n", fc, string_of_type(fc));
  2707. print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
  2708. *typestring_offset += 2;
  2709. align = 0;
  2710. if (fc != FC_BOGUS_ARRAY)
  2711. {
  2712. if (fc == FC_LGFARRAY || fc == FC_LGVARRAY)
  2713. {
  2714. print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", size, size);
  2715. *typestring_offset += 4;
  2716. }
  2717. else
  2718. {
  2719. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)size, size);
  2720. *typestring_offset += 2;
  2721. }
  2722. if (is_conformant_array(type))
  2723. *typestring_offset
  2724. += write_conf_or_var_desc(file, current_structure, baseoff,
  2725. type, size_is);
  2726. if (fc == FC_SMVARRAY || fc == FC_LGVARRAY)
  2727. {
  2728. unsigned int elsize = type_memsize(type_array_get_element_type(type));
  2729. unsigned int dim = type_array_get_dim(type);
  2730. if (fc == FC_LGVARRAY)
  2731. {
  2732. print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", dim, dim);
  2733. *typestring_offset += 4;
  2734. }
  2735. else
  2736. {
  2737. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
  2738. *typestring_offset += 2;
  2739. }
  2740. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)elsize, elsize);
  2741. *typestring_offset += 2;
  2742. }
  2743. if (length_is)
  2744. *typestring_offset
  2745. += write_conf_or_var_desc(file, current_structure, baseoff,
  2746. type, length_is);
  2747. if (type_has_pointers(type_array_get_element_type(type)) &&
  2748. (type_array_is_decl_as_ptr(type) || !current_structure))
  2749. {
  2750. print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
  2751. print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
  2752. *typestring_offset += 2;
  2753. write_pointer_description(file, is_string_type(attrs, type) ? attrs : NULL, type, typestring_offset);
  2754. print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
  2755. *typestring_offset += 1;
  2756. }
  2757. write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, FALSE, typestring_offset);
  2758. write_end(file, typestring_offset);
  2759. }
  2760. else
  2761. {
  2762. unsigned int dim = size_is ? 0 : type_array_get_dim(type);
  2763. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %u */\n", (unsigned short)dim, dim);
  2764. *typestring_offset += 2;
  2765. *typestring_offset
  2766. += write_conf_or_var_desc(file, current_structure, baseoff,
  2767. type, size_is);
  2768. *typestring_offset
  2769. += write_conf_or_var_desc(file, current_structure, baseoff,
  2770. type, length_is);
  2771. write_array_element_type(file, is_string_type(attrs, type) ? attrs : NULL, type, TRUE, typestring_offset);
  2772. write_end(file, typestring_offset);
  2773. }
  2774. return start_offset;
  2775. }
  2776. static const var_t *find_array_or_string_in_struct(const type_t *type)
  2777. {
  2778. const var_list_t *fields = type_struct_get_fields(type);
  2779. const var_t *last_field;
  2780. const type_t *ft;
  2781. if (!fields || list_empty(fields))
  2782. return NULL;
  2783. last_field = LIST_ENTRY( list_tail(fields), const var_t, entry );
  2784. ft = last_field->declspec.type;
  2785. if (is_conformant_array(ft) && !type_array_is_decl_as_ptr(ft))
  2786. return last_field;
  2787. if (type_get_type(ft) == TYPE_STRUCT)
  2788. return find_array_or_string_in_struct(ft);
  2789. else
  2790. return NULL;
  2791. }
  2792. static void write_struct_members(FILE *file, const type_t *type,
  2793. int is_complex, unsigned int *corroff,
  2794. unsigned int *typestring_offset)
  2795. {
  2796. const var_t *field;
  2797. unsigned short offset = 0;
  2798. unsigned int salign = 1;
  2799. int padding;
  2800. var_list_t *fields = type_struct_get_fields(type);
  2801. if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
  2802. {
  2803. type_t *ft = field->declspec.type;
  2804. unsigned int align = 0;
  2805. unsigned int size = type_memsize_and_alignment(ft, &align);
  2806. align = clamp_align(align);
  2807. if (salign < align) salign = align;
  2808. if (!is_conformant_array(ft) || type_array_is_decl_as_ptr(ft))
  2809. {
  2810. if ((align - 1) & offset)
  2811. {
  2812. unsigned char fc = 0;
  2813. switch (align)
  2814. {
  2815. case 2:
  2816. fc = FC_ALIGNM2;
  2817. break;
  2818. case 4:
  2819. fc = FC_ALIGNM4;
  2820. break;
  2821. case 8:
  2822. fc = FC_ALIGNM8;
  2823. break;
  2824. default:
  2825. error("write_struct_members: cannot align type %d\n", type_get_type(ft));
  2826. }
  2827. print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
  2828. offset = ROUND_SIZE(offset, align);
  2829. *typestring_offset += 1;
  2830. }
  2831. write_member_type(file, type, is_complex, field->attrs, field->declspec.type, corroff,
  2832. typestring_offset);
  2833. offset += size;
  2834. }
  2835. }
  2836. padding = ROUNDING(offset, salign);
  2837. if (padding)
  2838. {
  2839. print_file(file, 2, "0x%x,\t/* FC_STRUCTPAD%d */\n",
  2840. FC_STRUCTPAD1 + padding - 1,
  2841. padding);
  2842. *typestring_offset += 1;
  2843. }
  2844. write_end(file, typestring_offset);
  2845. }
  2846. static unsigned int write_struct_tfs(FILE *file, type_t *type,
  2847. const char *name, unsigned int *tfsoff)
  2848. {
  2849. const type_t *save_current_structure = current_structure;
  2850. unsigned int total_size;
  2851. const var_t *array;
  2852. unsigned int start_offset;
  2853. unsigned int align;
  2854. unsigned int corroff;
  2855. var_t *f;
  2856. unsigned char fc = get_struct_fc(type);
  2857. var_list_t *fields = type_struct_get_fields(type);
  2858. if (processed(type)) return type->typestring_offset;
  2859. guard_rec(type);
  2860. current_structure = type;
  2861. total_size = type_memsize(type);
  2862. align = type_buffer_alignment(type);
  2863. if (total_size > USHRT_MAX)
  2864. error("structure size for %s exceeds %d bytes by %d bytes\n",
  2865. name, USHRT_MAX, total_size - USHRT_MAX);
  2866. if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
  2867. write_embedded_types(file, f->attrs, f->declspec.type, f->name, FALSE, tfsoff);
  2868. array = find_array_or_string_in_struct(type);
  2869. if (array && !processed(array->declspec.type))
  2870. {
  2871. if(is_string_type(array->attrs, array->declspec.type))
  2872. write_string_tfs(file, array->attrs, array->declspec.type, TYPE_CONTEXT_CONTAINER, array->name, tfsoff);
  2873. else
  2874. write_array_tfs(file, array->attrs, array->declspec.type, array->name, tfsoff);
  2875. }
  2876. corroff = *tfsoff;
  2877. write_descriptors(file, type, tfsoff);
  2878. start_offset = *tfsoff;
  2879. update_tfsoff(type, start_offset, file);
  2880. print_start_tfs_comment(file, type, start_offset);
  2881. print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
  2882. print_file(file, 2, "0x%x,\t/* %d */\n", align - 1, align - 1);
  2883. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)total_size, total_size);
  2884. *tfsoff += 4;
  2885. if (array)
  2886. {
  2887. unsigned int absoff = array->declspec.type->typestring_offset;
  2888. short reloff = absoff - *tfsoff;
  2889. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
  2890. reloff, reloff, absoff);
  2891. *tfsoff += 2;
  2892. }
  2893. else if (fc == FC_BOGUS_STRUCT)
  2894. {
  2895. print_file(file, 2, "NdrFcShort(0x0),\n");
  2896. *tfsoff += 2;
  2897. }
  2898. if (fc == FC_BOGUS_STRUCT)
  2899. {
  2900. /* On the sizing pass, type->ptrdesc may be zero, but it's ok as
  2901. nothing is written to file yet. On the actual writing pass,
  2902. this will have been updated. */
  2903. unsigned int absoff = type->ptrdesc ? type->ptrdesc : *tfsoff;
  2904. int reloff = absoff - *tfsoff;
  2905. assert( reloff >= 0 );
  2906. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%u) */\n",
  2907. (unsigned short)reloff, reloff, absoff);
  2908. *tfsoff += 2;
  2909. }
  2910. else if ((fc == FC_PSTRUCT) ||
  2911. (fc == FC_CPSTRUCT) ||
  2912. (fc == FC_CVSTRUCT && type_has_pointers(type)))
  2913. {
  2914. print_file(file, 2, "0x%x,\t/* FC_PP */\n", FC_PP);
  2915. print_file(file, 2, "0x%x,\t/* FC_PAD */\n", FC_PAD);
  2916. *tfsoff += 2;
  2917. write_pointer_description(file, NULL, type, tfsoff);
  2918. print_file(file, 2, "0x%x,\t/* FC_END */\n", FC_END);
  2919. *tfsoff += 1;
  2920. }
  2921. write_struct_members(file, type, fc == FC_BOGUS_STRUCT, &corroff,
  2922. tfsoff);
  2923. if (fc == FC_BOGUS_STRUCT)
  2924. {
  2925. const var_t *f;
  2926. type->ptrdesc = *tfsoff;
  2927. if (fields) LIST_FOR_EACH_ENTRY(f, fields, const var_t, entry)
  2928. {
  2929. type_t *ft = f->declspec.type;
  2930. switch (typegen_detect_type(ft, f->attrs, TDT_IGNORE_STRINGS))
  2931. {
  2932. case TGT_POINTER:
  2933. if (is_string_type(f->attrs, ft))
  2934. write_string_tfs(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, f->name, tfsoff);
  2935. else
  2936. write_pointer_tfs(file, f->attrs, ft,
  2937. type_pointer_get_ref_type(ft)->typestring_offset,
  2938. TYPE_CONTEXT_CONTAINER, tfsoff);
  2939. break;
  2940. case TGT_ARRAY:
  2941. if (type_array_is_decl_as_ptr(ft))
  2942. {
  2943. unsigned int offset;
  2944. print_file(file, 0, "/* %d */\n", *tfsoff);
  2945. offset = ft->typestring_offset;
  2946. /* skip over the pointer that is written for strings, since a
  2947. * pointer has to be written in-place here */
  2948. if (is_string_type(f->attrs, ft))
  2949. offset += 4;
  2950. write_nonsimple_pointer(file, f->attrs, ft, TYPE_CONTEXT_CONTAINER, offset, tfsoff);
  2951. }
  2952. break;
  2953. default:
  2954. break;
  2955. }
  2956. }
  2957. if (type->ptrdesc == *tfsoff)
  2958. type->ptrdesc = 0;
  2959. }
  2960. current_structure = save_current_structure;
  2961. return start_offset;
  2962. }
  2963. static void write_branch_type(FILE *file, const type_t *t, unsigned int *tfsoff)
  2964. {
  2965. if (t == NULL)
  2966. {
  2967. print_file(file, 2, "NdrFcShort(0x0),\t/* No type */\n");
  2968. }
  2969. else
  2970. {
  2971. if (type_get_type(t) == TYPE_BASIC || type_get_type(t) == TYPE_ENUM)
  2972. {
  2973. unsigned char fc;
  2974. if (type_get_type(t) == TYPE_BASIC)
  2975. fc = get_basic_fc(t);
  2976. else
  2977. fc = get_enum_fc(t);
  2978. print_file(file, 2, "NdrFcShort(0x80%02x),\t/* Simple arm type: %s */\n",
  2979. fc, string_of_type(fc));
  2980. }
  2981. else if (t->typestring_offset)
  2982. {
  2983. short reloff = t->typestring_offset - *tfsoff;
  2984. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %d (%d) */\n",
  2985. reloff, reloff, t->typestring_offset);
  2986. }
  2987. else
  2988. error("write_branch_type: type unimplemented %d\n", type_get_type(t));
  2989. }
  2990. *tfsoff += 2;
  2991. }
  2992. static unsigned int write_union_tfs(FILE *file, const attr_list_t *attrs,
  2993. type_t *type, unsigned int *tfsoff)
  2994. {
  2995. unsigned int start_offset;
  2996. unsigned int size;
  2997. var_list_t *fields;
  2998. unsigned int nbranch = 0;
  2999. type_t *deftype = NULL;
  3000. short nodeftype = 0xffff;
  3001. unsigned int dummy;
  3002. var_t *f;
  3003. if (processed(type) &&
  3004. (type_get_type(type) == TYPE_ENCAPSULATED_UNION || !is_attr(type->attrs, ATTR_SWITCHTYPE)))
  3005. return type->typestring_offset;
  3006. guard_rec(type);
  3007. fields = type_union_get_cases(type);
  3008. size = union_memsize(fields, &dummy);
  3009. if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
  3010. {
  3011. expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
  3012. if (cases)
  3013. nbranch += list_count(cases);
  3014. if (f->declspec.type)
  3015. write_embedded_types(file, f->attrs, f->declspec.type, f->name, TRUE, tfsoff);
  3016. }
  3017. start_offset = *tfsoff;
  3018. update_tfsoff(type, start_offset, file);
  3019. print_start_tfs_comment(file, type, start_offset);
  3020. if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
  3021. {
  3022. const var_t *sv = type_union_get_switch_value(type);
  3023. const type_t *st = sv->declspec.type;
  3024. unsigned int align = 0;
  3025. unsigned char fc;
  3026. if (type_get_type(st) == TYPE_BASIC)
  3027. {
  3028. fc = get_basic_fc(st);
  3029. switch (fc)
  3030. {
  3031. case FC_CHAR:
  3032. case FC_SMALL:
  3033. case FC_BYTE:
  3034. case FC_USMALL:
  3035. case FC_WCHAR:
  3036. case FC_SHORT:
  3037. case FC_USHORT:
  3038. case FC_LONG:
  3039. case FC_ULONG:
  3040. break;
  3041. default:
  3042. fc = 0;
  3043. error("union switch type must be an integer, char, or enum\n");
  3044. }
  3045. }
  3046. else if (type_get_type(st) == TYPE_ENUM)
  3047. fc = get_enum_fc(st);
  3048. else
  3049. error("union switch type must be an integer, char, or enum\n");
  3050. type_memsize_and_alignment(st, &align);
  3051. if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
  3052. {
  3053. if (f->declspec.type)
  3054. type_memsize_and_alignment(f->declspec.type, &align);
  3055. }
  3056. print_file(file, 2, "0x%x,\t/* FC_ENCAPSULATED_UNION */\n", FC_ENCAPSULATED_UNION);
  3057. print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
  3058. (align << 4) | fc, string_of_type(fc));
  3059. *tfsoff += 2;
  3060. }
  3061. else if (is_attr(type->attrs, ATTR_SWITCHTYPE))
  3062. {
  3063. const expr_t *switch_is = get_attrp(attrs, ATTR_SWITCHIS);
  3064. const type_t *st = get_attrp(type->attrs, ATTR_SWITCHTYPE);
  3065. unsigned char fc;
  3066. if (type_get_type(st) == TYPE_BASIC)
  3067. {
  3068. fc = get_basic_fc(st);
  3069. switch (fc)
  3070. {
  3071. case FC_CHAR:
  3072. case FC_SMALL:
  3073. case FC_USMALL:
  3074. case FC_SHORT:
  3075. case FC_USHORT:
  3076. case FC_LONG:
  3077. case FC_ULONG:
  3078. case FC_ENUM16:
  3079. case FC_ENUM32:
  3080. break;
  3081. default:
  3082. fc = 0;
  3083. error("union switch type must be an integer, char, or enum\n");
  3084. }
  3085. }
  3086. else if (type_get_type(st) == TYPE_ENUM)
  3087. fc = get_enum_fc(st);
  3088. else
  3089. error("union switch type must be an integer, char, or enum\n");
  3090. print_file(file, 2, "0x%x,\t/* FC_NON_ENCAPSULATED_UNION */\n", FC_NON_ENCAPSULATED_UNION);
  3091. print_file(file, 2, "0x%x,\t/* Switch type= %s */\n",
  3092. fc, string_of_type(fc));
  3093. *tfsoff += 2;
  3094. *tfsoff += write_conf_or_var_desc(file, current_structure, 0, st, switch_is );
  3095. print_file(file, 2, "NdrFcShort(0x2),\t/* Offset= 2 (%u) */\n", *tfsoff + 2);
  3096. *tfsoff += 2;
  3097. print_file(file, 0, "/* %u */\n", *tfsoff);
  3098. }
  3099. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)size, size);
  3100. print_file(file, 2, "NdrFcShort(0x%hx),\t/* %d */\n", (unsigned short)nbranch, nbranch);
  3101. *tfsoff += 4;
  3102. if (fields) LIST_FOR_EACH_ENTRY(f, fields, var_t, entry)
  3103. {
  3104. type_t *ft = f->declspec.type;
  3105. expr_list_t *cases = get_attrp(f->attrs, ATTR_CASE);
  3106. int deflt = is_attr(f->attrs, ATTR_DEFAULT);
  3107. expr_t *c;
  3108. if (cases == NULL && !deflt)
  3109. error("union field %s with neither case nor default attribute\n", f->name);
  3110. if (cases) LIST_FOR_EACH_ENTRY(c, cases, expr_t, entry)
  3111. {
  3112. /* MIDL doesn't check for duplicate cases, even though that seems
  3113. like a reasonable thing to do, it just dumps them to the TFS
  3114. like we're going to do here. */
  3115. print_file(file, 2, "NdrFcLong(0x%x),\t/* %d */\n", c->cval, c->cval);
  3116. *tfsoff += 4;
  3117. write_branch_type(file, ft, tfsoff);
  3118. }
  3119. /* MIDL allows multiple default branches, even though that seems
  3120. illogical, it just chooses the last one, which is what we will
  3121. do. */
  3122. if (deflt)
  3123. {
  3124. deftype = ft;
  3125. nodeftype = 0;
  3126. }
  3127. }
  3128. if (deftype)
  3129. {
  3130. write_branch_type(file, deftype, tfsoff);
  3131. }
  3132. else
  3133. {
  3134. print_file(file, 2, "NdrFcShort(0x%hx),\n", nodeftype);
  3135. *tfsoff += 2;
  3136. }
  3137. return start_offset;
  3138. }
  3139. static unsigned int write_ip_tfs(FILE *file, const attr_list_t *attrs, type_t *type,
  3140. unsigned int *typeformat_offset)
  3141. {
  3142. unsigned int i;
  3143. unsigned int start_offset = *typeformat_offset;
  3144. expr_t *iid = get_attrp(attrs, ATTR_IIDIS);
  3145. if (!iid && processed(type)) return type->typestring_offset;
  3146. print_start_tfs_comment(file, type, start_offset);
  3147. update_tfsoff(type, start_offset, file);
  3148. if (iid)
  3149. {
  3150. print_file(file, 2, "0x2f, /* FC_IP */\n");
  3151. print_file(file, 2, "0x5c, /* FC_PAD */\n");
  3152. *typeformat_offset
  3153. += write_conf_or_var_desc(file, current_structure, 0, type, iid) + 2;
  3154. }
  3155. else
  3156. {
  3157. const type_t *base = is_ptr(type) ? type_pointer_get_ref_type(type) : type;
  3158. const UUID *uuid = get_attrp(base->attrs, ATTR_UUID);
  3159. if (! uuid)
  3160. error("%s: interface %s missing UUID\n", __FUNCTION__, base->name);
  3161. print_file(file, 2, "0x2f,\t/* FC_IP */\n");
  3162. print_file(file, 2, "0x5a,\t/* FC_CONSTANT_IID */\n");
  3163. print_file(file, 2, "NdrFcLong(0x%08x),\n", uuid->Data1);
  3164. print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data2);
  3165. print_file(file, 2, "NdrFcShort(0x%04x),\n", uuid->Data3);
  3166. for (i = 0; i < 8; ++i)
  3167. print_file(file, 2, "0x%02x,\n", uuid->Data4[i]);
  3168. if (file)
  3169. fprintf(file, "\n");
  3170. *typeformat_offset += 18;
  3171. }
  3172. return start_offset;
  3173. }
  3174. static unsigned int write_contexthandle_tfs(FILE *file,
  3175. const attr_list_t *attrs,
  3176. type_t *type,
  3177. enum type_context context,
  3178. unsigned int *typeformat_offset)
  3179. {
  3180. unsigned int start_offset = *typeformat_offset;
  3181. unsigned char flags = get_contexthandle_flags( current_iface, attrs, type, context == TYPE_CONTEXT_RETVAL );
  3182. print_start_tfs_comment(file, type, start_offset);
  3183. if (flags & 0x80) /* via ptr */
  3184. {
  3185. int pointer_type = get_pointer_fc( type, attrs, context == TYPE_CONTEXT_TOPLEVELPARAM );
  3186. if (!pointer_type) pointer_type = FC_RP;
  3187. *typeformat_offset += 4;
  3188. print_file(file, 2,"0x%x, 0x0,\t/* %s */\n", pointer_type, string_of_type(pointer_type) );
  3189. print_file(file, 2, "NdrFcShort(0x2),\t /* Offset= 2 (%u) */\n", *typeformat_offset);
  3190. print_file(file, 0, "/* %2u */\n", *typeformat_offset);
  3191. }
  3192. print_file(file, 2, "0x%02x,\t/* FC_BIND_CONTEXT */\n", FC_BIND_CONTEXT);
  3193. print_file(file, 2, "0x%x,\t/* Context flags: ", flags);
  3194. if (flags & NDR_CONTEXT_HANDLE_CANNOT_BE_NULL)
  3195. print_file(file, 0, "can't be null, ");
  3196. if (flags & NDR_CONTEXT_HANDLE_SERIALIZE)
  3197. print_file(file, 0, "serialize, ");
  3198. if (flags & NDR_CONTEXT_HANDLE_NOSERIALIZE)
  3199. print_file(file, 0, "no serialize, ");
  3200. if (flags & NDR_STRICT_CONTEXT_HANDLE)
  3201. print_file(file, 0, "strict, ");
  3202. if (flags & HANDLE_PARAM_IS_RETURN)
  3203. print_file(file, 0, "return, ");
  3204. if (flags & HANDLE_PARAM_IS_OUT)
  3205. print_file(file, 0, "out, ");
  3206. if (flags & HANDLE_PARAM_IS_IN)
  3207. print_file(file, 0, "in, ");
  3208. if (flags & HANDLE_PARAM_IS_VIA_PTR)
  3209. print_file(file, 0, "via ptr, ");
  3210. print_file(file, 0, "*/\n");
  3211. print_file(file, 2, "0x%x,\t/* rundown routine */\n", get_context_handle_offset( type ));
  3212. print_file(file, 2, "0, /* FIXME: param num */\n");
  3213. *typeformat_offset += 4;
  3214. update_tfsoff( type, start_offset, file );
  3215. return start_offset;
  3216. }
  3217. static unsigned int write_range_tfs(FILE *file, const attr_list_t *attrs,
  3218. type_t *type, expr_list_t *range_list,
  3219. unsigned int *typeformat_offset)
  3220. {
  3221. unsigned char fc;
  3222. unsigned int start_offset = *typeformat_offset;
  3223. const expr_t *range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
  3224. const expr_t *range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
  3225. if (type_get_type(type) == TYPE_BASIC)
  3226. fc = get_basic_fc(type);
  3227. else
  3228. fc = get_enum_fc(type);
  3229. /* fc must fit in lower 4-bits of 8-bit field below */
  3230. assert(fc <= 0xf);
  3231. print_file(file, 0, "/* %u */\n", *typeformat_offset);
  3232. print_file(file, 2, "0x%x,\t/* FC_RANGE */\n", FC_RANGE);
  3233. print_file(file, 2, "0x%x,\t/* %s */\n", fc, string_of_type(fc));
  3234. print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_min->cval, range_min->cval);
  3235. print_file(file, 2, "NdrFcLong(0x%x),\t/* %u */\n", range_max->cval, range_max->cval);
  3236. update_tfsoff( type, start_offset, file );
  3237. *typeformat_offset += 10;
  3238. return start_offset;
  3239. }
  3240. static unsigned int write_type_tfs(FILE *file, const attr_list_t *attrs,
  3241. type_t *type, const char *name,
  3242. enum type_context context,
  3243. unsigned int *typeformat_offset)
  3244. {
  3245. unsigned int offset;
  3246. switch (typegen_detect_type(type, attrs, TDT_ALL_TYPES))
  3247. {
  3248. case TGT_CTXT_HANDLE:
  3249. case TGT_CTXT_HANDLE_POINTER:
  3250. return write_contexthandle_tfs(file, attrs, type, context, typeformat_offset);
  3251. case TGT_USER_TYPE:
  3252. return write_user_tfs(file, type, typeformat_offset);
  3253. case TGT_STRING:
  3254. return write_string_tfs(file, attrs, type, context, name, typeformat_offset);
  3255. case TGT_ARRAY:
  3256. {
  3257. unsigned int off;
  3258. /* conformant and pointer arrays are handled specially */
  3259. if ((context != TYPE_CONTEXT_CONTAINER &&
  3260. context != TYPE_CONTEXT_CONTAINER_NO_POINTERS) ||
  3261. !is_conformant_array(type) || type_array_is_decl_as_ptr(type))
  3262. off = write_array_tfs(file, attrs, type, name, typeformat_offset);
  3263. else
  3264. off = 0;
  3265. if (context != TYPE_CONTEXT_CONTAINER &&
  3266. context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
  3267. {
  3268. int ptr_type;
  3269. ptr_type = get_pointer_fc_context(type, attrs, context);
  3270. if (type_array_is_decl_as_ptr(type)
  3271. || (ptr_type != FC_RP && context == TYPE_CONTEXT_TOPLEVELPARAM))
  3272. {
  3273. unsigned int absoff = type->typestring_offset;
  3274. short reloff = absoff - (*typeformat_offset + 2);
  3275. off = *typeformat_offset;
  3276. print_file(file, 0, "/* %d */\n", off);
  3277. print_file(file, 2, "0x%x, 0x0,\t/* %s */\n", ptr_type,
  3278. string_of_type(ptr_type));
  3279. print_file(file, 2, "NdrFcShort(0x%hx),\t/* Offset= %hd (%u) */\n",
  3280. reloff, reloff, absoff);
  3281. if (ptr_type != FC_RP) update_tfsoff( type, off, file );
  3282. *typeformat_offset += 4;
  3283. }
  3284. type_array_set_ptr_tfsoff(type, off);
  3285. }
  3286. return off;
  3287. }
  3288. case TGT_STRUCT:
  3289. return write_struct_tfs(file, type, name, typeformat_offset);
  3290. case TGT_UNION:
  3291. return write_union_tfs(file, attrs, type, typeformat_offset);
  3292. case TGT_ENUM:
  3293. case TGT_BASIC:
  3294. /* nothing to do */
  3295. return 0;
  3296. case TGT_RANGE:
  3297. {
  3298. expr_list_t *range_list = get_attrp(attrs, ATTR_RANGE);
  3299. if (!range_list)
  3300. range_list = get_aliaschain_attrp(type, ATTR_RANGE);
  3301. return write_range_tfs(file, attrs, type, range_list, typeformat_offset);
  3302. }
  3303. case TGT_IFACE_POINTER:
  3304. return write_ip_tfs(file, attrs, type, typeformat_offset);
  3305. case TGT_POINTER:
  3306. {
  3307. enum type_context ref_context;
  3308. type_t *ref = type_pointer_get_ref_type(type);
  3309. if (context == TYPE_CONTEXT_TOPLEVELPARAM)
  3310. ref_context = TYPE_CONTEXT_PARAM;
  3311. else if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
  3312. ref_context = TYPE_CONTEXT_CONTAINER;
  3313. else
  3314. ref_context = context;
  3315. if (is_string_type(attrs, ref))
  3316. {
  3317. if (context != TYPE_CONTEXT_CONTAINER_NO_POINTERS)
  3318. write_pointer_tfs(file, attrs, type, *typeformat_offset + 4, context, typeformat_offset);
  3319. offset = write_type_tfs(file, attrs, ref, name, ref_context, typeformat_offset);
  3320. if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
  3321. return 0;
  3322. return offset;
  3323. }
  3324. offset = write_type_tfs( file, attrs, type_pointer_get_ref_type(type), name,
  3325. ref_context, typeformat_offset);
  3326. if (context == TYPE_CONTEXT_CONTAINER_NO_POINTERS)
  3327. return 0;
  3328. return write_pointer_tfs(file, attrs, type, offset, context, typeformat_offset);
  3329. }
  3330. case TGT_INVALID:
  3331. break;
  3332. }
  3333. error("invalid type %s for var %s\n", type->name, name);
  3334. return 0;
  3335. }
  3336. static int write_embedded_types(FILE *file, const attr_list_t *attrs, type_t *type,
  3337. const char *name, int write_ptr, unsigned int *tfsoff)
  3338. {
  3339. return write_type_tfs(file, attrs, type, name, write_ptr ? TYPE_CONTEXT_CONTAINER : TYPE_CONTEXT_CONTAINER_NO_POINTERS, tfsoff);
  3340. }
  3341. static void process_tfs_iface(type_t *iface, FILE *file, int indent, unsigned int *offset)
  3342. {
  3343. const statement_list_t *stmts = type_iface_get_stmts(iface);
  3344. const statement_t *stmt;
  3345. var_t *var;
  3346. current_iface = iface;
  3347. if (stmts) LIST_FOR_EACH_ENTRY( stmt, stmts, statement_t, entry )
  3348. {
  3349. switch(stmt->type)
  3350. {
  3351. case STMT_DECLARATION:
  3352. {
  3353. const var_t *func = stmt->u.var;
  3354. if(stmt->u.var->declspec.stgclass != STG_NONE
  3355. || type_get_type_detect_alias(stmt->u.var->declspec.type) != TYPE_FUNCTION)
  3356. continue;
  3357. current_func = func;
  3358. if (is_local(func->attrs)) continue;
  3359. var = type_function_get_retval(func->declspec.type);
  3360. if (!is_void(var->declspec.type))
  3361. var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, func->name,
  3362. TYPE_CONTEXT_RETVAL, offset);
  3363. if (type_function_get_args(func->declspec.type))
  3364. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
  3365. var->typestring_offset = write_type_tfs( file, var->attrs, var->declspec.type, var->name,
  3366. TYPE_CONTEXT_TOPLEVELPARAM, offset );
  3367. break;
  3368. }
  3369. case STMT_TYPEDEF:
  3370. {
  3371. typeref_t *ref;
  3372. if (stmt->u.type_list) LIST_FOR_EACH_ENTRY(ref, stmt->u.type_list, typeref_t, entry)
  3373. {
  3374. if (is_attr(ref->type->attrs, ATTR_ENCODE)
  3375. || is_attr(ref->type->attrs, ATTR_DECODE))
  3376. ref->type->typestring_offset = write_type_tfs( file,
  3377. ref->type->attrs, ref->type, ref->type->name,
  3378. TYPE_CONTEXT_CONTAINER, offset);
  3379. }
  3380. break;
  3381. }
  3382. default:
  3383. break;
  3384. }
  3385. }
  3386. }
  3387. static unsigned int process_tfs(FILE *file, const statement_list_t *stmts, type_pred_t pred)
  3388. {
  3389. unsigned int typeformat_offset = 2;
  3390. for_each_iface(stmts, process_tfs_iface, pred, file, 0, &typeformat_offset);
  3391. return typeformat_offset + 1;
  3392. }
  3393. void write_typeformatstring(FILE *file, const statement_list_t *stmts, type_pred_t pred)
  3394. {
  3395. int indent = 0;
  3396. print_file(file, indent, "static const MIDL_TYPE_FORMAT_STRING __MIDL_TypeFormatString =\n");
  3397. print_file(file, indent, "{\n");
  3398. indent++;
  3399. print_file(file, indent, "0,\n");
  3400. print_file(file, indent, "{\n");
  3401. indent++;
  3402. print_file(file, indent, "NdrFcShort(0x0),\n");
  3403. set_all_tfswrite(TRUE);
  3404. process_tfs(file, stmts, pred);
  3405. print_file(file, indent, "0x0\n");
  3406. indent--;
  3407. print_file(file, indent, "}\n");
  3408. indent--;
  3409. print_file(file, indent, "};\n");
  3410. print_file(file, indent, "\n");
  3411. }
  3412. static unsigned int get_required_buffer_size_type(
  3413. const type_t *type, const char *name, const attr_list_t *attrs, int toplevel_param, unsigned int *alignment)
  3414. {
  3415. *alignment = 0;
  3416. switch (typegen_detect_type(type, NULL, TDT_IGNORE_RANGES))
  3417. {
  3418. case TGT_USER_TYPE:
  3419. {
  3420. const char *uname = NULL;
  3421. const type_t *utype = get_user_type(type, &uname);
  3422. return get_required_buffer_size_type(utype, uname, NULL, FALSE, alignment);
  3423. }
  3424. case TGT_BASIC:
  3425. switch (get_basic_fc(type))
  3426. {
  3427. case FC_BYTE:
  3428. case FC_CHAR:
  3429. case FC_USMALL:
  3430. case FC_SMALL:
  3431. *alignment = 4;
  3432. return 1;
  3433. case FC_WCHAR:
  3434. case FC_USHORT:
  3435. case FC_SHORT:
  3436. *alignment = 4;
  3437. return 2;
  3438. case FC_ULONG:
  3439. case FC_LONG:
  3440. case FC_FLOAT:
  3441. case FC_ERROR_STATUS_T:
  3442. *alignment = 4;
  3443. return 4;
  3444. case FC_HYPER:
  3445. case FC_DOUBLE:
  3446. *alignment = 8;
  3447. return 8;
  3448. case FC_INT3264:
  3449. case FC_UINT3264:
  3450. assert( pointer_size );
  3451. *alignment = pointer_size;
  3452. return pointer_size;
  3453. case FC_IGNORE:
  3454. case FC_BIND_PRIMITIVE:
  3455. return 0;
  3456. default:
  3457. error("get_required_buffer_size: unknown basic type 0x%02x\n",
  3458. get_basic_fc(type));
  3459. return 0;
  3460. }
  3461. break;
  3462. case TGT_ENUM:
  3463. switch (get_enum_fc(type))
  3464. {
  3465. case FC_ENUM32:
  3466. *alignment = 4;
  3467. return 4;
  3468. case FC_ENUM16:
  3469. *alignment = 4;
  3470. return 2;
  3471. }
  3472. break;
  3473. case TGT_STRUCT:
  3474. if (get_struct_fc(type) == FC_STRUCT)
  3475. {
  3476. if (!type_struct_get_fields(type)) return 0;
  3477. return fields_memsize(type_struct_get_fields(type), alignment);
  3478. }
  3479. break;
  3480. case TGT_POINTER:
  3481. {
  3482. unsigned int size, align;
  3483. const type_t *ref = type_pointer_get_ref_type(type);
  3484. if (is_string_type( attrs, ref )) break;
  3485. if (!(size = get_required_buffer_size_type( ref, name, NULL, FALSE, &align ))) break;
  3486. if (get_pointer_fc(type, attrs, toplevel_param) != FC_RP)
  3487. {
  3488. size += 4 + align;
  3489. align = 4;
  3490. }
  3491. *alignment = align;
  3492. return size;
  3493. }
  3494. case TGT_ARRAY:
  3495. switch (get_array_fc(type))
  3496. {
  3497. case FC_SMFARRAY:
  3498. case FC_LGFARRAY:
  3499. return type_array_get_dim(type) *
  3500. get_required_buffer_size_type(type_array_get_element_type(type), name,
  3501. NULL, FALSE, alignment);
  3502. }
  3503. break;
  3504. default:
  3505. break;
  3506. }
  3507. return 0;
  3508. }
  3509. static unsigned int get_required_buffer_size(const var_t *var, unsigned int *alignment, enum pass pass)
  3510. {
  3511. int in_attr = is_attr(var->attrs, ATTR_IN);
  3512. int out_attr = is_attr(var->attrs, ATTR_OUT);
  3513. if (!in_attr && !out_attr)
  3514. in_attr = 1;
  3515. *alignment = 0;
  3516. if ((pass == PASS_IN && in_attr) || (pass == PASS_OUT && out_attr) ||
  3517. pass == PASS_RETURN)
  3518. {
  3519. if (is_ptrchain_attr(var, ATTR_CONTEXTHANDLE))
  3520. {
  3521. *alignment = 4;
  3522. return 20;
  3523. }
  3524. if (!is_string_type(var->attrs, var->declspec.type))
  3525. return get_required_buffer_size_type(var->declspec.type, var->name,
  3526. var->attrs, TRUE, alignment);
  3527. }
  3528. return 0;
  3529. }
  3530. static unsigned int get_function_buffer_size( const var_t *func, enum pass pass )
  3531. {
  3532. const var_t *var;
  3533. unsigned int total_size = 0, alignment;
  3534. if (type_function_get_args(func->declspec.type))
  3535. {
  3536. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  3537. {
  3538. total_size += get_required_buffer_size(var, &alignment, pass);
  3539. total_size += alignment;
  3540. }
  3541. }
  3542. if (pass == PASS_OUT && !is_void(type_function_get_rettype(func->declspec.type)))
  3543. {
  3544. var_t v = *func;
  3545. v.declspec.type = type_function_get_rettype(func->declspec.type);
  3546. total_size += get_required_buffer_size(&v, &alignment, PASS_RETURN);
  3547. total_size += alignment;
  3548. }
  3549. return total_size;
  3550. }
  3551. static void print_phase_function(FILE *file, int indent, const char *type,
  3552. const char *local_var_prefix, enum remoting_phase phase,
  3553. const var_t *var, unsigned int type_offset)
  3554. {
  3555. const char *function;
  3556. switch (phase)
  3557. {
  3558. case PHASE_BUFFERSIZE:
  3559. function = "BufferSize";
  3560. break;
  3561. case PHASE_MARSHAL:
  3562. function = "Marshall";
  3563. break;
  3564. case PHASE_UNMARSHAL:
  3565. function = "Unmarshall";
  3566. break;
  3567. case PHASE_FREE:
  3568. function = "Free";
  3569. break;
  3570. default:
  3571. assert(0);
  3572. return;
  3573. }
  3574. print_file(file, indent, "Ndr%s%s(\n", type, function);
  3575. indent++;
  3576. print_file(file, indent, "&__frame->_StubMsg,\n");
  3577. print_file(file, indent, "%s%s%s%s%s,\n",
  3578. (phase == PHASE_UNMARSHAL) ? "(unsigned char **)" : "(unsigned char *)",
  3579. (phase == PHASE_UNMARSHAL || decl_indirect(var->declspec.type)) ? "&" : "",
  3580. local_var_prefix,
  3581. (phase == PHASE_UNMARSHAL && decl_indirect(var->declspec.type)) ? "_p_" : "",
  3582. var->name);
  3583. print_file(file, indent, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]%s\n",
  3584. type_offset, (phase == PHASE_UNMARSHAL) ? "," : ");");
  3585. if (phase == PHASE_UNMARSHAL)
  3586. print_file(file, indent, "0);\n");
  3587. indent--;
  3588. }
  3589. void print_phase_basetype(FILE *file, int indent, const char *local_var_prefix,
  3590. enum remoting_phase phase, enum pass pass, const var_t *var,
  3591. const char *varname)
  3592. {
  3593. type_t *type = var->declspec.type;
  3594. unsigned int alignment = 0;
  3595. /* no work to do for other phases, buffer sizing is done elsewhere */
  3596. if (phase != PHASE_MARSHAL && phase != PHASE_UNMARSHAL)
  3597. return;
  3598. if (type_get_type(type) == TYPE_ENUM ||
  3599. (type_get_type(type) == TYPE_BASIC &&
  3600. type_basic_get_type(type) == TYPE_BASIC_INT3264 &&
  3601. pointer_size != 4))
  3602. {
  3603. unsigned char fc;
  3604. if (type_get_type(type) == TYPE_ENUM)
  3605. fc = get_enum_fc(type);
  3606. else
  3607. fc = get_basic_fc(type);
  3608. if (phase == PHASE_MARSHAL)
  3609. print_file(file, indent, "NdrSimpleTypeMarshall(\n");
  3610. else
  3611. print_file(file, indent, "NdrSimpleTypeUnmarshall(\n");
  3612. print_file(file, indent+1, "&__frame->_StubMsg,\n");
  3613. print_file(file, indent+1, "(unsigned char *)&%s%s,\n",
  3614. local_var_prefix,
  3615. var->name);
  3616. print_file(file, indent+1, "0x%02x /* %s */);\n", fc, string_of_type(fc));
  3617. }
  3618. else
  3619. {
  3620. const decl_spec_t *ref = is_ptr(type) ? type_pointer_get_ref(type) : &var->declspec;
  3621. switch (get_basic_fc(ref->type))
  3622. {
  3623. case FC_BYTE:
  3624. case FC_CHAR:
  3625. case FC_SMALL:
  3626. case FC_USMALL:
  3627. alignment = 1;
  3628. break;
  3629. case FC_WCHAR:
  3630. case FC_USHORT:
  3631. case FC_SHORT:
  3632. alignment = 2;
  3633. break;
  3634. case FC_ULONG:
  3635. case FC_LONG:
  3636. case FC_FLOAT:
  3637. case FC_ERROR_STATUS_T:
  3638. /* pointer_size must be 4 if we got here in these two cases */
  3639. case FC_INT3264:
  3640. case FC_UINT3264:
  3641. alignment = 4;
  3642. break;
  3643. case FC_HYPER:
  3644. case FC_DOUBLE:
  3645. alignment = 8;
  3646. break;
  3647. case FC_IGNORE:
  3648. case FC_BIND_PRIMITIVE:
  3649. /* no marshalling needed */
  3650. return;
  3651. default:
  3652. error("print_phase_basetype: Unsupported type: %s (0x%02x, ptr_level: 0)\n",
  3653. var->name, get_basic_fc(ref->type));
  3654. }
  3655. if (phase == PHASE_MARSHAL && alignment > 1)
  3656. print_file(file, indent, "MIDL_memset(__frame->_StubMsg.Buffer, 0, (0x%x - (ULONG_PTR)__frame->_StubMsg.Buffer) & 0x%x);\n", alignment, alignment - 1);
  3657. print_file(file, indent, "__frame->_StubMsg.Buffer = (unsigned char *)(((ULONG_PTR)__frame->_StubMsg.Buffer + %u) & ~0x%x);\n",
  3658. alignment - 1, alignment - 1);
  3659. if (phase == PHASE_MARSHAL)
  3660. {
  3661. print_file(file, indent, "*(");
  3662. write_type_decl(file, ref, NULL);
  3663. if (is_ptr(type))
  3664. fprintf(file, " *)__frame->_StubMsg.Buffer = *");
  3665. else
  3666. fprintf(file, " *)__frame->_StubMsg.Buffer = ");
  3667. fprintf(file, "%s%s", local_var_prefix, varname);
  3668. fprintf(file, ";\n");
  3669. }
  3670. else if (phase == PHASE_UNMARSHAL)
  3671. {
  3672. print_file(file, indent, "if (__frame->_StubMsg.Buffer + sizeof(");
  3673. write_type_decl(file, ref, NULL);
  3674. fprintf(file, ") > __frame->_StubMsg.BufferEnd)\n");
  3675. print_file(file, indent, "{\n");
  3676. print_file(file, indent + 1, "RpcRaiseException(RPC_X_BAD_STUB_DATA);\n");
  3677. print_file(file, indent, "}\n");
  3678. print_file(file, indent, "%s%s%s",
  3679. (pass == PASS_IN || pass == PASS_RETURN) ? "" : "*",
  3680. local_var_prefix, varname);
  3681. if (pass == PASS_IN && is_ptr(type))
  3682. fprintf(file, " = (");
  3683. else
  3684. fprintf(file, " = *(");
  3685. write_type_decl(file, ref, NULL);
  3686. fprintf(file, " *)__frame->_StubMsg.Buffer;\n");
  3687. }
  3688. print_file(file, indent, "__frame->_StubMsg.Buffer += sizeof(");
  3689. write_type_decl(file, ref, NULL);
  3690. fprintf(file, ");\n");
  3691. }
  3692. }
  3693. /* returns whether the MaxCount, Offset or ActualCount members need to be
  3694. * filled in for the specified phase */
  3695. static inline int is_conformance_needed_for_phase(enum remoting_phase phase)
  3696. {
  3697. return (phase != PHASE_UNMARSHAL);
  3698. }
  3699. expr_t *get_size_is_expr(const type_t *t, const char *name)
  3700. {
  3701. expr_t *x = NULL;
  3702. for ( ; is_array(t); t = type_array_get_element_type(t))
  3703. if (type_array_has_conformance(t) &&
  3704. type_array_get_conformance(t)->type != EXPR_VOID)
  3705. {
  3706. if (!x)
  3707. x = type_array_get_conformance(t);
  3708. else
  3709. error("%s: multidimensional conformant"
  3710. " arrays not supported at the top level\n",
  3711. name);
  3712. }
  3713. return x;
  3714. }
  3715. void write_parameter_conf_or_var_exprs(FILE *file, int indent, const char *local_var_prefix,
  3716. enum remoting_phase phase, const var_t *var, int valid_variance)
  3717. {
  3718. const type_t *type = var->declspec.type;
  3719. /* get fundamental type for the argument */
  3720. for (;;)
  3721. {
  3722. switch (typegen_detect_type(type, var->attrs, TDT_IGNORE_STRINGS|TDT_IGNORE_RANGES))
  3723. {
  3724. case TGT_ARRAY:
  3725. if (is_conformance_needed_for_phase(phase))
  3726. {
  3727. if (type_array_has_conformance(type) &&
  3728. type_array_get_conformance(type)->type != EXPR_VOID)
  3729. {
  3730. print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
  3731. write_expr(file, type_array_get_conformance(type), 1, 1, NULL, NULL, local_var_prefix);
  3732. fprintf(file, ";\n\n");
  3733. }
  3734. if (type_array_has_variance(type))
  3735. {
  3736. print_file(file, indent, "__frame->_StubMsg.Offset = 0;\n"); /* FIXME */
  3737. if (valid_variance)
  3738. {
  3739. print_file(file, indent, "__frame->_StubMsg.ActualCount = (ULONG_PTR)");
  3740. write_expr(file, type_array_get_variance(type), 1, 1, NULL, NULL, local_var_prefix);
  3741. fprintf(file, ";\n\n");
  3742. }
  3743. else
  3744. print_file(file, indent, "__frame->_StubMsg.ActualCount = __frame->_StubMsg.MaxCount;\n\n");
  3745. }
  3746. }
  3747. break;
  3748. case TGT_UNION:
  3749. if (type_get_type(type) == TYPE_UNION &&
  3750. is_conformance_needed_for_phase(phase))
  3751. {
  3752. print_file(file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR)");
  3753. write_expr(file, get_attrp(var->attrs, ATTR_SWITCHIS), 1, 1, NULL, NULL, local_var_prefix);
  3754. fprintf(file, ";\n\n");
  3755. }
  3756. break;
  3757. case TGT_IFACE_POINTER:
  3758. {
  3759. expr_t *iid;
  3760. if (is_conformance_needed_for_phase(phase) && (iid = get_attrp( var->attrs, ATTR_IIDIS )))
  3761. {
  3762. print_file( file, indent, "__frame->_StubMsg.MaxCount = (ULONG_PTR) " );
  3763. write_expr( file, iid, 1, 1, NULL, NULL, local_var_prefix );
  3764. fprintf( file, ";\n\n" );
  3765. }
  3766. break;
  3767. }
  3768. case TGT_POINTER:
  3769. type = type_pointer_get_ref_type(type);
  3770. continue;
  3771. case TGT_INVALID:
  3772. case TGT_USER_TYPE:
  3773. case TGT_CTXT_HANDLE:
  3774. case TGT_CTXT_HANDLE_POINTER:
  3775. case TGT_STRING:
  3776. case TGT_BASIC:
  3777. case TGT_ENUM:
  3778. case TGT_STRUCT:
  3779. case TGT_RANGE:
  3780. break;
  3781. }
  3782. break;
  3783. }
  3784. }
  3785. static void write_remoting_arg(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
  3786. enum pass pass, enum remoting_phase phase, const var_t *var)
  3787. {
  3788. int in_attr, out_attr, pointer_type;
  3789. const char *type_str = NULL;
  3790. const type_t *type = var->declspec.type;
  3791. unsigned int alignment, start_offset = type->typestring_offset;
  3792. if (is_ptr(type) || is_array(type))
  3793. pointer_type = get_pointer_fc(type, var->attrs, pass != PASS_RETURN);
  3794. else
  3795. pointer_type = 0;
  3796. in_attr = is_attr(var->attrs, ATTR_IN);
  3797. out_attr = is_attr(var->attrs, ATTR_OUT);
  3798. if (!in_attr && !out_attr)
  3799. in_attr = 1;
  3800. if (phase != PHASE_FREE)
  3801. switch (pass)
  3802. {
  3803. case PASS_IN:
  3804. if (!in_attr) return;
  3805. break;
  3806. case PASS_OUT:
  3807. if (!out_attr) return;
  3808. break;
  3809. case PASS_RETURN:
  3810. break;
  3811. }
  3812. if (phase == PHASE_BUFFERSIZE && get_required_buffer_size( var, &alignment, pass )) return;
  3813. write_parameter_conf_or_var_exprs(file, indent, local_var_prefix, phase, var, TRUE);
  3814. switch (typegen_detect_type(type, var->attrs, TDT_ALL_TYPES))
  3815. {
  3816. case TGT_CTXT_HANDLE:
  3817. case TGT_CTXT_HANDLE_POINTER:
  3818. if (phase == PHASE_MARSHAL)
  3819. {
  3820. if (pass == PASS_IN)
  3821. {
  3822. /* if the context_handle attribute appears in the chain of types
  3823. * without pointers being followed, then the context handle must
  3824. * be direct, otherwise it is a pointer */
  3825. const char *ch_ptr = is_aliaschain_attr(type, ATTR_CONTEXTHANDLE) ? "" : "*";
  3826. print_file(file, indent, "NdrClientContextMarshall(\n");
  3827. print_file(file, indent + 1, "&__frame->_StubMsg,\n");
  3828. print_file(file, indent + 1, "(NDR_CCONTEXT)%s%s%s,\n", ch_ptr, local_var_prefix,
  3829. var->name);
  3830. print_file(file, indent + 1, "%s);\n", in_attr && out_attr ? "1" : "0");
  3831. }
  3832. else
  3833. {
  3834. print_file(file, indent, "NdrServerContextNewMarshall(\n");
  3835. print_file(file, indent + 1, "&__frame->_StubMsg,\n");
  3836. print_file(file, indent + 1, "(NDR_SCONTEXT)%s%s,\n", local_var_prefix, var->name);
  3837. print_file(file, indent + 1, "(NDR_RUNDOWN)%s_rundown,\n", get_context_handle_type_name(var->declspec.type));
  3838. print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
  3839. }
  3840. }
  3841. else if (phase == PHASE_UNMARSHAL)
  3842. {
  3843. if (pass == PASS_OUT || pass == PASS_RETURN)
  3844. {
  3845. if (!in_attr)
  3846. print_file(file, indent, "*%s%s = 0;\n", local_var_prefix, var->name);
  3847. print_file(file, indent, "NdrClientContextUnmarshall(\n");
  3848. print_file(file, indent + 1, "&__frame->_StubMsg,\n");
  3849. print_file(file, indent + 1, "(NDR_CCONTEXT *)%s%s%s,\n",
  3850. pass == PASS_RETURN ? "&" : "", local_var_prefix, var->name);
  3851. print_file(file, indent + 1, "__frame->_Handle);\n");
  3852. }
  3853. else
  3854. {
  3855. print_file(file, indent, "%s%s = NdrServerContextNewUnmarshall(\n", local_var_prefix, var->name);
  3856. print_file(file, indent + 1, "&__frame->_StubMsg,\n");
  3857. print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n", start_offset);
  3858. }
  3859. }
  3860. break;
  3861. case TGT_USER_TYPE:
  3862. print_phase_function(file, indent, "UserMarshal", local_var_prefix, phase, var, start_offset);
  3863. break;
  3864. case TGT_STRING:
  3865. if (phase == PHASE_FREE || pass == PASS_RETURN ||
  3866. pointer_type != FC_RP)
  3867. {
  3868. /* strings returned are assumed to be global and hence don't
  3869. * need freeing */
  3870. if (is_declptr(type) && !(phase == PHASE_FREE && pass == PASS_RETURN))
  3871. print_phase_function(file, indent, "Pointer", local_var_prefix,
  3872. phase, var, start_offset);
  3873. else if (pointer_type == FC_RP && phase == PHASE_FREE &&
  3874. !in_attr && is_conformant_array(type))
  3875. {
  3876. print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
  3877. indent++;
  3878. print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
  3879. }
  3880. }
  3881. else
  3882. {
  3883. unsigned int real_start_offset = start_offset;
  3884. /* skip over pointer description straight to string description */
  3885. if (is_declptr(type))
  3886. {
  3887. if (is_conformant_array(type))
  3888. real_start_offset += 4;
  3889. else
  3890. real_start_offset += 2;
  3891. }
  3892. if (is_array(type) && !is_conformant_array(type))
  3893. print_phase_function(file, indent, "NonConformantString",
  3894. local_var_prefix, phase, var,
  3895. real_start_offset);
  3896. else
  3897. print_phase_function(file, indent, "ConformantString", local_var_prefix,
  3898. phase, var, real_start_offset);
  3899. }
  3900. break;
  3901. case TGT_ARRAY:
  3902. {
  3903. unsigned char tc = get_array_fc(type);
  3904. const char *array_type = NULL;
  3905. /* We already have the size_is expression since it's at the
  3906. top level, but do checks for multidimensional conformant
  3907. arrays. When we handle them, we'll need to extend this
  3908. function to return a list, and then we'll actually use
  3909. the return value. */
  3910. get_size_is_expr(type, var->name);
  3911. switch (tc)
  3912. {
  3913. case FC_SMFARRAY:
  3914. case FC_LGFARRAY:
  3915. array_type = "FixedArray";
  3916. break;
  3917. case FC_SMVARRAY:
  3918. case FC_LGVARRAY:
  3919. array_type = "VaryingArray";
  3920. break;
  3921. case FC_CARRAY:
  3922. array_type = "ConformantArray";
  3923. break;
  3924. case FC_CVARRAY:
  3925. array_type = "ConformantVaryingArray";
  3926. break;
  3927. case FC_BOGUS_ARRAY:
  3928. array_type = "ComplexArray";
  3929. break;
  3930. }
  3931. if (pointer_type != FC_RP) array_type = "Pointer";
  3932. if (phase == PHASE_FREE && pointer_type == FC_RP)
  3933. {
  3934. /* these are all unmarshalled by allocating memory */
  3935. if (tc == FC_BOGUS_ARRAY ||
  3936. tc == FC_CVARRAY ||
  3937. ((tc == FC_SMVARRAY || tc == FC_LGVARRAY) && in_attr) ||
  3938. (tc == FC_CARRAY && !in_attr))
  3939. {
  3940. if (type_array_is_decl_as_ptr(type) && type_array_get_ptr_tfsoff(type))
  3941. {
  3942. print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var,
  3943. type_array_get_ptr_tfsoff(type));
  3944. break;
  3945. }
  3946. print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
  3947. print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
  3948. indent++;
  3949. print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
  3950. break;
  3951. }
  3952. }
  3953. print_phase_function(file, indent, array_type, local_var_prefix, phase, var, start_offset);
  3954. break;
  3955. }
  3956. case TGT_BASIC:
  3957. print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
  3958. break;
  3959. case TGT_ENUM:
  3960. print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
  3961. break;
  3962. case TGT_RANGE:
  3963. print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
  3964. /* Note: this goes beyond what MIDL does - it only supports arguments
  3965. * with the [range] attribute in Oicf mode */
  3966. if (phase == PHASE_UNMARSHAL)
  3967. {
  3968. const expr_t *range_min;
  3969. const expr_t *range_max;
  3970. expr_list_t *range_list = get_attrp(var->attrs, ATTR_RANGE);
  3971. if (!range_list)
  3972. range_list = get_aliaschain_attrp(type, ATTR_RANGE);
  3973. range_min = LIST_ENTRY(list_head(range_list), const expr_t, entry);
  3974. range_max = LIST_ENTRY(list_next(range_list, list_head(range_list)), const expr_t, entry);
  3975. print_file(file, indent, "if ((%s%s < (", local_var_prefix, var->name);
  3976. write_type_decl(file, &var->declspec, NULL);
  3977. fprintf(file, ")0x%x) || (%s%s > (", range_min->cval, local_var_prefix, var->name);
  3978. write_type_decl(file, &var->declspec, NULL);
  3979. fprintf(file, ")0x%x))\n", range_max->cval);
  3980. print_file(file, indent, "{\n");
  3981. print_file(file, indent+1, "RpcRaiseException(RPC_S_INVALID_BOUND);\n");
  3982. print_file(file, indent, "}\n");
  3983. }
  3984. break;
  3985. case TGT_STRUCT:
  3986. switch (get_struct_fc(type))
  3987. {
  3988. case FC_STRUCT:
  3989. if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
  3990. print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
  3991. break;
  3992. case FC_PSTRUCT:
  3993. print_phase_function(file, indent, "SimpleStruct", local_var_prefix, phase, var, start_offset);
  3994. break;
  3995. case FC_CSTRUCT:
  3996. case FC_CPSTRUCT:
  3997. print_phase_function(file, indent, "ConformantStruct", local_var_prefix, phase, var, start_offset);
  3998. break;
  3999. case FC_CVSTRUCT:
  4000. print_phase_function(file, indent, "ConformantVaryingStruct", local_var_prefix, phase, var, start_offset);
  4001. break;
  4002. case FC_BOGUS_STRUCT:
  4003. print_phase_function(file, indent, "ComplexStruct", local_var_prefix, phase, var, start_offset);
  4004. break;
  4005. default:
  4006. error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(type));
  4007. }
  4008. break;
  4009. case TGT_UNION:
  4010. {
  4011. const char *union_type = NULL;
  4012. if (type_get_type(type) == TYPE_UNION)
  4013. union_type = "NonEncapsulatedUnion";
  4014. else if (type_get_type(type) == TYPE_ENCAPSULATED_UNION)
  4015. union_type = "EncapsulatedUnion";
  4016. print_phase_function(file, indent, union_type, local_var_prefix,
  4017. phase, var, start_offset);
  4018. break;
  4019. }
  4020. case TGT_POINTER:
  4021. {
  4022. const type_t *ref = type_pointer_get_ref_type(type);
  4023. if (pointer_type == FC_RP) switch (typegen_detect_type(ref, NULL, TDT_ALL_TYPES))
  4024. {
  4025. case TGT_BASIC:
  4026. print_phase_basetype(file, indent, local_var_prefix, phase, pass, var, var->name);
  4027. break;
  4028. case TGT_ENUM:
  4029. /* base types have known sizes, so don't need a sizing pass
  4030. * and don't have any memory to free and so don't need a
  4031. * freeing pass */
  4032. if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
  4033. print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
  4034. break;
  4035. case TGT_STRUCT:
  4036. switch (get_struct_fc(ref))
  4037. {
  4038. case FC_STRUCT:
  4039. /* simple structs have known sizes, so don't need a sizing
  4040. * pass and don't have any memory to free and so don't
  4041. * need a freeing pass */
  4042. if (phase == PHASE_MARSHAL || phase == PHASE_UNMARSHAL)
  4043. type_str = "SimpleStruct";
  4044. else if (phase == PHASE_FREE && pass == PASS_RETURN)
  4045. {
  4046. print_file(file, indent, "if (%s%s)\n", local_var_prefix, var->name);
  4047. indent++;
  4048. print_file(file, indent, "__frame->_StubMsg.pfnFree((void*)%s%s);\n", local_var_prefix, var->name);
  4049. indent--;
  4050. }
  4051. break;
  4052. case FC_PSTRUCT:
  4053. type_str = "SimpleStruct";
  4054. break;
  4055. case FC_CSTRUCT:
  4056. case FC_CPSTRUCT:
  4057. type_str = "ConformantStruct";
  4058. break;
  4059. case FC_CVSTRUCT:
  4060. type_str = "ConformantVaryingStruct";
  4061. break;
  4062. case FC_BOGUS_STRUCT:
  4063. type_str = "ComplexStruct";
  4064. break;
  4065. default:
  4066. error("write_remoting_arguments: Unsupported type: %s (0x%02x)\n", var->name, get_struct_fc(ref));
  4067. }
  4068. if (type_str)
  4069. {
  4070. if (phase == PHASE_FREE)
  4071. type_str = "Pointer";
  4072. else
  4073. start_offset = ref->typestring_offset;
  4074. print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
  4075. }
  4076. break;
  4077. case TGT_UNION:
  4078. if (phase == PHASE_FREE)
  4079. type_str = "Pointer";
  4080. else
  4081. {
  4082. if (type_get_type(ref) == TYPE_UNION)
  4083. type_str = "NonEncapsulatedUnion";
  4084. else if (type_get_type(ref) == TYPE_ENCAPSULATED_UNION)
  4085. type_str = "EncapsulatedUnion";
  4086. start_offset = ref->typestring_offset;
  4087. }
  4088. print_phase_function(file, indent, type_str, local_var_prefix,
  4089. phase, var, start_offset);
  4090. break;
  4091. case TGT_USER_TYPE:
  4092. if (phase != PHASE_FREE)
  4093. {
  4094. type_str = "UserMarshal";
  4095. start_offset = ref->typestring_offset;
  4096. }
  4097. else type_str = "Pointer";
  4098. print_phase_function(file, indent, type_str, local_var_prefix, phase, var, start_offset);
  4099. break;
  4100. case TGT_STRING:
  4101. case TGT_POINTER:
  4102. case TGT_ARRAY:
  4103. case TGT_RANGE:
  4104. case TGT_IFACE_POINTER:
  4105. case TGT_CTXT_HANDLE:
  4106. case TGT_CTXT_HANDLE_POINTER:
  4107. print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
  4108. break;
  4109. case TGT_INVALID:
  4110. assert(0);
  4111. break;
  4112. }
  4113. else
  4114. print_phase_function(file, indent, "Pointer", local_var_prefix, phase, var, start_offset);
  4115. break;
  4116. }
  4117. case TGT_IFACE_POINTER:
  4118. print_phase_function(file, indent, "InterfacePointer", local_var_prefix, phase, var, start_offset);
  4119. break;
  4120. case TGT_INVALID:
  4121. assert(0);
  4122. break;
  4123. }
  4124. fprintf(file, "\n");
  4125. }
  4126. void write_remoting_arguments(FILE *file, int indent, const var_t *func, const char *local_var_prefix,
  4127. enum pass pass, enum remoting_phase phase)
  4128. {
  4129. if (phase == PHASE_BUFFERSIZE && pass != PASS_RETURN)
  4130. {
  4131. unsigned int size = get_function_buffer_size( func, pass );
  4132. print_file(file, indent, "__frame->_StubMsg.BufferLength = %u;\n", size);
  4133. }
  4134. if (pass == PASS_RETURN)
  4135. {
  4136. write_remoting_arg( file, indent, func, local_var_prefix, pass, phase,
  4137. type_function_get_retval(func->declspec.type) );
  4138. }
  4139. else
  4140. {
  4141. const var_t *var;
  4142. if (!type_function_get_args(func->declspec.type))
  4143. return;
  4144. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  4145. write_remoting_arg( file, indent, func, local_var_prefix, pass, phase, var );
  4146. }
  4147. }
  4148. unsigned int get_size_procformatstring_func(const type_t *iface, const var_t *func)
  4149. {
  4150. unsigned int offset = 0;
  4151. write_procformatstring_func( NULL, 0, iface, func, &offset, 0 );
  4152. return offset;
  4153. }
  4154. static void get_size_procformatstring_iface(type_t *iface, FILE *file, int indent, unsigned int *size)
  4155. {
  4156. const statement_t *stmt;
  4157. STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
  4158. {
  4159. const var_t *func = stmt->u.var;
  4160. if (!is_local(func->attrs))
  4161. *size += get_size_procformatstring_func( iface, func );
  4162. }
  4163. }
  4164. unsigned int get_size_procformatstring(const statement_list_t *stmts, type_pred_t pred)
  4165. {
  4166. unsigned int size = 1;
  4167. for_each_iface(stmts, get_size_procformatstring_iface, pred, NULL, 0, &size);
  4168. return size;
  4169. }
  4170. unsigned int get_size_typeformatstring(const statement_list_t *stmts, type_pred_t pred)
  4171. {
  4172. set_all_tfswrite(FALSE);
  4173. return process_tfs(NULL, stmts, pred);
  4174. }
  4175. void declare_stub_args( FILE *file, int indent, const var_t *func )
  4176. {
  4177. int in_attr, out_attr;
  4178. int i = 0;
  4179. var_t *var = type_function_get_retval(func->declspec.type);
  4180. /* declare return value */
  4181. if (!is_void(var->declspec.type))
  4182. {
  4183. if (is_context_handle(var->declspec.type))
  4184. print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
  4185. else
  4186. {
  4187. print_file(file, indent, "%s", "");
  4188. write_type_decl(file, &var->declspec, var->name);
  4189. fprintf(file, ";\n");
  4190. }
  4191. }
  4192. if (!type_function_get_args(func->declspec.type))
  4193. return;
  4194. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), var_t, entry )
  4195. {
  4196. in_attr = is_attr(var->attrs, ATTR_IN);
  4197. out_attr = is_attr(var->attrs, ATTR_OUT);
  4198. if (!out_attr && !in_attr)
  4199. in_attr = 1;
  4200. if (is_context_handle(var->declspec.type))
  4201. print_file(file, indent, "NDR_SCONTEXT %s;\n", var->name);
  4202. else
  4203. {
  4204. if (!in_attr && !is_conformant_array(var->declspec.type))
  4205. {
  4206. const decl_spec_t *type_to_print;
  4207. char name[16];
  4208. print_file(file, indent, "%s", "");
  4209. if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
  4210. !type_array_is_decl_as_ptr(var->declspec.type))
  4211. type_to_print = &var->declspec;
  4212. else
  4213. type_to_print = type_pointer_get_ref(var->declspec.type);
  4214. sprintf(name, "_W%u", i++);
  4215. write_type_decl(file, type_to_print, name);
  4216. fprintf(file, ";\n");
  4217. }
  4218. print_file(file, indent, "%s", "");
  4219. write_type_decl_left(file, &var->declspec);
  4220. fprintf(file, " ");
  4221. if (type_get_type(var->declspec.type) == TYPE_ARRAY &&
  4222. !type_array_is_decl_as_ptr(var->declspec.type)) {
  4223. fprintf(file, "(*%s)", var->name);
  4224. } else
  4225. fprintf(file, "%s", var->name);
  4226. write_type_right(file, var->declspec.type, FALSE);
  4227. fprintf(file, ";\n");
  4228. if (decl_indirect(var->declspec.type))
  4229. print_file(file, indent, "void *_p_%s;\n", var->name);
  4230. }
  4231. }
  4232. }
  4233. void assign_stub_out_args( FILE *file, int indent, const var_t *func, const char *local_var_prefix )
  4234. {
  4235. int in_attr, out_attr;
  4236. int i = 0, sep = 0;
  4237. const var_t *var;
  4238. type_t *ref;
  4239. if (!type_function_get_args(func->declspec.type))
  4240. return;
  4241. LIST_FOR_EACH_ENTRY( var, type_function_get_args(func->declspec.type), const var_t, entry )
  4242. {
  4243. in_attr = is_attr(var->attrs, ATTR_IN);
  4244. out_attr = is_attr(var->attrs, ATTR_OUT);
  4245. if (!out_attr && !in_attr)
  4246. in_attr = 1;
  4247. if (!in_attr)
  4248. {
  4249. print_file(file, indent, "%s%s", local_var_prefix, var->name);
  4250. switch (typegen_detect_type(var->declspec.type, var->attrs, TDT_IGNORE_STRINGS))
  4251. {
  4252. case TGT_CTXT_HANDLE_POINTER:
  4253. fprintf(file, " = NdrContextHandleInitialize(\n");
  4254. print_file(file, indent + 1, "&__frame->_StubMsg,\n");
  4255. print_file(file, indent + 1, "(PFORMAT_STRING)&__MIDL_TypeFormatString.Format[%d]);\n",
  4256. var->typestring_offset);
  4257. break;
  4258. case TGT_ARRAY:
  4259. if (type_array_has_conformance(var->declspec.type))
  4260. {
  4261. unsigned int size;
  4262. type_t *type;
  4263. fprintf(file, " = NdrAllocate(&__frame->_StubMsg, ");
  4264. for (type = var->declspec.type;
  4265. is_array(type) && type_array_has_conformance(type);
  4266. type = type_array_get_element_type(type))
  4267. {
  4268. write_expr(file, type_array_get_conformance(type), TRUE,
  4269. TRUE, NULL, NULL, local_var_prefix);
  4270. fprintf(file, " * ");
  4271. }
  4272. size = type_memsize(type);
  4273. fprintf(file, "%u);\n", size);
  4274. print_file(file, indent, "memset(%s%s, 0, ", local_var_prefix, var->name);
  4275. for (type = var->declspec.type;
  4276. is_array(type) && type_array_has_conformance(type);
  4277. type = type_array_get_element_type(type))
  4278. {
  4279. write_expr(file, type_array_get_conformance(type), TRUE,
  4280. TRUE, NULL, NULL, local_var_prefix);
  4281. fprintf(file, " * ");
  4282. }
  4283. size = type_memsize(type);
  4284. fprintf(file, "%u);\n", size);
  4285. }
  4286. else
  4287. fprintf(file, " = &%s_W%u;\n", local_var_prefix, i++);
  4288. break;
  4289. case TGT_POINTER:
  4290. fprintf(file, " = &%s_W%u;\n", local_var_prefix, i);
  4291. ref = type_pointer_get_ref_type(var->declspec.type);
  4292. switch (typegen_detect_type(ref, var->attrs, TDT_IGNORE_STRINGS))
  4293. {
  4294. case TGT_BASIC:
  4295. case TGT_ENUM:
  4296. case TGT_POINTER:
  4297. case TGT_RANGE:
  4298. case TGT_IFACE_POINTER:
  4299. print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
  4300. break;
  4301. case TGT_USER_TYPE:
  4302. print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
  4303. local_var_prefix, i, local_var_prefix, i);
  4304. break;
  4305. case TGT_ARRAY:
  4306. if (type_array_is_decl_as_ptr(ref))
  4307. {
  4308. print_file(file, indent, "%s_W%u = 0;\n", local_var_prefix, i);
  4309. break;
  4310. }
  4311. ref = type_array_get_element_type(ref);
  4312. /* fall through */
  4313. case TGT_STRUCT:
  4314. case TGT_UNION:
  4315. if (type_has_pointers(ref))
  4316. print_file(file, indent, "memset(&%s_W%u, 0, sizeof(%s_W%u));\n",
  4317. local_var_prefix, i, local_var_prefix, i);
  4318. break;
  4319. case TGT_CTXT_HANDLE:
  4320. case TGT_CTXT_HANDLE_POINTER:
  4321. case TGT_INVALID:
  4322. case TGT_STRING:
  4323. /* not initialised */
  4324. break;
  4325. }
  4326. i++;
  4327. break;
  4328. default:
  4329. break;
  4330. }
  4331. sep = 1;
  4332. }
  4333. }
  4334. if (sep)
  4335. fprintf(file, "\n");
  4336. }
  4337. void write_func_param_struct( FILE *file, const type_t *iface, const type_t *func,
  4338. const char *var_decl, int add_retval )
  4339. {
  4340. var_t *retval = type_function_get_retval( func );
  4341. const var_list_t *args = type_function_get_args( func );
  4342. const var_t *arg;
  4343. int needs_packing;
  4344. unsigned int align = 0;
  4345. if (args)
  4346. LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
  4347. if (!is_array( arg->declspec.type )) type_memsize_and_alignment( arg->declspec.type, &align );
  4348. needs_packing = (align > pointer_size);
  4349. if (needs_packing) print_file( file, 0, "#include <pshpack%u.h>\n", pointer_size );
  4350. print_file(file, 1, "struct _PARAM_STRUCT\n" );
  4351. print_file(file, 1, "{\n" );
  4352. if (is_object( iface )) print_file(file, 2, "%s *This;\n", iface->name );
  4353. if (args) LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
  4354. {
  4355. print_file(file, 2, "%s", "");
  4356. write_type_left( file, &arg->declspec, NAME_DEFAULT, TRUE, TRUE );
  4357. if (needs_space_after( arg->declspec.type )) fputc( ' ', file );
  4358. if (is_array( arg->declspec.type ) && !type_array_is_decl_as_ptr( arg->declspec.type )) fputc( '*', file );
  4359. /* FIXME: should check for large args being passed by pointer */
  4360. align = 0;
  4361. if (is_array( arg->declspec.type ) || is_ptr( arg->declspec.type )) align = pointer_size;
  4362. else type_memsize_and_alignment( arg->declspec.type, &align );
  4363. if (align < pointer_size)
  4364. fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
  4365. fprintf( file, "%s;\n", arg->name );
  4366. }
  4367. if (add_retval && !is_void( retval->declspec.type ))
  4368. {
  4369. print_file(file, 2, "%s", "");
  4370. write_type_left( file, &retval->declspec, NAME_DEFAULT, TRUE, TRUE );
  4371. if (needs_space_after( retval->declspec.type )) fputc( ' ', file );
  4372. if (!is_array( retval->declspec.type ) && !is_ptr( retval->declspec.type ) &&
  4373. type_memsize( retval->declspec.type ) != pointer_size)
  4374. {
  4375. fprintf( file, "DECLSPEC_ALIGN(%u) ", pointer_size );
  4376. }
  4377. fprintf( file, "%s;\n", retval->name );
  4378. }
  4379. print_file(file, 1, "} %s;\n", var_decl );
  4380. if (needs_packing) print_file( file, 0, "#include <poppack.h>\n" );
  4381. print_file( file, 0, "\n" );
  4382. }
  4383. void write_pointer_checks( FILE *file, int indent, const var_t *func )
  4384. {
  4385. const var_list_t *args = type_function_get_args( func->declspec.type );
  4386. const var_t *var;
  4387. if (!args) return;
  4388. LIST_FOR_EACH_ENTRY( var, args, const var_t, entry )
  4389. if (cant_be_null( var ))
  4390. print_file( file, indent, "if (!%s) RpcRaiseException(RPC_X_NULL_REF_POINTER);\n", var->name );
  4391. }
  4392. int write_expr_eval_routines(FILE *file, const char *iface)
  4393. {
  4394. static const char *var_name = "pS";
  4395. static const char *var_name_expr = "pS->";
  4396. int result = 0;
  4397. struct expr_eval_routine *eval;
  4398. unsigned short callback_offset = 0;
  4399. LIST_FOR_EACH_ENTRY(eval, &expr_eval_routines, struct expr_eval_routine, entry)
  4400. {
  4401. const char *name = eval->name;
  4402. result = 1;
  4403. print_file(file, 0, "static void __RPC_USER %s_%sExprEval_%04u(PMIDL_STUB_MESSAGE pStubMsg)\n",
  4404. eval->iface ? eval->iface->name : iface, name, callback_offset);
  4405. print_file(file, 0, "{\n");
  4406. if (type_get_type( eval->cont_type ) == TYPE_FUNCTION)
  4407. {
  4408. write_func_param_struct( file, eval->iface, eval->cont_type,
  4409. "*pS = (struct _PARAM_STRUCT *)pStubMsg->StackTop", FALSE );
  4410. }
  4411. else
  4412. {
  4413. decl_spec_t ds = {.type = (type_t *)eval->cont_type};
  4414. print_file(file, 1, "%s", "");
  4415. write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
  4416. fprintf(file, " *%s = (", var_name);
  4417. write_type_left(file, &ds, NAME_DEFAULT, TRUE, TRUE);
  4418. fprintf(file, " *)(pStubMsg->StackTop - %u);\n", eval->baseoff);
  4419. }
  4420. print_file(file, 1, "pStubMsg->Offset = 0;\n"); /* FIXME */
  4421. print_file(file, 1, "pStubMsg->MaxCount = (ULONG_PTR)");
  4422. write_expr(file, eval->expr, 1, 1, var_name_expr, eval->cont_type, "");
  4423. fprintf(file, ";\n");
  4424. print_file(file, 0, "}\n\n");
  4425. callback_offset++;
  4426. }
  4427. return result;
  4428. }
  4429. void write_expr_eval_routine_list(FILE *file, const char *iface)
  4430. {
  4431. struct expr_eval_routine *eval;
  4432. struct expr_eval_routine *cursor;
  4433. unsigned short callback_offset = 0;
  4434. fprintf(file, "static const EXPR_EVAL ExprEvalRoutines[] =\n");
  4435. fprintf(file, "{\n");
  4436. LIST_FOR_EACH_ENTRY_SAFE(eval, cursor, &expr_eval_routines, struct expr_eval_routine, entry)
  4437. {
  4438. print_file(file, 1, "%s_%sExprEval_%04u,\n",
  4439. eval->iface ? eval->iface->name : iface, eval->name, callback_offset);
  4440. callback_offset++;
  4441. list_remove(&eval->entry);
  4442. free(eval->name);
  4443. free(eval);
  4444. }
  4445. fprintf(file, "};\n\n");
  4446. }
  4447. void write_user_quad_list(FILE *file)
  4448. {
  4449. user_type_t *ut;
  4450. if (list_empty(&user_type_list))
  4451. return;
  4452. fprintf(file, "static const USER_MARSHAL_ROUTINE_QUADRUPLE UserMarshalRoutines[] =\n");
  4453. fprintf(file, "{\n");
  4454. LIST_FOR_EACH_ENTRY(ut, &user_type_list, user_type_t, entry)
  4455. {
  4456. const char *sep = &ut->entry == list_tail(&user_type_list) ? "" : ",";
  4457. print_file(file, 1, "{\n");
  4458. print_file(file, 2, "(USER_MARSHAL_SIZING_ROUTINE)%s_UserSize,\n", ut->name);
  4459. print_file(file, 2, "(USER_MARSHAL_MARSHALLING_ROUTINE)%s_UserMarshal,\n", ut->name);
  4460. print_file(file, 2, "(USER_MARSHAL_UNMARSHALLING_ROUTINE)%s_UserUnmarshal,\n", ut->name);
  4461. print_file(file, 2, "(USER_MARSHAL_FREEING_ROUTINE)%s_UserFree\n", ut->name);
  4462. print_file(file, 1, "}%s\n", sep);
  4463. }
  4464. fprintf(file, "};\n\n");
  4465. }
  4466. void write_endpoints( FILE *f, const char *prefix, const str_list_t *list )
  4467. {
  4468. const struct str_list_entry_t *endpoint;
  4469. const char *p;
  4470. /* this should be an array of RPC_PROTSEQ_ENDPOINT but we want const strings */
  4471. print_file( f, 0, "static const unsigned char * const %s__RpcProtseqEndpoint[][2] =\n{\n", prefix );
  4472. LIST_FOR_EACH_ENTRY( endpoint, list, const struct str_list_entry_t, entry )
  4473. {
  4474. print_file( f, 1, "{ (const unsigned char *)\"" );
  4475. for (p = endpoint->str; *p && *p != ':'; p++)
  4476. {
  4477. if (*p == '"' || *p == '\\') fputc( '\\', f );
  4478. fputc( *p, f );
  4479. }
  4480. if (!*p) goto error;
  4481. if (p[1] != '[') goto error;
  4482. fprintf( f, "\", (const unsigned char *)\"" );
  4483. for (p += 2; *p && *p != ']'; p++)
  4484. {
  4485. if (*p == '"' || *p == '\\') fputc( '\\', f );
  4486. fputc( *p, f );
  4487. }
  4488. if (*p != ']') goto error;
  4489. fprintf( f, "\" },\n" );
  4490. }
  4491. print_file( f, 0, "};\n\n" );
  4492. return;
  4493. error:
  4494. error("Invalid endpoint syntax '%s'\n", endpoint->str);
  4495. }
  4496. void write_client_call_routine( FILE *file, const type_t *iface, const var_t *func,
  4497. const char *prefix, unsigned int proc_offset )
  4498. {
  4499. const decl_spec_t *rettype = type_function_get_ret( func->declspec.type );
  4500. int has_ret = !is_void( rettype->type );
  4501. const var_list_t *args = type_function_get_args( func->declspec.type );
  4502. const var_t *arg;
  4503. int len, needs_params = 0;
  4504. /* we need a param structure if we have more than one arg */
  4505. if (pointer_size == 4 && args) needs_params = is_object( iface ) || list_count( args ) > 1;
  4506. print_file( file, 0, "{\n");
  4507. if (needs_params)
  4508. {
  4509. if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n" );
  4510. write_func_param_struct( file, iface, func->declspec.type, "__params", FALSE );
  4511. if (is_object( iface )) print_file( file, 1, "__params.This = This;\n" );
  4512. if (args)
  4513. LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
  4514. print_file( file, 1, "__params.%s = %s;\n", arg->name, arg->name );
  4515. }
  4516. else if (has_ret) print_file( file, 1, "%s", "CLIENT_CALL_RETURN _RetVal;\n\n" );
  4517. len = fprintf( file, " %s%s( ",
  4518. has_ret ? "_RetVal = " : "",
  4519. get_stub_mode() == MODE_Oif ? "NdrClientCall2" : "NdrClientCall" );
  4520. fprintf( file, "&%s_StubDesc,", prefix );
  4521. fprintf( file, "\n%*s&__MIDL_ProcFormatString.Format[%u]", len, "", proc_offset );
  4522. if (needs_params)
  4523. {
  4524. fprintf( file, ",\n%*s&__params", len, "" );
  4525. }
  4526. else if (pointer_size == 8)
  4527. {
  4528. if (is_object( iface )) fprintf( file, ",\n%*sThis", len, "" );
  4529. if (args)
  4530. LIST_FOR_EACH_ENTRY( arg, args, const var_t, entry )
  4531. fprintf( file, ",\n%*s%s", len, "", arg->name );
  4532. }
  4533. else
  4534. {
  4535. if (is_object( iface )) fprintf( file, ",\n%*s&This", len, "" );
  4536. else if (args)
  4537. {
  4538. arg = LIST_ENTRY( list_head(args), const var_t, entry );
  4539. fprintf( file, ",\n%*s&%s", len, "", arg->name );
  4540. }
  4541. }
  4542. fprintf( file, " );\n" );
  4543. if (has_ret)
  4544. {
  4545. print_file( file, 1, "return (" );
  4546. write_type_decl_left(file, rettype);
  4547. fprintf( file, ")%s;\n", pointer_size == 8 ? "_RetVal.Simple" : "*(LONG_PTR *)&_RetVal" );
  4548. }
  4549. print_file( file, 0, "}\n\n");
  4550. }
  4551. void write_exceptions( FILE *file )
  4552. {
  4553. fprintf( file, "#ifndef USE_COMPILER_EXCEPTIONS\n");
  4554. fprintf( file, "\n");
  4555. fprintf( file, "#include \"wine/exception.h\"\n");
  4556. fprintf( file, "#undef RpcTryExcept\n");
  4557. fprintf( file, "#undef RpcExcept\n");
  4558. fprintf( file, "#undef RpcEndExcept\n");
  4559. fprintf( file, "#undef RpcTryFinally\n");
  4560. fprintf( file, "#undef RpcFinally\n");
  4561. fprintf( file, "#undef RpcEndFinally\n");
  4562. fprintf( file, "#undef RpcExceptionCode\n");
  4563. fprintf( file, "#undef RpcAbnormalTermination\n");
  4564. fprintf( file, "\n");
  4565. fprintf( file, "struct __exception_frame;\n");
  4566. fprintf( file, "typedef int (*__filter_func)(struct __exception_frame *);\n");
  4567. fprintf( file, "typedef void (*__finally_func)(struct __exception_frame *);\n");
  4568. fprintf( file, "\n");
  4569. fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
  4570. fprintf( file, " EXCEPTION_REGISTRATION_RECORD frame; \\\n");
  4571. fprintf( file, " __filter_func filter; \\\n");
  4572. fprintf( file, " __finally_func finally; \\\n");
  4573. fprintf( file, " __wine_jmp_buf jmp; \\\n");
  4574. fprintf( file, " DWORD code; \\\n");
  4575. fprintf( file, " unsigned char abnormal_termination; \\\n");
  4576. fprintf( file, " unsigned char filter_level; \\\n");
  4577. fprintf( file, " unsigned char finally_level;\n");
  4578. fprintf( file, "\n");
  4579. fprintf( file, "struct __exception_frame\n{\n");
  4580. fprintf( file, " __DECL_EXCEPTION_FRAME\n");
  4581. fprintf( file, "};\n");
  4582. fprintf( file, "\n");
  4583. fprintf( file, "static inline void __widl_unwind_target(void)\n" );
  4584. fprintf( file, "{\n");
  4585. fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)__wine_get_frame();\n" );
  4586. fprintf( file, " if (exc_frame->finally_level > exc_frame->filter_level)\n" );
  4587. fprintf( file, " {\n");
  4588. fprintf( file, " exc_frame->abnormal_termination = 1;\n");
  4589. fprintf( file, " exc_frame->finally( exc_frame );\n");
  4590. fprintf( file, " __wine_pop_frame( &exc_frame->frame );\n");
  4591. fprintf( file, " }\n");
  4592. fprintf( file, " exc_frame->filter_level = 0;\n");
  4593. fprintf( file, " __wine_longjmp( &exc_frame->jmp, 1 );\n");
  4594. fprintf( file, "}\n");
  4595. fprintf( file, "\n");
  4596. fprintf( file, "static DWORD __cdecl __widl_exception_handler( EXCEPTION_RECORD *record,\n");
  4597. fprintf( file, " EXCEPTION_REGISTRATION_RECORD *frame,\n");
  4598. fprintf( file, " CONTEXT *context,\n");
  4599. fprintf( file, " EXCEPTION_REGISTRATION_RECORD **pdispatcher )\n");
  4600. fprintf( file, "{\n");
  4601. fprintf( file, " struct __exception_frame *exc_frame = (struct __exception_frame *)frame;\n");
  4602. fprintf( file, "\n");
  4603. fprintf( file, " if (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND | EH_NESTED_CALL))\n");
  4604. fprintf( file, " {\n" );
  4605. fprintf( file, " if (exc_frame->finally_level && (record->ExceptionFlags & (EH_UNWINDING | EH_EXIT_UNWIND)))\n");
  4606. fprintf( file, " {\n" );
  4607. fprintf( file, " exc_frame->abnormal_termination = 1;\n");
  4608. fprintf( file, " exc_frame->finally( exc_frame );\n");
  4609. fprintf( file, " }\n" );
  4610. fprintf( file, " return ExceptionContinueSearch;\n");
  4611. fprintf( file, " }\n" );
  4612. fprintf( file, " exc_frame->code = record->ExceptionCode;\n");
  4613. fprintf( file, " if (exc_frame->filter_level && exc_frame->filter( exc_frame ) == EXCEPTION_EXECUTE_HANDLER)\n" );
  4614. fprintf( file, " __wine_rtl_unwind( frame, record, __widl_unwind_target );\n");
  4615. fprintf( file, " return ExceptionContinueSearch;\n");
  4616. fprintf( file, "}\n");
  4617. fprintf( file, "\n");
  4618. fprintf( file, "#define RpcTryExcept \\\n");
  4619. fprintf( file, " if (!__wine_setjmpex( &__frame->jmp, &__frame->frame )) \\\n");
  4620. fprintf( file, " { \\\n");
  4621. fprintf( file, " if (!__frame->finally_level) \\\n" );
  4622. fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
  4623. fprintf( file, " __frame->filter_level = __frame->finally_level + 1;\n" );
  4624. fprintf( file, "\n");
  4625. fprintf( file, "#define RpcExcept(expr) \\\n");
  4626. fprintf( file, " if (!__frame->finally_level) \\\n" );
  4627. fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
  4628. fprintf( file, " __frame->filter_level = 0; \\\n" );
  4629. fprintf( file, " } \\\n");
  4630. fprintf( file, " else \\\n");
  4631. fprintf( file, "\n");
  4632. fprintf( file, "#define RpcEndExcept\n");
  4633. fprintf( file, "\n");
  4634. fprintf( file, "#define RpcExceptionCode() (__frame->code)\n");
  4635. fprintf( file, "\n");
  4636. fprintf( file, "#define RpcTryFinally \\\n");
  4637. fprintf( file, " if (!__frame->filter_level) \\\n");
  4638. fprintf( file, " __wine_push_frame( &__frame->frame ); \\\n");
  4639. fprintf( file, " __frame->finally_level = __frame->filter_level + 1;\n");
  4640. fprintf( file, "\n");
  4641. fprintf( file, "#define RpcFinally \\\n");
  4642. fprintf( file, " if (!__frame->filter_level) \\\n");
  4643. fprintf( file, " __wine_pop_frame( &__frame->frame ); \\\n");
  4644. fprintf( file, " __frame->finally_level = 0;\n");
  4645. fprintf( file, "\n");
  4646. fprintf( file, "#define RpcEndFinally\n");
  4647. fprintf( file, "\n");
  4648. fprintf( file, "#define RpcAbnormalTermination() (__frame->abnormal_termination)\n");
  4649. fprintf( file, "\n");
  4650. fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
  4651. fprintf( file, " do { \\\n");
  4652. fprintf( file, " __frame->frame.Handler = __widl_exception_handler; \\\n");
  4653. fprintf( file, " __frame->filter = (__filter_func)(filter_func); \\\n" );
  4654. fprintf( file, " __frame->finally = (__finally_func)(finally_func); \\\n");
  4655. fprintf( file, " __frame->abnormal_termination = 0; \\\n");
  4656. fprintf( file, " __frame->filter_level = 0; \\\n");
  4657. fprintf( file, " __frame->finally_level = 0; \\\n");
  4658. fprintf( file, " } while (0)\n");
  4659. fprintf( file, "\n");
  4660. fprintf( file, "#else /* USE_COMPILER_EXCEPTIONS */\n");
  4661. fprintf( file, "\n");
  4662. fprintf( file, "#define RpcExceptionInit(filter_func,finally_func) \\\n");
  4663. fprintf( file, " do { (void)(filter_func); } while(0)\n");
  4664. fprintf( file, "\n");
  4665. fprintf( file, "#define __DECL_EXCEPTION_FRAME \\\n");
  4666. fprintf( file, " DWORD code;\n");
  4667. fprintf( file, "\n");
  4668. fprintf( file, "#endif /* USE_COMPILER_EXCEPTIONS */\n");
  4669. }