h2pas.y 79 KB

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