parser.pas 17 KB

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