parser.pas 20 KB

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