parser.pas 20 KB

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