parser.pas 18 KB

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