h2pas.y 100 KB

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