parser.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. {
  2. $Id$
  3. Copyright (c) 1993-98 by Florian Klaempfl
  4. This unit does the parsing process
  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. }
  18. {$ifdef tp}
  19. {$E+,N+,D+,F+}
  20. {$endif}
  21. unit parser;
  22. interface
  23. procedure compile(const filename:string;compile_system:boolean);
  24. procedure initparser;
  25. implementation
  26. uses
  27. cobjects,comphook,systems,globals,
  28. hcodegen,verbose, { leave this order, else hcodegen.message will be used !! }
  29. symtable,files,aasm,
  30. assemble,link,script,gendef,
  31. {$ifdef UseBrowser}
  32. browser,
  33. {$endif UseBrowser}
  34. scanner,pbase,pdecl,psystem,pmodules;
  35. procedure initparser;
  36. begin
  37. forwardsallowed:=false;
  38. { ^M means a string or a char, because we don't parse a }
  39. { type declaration }
  40. ignore_equal:=false;
  41. { we didn't parse a object or class declaration }
  42. { and no function header }
  43. testcurobject:=0;
  44. { a long time, this was forgotten }
  45. aktprocsym:=nil;
  46. current_module:=nil;
  47. loaded_units.init;
  48. usedunits.init;
  49. { global switches }
  50. aktglobalswitches:=initglobalswitches;
  51. { memory sizes }
  52. if heapsize=0 then
  53. heapsize:=target_info.heapsize;
  54. if maxheapsize=0 then
  55. maxheapsize:=target_info.maxheapsize;
  56. if stacksize=0 then
  57. stacksize:=target_info.stacksize;
  58. end;
  59. procedure default_macros;
  60. var
  61. hp : pstring_item;
  62. begin
  63. { commandline }
  64. hp:=pstring_item(initdefines.first);
  65. while assigned(hp) do
  66. begin
  67. def_macro(hp^.str^);
  68. hp:=pstring_item(hp^.next);
  69. end;
  70. { set macros for version checking }
  71. set_macro('FPC_VERSION',version_nr);
  72. set_macro('FPC_RELEASE',release_nr);
  73. set_macro('FPC_PATCH',patch_nr);
  74. end;
  75. procedure compile(const filename:string;compile_system:boolean);
  76. var
  77. { scanner }
  78. oldtoken : ttoken;
  79. oldtokenpos : tfileposinfo;
  80. oldc : char;
  81. oldpattern,
  82. oldorgpattern : string;
  83. old_block_type : tblock_type;
  84. oldcurrent_scanner : pscannerfile;
  85. { symtable }
  86. oldmacros,
  87. oldrefsymtable,
  88. oldsymtablestack : psymtable;
  89. oldprocprefix : string;
  90. oldaktprocsym : pprocsym;
  91. { cg }
  92. oldnextlabelnr : longint;
  93. oldparse_only : boolean;
  94. { asmlists }
  95. oldimports,
  96. oldexports,
  97. oldresource,
  98. oldrttilist,
  99. oldbsssegment,
  100. olddatasegment,
  101. oldcodesegment,
  102. oldexprasmlist,
  103. olddebuglist,
  104. oldinternals,
  105. oldexternals,
  106. oldconsts : paasmoutput;
  107. { akt.. things }
  108. oldaktlocalswitches : tlocalswitches;
  109. oldaktmoduleswitches : tmoduleswitches;
  110. oldaktfilepos : tfileposinfo;
  111. oldaktpackrecords : word;
  112. oldaktoutputformat : tasm;
  113. oldaktoptprocessor : tprocessors;
  114. oldaktasmmode : tasmmode;
  115. begin
  116. inc(compile_level);
  117. { save symtable state }
  118. oldsymtablestack:=symtablestack;
  119. oldrefsymtable:=refsymtable;
  120. oldmacros:=macros;
  121. oldprocprefix:=procprefix;
  122. oldaktprocsym:=aktprocsym;
  123. { save scanner state }
  124. oldc:=c;
  125. oldpattern:=pattern;
  126. oldorgpattern:=orgpattern;
  127. oldtoken:=token;
  128. old_block_type:=block_type;
  129. oldtokenpos:=tokenpos;
  130. oldcurrent_scanner:=current_scanner;
  131. { save cg }
  132. oldnextlabelnr:=nextlabelnr;
  133. oldparse_only:=parse_only;
  134. { save assembler lists }
  135. olddatasegment:=datasegment;
  136. oldbsssegment:=bsssegment;
  137. oldcodesegment:=codesegment;
  138. olddebuglist:=debuglist;
  139. oldexternals:=externals;
  140. oldinternals:=internals;
  141. oldconsts:=consts;
  142. oldrttilist:=rttilist;
  143. oldexprasmlist:=exprasmlist;
  144. oldimports:=importssection;
  145. oldexports:=exportssection;
  146. oldresource:=resourcesection;
  147. { save akt... state }
  148. oldaktlocalswitches:=aktlocalswitches;
  149. oldaktmoduleswitches:=aktmoduleswitches;
  150. oldaktpackrecords:=aktpackrecords;
  151. oldaktoutputformat:=aktoutputformat;
  152. oldaktoptprocessor:=aktoptprocessor;
  153. oldaktasmmode:=aktasmmode;
  154. oldaktfilepos:=aktfilepos;
  155. { show info }
  156. Message1(parser_i_compiling,filename);
  157. { reset symtable }
  158. symtablestack:=nil;
  159. refsymtable:=nil;
  160. procprefix:='';
  161. aktprocsym:=nil;
  162. { macros }
  163. macros:=new(psymtable,init(macrosymtable));
  164. macros^.name:=stringdup('Conditionals for '+filename);
  165. default_macros;
  166. { reset the unit or create a new program }
  167. if assigned(current_module) then
  168. begin
  169. current_module^.sourcefiles.done;
  170. current_module^.sourcefiles.init;
  171. current_module^.used_units.done;
  172. current_module^.used_units.init;
  173. current_module^.linkofiles.done;
  174. current_module^.linkofiles.init;
  175. current_module^.linkstaticlibs.done;
  176. current_module^.linkstaticlibs.init;
  177. current_module^.linksharedlibs.done;
  178. current_module^.linksharedlibs.init;
  179. end
  180. else
  181. begin
  182. current_module:=new(pmodule,init(filename,false));
  183. main_module:=current_module;
  184. end;
  185. { Load current state from the init values }
  186. aktlocalswitches:=initlocalswitches;
  187. aktmoduleswitches:=initmoduleswitches;
  188. aktpackrecords:=initpackrecords;
  189. aktpackenum:=initpackenum;
  190. aktoutputformat:=initoutputformat;
  191. aktoptprocessor:=initoptprocessor;
  192. aktasmmode:=initasmmode;
  193. { we need this to make the system unit }
  194. if compile_system then
  195. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  196. { startup scanner }
  197. current_scanner:=new(pscannerfile,Init(filename));
  198. token:=current_scanner^.yylex;
  199. { init code generator for a new module }
  200. codegen_newmodule;
  201. {$ifdef GDB}
  202. reset_gdb_info;
  203. {$endif GDB}
  204. { Handle things which need to be once }
  205. if (compile_level=1) then
  206. begin
  207. { open assembler response }
  208. AsmRes.Init('ppas');
  209. end;
  210. { load system unit always }
  211. loadsystemunit;
  212. registerdef:=true;
  213. make_ref:=true;
  214. { current return type is void }
  215. procinfo.retdef:=voiddef;
  216. { reset lexical level }
  217. lexlevel:=0;
  218. { If the compile level > 1 we get a nice "unit expected" error
  219. message if we are trying to use a program as unit.}
  220. if (token=_UNIT) or (compile_level>1) then
  221. begin
  222. current_module^.is_unit:=true;
  223. proc_unit;
  224. end
  225. else
  226. proc_program(token=_LIBRARY);
  227. if status.errorcount>0 then
  228. Message1(unit_f_errors_in_unit,tostr(status.errorcount));
  229. { clear memory }
  230. {$ifdef Splitheap}
  231. if testsplit then
  232. begin
  233. { temp heap should be empty after that !!!}
  234. codegen_donemodule;
  235. Releasetempheap;
  236. end;
  237. {$endif Splitheap}
  238. { restore old state, close trees, > 0.99.5 has heapblocks, so
  239. it's the default to release the trees }
  240. {$ifdef VER0_99_5}
  241. if dispose_asm_lists then
  242. codegen_donemodule;
  243. {$else}
  244. {$ifdef TP}
  245. if dispose_asm_lists then
  246. {$endif}
  247. codegen_donemodule;
  248. {$endif}
  249. {$ifdef GDB}
  250. reset_gdb_info;
  251. {$endif GDB}
  252. { free ppu }
  253. if assigned(current_module^.ppufile) then
  254. begin
  255. dispose(current_module^.ppufile,done);
  256. current_module^.ppufile:=nil;
  257. end;
  258. { free scanner }
  259. dispose(current_scanner,done);
  260. { free macros }
  261. {!!! No check for unused macros yet !!! }
  262. dispose(macros,done);
  263. if (compile_level>1) then
  264. begin
  265. { restore scanner }
  266. c:=oldc;
  267. pattern:=oldpattern;
  268. orgpattern:=oldorgpattern;
  269. token:=oldtoken;
  270. tokenpos:=oldtokenpos;
  271. block_type:=old_block_type;
  272. current_scanner:=oldcurrent_scanner;
  273. { restore cg }
  274. nextlabelnr:=oldnextlabelnr;
  275. parse_only:=oldparse_only;
  276. { restore asmlists }
  277. exprasmlist:=oldexprasmlist;
  278. datasegment:=olddatasegment;
  279. bsssegment:=oldbsssegment;
  280. codesegment:=oldcodesegment;
  281. consts:=oldconsts;
  282. debuglist:=olddebuglist;
  283. externals:=oldexternals;
  284. internals:=oldinternals;
  285. importssection:=oldimports;
  286. exportssection:=oldexports;
  287. resourcesection:=oldresource;
  288. rttilist:=oldrttilist;
  289. { restore symtable state }
  290. refsymtable:=oldrefsymtable;
  291. symtablestack:=oldsymtablestack;
  292. macros:=oldmacros;
  293. aktprocsym:=oldaktprocsym;
  294. procprefix:=oldprocprefix;
  295. aktlocalswitches:=oldaktlocalswitches;
  296. aktmoduleswitches:=oldaktmoduleswitches;
  297. aktpackrecords:=oldaktpackrecords;
  298. aktoutputformat:=oldaktoutputformat;
  299. aktoptprocessor:=oldaktoptprocessor;
  300. aktasmmode:=oldaktasmmode;
  301. aktfilepos:=oldaktfilepos;
  302. end;
  303. { Shut down things when the last file is compiled }
  304. if (compile_level=1) then
  305. begin
  306. { Close script }
  307. if (not AsmRes.Empty) then
  308. begin
  309. Message1(exec_i_closing_script,AsmRes.Fn);
  310. AsmRes.WriteToDisk;
  311. end;
  312. {$ifdef UseBrowser}
  313. { Write Browser }
  314. if cs_browser in aktmoduleswitches then
  315. begin
  316. Message1(parser_i_writing_browser_log,Browse.Fname);
  317. write_browser_log;
  318. end;
  319. {$endif UseBrowser}
  320. end;
  321. dec(compile_level);
  322. end;
  323. end.
  324. {
  325. $Log$
  326. Revision 1.45 1998-09-18 08:01:35 pierre
  327. + improvement on the usebrowser part
  328. (does not work correctly for now)
  329. Revision 1.44 1998/09/10 15:25:34 daniel
  330. + Added maxheapsize.
  331. * Corrected semi-bug in calling the assembler and the linker
  332. Revision 1.43 1998/09/09 15:33:06 peter
  333. * fixed in_global to allow directives also after interface token
  334. Revision 1.42 1998/09/04 08:41:59 peter
  335. * updated some error messages
  336. Revision 1.41 1998/09/01 12:53:24 peter
  337. + aktpackenum
  338. Revision 1.40 1998/09/01 07:54:19 pierre
  339. * UseBrowser a little updated (might still be buggy !!)
  340. * bug in psub.pas in function specifier removed
  341. * stdcall allowed in interface and in implementation
  342. (FPC will not yet complain if it is missing in either part
  343. because stdcall is only a dummy !!)
  344. Revision 1.39 1998/08/26 10:07:09 peter
  345. * dispose trees is now default for all > 0.99.5 compiles
  346. Revision 1.38 1998/08/18 20:52:20 peter
  347. * renamed in_main to in_global which is more logical
  348. Revision 1.37 1998/08/17 09:17:49 peter
  349. * static/shared linking updates
  350. Revision 1.36 1998/08/14 21:56:36 peter
  351. * setting the outputfile using -o works now to create static libs
  352. Revision 1.35 1998/08/12 19:22:09 peter
  353. * reset also the link* lists when recompiling an existing unit
  354. Revision 1.34 1998/08/10 23:58:56 peter
  355. * fixed asmlist dispose for 0.99.5
  356. Revision 1.33 1998/08/10 14:50:07 peter
  357. + localswitches, moduleswitches, globalswitches splitting
  358. Revision 1.32 1998/08/10 10:18:28 peter
  359. + Compiler,Comphook unit which are the new interface units to the
  360. compiler
  361. Revision 1.31 1998/07/14 21:46:46 peter
  362. * updated messages file
  363. Revision 1.30 1998/07/14 14:46:49 peter
  364. * released NEWINPUT
  365. Revision 1.29 1998/07/07 11:19:59 peter
  366. + NEWINPUT for a better inputfile and scanner object
  367. Revision 1.28 1998/06/25 11:15:33 pierre
  368. * ppu files where not closed in newppu !!
  369. second compilation was impossible due to too many opened files
  370. (not visible in 'make cycle' as we remove all the ppu files)
  371. Revision 1.27 1998/06/17 14:10:15 peter
  372. * small os2 fixes
  373. * fixed interdependent units with newppu (remake3 under linux works now)
  374. Revision 1.26 1998/06/16 08:56:23 peter
  375. + targetcpu
  376. * cleaner pmodules for newppu
  377. Revision 1.25 1998/06/15 15:38:07 pierre
  378. * small bug in systems.pas corrected
  379. + operators in different units better hanlded
  380. Revision 1.24 1998/06/13 00:10:08 peter
  381. * working browser and newppu
  382. * some small fixes against crashes which occured in bp7 (but not in
  383. fpc?!)
  384. Revision 1.23 1998/06/08 22:59:48 peter
  385. * smartlinking works for win32
  386. * some defines to exclude some compiler parts
  387. Revision 1.22 1998/06/05 17:47:28 peter
  388. * some better uses clauses
  389. Revision 1.21 1998/06/04 23:51:49 peter
  390. * m68k compiles
  391. + .def file creation moved to gendef.pas so it could also be used
  392. for win32
  393. Revision 1.20 1998/06/03 22:48:55 peter
  394. + wordbool,longbool
  395. * rename bis,von -> high,low
  396. * moved some systemunit loading/creating to psystem.pas
  397. Revision 1.19 1998/05/27 19:45:04 peter
  398. * symtable.pas splitted into includefiles
  399. * symtable adapted for $ifdef NEWPPU
  400. Revision 1.18 1998/05/23 01:21:15 peter
  401. + aktasmmode, aktoptprocessor, aktoutputformat
  402. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  403. + $LIBNAME to set the library name where the unit will be put in
  404. * splitted cgi386 a bit (codeseg to large for bp7)
  405. * nasm, tasm works again. nasm moved to ag386nsm.pas
  406. Revision 1.17 1998/05/20 09:42:34 pierre
  407. + UseTokenInfo now default
  408. * unit in interface uses and implementation uses gives error now
  409. * only one error for unknown symbol (uses lastsymknown boolean)
  410. the problem came from the label code !
  411. + first inlined procedures and function work
  412. (warning there might be allowed cases were the result is still wrong !!)
  413. * UseBrower updated gives a global list of all position of all used symbols
  414. with switch -gb
  415. Revision 1.16 1998/05/12 10:47:00 peter
  416. * moved printstatus to verb_def
  417. + V_Normal which is between V_Error and V_Warning and doesn't have a
  418. prefix like error: warning: and is included in V_Default
  419. * fixed some messages
  420. * first time parameter scan is only for -v and -T
  421. - removed old style messages
  422. Revision 1.15 1998/05/11 13:07:54 peter
  423. + $ifdef NEWPPU for the new ppuformat
  424. + $define GDB not longer required
  425. * removed all warnings and stripped some log comments
  426. * no findfirst/findnext anymore to remove smartlink *.o files
  427. Revision 1.14 1998/05/06 18:36:53 peter
  428. * tai_section extended with code,data,bss sections and enumerated type
  429. * ident 'compiled by FPC' moved to pmodules
  430. * small fix for smartlink
  431. Revision 1.13 1998/05/06 08:38:42 pierre
  432. * better position info with UseTokenInfo
  433. UseTokenInfo greatly simplified
  434. + added check for changed tree after first time firstpass
  435. (if we could remove all the cases were it happen
  436. we could skip all firstpass if firstpasscount > 1)
  437. Only with ExtDebug
  438. Revision 1.12 1998/05/04 17:54:28 peter
  439. + smartlinking works (only case jumptable left todo)
  440. * redesign of systems.pas to support assemblers and linkers
  441. + Unitname is now also in the PPU-file, increased version to 14
  442. Revision 1.11 1998/05/01 16:38:45 florian
  443. * handling of private and protected fixed
  444. + change_keywords_to_tp implemented to remove
  445. keywords which aren't supported by tp
  446. * break and continue are now symbols of the system unit
  447. + widestring, longstring and ansistring type released
  448. Revision 1.10 1998/05/01 07:43:56 florian
  449. + basics for rtti implemented
  450. + switch $m (generate rtti for published sections)
  451. Revision 1.9 1998/04/30 15:59:40 pierre
  452. * GDB works again better :
  453. correct type info in one pass
  454. + UseTokenInfo for better source position
  455. * fixed one remaining bug in scanner for line counts
  456. * several little fixes
  457. Revision 1.8 1998/04/29 10:33:55 pierre
  458. + added some code for ansistring (not complete nor working yet)
  459. * corrected operator overloading
  460. * corrected nasm output
  461. + started inline procedures
  462. + added starstarn : use ** for exponentiation (^ gave problems)
  463. + started UseTokenInfo cond to get accurate positions
  464. Revision 1.7 1998/04/27 23:10:28 peter
  465. + new scanner
  466. * $makelib -> if smartlink
  467. * small filename fixes pmodule.setfilename
  468. * moved import from files.pas -> import.pas
  469. Revision 1.6 1998/04/21 10:16:48 peter
  470. * patches from strasbourg
  471. * objects is not used anymore in the fpc compiled version
  472. Revision 1.5 1998/04/10 14:41:43 peter
  473. * removed some Hints
  474. * small speed optimization for AsmLn
  475. Revision 1.4 1998/04/08 16:58:03 pierre
  476. * several bugfixes
  477. ADD ADC and AND are also sign extended
  478. nasm output OK (program still crashes at end
  479. and creates wrong assembler files !!)
  480. procsym types sym in tdef removed !!
  481. Revision 1.3 1998/04/07 22:45:04 florian
  482. * bug0092, bug0115 and bug0121 fixed
  483. + packed object/class/array
  484. }