h2pas.y 100 KB

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