h2pas.y 77 KB

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