h2pas.y 100 KB

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