h2pas.y 100 KB

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