parser.pas 20 KB

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