h2pas.y 97 KB

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