typegen.c 181 KB

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