h2pas.y 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466
  1. %{
  2. program h2pas;
  3. (*
  4. $Id$
  5. Copyright (c) 1998-2000 by Florian Klaempfl
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************)
  18. uses
  19. {$ifdef Delphi}
  20. SysUtils,
  21. {$else Delphi}
  22. strings,
  23. {$endif Delphi}
  24. options,scan,converu,lexlib,yacclib;
  25. type
  26. YYSTYPE = presobject;
  27. const
  28. SHORT_STR = 'smallint';
  29. USHORT_STR = 'word';
  30. INT_STR = 'longint';
  31. UINT_STR = 'dword';
  32. CHAR_STR = 'char';
  33. UCHAR_STR = 'byte'; { should we use byte or char for 'unsigned char' ?? }
  34. INT64_STR = 'int64';
  35. QWORD_STR = 'qword';
  36. REAL_STR = 'double';
  37. WCHAR_STR = 'widechar';
  38. var
  39. hp,ph : presobject;
  40. extfile : text; (* file for implementation headers extern procs *)
  41. IsExtern : boolean;
  42. NeedEllipsisOverload : boolean;
  43. must_write_packed_field : boolean;
  44. tempfile : text;
  45. No_pop : boolean;
  46. s,TN,PN : String;
  47. (* $ define yydebug
  48. compile with -dYYDEBUG to get debugging info *)
  49. const
  50. (* number of a?b:c construction in one define *)
  51. if_nb : longint = 0;
  52. is_packed : boolean = false;
  53. is_procvar : boolean = false;
  54. var space_array : array [0..255] of byte;
  55. space_index : byte;
  56. procedure shift(space_number : byte);
  57. var
  58. i : byte;
  59. begin
  60. space_array[space_index]:=space_number;
  61. inc(space_index);
  62. for i:=1 to space_number do
  63. aktspace:=aktspace+' ';
  64. end;
  65. procedure popshift;
  66. begin
  67. dec(space_index);
  68. if space_index<0 then
  69. internalerror(20);
  70. delete(aktspace,1,space_array[space_index]);
  71. end;
  72. function str(i : longint) : string;
  73. var
  74. s : string;
  75. begin
  76. system.str(i,s);
  77. str:=s;
  78. end;
  79. function hexstr(i : cardinal) : string;
  80. const
  81. HexTbl : array[0..15] of char='0123456789ABCDEF';
  82. var
  83. str : string;
  84. begin
  85. str:='';
  86. while i<>0 do
  87. begin
  88. str:=hextbl[i and $F]+str;
  89. i:=i shr 4;
  90. end;
  91. if str='' then str:='0';
  92. hexstr:='$'+str;
  93. end;
  94. function uppercase(s : string) : string;
  95. var
  96. i : byte;
  97. begin
  98. for i:=1 to length(s) do
  99. s[i]:=UpCase(s[i]);
  100. uppercase:=s;
  101. end;
  102. procedure write_type_specifier(var outfile:text; p : presobject);forward;
  103. procedure write_p_a_def(var outfile:text; p,simple_type : presobject);forward;
  104. procedure write_ifexpr(var outfile:text; p : presobject);forward;
  105. procedure write_funexpr(var outfile:text; p : presobject);forward;
  106. procedure yymsg(const msg : string);
  107. begin
  108. writeln('line ',line_no,': ',msg);
  109. end;
  110. function FixId(const s:string):string;
  111. var
  112. b : boolean;
  113. up : string;
  114. begin
  115. if s='' then
  116. begin
  117. FixId:='';
  118. exit;
  119. end;
  120. b:=false;
  121. up:=Uppercase(s);
  122. case up[1] of
  123. 'C' : b:=(up='CLASS');
  124. 'D' : b:=(up='DISPOSE');
  125. 'F' : b:=(up='FUNCTION') or (up='FALSE');
  126. 'N' : b:=(up='NEW');
  127. 'P' : b:=(up='PROPERTY') or (up='PROCEDURE');
  128. 'R' : b:=(up='RECORD') or (up='REPEAT');
  129. 'S' : b:=(up='STRING');
  130. 'T' : b:=(up='TYPE') or (up='TRUE');
  131. 'U' : b:=(up='UNTIL');
  132. end;
  133. if b then
  134. FixId:='_'+s
  135. else
  136. FixId:=s;
  137. end;
  138. function TypeName(const s:string):string;
  139. var
  140. i : longint;
  141. begin
  142. i:=1;
  143. if RemoveUnderScore and (length(s)>1) and (s[1]='_') then
  144. i:=2;
  145. if PrependTypes then
  146. TypeName:='T'+Copy(s,i,255)
  147. else
  148. TypeName:=Copy(s,i,255);
  149. end;
  150. function PointerName(const s:string):string;
  151. var
  152. i : longint;
  153. begin
  154. i:=1;
  155. if RemoveUnderScore and (length(s)>1) and (s[1]='_') then
  156. i:=2;
  157. if UsePPointers then
  158. PointerName:='P'+Copy(s,i,255)
  159. else
  160. PointerName:=Copy(s,i,255);
  161. end;
  162. procedure write_packed_fields_info(var outfile:text; p : presobject; ph : string);
  163. var
  164. hp1,hp2,hp3 : presobject;
  165. is_sized : boolean;
  166. line : string;
  167. flag_index : longint;
  168. name : pchar;
  169. ps : byte;
  170. begin
  171. { write out the tempfile created }
  172. close(tempfile);
  173. reset(tempfile);
  174. is_sized:=false;
  175. flag_index:=0;
  176. writeln(outfile,aktspace,'const');
  177. shift(3);
  178. while not eof(tempfile) do
  179. begin
  180. readln(tempfile,line);
  181. ps:=pos('&',line);
  182. if ps>0 then
  183. line:=copy(line,1,ps-1)+ph+'_'+copy(line,ps+1,255);
  184. writeln(outfile,aktspace,line);
  185. end;
  186. close(tempfile);
  187. rewrite(tempfile);
  188. popshift;
  189. (* walk through all members *)
  190. hp1 := p^.p1;
  191. while assigned(hp1) do
  192. begin
  193. (* hp2 is t_memberdec *)
  194. hp2:=hp1^.p1;
  195. (* hp3 is t_declist *)
  196. hp3:=hp2^.p2;
  197. while assigned(hp3) do
  198. begin
  199. if assigned(hp3^.p1^.p3) and
  200. (hp3^.p1^.p3^.typ = t_size_specifier) then
  201. begin
  202. is_sized:=true;
  203. name:=hp3^.p1^.p2^.p;
  204. { get function in interface }
  205. write(outfile,aktspace,'function ',name);
  206. write(outfile,'(var a : ',ph,') : ');
  207. shift(2);
  208. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  209. writeln(outfile,';');
  210. popshift;
  211. { get function in implementation }
  212. write(extfile,aktspace,'function ',name);
  213. write(extfile,'(var a : ',ph,') : ');
  214. if not compactmode then
  215. shift(2);
  216. write_p_a_def(extfile,hp3^.p1^.p1,hp2^.p1);
  217. writeln(extfile,';');
  218. writeln(extfile,aktspace,'begin');
  219. shift(3);
  220. write(extfile,aktspace,name,':=(a.flag',flag_index);
  221. writeln(extfile,' and bm_',ph,'_',name,') shr bp_',ph,'_',name,';');
  222. popshift;
  223. writeln(extfile,aktspace,'end;');
  224. if not compactmode then
  225. popshift;
  226. writeln(extfile,'');
  227. { set function in interface }
  228. write(outfile,aktspace,'procedure set_',name);
  229. write(outfile,'(var a : ',ph,'; __',name,' : ');
  230. shift(2);
  231. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  232. writeln(outfile,');');
  233. popshift;
  234. { set function in implementation }
  235. write(extfile,aktspace,'procedure set_',name);
  236. write(extfile,'(var a : ',ph,'; __',name,' : ');
  237. if not compactmode then
  238. shift(2);
  239. write_p_a_def(extfile,hp3^.p1^.p1,hp2^.p1);
  240. writeln(extfile,');');
  241. writeln(extfile,aktspace,'begin');
  242. shift(3);
  243. write(extfile,aktspace,'a.flag',flag_index,':=');
  244. write(extfile,'a.flag',flag_index,' or ');
  245. writeln(extfile,'((__',name,' shl bp_',ph,'_',name,') and bm_',ph,'_',name,');');
  246. popshift;
  247. writeln(extfile,aktspace,'end;');
  248. if not compactmode then
  249. popshift;
  250. writeln(extfile,'');
  251. end
  252. else if is_sized then
  253. begin
  254. is_sized:=false;
  255. inc(flag_index);
  256. end;
  257. hp3:=hp3^.next;
  258. end;
  259. hp1:=hp1^.next;
  260. end;
  261. must_write_packed_field:=false;
  262. block_type:=bt_no;
  263. end;
  264. procedure write_expr(var outfile:text; p : presobject);
  265. begin
  266. if assigned(p) then
  267. begin
  268. case p^.typ of
  269. t_id,
  270. t_ifexpr :
  271. write(outfile,FixId(p^.p));
  272. t_funexprlist :
  273. write_funexpr(outfile,p);
  274. t_exprlist :
  275. begin
  276. if assigned(p^.p1) then
  277. write_expr(outfile,p^.p1);
  278. if assigned(p^.next) then
  279. begin
  280. write(', ');
  281. write_expr(outfile,p^.next);
  282. end;
  283. end;
  284. t_preop : begin
  285. write(outfile,p^.p,'(');
  286. write_expr(outfile,p^.p1);
  287. write(outfile,')');
  288. flush(outfile);
  289. end;
  290. t_typespec : begin
  291. write_type_specifier(outfile,p^.p1);
  292. write(outfile,'(');
  293. write_expr(outfile,p^.p2);
  294. write(outfile,')');
  295. flush(outfile);
  296. end;
  297. t_bop : begin
  298. if p^.p1^.typ<>t_id then
  299. write(outfile,'(');
  300. write_expr(outfile,p^.p1);
  301. if p^.p1^.typ<>t_id then
  302. write(outfile,')');
  303. write(outfile,p^.p);
  304. if p^.p2^.typ<>t_id then
  305. write(outfile,'(');
  306. write_expr(outfile,p^.p2);
  307. if p^.p2^.typ<>t_id then
  308. write(outfile,')');
  309. flush(outfile);
  310. end;
  311. t_arrayop :
  312. begin
  313. write_expr(outfile,p^.p1);
  314. write(outfile,p^.p,'[');
  315. write_expr(outfile,p^.p2);
  316. write(outfile,']');
  317. flush(outfile);
  318. end;
  319. t_callop :
  320. begin
  321. write_expr(outfile,p^.p1);
  322. write(outfile,p^.p,'(');
  323. write_expr(outfile,p^.p2);
  324. write(outfile,')');
  325. flush(outfile);
  326. end;
  327. else internalerror(2);
  328. end;
  329. end;
  330. end;
  331. procedure write_ifexpr(var outfile:text; p : presobject);
  332. begin
  333. flush(outfile);
  334. write(outfile,'if ');
  335. write_expr(outfile,p^.p1);
  336. writeln(outfile,' then');
  337. write(outfile,aktspace,' ');
  338. write(outfile,p^.p);
  339. write(outfile,':=');
  340. write_expr(outfile,p^.p2);
  341. writeln(outfile);
  342. writeln(outfile,aktspace,'else');
  343. write(outfile,aktspace,' ');
  344. write(outfile,p^.p);
  345. write(outfile,':=');
  346. write_expr(outfile,p^.p3);
  347. writeln(outfile,';');
  348. write(outfile,aktspace);
  349. flush(outfile);
  350. end;
  351. procedure write_all_ifexpr(var outfile:text; p : presobject);
  352. begin
  353. if assigned(p) then
  354. begin
  355. case p^.typ of
  356. t_id :;
  357. t_preop :
  358. write_all_ifexpr(outfile,p^.p1);
  359. t_callop,
  360. t_arrayop,
  361. t_bop :
  362. begin
  363. write_all_ifexpr(outfile,p^.p1);
  364. write_all_ifexpr(outfile,p^.p2);
  365. end;
  366. t_ifexpr :
  367. begin
  368. write_all_ifexpr(outfile,p^.p1);
  369. write_all_ifexpr(outfile,p^.p2);
  370. write_all_ifexpr(outfile,p^.p3);
  371. write_ifexpr(outfile,p);
  372. end;
  373. t_typespec :
  374. write_all_ifexpr(outfile,p^.p2);
  375. t_funexprlist,
  376. t_exprlist :
  377. begin
  378. if assigned(p^.p1) then
  379. write_all_ifexpr(outfile,p^.p1);
  380. if assigned(p^.next) then
  381. write_all_ifexpr(outfile,p^.next);
  382. end
  383. else
  384. internalerror(6);
  385. end;
  386. end;
  387. end;
  388. procedure write_funexpr(var outfile:text; p : presobject);
  389. var
  390. i : longint;
  391. begin
  392. if assigned(p) then
  393. begin
  394. case p^.typ of
  395. t_ifexpr :
  396. write(outfile,p^.p);
  397. t_exprlist :
  398. begin
  399. write_expr(outfile,p^.p1);
  400. if assigned(p^.next) then
  401. begin
  402. write(outfile,',');
  403. write_funexpr(outfile,p^.next);
  404. end
  405. end;
  406. t_funcname :
  407. begin
  408. if not compactmode then
  409. shift(2);
  410. if if_nb>0 then
  411. begin
  412. writeln(outfile,aktspace,'var');
  413. write(outfile,aktspace,' ');
  414. for i:=1 to if_nb do
  415. begin
  416. write(outfile,'if_local',i);
  417. if i<if_nb then
  418. write(outfile,', ')
  419. else
  420. writeln(outfile,' : longint;');
  421. end;
  422. writeln(outfile,aktspace,'(* result types are not known *)');
  423. if_nb:=0;
  424. end;
  425. writeln(outfile,aktspace,'begin');
  426. shift(3);
  427. write(outfile,aktspace);
  428. write_all_ifexpr(outfile,p^.p2);
  429. write_expr(outfile,p^.p1);
  430. write(outfile,':=');
  431. write_funexpr(outfile,p^.p2);
  432. writeln(outfile,';');
  433. popshift;
  434. writeln(outfile,aktspace,'end;');
  435. if not compactmode then
  436. popshift;
  437. flush(outfile);
  438. end;
  439. t_funexprlist :
  440. begin
  441. if assigned(p^.p3) then
  442. begin
  443. write_type_specifier(outfile,p^.p3);
  444. write(outfile,'(');
  445. end;
  446. if assigned(p^.p1) then
  447. write_funexpr(outfile,p^.p1);
  448. if assigned(p^.p2) then
  449. begin
  450. write(outfile,'(');
  451. write_funexpr(outfile,p^.p2);
  452. write(outfile,')');
  453. end;
  454. if assigned(p^.p3) then
  455. write(outfile,')');
  456. end
  457. else internalerror(5);
  458. end;
  459. end;
  460. end;
  461. function ellipsisarg : presobject;
  462. begin
  463. ellipsisarg:=new(presobject,init_two(t_arg,nil,nil));
  464. end;
  465. const
  466. (* if in args *dname is replaced by pdname *)
  467. in_args : boolean = false;
  468. typedef_level : longint = 0;
  469. (* writes an argument list, where p is t_arglist *)
  470. procedure write_args(var outfile:text; p : presobject);
  471. var
  472. len,para : longint;
  473. old_in_args : boolean;
  474. varpara : boolean;
  475. lastp : presobject;
  476. hs : string;
  477. begin
  478. NeedEllipsisOverload:=false;
  479. para:=1;
  480. len:=0;
  481. lastp:=nil;
  482. old_in_args:=in_args;
  483. in_args:=true;
  484. write(outfile,'(');
  485. shift(2);
  486. (* walk through all arguments *)
  487. (* p must be of type t_arglist *)
  488. while assigned(p) do
  489. begin
  490. if p^.typ<>t_arglist then
  491. internalerror(10);
  492. (* is ellipsis ? *)
  493. if not assigned(p^.p1^.p1) and
  494. not assigned(p^.p1^.next) then
  495. begin
  496. write(outfile,'args:array of const');
  497. (* if variable number of args we must allways pop *)
  498. no_pop:=false;
  499. (* Needs 2 declarations, also one without args, becuase
  500. in C you can omit the second parameter. Default parameter
  501. doesn't help as that isn't possible with array of const *)
  502. NeedEllipsisOverload:=true;
  503. (* Remove this para *)
  504. if assigned(lastp) then
  505. lastp^.next:=nil;
  506. dispose(p,done);
  507. (* leave the loop as p isnot valid anymore *)
  508. break;
  509. end
  510. (* we need to correct this in the pp file after *)
  511. else
  512. begin
  513. (* generate a call by reference parameter ? *)
  514. varpara:=usevarparas and
  515. assigned(p^.p1^.p2^.p1) and
  516. (p^.p1^.p2^.p1^.typ in [t_addrdef,t_pointerdef]) and
  517. assigned(p^.p1^.p2^.p1^.p1) and
  518. (p^.p1^.p2^.p1^.p1^.typ<>t_procdef);
  519. (* do not do it for char pointer !! *)
  520. (* para : pchar; and var para : char; are *)
  521. (* completely different in pascal *)
  522. (* here we exclude all typename containing char *)
  523. (* is this a good method ?? *)
  524. if varpara and
  525. (p^.p1^.p2^.p1^.typ=t_pointerdef) and
  526. (p^.p1^.p2^.p1^.p1^.typ=t_id) and
  527. (pos('CHAR',uppercase(p^.p1^.p2^.p1^.p1^.str))<>0) then
  528. varpara:=false;
  529. if varpara then
  530. begin
  531. write(outfile,'var ');
  532. inc(len,4);
  533. end;
  534. (* write new parameter name *)
  535. if assigned(p^.p1^.p2^.p2) then
  536. begin
  537. hs:=FixId(p^.p1^.p2^.p2^.p);
  538. write(outfile,hs);
  539. inc(len,length(hs));
  540. end
  541. else
  542. begin
  543. If removeUnderscore then
  544. begin
  545. Write (outfile,'para',para);
  546. inc(Len,5);
  547. end
  548. else
  549. begin
  550. write(outfile,'_para',para);
  551. inc(Len,6);
  552. end;
  553. end;
  554. write(outfile,':');
  555. if varpara then
  556. write_p_a_def(outfile,p^.p1^.p2^.p1^.p1,p^.p1^.p1)
  557. else
  558. write_p_a_def(outfile,p^.p1^.p2^.p1,p^.p1^.p1);
  559. end;
  560. lastp:=p;
  561. p:=p^.next;
  562. if assigned(p) then
  563. begin
  564. write(outfile,'; ');
  565. { if len>40 then : too complicated to compute }
  566. if (para mod 5) = 0 then
  567. begin
  568. writeln(outfile);
  569. write(outfile,aktspace);
  570. end;
  571. end;
  572. inc(para);
  573. end;
  574. write(outfile,')');
  575. flush(outfile);
  576. in_args:=old_in_args;
  577. popshift;
  578. end;
  579. procedure write_p_a_def(var outfile:text; p,simple_type : presobject);
  580. var
  581. i : longint;
  582. error : integer;
  583. pointerwritten,
  584. constant : boolean;
  585. begin
  586. if not(assigned(p)) then
  587. begin
  588. write_type_specifier(outfile,simple_type);
  589. exit;
  590. end;
  591. case p^.typ of
  592. t_pointerdef : begin
  593. (* procedure variable ? *)
  594. if assigned(p^.p1) and (p^.p1^.typ=t_procdef) then
  595. begin
  596. is_procvar:=true;
  597. (* distinguish between procedure and function *)
  598. if (simple_type^.typ=t_void) and (p^.p1^.p1=nil) then
  599. begin
  600. write(outfile,'procedure ');
  601. shift(10);
  602. (* write arguments *)
  603. if assigned(p^.p1^.p2) then
  604. write_args(outfile,p^.p1^.p2);
  605. flush(outfile);
  606. popshift;
  607. end
  608. else
  609. begin
  610. write(outfile,'function ');
  611. shift(9);
  612. (* write arguments *)
  613. if assigned(p^.p1^.p2) then
  614. write_args(outfile,p^.p1^.p2);
  615. write(outfile,':');
  616. flush(outfile);
  617. write_p_a_def(outfile,p^.p1^.p1,simple_type);
  618. popshift;
  619. end
  620. end
  621. else
  622. begin
  623. (* generate "pointer" ? *)
  624. if (simple_type^.typ=t_void) and (p^.p1=nil) then
  625. begin
  626. write(outfile,'pointer');
  627. flush(outfile);
  628. end
  629. else
  630. begin
  631. pointerwritten:=false;
  632. if (p^.p1=nil) and UsePPointers then
  633. begin
  634. if (simple_type^.typ=t_id) then
  635. begin
  636. write(outfile,PointerName(simple_type^.p));
  637. pointerwritten:=true;
  638. end
  639. { structure }
  640. else if (simple_type^.typ in [t_uniondef,t_structdef]) and
  641. (simple_type^.p1=nil) and (simple_type^.p2^.typ=t_id) then
  642. begin
  643. write(outfile,PointerName(simple_type^.p2^.p));
  644. pointerwritten:=true;
  645. end;
  646. end;
  647. if not pointerwritten then
  648. begin
  649. if in_args then
  650. write(outfile,'P')
  651. else
  652. write(outfile,'^');
  653. write_p_a_def(outfile,p^.p1,simple_type);
  654. end;
  655. end;
  656. end;
  657. end;
  658. t_arraydef : begin
  659. constant:=false;
  660. if assigned(p^.p2) then
  661. begin
  662. if p^.p2^.typ=t_id then
  663. begin
  664. val(p^.p2^.str,i,error);
  665. if error=0 then
  666. begin
  667. dec(i);
  668. constant:=true;
  669. end;
  670. end;
  671. if not constant then
  672. begin
  673. write(outfile,'array[0..(');
  674. write_expr(outfile,p^.p2);
  675. write(outfile,')-1] of ');
  676. end
  677. else
  678. begin
  679. write(outfile,'array[0..',i,'] of ');
  680. end;
  681. end
  682. else
  683. begin
  684. (* open array *)
  685. write(outfile,'array of ');
  686. end;
  687. flush(outfile);
  688. write_p_a_def(outfile,p^.p1,simple_type);
  689. end;
  690. else internalerror(1);
  691. end;
  692. end;
  693. procedure write_type_specifier(var outfile:text; p : presobject);
  694. var
  695. hp1,hp2,hp3,lastexpr : presobject;
  696. i,l,w : longint;
  697. error : integer;
  698. current_power,
  699. mask : cardinal;
  700. flag_index : longint;
  701. current_level : byte;
  702. pointerwritten,
  703. is_sized : boolean;
  704. begin
  705. case p^.typ of
  706. t_id :
  707. begin
  708. if p^.intname then
  709. write(outfile,p^.p)
  710. else
  711. write(outfile,TypeName(p^.p));
  712. end;
  713. { what can we do with void defs ? }
  714. t_void :
  715. write(outfile,'void');
  716. t_pointerdef :
  717. begin
  718. pointerwritten:=false;
  719. if (p^.p1^.typ=t_void) then
  720. begin
  721. write(outfile,'pointer');
  722. pointerwritten:=true;
  723. end
  724. else
  725. if UsePPointers then
  726. begin
  727. if (p^.p1^.typ=t_id) then
  728. begin
  729. write(outfile,PointerName(p^.p1^.p));
  730. pointerwritten:=true;
  731. end
  732. { structure }
  733. else if (p^.p1^.typ in [t_uniondef,t_structdef]) and
  734. (p^.p1^.p1=nil) and (p^.p1^.p2^.typ=t_id) then
  735. begin
  736. write(outfile,PointerName(p^.p1^.p2^.p));
  737. pointerwritten:=true;
  738. end;
  739. end;
  740. if not pointerwritten then
  741. begin
  742. if in_args then
  743. write(outfile,'P')
  744. else
  745. write(outfile,'^');
  746. write_type_specifier(outfile,p^.p1);
  747. end;
  748. end;
  749. t_enumdef :
  750. begin
  751. if (typedef_level>1) and (p^.p1=nil) and
  752. (p^.p2^.typ=t_id) then
  753. begin
  754. write(outfile,p^.p2^.p);
  755. end
  756. else
  757. if not EnumToConst then
  758. begin
  759. write(outfile,'(');
  760. hp1:=p^.p1;
  761. w:=length(aktspace);
  762. while assigned(hp1) do
  763. begin
  764. write(outfile,hp1^.p1^.p);
  765. if assigned(hp1^.p2) then
  766. begin
  767. write(outfile,' := ');
  768. write_expr(outfile,hp1^.p2);
  769. w:=w+6;(* strlen(hp1^.p); *)
  770. end;
  771. w:=w+length(hp1^.p1^.str);
  772. hp1:=hp1^.next;
  773. if assigned(hp1) then
  774. write(outfile,',');
  775. if w>40 then
  776. begin
  777. writeln(outfile);
  778. write(outfile,aktspace);
  779. w:=length(aktspace);
  780. end;
  781. flush(outfile);
  782. end;
  783. write(outfile,')');
  784. flush(outfile);
  785. end
  786. else
  787. begin
  788. Writeln (outfile,' Longint;');
  789. hp1:=p^.p1;
  790. l:=0;
  791. lastexpr:=nil;
  792. Writeln (outfile,copy(aktspace,1,length(aktspace)-2),'Const');
  793. while assigned(hp1) do
  794. begin
  795. write (outfile,aktspace,hp1^.p1^.p,' = ');
  796. if assigned(hp1^.p2) then
  797. begin
  798. write_expr(outfile,hp1^.p2);
  799. writeln(outfile,';');
  800. lastexpr:=hp1^.p2;
  801. if lastexpr^.typ=t_id then
  802. begin
  803. val(lastexpr^.str,l,error);
  804. if error=0 then
  805. begin
  806. inc(l);
  807. lastexpr:=nil;
  808. end
  809. else
  810. l:=1;
  811. end
  812. else
  813. l:=1;
  814. end
  815. else
  816. begin
  817. if assigned(lastexpr) then
  818. begin
  819. write(outfile,'(');
  820. write_expr(outfile,lastexpr);
  821. writeln(outfile,')+',l,';');
  822. end
  823. else
  824. writeln (outfile,l,';');
  825. inc(l);
  826. end;
  827. hp1:=hp1^.next;
  828. flush(outfile);
  829. end;
  830. block_type:=bt_const;
  831. end;
  832. end;
  833. t_structdef :
  834. begin
  835. inc(typedef_level);
  836. flag_index:=-1;
  837. is_sized:=false;
  838. current_level:=0;
  839. if ((in_args) or (typedef_level>1)) and
  840. (p^.p1=nil) and (p^.p2^.typ=t_id) then
  841. begin
  842. write(outfile,TypeName(p^.p2^.p));
  843. end
  844. else
  845. begin
  846. writeln(outfile,'record');
  847. shift(3);
  848. hp1:=p^.p1;
  849. (* walk through all members *)
  850. while assigned(hp1) do
  851. begin
  852. (* hp2 is t_memberdec *)
  853. hp2:=hp1^.p1;
  854. (* hp3 is t_declist *)
  855. hp3:=hp2^.p2;
  856. while assigned(hp3) do
  857. begin
  858. if not assigned(hp3^.p1^.p3) or
  859. (hp3^.p1^.p3^.typ <> t_size_specifier) then
  860. begin
  861. if is_sized then
  862. begin
  863. if current_level <= 16 then
  864. writeln(outfile,'word;')
  865. else if current_level <= 32 then
  866. writeln(outfile,'longint;')
  867. else
  868. internalerror(11);
  869. is_sized:=false;
  870. end;
  871. write(outfile,aktspace,FixId(hp3^.p1^.p2^.p));
  872. write(outfile,' : ');
  873. shift(2);
  874. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  875. popshift;
  876. end;
  877. { size specifier or default value ? }
  878. if assigned(hp3^.p1^.p3) then
  879. begin
  880. { we could use mask to implement this }
  881. { because we need to respect the positions }
  882. if hp3^.p1^.p3^.typ = t_size_specifier then
  883. begin
  884. if not is_sized then
  885. begin
  886. current_power:=1;
  887. current_level:=0;
  888. inc(flag_index);
  889. write(outfile,aktspace,'flag',flag_index,' : ');
  890. end;
  891. must_write_packed_field:=true;
  892. is_sized:=true;
  893. { can it be something else than a constant ? }
  894. { it can be a macro !! }
  895. if hp3^.p1^.p3^.p1^.typ=t_id then
  896. begin
  897. val(hp3^.p1^.p3^.p1^.str,l,error);
  898. if error=0 then
  899. begin
  900. mask:=0;
  901. for i:=1 to l do
  902. begin
  903. inc(mask,current_power);
  904. current_power:=current_power*2;
  905. end;
  906. write(tempfile,'bm_&',hp3^.p1^.p2^.p);
  907. writeln(tempfile,' = ',hexstr(mask),';');
  908. write(tempfile,'bp_&',hp3^.p1^.p2^.p);
  909. writeln(tempfile,' = ',current_level,';');
  910. current_level:=current_level + l;
  911. { go to next flag if 31 }
  912. if current_level = 32 then
  913. begin
  914. write(outfile,'longint');
  915. is_sized:=false;
  916. end;
  917. end;
  918. end;
  919. end
  920. else if hp3^.p1^.p3^.typ = t_default_value then
  921. begin
  922. write(outfile,'{=');
  923. write_expr(outfile,hp3^.p1^.p3^.p1);
  924. write(outfile,' ignored}');
  925. end;
  926. end;
  927. if not is_sized then
  928. begin
  929. if is_procvar then
  930. begin
  931. if not no_pop then
  932. begin
  933. write(outfile,';cdecl');
  934. no_pop:=true;
  935. end;
  936. is_procvar:=false;
  937. end;
  938. writeln(outfile,';');
  939. end;
  940. hp3:=hp3^.next;
  941. end;
  942. hp1:=hp1^.next;
  943. end;
  944. if is_sized then
  945. begin
  946. if current_level <= 16 then
  947. writeln(outfile,'word;')
  948. else if current_level <= 32 then
  949. writeln(outfile,'longint;')
  950. else
  951. internalerror(11);
  952. is_sized:=false;
  953. end;
  954. popshift;
  955. write(outfile,aktspace,'end');
  956. flush(outfile);
  957. end;
  958. dec(typedef_level);
  959. end;
  960. t_uniondef :
  961. begin
  962. inc(typedef_level);
  963. if (typedef_level>1) and (p^.p1=nil) and
  964. (p^.p2^.typ=t_id) then
  965. begin
  966. write(outfile,p^.p2^.p);
  967. end
  968. else
  969. begin
  970. inc(typedef_level);
  971. writeln(outfile,'record');
  972. shift(2);
  973. writeln(outfile,aktspace,'case longint of');
  974. shift(3);
  975. l:=0;
  976. hp1:=p^.p1;
  977. (* walk through all members *)
  978. while assigned(hp1) do
  979. begin
  980. (* hp2 is t_memberdec *)
  981. hp2:=hp1^.p1;
  982. (* hp3 is t_declist *)
  983. hp3:=hp2^.p2;
  984. while assigned(hp3) do
  985. begin
  986. write(outfile,aktspace,l,' : ( ');
  987. write(outfile,FixId(hp3^.p1^.p2^.p),' : ');
  988. shift(2);
  989. write_p_a_def(outfile,hp3^.p1^.p1,hp2^.p1);
  990. popshift;
  991. writeln(outfile,' );');
  992. hp3:=hp3^.next;
  993. inc(l);
  994. end;
  995. hp1:=hp1^.next;
  996. end;
  997. popshift;
  998. write(outfile,aktspace,'end');
  999. popshift;
  1000. flush(outfile);
  1001. dec(typedef_level);
  1002. end;
  1003. dec(typedef_level);
  1004. end;
  1005. else
  1006. internalerror(3);
  1007. end;
  1008. end;
  1009. procedure write_def_params(var outfile:text; p : presobject);
  1010. var
  1011. hp1 : presobject;
  1012. begin
  1013. case p^.typ of
  1014. t_enumdef : begin
  1015. hp1:=p^.p1;
  1016. while assigned(hp1) do
  1017. begin
  1018. write(outfile,FixId(hp1^.p1^.p));
  1019. hp1:=hp1^.next;
  1020. if assigned(hp1) then
  1021. write(outfile,',')
  1022. else
  1023. write(outfile);
  1024. flush(outfile);
  1025. end;
  1026. flush(outfile);
  1027. end;
  1028. else internalerror(4);
  1029. end;
  1030. end;
  1031. %}
  1032. %token TYPEDEF DEFINE
  1033. %token COLON SEMICOLON COMMA
  1034. %token LKLAMMER RKLAMMER LECKKLAMMER RECKKLAMMER
  1035. %token LGKLAMMER RGKLAMMER
  1036. %token STRUCT UNION ENUM
  1037. %token ID NUMBER CSTRING
  1038. %token SHORT UNSIGNED LONG INT REAL _CHAR
  1039. %token VOID _CONST
  1040. %token _FAR _HUGE _NEAR
  1041. %token _ASSIGN NEW_LINE SPACE_DEFINE
  1042. %token EXTERN STDCALL CDECL CALLBACK PASCAL WINAPI APIENTRY WINGDIAPI SYS_TRAP
  1043. %token _PACKED
  1044. %token ELLIPSIS
  1045. %right R_AND
  1046. %left EQUAL UNEQUAL GT LT GTE LTE
  1047. %left QUESTIONMARK COLON
  1048. %left _OR
  1049. %left _AND
  1050. %left _PLUS MINUS
  1051. %left _SHR _SHL
  1052. %left STAR _SLASH
  1053. %right _NOT
  1054. %right LKLAMMER
  1055. %right PSTAR
  1056. %right P_AND
  1057. %right LECKKLAMMER
  1058. %left POINT DEREF
  1059. %left COMMA
  1060. %left STICK
  1061. %token SIGNED
  1062. %%
  1063. file : declaration_list
  1064. ;
  1065. maybe_space :
  1066. SPACE_DEFINE
  1067. {
  1068. $$:=nil;
  1069. } |
  1070. {
  1071. $$:=nil;
  1072. }
  1073. ;
  1074. error_info : {
  1075. writeln(outfile,'(* error ');
  1076. writeln(outfile,yyline);
  1077. };
  1078. declaration_list : declaration_list declaration
  1079. { if yydebug then writeln('declaration reduced at line ',line_no);
  1080. if yydebug then writeln(outfile,'(* declaration reduced *)');
  1081. }
  1082. | declaration_list define_dec
  1083. { if yydebug then writeln('define declaration reduced at line ',line_no);
  1084. if yydebug then writeln(outfile,'(* define declaration reduced *)');
  1085. }
  1086. | declaration
  1087. { if yydebug then writeln('declaration reduced at line ',line_no);
  1088. }
  1089. | define_dec
  1090. { if yydebug then writeln('define declaration reduced at line ',line_no);
  1091. }
  1092. ;
  1093. dec_specifier :
  1094. EXTERN { $$:=new(presobject,init_id('extern')); }
  1095. |{ $$:=new(presobject,init_id('intern')); }
  1096. ;
  1097. dec_modifier :
  1098. STDCALL { $$:=new(presobject,init_id('no_pop')); }
  1099. | CDECL { $$:=new(presobject,init_id('cdecl')); }
  1100. | CALLBACK { $$:=new(presobject,init_id('no_pop')); }
  1101. | PASCAL { $$:=new(presobject,init_id('no_pop')); }
  1102. | WINAPI { $$:=new(presobject,init_id('no_pop')); }
  1103. | APIENTRY { $$:=new(presobject,init_id('no_pop')); }
  1104. | WINGDIAPI { $$:=new(presobject,init_id('no_pop')); }
  1105. | { $$:=nil }
  1106. ;
  1107. systrap_specifier:
  1108. SYS_TRAP LKLAMMER dname RKLAMMER { $$:=$3; }
  1109. | { $$:=nil; }
  1110. ;
  1111. declaration :
  1112. dec_specifier type_specifier dec_modifier declarator_list systrap_specifier SEMICOLON
  1113. {
  1114. IsExtern:=false;
  1115. (* by default we must pop the args pushed on stack *)
  1116. no_pop:=false;
  1117. if (assigned($4)and assigned($4^.p1)and assigned($4^.p1^.p1))
  1118. and ($4^.p1^.p1^.typ=t_procdef) then
  1119. begin
  1120. repeat
  1121. If UseLib then
  1122. IsExtern:=true
  1123. else
  1124. IsExtern:=assigned($1)and($1^.str='extern');
  1125. no_pop:=assigned($3) and ($3^.str='no_pop');
  1126. if block_type<>bt_func then
  1127. writeln(outfile);
  1128. block_type:=bt_func;
  1129. if not CompactMode then
  1130. begin
  1131. write(outfile,aktspace);
  1132. if not IsExtern then
  1133. write(extfile,aktspace);
  1134. end;
  1135. (* distinguish between procedure and function *)
  1136. if assigned($2) then
  1137. if ($2^.typ=t_void) and ($4^.p1^.p1^.p1=nil) then
  1138. begin
  1139. shift(10);
  1140. write(outfile,'procedure ',$4^.p1^.p2^.p);
  1141. if assigned($4^.p1^.p1^.p2) then
  1142. write_args(outfile,$4^.p1^.p1^.p2);
  1143. if not IsExtern then
  1144. begin
  1145. write(extfile,'procedure ',$4^.p1^.p2^.p);
  1146. if assigned($4^.p1^.p1^.p2) then
  1147. write_args(extfile,$4^.p1^.p1^.p2);
  1148. end;
  1149. end
  1150. else
  1151. begin
  1152. shift(9);
  1153. write(outfile,'function ',$4^.p1^.p2^.p);
  1154. if assigned($4^.p1^.p1^.p2) then
  1155. write_args(outfile,$4^.p1^.p1^.p2);
  1156. write(outfile,':');
  1157. write_p_a_def(outfile,$4^.p1^.p1^.p1,$2);
  1158. if not IsExtern then
  1159. begin
  1160. write(extfile,'function ',$4^.p1^.p2^.p);
  1161. if assigned($4^.p1^.p1^.p2) then
  1162. write_args(extfile,$4^.p1^.p1^.p2);
  1163. write(extfile,':');
  1164. write_p_a_def(extfile,$4^.p1^.p1^.p1,$2);
  1165. end;
  1166. end;
  1167. if assigned($5) then
  1168. write(outfile,';systrap ',$5^.p);
  1169. (* No CDECL in interface for Uselib *)
  1170. if IsExtern and (not no_pop) then
  1171. write(outfile,';cdecl');
  1172. popshift;
  1173. if UseLib then
  1174. begin
  1175. if IsExtern then
  1176. begin
  1177. write (outfile,';external');
  1178. If UseName then
  1179. Write(outfile,' External_library name ''',$4^.p1^.p2^.p,'''');
  1180. end;
  1181. writeln(outfile,';');
  1182. end
  1183. else
  1184. begin
  1185. writeln(outfile,';');
  1186. if not IsExtern then
  1187. begin
  1188. writeln(extfile,';');
  1189. writeln(extfile,aktspace,'begin');
  1190. writeln(extfile,aktspace,' { You must implemented this function }');
  1191. writeln(extfile,aktspace,'end;');
  1192. end;
  1193. end;
  1194. IsExtern:=false;
  1195. if not compactmode then
  1196. writeln(outfile);
  1197. until not NeedEllipsisOverload;
  1198. end
  1199. else (* $4^.p1^.p1^.typ=t_procdef *)
  1200. if assigned($4)and assigned($4^.p1) then
  1201. begin
  1202. shift(2);
  1203. if block_type<>bt_var then
  1204. begin
  1205. if not(compactmode) then
  1206. writeln(outfile);
  1207. writeln(outfile,aktspace,'var');
  1208. end;
  1209. block_type:=bt_var;
  1210. shift(3);
  1211. IsExtern:=assigned($1)and($1^.str='extern');
  1212. (* walk through all declarations *)
  1213. hp:=$4;
  1214. while assigned(hp) and assigned(hp^.p1) do
  1215. begin
  1216. (* write new var name *)
  1217. if assigned(hp^.p1^.p2) and assigned(hp^.p1^.p2^.p) then
  1218. write(outfile,aktspace,hp^.p1^.p2^.p);
  1219. write(outfile,' : ');
  1220. shift(2);
  1221. (* write its type *)
  1222. write_p_a_def(outfile,hp^.p1^.p1,$2);
  1223. if assigned(hp^.p1^.p2)and assigned(hp^.p1^.p2^.p)then
  1224. begin
  1225. if isExtern then
  1226. write(outfile,';cvar;external')
  1227. else
  1228. write(outfile,';cvar;public');
  1229. end;
  1230. writeln(outfile,';');
  1231. popshift;
  1232. hp:=hp^.p2;
  1233. end;
  1234. popshift;
  1235. popshift;
  1236. end;
  1237. if assigned($1)then dispose($1,done);
  1238. if assigned($2)then dispose($2,done);
  1239. if assigned($4)then dispose($4,done);
  1240. } |
  1241. special_type_specifier SEMICOLON
  1242. {
  1243. if block_type<>bt_type then
  1244. begin
  1245. if not(compactmode) then
  1246. writeln(outfile);
  1247. writeln(outfile,aktspace,'type');
  1248. block_type:=bt_type;
  1249. end;
  1250. shift(3);
  1251. if ( yyv[yysp-1]^.p2 <> nil ) then
  1252. begin
  1253. (* write new type name *)
  1254. TN:=TypeName($1^.p2^.p);
  1255. PN:=PointerName($1^.p2^.p);
  1256. (* define a Pointer type also for structs *)
  1257. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1258. assigned($1) and ($1^.typ in [t_uniondef,t_structdef]) then
  1259. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1260. write(outfile,aktspace,TN,' = ');
  1261. shift(2);
  1262. hp:=$1;
  1263. write_type_specifier(outfile,hp);
  1264. popshift;
  1265. (* enum_to_const can make a switch to const *)
  1266. if block_type=bt_type then
  1267. writeln(outfile,';');
  1268. writeln(outfile);
  1269. flush(outfile);
  1270. popshift;
  1271. if must_write_packed_field then
  1272. write_packed_fields_info(outfile,hp,TN);
  1273. if assigned(hp) then
  1274. dispose(hp,done)
  1275. end
  1276. else
  1277. begin
  1278. TN:=TypeName(yyv[yysp-1]^.str);
  1279. PN:=PointerName(yyv[yysp-1]^.str);
  1280. if UsePPointers then writeln(outfile,aktspace,PN,' = ^',TN,';');
  1281. writeln(outfile, aktspace, TN, ' = record');
  1282. writeln(outfile, aktspace, ' {undefined structure}');
  1283. writeln(outfile, aktspace, ' end;');
  1284. writeln(outfile);
  1285. popshift;
  1286. end;
  1287. } |
  1288. TYPEDEF STRUCT dname dname SEMICOLON
  1289. {
  1290. (* TYPEDEF STRUCT dname dname SEMICOLON *)
  1291. if block_type<>bt_type then
  1292. begin
  1293. if not(compactmode) then
  1294. writeln(outfile);
  1295. writeln(outfile,aktspace,'type');
  1296. block_type:=bt_type;
  1297. end;
  1298. PN:=TypeName($3^.p);
  1299. TN:=TypeName($4^.p);
  1300. if Uppercase(tn)<>Uppercase(pn) then
  1301. begin
  1302. shift(3);
  1303. writeln(outfile,aktspace,PN,' = ',TN,';');
  1304. popshift;
  1305. end;
  1306. if assigned($3) then
  1307. dispose($3,done);
  1308. if assigned($4) then
  1309. dispose($4,done);
  1310. } |
  1311. TYPEDEF type_specifier LKLAMMER dec_modifier declarator RKLAMMER maybe_space LKLAMMER argument_declaration_list RKLAMMER SEMICOLON
  1312. {
  1313. (* TYPEDEF type_specifier LKLAMMER dec_modifier declarator RKLAMMER maybe_space LKLAMMER argument_declaration_list RKLAMMER SEMICOLON *)
  1314. if block_type<>bt_type then
  1315. begin
  1316. if not(compactmode) then
  1317. writeln(outfile);
  1318. writeln(outfile,aktspace,'type');
  1319. block_type:=bt_type;
  1320. end;
  1321. no_pop:=assigned($4) and ($4^.str='no_pop');
  1322. shift(3);
  1323. (* walk through all declarations *)
  1324. hp:=$5;
  1325. if assigned(hp) then
  1326. begin
  1327. hp:=$5;
  1328. while assigned(hp^.p1) do
  1329. hp:=hp^.p1;
  1330. hp^.p1:=new(presobject,init_two(t_procdef,nil,$9));
  1331. hp:=$5;
  1332. if assigned(hp^.p1) and assigned(hp^.p1^.p1) then
  1333. begin
  1334. writeln(outfile);
  1335. (* write new type name *)
  1336. write(outfile,aktspace,TypeName(hp^.p2^.p),' = ');
  1337. shift(2);
  1338. write_p_a_def(outfile,hp^.p1,$2);
  1339. popshift;
  1340. (* if no_pop it is normal fpc calling convention *)
  1341. if is_procvar and
  1342. (not no_pop) then
  1343. write(outfile,';cdecl');
  1344. writeln(outfile,';');
  1345. flush(outfile);
  1346. end;
  1347. end;
  1348. popshift;
  1349. if assigned($2)then
  1350. dispose($2,done);
  1351. if assigned($4)then
  1352. dispose($4,done);
  1353. if assigned($5)then (* disposes also $9 *)
  1354. dispose($5,done);
  1355. } |
  1356. TYPEDEF type_specifier dec_modifier declarator_list SEMICOLON
  1357. {
  1358. (* TYPEDEF type_specifier dec_modifier declarator_list SEMICOLON *)
  1359. if block_type<>bt_type then
  1360. begin
  1361. if not(compactmode) then
  1362. writeln(outfile);
  1363. writeln(outfile,aktspace,'type');
  1364. block_type:=bt_type;
  1365. end;
  1366. no_pop:=assigned($3) and ($3^.str='no_pop');
  1367. shift(3);
  1368. (* Get the name to write the type definition for, try
  1369. to use the tag name first *)
  1370. if assigned($2^.p2) then
  1371. begin
  1372. ph:=$2^.p2;
  1373. end
  1374. else
  1375. begin
  1376. if not assigned($4^.p1^.p2) then
  1377. internalerror(4444);
  1378. ph:=$4^.p1^.p2;
  1379. end;
  1380. (* write type definition *)
  1381. is_procvar:=false;
  1382. writeln(outfile);
  1383. TN:=TypeName(ph^.p);
  1384. PN:=PointerName(ph^.p);
  1385. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1386. assigned($2) and ($2^.typ<>t_procdef) then
  1387. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1388. (* write new type name *)
  1389. write(outfile,aktspace,TN,' = ');
  1390. shift(2);
  1391. write_type_specifier(outfile,$2);
  1392. popshift;
  1393. (* if no_pop it is normal fpc calling convention *)
  1394. if is_procvar and
  1395. (not no_pop) then
  1396. write(outfile,';cdecl');
  1397. writeln(outfile,';');
  1398. flush(outfile);
  1399. (* write alias names, ph points to the name already used *)
  1400. hp:=$4;
  1401. while assigned(hp) do
  1402. begin
  1403. if (hp<>ph) and assigned(hp^.p1^.p2) then
  1404. begin
  1405. PN:=TypeName(ph^.p);
  1406. TN:=TypeName(hp^.p1^.p2^.p);
  1407. if Uppercase(TN)<>Uppercase(PN) then
  1408. begin
  1409. write(outfile,aktspace,TN,' = ');
  1410. write_p_a_def(outfile,hp^.p1^.p1,ph);
  1411. writeln(outfile,';');
  1412. PN:=PointerName(hp^.p1^.p2^.p);
  1413. if UsePPointers and (Uppercase(tn)<>Uppercase(pn)) and
  1414. assigned($2) and ($2^.typ<>t_procdef) then
  1415. writeln(outfile,aktspace,PN,' = ^',TN,';');
  1416. end;
  1417. end;
  1418. hp:=hp^.next;
  1419. end;
  1420. popshift;
  1421. if must_write_packed_field then
  1422. if assigned(ph) then
  1423. write_packed_fields_info(outfile,$2,ph^.str)
  1424. else if assigned($2^.p2) then
  1425. write_packed_fields_info(outfile,$2,$2^.p2^.str);
  1426. if assigned($2)then
  1427. dispose($2,done);
  1428. if assigned($3)then
  1429. dispose($3,done);
  1430. if assigned($4)then
  1431. dispose($4,done);
  1432. } |
  1433. TYPEDEF dname SEMICOLON
  1434. {
  1435. if block_type<>bt_type then
  1436. begin
  1437. if not(compactmode) then
  1438. writeln(outfile);
  1439. writeln(outfile,aktspace,'type');
  1440. block_type:=bt_type;
  1441. end;
  1442. shift(3);
  1443. (* write as pointer *)
  1444. writeln(outfile);
  1445. writeln(outfile,'(* generic typedef *)');
  1446. writeln(outfile,aktspace,$2^.p,' = pointer;');
  1447. flush(outfile);
  1448. popshift;
  1449. if assigned($2) then
  1450. dispose($2,done);
  1451. }
  1452. | error error_info SEMICOLON
  1453. { writeln(outfile,'in declaration at line ',line_no,' *)');
  1454. aktspace:='';
  1455. in_space_define:=0;
  1456. in_define:=false;
  1457. arglevel:=0;
  1458. if_nb:=0;
  1459. aktspace:=' ';
  1460. space_index:=1;
  1461. yyerrok;}
  1462. ;
  1463. define_dec :
  1464. DEFINE dname LKLAMMER enum_list RKLAMMER para_def_expr NEW_LINE
  1465. {
  1466. (* DEFINE dname LKLAMMER enum_list RKLAMMER para_def_expr NEW_LINE *)
  1467. if not stripinfo then
  1468. begin
  1469. writeln (outfile,aktspace,'{ was #define dname(params) para_def_expr }');
  1470. writeln (extfile,aktspace,'{ was #define dname(params) para_def_expr }');
  1471. if assigned($4) then
  1472. begin
  1473. writeln (outfile,aktspace,'{ argument types are unknown }');
  1474. writeln (extfile,aktspace,'{ argument types are unknown }');
  1475. end;
  1476. if not assigned($6^.p3) then
  1477. begin
  1478. writeln(outfile,aktspace,'{ return type might be wrong } ');
  1479. writeln(extfile,aktspace,'{ return type might be wrong } ');
  1480. end;
  1481. end;
  1482. block_type:=bt_func;
  1483. write(outfile,aktspace,'function ',$2^.p);
  1484. write(extfile,aktspace,'function ',$2^.p);
  1485. if assigned($4) then
  1486. begin
  1487. write(outfile,'(');
  1488. write(extfile,'(');
  1489. ph:=new(presobject,init_one(t_enumdef,$4));
  1490. write_def_params(outfile,ph);
  1491. write_def_params(extfile,ph);
  1492. if assigned(ph) then dispose(ph,done);
  1493. ph:=nil;
  1494. (* types are unknown *)
  1495. write(outfile,' : longint)');
  1496. write(extfile,' : longint)');
  1497. end;
  1498. if not assigned($6^.p3) then
  1499. begin
  1500. writeln(outfile,' : longint;');
  1501. writeln(extfile,' : longint;');
  1502. flush(outfile);
  1503. end
  1504. else
  1505. begin
  1506. write(outfile,' : ');
  1507. write_type_specifier(outfile,$6^.p3);
  1508. writeln(outfile,';');
  1509. flush(outfile);
  1510. write(extfile,' : ');
  1511. write_type_specifier(extfile,$6^.p3);
  1512. writeln(extfile,';');
  1513. end;
  1514. writeln(outfile);
  1515. flush(outfile);
  1516. hp:=new(presobject,init_two(t_funcname,$2,$6));
  1517. write_funexpr(extfile,hp);
  1518. writeln(extfile);
  1519. flush(extfile);
  1520. if assigned(hp)then dispose(hp,done);
  1521. }|
  1522. DEFINE dname SPACE_DEFINE NEW_LINE
  1523. {
  1524. (* DEFINE dname SPACE_DEFINE NEW_LINE *)
  1525. writeln(outfile,'{$define ',$2^.p,'}');
  1526. flush(outfile);
  1527. if assigned($2)then
  1528. dispose($2,done);
  1529. }|
  1530. DEFINE dname NEW_LINE
  1531. {
  1532. writeln(outfile,'{$define ',$2^.p,'}');
  1533. flush(outfile);
  1534. if assigned($2)then
  1535. dispose($2,done);
  1536. } |
  1537. DEFINE dname SPACE_DEFINE def_expr NEW_LINE
  1538. {
  1539. (* DEFINE dname SPACE_DEFINE def_expr NEW_LINE *)
  1540. if ($4^.typ=t_exprlist) and
  1541. $4^.p1^.is_const and
  1542. not assigned($4^.next) then
  1543. begin
  1544. if block_type<>bt_const then
  1545. begin
  1546. writeln(outfile);
  1547. writeln(outfile,aktspace,'const');
  1548. end;
  1549. block_type:=bt_const;
  1550. shift(3);
  1551. write(outfile,aktspace,$2^.p);
  1552. write(outfile,' = ');
  1553. flush(outfile);
  1554. write_expr(outfile,$4^.p1);
  1555. writeln(outfile,';');
  1556. popshift;
  1557. if assigned($2) then
  1558. dispose($2,done);
  1559. if assigned($4) then
  1560. dispose($4,done);
  1561. end
  1562. else
  1563. begin
  1564. if not stripinfo then
  1565. begin
  1566. writeln (outfile,aktspace,'{ was #define dname def_expr }');
  1567. writeln (extfile,aktspace,'{ was #define dname def_expr }');
  1568. end;
  1569. block_type:=bt_func;
  1570. write(outfile,aktspace,'function ',$2^.p);
  1571. write(extfile,aktspace,'function ',$2^.p);
  1572. shift(2);
  1573. if not assigned($4^.p3) then
  1574. begin
  1575. writeln(outfile,' : longint;');
  1576. writeln(outfile,aktspace,' { return type might be wrong }');
  1577. flush(outfile);
  1578. writeln(extfile,' : longint;');
  1579. writeln(extfile,aktspace,' { return type might be wrong }');
  1580. end
  1581. else
  1582. begin
  1583. write(outfile,' : ');
  1584. write_type_specifier(outfile,$4^.p3);
  1585. writeln(outfile,';');
  1586. flush(outfile);
  1587. write(extfile,' : ');
  1588. write_type_specifier(extfile,$4^.p3);
  1589. writeln(extfile,';');
  1590. end;
  1591. writeln(outfile);
  1592. flush(outfile);
  1593. hp:=new(presobject,init_two(t_funcname,$2,$4));
  1594. write_funexpr(extfile,hp);
  1595. popshift;
  1596. dispose(hp,done);
  1597. writeln(extfile);
  1598. flush(extfile);
  1599. end;
  1600. }
  1601. | error error_info NEW_LINE
  1602. { writeln(outfile,'in define line ',line_no,' *)');
  1603. aktspace:='';
  1604. in_space_define:=0;
  1605. in_define:=false;
  1606. arglevel:=0;
  1607. if_nb:=0;
  1608. aktspace:=' ';
  1609. space_index:=1;
  1610. yyerrok;}
  1611. ;
  1612. closed_list : LGKLAMMER member_list RGKLAMMER
  1613. {$$:=$2;} |
  1614. error error_info RGKLAMMER
  1615. { writeln(outfile,' in member_list *)');
  1616. yyerrok;
  1617. $$:=nil;
  1618. }
  1619. ;
  1620. closed_enum_list : LGKLAMMER enum_list RGKLAMMER
  1621. {$$:=$2;} |
  1622. error error_info RGKLAMMER
  1623. { writeln(outfile,' in enum_list *)');
  1624. yyerrok;
  1625. $$:=nil;
  1626. }
  1627. ;
  1628. special_type_specifier :
  1629. STRUCT dname closed_list _PACKED
  1630. {
  1631. if not is_packed then
  1632. writeln(outfile,'{$PACKRECORDS 1}');
  1633. is_packed:=true;
  1634. $$:=new(presobject,init_two(t_structdef,$3,$2));
  1635. } |
  1636. STRUCT dname closed_list
  1637. {
  1638. if is_packed then
  1639. writeln(outfile,'{$PACKRECORDS 4}');
  1640. is_packed:=false;
  1641. $$:=new(presobject,init_two(t_structdef,$3,$2));
  1642. } |
  1643. UNION dname closed_list _PACKED
  1644. {
  1645. if not is_packed then
  1646. writeln(outfile,'{$PACKRECORDS 1}');
  1647. is_packed:=true;
  1648. $$:=new(presobject,init_two(t_uniondef,$3,$2));
  1649. } |
  1650. UNION dname closed_list
  1651. {
  1652. $$:=new(presobject,init_two(t_uniondef,$3,$2));
  1653. } |
  1654. UNION dname
  1655. {
  1656. $$:=$2;
  1657. } |
  1658. STRUCT dname
  1659. {
  1660. $$:=$2;
  1661. } |
  1662. ENUM dname closed_enum_list
  1663. {
  1664. $$:=new(presobject,init_two(t_enumdef,$3,$2));
  1665. } |
  1666. ENUM dname
  1667. {
  1668. $$:=$2;
  1669. };
  1670. type_specifier :
  1671. _CONST type_specifier
  1672. {
  1673. if not stripinfo then
  1674. writeln(outfile,'(* Const before type ignored *)');
  1675. $$:=$2;
  1676. } |
  1677. UNION closed_list _PACKED
  1678. {
  1679. if not is_packed then
  1680. writeln(outfile,'{$PACKRECORDS 1}');
  1681. is_packed:=true;
  1682. $$:=new(presobject,init_one(t_uniondef,$2));
  1683. } |
  1684. UNION closed_list
  1685. {
  1686. $$:=new(presobject,init_one(t_uniondef,$2));
  1687. } |
  1688. STRUCT closed_list _PACKED
  1689. {
  1690. if not is_packed then
  1691. writeln(outfile,'{$PACKRECORDS 1}');
  1692. is_packed:=true;
  1693. $$:=new(presobject,init_one(t_structdef,$2));
  1694. } |
  1695. STRUCT closed_list
  1696. {
  1697. if is_packed then
  1698. writeln(outfile,'{$PACKRECORDS 4}');
  1699. is_packed:=false;
  1700. $$:=new(presobject,init_one(t_structdef,$2));
  1701. } |
  1702. ENUM closed_enum_list
  1703. {
  1704. $$:=new(presobject,init_one(t_enumdef,$2));
  1705. } |
  1706. special_type_specifier
  1707. {
  1708. $$:=$1;
  1709. } |
  1710. simple_type_name { $$:=$1; }
  1711. ;
  1712. member_list : member_declaration member_list
  1713. {
  1714. $$:=new(presobject,init_one(t_memberdeclist,$1));
  1715. $$^.next:=$2;
  1716. } |
  1717. member_declaration
  1718. {
  1719. $$:=new(presobject,init_one(t_memberdeclist,$1));
  1720. }
  1721. ;
  1722. member_declaration :
  1723. type_specifier declarator_list SEMICOLON
  1724. {
  1725. $$:=new(presobject,init_two(t_memberdec,$1,$2));
  1726. }
  1727. ;
  1728. dname : ID { (*dname*)
  1729. $$:=new(presobject,init_id(act_token));
  1730. }
  1731. ;
  1732. special_type_name :
  1733. SIGNED special_type_name
  1734. {
  1735. hp:=$2;
  1736. $$:=hp;
  1737. if assigned(hp) then
  1738. begin
  1739. s:=strpas(hp^.p);
  1740. if s=UINT_STR then
  1741. s:=INT_STR
  1742. else if s=USHORT_STR then
  1743. s:=SHORT_STR
  1744. else if s=UCHAR_STR then
  1745. s:=CHAR_STR
  1746. else if s=QWORD_STR then
  1747. s:=INT64_STR
  1748. else
  1749. s:='';
  1750. if s<>'' then
  1751. hp^.setstr(s);
  1752. end;
  1753. } |
  1754. UNSIGNED special_type_name
  1755. {
  1756. hp:=$2;
  1757. $$:=hp;
  1758. if assigned(hp) then
  1759. begin
  1760. s:=strpas(hp^.p);
  1761. if s=INT_STR then
  1762. s:=UINT_STR
  1763. else if s=SHORT_STR then
  1764. s:=USHORT_STR
  1765. else if s=CHAR_STR then
  1766. s:=UCHAR_STR
  1767. else if s=INT64_STR then
  1768. s:=QWORD_STR
  1769. else
  1770. s:='';
  1771. if s<>'' then
  1772. hp^.setstr(s);
  1773. end;
  1774. } |
  1775. INT
  1776. {
  1777. $$:=new(presobject,init_intid(INT_STR));
  1778. } |
  1779. LONG
  1780. {
  1781. $$:=new(presobject,init_intid(INT_STR));
  1782. } |
  1783. LONG INT
  1784. {
  1785. $$:=new(presobject,init_intid(INT_STR));
  1786. } |
  1787. LONG LONG
  1788. {
  1789. $$:=new(presobject,init_intid(INT64_STR));
  1790. } |
  1791. LONG LONG INT
  1792. {
  1793. $$:=new(presobject,init_intid(INT64_STR));
  1794. } |
  1795. SHORT
  1796. {
  1797. $$:=new(presobject,init_intid(SHORT_STR));
  1798. } |
  1799. SHORT INT
  1800. {
  1801. $$:=new(presobject,init_intid(SHORT_STR));
  1802. } |
  1803. REAL
  1804. {
  1805. $$:=new(presobject,init_intid(REAL_STR));
  1806. } |
  1807. VOID
  1808. {
  1809. $$:=new(presobject,init_no(t_void));
  1810. } |
  1811. _CHAR
  1812. {
  1813. $$:=new(presobject,init_intid(CHAR_STR));
  1814. } |
  1815. UNSIGNED
  1816. {
  1817. $$:=new(presobject,init_intid(UINT_STR));
  1818. }
  1819. ;
  1820. simple_type_name :
  1821. special_type_name
  1822. {
  1823. $$:=$1;
  1824. }
  1825. |
  1826. dname
  1827. {
  1828. $$:=$1;
  1829. tn:=$$^.str;
  1830. if removeunderscore and
  1831. (length(tn)>1) and (tn[1]='_') then
  1832. $$^.setstr(Copy(tn,2,length(tn)-1));
  1833. }
  1834. ;
  1835. declarator_list :
  1836. declarator_list COMMA declarator
  1837. {
  1838. $$:=$1;
  1839. hp:=$1;
  1840. while assigned(hp^.next) do
  1841. hp:=hp^.next;
  1842. hp^.next:=new(presobject,init_one(t_declist,$3));
  1843. }|
  1844. error error_info COMMA declarator_list
  1845. {
  1846. writeln(outfile,' in declarator_list *)');
  1847. $$:=$4;
  1848. yyerrok;
  1849. }|
  1850. error error_info
  1851. {
  1852. writeln(outfile,' in declarator_list *)');
  1853. yyerrok;
  1854. }|
  1855. declarator
  1856. {
  1857. $$:=new(presobject,init_one(t_declist,$1));
  1858. }
  1859. ;
  1860. argument_declaration : type_specifier declarator
  1861. {
  1862. $$:=new(presobject,init_two(t_arg,$1,$2));
  1863. } |
  1864. type_specifier STAR declarator
  1865. {
  1866. hp:=new(presobject,init_one(t_pointerdef,$1));
  1867. $$:=new(presobject,init_two(t_arg,hp,$3));
  1868. } |
  1869. type_specifier abstract_declarator
  1870. {
  1871. $$:=new(presobject,init_two(t_arg,$1,$2));
  1872. }
  1873. ;
  1874. argument_declaration_list : argument_declaration
  1875. {
  1876. $$:=new(presobject,init_two(t_arglist,$1,nil));
  1877. } |
  1878. argument_declaration COMMA argument_declaration_list
  1879. {
  1880. $$:=new(presobject,init_two(t_arglist,$1,nil));
  1881. $$^.next:=$3;
  1882. } |
  1883. ELLIPSIS
  1884. {
  1885. $$:=new(presobject,init_two(t_arglist,ellipsisarg,nil));
  1886. } |
  1887. {
  1888. $$:=nil;
  1889. }
  1890. ;
  1891. size_overrider :
  1892. _FAR
  1893. { $$:=new(presobject,init_id('far'));}
  1894. | _NEAR
  1895. { $$:=new(presobject,init_id('near'));}
  1896. | _HUGE
  1897. { $$:=new(presobject,init_id('huge'));}
  1898. ;
  1899. declarator :
  1900. _CONST declarator
  1901. {
  1902. if not stripinfo then
  1903. writeln(outfile,'(* Const before declarator ignored *)');
  1904. $$:=$2;
  1905. } |
  1906. size_overrider STAR declarator
  1907. {
  1908. if not stripinfo then
  1909. writeln(outfile,aktspace,'(* ',$1^.p,' ignored *)');
  1910. dispose($1,done);
  1911. hp:=$3;
  1912. $$:=hp;
  1913. while assigned(hp^.p1) do
  1914. hp:=hp^.p1;
  1915. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  1916. } |
  1917. STAR declarator
  1918. {
  1919. (* %prec PSTAR this was wrong!! *)
  1920. hp:=$2;
  1921. $$:=hp;
  1922. while assigned(hp^.p1) do
  1923. hp:=hp^.p1;
  1924. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  1925. } |
  1926. _AND declarator %prec P_AND
  1927. {
  1928. hp:=$2;
  1929. $$:=hp;
  1930. while assigned(hp^.p1) do
  1931. hp:=hp^.p1;
  1932. hp^.p1:=new(presobject,init_one(t_addrdef,nil));
  1933. } |
  1934. dname COLON expr
  1935. {
  1936. (* size specifier supported *)
  1937. hp:=new(presobject,init_one(t_size_specifier,$3));
  1938. $$:=new(presobject,init_three(t_dec,nil,$1,hp));
  1939. }|
  1940. dname ASSIGN expr
  1941. {
  1942. if not stripinfo then
  1943. writeln(outfile,'(* Warning : default value for ',$1^.p,' ignored *)');
  1944. hp:=new(presobject,init_one(t_default_value,$3));
  1945. $$:=new(presobject,init_three(t_dec,nil,$1,hp));
  1946. }|
  1947. dname
  1948. {
  1949. $$:=new(presobject,init_two(t_dec,nil,$1));
  1950. }|
  1951. declarator LKLAMMER argument_declaration_list RKLAMMER
  1952. {
  1953. hp:=$1;
  1954. $$:=hp;
  1955. while assigned(hp^.p1) do
  1956. hp:=hp^.p1;
  1957. hp^.p1:=new(presobject,init_two(t_procdef,nil,$3));
  1958. } |
  1959. declarator no_arg
  1960. {
  1961. hp:=$1;
  1962. $$:=hp;
  1963. while assigned(hp^.p1) do
  1964. hp:=hp^.p1;
  1965. hp^.p1:=new(presobject,init_two(t_procdef,nil,nil));
  1966. } |
  1967. declarator LECKKLAMMER expr RECKKLAMMER
  1968. {
  1969. hp:=$1;
  1970. $$:=hp;
  1971. while assigned(hp^.p1) do
  1972. hp:=hp^.p1;
  1973. hp^.p1:=new(presobject,init_two(t_arraydef,nil,$3));
  1974. } |
  1975. declarator LECKKLAMMER RECKKLAMMER
  1976. {
  1977. (* this is translated into a pointer *)
  1978. hp:=$1;
  1979. $$:=hp;
  1980. while assigned(hp^.p1) do
  1981. hp:=hp^.p1;
  1982. hp^.p1:=new(presobject,init_two(t_arraydef,nil,nil));
  1983. } |
  1984. LKLAMMER declarator RKLAMMER
  1985. {
  1986. $$:=$2;
  1987. }
  1988. ;
  1989. no_arg : LKLAMMER RKLAMMER |
  1990. LKLAMMER VOID RKLAMMER;
  1991. abstract_declarator :
  1992. _CONST abstract_declarator
  1993. {
  1994. if not stripinfo then
  1995. writeln(outfile,'(* Const before abstract_declarator ignored *)');
  1996. $$:=$2;
  1997. } |
  1998. size_overrider STAR abstract_declarator
  1999. {
  2000. if not stripinfo then
  2001. writeln(outfile,aktspace,'(* ',$1^.p,' ignored *)');
  2002. dispose($1,done);
  2003. hp:=$3;
  2004. $$:=hp;
  2005. while assigned(hp^.p1) do
  2006. hp:=hp^.p1;
  2007. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2008. } |
  2009. STAR abstract_declarator %prec PSTAR
  2010. {
  2011. hp:=$2;
  2012. $$:=hp;
  2013. while assigned(hp^.p1) do
  2014. hp:=hp^.p1;
  2015. hp^.p1:=new(presobject,init_one(t_pointerdef,nil));
  2016. } |
  2017. abstract_declarator LKLAMMER argument_declaration_list RKLAMMER
  2018. {
  2019. hp:=$1;
  2020. $$:=hp;
  2021. while assigned(hp^.p1) do
  2022. hp:=hp^.p1;
  2023. hp^.p1:=new(presobject,init_two(t_procdef,nil,$3));
  2024. } |
  2025. abstract_declarator no_arg
  2026. {
  2027. hp:=$1;
  2028. $$:=hp;
  2029. while assigned(hp^.p1) do
  2030. hp:=hp^.p1;
  2031. hp^.p1:=new(presobject,init_two(t_procdef,nil,nil));
  2032. } |
  2033. abstract_declarator LECKKLAMMER expr RECKKLAMMER
  2034. {
  2035. hp:=$1;
  2036. $$:=hp;
  2037. while assigned(hp^.p1) do
  2038. hp:=hp^.p1;
  2039. hp^.p1:=new(presobject,init_two(t_arraydef,nil,$3));
  2040. } |
  2041. declarator LECKKLAMMER RECKKLAMMER
  2042. {
  2043. (* this is translated into a pointer *)
  2044. hp:=$1;
  2045. $$:=hp;
  2046. while assigned(hp^.p1) do
  2047. hp:=hp^.p1;
  2048. hp^.p1:=new(presobject,init_two(t_arraydef,nil,nil));
  2049. } |
  2050. LKLAMMER abstract_declarator RKLAMMER
  2051. {
  2052. $$:=$2;
  2053. } |
  2054. {
  2055. $$:=new(presobject,init_two(t_dec,nil,nil));
  2056. }
  2057. ;
  2058. expr :
  2059. shift_expr
  2060. {$$:=$1;}
  2061. ;
  2062. shift_expr :
  2063. expr EQUAL expr
  2064. { $$:=new(presobject,init_bop(' = ',$1,$3));}
  2065. | expr UNEQUAL expr
  2066. { $$:=new(presobject,init_bop(' <> ',$1,$3));}
  2067. | expr GT expr
  2068. { $$:=new(presobject,init_bop(' > ',$1,$3));}
  2069. | expr GTE expr
  2070. { $$:=new(presobject,init_bop(' >= ',$1,$3));}
  2071. | expr LT expr
  2072. { $$:=new(presobject,init_bop(' < ',$1,$3));}
  2073. | expr LTE expr
  2074. { $$:=new(presobject,init_bop(' <= ',$1,$3));}
  2075. | expr _PLUS expr
  2076. { $$:=new(presobject,init_bop(' + ',$1,$3));}
  2077. | expr MINUS expr
  2078. { $$:=new(presobject,init_bop(' - ',$1,$3));}
  2079. | expr STAR expr
  2080. { $$:=new(presobject,init_bop(' * ',$1,$3));}
  2081. | expr _SLASH expr
  2082. { $$:=new(presobject,init_bop(' / ',$1,$3));}
  2083. | expr _OR expr
  2084. { $$:=new(presobject,init_bop(' or ',$1,$3));}
  2085. | expr _AND expr
  2086. { $$:=new(presobject,init_bop(' and ',$1,$3));}
  2087. | expr _NOT expr
  2088. { $$:=new(presobject,init_bop(' not ',$1,$3));}
  2089. | expr _SHL expr
  2090. { $$:=new(presobject,init_bop(' shl ',$1,$3));}
  2091. | expr _SHR expr
  2092. { $$:=new(presobject,init_bop(' shr ',$1,$3));}
  2093. | expr QUESTIONMARK colon_expr
  2094. { $3^.p1:=$1;
  2095. $$:=$3;
  2096. inc(if_nb);
  2097. $$^.p:=strpnew('if_local'+str(if_nb));
  2098. } |
  2099. unary_expr {$$:=$1;}
  2100. ;
  2101. colon_expr : expr COLON expr
  2102. { (* if A then B else C *)
  2103. $$:=new(presobject,init_three(t_ifexpr,nil,$1,$3));}
  2104. ;
  2105. maybe_empty_unary_expr :
  2106. unary_expr
  2107. { $$:=$1; }
  2108. |
  2109. { $$:=nil;}
  2110. ;
  2111. unary_expr:
  2112. dname
  2113. {
  2114. $$:=$1;
  2115. } |
  2116. special_type_name
  2117. {
  2118. $$:=$1;
  2119. } |
  2120. CSTRING
  2121. {
  2122. (* remove L prefix for widestrings *)
  2123. s:=act_token;
  2124. if Win32headers and (s[1]='L') then
  2125. delete(s,1,1);
  2126. $$:=new(presobject,init_id(''''+copy(s,2,length(s)-2)+''''));
  2127. } |
  2128. NUMBER
  2129. {
  2130. $$:=new(presobject,init_id(act_token));
  2131. } |
  2132. unary_expr POINT expr
  2133. {
  2134. $$:=new(presobject,init_bop('.',$1,$3));
  2135. } |
  2136. unary_expr DEREF expr
  2137. {
  2138. $$:=new(presobject,init_bop('^.',$1,$3));
  2139. } |
  2140. MINUS unary_expr
  2141. {
  2142. $$:=new(presobject,init_preop('-',$2));
  2143. }|
  2144. _AND unary_expr %prec R_AND
  2145. {
  2146. $$:=new(presobject,init_preop('@',$2));
  2147. }|
  2148. _NOT unary_expr
  2149. {
  2150. $$:=new(presobject,init_preop(' not ',$2));
  2151. } |
  2152. LKLAMMER dname RKLAMMER maybe_empty_unary_expr
  2153. {
  2154. if assigned($4) then
  2155. $$:=new(presobject,init_two(t_typespec,$2,$4))
  2156. else
  2157. $$:=$2;
  2158. } |
  2159. LKLAMMER type_specifier RKLAMMER unary_expr
  2160. {
  2161. $$:=new(presobject,init_two(t_typespec,$2,$4));
  2162. } |
  2163. LKLAMMER type_specifier STAR RKLAMMER unary_expr
  2164. {
  2165. hp:=new(presobject,init_one(t_pointerdef,$2));
  2166. $$:=new(presobject,init_two(t_typespec,hp,$5));
  2167. } |
  2168. LKLAMMER type_specifier size_overrider STAR RKLAMMER unary_expr
  2169. {
  2170. if not stripinfo then
  2171. writeln(outfile,aktspace,'(* ',$3^.p,' ignored *)');
  2172. dispose($3,done);
  2173. write_type_specifier(outfile,$2);
  2174. writeln(outfile,' ignored *)');
  2175. hp:=new(presobject,init_one(t_pointerdef,$2));
  2176. $$:=new(presobject,init_two(t_typespec,hp,$6));
  2177. } |
  2178. dname LKLAMMER exprlist RKLAMMER
  2179. {
  2180. hp:=new(presobject,init_one(t_exprlist,$1));
  2181. $$:=new(presobject,init_three(t_funexprlist,hp,$3,nil));
  2182. } |
  2183. LKLAMMER shift_expr RKLAMMER
  2184. {
  2185. $$:=$2;
  2186. } |
  2187. LKLAMMER STAR unary_expr RKLAMMER maybe_space LKLAMMER exprlist RKLAMMER
  2188. {
  2189. $$:=new(presobject,init_two(t_callop,$3,$7));
  2190. } |
  2191. dname LECKKLAMMER exprlist RECKKLAMMER
  2192. {
  2193. $$:=new(presobject,init_two(t_arrayop,$1,$3));
  2194. }
  2195. ;
  2196. enum_list :
  2197. enum_element COMMA enum_list
  2198. { (*enum_element COMMA enum_list *)
  2199. $$:=$1;
  2200. $$^.next:=$3;
  2201. } |
  2202. enum_element {
  2203. $$:=$1;
  2204. } |
  2205. {(* empty enum list *)
  2206. $$:=nil;};
  2207. enum_element :
  2208. dname _ASSIGN expr
  2209. { begin (*enum_element: dname _ASSIGN expr *)
  2210. $$:=new(presobject,init_two(t_enumlist,$1,$3));
  2211. end;
  2212. } |
  2213. dname
  2214. {
  2215. begin (*enum_element: dname*)
  2216. $$:=new(presobject,init_two(t_enumlist,$1,nil));
  2217. end;
  2218. };
  2219. def_expr :
  2220. unary_expr
  2221. {
  2222. if $1^.typ=t_funexprlist then
  2223. $$:=$1
  2224. else
  2225. $$:=new(presobject,init_two(t_exprlist,$1,nil));
  2226. (* if here is a type specifier
  2227. we know the return type *)
  2228. if ($1^.typ=t_typespec) then
  2229. $$^.p3:=$1^.p1^.get_copy;
  2230. }
  2231. ;
  2232. para_def_expr :
  2233. SPACE_DEFINE def_expr
  2234. {
  2235. $$:=$2;
  2236. } |
  2237. maybe_space LKLAMMER def_expr RKLAMMER
  2238. {
  2239. $$:=$3
  2240. }
  2241. ;
  2242. exprlist : exprelem COMMA exprlist
  2243. { (*exprlist COMMA expr*)
  2244. $$:=$1;
  2245. $1^.next:=$3;
  2246. } |
  2247. exprelem
  2248. {
  2249. $$:=$1;
  2250. } |
  2251. { (* empty expression list *)
  2252. $$:=nil; };
  2253. exprelem :
  2254. expr
  2255. {
  2256. $$:=new(presobject,init_one(t_exprlist,$1));
  2257. };
  2258. %%
  2259. function yylex : Integer;
  2260. begin
  2261. yylex:=scan.yylex;
  2262. line_no:=yylineno;
  2263. end;
  2264. var
  2265. SS : string;
  2266. i : longint;
  2267. begin
  2268. { Initialize }
  2269. yydebug:=true;
  2270. aktspace:='';
  2271. block_type:=bt_no;
  2272. IsExtern:=false;
  2273. { Read commandline options }
  2274. ProcessOptions;
  2275. if not CompactMode then
  2276. aktspace:=' ';
  2277. { open input and output files }
  2278. assign(yyinput, inputfilename);
  2279. {$I-}
  2280. reset(yyinput);
  2281. {$I+}
  2282. if ioresult<>0 then
  2283. begin
  2284. writeln('file ',inputfilename,' not found!');
  2285. halt(1);
  2286. end;
  2287. assign(outfile, outputfilename);
  2288. rewrite(outfile);
  2289. { write unit header }
  2290. if not includefile then
  2291. begin
  2292. writeln(outfile,'unit ',unitname,';');
  2293. writeln(outfile,'interface');
  2294. writeln(outfile);
  2295. writeln(outfile,'{');
  2296. writeln(outfile,' Automatically converted by H2Pas ',version,' from ',inputfilename);
  2297. writeln(outfile,' The following command line parameters were used:');
  2298. for i:=1 to paramcount do
  2299. writeln(outfile,' ',paramstr(i));
  2300. writeln(outfile,'}');
  2301. writeln(outfile);
  2302. end;
  2303. if UseName then
  2304. begin
  2305. writeln(outfile,aktspace,'const');
  2306. writeln(outfile,aktspace,' External_library=''',libfilename,'''; {Setup as you need}');
  2307. writeln(outfile);
  2308. end;
  2309. if UsePPointers then
  2310. begin
  2311. Writeln(outfile,aktspace,'{ Pointers to basic pascal types, inserted by h2pas conversion program.}');
  2312. Writeln(outfile,aktspace,'Type');
  2313. Writeln(outfile,aktspace,' PLongint = ^Longint;');
  2314. Writeln(outfile,aktspace,' PSmallInt = ^SmallInt;');
  2315. Writeln(outfile,aktspace,' PByte = ^Byte;');
  2316. Writeln(outfile,aktspace,' PWord = ^Word;');
  2317. Writeln(outfile,aktspace,' PDWord = ^DWord;');
  2318. Writeln(outfile,aktspace,' PDouble = ^Double;');
  2319. Writeln(outfile);
  2320. end;
  2321. writeln(outfile,'{$PACKRECORDS C}');
  2322. writeln(outfile);
  2323. { Open tempfiles }
  2324. Assign(extfile,'ext.tmp');
  2325. rewrite(extfile);
  2326. Assign(tempfile,'ext2.tmp');
  2327. rewrite(tempfile);
  2328. { Parse! }
  2329. yyparse;
  2330. { Write implementation if needed }
  2331. if not(includefile) then
  2332. begin
  2333. writeln(outfile);
  2334. writeln(outfile,'implementation');
  2335. writeln(outfile);
  2336. end;
  2337. { here we have a problem if a line is longer than 255 chars !! }
  2338. reset(extfile);
  2339. while not eof(extfile) do
  2340. begin
  2341. readln(extfile,SS);
  2342. writeln(outfile,SS);
  2343. end;
  2344. { write end of file }
  2345. writeln(outfile);
  2346. if not(includefile) then
  2347. writeln(outfile,'end.');
  2348. { close and erase tempfiles }
  2349. close(extfile);
  2350. erase(extfile);
  2351. close(outfile);
  2352. close(tempfile);
  2353. erase(tempfile);
  2354. end.
  2355. {
  2356. $Log$
  2357. Revision 1.6 2003-02-13 22:20:24 michael
  2358. + Patch from Jeff Pohlmeyer to process empty structs
  2359. Revision 1.5 2002/09/07 15:40:33 peter
  2360. * old logs removed and tabs fixed
  2361. }