h2pas.y 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171
  1. %{
  2. program h2pas;
  3. (*
  4. Copyright (c) 1998-2000 by Florian Klaempfl
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************)
  17. {$message TODO: warning Unit types is only needed due to issue 7910}
  18. uses
  19. SysUtils,types, classes,
  20. h2poptions,scan,converu,h2plexlib,h2pyacclib;
  21. type
  22. YYSTYPE = presobject;
  23. const
  24. SHORT_STR = 'shortint';
  25. USHORT_STR = 'byte';
  26. //C++ SHORT types usually map to the small types
  27. SMALL_STR = 'smallint';
  28. USMALL_STR = 'word';
  29. INT_STR = 'longint';
  30. UINT_STR = 'dword';
  31. CHAR_STR = 'char';
  32. UCHAR_STR = USHORT_STR; { should we use byte or char for 'unsigned char' ?? }
  33. INT64_STR = 'int64';
  34. QWORD_STR = 'qword';
  35. FLOAT_STR = 'single';
  36. WCHAR_STR = 'widechar';
  37. {ctypes strings}
  38. const
  39. cint8_STR = 'cint8';
  40. cuint8_STR = 'cuint8';
  41. cchar_STR = 'cchar';
  42. cschar_STR = 'cschar';
  43. cuchar_STR = 'cuchar';
  44. cint16_STR = 'cint16';
  45. cuint16_STR = 'cuint16';
  46. cshort_STR = 'cshort';
  47. csshort_STR = 'csshort';
  48. cushort_STR = 'cushort';
  49. cint32_STR = 'cint32';
  50. cuint32_STR = 'cuint32';
  51. cint_STR = 'cint';
  52. csint_STR = 'csint';
  53. cuint_STR = 'cuint';
  54. csigned_STR = 'csigned';
  55. cunsigned_STR = 'cunsigned';
  56. cint64_STR = 'cint64';
  57. cuint64_STR = 'cuint64';
  58. clonglong_STR = 'clonglong';
  59. cslonglong_STR = 'cslonglong';
  60. culonglong_STR = 'culonglong';
  61. cbool_STR = 'cbool';
  62. clong_STR = 'clong';
  63. cslong_STR = 'cslong';
  64. culong_STR = 'culong';
  65. cfloat_STR = 'cfloat';
  66. cdouble_STR = 'cdouble';
  67. clongdouble_STR = 'clongdouble';
  68. const
  69. MAX_CTYPESARRAY = 25;
  70. CTypesArray : array [0..MAX_CTYPESARRAY] of string =
  71. (cint8_STR, cuint8_STR,
  72. cchar_STR, cschar_STR, cuchar_STR,
  73. cint16_STR, cuint16_STR,
  74. cshort_STR, csshort_STR, cushort_STR,
  75. csigned_STR, cunsigned_STR,
  76. cint32_STR, cuint32_STR, cint_STR,
  77. csint_STR, cuint_STR,
  78. cint64_STR, cuint64_STR,
  79. clonglong_STR, cslonglong_STR, culonglong_STR,
  80. cbool_STR,
  81. clong_STR, cslong_STR, culong_STR);
  82. var
  83. hp,ph : presobject;
  84. implemfile : text; (* file for implementation headers extern procs *)
  85. IsExtern : boolean;
  86. NeedEllipsisOverload : boolean;
  87. must_write_packed_field : boolean;
  88. tempfile : text;
  89. No_pop : boolean;
  90. s,TN,PN : String;
  91. pointerprefix: boolean;
  92. freedynlibproc,
  93. loaddynlibproc : tstringlist;
  94. (* $ define yydebug
  95. compile with -dYYDEBUG to get debugging info *)
  96. const
  97. (* number of a?b:c construction in one define *)
  98. if_nb : longint = 0;
  99. is_packed : boolean = false;
  100. is_procvar : boolean = false;
  101. var space_array : array [0..255] of byte;
  102. space_index : byte;
  103. { Used when PPointers is used - pointer type definitions }
  104. PTypeList : TStringList;
  105. procedure shift(space_number : byte);
  106. var
  107. i : byte;
  108. begin
  109. space_array[space_index]:=space_number;
  110. inc(space_index);
  111. for i:=1 to space_number do
  112. aktspace:=aktspace+' ';
  113. end;
  114. procedure popshift;
  115. begin
  116. dec(space_index);
  117. if space_index<0 then
  118. internalerror(20);
  119. delete(aktspace,1,space_array[space_index]);
  120. end;
  121. function str(i : longint) : string;
  122. var
  123. s : string;
  124. begin
  125. system.str(i,s);
  126. str:=s;
  127. end;
  128. function hexstr(i : cardinal) : string;
  129. const
  130. HexTbl : array[0..15] of char='0123456789ABCDEF';
  131. var
  132. str : string;
  133. begin
  134. str:='';
  135. while i<>0 do
  136. begin
  137. str:=hextbl[i and $F]+str;
  138. i:=i shr 4;
  139. end;
  140. if str='' then str:='0';
  141. hexstr:='$'+str;
  142. end;
  143. function uppercase(s : string) : string;
  144. var
  145. i : byte;
  146. begin
  147. for i:=1 to length(s) do
  148. s[i]:=UpCase(s[i]);
  149. uppercase:=s;
  150. end;
  151. procedure write_type_specifier(var outfile:text; p : presobject);forward;
  152. procedure write_p_a_def(var outfile:text; p,simple_type : presobject);forward;
  153. procedure write_ifexpr(var outfile:text; p : presobject);forward;
  154. procedure write_funexpr(var outfile:text; p : presobject);forward;
  155. procedure yymsg(const msg : string);
  156. begin
  157. writeln('line ',line_no,': ',msg);
  158. end;
  159. { This converts pascal reserved words to
  160. the correct syntax.
  161. }
  162. function FixId(const s:string):string;
  163. const
  164. maxtokens = 14;
  165. reservedid: array[1..maxtokens] of string[14] =
  166. (
  167. 'CLASS',
  168. 'DISPOSE',
  169. 'FUNCTION',
  170. 'FALSE',
  171. 'LABEL',
  172. 'NEW',
  173. 'PROPERTY',
  174. 'PROCEDURE',
  175. 'RECORD',
  176. 'REPEAT',
  177. 'STRING',
  178. 'TYPE',
  179. 'TRUE',
  180. 'UNTIL'
  181. );
  182. var
  183. b : boolean;
  184. up : string;
  185. i: integer;
  186. begin
  187. if s='' then
  188. begin
  189. FixId:='';
  190. exit;
  191. end;
  192. b:=false;
  193. up:=Uppercase(s);
  194. for i:=1 to maxtokens do
  195. begin
  196. if up=reservedid[i] then
  197. begin
  198. b:=true;
  199. break;
  200. end;
  201. end;
  202. if b then
  203. FixId:='_'+s
  204. else
  205. FixId:=s;
  206. end;
  207. function TypeName(const s:string):string;
  208. var
  209. i : longint;
  210. begin
  211. i:=1;
  212. if RemoveUnderScore and (length(s)>1) and (s[1]='_') then
  213. i:=2;
  214. if PrependTypes then
  215. TypeName:='T'+Copy(s,i,255)
  216. else
  217. TypeName:=Copy(s,i,255);
  218. end;
  219. function IsACType(const s : String) : Boolean;
  220. var i : Integer;
  221. begin
  222. IsACType := True;
  223. for i := 0 to MAX_CTYPESARRAY do
  224. begin
  225. if s = CTypesArray[i] then
  226. begin
  227. Exit;
  228. end;
  229. end;
  230. IsACType := False;
  231. end;
  232. function PointerName(const s:string):string;
  233. var
  234. i : longint;
  235. begin
  236. if UseCTypesUnit then
  237. begin
  238. if IsACType(s) then
  239. begin
  240. PointerName := 'p'+s;
  241. exit;
  242. end;
  243. end;
  244. i:=1;
  245. if RemoveUnderScore and (length(s)>1) and (s[1]='_') then
  246. i:=2;
  247. if UsePPointers then
  248. begin
  249. PointerName:='P'+Copy(s,i,255);
  250. PTypeList.Add(PointerName);
  251. end
  252. else
  253. PointerName:=Copy(s,i,255);
  254. if PointerPrefix then
  255. PTypeList.Add('P'+s);
  256. end;
  257. procedure write_packed_fields_info(var outfile:text; p : presobject; ph : string);
  258. var
  259. hp1,hp2,hp3 : presobject;
  260. is_sized : boolean;
  261. line : string;
  262. flag_index : longint;
  263. name : pchar;
  264. ps : byte;
  265. begin
  266. { write out the tempfile created }
  267. close(tempfile);
  268. reset(tempfile);
  269. is_sized:=false;
  270. flag_index:=0;
  271. writeln(outfile);
  272. writeln(outfile,aktspace,'const');
  273. shift(2);
  274. while not eof(tempfile) do
  275. begin
  276. readln(tempfile,line);
  277. ps:=pos('&',line);
  278. if ps>0 then
  279. line:=copy(line,1,ps-1)+ph+'_'+copy(line,ps+1,255);
  280. writeln(outfile,aktspace,line);
  281. end;
  282. writeln(outfile);
  283. close(tempfile);
  284. rewrite(tempfile);
  285. popshift;
  286. (* walk through all members *)
  287. hp1 := p^.p1;
  288. while assigned(hp1) do
  289. begin
  290. (* hp2 is t_memberdec *)
  291. hp2:=hp1^.p1;
  292. (* hp3 is t_declist *)
  293. hp3:=hp2^.p2;
  294. while assigned(hp3) do
  295. begin
  296. if assigned(hp3^.p1^.p3) and
  297. (hp3^.p1^.p3^.typ = t_size_specifier) then
  298. begin
  299. is_sized:=true;
  300. name:=hp3^.p1^.p2^.p;
  301. { get function in interface }
  302. write(outfile,aktspace,'function ',name);
  303. write(outfile,'(var a : ',ph,') : ');
  304. shift(2);
  305. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  306. writeln(outfile,';');
  307. popshift;
  308. { get function in implementation }
  309. write(implemfile,aktspace,'function ',name);
  310. write(implemfile,'(var a : ',ph,') : ');
  311. if not compactmode then
  312. shift(2);
  313. write_p_a_def(implemfile,hp3^.p1^.p1,hp2^.p1);
  314. writeln(implemfile,';');
  315. writeln(implemfile,aktspace,'begin');
  316. shift(2);
  317. write(implemfile,aktspace,name,':=(a.flag',flag_index);
  318. writeln(implemfile,' and bm_',ph,'_',name,') shr bp_',ph,'_',name,';');
  319. popshift;
  320. writeln(implemfile,aktspace,'end;');
  321. if not compactmode then
  322. popshift;
  323. writeln(implemfile,'');
  324. { set function in interface }
  325. write(outfile,aktspace,'procedure set_',name);
  326. write(outfile,'(var a : ',ph,'; __',name,' : ');
  327. shift(2);
  328. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  329. writeln(outfile,');');
  330. popshift;
  331. { set function in implementation }
  332. write(implemfile,aktspace,'procedure set_',name);
  333. write(implemfile,'(var a : ',ph,'; __',name,' : ');
  334. if not compactmode then
  335. shift(2);
  336. write_p_a_def(implemfile,hp3^.p1^.p1,hp2^.p1);
  337. writeln(implemfile,');');
  338. writeln(implemfile,aktspace,'begin');
  339. shift(2);
  340. write(implemfile,aktspace,'a.flag',flag_index,':=');
  341. write(implemfile,'a.flag',flag_index,' or ');
  342. writeln(implemfile,'((__',name,' shl bp_',ph,'_',name,') and bm_',ph,'_',name,');');
  343. popshift;
  344. writeln(implemfile,aktspace,'end;');
  345. if not compactmode then
  346. popshift;
  347. writeln(implemfile,'');
  348. end
  349. else if is_sized then
  350. begin
  351. is_sized:=false;
  352. inc(flag_index);
  353. end;
  354. hp3:=hp3^.next;
  355. end;
  356. hp1:=hp1^.next;
  357. end;
  358. must_write_packed_field:=false;
  359. block_type:=bt_no;
  360. end;
  361. procedure write_expr(var outfile:text; p : presobject);
  362. begin
  363. if assigned(p) then
  364. begin
  365. case p^.typ of
  366. t_id,
  367. t_ifexpr :
  368. write(outfile,FixId(p^.p));
  369. t_funexprlist :
  370. write_funexpr(outfile,p);
  371. t_exprlist :
  372. begin
  373. if assigned(p^.p1) then
  374. write_expr(outfile,p^.p1);
  375. if assigned(p^.next) then
  376. begin
  377. write(', ');
  378. write_expr(outfile,p^.next);
  379. end;
  380. end;
  381. t_preop : begin
  382. write(outfile,p^.p,'(');
  383. write_expr(outfile,p^.p1);
  384. write(outfile,')');
  385. flush(outfile);
  386. end;
  387. t_typespec : begin
  388. write_type_specifier(outfile,p^.p1);
  389. write(outfile,'(');
  390. write_expr(outfile,p^.p2);
  391. write(outfile,')');
  392. flush(outfile);
  393. end;
  394. t_bop : begin
  395. if p^.p1^.typ<>t_id then
  396. write(outfile,'(');
  397. write_expr(outfile,p^.p1);
  398. if p^.p1^.typ<>t_id then
  399. write(outfile,')');
  400. write(outfile,p^.p);
  401. if p^.p2^.typ<>t_id then
  402. write(outfile,'(');
  403. write_expr(outfile,p^.p2);
  404. if p^.p2^.typ<>t_id then
  405. write(outfile,')');
  406. flush(outfile);
  407. end;
  408. t_arrayop :
  409. begin
  410. write_expr(outfile,p^.p1);
  411. write(outfile,p^.p,'[');
  412. write_expr(outfile,p^.p2);
  413. write(outfile,']');
  414. flush(outfile);
  415. end;
  416. t_callop :
  417. begin
  418. write_expr(outfile,p^.p1);
  419. write(outfile,p^.p,'(');
  420. write_expr(outfile,p^.p2);
  421. write(outfile,')');
  422. flush(outfile);
  423. end;
  424. else
  425. begin
  426. writeln(ord(p^.typ));
  427. internalerror(2);
  428. end;
  429. end;
  430. end;
  431. end;
  432. procedure write_ifexpr(var outfile:text; p : presobject);
  433. begin
  434. flush(outfile);
  435. write(outfile,'if ');
  436. write_expr(outfile,p^.p1);
  437. writeln(outfile,' then');
  438. write(outfile,aktspace,' ');
  439. write(outfile,p^.p);
  440. write(outfile,':=');
  441. write_expr(outfile,p^.p2);
  442. writeln(outfile);
  443. writeln(outfile,aktspace,'else');
  444. write(outfile,aktspace,' ');
  445. write(outfile,p^.p);
  446. write(outfile,':=');
  447. write_expr(outfile,p^.p3);
  448. writeln(outfile,';');
  449. write(outfile,aktspace);
  450. flush(outfile);
  451. end;
  452. procedure write_all_ifexpr(var outfile:text; p : presobject);
  453. begin
  454. if assigned(p) then
  455. begin
  456. case p^.typ of
  457. t_id :;
  458. t_preop :
  459. write_all_ifexpr(outfile,p^.p1);
  460. t_callop,
  461. t_arrayop,
  462. t_bop :
  463. begin
  464. write_all_ifexpr(outfile,p^.p1);
  465. write_all_ifexpr(outfile,p^.p2);
  466. end;
  467. t_ifexpr :
  468. begin
  469. write_all_ifexpr(outfile,p^.p1);
  470. write_all_ifexpr(outfile,p^.p2);
  471. write_all_ifexpr(outfile,p^.p3);
  472. write_ifexpr(outfile,p);
  473. end;
  474. t_typespec :
  475. write_all_ifexpr(outfile,p^.p2);
  476. t_funexprlist,
  477. t_exprlist :
  478. begin
  479. if assigned(p^.p1) then
  480. write_all_ifexpr(outfile,p^.p1);
  481. if assigned(p^.next) then
  482. write_all_ifexpr(outfile,p^.next);
  483. end
  484. else
  485. internalerror(6);
  486. end;
  487. end;
  488. end;
  489. procedure write_funexpr(var outfile:text; p : presobject);
  490. var
  491. i : longint;
  492. begin
  493. if assigned(p) then
  494. begin
  495. case p^.typ of
  496. t_ifexpr :
  497. write(outfile,p^.p);
  498. t_exprlist :
  499. begin
  500. write_expr(outfile,p^.p1);
  501. if assigned(p^.next) then
  502. begin
  503. write(outfile,',');
  504. write_funexpr(outfile,p^.next);
  505. end
  506. end;
  507. t_funcname :
  508. begin
  509. if if_nb>0 then
  510. begin
  511. writeln(outfile,aktspace,'var');
  512. write(outfile,aktspace,' ');
  513. for i:=1 to if_nb do
  514. begin
  515. write(outfile,'if_local',i);
  516. if i<if_nb then
  517. write(outfile,', ')
  518. else
  519. writeln(outfile,' : longint;');
  520. end;
  521. writeln(outfile,aktspace,'(* result types are not known *)');
  522. if_nb:=0;
  523. end;
  524. writeln(outfile,aktspace,'begin');
  525. shift(2);
  526. write(outfile,aktspace);
  527. write_all_ifexpr(outfile,p^.p2);
  528. write_expr(outfile,p^.p1);
  529. write(outfile,':=');
  530. write_funexpr(outfile,p^.p2);
  531. writeln(outfile,';');
  532. popshift;
  533. writeln(outfile,aktspace,'end;');
  534. if not compactmode then
  535. popshift;
  536. flush(outfile);
  537. end;
  538. t_funexprlist :
  539. begin
  540. if assigned(p^.p3) then
  541. begin
  542. write_type_specifier(outfile,p^.p3);
  543. write(outfile,'(');
  544. end;
  545. if assigned(p^.p1) then
  546. write_funexpr(outfile,p^.p1);
  547. if assigned(p^.p2) then
  548. begin
  549. write(outfile,'(');
  550. write_funexpr(outfile,p^.p2);
  551. write(outfile,')');
  552. end;
  553. if assigned(p^.p3) then
  554. write(outfile,')');
  555. end
  556. else internalerror(5);
  557. end;
  558. end;
  559. end;
  560. function ellipsisarg : presobject;
  561. begin
  562. ellipsisarg:=new(presobject,init_two(t_arg,nil,nil));
  563. end;
  564. const
  565. (* if in args *dname is replaced by pdname *)
  566. in_args : boolean = false;
  567. typedef_level : longint = 0;
  568. (* writes an argument list, where p is t_arglist *)
  569. procedure write_args(var outfile:text; p : presobject);
  570. var
  571. len,para : longint;
  572. old_in_args : boolean;
  573. varpara : boolean;
  574. lastp : presobject;
  575. hs : string;
  576. begin
  577. NeedEllipsisOverload:=false;
  578. para:=1;
  579. len:=0;
  580. lastp:=nil;
  581. old_in_args:=in_args;
  582. in_args:=true;
  583. write(outfile,'(');
  584. shift(2);
  585. (* walk through all arguments *)
  586. (* p must be of type t_arglist *)
  587. while assigned(p) do
  588. begin
  589. if p^.typ<>t_arglist then
  590. internalerror(10);
  591. (* is ellipsis ? *)
  592. if not assigned(p^.p1^.p1) and
  593. not assigned(p^.p1^.next) then
  594. begin
  595. write(outfile,'args:array of const');
  596. (* if variable number of args we must allways pop *)
  597. no_pop:=false;
  598. (* Needs 2 declarations, also one without args, becuase
  599. in C you can omit the second parameter. Default parameter
  600. doesn't help as that isn't possible with array of const *)
  601. NeedEllipsisOverload:=true;
  602. (* Remove this para *)
  603. if assigned(lastp) then
  604. lastp^.next:=nil;
  605. dispose(p,done);
  606. (* leave the loop as p isnot valid anymore *)
  607. break;
  608. end
  609. (* we need to correct this in the pp file after *)
  610. else
  611. begin
  612. (* generate a call by reference parameter ? *)
  613. // varpara:=usevarparas and
  614. // assigned(p^.p1^.p2^.p1) and
  615. // (p^.p1^.p2^.p1^.typ in [t_addrdef,t_pointerdef]) and
  616. // assigned(p^.p1^.p2^.p1^.p1) and
  617. // (p^.p1^.p2^.p1^.p1^.typ<>t_procdef);
  618. varpara:=usevarparas and
  619. assigned(p^.p1^.p1) and
  620. (p^.p1^.p1^.typ in [t_addrdef,t_pointerdef]) and
  621. assigned(p^.p1^.p1^.p1) and
  622. (p^.p1^.p1^.p1^.typ<>t_procdef);
  623. (* do not do it for char pointer !! *)
  624. (* para : pchar; and var para : char; are *)
  625. (* completely different in pascal *)
  626. (* here we exclude all typename containing char *)
  627. (* is this a good method ?? *)
  628. if varpara and
  629. (p^.p1^.p1^.typ=t_pointerdef) and
  630. (p^.p1^.p1^.p1^.typ=t_id) and
  631. (pos('CHAR',uppercase(p^.p1^.p1^.p1^.str))<>0) then
  632. varpara:=false;
  633. if varpara then
  634. begin
  635. write(outfile,'var ');
  636. inc(len,4);
  637. end;
  638. (* write new parameter name *)
  639. if assigned(p^.p1^.p2^.p2) then
  640. begin
  641. hs:=FixId(p^.p1^.p2^.p2^.p);
  642. write(outfile,hs);
  643. inc(len,length(hs));
  644. end
  645. else
  646. begin
  647. If removeUnderscore then
  648. begin
  649. Write (outfile,'para',para);
  650. inc(Len,5);
  651. end
  652. else
  653. begin
  654. write(outfile,'_para',para);
  655. inc(Len,6);
  656. end;
  657. end;
  658. write(outfile,':');
  659. if varpara then
  660. begin
  661. write_p_a_def(outfile,p^.p1^.p2^.p1,p^.p1^.p1^.p1);
  662. end
  663. else
  664. write_p_a_def(outfile,p^.p1^.p2^.p1,p^.p1^.p1);
  665. end;
  666. lastp:=p;
  667. p:=p^.next;
  668. if assigned(p) then
  669. begin
  670. write(outfile,'; ');
  671. { if len>40 then : too complicated to compute }
  672. if (para mod 5) = 0 then
  673. begin
  674. writeln(outfile);
  675. write(outfile,aktspace);
  676. end;
  677. end;
  678. inc(para);
  679. end;
  680. write(outfile,')');
  681. flush(outfile);
  682. in_args:=old_in_args;
  683. popshift;
  684. end;
  685. procedure write_p_a_def(var outfile:text; p,simple_type : presobject);
  686. var
  687. i : longint;
  688. error : integer;
  689. pointerwritten,
  690. constant : boolean;
  691. begin
  692. if not(assigned(p)) then
  693. begin
  694. write_type_specifier(outfile,simple_type);
  695. exit;
  696. end;
  697. case p^.typ of
  698. t_pointerdef :
  699. begin
  700. (* procedure variable ? *)
  701. if assigned(p^.p1) and (p^.p1^.typ=t_procdef) then
  702. begin
  703. is_procvar:=true;
  704. (* distinguish between procedure and function *)
  705. if (simple_type^.typ=t_void) and (p^.p1^.p1=nil) then
  706. begin
  707. write(outfile,'procedure ');
  708. shift(10);
  709. (* write arguments *)
  710. if assigned(p^.p1^.p2) then
  711. write_args(outfile,p^.p1^.p2);
  712. flush(outfile);
  713. popshift;
  714. end
  715. else
  716. begin
  717. write(outfile,'function ');
  718. shift(9);
  719. (* write arguments *)
  720. if assigned(p^.p1^.p2) then
  721. write_args(outfile,p^.p1^.p2);
  722. write(outfile,':');
  723. flush(outfile);
  724. write_p_a_def(outfile,p^.p1^.p1,simple_type);
  725. popshift;
  726. end
  727. end
  728. else
  729. begin
  730. (* generate "pointer" ? *)
  731. if (simple_type^.typ=t_void) and (p^.p1=nil) then
  732. begin
  733. write(outfile,'pointer');
  734. flush(outfile);
  735. end
  736. else
  737. begin
  738. pointerwritten:=false;
  739. if (p^.p1=nil) and UsePPointers then
  740. begin
  741. if (simple_type^.typ=t_id) then
  742. begin
  743. write(outfile,PointerName(simple_type^.p));
  744. pointerwritten:=true;
  745. end
  746. { structure }
  747. else if (simple_type^.typ in [t_uniondef,t_structdef]) and
  748. (simple_type^.p1=nil) and (simple_type^.p2^.typ=t_id) then
  749. begin
  750. write(outfile,PointerName(simple_type^.p2^.p));
  751. pointerwritten:=true;
  752. end;
  753. end;
  754. if not pointerwritten then
  755. begin
  756. if in_args then
  757. begin
  758. write(outfile,'P');
  759. pointerprefix:=true;
  760. end
  761. else
  762. write(outfile,'^');
  763. write_p_a_def(outfile,p^.p1,simple_type);
  764. pointerprefix:=false;
  765. end;
  766. end;
  767. end;
  768. end;
  769. t_arraydef :
  770. begin
  771. constant:=false;
  772. if assigned(p^.p2) then
  773. begin
  774. if p^.p2^.typ=t_id then
  775. begin
  776. val(p^.p2^.str,i,error);
  777. if error=0 then
  778. begin
  779. dec(i);
  780. constant:=true;
  781. end;
  782. end;
  783. if not constant then
  784. begin
  785. write(outfile,'array[0..(');
  786. write_expr(outfile,p^.p2);
  787. write(outfile,')-1] of ');
  788. end
  789. else
  790. begin
  791. write(outfile,'array[0..',i,'] of ');
  792. end;
  793. end
  794. else
  795. begin
  796. (* open array *)
  797. write(outfile,'array of ');
  798. end;
  799. flush(outfile);
  800. write_p_a_def(outfile,p^.p1,simple_type);
  801. end;
  802. else internalerror(1);
  803. end;
  804. end;
  805. procedure write_type_specifier(var outfile:text; p : presobject);
  806. var
  807. hp1,hp2,hp3,lastexpr : presobject;
  808. i,l,w : longint;
  809. error : integer;
  810. current_power,
  811. mask : cardinal;
  812. flag_index : longint;
  813. current_level : byte;
  814. pointerwritten,
  815. is_sized : boolean;
  816. begin
  817. case p^.typ of
  818. t_id :
  819. begin
  820. if pointerprefix then
  821. if UseCtypesUnit then
  822. begin
  823. if not IsACType(p^.p) then
  824. begin
  825. PTypeList.Add('P'+p^.str);
  826. end;
  827. end
  828. else
  829. PTypeList.Add('P'+p^.str);
  830. if p^.intname then
  831. write(outfile,p^.p)
  832. else
  833. write(outfile,TypeName(p^.p));
  834. end;
  835. { what can we do with void defs ? }
  836. t_void :
  837. write(outfile,'pointer');
  838. t_pointerdef :
  839. begin
  840. pointerwritten:=false;
  841. if (p^.p1^.typ=t_void) then
  842. begin
  843. write(outfile,'pointer');
  844. pointerwritten:=true;
  845. end
  846. else
  847. if UsePPointers then
  848. begin
  849. if (p^.p1^.typ=t_id) then
  850. begin
  851. write(outfile,PointerName(p^.p1^.p));
  852. pointerwritten:=true;
  853. end
  854. { structure }
  855. else if (p^.p1^.typ in [t_uniondef,t_structdef]) and
  856. (p^.p1^.p1=nil) and (p^.p1^.p2^.typ=t_id) then
  857. begin
  858. write(outfile,PointerName(p^.p1^.p2^.p));
  859. pointerwritten:=true;
  860. end;
  861. end;
  862. if not pointerwritten then
  863. begin
  864. if in_args then
  865. begin
  866. if UseCTypesUnit and (IsACType(p^.p1^.p)=False) then
  867. write(outfile,'P')
  868. else
  869. write(outfile,'p');
  870. pointerprefix:=true;
  871. end
  872. else
  873. begin
  874. if UseCTypesUnit and (IsACType(p^.p1^.p)=False) then
  875. write(outfile,'^')
  876. else
  877. write(outfile,'p');
  878. end;
  879. write_type_specifier(outfile,p^.p1);
  880. pointerprefix:=false;
  881. end;
  882. end;
  883. t_enumdef :
  884. begin
  885. if (typedef_level>1) and (p^.p1=nil) and
  886. (p^.p2^.typ=t_id) then
  887. begin
  888. if pointerprefix then
  889. if UseCTypesUnit and (IsACType( p^.p2^.p )=False) then
  890. PTypeList.Add('P'+p^.p2^.str);
  891. write(outfile,p^.p2^.p);
  892. end
  893. else
  894. if not EnumToConst then
  895. begin
  896. write(outfile,'(');
  897. hp1:=p^.p1;
  898. w:=length(aktspace);
  899. while assigned(hp1) do
  900. begin
  901. write(outfile,hp1^.p1^.p);
  902. if assigned(hp1^.p2) then
  903. begin
  904. write(outfile,' := ');
  905. write_expr(outfile,hp1^.p2);
  906. w:=w+6;(* strlen(hp1^.p); *)
  907. end;
  908. w:=w+length(hp1^.p1^.str);
  909. hp1:=hp1^.next;
  910. if assigned(hp1) then
  911. write(outfile,',');
  912. if w>40 then
  913. begin
  914. writeln(outfile);
  915. write(outfile,aktspace);
  916. w:=length(aktspace);
  917. end;
  918. flush(outfile);
  919. end;
  920. write(outfile,')');
  921. flush(outfile);
  922. end
  923. else
  924. begin
  925. Writeln (outfile,' Longint;');
  926. hp1:=p^.p1;
  927. l:=0;
  928. lastexpr:=nil;
  929. Writeln (outfile,copy(aktspace,1,length(aktspace)-2),'Const');
  930. while assigned(hp1) do
  931. begin
  932. write (outfile,aktspace,hp1^.p1^.p,' = ');
  933. if assigned(hp1^.p2) then
  934. begin
  935. write_expr(outfile,hp1^.p2);
  936. writeln(outfile,';');
  937. lastexpr:=hp1^.p2;
  938. if lastexpr^.typ=t_id then
  939. begin
  940. val(lastexpr^.str,l,error);
  941. if error=0 then
  942. begin
  943. inc(l);
  944. lastexpr:=nil;
  945. end
  946. else
  947. l:=1;
  948. end
  949. else
  950. l:=1;
  951. end
  952. else
  953. begin
  954. if assigned(lastexpr) then
  955. begin
  956. write(outfile,'(');
  957. write_expr(outfile,lastexpr);
  958. writeln(outfile,')+',l,';');
  959. end
  960. else
  961. writeln (outfile,l,';');
  962. inc(l);
  963. end;
  964. hp1:=hp1^.next;
  965. flush(outfile);
  966. end;
  967. block_type:=bt_const;
  968. end;
  969. end;
  970. t_structdef :
  971. begin
  972. inc(typedef_level);
  973. flag_index:=-1;
  974. is_sized:=false;
  975. current_level:=0;
  976. if ((in_args) or (typedef_level>1)) and
  977. (p^.p1=nil) and (p^.p2^.typ=t_id) then
  978. begin
  979. if pointerprefix then
  980. if UseCTypesUnit and (IsACType(p^.p2^.str)=false) then
  981. PTypeList.Add('P'+p^.p2^.str);
  982. write(outfile,TypeName(p^.p2^.p));
  983. end
  984. else
  985. begin
  986. if packrecords then
  987. writeln(outfile,'packed record')
  988. else
  989. writeln(outfile,'record');
  990. shift(2);
  991. hp1:=p^.p1;
  992. (* walk through all members *)
  993. while assigned(hp1) do
  994. begin
  995. (* hp2 is t_memberdec *)
  996. hp2:=hp1^.p1;
  997. (* hp3 is t_declist *)
  998. hp3:=hp2^.p2;
  999. while assigned(hp3) do
  1000. begin
  1001. if not assigned(hp3^.p1^.p3) or
  1002. (hp3^.p1^.p3^.typ <> t_size_specifier) then
  1003. begin
  1004. if is_sized then
  1005. begin
  1006. if current_level <= 16 then
  1007. writeln(outfile,'word;')
  1008. else if current_level <= 32 then
  1009. writeln(outfile,'longint;')
  1010. else
  1011. internalerror(11);
  1012. is_sized:=false;
  1013. end;
  1014. write(outfile,aktspace,FixId(hp3^.p1^.p2^.p));
  1015. write(outfile,' : ');
  1016. shift(2);
  1017. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  1018. popshift;
  1019. end;
  1020. { size specifier or default value ? }
  1021. if assigned(hp3^.p1^.p3) then
  1022. begin
  1023. { we could use mask to implement this }
  1024. { because we need to respect the positions }
  1025. if hp3^.p1^.p3^.typ = t_size_specifier then
  1026. begin
  1027. if not is_sized then
  1028. begin
  1029. current_power:=1;
  1030. current_level:=0;
  1031. inc(flag_index);
  1032. write(outfile,aktspace,'flag',flag_index,' : ');
  1033. end;
  1034. must_write_packed_field:=true;
  1035. is_sized:=true;
  1036. { can it be something else than a constant ? }
  1037. { it can be a macro !! }
  1038. if hp3^.p1^.p3^.p1^.typ=t_id then
  1039. begin
  1040. val(hp3^.p1^.p3^.p1^.str,l,error);
  1041. if error=0 then
  1042. begin
  1043. mask:=0;
  1044. for i:=1 to l do
  1045. begin
  1046. inc(mask,current_power);
  1047. current_power:=current_power*2;
  1048. end;
  1049. write(tempfile,'bm_&',hp3^.p1^.p2^.p);
  1050. writeln(tempfile,' = ',hexstr(mask),';');
  1051. write(tempfile,'bp_&',hp3^.p1^.p2^.p);
  1052. writeln(tempfile,' = ',current_level,';');
  1053. current_level:=current_level + l;
  1054. { go to next flag if 31 }
  1055. if current_level = 32 then
  1056. begin
  1057. write(outfile,'longint');
  1058. is_sized:=false;
  1059. end;
  1060. end;
  1061. end;
  1062. end
  1063. else if hp3^.p1^.p3^.typ = t_default_value then
  1064. begin
  1065. write(outfile,'{=');
  1066. write_expr(outfile,hp3^.p1^.p3^.p1);
  1067. write(outfile,' ignored}');
  1068. end;
  1069. end;
  1070. if not is_sized then
  1071. begin
  1072. if is_procvar then
  1073. begin
  1074. if not no_pop then
  1075. write(outfile,';cdecl');
  1076. is_procvar:=false;
  1077. end;
  1078. writeln(outfile,';');
  1079. end;
  1080. hp3:=hp3^.next;
  1081. end;
  1082. hp1:=hp1^.next;
  1083. end;
  1084. if is_sized then
  1085. begin
  1086. if current_level <= 16 then
  1087. writeln(outfile,'word;')
  1088. else if current_level <= 32 then
  1089. writeln(outfile,'longint;')
  1090. else
  1091. internalerror(11);
  1092. is_sized:=false;
  1093. end;
  1094. popshift;
  1095. write(outfile,aktspace,'end');
  1096. flush(outfile);
  1097. end;
  1098. dec(typedef_level);
  1099. end;
  1100. t_uniondef :
  1101. begin
  1102. inc(typedef_level);
  1103. if (typedef_level>1) and (p^.p1=nil) and
  1104. (p^.p2^.typ=t_id) then
  1105. begin
  1106. write(outfile,p^.p2^.p);
  1107. end
  1108. else
  1109. begin
  1110. inc(typedef_level);
  1111. if packrecords then
  1112. writeln(outfile,'packed record')
  1113. else
  1114. writeln(outfile,'record');
  1115. shift(2);
  1116. writeln(outfile,aktspace,'case longint of');
  1117. shift(2);
  1118. l:=0;
  1119. hp1:=p^.p1;
  1120. (* walk through all members *)
  1121. while assigned(hp1) do
  1122. begin
  1123. (* hp2 is t_memberdec *)
  1124. hp2:=hp1^.p1;
  1125. (* hp3 is t_declist *)
  1126. hp3:=hp2^.p2;
  1127. while assigned(hp3) do
  1128. begin
  1129. write(outfile,aktspace,l,' : ( ');
  1130. write(outfile,FixId(hp3^.p1^.p2^.p),' : ');
  1131. shift(2);
  1132. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  1133. popshift;
  1134. writeln(outfile,' );');
  1135. hp3:=hp3^.next;
  1136. inc(l);
  1137. end;
  1138. hp1:=hp1^.next;
  1139. end;
  1140. popshift;
  1141. write(outfile,aktspace,'end');
  1142. popshift;
  1143. flush(outfile);
  1144. dec(typedef_level);
  1145. end;
  1146. dec(typedef_level);
  1147. end;
  1148. else
  1149. internalerror(3);
  1150. end;
  1151. end;
  1152. procedure write_def_params(var outfile:text; p : presobject);
  1153. var
  1154. hp1 : presobject;
  1155. begin
  1156. case p^.typ of
  1157. t_enumdef : begin
  1158. hp1:=p^.p1;
  1159. while assigned(hp1) do
  1160. begin
  1161. write(outfile,FixId(hp1^.p1^.p));
  1162. hp1:=hp1^.next;
  1163. if assigned(hp1) then
  1164. write(outfile,',')
  1165. else
  1166. write(outfile);
  1167. flush(outfile);
  1168. end;
  1169. flush(outfile);
  1170. end;
  1171. else internalerror(4);
  1172. end;
  1173. end;
  1174. procedure write_statement_block(var outfile:text; p : presobject);
  1175. begin
  1176. writeln(outfile,aktspace,'begin');
  1177. while assigned(p) do
  1178. begin
  1179. shift(2);
  1180. if assigned(p^.p1) then
  1181. begin
  1182. case p^.p1^.typ of
  1183. t_whilenode:
  1184. begin
  1185. write(outfile,aktspace,'while ');
  1186. write_expr(outfile,p^.p1^.p1);
  1187. writeln(outfile,' do');
  1188. shift(2);
  1189. write_statement_block(outfile,p^.p1^.p2);
  1190. popshift;
  1191. end;
  1192. else
  1193. begin
  1194. write(outfile,aktspace);
  1195. write_expr(outfile,p^.p1);
  1196. writeln(outfile,';');
  1197. end;
  1198. end;
  1199. end;
  1200. p:=p^.next;
  1201. popshift;
  1202. end;
  1203. writeln(outfile,aktspace,'end;');
  1204. end;
  1205. %}
  1206. %token _WHILE _FOR _DO _GOTO _CONTINUE _BREAK
  1207. %token TYPEDEF DEFINE
  1208. %token COLON SEMICOLON COMMA
  1209. %token LKLAMMER RKLAMMER LECKKLAMMER RECKKLAMMER
  1210. %token LGKLAMMER RGKLAMMER
  1211. %token STRUCT UNION ENUM
  1212. %token ID NUMBER CSTRING
  1213. %token SHORT UNSIGNED LONG INT FLOAT _CHAR
  1214. %token VOID _CONST
  1215. %token _FAR _HUGE _NEAR
  1216. %token NEW_LINE SPACE_DEFINE
  1217. %token EXTERN STDCALL CDECL CALLBACK PASCAL WINAPI APIENTRY WINGDIAPI SYS_TRAP
  1218. %token _PACKED
  1219. %token ELLIPSIS
  1220. %right _ASSIGN
  1221. %right R_AND
  1222. %left EQUAL UNEQUAL GT LT GTE LTE
  1223. %left QUESTIONMARK COLON
  1224. %left _OR
  1225. %left _AND
  1226. %left _PLUS MINUS
  1227. %left _SHR _SHL
  1228. %left STAR _SLASH
  1229. %right _NOT
  1230. %right LKLAMMER
  1231. %right PSTAR
  1232. %right P_AND
  1233. %right LECKKLAMMER
  1234. %left POINT DEREF
  1235. %left COMMA
  1236. %left STICK
  1237. %token SIGNED
  1238. %token INT8 INT16 INT32 INT64
  1239. %%
  1240. file : declaration_list
  1241. ;
  1242. maybe_space :
  1243. SPACE_DEFINE
  1244. {
  1245. $$:=nil;
  1246. } |
  1247. {
  1248. $$:=nil;
  1249. }
  1250. ;
  1251. error_info : {
  1252. writeln(outfile,'(* error ');
  1253. writeln(outfile,yyline);
  1254. };
  1255. declaration_list : declaration_list declaration
  1256. { if yydebug then writeln('declaration reduced at line ',line_no);
  1257. if yydebug then writeln(outfile,'(* declaration reduced *)');
  1258. }
  1259. | declaration_list define_dec
  1260. { if yydebug then writeln('define declaration reduced at line ',line_no);
  1261. if yydebug then writeln(outfile,'(* define declaration reduced *)');
  1262. }
  1263. | declaration
  1264. { if yydebug then writeln('declaration reduced at line ',line_no);
  1265. }
  1266. | define_dec
  1267. { if yydebug then writeln('define declaration reduced at line ',line_no);
  1268. }
  1269. ;
  1270. dec_specifier :
  1271. EXTERN { $$:=new(presobject,init_id('extern')); }
  1272. |{ $$:=new(presobject,init_id('intern')); }
  1273. ;
  1274. dec_modifier :
  1275. STDCALL { $$:=new(presobject,init_id('no_pop')); }
  1276. | CDECL { $$:=new(presobject,init_id('cdecl')); }
  1277. | CALLBACK { $$:=new(presobject,init_id('no_pop')); }
  1278. | PASCAL { $$:=new(presobject,init_id('no_pop')); }
  1279. | WINAPI { $$:=new(presobject,init_id('no_pop')); }
  1280. | APIENTRY { $$:=new(presobject,init_id('no_pop')); }
  1281. | WINGDIAPI { $$:=new(presobject,init_id('no_pop')); }
  1282. | { $$:=nil }
  1283. ;
  1284. systrap_specifier:
  1285. SYS_TRAP LKLAMMER dname RKLAMMER { $$:=$3; }
  1286. | { $$:=nil; }
  1287. ;
  1288. statement :
  1289. expr SEMICOLON { $$:=$1; } |
  1290. _WHILE LKLAMMER expr RKLAMMER statement_list { $$:=new(presobject,init_two(t_whilenode,$3,$5)); }
  1291. ;
  1292. statement_list : statement statement_list
  1293. {
  1294. $$:=new(presobject,init_one(t_statement_list,$1));
  1295. $$^.next:=$2;
  1296. } |
  1297. statement
  1298. {
  1299. $$:=new(presobject,init_one(t_statement_list,$1));
  1300. } |
  1301. SEMICOLON
  1302. {
  1303. $$:=new(presobject,init_one(t_statement_list,nil));
  1304. } |
  1305. {
  1306. $$:=new(presobject,init_one(t_statement_list,nil));
  1307. }
  1308. ;
  1309. statement_block :
  1310. LGKLAMMER statement_list RGKLAMMER { $$:=$2; }
  1311. ;
  1312. declaration :
  1313. dec_specifier type_specifier dec_modifier declarator_list statement_block
  1314. {
  1315. IsExtern:=false;
  1316. (* by default we must pop the args pushed on stack *)
  1317. no_pop:=false;
  1318. if (assigned($4)and assigned($4^.p1)and assigned($4^.p1^.p1))
  1319. and ($4^.p1^.p1^.typ=t_procdef) then
  1320. begin
  1321. repeat
  1322. If UseLib then
  1323. IsExtern:=true
  1324. else
  1325. IsExtern:=assigned($1)and($1^.str='extern');
  1326. no_pop:=assigned($3) and ($3^.str='no_pop');
  1327. if (block_type<>bt_func) and not(createdynlib) then
  1328. begin
  1329. writeln(outfile);
  1330. block_type:=bt_func;
  1331. end;
  1332. (* dyn. procedures must be put into a var block *)
  1333. if createdynlib then
  1334. begin
  1335. if (block_type<>bt_var) then
  1336. begin
  1337. if not(compactmode) then
  1338. writeln(outfile);
  1339. writeln(outfile,aktspace,'var');
  1340. block_type:=bt_var;
  1341. end;
  1342. shift(2);
  1343. end;
  1344. if not CompactMode then
  1345. begin
  1346. write(outfile,aktspace);
  1347. if not IsExtern then
  1348. write(implemfile,aktspace);
  1349. end;
  1350. (* distinguish between procedure and function *)
  1351. if assigned($2) then
  1352. if ($2^.typ=t_void) and ($4^.p1^.p1^.p1=nil) then
  1353. begin
  1354. if createdynlib then
  1355. begin
  1356. write(outfile,$4^.p1^.p2^.p,' : procedure');
  1357. end
  1358. else
  1359. begin
  1360. shift(10);
  1361. write(outfile,'procedure ',$4^.p1^.p2^.p);
  1362. end;
  1363. if assigned($4^.p1^.p1^.p2) then
  1364. write_args(outfile,$4^.p1^.p1^.p2);
  1365. if createdynlib then
  1366. begin
  1367. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1368. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1369. end
  1370. else if not IsExtern then
  1371. begin
  1372. write(implemfile,'procedure ',$4^.p1^.p2^.p);
  1373. if assigned($4^.p1^.p1^.p2) then
  1374. write_args(implemfile,$4^.p1^.p1^.p2);
  1375. end;
  1376. end
  1377. else
  1378. begin
  1379. if createdynlib then
  1380. begin
  1381. write(outfile,$4^.p1^.p2^.p,' : function');
  1382. end
  1383. else
  1384. begin
  1385. shift(9);
  1386. write(outfile,'function ',$4^.p1^.p2^.p);
  1387. end;
  1388. if assigned($4^.p1^.p1^.p2) then
  1389. write_args(outfile,$4^.p1^.p1^.p2);
  1390. write(outfile,':');
  1391. write_p_a_def(outfile,$4^.p1^.p1^.p1,$2);
  1392. if createdynlib then
  1393. begin
  1394. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1395. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1396. end
  1397. else if not IsExtern then
  1398. begin
  1399. write(implemfile,'function ',$4^.p1^.p2^.p);
  1400. if assigned($4^.p1^.p1^.p2) then
  1401. write_args(implemfile,$4^.p1^.p1^.p2);
  1402. write(implemfile,':');
  1403. write_p_a_def(implemfile,$4^.p1^.p1^.p1,$2);
  1404. end;
  1405. end;
  1406. (* No CDECL in interface for Uselib *)
  1407. if IsExtern and (not no_pop) then
  1408. write(outfile,';cdecl');
  1409. popshift;
  1410. if createdynlib then
  1411. begin
  1412. writeln(outfile,';');
  1413. end
  1414. else if UseLib then
  1415. begin
  1416. if IsExtern then
  1417. begin
  1418. write (outfile,';external');
  1419. If UseName then
  1420. Write(outfile,' External_library name ''',$4^.p1^.p2^.p,'''');
  1421. end;
  1422. writeln(outfile,';');
  1423. end
  1424. else
  1425. begin
  1426. writeln(outfile,';');
  1427. if not IsExtern then
  1428. begin
  1429. writeln(implemfile,';');
  1430. shift(2);
  1431. if $5^.typ=t_statement_list then
  1432. write_statement_block(implemfile,$5);
  1433. popshift;
  1434. end;
  1435. end;
  1436. IsExtern:=false;
  1437. if not(compactmode) and not(createdynlib) then
  1438. writeln(outfile);
  1439. until not NeedEllipsisOverload;
  1440. end
  1441. else (* $4^.p1^.p1^.typ=t_procdef *)
  1442. if assigned($4)and assigned($4^.p1) then
  1443. begin
  1444. shift(2);
  1445. if block_type<>bt_var then
  1446. begin
  1447. if not(compactmode) then
  1448. writeln(outfile);
  1449. writeln(outfile,aktspace,'var');
  1450. end;
  1451. block_type:=bt_var;
  1452. shift(2);
  1453. IsExtern:=assigned($1)and($1^.str='extern');
  1454. (* walk through all declarations *)
  1455. hp:=$4;
  1456. while assigned(hp) and assigned(hp^.p1) do
  1457. begin
  1458. (* write new var name *)
  1459. if assigned(hp^.p1^.p2) and assigned(hp^.p1^.p2^.p) then
  1460. write(outfile,aktspace,hp^.p1^.p2^.p);
  1461. write(outfile,' : ');
  1462. shift(2);
  1463. (* write its type *)
  1464. write_p_a_def(outfile,hp^.p1^.p1,$2);
  1465. if assigned(hp^.p1^.p2)and assigned(hp^.p1^.p2^.p)then
  1466. begin
  1467. if isExtern then
  1468. write(outfile,';cvar;external')
  1469. else
  1470. write(outfile,';cvar;public');
  1471. end;
  1472. writeln(outfile,';');
  1473. popshift;
  1474. hp:=hp^.p2;
  1475. end;
  1476. popshift;
  1477. popshift;
  1478. end;
  1479. if assigned($1) then
  1480. dispose($1,done);
  1481. if assigned($2) then
  1482. dispose($2,done);
  1483. if assigned($3) then
  1484. dispose($3,done);
  1485. if assigned($4) then
  1486. dispose($4,done);
  1487. if assigned($5) then
  1488. dispose($5,done);
  1489. }
  1490. | dec_specifier type_specifier dec_modifier declarator_list systrap_specifier SEMICOLON
  1491. {
  1492. IsExtern:=false;
  1493. (* by default we must pop the args pushed on stack *)
  1494. no_pop:=false;
  1495. if (assigned($4)and assigned($4^.p1)and assigned($4^.p1^.p1))
  1496. and ($4^.p1^.p1^.typ=t_procdef) then
  1497. begin
  1498. repeat
  1499. If UseLib then
  1500. IsExtern:=true
  1501. else
  1502. IsExtern:=assigned($1)and($1^.str='extern');
  1503. no_pop:=assigned($3) and ($3^.str='no_pop');
  1504. if (block_type<>bt_func) and not(createdynlib) then
  1505. begin
  1506. writeln(outfile);
  1507. block_type:=bt_func;
  1508. end;
  1509. (* dyn. procedures must be put into a var block *)
  1510. if createdynlib then
  1511. begin
  1512. if (block_type<>bt_var) then
  1513. begin
  1514. if not(compactmode) then
  1515. writeln(outfile);
  1516. writeln(outfile,aktspace,'var');
  1517. block_type:=bt_var;
  1518. end;
  1519. shift(2);
  1520. end;
  1521. if not CompactMode then
  1522. begin
  1523. write(outfile,aktspace);
  1524. if not IsExtern then
  1525. write(implemfile,aktspace);
  1526. end;
  1527. (* distinguish between procedure and function *)
  1528. if assigned($2) then
  1529. if ($2^.typ=t_void) and ($4^.p1^.p1^.p1=nil) then
  1530. begin
  1531. if createdynlib then
  1532. begin
  1533. write(outfile,$4^.p1^.p2^.p,' : procedure');
  1534. end
  1535. else
  1536. begin
  1537. shift(10);
  1538. write(outfile,'procedure ',$4^.p1^.p2^.p);
  1539. end;
  1540. if assigned($4^.p1^.p1^.p2) then
  1541. write_args(outfile,$4^.p1^.p1^.p2);
  1542. if createdynlib then
  1543. begin
  1544. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1545. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1546. end
  1547. else if not IsExtern then
  1548. begin
  1549. write(implemfile,'procedure ',$4^.p1^.p2^.p);
  1550. if assigned($4^.p1^.p1^.p2) then
  1551. write_args(implemfile,$4^.p1^.p1^.p2);
  1552. end;
  1553. end
  1554. else
  1555. begin
  1556. if createdynlib then
  1557. begin
  1558. write(outfile,$4^.p1^.p2^.p,' : function');
  1559. end
  1560. else
  1561. begin
  1562. shift(9);
  1563. write(outfile,'function ',$4^.p1^.p2^.p);
  1564. end;
  1565. if assigned($4^.p1^.p1^.p2) then
  1566. write_args(outfile,$4^.p1^.p1^.p2);
  1567. write(outfile,':');
  1568. write_p_a_def(outfile,$4^.p1^.p1^.p1,$2);
  1569. if createdynlib then
  1570. begin
  1571. loaddynlibproc.add('pointer('+$4^.p1^.p2^.p+'):=GetProcAddress(hlib,'''+$4^.p1^.p2^.p+''');');
  1572. freedynlibproc.add($4^.p1^.p2^.p+':=nil;');
  1573. end
  1574. else if not IsExtern then
  1575. begin
  1576. write(implemfile,'function ',$4^.p1^.p2^.p);
  1577. if assigned($4^.p1^.p1^.p2) then
  1578. write_args(implemfile,$4^.p1^.p1^.p2);
  1579. write(implemfile,':');
  1580. write_p_a_def(implemfile,$4^.p1^.p1^.p1,$2);
  1581. end;
  1582. end;
  1583. if assigned($5) then
  1584. write(outfile,';systrap ',$5^.p);
  1585. (* No CDECL in interface for Uselib *)
  1586. if IsExtern and (not no_pop) then
  1587. write(outfile,';cdecl');
  1588. popshift;
  1589. if createdynlib then
  1590. begin
  1591. writeln(outfile,';');
  1592. end
  1593. else if UseLib then
  1594. begin
  1595. if IsExtern then
  1596. begin
  1597. write (outfile,';external');
  1598. If UseName then
  1599. Write(outfile,' External_library name ''',$4^.p1^.p2^.p,'''');
  1600. end;
  1601. writeln(outfile,';');
  1602. end
  1603. else
  1604. begin
  1605. writeln(outfile,';');
  1606. if not IsExtern then
  1607. begin
  1608. writeln(implemfile,';');
  1609. writeln(implemfile,aktspace,'begin');
  1610. writeln(implemfile,aktspace,' { You must implement this function }');
  1611. writeln(implemfile,aktspace,'end;');
  1612. end;
  1613. end;
  1614. IsExtern:=false;
  1615. if not(compactmode) and not(createdynlib) then
  1616. writeln(outfile);
  1617. until not NeedEllipsisOverload;
  1618. end
  1619. else (* $4^.p1^.p1^.typ=t_procdef *)
  1620. if assigned($4)and assigned($4^.p1) then
  1621. begin
  1622. shift(2);
  1623. if block_type<>bt_var then
  1624. begin
  1625. if not(compactmode) then
  1626. writeln(outfile);
  1627. writeln(outfile,aktspace,'var');
  1628. end;
  1629. block_type:=bt_var;
  1630. shift(2);
  1631. IsExtern:=assigned($1)and($1^.str='extern');
  1632. (* walk through all declarations *)
  1633. hp:=$4;
  1634. while assigned(hp) and assigned(hp^.p1) do
  1635. begin
  1636. (* write new var name *)
  1637. if assigned(hp^.p1^.p2) and assigned(hp^.p1^.p2^.p) then
  1638. write(outfile,aktspace,hp^.p1^.p2^.p);
  1639. write(outfile,' : ');
  1640. shift(2);
  1641. (* write its type *)
  1642. write_p_a_def(outfile,hp^.p1^.p1,$2);
  1643. if assigned(hp^.p1^.p2)and assigned(hp^.p1^.p2^.p)then
  1644. begin
  1645. if isExtern then
  1646. write(outfile,';cvar;external')
  1647. else
  1648. write(outfile,';cvar;public');
  1649. end;
  1650. writeln(outfile,';');
  1651. popshift;
  1652. hp:=hp^.p2;
  1653. end;
  1654. popshift;
  1655. popshift;
  1656. end;
  1657. if assigned($1)then dispose($1,done);
  1658. if assigned($2)then dispose($2,done);
  1659. if assigned($4)then dispose($4,done);
  1660. } |
  1661. special_type_specifier SEMICOLON
  1662. {
  1663. if block_type<>bt_type then
  1664. begin
  1665. if not(compactmode) then
  1666. writeln(outfile);
  1667. writeln(outfile,aktspace,'type');
  1668. block_type:=bt_type;
  1669. end;
  1670. shift(2);
  1671. if ( yyv[yysp-1]^.p2 <> nil ) then
  1672. begin
  1673. (* write new type name *)
  1674. TN:=TypeName($1^.p2^.p);
  1675. PN:=PointerName($1^.p2^.p);
  1676. (* define a Pointer type also for structs *)
  1677. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1678. assigned($1) and ($1^.typ in [t_uniondef,t_structdef]) then
  1679. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1680. write(outfile,aktspace,TN,' = ');
  1681. shift(2);
  1682. hp:=$1;
  1683. write_type_specifier(outfile,hp);
  1684. popshift;
  1685. (* enum_to_const can make a switch to const *)
  1686. if block_type=bt_type then
  1687. writeln(outfile,';');
  1688. writeln(outfile);
  1689. flush(outfile);
  1690. popshift;
  1691. if must_write_packed_field then
  1692. write_packed_fields_info(outfile,hp,TN);
  1693. if assigned(hp) then
  1694. dispose(hp,done)
  1695. end
  1696. else
  1697. begin
  1698. TN:=TypeName(yyv[yysp-1]^.str);
  1699. PN:=PointerName(yyv[yysp-1]^.str);
  1700. if UsePPointers then writeln(outfile,aktspace,PN,' = ^',TN,';');
  1701. if PackRecords then
  1702. writeln(outfile, aktspace, TN, ' = packed record')
  1703. else
  1704. writeln(outfile, aktspace, TN, ' = record');
  1705. writeln(outfile, aktspace, ' {undefined structure}');
  1706. writeln(outfile, aktspace, ' end;');
  1707. writeln(outfile);
  1708. popshift;
  1709. end;
  1710. } |
  1711. TYPEDEF STRUCT dname dname SEMICOLON
  1712. {
  1713. (* TYPEDEF STRUCT dname dname SEMICOLON *)
  1714. if block_type<>bt_type then
  1715. begin
  1716. if not(compactmode) then
  1717. writeln(outfile);
  1718. writeln(outfile,aktspace,'type');
  1719. block_type:=bt_type;
  1720. end;
  1721. PN:=TypeName($3^.p);
  1722. TN:=TypeName($4^.p);
  1723. if Uppercase(tn)<>Uppercase(pn) then
  1724. begin
  1725. shift(2);
  1726. writeln(outfile,aktspace,PN,' = ',TN,';');
  1727. popshift;
  1728. end;
  1729. if assigned($3) then
  1730. dispose($3,done);
  1731. if assigned($4) then
  1732. dispose($4,done);
  1733. } |
  1734. TYPEDEF type_specifier LKLAMMER dec_modifier declarator RKLAMMER maybe_space LKLAMMER argument_declaration_list RKLAMMER SEMICOLON
  1735. {
  1736. (* TYPEDEF type_specifier LKLAMMER dec_modifier declarator RKLAMMER maybe_space LKLAMMER argument_declaration_list RKLAMMER SEMICOLON *)
  1737. if block_type<>bt_type then
  1738. begin
  1739. if not(compactmode) then
  1740. writeln(outfile);
  1741. writeln(outfile,aktspace,'type');
  1742. block_type:=bt_type;
  1743. end;
  1744. no_pop:=assigned($4) and ($4^.str='no_pop');
  1745. shift(2);
  1746. (* walk through all declarations *)
  1747. hp:=$5;
  1748. if assigned(hp) then
  1749. begin
  1750. hp:=$5;
  1751. while assigned(hp^.p1) do
  1752. hp:=hp^.p1;
  1753. hp^.p1:=new(presobject,init_two(t_procdef,nil,$9));
  1754. hp:=$5;
  1755. if assigned(hp^.p1) and assigned(hp^.p1^.p1) then
  1756. begin
  1757. writeln(outfile);
  1758. (* write new type name *)
  1759. write(outfile,aktspace,TypeName(hp^.p2^.p),' = ');
  1760. shift(2);
  1761. write_p_a_def(outfile,hp^.p1,$2);
  1762. popshift;
  1763. (* if no_pop it is normal fpc calling convention *)
  1764. if is_procvar and
  1765. (not no_pop) then
  1766. write(outfile,';cdecl');
  1767. writeln(outfile,';');
  1768. flush(outfile);
  1769. end;
  1770. end;
  1771. popshift;
  1772. if assigned($2)then
  1773. dispose($2,done);
  1774. if assigned($4)then
  1775. dispose($4,done);
  1776. if assigned($5)then (* disposes also $9 *)
  1777. dispose($5,done);
  1778. } |
  1779. TYPEDEF type_specifier dec_modifier declarator_list SEMICOLON
  1780. {
  1781. (* TYPEDEF type_specifier dec_modifier declarator_list SEMICOLON *)
  1782. if block_type<>bt_type then
  1783. begin
  1784. if not(compactmode) then
  1785. writeln(outfile);
  1786. writeln(outfile,aktspace,'type');
  1787. block_type:=bt_type;
  1788. end
  1789. else
  1790. writeln(outfile);
  1791. no_pop:=assigned($3) and ($3^.str='no_pop');
  1792. shift(2);
  1793. (* Get the name to write the type definition for, try
  1794. to use the tag name first *)
  1795. if assigned($2^.p2) then
  1796. begin
  1797. ph:=$2^.p2;
  1798. end
  1799. else
  1800. begin
  1801. if not assigned($4^.p1^.p2) then
  1802. internalerror(4444);
  1803. ph:=$4^.p1^.p2;
  1804. end;
  1805. (* write type definition *)
  1806. is_procvar:=false;
  1807. TN:=TypeName(ph^.p);
  1808. PN:=PointerName(ph^.p);
  1809. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1810. assigned($2) and ($2^.typ<>t_procdef) then
  1811. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1812. (* write new type name *)
  1813. write(outfile,aktspace,TN,' = ');
  1814. shift(2);
  1815. write_p_a_def(outfile,$4^.p1^.p1,$2);
  1816. popshift;
  1817. (* if no_pop it is normal fpc calling convention *)
  1818. if is_procvar and
  1819. (not no_pop) then
  1820. write(outfile,';cdecl');
  1821. writeln(outfile,';');
  1822. flush(outfile);
  1823. (* write alias names, ph points to the name already used *)
  1824. hp:=$4;
  1825. while assigned(hp) do
  1826. begin
  1827. if (hp<>ph) and assigned(hp^.p1^.p2) then
  1828. begin
  1829. PN:=TypeName(ph^.p);
  1830. TN:=TypeName(hp^.p1^.p2^.p);
  1831. if Uppercase(TN)<>Uppercase(PN) then
  1832. begin
  1833. write(outfile,aktspace,TN,' = ');
  1834. write_p_a_def(outfile,hp^.p1^.p1,ph);
  1835. writeln(outfile,';');
  1836. PN:=PointerName(hp^.p1^.p2^.p);
  1837. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1838. assigned($2) and ($2^.typ<>t_procdef) then
  1839. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1840. end;
  1841. end;
  1842. hp:=hp^.next;
  1843. end;
  1844. popshift;
  1845. if must_write_packed_field then
  1846. if assigned(ph) then
  1847. write_packed_fields_info(outfile,$2,ph^.str)
  1848. else if assigned($2^.p2) then
  1849. write_packed_fields_info(outfile,$2,$2^.p2^.str);
  1850. if assigned($2)then
  1851. dispose($2,done);
  1852. if assigned($3)then
  1853. dispose($3,done);
  1854. if assigned($4)then
  1855. dispose($4,done);
  1856. } |
  1857. TYPEDEF dname SEMICOLON
  1858. {
  1859. if block_type<>bt_type then
  1860. begin
  1861. if not(compactmode) then
  1862. writeln(outfile);
  1863. writeln(outfile,aktspace,'type');
  1864. block_type:=bt_type;
  1865. end
  1866. else
  1867. writeln(outfile);
  1868. shift(2);
  1869. (* write as pointer *)
  1870. writeln(outfile,'(* generic typedef *)');
  1871. writeln(outfile,aktspace,$2^.p,' = pointer;');
  1872. flush(outfile);
  1873. popshift;
  1874. if assigned($2) then
  1875. dispose($2,done);
  1876. }
  1877. | error error_info SEMICOLON
  1878. { writeln(outfile,'in declaration at line ',line_no,' *)');
  1879. aktspace:='';
  1880. in_space_define:=0;
  1881. in_define:=false;
  1882. arglevel:=0;
  1883. if_nb:=0;
  1884. aktspace:=' ';
  1885. space_index:=1;
  1886. yyerrok;}
  1887. ;
  1888. define_dec :
  1889. DEFINE dname LKLAMMER enum_list RKLAMMER para_def_expr NEW_LINE
  1890. {
  1891. (* DEFINE dname LKLAMMER enum_list RKLAMMER para_def_expr NEW_LINE *)
  1892. if not stripinfo then
  1893. begin
  1894. writeln (outfile,aktspace,'{ was #define dname(params) para_def_expr }');
  1895. writeln (implemfile,aktspace,'{ was #define dname(params) para_def_expr }');
  1896. if assigned($4) then
  1897. begin
  1898. writeln (outfile,aktspace,'{ argument types are unknown }');
  1899. writeln (implemfile,aktspace,'{ argument types are unknown }');
  1900. end;
  1901. if not assigned($6^.p3) then
  1902. begin
  1903. writeln(outfile,aktspace,'{ return type might be wrong } ');
  1904. writeln(implemfile,aktspace,'{ return type might be wrong } ');
  1905. end;
  1906. end;
  1907. if block_type<>bt_func then
  1908. writeln(outfile);
  1909. block_type:=bt_func;
  1910. write(outfile,aktspace,'function ',$2^.p);
  1911. write(implemfile,aktspace,'function ',$2^.p);
  1912. if assigned($4) then
  1913. begin
  1914. write(outfile,'(');
  1915. write(implemfile,'(');
  1916. ph:=new(presobject,init_one(t_enumdef,$4));
  1917. write_def_params(outfile,ph);
  1918. write_def_params(implemfile,ph);
  1919. if assigned(ph) then dispose(ph,done);
  1920. ph:=nil;
  1921. (* types are unknown *)
  1922. write(outfile,' : longint)');
  1923. write(implemfile,' : longint)');
  1924. end;
  1925. if not assigned($6^.p3) then
  1926. begin
  1927. writeln(outfile,' : longint;',aktspace,commentstr);
  1928. writeln(implemfile,' : longint;');
  1929. flush(outfile);
  1930. end
  1931. else
  1932. begin
  1933. write(outfile,' : ');
  1934. write_type_specifier(outfile,$6^.p3);
  1935. writeln(outfile,';',aktspace,commentstr);
  1936. flush(outfile);
  1937. write(implemfile,' : ');
  1938. write_type_specifier(implemfile,$6^.p3);
  1939. writeln(implemfile,';');
  1940. end;
  1941. writeln(outfile);
  1942. flush(outfile);
  1943. hp:=new(presobject,init_two(t_funcname,$2,$6));
  1944. write_funexpr(implemfile,hp);
  1945. writeln(implemfile);
  1946. flush(implemfile);
  1947. if assigned(hp)then dispose(hp,done);
  1948. }|
  1949. DEFINE dname SPACE_DEFINE NEW_LINE
  1950. {
  1951. (* DEFINE dname SPACE_DEFINE NEW_LINE *)
  1952. writeln(outfile,'{$define ',$2^.p,'}',aktspace,commentstr);
  1953. flush(outfile);
  1954. if assigned($2)then
  1955. dispose($2,done);
  1956. }|
  1957. DEFINE dname NEW_LINE
  1958. {
  1959. writeln(outfile,'{$define ',$2^.p,'}',aktspace,commentstr);
  1960. flush(outfile);
  1961. if assigned($2)then
  1962. dispose($2,done);
  1963. } |
  1964. DEFINE dname SPACE_DEFINE def_expr NEW_LINE
  1965. {
  1966. (* DEFINE dname SPACE_DEFINE def_expr NEW_LINE *)
  1967. if ($4^.typ=t_exprlist) and
  1968. $4^.p1^.is_const and
  1969. not assigned($4^.next) then
  1970. begin
  1971. if block_type<>bt_const then
  1972. begin
  1973. if block_type<>bt_func then
  1974. writeln(outfile);
  1975. writeln(outfile,aktspace,'const');
  1976. end;
  1977. block_type:=bt_const;
  1978. shift(2);
  1979. write(outfile,aktspace,$2^.p);
  1980. write(outfile,' = ');
  1981. flush(outfile);
  1982. write_expr(outfile,$4^.p1);
  1983. writeln(outfile,';',aktspace,commentstr);
  1984. popshift;
  1985. if assigned($2) then
  1986. dispose($2,done);
  1987. if assigned($4) then
  1988. dispose($4,done);
  1989. end
  1990. else
  1991. begin
  1992. if block_type<>bt_func then
  1993. writeln(outfile);
  1994. if not stripinfo then
  1995. begin
  1996. writeln (outfile,aktspace,'{ was #define dname def_expr }');
  1997. writeln (implemfile,aktspace,'{ was #define dname def_expr }');
  1998. end;
  1999. block_type:=bt_func;
  2000. write(outfile,aktspace,'function ',$2^.p);
  2001. write(implemfile,aktspace,'function ',$2^.p);
  2002. shift(2);
  2003. if not assigned($4^.p3) then
  2004. begin
  2005. writeln(outfile,' : longint; { return type might be wrong }');
  2006. flush(outfile);
  2007. writeln(implemfile,' : longint; { return type might be wrong }');
  2008. end
  2009. else
  2010. begin
  2011. write(outfile,' : ');
  2012. write_type_specifier(outfile,$4^.p3);
  2013. writeln(outfile,';',aktspace,commentstr);
  2014. flush(outfile);
  2015. write(implemfile,' : ');
  2016. write_type_specifier(implemfile,$4^.p3);
  2017. writeln(implemfile,';');
  2018. end;
  2019. writeln(outfile);
  2020. flush(outfile);
  2021. hp:=new(presobject,init_two(t_funcname,$2,$4));
  2022. write_funexpr(implemfile,hp);
  2023. popshift;
  2024. dispose(hp,done);
  2025. writeln(implemfile);
  2026. flush(implemfile);
  2027. end;
  2028. }
  2029. | error error_info NEW_LINE
  2030. { writeln(outfile,'in define line ',line_no,' *)');
  2031. aktspace:='';
  2032. in_space_define:=0;
  2033. in_define:=false;
  2034. arglevel:=0;
  2035. if_nb:=0;
  2036. aktspace:=' ';
  2037. space_index:=1;
  2038. yyerrok;}
  2039. ;
  2040. closed_list : LGKLAMMER member_list RGKLAMMER
  2041. {$$:=$2;} |
  2042. error error_info RGKLAMMER
  2043. { writeln(outfile,' in member_list *)');
  2044. yyerrok;
  2045. $$:=nil;
  2046. }
  2047. ;
  2048. closed_enum_list : LGKLAMMER enum_list RGKLAMMER
  2049. {$$:=$2;} |
  2050. error error_info RGKLAMMER
  2051. { writeln(outfile,' in enum_list *)');
  2052. yyerrok;
  2053. $$:=nil;
  2054. }
  2055. ;
  2056. special_type_specifier :
  2057. STRUCT dname closed_list _PACKED
  2058. {
  2059. if (not is_packed) and (not packrecords) then
  2060. writeln(outfile,'{$PACKRECORDS 1}');
  2061. is_packed:=true;
  2062. $$:=new(presobject,init_two(t_structdef,$3,$2));
  2063. } |
  2064. STRUCT dname closed_list
  2065. {
  2066. if (is_packed) and (not packrecords) then
  2067. writeln(outfile,'{$PACKRECORDS 4}');
  2068. is_packed:=false;
  2069. $$:=new(presobject,init_two(t_structdef,$3,$2));
  2070. } |
  2071. UNION dname closed_list _PACKED
  2072. {
  2073. if (not is_packed) and (not packrecords) then
  2074. writeln(outfile,'{$PACKRECORDS 1}');
  2075. is_packed:=true;
  2076. $$:=new(presobject,init_two(t_uniondef,$3,$2));
  2077. } |
  2078. UNION dname closed_list
  2079. {
  2080. $$:=new(presobject,init_two(t_uniondef,$3,$2));
  2081. } |
  2082. UNION dname
  2083. {
  2084. $$:=$2;
  2085. } |
  2086. STRUCT dname
  2087. {
  2088. $$:=$2;
  2089. } |
  2090. ENUM dname closed_enum_list
  2091. {
  2092. $$:=new(presobject,init_two(t_enumdef,$3,$2));
  2093. } |
  2094. ENUM dname
  2095. {
  2096. $$:=$2;
  2097. };
  2098. type_specifier :
  2099. _CONST type_specifier
  2100. {
  2101. if not stripinfo then
  2102. writeln(outfile,'(* Const before type ignored *)');
  2103. $$:=$2;
  2104. } |
  2105. UNION closed_list _PACKED
  2106. {
  2107. if (not is_packed) and (not packrecords)then
  2108. writeln(outfile,'{$PACKRECORDS 1}');
  2109. is_packed:=true;
  2110. $$:=new(presobject,init_one(t_uniondef,$2));
  2111. } |
  2112. UNION closed_list
  2113. {
  2114. $$:=new(presobject,init_one(t_uniondef,$2));
  2115. } |
  2116. STRUCT closed_list _PACKED
  2117. {
  2118. if (not is_packed) and (not packrecords) then
  2119. writeln(outfile,'{$PACKRECORDS 1}');
  2120. is_packed:=true;
  2121. $$:=new(presobject,init_one(t_structdef,$2));
  2122. } |
  2123. STRUCT closed_list
  2124. {
  2125. if (is_packed) and (not packrecords) then
  2126. writeln(outfile,'{$PACKRECORDS 4}');
  2127. is_packed:=false;
  2128. $$:=new(presobject,init_one(t_structdef,$2));
  2129. } |
  2130. ENUM closed_enum_list
  2131. {
  2132. $$:=new(presobject,init_one(t_enumdef,$2));
  2133. } |
  2134. special_type_specifier
  2135. {
  2136. $$:=$1;
  2137. } |
  2138. simple_type_name { $$:=$1; }
  2139. ;
  2140. member_list : member_declaration member_list
  2141. {
  2142. $$:=new(presobject,init_one(t_memberdeclist,$1));
  2143. $$^.next:=$2;
  2144. } |
  2145. member_declaration
  2146. {
  2147. $$:=new(presobject,init_one(t_memberdeclist,$1));
  2148. }
  2149. ;
  2150. member_declaration :
  2151. type_specifier declarator_list SEMICOLON
  2152. {
  2153. $$:=new(presobject,init_two(t_memberdec,$1,$2));
  2154. }
  2155. ;
  2156. dname : ID { (*dname*)
  2157. $$:=new(presobject,init_id(act_token));
  2158. }
  2159. ;
  2160. special_type_name :
  2161. SIGNED special_type_name
  2162. {
  2163. hp:=$2;
  2164. $$:=hp;
  2165. if assigned(hp) then
  2166. begin
  2167. s:=strpas(hp^.p);
  2168. if UseCTypesUnit then
  2169. begin
  2170. if s=cint_STR then
  2171. s:=csint_STR
  2172. else if s=cshort_STR then
  2173. s:=csshort_STR
  2174. else if s=cchar_STR then
  2175. s:=cschar_STR
  2176. else if s=clong_STR then
  2177. s:=cslong_STR
  2178. else if s=clonglong_STR then
  2179. s:=cslonglong_STR
  2180. else if s=cint8_STR then
  2181. s:=cint8_STR
  2182. else if s=cint16_STR then
  2183. s:=cint16_STR
  2184. else if s=cint32_STR then
  2185. s:=cint32_STR
  2186. else if s=cint64_STR then
  2187. s:=cint64_STR
  2188. else
  2189. s:='';
  2190. end
  2191. else
  2192. begin
  2193. if s=UINT_STR then
  2194. s:=INT_STR
  2195. else if s=USHORT_STR then
  2196. s:=SHORT_STR
  2197. else if s=USMALL_STR then
  2198. s:=SMALL_STR
  2199. else if s=UCHAR_STR then
  2200. s:=CHAR_STR
  2201. else if s=QWORD_STR then
  2202. s:=INT64_STR
  2203. else
  2204. s:='';
  2205. end;
  2206. if s<>'' then
  2207. hp^.setstr(s);
  2208. end;
  2209. } |
  2210. UNSIGNED special_type_name
  2211. {
  2212. hp:=$2;
  2213. $$:=hp;
  2214. if assigned(hp) then
  2215. begin
  2216. s:=strpas(hp^.p);
  2217. if UseCTypesUnit then
  2218. begin
  2219. if s=cint_STR then
  2220. s:=cuint_STR
  2221. else if s=cshort_STR then
  2222. s:=cushort_STR
  2223. else if s=cchar_STR then
  2224. s:=cuchar_STR
  2225. else if s=clong_STR then
  2226. s:=culong_STR
  2227. else if s=clonglong_STR then
  2228. s:=culonglong_STR
  2229. else if s=cint8_STR then
  2230. s:=cuint8_STR
  2231. else if s=cint16_STR then
  2232. s:=cuint16_STR
  2233. else if s=cint32_STR then
  2234. s:=cuint32_STR
  2235. else if s=cint64_STR then
  2236. s:=cuint64_STR
  2237. else
  2238. s:='';
  2239. end
  2240. else
  2241. begin
  2242. if s=INT_STR then
  2243. s:=UINT_STR
  2244. else if s=SHORT_STR then
  2245. s:=USHORT_STR
  2246. else if s=SMALL_STR then
  2247. s:=USMALL_STR
  2248. else if s=CHAR_STR then
  2249. s:=UCHAR_STR
  2250. else if s=INT64_STR then
  2251. s:=QWORD_STR
  2252. else
  2253. s:='';
  2254. end;
  2255. if s<>'' then
  2256. hp^.setstr(s);
  2257. end;
  2258. } |
  2259. INT
  2260. {
  2261. if UseCTypesUnit then
  2262. $$:=new(presobject,init_id(cint_STR))
  2263. else
  2264. $$:=new(presobject,init_intid(INT_STR));
  2265. } |
  2266. LONG
  2267. {
  2268. if UseCTypesUnit then
  2269. $$:=new(presobject,init_id(clong_STR))
  2270. else
  2271. $$:=new(presobject,init_intid(INT_STR));
  2272. } |
  2273. LONG INT
  2274. {
  2275. if UseCTypesUnit then
  2276. $$:=new(presobject,init_id(clong_STR))
  2277. else
  2278. $$:=new(presobject,init_intid(INT_STR));
  2279. } |
  2280. LONG LONG
  2281. {
  2282. if UseCTypesUnit then
  2283. $$:=new(presobject,init_id(clonglong_STR))
  2284. else
  2285. $$:=new(presobject,init_intid(INT64_STR));
  2286. } |
  2287. LONG LONG INT
  2288. {
  2289. if UseCTypesUnit then
  2290. $$:=new(presobject,init_id(clonglong_STR))
  2291. else
  2292. $$:=new(presobject,init_intid(INT64_STR));
  2293. } |
  2294. SHORT
  2295. {
  2296. if UseCTypesUnit then
  2297. $$:=new(presobject,init_id(cshort_STR))
  2298. else
  2299. $$:=new(presobject,init_intid(SMALL_STR));
  2300. } |
  2301. SHORT INT
  2302. {
  2303. if UseCTypesUnit then
  2304. $$:=new(presobject,init_id(csint_STR))
  2305. else
  2306. $$:=new(presobject,init_intid(SMALL_STR));
  2307. } |
  2308. INT8
  2309. {
  2310. if UseCTypesUnit then
  2311. $$:=new(presobject,init_id(cint8_STR))
  2312. else
  2313. $$:=new(presobject,init_intid(SHORT_STR));
  2314. } |
  2315. INT16
  2316. {
  2317. if UseCTypesUnit then
  2318. $$:=new(presobject,init_id(cint16_STR))
  2319. else
  2320. $$:=new(presobject,init_intid(SMALL_STR));
  2321. } |
  2322. INT32
  2323. {
  2324. if UseCTypesUnit then
  2325. $$:=new(presobject,init_id(cint32_STR))
  2326. else
  2327. $$:=new(presobject,init_intid(INT_STR));
  2328. } |
  2329. INT64
  2330. {
  2331. if UseCTypesUnit then
  2332. $$:=new(presobject,init_id(cint64_STR))
  2333. else
  2334. $$:=new(presobject,init_intid(INT64_STR));
  2335. } |
  2336. FLOAT
  2337. {
  2338. if UseCTypesUnit then
  2339. $$:=new(presobject,init_id(cfloat_STR))
  2340. else
  2341. $$:=new(presobject,init_intid(FLOAT_STR));
  2342. } |
  2343. VOID
  2344. {
  2345. $$:=new(presobject,init_no(t_void));
  2346. } |
  2347. _CHAR
  2348. {
  2349. if UseCTypesUnit then
  2350. $$:=new(presobject,init_id(cchar_STR))
  2351. else
  2352. $$:=new(presobject,init_intid(CHAR_STR));
  2353. } |
  2354. UNSIGNED
  2355. {
  2356. if UseCTypesUnit then
  2357. $$:=new(presobject,init_id(cunsigned_STR))
  2358. else
  2359. $$:=new(presobject,init_intid(UINT_STR));
  2360. }
  2361. ;
  2362. simple_type_name :
  2363. special_type_name
  2364. {
  2365. $$:=$1;
  2366. }
  2367. |
  2368. dname
  2369. {
  2370. $$:=$1;
  2371. tn:=$$^.str;
  2372. if removeunderscore and
  2373. (length(tn)>1) and (tn[1]='_') then
  2374. $$^.setstr(Copy(tn,2,length(tn)-1));
  2375. }
  2376. ;
  2377. declarator_list :
  2378. declarator_list COMMA declarator
  2379. {
  2380. $$:=$1;
  2381. hp:=$1;
  2382. while assigned(hp^.next) do
  2383. hp:=hp^.next;
  2384. hp^.next:=new(presobject,init_one(t_declist,$3));
  2385. }|
  2386. error error_info COMMA declarator_list
  2387. {
  2388. writeln(outfile,' in declarator_list *)');
  2389. $$:=$4;
  2390. yyerrok;
  2391. }|
  2392. error error_info
  2393. {
  2394. writeln(outfile,' in declarator_list *)');
  2395. yyerrok;
  2396. }|
  2397. declarator
  2398. {
  2399. $$:=new(presobject,init_one(t_declist,$1));
  2400. }
  2401. ;
  2402. argument_declaration : type_specifier declarator
  2403. {
  2404. $$:=new(presobject,init_two(t_arg,$1,$2));
  2405. } |
  2406. type_specifier STAR declarator
  2407. {
  2408. (* type_specifier STAR declarator *)
  2409. hp:=new(presobject,init_one(t_pointerdef,$1));
  2410. $$:=new(presobject,init_two(t_arg,hp,$3));
  2411. } |
  2412. type_specifier abstract_declarator
  2413. {
  2414. $$:=new(presobject,init_two(t_arg,$1,$2));
  2415. }
  2416. ;
  2417. argument_declaration_list : argument_declaration
  2418. {
  2419. $$:=new(presobject,init_two(t_arglist,$1,nil));
  2420. } |
  2421. argument_declaration COMMA argument_declaration_list
  2422. {
  2423. $$:=new(presobject,init_two(t_arglist,$1,nil));
  2424. $$^.next:=$3;
  2425. } |
  2426. ELLIPSIS
  2427. {
  2428. $$:=new(presobject,init_two(t_arglist,ellipsisarg,nil));
  2429. } |
  2430. {
  2431. $$:=nil;
  2432. }
  2433. ;
  2434. size_overrider :
  2435. _FAR
  2436. { $$:=new(presobject,init_id('far'));}
  2437. | _NEAR
  2438. { $$:=new(presobject,init_id('near'));}
  2439. | _HUGE
  2440. { $$:=new(presobject,init_id('huge'));}
  2441. ;
  2442. declarator :
  2443. _CONST declarator
  2444. {
  2445. if not stripinfo then
  2446. writeln(outfile,'(* Const before declarator ignored *)');
  2447. $$:=$2;
  2448. } |
  2449. size_overrider STAR declarator
  2450. {
  2451. if not stripinfo then
  2452. writeln(outfile,aktspace,'(* ',$1^.p,' ignored *)');
  2453. dispose($1,done);
  2454. hp:=$3;
  2455. $$:=hp;
  2456. while assigned(hp^.p1) do
  2457. hp:=hp^.p1;
  2458. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2459. } |
  2460. STAR declarator
  2461. {
  2462. (* %prec PSTAR this was wrong!! *)
  2463. hp:=$2;
  2464. $$:=hp;
  2465. while assigned(hp^.p1) do
  2466. hp:=hp^.p1;
  2467. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2468. } |
  2469. _AND declarator %prec P_AND
  2470. {
  2471. hp:=$2;
  2472. $$:=hp;
  2473. while assigned(hp^.p1) do
  2474. hp:=hp^.p1;
  2475. hp^.p1:=new(presobject,init_one(t_addrdef,nil));
  2476. } |
  2477. dname COLON expr
  2478. {
  2479. (* size specifier supported *)
  2480. hp:=new(presobject,init_one(t_size_specifier,$3));
  2481. $$:=new(presobject,init_three(t_dec,nil,$1,hp));
  2482. }|
  2483. dname ASSIGN expr
  2484. {
  2485. if not stripinfo then
  2486. writeln(outfile,'(* Warning : default value for ',$1^.p,' ignored *)');
  2487. hp:=new(presobject,init_one(t_default_value,$3));
  2488. $$:=new(presobject,init_three(t_dec,nil,$1,hp));
  2489. }|
  2490. dname
  2491. {
  2492. $$:=new(presobject,init_two(t_dec,nil,$1));
  2493. }|
  2494. declarator LKLAMMER argument_declaration_list RKLAMMER
  2495. {
  2496. hp:=$1;
  2497. $$:=hp;
  2498. while assigned(hp^.p1) do
  2499. hp:=hp^.p1;
  2500. hp^.p1:=new(presobject,init_two(t_procdef,nil,$3));
  2501. } |
  2502. declarator no_arg
  2503. {
  2504. hp:=$1;
  2505. $$:=hp;
  2506. while assigned(hp^.p1) do
  2507. hp:=hp^.p1;
  2508. hp^.p1:=new(presobject,init_two(t_procdef,nil,nil));
  2509. } |
  2510. declarator LECKKLAMMER expr RECKKLAMMER
  2511. {
  2512. hp:=$1;
  2513. $$:=hp;
  2514. while assigned(hp^.p1) do
  2515. hp:=hp^.p1;
  2516. hp^.p1:=new(presobject,init_two(t_arraydef,nil,$3));
  2517. } |
  2518. declarator LECKKLAMMER RECKKLAMMER
  2519. {
  2520. (* this is translated into a pointer *)
  2521. hp:=$1;
  2522. $$:=hp;
  2523. while assigned(hp^.p1) do
  2524. hp:=hp^.p1;
  2525. hp^.p1:=new(presobject,init_two(t_arraydef,nil,nil));
  2526. } |
  2527. LKLAMMER declarator RKLAMMER
  2528. {
  2529. $$:=$2;
  2530. }
  2531. ;
  2532. no_arg : LKLAMMER RKLAMMER |
  2533. LKLAMMER VOID RKLAMMER;
  2534. abstract_declarator :
  2535. _CONST abstract_declarator
  2536. {
  2537. if not stripinfo then
  2538. writeln(outfile,'(* Const before abstract_declarator ignored *)');
  2539. $$:=$2;
  2540. } |
  2541. size_overrider STAR abstract_declarator
  2542. {
  2543. if not stripinfo then
  2544. writeln(outfile,aktspace,'(* ',$1^.p,' ignored *)');
  2545. dispose($1,done);
  2546. hp:=$3;
  2547. $$:=hp;
  2548. while assigned(hp^.p1) do
  2549. hp:=hp^.p1;
  2550. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2551. } |
  2552. STAR abstract_declarator %prec PSTAR
  2553. {
  2554. hp:=$2;
  2555. $$:=hp;
  2556. while assigned(hp^.p1) do
  2557. hp:=hp^.p1;
  2558. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2559. } |
  2560. abstract_declarator LKLAMMER argument_declaration_list RKLAMMER
  2561. {
  2562. hp:=$1;
  2563. $$:=hp;
  2564. while assigned(hp^.p1) do
  2565. hp:=hp^.p1;
  2566. hp^.p1:=new(presobject,init_two(t_procdef,nil,$3));
  2567. } |
  2568. abstract_declarator no_arg
  2569. {
  2570. hp:=$1;
  2571. $$:=hp;
  2572. while assigned(hp^.p1) do
  2573. hp:=hp^.p1;
  2574. hp^.p1:=new(presobject,init_two(t_procdef,nil,nil));
  2575. } |
  2576. abstract_declarator LECKKLAMMER expr RECKKLAMMER
  2577. {
  2578. hp:=$1;
  2579. $$:=hp;
  2580. while assigned(hp^.p1) do
  2581. hp:=hp^.p1;
  2582. hp^.p1:=new(presobject,init_two(t_arraydef,nil,$3));
  2583. } |
  2584. declarator LECKKLAMMER RECKKLAMMER
  2585. {
  2586. (* this is translated into a pointer *)
  2587. hp:=$1;
  2588. $$:=hp;
  2589. while assigned(hp^.p1) do
  2590. hp:=hp^.p1;
  2591. hp^.p1:=new(presobject,init_two(t_arraydef,nil,nil));
  2592. } |
  2593. LKLAMMER abstract_declarator RKLAMMER
  2594. {
  2595. $$:=$2;
  2596. } |
  2597. {
  2598. $$:=new(presobject,init_two(t_dec,nil,nil));
  2599. }
  2600. ;
  2601. expr : shift_expr
  2602. { $$:=$1; }
  2603. ;
  2604. shift_expr :
  2605. expr _ASSIGN expr
  2606. { $$:=new(presobject,init_bop(':=',$1,$3)); }
  2607. | expr EQUAL expr
  2608. { $$:=new(presobject,init_bop('=',$1,$3));}
  2609. | expr UNEQUAL expr
  2610. { $$:=new(presobject,init_bop('<>',$1,$3));}
  2611. | expr GT expr
  2612. { $$:=new(presobject,init_bop('>',$1,$3));}
  2613. | expr GTE expr
  2614. { $$:=new(presobject,init_bop('>=',$1,$3));}
  2615. | expr LT expr
  2616. { $$:=new(presobject,init_bop('<',$1,$3));}
  2617. | expr LTE expr
  2618. { $$:=new(presobject,init_bop('<=',$1,$3));}
  2619. | expr _PLUS expr
  2620. { $$:=new(presobject,init_bop('+',$1,$3));}
  2621. | expr MINUS expr
  2622. { $$:=new(presobject,init_bop('-',$1,$3));}
  2623. | expr STAR expr
  2624. { $$:=new(presobject,init_bop('*',$1,$3));}
  2625. | expr _SLASH expr
  2626. { $$:=new(presobject,init_bop('/',$1,$3));}
  2627. | expr _OR expr
  2628. { $$:=new(presobject,init_bop(' or ',$1,$3));}
  2629. | expr _AND expr
  2630. { $$:=new(presobject,init_bop(' and ',$1,$3));}
  2631. | expr _NOT expr
  2632. { $$:=new(presobject,init_bop(' not ',$1,$3));}
  2633. | expr _SHL expr
  2634. { $$:=new(presobject,init_bop(' shl ',$1,$3));}
  2635. | expr _SHR expr
  2636. { $$:=new(presobject,init_bop(' shr ',$1,$3));}
  2637. | expr QUESTIONMARK colon_expr
  2638. {
  2639. $3^.p1:=$1;
  2640. $$:=$3;
  2641. inc(if_nb);
  2642. $$^.p:=strpnew('if_local'+str(if_nb));
  2643. } |
  2644. unary_expr {$$:=$1;}
  2645. ;
  2646. colon_expr : expr COLON expr
  2647. { (* if A then B else C *)
  2648. $$:=new(presobject,init_three(t_ifexpr,nil,$1,$3));}
  2649. ;
  2650. maybe_empty_unary_expr :
  2651. unary_expr
  2652. { $$:=$1; }
  2653. |
  2654. { $$:=nil;}
  2655. ;
  2656. unary_expr:
  2657. dname
  2658. {
  2659. $$:=$1;
  2660. } |
  2661. special_type_name
  2662. {
  2663. $$:=$1;
  2664. } |
  2665. CSTRING
  2666. {
  2667. (* remove L prefix for widestrings *)
  2668. s:=act_token;
  2669. if Win32headers and (s[1]='L') then
  2670. delete(s,1,1);
  2671. $$:=new(presobject,init_id(''''+copy(s,2,length(s)-2)+''''));
  2672. } |
  2673. NUMBER
  2674. {
  2675. $$:=new(presobject,init_id(act_token));
  2676. } |
  2677. unary_expr POINT expr
  2678. {
  2679. $$:=new(presobject,init_bop('.',$1,$3));
  2680. } |
  2681. unary_expr DEREF expr
  2682. {
  2683. $$:=new(presobject,init_bop('^.',$1,$3));
  2684. } |
  2685. MINUS unary_expr
  2686. {
  2687. $$:=new(presobject,init_preop('-',$2));
  2688. }|
  2689. _PLUS unary_expr
  2690. {
  2691. $$:=new(presobject,init_preop('+',$2));
  2692. }|
  2693. _AND unary_expr %prec R_AND
  2694. {
  2695. $$:=new(presobject,init_preop('@',$2));
  2696. }|
  2697. _NOT unary_expr
  2698. {
  2699. $$:=new(presobject,init_preop(' not ',$2));
  2700. } |
  2701. LKLAMMER dname RKLAMMER maybe_empty_unary_expr
  2702. {
  2703. if assigned($4) then
  2704. $$:=new(presobject,init_two(t_typespec,$2,$4))
  2705. else
  2706. $$:=$2;
  2707. } |
  2708. LKLAMMER type_specifier RKLAMMER unary_expr
  2709. {
  2710. $$:=new(presobject,init_two(t_typespec,$2,$4));
  2711. } |
  2712. LKLAMMER type_specifier STAR RKLAMMER unary_expr
  2713. {
  2714. hp:=new(presobject,init_one(t_pointerdef,$2));
  2715. $$:=new(presobject,init_two(t_typespec,hp,$5));
  2716. } |
  2717. LKLAMMER type_specifier size_overrider STAR RKLAMMER unary_expr
  2718. {
  2719. if not stripinfo then
  2720. writeln(outfile,aktspace,'(* ',$3^.p,' ignored *)');
  2721. dispose($3,done);
  2722. write_type_specifier(outfile,$2);
  2723. writeln(outfile,' ignored *)');
  2724. hp:=new(presobject,init_one(t_pointerdef,$2));
  2725. $$:=new(presobject,init_two(t_typespec,hp,$6));
  2726. } |
  2727. dname LKLAMMER exprlist RKLAMMER
  2728. {
  2729. hp:=new(presobject,init_one(t_exprlist,$1));
  2730. $$:=new(presobject,init_three(t_funexprlist,hp,$3,nil));
  2731. } |
  2732. LKLAMMER shift_expr RKLAMMER
  2733. {
  2734. $$:=$2;
  2735. } |
  2736. LKLAMMER STAR unary_expr RKLAMMER maybe_space LKLAMMER exprlist RKLAMMER
  2737. {
  2738. $$:=new(presobject,init_two(t_callop,$3,$7));
  2739. } |
  2740. dname LECKKLAMMER exprlist RECKKLAMMER
  2741. {
  2742. $$:=new(presobject,init_two(t_arrayop,$1,$3));
  2743. }
  2744. ;
  2745. enum_list :
  2746. enum_element COMMA enum_list
  2747. { (*enum_element COMMA enum_list *)
  2748. $$:=$1;
  2749. $$^.next:=$3;
  2750. } |
  2751. enum_element {
  2752. $$:=$1;
  2753. } |
  2754. {(* empty enum list *)
  2755. $$:=nil;};
  2756. enum_element :
  2757. dname _ASSIGN expr
  2758. { begin (*enum_element: dname _ASSIGN expr *)
  2759. $$:=new(presobject,init_two(t_enumlist,$1,$3));
  2760. end;
  2761. } |
  2762. dname
  2763. {
  2764. begin (*enum_element: dname*)
  2765. $$:=new(presobject,init_two(t_enumlist,$1,nil));
  2766. end;
  2767. };
  2768. def_expr :
  2769. unary_expr
  2770. {
  2771. if $1^.typ=t_funexprlist then
  2772. $$:=$1
  2773. else
  2774. $$:=new(presobject,init_two(t_exprlist,$1,nil));
  2775. (* if here is a type specifier
  2776. we know the return type *)
  2777. if ($1^.typ=t_typespec) then
  2778. $$^.p3:=$1^.p1^.get_copy;
  2779. }
  2780. ;
  2781. para_def_expr :
  2782. SPACE_DEFINE def_expr
  2783. {
  2784. $$:=$2;
  2785. } |
  2786. maybe_space LKLAMMER def_expr RKLAMMER
  2787. {
  2788. $$:=$3
  2789. }
  2790. ;
  2791. exprlist : exprelem COMMA exprlist
  2792. { (*exprlist COMMA expr*)
  2793. $$:=$1;
  2794. $1^.next:=$3;
  2795. } |
  2796. exprelem
  2797. {
  2798. $$:=$1;
  2799. } |
  2800. { (* empty expression list *)
  2801. $$:=nil; };
  2802. exprelem :
  2803. expr
  2804. {
  2805. $$:=new(presobject,init_one(t_exprlist,$1));
  2806. };
  2807. %%
  2808. function yylex : Integer;
  2809. begin
  2810. yylex:=scan.yylex;
  2811. line_no:=yylineno;
  2812. end;
  2813. procedure WriteFileHeader(var headerfile: Text);
  2814. var
  2815. i: integer;
  2816. originalstr: string;
  2817. begin
  2818. { write unit header }
  2819. if not includefile then
  2820. begin
  2821. if createdynlib then
  2822. writeln(headerfile,'{$mode objfpc}');
  2823. writeln(headerfile,'unit ',unitname,';');
  2824. writeln(headerfile,'interface');
  2825. writeln(headerfile);
  2826. if UseCTypesUnit then
  2827. begin
  2828. writeln(headerfile,'uses');
  2829. writeln(headerfile,' ctypes;');
  2830. writeln(headerfile);
  2831. end;
  2832. writeln(headerfile,'{');
  2833. writeln(headerfile,' Automatically converted by H2Pas ',version,' from ',inputfilename);
  2834. writeln(headerfile,' The following command line parameters were used:');
  2835. for i:=1 to paramcount do
  2836. writeln(headerfile,' ',paramstr(i));
  2837. writeln(headerfile,'}');
  2838. writeln(headerfile);
  2839. end;
  2840. if UseName then
  2841. begin
  2842. writeln(headerfile,aktspace,'const');
  2843. writeln(headerfile,aktspace,' External_library=''',libfilename,'''; {Setup as you need}');
  2844. writeln(headerfile);
  2845. end;
  2846. if UsePPointers then
  2847. begin
  2848. Writeln(headerfile,aktspace,'{ Pointers to basic pascal types, inserted by h2pas conversion program.}');
  2849. Writeln(headerfile,aktspace,'Type');
  2850. Writeln(headerfile,aktspace,' PLongint = ^Longint;');
  2851. Writeln(headerfile,aktspace,' PSmallInt = ^SmallInt;');
  2852. Writeln(headerfile,aktspace,' PByte = ^Byte;');
  2853. Writeln(headerfile,aktspace,' PWord = ^Word;');
  2854. Writeln(headerfile,aktspace,' PDWord = ^DWord;');
  2855. Writeln(headerfile,aktspace,' PDouble = ^Double;');
  2856. Writeln(headerfile);
  2857. end;
  2858. if PTypeList.count <> 0 then
  2859. Writeln(headerfile,aktspace,'Type');
  2860. for i:=0 to (PTypeList.Count-1) do
  2861. begin
  2862. originalstr:=copy(PTypelist[i],2,length(PTypeList[i]));
  2863. Writeln(headerfile,aktspace,PTypeList[i],' = ^',originalstr,';');
  2864. end;
  2865. if not packrecords then
  2866. begin
  2867. writeln(headerfile,'{$IFDEF FPC}');
  2868. writeln(headerfile,'{$PACKRECORDS C}');
  2869. writeln(headerfile,'{$ENDIF}');
  2870. end;
  2871. writeln(headerfile);
  2872. end;
  2873. var
  2874. SS : string;
  2875. i : longint;
  2876. headerfile: Text;
  2877. finaloutfile: Text;
  2878. begin
  2879. pointerprefix:=false;
  2880. { Initialize }
  2881. PTypeList:=TStringList.Create;
  2882. PTypeList.Sorted := true;
  2883. PTypeList.Duplicates := dupIgnore;
  2884. freedynlibproc:=TStringList.Create;
  2885. loaddynlibproc:=TStringList.Create;
  2886. yydebug:=true;
  2887. aktspace:='';
  2888. block_type:=bt_no;
  2889. IsExtern:=false;
  2890. { Read commandline options }
  2891. ProcessOptions;
  2892. if not CompactMode then
  2893. aktspace:=' ';
  2894. { open input and output files }
  2895. assign(yyinput, inputfilename);
  2896. {$I-}
  2897. reset(yyinput);
  2898. {$I+}
  2899. if ioresult<>0 then
  2900. begin
  2901. writeln('file ',inputfilename,' not found!');
  2902. halt(1);
  2903. end;
  2904. { This is the intermediate output file }
  2905. assign(outfile, 'ext3.tmp');
  2906. {$I-}
  2907. rewrite(outfile);
  2908. {$I+}
  2909. if ioresult<>0 then
  2910. begin
  2911. writeln('file ext3.tmp could not be created!');
  2912. halt(1);
  2913. end;
  2914. writeln(outfile);
  2915. { Open tempfiles }
  2916. { This is where the implementation section of the unit shall be stored }
  2917. Assign(implemfile,'ext.tmp');
  2918. rewrite(implemfile);
  2919. Assign(tempfile,'ext2.tmp');
  2920. rewrite(tempfile);
  2921. { Parse! }
  2922. yyparse;
  2923. { Write implementation if needed }
  2924. if not(includefile) then
  2925. begin
  2926. writeln(outfile);
  2927. writeln(outfile,'implementation');
  2928. writeln(outfile);
  2929. end;
  2930. { here we have a problem if a line is longer than 255 chars !! }
  2931. reset(implemfile);
  2932. while not eof(implemfile) do
  2933. begin
  2934. readln(implemfile,SS);
  2935. writeln(outfile,SS);
  2936. end;
  2937. if createdynlib then
  2938. begin
  2939. writeln(outfile,' uses');
  2940. writeln(outfile,' SysUtils, dynlibs;');
  2941. writeln(outfile);
  2942. writeln(outfile,' var');
  2943. writeln(outfile,' hlib : tlibhandle;');
  2944. writeln(outfile);
  2945. writeln(outfile);
  2946. writeln(outfile,' procedure Free',unitname,';');
  2947. writeln(outfile,' begin');
  2948. writeln(outfile,' FreeLibrary(hlib);');
  2949. for i:=0 to (freedynlibproc.Count-1) do
  2950. Writeln(outfile,' ',freedynlibproc[i]);
  2951. writeln(outfile,' end;');
  2952. writeln(outfile);
  2953. writeln(outfile);
  2954. writeln(outfile,' procedure Load',unitname,'(lib : pchar);');
  2955. writeln(outfile,' begin');
  2956. writeln(outfile,' Free',unitname,';');
  2957. writeln(outfile,' hlib:=LoadLibrary(lib);');
  2958. writeln(outfile,' if hlib=0 then');
  2959. writeln(outfile,' raise Exception.Create(format(''Could not load library: %s'',[lib]));');
  2960. writeln(outfile);
  2961. for i:=0 to (loaddynlibproc.Count-1) do
  2962. Writeln(outfile,' ',loaddynlibproc[i]);
  2963. writeln(outfile,' end;');
  2964. writeln(outfile);
  2965. writeln(outfile);
  2966. writeln(outfile,'initialization');
  2967. writeln(outfile,' Load',unitname,'(''',unitname,''');');
  2968. writeln(outfile,'finalization');
  2969. writeln(outfile,' Free',unitname,';');
  2970. end;
  2971. { write end of file }
  2972. writeln(outfile);
  2973. if not(includefile) then
  2974. writeln(outfile,'end.');
  2975. { close and erase tempfiles }
  2976. close(implemfile);
  2977. erase(implemfile);
  2978. close(tempfile);
  2979. erase(tempfile);
  2980. flush(outfile);
  2981. {**** generate full file ****}
  2982. assign(headerfile, 'ext4.tmp');
  2983. {$I-}
  2984. rewrite(headerfile);
  2985. {$I+}
  2986. if ioresult<>0 then
  2987. begin
  2988. writeln('file ext4.tmp could not be created!');
  2989. halt(1);
  2990. end;
  2991. WriteFileHeader(HeaderFile);
  2992. { Final output filename }
  2993. assign(finaloutfile, outputfilename);
  2994. {$I-}
  2995. rewrite(finaloutfile);
  2996. {$I+}
  2997. if ioresult<>0 then
  2998. begin
  2999. writeln('file ',outputfilename,' could not be created!');
  3000. halt(1);
  3001. end;
  3002. writeln(finaloutfile);
  3003. { Read unit header file }
  3004. reset(headerfile);
  3005. while not eof(headerfile) do
  3006. begin
  3007. readln(headerfile,SS);
  3008. writeln(finaloutfile,SS);
  3009. end;
  3010. { Read interface and implementation file }
  3011. reset(outfile);
  3012. while not eof(outfile) do
  3013. begin
  3014. readln(outfile,SS);
  3015. writeln(finaloutfile,SS);
  3016. end;
  3017. close(HeaderFile);
  3018. close(outfile);
  3019. close(finaloutfile);
  3020. erase(outfile);
  3021. erase(headerfile);
  3022. PTypeList.Free;
  3023. freedynlibproc.free;
  3024. loaddynlibproc.free;
  3025. end.