h2pas.y 103 KB

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