parser.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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 stacksize=0 then
  55. stacksize:=target_info.stacksize;
  56. end;
  57. procedure default_macros;
  58. var
  59. hp : pstring_item;
  60. begin
  61. { commandline }
  62. hp:=pstring_item(initdefines.first);
  63. while assigned(hp) do
  64. begin
  65. def_macro(hp^.str^);
  66. hp:=pstring_item(hp^.next);
  67. end;
  68. { set macros for version checking }
  69. set_macro('FPC_VERSION',version_nr);
  70. set_macro('FPC_RELEASE',release_nr);
  71. set_macro('FPC_PATCH',patch_nr);
  72. end;
  73. procedure compile(const filename:string;compile_system:boolean);
  74. var
  75. { scanner }
  76. oldtoken : ttoken;
  77. oldtokenpos : tfileposinfo;
  78. oldc : char;
  79. oldpattern,
  80. oldorgpattern : string;
  81. old_block_type : tblock_type;
  82. oldcurrent_scanner : pscannerfile;
  83. { symtable }
  84. oldmacros,
  85. oldrefsymtable,
  86. oldsymtablestack : psymtable;
  87. oldprocprefix : string;
  88. oldaktprocsym : pprocsym;
  89. { cg }
  90. oldnextlabelnr : longint;
  91. oldparse_only : boolean;
  92. { asmlists }
  93. oldimports,
  94. oldexports,
  95. oldresource,
  96. oldrttilist,
  97. oldbsssegment,
  98. olddatasegment,
  99. oldcodesegment,
  100. oldexprasmlist,
  101. olddebuglist,
  102. oldinternals,
  103. oldexternals,
  104. oldconsts : paasmoutput;
  105. { akt.. things }
  106. oldaktlocalswitches : tlocalswitches;
  107. oldaktmoduleswitches : tmoduleswitches;
  108. oldaktfilepos : tfileposinfo;
  109. oldaktpackrecords : word;
  110. oldaktoutputformat : tasm;
  111. oldaktoptprocessor : tprocessors;
  112. oldaktasmmode : tasmmode;
  113. begin
  114. inc(compile_level);
  115. { save symtable state }
  116. oldsymtablestack:=symtablestack;
  117. oldrefsymtable:=refsymtable;
  118. oldmacros:=macros;
  119. oldprocprefix:=procprefix;
  120. oldaktprocsym:=aktprocsym;
  121. { save scanner state }
  122. oldc:=c;
  123. oldpattern:=pattern;
  124. oldorgpattern:=orgpattern;
  125. oldtoken:=token;
  126. old_block_type:=block_type;
  127. oldtokenpos:=tokenpos;
  128. oldcurrent_scanner:=current_scanner;
  129. { save cg }
  130. oldnextlabelnr:=nextlabelnr;
  131. oldparse_only:=parse_only;
  132. { save assembler lists }
  133. olddatasegment:=datasegment;
  134. oldbsssegment:=bsssegment;
  135. oldcodesegment:=codesegment;
  136. olddebuglist:=debuglist;
  137. oldexternals:=externals;
  138. oldinternals:=internals;
  139. oldconsts:=consts;
  140. oldrttilist:=rttilist;
  141. oldexprasmlist:=exprasmlist;
  142. oldimports:=importssection;
  143. oldexports:=exportssection;
  144. oldresource:=resourcesection;
  145. { save akt... state }
  146. oldaktlocalswitches:=aktlocalswitches;
  147. oldaktmoduleswitches:=aktmoduleswitches;
  148. oldaktpackrecords:=aktpackrecords;
  149. oldaktoutputformat:=aktoutputformat;
  150. oldaktoptprocessor:=aktoptprocessor;
  151. oldaktasmmode:=aktasmmode;
  152. oldaktfilepos:=aktfilepos;
  153. { show info }
  154. Message1(parser_i_compiling,filename);
  155. { reset symtable }
  156. symtablestack:=nil;
  157. refsymtable:=nil;
  158. procprefix:='';
  159. aktprocsym:=nil;
  160. { macros }
  161. macros:=new(psymtable,init(macrosymtable));
  162. macros^.name:=stringdup('Conditionals for '+filename);
  163. default_macros;
  164. { reset the unit or create a new program }
  165. if assigned(current_module) then
  166. begin
  167. current_module^.sourcefiles.done;
  168. current_module^.sourcefiles.init;
  169. current_module^.used_units.done;
  170. current_module^.used_units.init;
  171. current_module^.linkofiles.done;
  172. current_module^.linkofiles.init;
  173. current_module^.linkstaticlibs.done;
  174. current_module^.linkstaticlibs.init;
  175. current_module^.linksharedlibs.done;
  176. current_module^.linksharedlibs.init;
  177. end
  178. else
  179. begin
  180. current_module:=new(pmodule,init(filename,false));
  181. main_module:=current_module;
  182. end;
  183. { Load current state from the init values }
  184. aktlocalswitches:=initlocalswitches;
  185. aktmoduleswitches:=initmoduleswitches;
  186. aktpackrecords:=initpackrecords;
  187. aktpackenum:=initpackenum;
  188. aktoutputformat:=initoutputformat;
  189. aktoptprocessor:=initoptprocessor;
  190. aktasmmode:=initasmmode;
  191. { we need this to make the system unit }
  192. if compile_system then
  193. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  194. { startup scanner }
  195. current_scanner:=new(pscannerfile,Init(filename));
  196. token:=current_scanner^.yylex;
  197. { global switches are read, so further changes aren't allowed }
  198. current_module^.in_global:=false;
  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. { restore scanner }
  264. c:=oldc;
  265. pattern:=oldpattern;
  266. orgpattern:=oldorgpattern;
  267. token:=oldtoken;
  268. tokenpos:=oldtokenpos;
  269. block_type:=old_block_type;
  270. current_scanner:=oldcurrent_scanner;
  271. { restore cg }
  272. nextlabelnr:=oldnextlabelnr;
  273. parse_only:=oldparse_only;
  274. { restore asmlists }
  275. exprasmlist:=oldexprasmlist;
  276. datasegment:=olddatasegment;
  277. bsssegment:=oldbsssegment;
  278. codesegment:=oldcodesegment;
  279. consts:=oldconsts;
  280. debuglist:=olddebuglist;
  281. externals:=oldexternals;
  282. internals:=oldinternals;
  283. importssection:=oldimports;
  284. exportssection:=oldexports;
  285. resourcesection:=oldresource;
  286. rttilist:=oldrttilist;
  287. { restore symtable state }
  288. if (compile_level>1) then
  289. begin
  290. refsymtable:=oldrefsymtable;
  291. symtablestack:=oldsymtablestack;
  292. end;
  293. macros:=oldmacros;
  294. aktprocsym:=oldaktprocsym;
  295. procprefix:=oldprocprefix;
  296. { restore current state }
  297. aktlocalswitches:=oldaktlocalswitches;
  298. aktmoduleswitches:=oldaktmoduleswitches;
  299. aktpackrecords:=oldaktpackrecords;
  300. aktoutputformat:=oldaktoutputformat;
  301. aktoptprocessor:=oldaktoptprocessor;
  302. aktasmmode:=oldaktasmmode;
  303. aktfilepos:=oldaktfilepos;
  304. { Shut down things when the last file is compiled }
  305. if (compile_level=1) then
  306. begin
  307. { Close script }
  308. if (not AsmRes.Empty) then
  309. begin
  310. Message1(exec_i_closing_script,AsmRes.Fn);
  311. AsmRes.WriteToDisk;
  312. end;
  313. {$ifdef UseBrowser}
  314. { Write Browser }
  315. if cs_browser in aktmoduleswitches then
  316. begin
  317. Message1(parser_i_writing_browser_log,Browse.Fname);
  318. Browse.CreateLog;
  319. write_browser_log;
  320. Browse.CloseLog;
  321. end;
  322. {$endif UseBrowser}
  323. end;
  324. dec(compile_level);
  325. end;
  326. end.
  327. {
  328. $Log$
  329. Revision 1.42 1998-09-04 08:41:59 peter
  330. * updated some error messages
  331. Revision 1.41 1998/09/01 12:53:24 peter
  332. + aktpackenum
  333. Revision 1.40 1998/09/01 07:54:19 pierre
  334. * UseBrowser a little updated (might still be buggy !!)
  335. * bug in psub.pas in function specifier removed
  336. * stdcall allowed in interface and in implementation
  337. (FPC will not yet complain if it is missing in either part
  338. because stdcall is only a dummy !!)
  339. Revision 1.39 1998/08/26 10:07:09 peter
  340. * dispose trees is now default for all > 0.99.5 compiles
  341. Revision 1.38 1998/08/18 20:52:20 peter
  342. * renamed in_main to in_global which is more logical
  343. Revision 1.37 1998/08/17 09:17:49 peter
  344. * static/shared linking updates
  345. Revision 1.36 1998/08/14 21:56:36 peter
  346. * setting the outputfile using -o works now to create static libs
  347. Revision 1.35 1998/08/12 19:22:09 peter
  348. * reset also the link* lists when recompiling an existing unit
  349. Revision 1.34 1998/08/10 23:58:56 peter
  350. * fixed asmlist dispose for 0.99.5
  351. Revision 1.33 1998/08/10 14:50:07 peter
  352. + localswitches, moduleswitches, globalswitches splitting
  353. Revision 1.32 1998/08/10 10:18:28 peter
  354. + Compiler,Comphook unit which are the new interface units to the
  355. compiler
  356. Revision 1.31 1998/07/14 21:46:46 peter
  357. * updated messages file
  358. Revision 1.30 1998/07/14 14:46:49 peter
  359. * released NEWINPUT
  360. Revision 1.29 1998/07/07 11:19:59 peter
  361. + NEWINPUT for a better inputfile and scanner object
  362. Revision 1.28 1998/06/25 11:15:33 pierre
  363. * ppu files where not closed in newppu !!
  364. second compilation was impossible due to too many opened files
  365. (not visible in 'make cycle' as we remove all the ppu files)
  366. Revision 1.27 1998/06/17 14:10:15 peter
  367. * small os2 fixes
  368. * fixed interdependent units with newppu (remake3 under linux works now)
  369. Revision 1.26 1998/06/16 08:56:23 peter
  370. + targetcpu
  371. * cleaner pmodules for newppu
  372. Revision 1.25 1998/06/15 15:38:07 pierre
  373. * small bug in systems.pas corrected
  374. + operators in different units better hanlded
  375. Revision 1.24 1998/06/13 00:10:08 peter
  376. * working browser and newppu
  377. * some small fixes against crashes which occured in bp7 (but not in
  378. fpc?!)
  379. Revision 1.23 1998/06/08 22:59:48 peter
  380. * smartlinking works for win32
  381. * some defines to exclude some compiler parts
  382. Revision 1.22 1998/06/05 17:47:28 peter
  383. * some better uses clauses
  384. Revision 1.21 1998/06/04 23:51:49 peter
  385. * m68k compiles
  386. + .def file creation moved to gendef.pas so it could also be used
  387. for win32
  388. Revision 1.20 1998/06/03 22:48:55 peter
  389. + wordbool,longbool
  390. * rename bis,von -> high,low
  391. * moved some systemunit loading/creating to psystem.pas
  392. Revision 1.19 1998/05/27 19:45:04 peter
  393. * symtable.pas splitted into includefiles
  394. * symtable adapted for $ifdef NEWPPU
  395. Revision 1.18 1998/05/23 01:21:15 peter
  396. + aktasmmode, aktoptprocessor, aktoutputformat
  397. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  398. + $LIBNAME to set the library name where the unit will be put in
  399. * splitted cgi386 a bit (codeseg to large for bp7)
  400. * nasm, tasm works again. nasm moved to ag386nsm.pas
  401. Revision 1.17 1998/05/20 09:42:34 pierre
  402. + UseTokenInfo now default
  403. * unit in interface uses and implementation uses gives error now
  404. * only one error for unknown symbol (uses lastsymknown boolean)
  405. the problem came from the label code !
  406. + first inlined procedures and function work
  407. (warning there might be allowed cases were the result is still wrong !!)
  408. * UseBrower updated gives a global list of all position of all used symbols
  409. with switch -gb
  410. Revision 1.16 1998/05/12 10:47:00 peter
  411. * moved printstatus to verb_def
  412. + V_Normal which is between V_Error and V_Warning and doesn't have a
  413. prefix like error: warning: and is included in V_Default
  414. * fixed some messages
  415. * first time parameter scan is only for -v and -T
  416. - removed old style messages
  417. Revision 1.15 1998/05/11 13:07:54 peter
  418. + $ifdef NEWPPU for the new ppuformat
  419. + $define GDB not longer required
  420. * removed all warnings and stripped some log comments
  421. * no findfirst/findnext anymore to remove smartlink *.o files
  422. Revision 1.14 1998/05/06 18:36:53 peter
  423. * tai_section extended with code,data,bss sections and enumerated type
  424. * ident 'compiled by FPC' moved to pmodules
  425. * small fix for smartlink
  426. Revision 1.13 1998/05/06 08:38:42 pierre
  427. * better position info with UseTokenInfo
  428. UseTokenInfo greatly simplified
  429. + added check for changed tree after first time firstpass
  430. (if we could remove all the cases were it happen
  431. we could skip all firstpass if firstpasscount > 1)
  432. Only with ExtDebug
  433. Revision 1.12 1998/05/04 17:54:28 peter
  434. + smartlinking works (only case jumptable left todo)
  435. * redesign of systems.pas to support assemblers and linkers
  436. + Unitname is now also in the PPU-file, increased version to 14
  437. Revision 1.11 1998/05/01 16:38:45 florian
  438. * handling of private and protected fixed
  439. + change_keywords_to_tp implemented to remove
  440. keywords which aren't supported by tp
  441. * break and continue are now symbols of the system unit
  442. + widestring, longstring and ansistring type released
  443. Revision 1.10 1998/05/01 07:43:56 florian
  444. + basics for rtti implemented
  445. + switch $m (generate rtti for published sections)
  446. Revision 1.9 1998/04/30 15:59:40 pierre
  447. * GDB works again better :
  448. correct type info in one pass
  449. + UseTokenInfo for better source position
  450. * fixed one remaining bug in scanner for line counts
  451. * several little fixes
  452. Revision 1.8 1998/04/29 10:33:55 pierre
  453. + added some code for ansistring (not complete nor working yet)
  454. * corrected operator overloading
  455. * corrected nasm output
  456. + started inline procedures
  457. + added starstarn : use ** for exponentiation (^ gave problems)
  458. + started UseTokenInfo cond to get accurate positions
  459. Revision 1.7 1998/04/27 23:10:28 peter
  460. + new scanner
  461. * $makelib -> if smartlink
  462. * small filename fixes pmodule.setfilename
  463. * moved import from files.pas -> import.pas
  464. Revision 1.6 1998/04/21 10:16:48 peter
  465. * patches from strasbourg
  466. * objects is not used anymore in the fpc compiled version
  467. Revision 1.5 1998/04/10 14:41:43 peter
  468. * removed some Hints
  469. * small speed optimization for AsmLn
  470. Revision 1.4 1998/04/08 16:58:03 pierre
  471. * several bugfixes
  472. ADD ADC and AND are also sign extended
  473. nasm output OK (program still crashes at end
  474. and creates wrong assembler files !!)
  475. procsym types sym in tdef removed !!
  476. Revision 1.3 1998/04/07 22:45:04 florian
  477. * bug0092, bug0115 and bug0121 fixed
  478. + packed object/class/array
  479. }