h2pas.y 83 KB

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