parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. unit parser;
  19. {$i defines.inc}
  20. interface
  21. {$ifdef PREPROCWRITE}
  22. procedure preprocess(const filename:string);
  23. {$endif PREPROCWRITE}
  24. procedure compile(const filename:string;compile_system:boolean);
  25. procedure initparser;
  26. procedure doneparser;
  27. implementation
  28. uses
  29. cutils,cclasses,
  30. globtype,version,tokens,systems,globals,verbose,
  31. symbase,symtable,symsym,fmodule,aasm,
  32. hcodegen,
  33. script,gendef,
  34. {$ifdef BrowserLog}
  35. browlog,
  36. {$endif BrowserLog}
  37. {$ifdef UseExcept}
  38. tpexcept,
  39. {$endif UseExcept}
  40. {$ifdef GDB}
  41. gdb,
  42. {$endif GDB}
  43. comphook,scanner,pbase,ptype,pmodules,cresstr;
  44. procedure initparser;
  45. begin
  46. { ^M means a string or a char, because we don't parse a }
  47. { type declaration }
  48. ignore_equal:=false;
  49. { we didn't parse a object or class declaration }
  50. { and no function header }
  51. testcurobject:=0;
  52. { a long time, this was forgotten }
  53. aktprocsym:=nil;
  54. current_module:=nil;
  55. compiled_module:=nil;
  56. procinfo:=nil;
  57. loaded_units:=TLinkedList.Create;
  58. usedunits:=TLinkedList.Create;
  59. { global switches }
  60. aktglobalswitches:=initglobalswitches;
  61. { scanner }
  62. c:=#0;
  63. pattern:='';
  64. orgpattern:='';
  65. current_scanner:=nil;
  66. { memory sizes }
  67. if heapsize=0 then
  68. heapsize:=target_info.heapsize;
  69. if maxheapsize=0 then
  70. maxheapsize:=target_info.maxheapsize;
  71. if stacksize=0 then
  72. stacksize:=target_info.stacksize;
  73. { open assembler response }
  74. AsmRes:=TAsmScript.Create(outputexedir+'ppas');
  75. { open deffile }
  76. DefFile:=TDefFile.Create(outputexedir+inputfile+target_os.defext);
  77. { list of generated .o files, so the linker can remove them }
  78. SmartLinkOFiles:=TStringList.Create;
  79. end;
  80. procedure doneparser;
  81. begin
  82. { unload units }
  83. loaded_units.free;
  84. usedunits.free;
  85. { if there was an error in the scanner, the scanner is
  86. still assinged }
  87. if assigned(current_scanner) then
  88. begin
  89. dispose(current_scanner,done);
  90. current_scanner:=nil;
  91. end;
  92. { close ppas,deffile }
  93. asmres.free;
  94. deffile.free;
  95. { free list of .o files }
  96. SmartLinkOFiles.Free;
  97. end;
  98. procedure default_macros;
  99. var
  100. hp : tstringlistitem;
  101. begin
  102. { commandline }
  103. hp:=tstringlistitem(initdefines.first);
  104. while assigned(hp) do
  105. begin
  106. current_scanner^.def_macro(hp.str);
  107. hp:=tstringlistitem(hp.next);
  108. end;
  109. { set macros for version checking }
  110. current_scanner^.set_macro('FPC_VERSION',version_nr);
  111. current_scanner^.set_macro('FPC_RELEASE',release_nr);
  112. current_scanner^.set_macro('FPC_PATCH',patch_nr);
  113. end;
  114. {$ifdef PREPROCWRITE}
  115. procedure preprocess(const filename:string);
  116. var
  117. i : longint;
  118. begin
  119. new(preprocfile,init('pre'));
  120. { default macros }
  121. current_scanner^.macros:=new(pdictionary,init);
  122. default_macros;
  123. { initialize a module }
  124. current_module:=new(pmodule,init(filename,false));
  125. main_module:=current_module;
  126. { startup scanner, and save in current_module }
  127. current_scanner:=new(pscannerfile,Init(filename));
  128. current_module.scanner:=current_scanner;
  129. { loop until EOF is found }
  130. repeat
  131. current_scanner^.readtoken;
  132. preprocfile^.AddSpace;
  133. case token of
  134. _ID :
  135. begin
  136. preprocfile^.Add(orgpattern);
  137. end;
  138. _REALNUMBER,
  139. _INTCONST :
  140. preprocfile^.Add(pattern);
  141. _CSTRING :
  142. begin
  143. i:=0;
  144. while (i<length(pattern)) do
  145. begin
  146. inc(i);
  147. if pattern[i]='''' then
  148. begin
  149. insert('''',pattern,i);
  150. inc(i);
  151. end;
  152. end;
  153. preprocfile^.Add(''''+pattern+'''');
  154. end;
  155. _CCHAR :
  156. begin
  157. case pattern[1] of
  158. #39 :
  159. pattern:='''''''';
  160. #0..#31,
  161. #128..#255 :
  162. begin
  163. str(ord(pattern[1]),pattern);
  164. pattern:='#'+pattern;
  165. end;
  166. else
  167. pattern:=''''+pattern[1]+'''';
  168. end;
  169. preprocfile^.Add(pattern);
  170. end;
  171. _EOF :
  172. break;
  173. else
  174. preprocfile^.Add(tokeninfo^[token].str)
  175. end;
  176. until false;
  177. { free scanner }
  178. dispose(current_scanner,done);
  179. current_scanner:=nil;
  180. { close }
  181. dispose(preprocfile,done);
  182. end;
  183. {$endif PREPROCWRITE}
  184. procedure compile(const filename:string;compile_system:boolean);
  185. var
  186. { scanner }
  187. oldidtoken,
  188. oldtoken : ttoken;
  189. oldtokenpos : tfileposinfo;
  190. oldc : char;
  191. oldpattern,
  192. oldorgpattern : string;
  193. old_block_type : tblock_type;
  194. oldcurrent_scanner,prev_scanner,
  195. scanner : pscannerfile;
  196. { symtable }
  197. oldrefsymtable,
  198. olddefaultsymtablestack,
  199. oldsymtablestack : tsymtable;
  200. oldprocprefix : string;
  201. oldaktprocsym : tprocsym;
  202. oldoverloaded_operators : toverloaded_operators;
  203. { cg }
  204. oldnextlabelnr : longint;
  205. oldparse_only : boolean;
  206. { asmlists }
  207. oldimports,
  208. oldexports,
  209. oldresource,
  210. oldrttilist,
  211. oldresourcestringlist,
  212. oldbsssegment,
  213. olddatasegment,
  214. oldcodesegment,
  215. oldexprasmlist,
  216. olddebuglist,
  217. oldwithdebuglist,
  218. oldconsts : taasmoutput;
  219. oldasmsymbollist : tdictionary;
  220. { resourcestrings }
  221. OldResourceStrings : tResourceStrings;
  222. { akt.. things }
  223. oldaktlocalswitches : tlocalswitches;
  224. oldaktmoduleswitches : tmoduleswitches;
  225. oldaktfilepos : tfileposinfo;
  226. oldaktpackenum,oldaktmaxfpuregisters : longint;
  227. oldaktpackrecords : tpackrecords;
  228. oldaktoutputformat : tasm;
  229. oldaktspecificoptprocessor,
  230. oldaktoptprocessor : tprocessors;
  231. oldaktasmmode : tasmmode;
  232. oldaktinterfacetype: tinterfacetypes;
  233. oldaktmodeswitches : tmodeswitches;
  234. old_compiled_module : tmodule;
  235. prev_name : pstring;
  236. {$ifdef USEEXCEPT}
  237. {$ifndef Delphi}
  238. recoverpos : jmp_buf;
  239. oldrecoverpos : pjmp_buf;
  240. {$endif Delphi}
  241. {$endif useexcept}
  242. {$ifdef newcg}
  243. oldcg : pcg;
  244. {$endif newcg}
  245. {$ifdef GDB}
  246. store_dbx : plongint;
  247. {$endif GDB}
  248. begin
  249. inc(compile_level);
  250. prev_name:=stringdup(parser_current_file);
  251. parser_current_file:=filename;
  252. old_compiled_module:=compiled_module;
  253. { save symtable state }
  254. oldsymtablestack:=symtablestack;
  255. olddefaultsymtablestack:=defaultsymtablestack;
  256. oldrefsymtable:=refsymtable;
  257. oldprocprefix:=procprefix;
  258. oldaktprocsym:=aktprocsym;
  259. move(overloaded_operators,oldoverloaded_operators,sizeof(toverloaded_operators));
  260. { save scanner state }
  261. oldc:=c;
  262. oldpattern:=pattern;
  263. oldorgpattern:=orgpattern;
  264. oldtoken:=token;
  265. oldidtoken:=idtoken;
  266. old_block_type:=block_type;
  267. oldtokenpos:=akttokenpos;
  268. oldcurrent_scanner:=current_scanner;
  269. { save cg }
  270. oldnextlabelnr:=nextlabelnr;
  271. oldparse_only:=parse_only;
  272. { save assembler lists }
  273. olddatasegment:=datasegment;
  274. oldbsssegment:=bsssegment;
  275. oldcodesegment:=codesegment;
  276. olddebuglist:=debuglist;
  277. oldwithdebuglist:=withdebuglist;
  278. oldconsts:=consts;
  279. oldrttilist:=rttilist;
  280. oldexprasmlist:=exprasmlist;
  281. oldimports:=importssection;
  282. oldexports:=exportssection;
  283. oldresource:=resourcesection;
  284. oldresourcestringlist:=resourcestringlist;
  285. oldasmsymbollist:=asmsymbollist;
  286. OldResourceStrings:=ResourceStrings;
  287. { save akt... state }
  288. { handle the postponed case first }
  289. if localswitcheschanged then
  290. begin
  291. aktlocalswitches:=nextaktlocalswitches;
  292. localswitcheschanged:=false;
  293. end;
  294. oldaktlocalswitches:=aktlocalswitches;
  295. oldaktmoduleswitches:=aktmoduleswitches;
  296. oldaktpackrecords:=aktpackrecords;
  297. oldaktpackenum:=aktpackenum;
  298. oldaktmaxfpuregisters:=aktmaxfpuregisters;
  299. oldaktoutputformat:=aktoutputformat;
  300. oldaktoptprocessor:=aktoptprocessor;
  301. oldaktspecificoptprocessor:=aktspecificoptprocessor;
  302. oldaktasmmode:=aktasmmode;
  303. oldaktinterfacetype:=aktinterfacetype;
  304. oldaktfilepos:=aktfilepos;
  305. oldaktmodeswitches:=aktmodeswitches;
  306. {$ifdef newcg}
  307. oldcg:=cg;
  308. {$endif newcg}
  309. {$ifdef GDB}
  310. store_dbx:=dbx_counter;
  311. dbx_counter:=nil;
  312. {$endif GDB}
  313. { show info }
  314. Message1(parser_i_compiling,filename);
  315. { reset symtable }
  316. symtablestack:=nil;
  317. defaultsymtablestack:=nil;
  318. systemunit:=nil;
  319. refsymtable:=nil;
  320. aktprocsym:=nil;
  321. procprefix:='';
  322. registerdef:=true;
  323. aktmaxfpuregisters:=-1;
  324. fillchar(overloaded_operators,sizeof(toverloaded_operators),0);
  325. { reset the unit or create a new program }
  326. if assigned(current_module) then
  327. begin
  328. {current_module.reset this is wrong !! }
  329. scanner:=current_module.scanner;
  330. current_module.reset;
  331. current_module.scanner:=scanner;
  332. end
  333. else
  334. begin
  335. current_module:=tmodule.create(filename,false);
  336. main_module:=current_module;
  337. end;
  338. { a unit compiled at command line must be inside the loaded_unit list }
  339. if (compile_level=1) then
  340. loaded_units.insert(current_module);
  341. { Set the module to use for verbose }
  342. SetCompileModule(current_module);
  343. compiled_module:=current_module;
  344. current_module.in_compile:=true;
  345. { Load current state from the init values }
  346. aktlocalswitches:=initlocalswitches;
  347. aktmoduleswitches:=initmoduleswitches;
  348. aktmodeswitches:=initmodeswitches;
  349. {$IFDEF Testvarsets}
  350. aktsetalloc:=initsetalloc;
  351. {$ENDIF}
  352. aktpackrecords:=initpackrecords;
  353. aktpackenum:=initpackenum;
  354. aktoutputformat:=initoutputformat;
  355. aktoptprocessor:=initoptprocessor;
  356. aktspecificoptprocessor:=initspecificoptprocessor;
  357. aktasmmode:=initasmmode;
  358. aktinterfacetype:=initinterfacetype;
  359. { we need this to make the system unit }
  360. if compile_system then
  361. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  362. { startup scanner, and save in current_module }
  363. current_scanner:=new(pscannerfile,Init(filename));
  364. { macros }
  365. default_macros;
  366. { read the first token }
  367. current_scanner^.readtoken;
  368. prev_scanner:=current_module.scanner;
  369. current_module.scanner:=current_scanner;
  370. { init code generator for a new module }
  371. codegen_newmodule;
  372. {$ifdef newcg}
  373. {$ifdef i386}
  374. cg:=new(pcg386,init);
  375. {$endif i386}
  376. {$ifdef alpha}
  377. cg:=new(pcgalpha,init);
  378. {$endif alpha}
  379. {$ifdef powerpc}
  380. cg:=new(pcgppc,init);
  381. {$endif powerpc}
  382. {$endif newcg}
  383. { If the compile level > 1 we get a nice "unit expected" error
  384. message if we are trying to use a program as unit.}
  385. {$ifdef USEEXCEPT}
  386. if setjmp(recoverpos)=0 then
  387. begin
  388. oldrecoverpos:=recoverpospointer;
  389. recoverpospointer:=@recoverpos;
  390. {$endif USEEXCEPT}
  391. if (token=_UNIT) or (compile_level>1) then
  392. begin
  393. current_module.is_unit:=true;
  394. proc_unit;
  395. end
  396. else
  397. proc_program(token=_LIBRARY);
  398. {$ifdef USEEXCEPT}
  399. recoverpospointer:=oldrecoverpos;
  400. end
  401. else
  402. begin
  403. recoverpospointer:=oldrecoverpos;
  404. longjump_used:=true;
  405. end;
  406. {$endif USEEXCEPT}
  407. { clear memory }
  408. {$ifdef Splitheap}
  409. if testsplit then
  410. begin
  411. { temp heap should be empty after that !!!}
  412. codegen_donemodule;
  413. Releasetempheap;
  414. end;
  415. {$endif Splitheap}
  416. { restore old state, close trees, > 0.99.5 has heapblocks, so
  417. it's the default to release the trees }
  418. codegen_donemodule;
  419. {$ifdef newcg}
  420. dispose(cg,done);
  421. {$endif newcg}
  422. { free ppu }
  423. if assigned(current_module.ppufile) then
  424. begin
  425. dispose(current_module.ppufile,done);
  426. current_module.ppufile:=nil;
  427. end;
  428. { free scanner }
  429. dispose(current_scanner,done);
  430. current_scanner:=nil;
  431. { restore previous scanner !! }
  432. current_module.scanner:=prev_scanner;
  433. if assigned(prev_scanner) then
  434. prev_scanner^.invalid:=true;
  435. if (compile_level>1) then
  436. begin
  437. {$ifdef newcg}
  438. cg:=oldcg;
  439. {$endif newcg}
  440. {$ifdef GDB}
  441. dbx_counter:=store_dbx;
  442. {$endif GDB}
  443. { restore scanner }
  444. c:=oldc;
  445. pattern:=oldpattern;
  446. orgpattern:=oldorgpattern;
  447. token:=oldtoken;
  448. idtoken:=oldidtoken;
  449. akttokenpos:=oldtokenpos;
  450. block_type:=old_block_type;
  451. current_scanner:=oldcurrent_scanner;
  452. { restore cg }
  453. nextlabelnr:=oldnextlabelnr;
  454. parse_only:=oldparse_only;
  455. { restore asmlists }
  456. exprasmlist:=oldexprasmlist;
  457. datasegment:=olddatasegment;
  458. bsssegment:=oldbsssegment;
  459. codesegment:=oldcodesegment;
  460. consts:=oldconsts;
  461. debuglist:=olddebuglist;
  462. withdebuglist:=oldwithdebuglist;
  463. importssection:=oldimports;
  464. exportssection:=oldexports;
  465. resourcesection:=oldresource;
  466. rttilist:=oldrttilist;
  467. resourcestringlist:=oldresourcestringlist;
  468. asmsymbollist:=oldasmsymbollist;
  469. ResourceStrings:=OldResourceStrings;
  470. { restore symtable state }
  471. refsymtable:=oldrefsymtable;
  472. symtablestack:=oldsymtablestack;
  473. defaultsymtablestack:=olddefaultsymtablestack;
  474. aktprocsym:=oldaktprocsym;
  475. procprefix:=oldprocprefix;
  476. move(oldoverloaded_operators,overloaded_operators,sizeof(toverloaded_operators));
  477. aktlocalswitches:=oldaktlocalswitches;
  478. aktmoduleswitches:=oldaktmoduleswitches;
  479. aktpackrecords:=oldaktpackrecords;
  480. aktpackenum:=oldaktpackenum;
  481. aktmaxfpuregisters:=oldaktmaxfpuregisters;
  482. aktoutputformat:=oldaktoutputformat;
  483. aktoptprocessor:=oldaktoptprocessor;
  484. aktspecificoptprocessor:=oldaktspecificoptprocessor;
  485. aktasmmode:=oldaktasmmode;
  486. aktinterfacetype:=oldaktinterfacetype;
  487. aktfilepos:=oldaktfilepos;
  488. aktmodeswitches:=oldaktmodeswitches;
  489. end;
  490. { Shut down things when the last file is compiled }
  491. if (compile_level=1) then
  492. begin
  493. { Close script }
  494. if (not AsmRes.Empty) then
  495. begin
  496. Message1(exec_i_closing_script,AsmRes.Fn);
  497. AsmRes.WriteToDisk;
  498. end;
  499. {$ifdef USEEXCEPT}
  500. if not longjump_used then
  501. {$endif USEEXCEPT}
  502. { do not create browsers on errors !! }
  503. if status.errorcount=0 then
  504. begin
  505. {$ifdef BrowserLog}
  506. { Write Browser Log }
  507. if (cs_browser_log in aktglobalswitches) and
  508. (cs_browser in aktmoduleswitches) then
  509. begin
  510. if browserlog.elements_to_list.empty then
  511. begin
  512. Message1(parser_i_writing_browser_log,browserlog.Fname);
  513. WriteBrowserLog;
  514. end
  515. else
  516. browserlog.list_elements;
  517. end;
  518. {$endif BrowserLog}
  519. { Write Browser Collections }
  520. do_extractsymbolinfo{$ifdef FPC}(){$endif};
  521. end;
  522. if current_module.in_second_compile then
  523. begin
  524. current_module.in_second_compile:=false;
  525. current_module.in_compile:=true;
  526. end
  527. else
  528. current_module.in_compile:=false;
  529. (* Obsolete code aktprocsym
  530. is disposed by the localsymtable disposal (PM)
  531. { Free last aktprocsym }
  532. if assigned(aktprocsym) and (aktprocsym.owner=nil) then
  533. begin
  534. { init parts are not needed in units !! }
  535. if current_module.is_unit then
  536. aktprocsym.definition.forwarddef:=false;
  537. dispose(aktprocsym,done);
  538. end; *)
  539. end;
  540. dec(compile_level);
  541. parser_current_file:=prev_name^;
  542. stringdispose(prev_name);
  543. compiled_module:=old_compiled_module;
  544. {$ifdef USEEXCEPT}
  545. if longjump_used then
  546. longjmp(recoverpospointer^,1);
  547. {$endif USEEXCEPT}
  548. end;
  549. end.
  550. {
  551. $Log$
  552. Revision 1.14 2001-04-13 01:22:10 peter
  553. * symtable change to classes
  554. * range check generation and errors fixed, make cycle DEBUG=1 works
  555. * memory leaks fixed
  556. Revision 1.13 2000/12/25 00:07:27 peter
  557. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  558. tlinkedlist objects)
  559. Revision 1.12 2000/12/24 12:24:38 peter
  560. * moved preprocessfile into a conditional
  561. Revision 1.11 2000/11/29 00:30:34 florian
  562. * unused units removed from uses clause
  563. * some changes for widestrings
  564. Revision 1.10 2000/11/12 22:17:46 peter
  565. * some realname updates for messages
  566. Revision 1.9 2000/11/04 14:25:20 florian
  567. + merged Attila's changes for interfaces, not tested yet
  568. Revision 1.8 2000/10/31 22:02:49 peter
  569. * symtable splitted, no real code changes
  570. Revision 1.7 2000/10/14 10:14:51 peter
  571. * moehrendorf oct 2000 rewrite
  572. Revision 1.6 2000/10/01 19:48:25 peter
  573. * lot of compile updates for cg11
  574. Revision 1.5 2000/09/24 15:06:20 peter
  575. * use defines.inc
  576. Revision 1.4 2000/08/27 16:11:51 peter
  577. * moved some util functions from globals,cobjects to cutils
  578. * splitted files into finput,fmodule
  579. Revision 1.3 2000/08/12 15:34:22 peter
  580. + usedasmsymbollist to check and reset only the used symbols (merged)
  581. Revision 1.2 2000/07/13 11:32:44 michael
  582. + removed logs
  583. }