parser.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. if Browse.elements_to_list^.empty then
  316. begin
  317. Message1(parser_i_writing_browser_log,Browse.Fname);
  318. write_browser_log;
  319. end
  320. else
  321. Browse.list_elements;
  322. {$endif UseBrowser}
  323. end;
  324. dec(compile_level);
  325. end;
  326. end.
  327. {
  328. $Log$
  329. Revision 1.46 1998-09-21 08:45:12 pierre
  330. + added vmt_offset in tobjectdef.write for fututre use
  331. (first steps to have objects without vmt if no virtual !!)
  332. + added fpu_used field for tabstractprocdef :
  333. sets this level to 2 if the functions return with value in FPU
  334. (is then set to correct value at parsing of implementation)
  335. THIS MIGHT refuse some code with FPU expression too complex
  336. that were accepted before and even in some cases
  337. that don't overflow in fact
  338. ( like if f : float; is a forward that finally in implementation
  339. only uses one fpu register !!)
  340. Nevertheless I think that it will improve security on
  341. FPU operations !!
  342. * most other changes only for UseBrowser code
  343. (added symtable references for record and objects)
  344. local switch for refs to args and local of each function
  345. (static symtable still missing)
  346. UseBrowser still not stable and probably broken by
  347. the definition hash array !!
  348. Revision 1.45 1998/09/18 08:01:35 pierre
  349. + improvement on the usebrowser part
  350. (does not work correctly for now)
  351. Revision 1.44 1998/09/10 15:25:34 daniel
  352. + Added maxheapsize.
  353. * Corrected semi-bug in calling the assembler and the linker
  354. Revision 1.43 1998/09/09 15:33:06 peter
  355. * fixed in_global to allow directives also after interface token
  356. Revision 1.42 1998/09/04 08:41:59 peter
  357. * updated some error messages
  358. Revision 1.41 1998/09/01 12:53:24 peter
  359. + aktpackenum
  360. Revision 1.40 1998/09/01 07:54:19 pierre
  361. * UseBrowser a little updated (might still be buggy !!)
  362. * bug in psub.pas in function specifier removed
  363. * stdcall allowed in interface and in implementation
  364. (FPC will not yet complain if it is missing in either part
  365. because stdcall is only a dummy !!)
  366. Revision 1.39 1998/08/26 10:07:09 peter
  367. * dispose trees is now default for all > 0.99.5 compiles
  368. Revision 1.38 1998/08/18 20:52:20 peter
  369. * renamed in_main to in_global which is more logical
  370. Revision 1.37 1998/08/17 09:17:49 peter
  371. * static/shared linking updates
  372. Revision 1.36 1998/08/14 21:56:36 peter
  373. * setting the outputfile using -o works now to create static libs
  374. Revision 1.35 1998/08/12 19:22:09 peter
  375. * reset also the link* lists when recompiling an existing unit
  376. Revision 1.34 1998/08/10 23:58:56 peter
  377. * fixed asmlist dispose for 0.99.5
  378. Revision 1.33 1998/08/10 14:50:07 peter
  379. + localswitches, moduleswitches, globalswitches splitting
  380. Revision 1.32 1998/08/10 10:18:28 peter
  381. + Compiler,Comphook unit which are the new interface units to the
  382. compiler
  383. Revision 1.31 1998/07/14 21:46:46 peter
  384. * updated messages file
  385. Revision 1.30 1998/07/14 14:46:49 peter
  386. * released NEWINPUT
  387. Revision 1.29 1998/07/07 11:19:59 peter
  388. + NEWINPUT for a better inputfile and scanner object
  389. Revision 1.28 1998/06/25 11:15:33 pierre
  390. * ppu files where not closed in newppu !!
  391. second compilation was impossible due to too many opened files
  392. (not visible in 'make cycle' as we remove all the ppu files)
  393. Revision 1.27 1998/06/17 14:10:15 peter
  394. * small os2 fixes
  395. * fixed interdependent units with newppu (remake3 under linux works now)
  396. Revision 1.26 1998/06/16 08:56:23 peter
  397. + targetcpu
  398. * cleaner pmodules for newppu
  399. Revision 1.25 1998/06/15 15:38:07 pierre
  400. * small bug in systems.pas corrected
  401. + operators in different units better hanlded
  402. Revision 1.24 1998/06/13 00:10:08 peter
  403. * working browser and newppu
  404. * some small fixes against crashes which occured in bp7 (but not in
  405. fpc?!)
  406. Revision 1.23 1998/06/08 22:59:48 peter
  407. * smartlinking works for win32
  408. * some defines to exclude some compiler parts
  409. Revision 1.22 1998/06/05 17:47:28 peter
  410. * some better uses clauses
  411. Revision 1.21 1998/06/04 23:51:49 peter
  412. * m68k compiles
  413. + .def file creation moved to gendef.pas so it could also be used
  414. for win32
  415. Revision 1.20 1998/06/03 22:48:55 peter
  416. + wordbool,longbool
  417. * rename bis,von -> high,low
  418. * moved some systemunit loading/creating to psystem.pas
  419. Revision 1.19 1998/05/27 19:45:04 peter
  420. * symtable.pas splitted into includefiles
  421. * symtable adapted for $ifdef NEWPPU
  422. Revision 1.18 1998/05/23 01:21:15 peter
  423. + aktasmmode, aktoptprocessor, aktoutputformat
  424. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  425. + $LIBNAME to set the library name where the unit will be put in
  426. * splitted cgi386 a bit (codeseg to large for bp7)
  427. * nasm, tasm works again. nasm moved to ag386nsm.pas
  428. Revision 1.17 1998/05/20 09:42:34 pierre
  429. + UseTokenInfo now default
  430. * unit in interface uses and implementation uses gives error now
  431. * only one error for unknown symbol (uses lastsymknown boolean)
  432. the problem came from the label code !
  433. + first inlined procedures and function work
  434. (warning there might be allowed cases were the result is still wrong !!)
  435. * UseBrower updated gives a global list of all position of all used symbols
  436. with switch -gb
  437. Revision 1.16 1998/05/12 10:47:00 peter
  438. * moved printstatus to verb_def
  439. + V_Normal which is between V_Error and V_Warning and doesn't have a
  440. prefix like error: warning: and is included in V_Default
  441. * fixed some messages
  442. * first time parameter scan is only for -v and -T
  443. - removed old style messages
  444. Revision 1.15 1998/05/11 13:07:54 peter
  445. + $ifdef NEWPPU for the new ppuformat
  446. + $define GDB not longer required
  447. * removed all warnings and stripped some log comments
  448. * no findfirst/findnext anymore to remove smartlink *.o files
  449. Revision 1.14 1998/05/06 18:36:53 peter
  450. * tai_section extended with code,data,bss sections and enumerated type
  451. * ident 'compiled by FPC' moved to pmodules
  452. * small fix for smartlink
  453. Revision 1.13 1998/05/06 08:38:42 pierre
  454. * better position info with UseTokenInfo
  455. UseTokenInfo greatly simplified
  456. + added check for changed tree after first time firstpass
  457. (if we could remove all the cases were it happen
  458. we could skip all firstpass if firstpasscount > 1)
  459. Only with ExtDebug
  460. Revision 1.12 1998/05/04 17:54:28 peter
  461. + smartlinking works (only case jumptable left todo)
  462. * redesign of systems.pas to support assemblers and linkers
  463. + Unitname is now also in the PPU-file, increased version to 14
  464. Revision 1.11 1998/05/01 16:38:45 florian
  465. * handling of private and protected fixed
  466. + change_keywords_to_tp implemented to remove
  467. keywords which aren't supported by tp
  468. * break and continue are now symbols of the system unit
  469. + widestring, longstring and ansistring type released
  470. Revision 1.10 1998/05/01 07:43:56 florian
  471. + basics for rtti implemented
  472. + switch $m (generate rtti for published sections)
  473. Revision 1.9 1998/04/30 15:59:40 pierre
  474. * GDB works again better :
  475. correct type info in one pass
  476. + UseTokenInfo for better source position
  477. * fixed one remaining bug in scanner for line counts
  478. * several little fixes
  479. Revision 1.8 1998/04/29 10:33:55 pierre
  480. + added some code for ansistring (not complete nor working yet)
  481. * corrected operator overloading
  482. * corrected nasm output
  483. + started inline procedures
  484. + added starstarn : use ** for exponentiation (^ gave problems)
  485. + started UseTokenInfo cond to get accurate positions
  486. Revision 1.7 1998/04/27 23:10:28 peter
  487. + new scanner
  488. * $makelib -> if smartlink
  489. * small filename fixes pmodule.setfilename
  490. * moved import from files.pas -> import.pas
  491. Revision 1.6 1998/04/21 10:16:48 peter
  492. * patches from strasbourg
  493. * objects is not used anymore in the fpc compiled version
  494. Revision 1.5 1998/04/10 14:41:43 peter
  495. * removed some Hints
  496. * small speed optimization for AsmLn
  497. Revision 1.4 1998/04/08 16:58:03 pierre
  498. * several bugfixes
  499. ADD ADC and AND are also sign extended
  500. nasm output OK (program still crashes at end
  501. and creates wrong assembler files !!)
  502. procsym types sym in tdef removed !!
  503. Revision 1.3 1998/04/07 22:45:04 florian
  504. * bug0092, bug0115 and bug0121 fixed
  505. + packed object/class/array
  506. }