typegen.c 181 KB

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