parser.pas 20 KB

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