h2pas.y 100 KB

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