h2pas.y 103 KB

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