h2pas.y 100 KB

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