parser.pas 21 KB

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