parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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 }
  251. dispose(current_scanner,done);
  252. { free macros }
  253. {!!! No check for unused macros yet !!! }
  254. dispose(macros,done);
  255. if (compile_level>1) then
  256. begin
  257. { restore scanner }
  258. c:=oldc;
  259. pattern:=oldpattern;
  260. orgpattern:=oldorgpattern;
  261. token:=oldtoken;
  262. idtoken:=oldidtoken;
  263. tokenpos:=oldtokenpos;
  264. block_type:=old_block_type;
  265. current_scanner:=oldcurrent_scanner;
  266. { restore cg }
  267. nextlabelnr:=oldnextlabelnr;
  268. parse_only:=oldparse_only;
  269. { restore asmlists }
  270. exprasmlist:=oldexprasmlist;
  271. datasegment:=olddatasegment;
  272. bsssegment:=oldbsssegment;
  273. codesegment:=oldcodesegment;
  274. consts:=oldconsts;
  275. debuglist:=olddebuglist;
  276. externals:=oldexternals;
  277. internals:=oldinternals;
  278. importssection:=oldimports;
  279. exportssection:=oldexports;
  280. resourcesection:=oldresource;
  281. rttilist:=oldrttilist;
  282. { restore symtable state }
  283. refsymtable:=oldrefsymtable;
  284. symtablestack:=oldsymtablestack;
  285. macros:=oldmacros;
  286. aktprocsym:=oldaktprocsym;
  287. procprefix:=oldprocprefix;
  288. aktlocalswitches:=oldaktlocalswitches;
  289. aktmoduleswitches:=oldaktmoduleswitches;
  290. aktpackrecords:=oldaktpackrecords;
  291. aktoutputformat:=oldaktoutputformat;
  292. aktoptprocessor:=oldaktoptprocessor;
  293. aktasmmode:=oldaktasmmode;
  294. aktfilepos:=oldaktfilepos;
  295. end;
  296. { Shut down things when the last file is compiled }
  297. if (compile_level=1) then
  298. begin
  299. { Close script }
  300. if (not AsmRes.Empty) then
  301. begin
  302. Message1(exec_i_closing_script,AsmRes.Fn);
  303. AsmRes.WriteToDisk;
  304. end;
  305. {$ifdef UseBrowser}
  306. { Write Browser }
  307. {$ifdef dummydebug}
  308. hp:=pmodule(loaded_units.first);
  309. while assigned(hp) do
  310. begin
  311. writeln('Unit ',hp^.modulename^,' has index ',hp^.unit_index);
  312. hp:=pmodule(hp^.next);
  313. end;
  314. {$endif dummydebug}
  315. if cs_browser in aktmoduleswitches then
  316. if Browse.elements_to_list^.empty then
  317. begin
  318. Message1(parser_i_writing_browser_log,Browse.Fname);
  319. write_browser_log;
  320. end
  321. else
  322. Browse.list_elements;
  323. {$endif UseBrowser}
  324. end;
  325. dec(compile_level);
  326. end;
  327. end.
  328. {
  329. $Log$
  330. Revision 1.55 1998-10-06 17:16:53 pierre
  331. * some memory leaks fixed (thanks to Peter for heaptrc !)
  332. Revision 1.54 1998/10/05 21:33:23 peter
  333. * fixed 161,165,166,167,168
  334. Revision 1.53 1998/09/30 16:43:36 peter
  335. * fixed unit interdependency with circular uses
  336. Revision 1.52 1998/09/28 16:57:22 pierre
  337. * changed all length(p^.value_str^) into str_length(p)
  338. to get it work with and without ansistrings
  339. * changed sourcefiles field of tmodule to a pointer
  340. Revision 1.51 1998/09/26 17:45:30 peter
  341. + idtoken and only one token table
  342. Revision 1.50 1998/09/24 23:49:08 peter
  343. + aktmodeswitches
  344. Revision 1.49 1998/09/23 15:39:07 pierre
  345. * browser bugfixes
  346. was adding a reference when looking for the symbol
  347. if -bSYM_NAME was used
  348. Revision 1.48 1998/09/22 17:13:48 pierre
  349. + browsing updated and developed
  350. records and objects fields are also stored
  351. Revision 1.47 1998/09/21 09:00:18 peter
  352. * reset_gdb_info only when debuginfo is set
  353. Revision 1.46 1998/09/21 08:45:12 pierre
  354. + added vmt_offset in tobjectdef.write for fututre use
  355. (first steps to have objects without vmt if no virtual !!)
  356. + added fpu_used field for tabstractprocdef :
  357. sets this level to 2 if the functions return with value in FPU
  358. (is then set to correct value at parsing of implementation)
  359. THIS MIGHT refuse some code with FPU expression too complex
  360. that were accepted before and even in some cases
  361. that don't overflow in fact
  362. ( like if f : float; is a forward that finally in implementation
  363. only uses one fpu register !!)
  364. Nevertheless I think that it will improve security on
  365. FPU operations !!
  366. * most other changes only for UseBrowser code
  367. (added symtable references for record and objects)
  368. local switch for refs to args and local of each function
  369. (static symtable still missing)
  370. UseBrowser still not stable and probably broken by
  371. the definition hash array !!
  372. Revision 1.45 1998/09/18 08:01:35 pierre
  373. + improvement on the usebrowser part
  374. (does not work correctly for now)
  375. Revision 1.44 1998/09/10 15:25:34 daniel
  376. + Added maxheapsize.
  377. * Corrected semi-bug in calling the assembler and the linker
  378. Revision 1.43 1998/09/09 15:33:06 peter
  379. * fixed in_global to allow directives also after interface token
  380. Revision 1.42 1998/09/04 08:41:59 peter
  381. * updated some error messages
  382. Revision 1.41 1998/09/01 12:53:24 peter
  383. + aktpackenum
  384. Revision 1.40 1998/09/01 07:54:19 pierre
  385. * UseBrowser a little updated (might still be buggy !!)
  386. * bug in psub.pas in function specifier removed
  387. * stdcall allowed in interface and in implementation
  388. (FPC will not yet complain if it is missing in either part
  389. because stdcall is only a dummy !!)
  390. Revision 1.39 1998/08/26 10:07:09 peter
  391. * dispose trees is now default for all > 0.99.5 compiles
  392. Revision 1.38 1998/08/18 20:52:20 peter
  393. * renamed in_main to in_global which is more logical
  394. Revision 1.37 1998/08/17 09:17:49 peter
  395. * static/shared linking updates
  396. Revision 1.36 1998/08/14 21:56:36 peter
  397. * setting the outputfile using -o works now to create static libs
  398. Revision 1.35 1998/08/12 19:22:09 peter
  399. * reset also the link* lists when recompiling an existing unit
  400. Revision 1.34 1998/08/10 23:58:56 peter
  401. * fixed asmlist dispose for 0.99.5
  402. Revision 1.33 1998/08/10 14:50:07 peter
  403. + localswitches, moduleswitches, globalswitches splitting
  404. Revision 1.32 1998/08/10 10:18:28 peter
  405. + Compiler,Comphook unit which are the new interface units to the
  406. compiler
  407. Revision 1.31 1998/07/14 21:46:46 peter
  408. * updated messages file
  409. Revision 1.30 1998/07/14 14:46:49 peter
  410. * released NEWINPUT
  411. Revision 1.29 1998/07/07 11:19:59 peter
  412. + NEWINPUT for a better inputfile and scanner object
  413. Revision 1.28 1998/06/25 11:15:33 pierre
  414. * ppu files where not closed in newppu !!
  415. second compilation was impossible due to too many opened files
  416. (not visible in 'make cycle' as we remove all the ppu files)
  417. Revision 1.27 1998/06/17 14:10:15 peter
  418. * small os2 fixes
  419. * fixed interdependent units with newppu (remake3 under linux works now)
  420. Revision 1.26 1998/06/16 08:56:23 peter
  421. + targetcpu
  422. * cleaner pmodules for newppu
  423. Revision 1.25 1998/06/15 15:38:07 pierre
  424. * small bug in systems.pas corrected
  425. + operators in different units better hanlded
  426. Revision 1.24 1998/06/13 00:10:08 peter
  427. * working browser and newppu
  428. * some small fixes against crashes which occured in bp7 (but not in
  429. fpc?!)
  430. Revision 1.23 1998/06/08 22:59:48 peter
  431. * smartlinking works for win32
  432. * some defines to exclude some compiler parts
  433. Revision 1.22 1998/06/05 17:47:28 peter
  434. * some better uses clauses
  435. Revision 1.21 1998/06/04 23:51:49 peter
  436. * m68k compiles
  437. + .def file creation moved to gendef.pas so it could also be used
  438. for win32
  439. Revision 1.20 1998/06/03 22:48:55 peter
  440. + wordbool,longbool
  441. * rename bis,von -> high,low
  442. * moved some systemunit loading/creating to psystem.pas
  443. Revision 1.19 1998/05/27 19:45:04 peter
  444. * symtable.pas splitted into includefiles
  445. * symtable adapted for $ifdef NEWPPU
  446. Revision 1.18 1998/05/23 01:21:15 peter
  447. + aktasmmode, aktoptprocessor, aktoutputformat
  448. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  449. + $LIBNAME to set the library name where the unit will be put in
  450. * splitted cgi386 a bit (codeseg to large for bp7)
  451. * nasm, tasm works again. nasm moved to ag386nsm.pas
  452. Revision 1.17 1998/05/20 09:42:34 pierre
  453. + UseTokenInfo now default
  454. * unit in interface uses and implementation uses gives error now
  455. * only one error for unknown symbol (uses lastsymknown boolean)
  456. the problem came from the label code !
  457. + first inlined procedures and function work
  458. (warning there might be allowed cases were the result is still wrong !!)
  459. * UseBrower updated gives a global list of all position of all used symbols
  460. with switch -gb
  461. Revision 1.16 1998/05/12 10:47:00 peter
  462. * moved printstatus to verb_def
  463. + V_Normal which is between V_Error and V_Warning and doesn't have a
  464. prefix like error: warning: and is included in V_Default
  465. * fixed some messages
  466. * first time parameter scan is only for -v and -T
  467. - removed old style messages
  468. Revision 1.15 1998/05/11 13:07:54 peter
  469. + $ifdef NEWPPU for the new ppuformat
  470. + $define GDB not longer required
  471. * removed all warnings and stripped some log comments
  472. * no findfirst/findnext anymore to remove smartlink *.o files
  473. Revision 1.14 1998/05/06 18:36:53 peter
  474. * tai_section extended with code,data,bss sections and enumerated type
  475. * ident 'compiled by FPC' moved to pmodules
  476. * small fix for smartlink
  477. Revision 1.13 1998/05/06 08:38:42 pierre
  478. * better position info with UseTokenInfo
  479. UseTokenInfo greatly simplified
  480. + added check for changed tree after first time firstpass
  481. (if we could remove all the cases were it happen
  482. we could skip all firstpass if firstpasscount > 1)
  483. Only with ExtDebug
  484. Revision 1.12 1998/05/04 17:54:28 peter
  485. + smartlinking works (only case jumptable left todo)
  486. * redesign of systems.pas to support assemblers and linkers
  487. + Unitname is now also in the PPU-file, increased version to 14
  488. Revision 1.11 1998/05/01 16:38:45 florian
  489. * handling of private and protected fixed
  490. + change_keywords_to_tp implemented to remove
  491. keywords which aren't supported by tp
  492. * break and continue are now symbols of the system unit
  493. + widestring, longstring and ansistring type released
  494. Revision 1.10 1998/05/01 07:43:56 florian
  495. + basics for rtti implemented
  496. + switch $m (generate rtti for published sections)
  497. Revision 1.9 1998/04/30 15:59:40 pierre
  498. * GDB works again better :
  499. correct type info in one pass
  500. + UseTokenInfo for better source position
  501. * fixed one remaining bug in scanner for line counts
  502. * several little fixes
  503. Revision 1.8 1998/04/29 10:33:55 pierre
  504. + added some code for ansistring (not complete nor working yet)
  505. * corrected operator overloading
  506. * corrected nasm output
  507. + started inline procedures
  508. + added starstarn : use ** for exponentiation (^ gave problems)
  509. + started UseTokenInfo cond to get accurate positions
  510. Revision 1.7 1998/04/27 23:10:28 peter
  511. + new scanner
  512. * $makelib -> if smartlink
  513. * small filename fixes pmodule.setfilename
  514. * moved import from files.pas -> import.pas
  515. Revision 1.6 1998/04/21 10:16:48 peter
  516. * patches from strasbourg
  517. * objects is not used anymore in the fpc compiled version
  518. Revision 1.5 1998/04/10 14:41:43 peter
  519. * removed some Hints
  520. * small speed optimization for AsmLn
  521. Revision 1.4 1998/04/08 16:58:03 pierre
  522. * several bugfixes
  523. ADD ADC and AND are also sign extended
  524. nasm output OK (program still crashes at end
  525. and creates wrong assembler files !!)
  526. procsym types sym in tdef removed !!
  527. Revision 1.3 1998/04/07 22:45:04 florian
  528. * bug0092, bug0115 and bug0121 fixed
  529. + packed object/class/array
  530. }