parser.pas 19 KB

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