parser.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. {
  2. $Id$
  3. Copyright (c) 1993-99 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. { Use exception catching so the compiler goes futher after a Stop }
  23. {$ifdef i386}
  24. {$define USEEXCEPT}
  25. {$endif}
  26. {$ifdef TP}
  27. {$ifdef DPMI}
  28. {$undef USEEXCEPT}
  29. {$endif}
  30. {$endif}
  31. interface
  32. procedure preprocess(const filename:string);
  33. procedure compile(const filename:string;compile_system:boolean);
  34. procedure initparser;
  35. procedure doneparser;
  36. implementation
  37. uses
  38. globtype,version,tokens,systems,
  39. cobjects,globals,verbose,
  40. symtable,files,aasm,
  41. {$ifndef newcg}
  42. hcodegen,
  43. {$endif newcg}
  44. assemble,link,script,gendef,
  45. {$ifdef BrowserLog}
  46. browlog,
  47. {$endif BrowserLog}
  48. {$ifdef BrowserCol}
  49. browcol,
  50. {$endif BrowserCol}
  51. {$ifdef UseExcept}
  52. tpexcept,compiler,
  53. {$endif UseExcept}
  54. {$ifdef newcg}
  55. cgobj,
  56. cgcpu,
  57. { cgbase must be after hcodegen to use the correct procinfo !!! }
  58. cgbase,
  59. {$endif newcg}
  60. {$ifdef GDB}
  61. gdb,
  62. {$endif GDB}
  63. comphook,tree,scanner,pbase,ptype,psystem,pmodules,cresstr;
  64. procedure initparser;
  65. begin
  66. { ^M means a string or a char, because we don't parse a }
  67. { type declaration }
  68. ignore_equal:=false;
  69. { we didn't parse a object or class declaration }
  70. { and no function header }
  71. testcurobject:=0;
  72. { a long time, this was forgotten }
  73. aktprocsym:=nil;
  74. current_module:=nil;
  75. compiled_module:=nil;
  76. loaded_units.init;
  77. usedunits.init;
  78. { global switches }
  79. aktglobalswitches:=initglobalswitches;
  80. { scanner }
  81. c:=#0;
  82. pattern:='';
  83. orgpattern:='';
  84. current_scanner:=nil;
  85. { memory sizes }
  86. if heapsize=0 then
  87. heapsize:=target_info.heapsize;
  88. if maxheapsize=0 then
  89. maxheapsize:=target_info.maxheapsize;
  90. if stacksize=0 then
  91. stacksize:=target_info.stacksize;
  92. { open assembler response }
  93. AsmRes.Init(outputexedir+'ppas');
  94. { open deffile }
  95. DefFile.Init(outputexedir+inputfile+target_os.defext);
  96. end;
  97. procedure doneparser;
  98. begin
  99. { unload units }
  100. loaded_units.done;
  101. usedunits.done;
  102. { close ppas and deffile }
  103. asmres.done;
  104. deffile.done;
  105. end;
  106. procedure default_macros;
  107. var
  108. hp : pstring_item;
  109. begin
  110. { commandline }
  111. hp:=pstring_item(initdefines.first);
  112. while assigned(hp) do
  113. begin
  114. def_macro(hp^.str^);
  115. hp:=pstring_item(hp^.next);
  116. end;
  117. { set macros for version checking }
  118. set_macro('FPC_VERSION',version_nr);
  119. set_macro('FPC_RELEASE',release_nr);
  120. set_macro('FPC_PATCH',patch_nr);
  121. end;
  122. procedure preprocess(const filename:string);
  123. var
  124. i : longint;
  125. begin
  126. new(preprocfile,init('pre'));
  127. { default macros }
  128. macros:=new(psymtable,init(macrosymtable));
  129. macros^.name:=stringdup('Conditionals for '+filename);
  130. default_macros;
  131. { initialize a module }
  132. current_module:=new(pmodule,init(filename,false));
  133. main_module:=current_module;
  134. { startup scanner, and save in current_module }
  135. current_scanner:=new(pscannerfile,Init(filename));
  136. current_module^.scanner:=current_scanner;
  137. { loop until EOF is found }
  138. repeat
  139. current_scanner^.readtoken;
  140. preprocfile^.AddSpace;
  141. case token of
  142. _ID :
  143. begin
  144. preprocfile^.Add(orgpattern);
  145. end;
  146. _REALNUMBER,
  147. _INTCONST :
  148. preprocfile^.Add(pattern);
  149. _CSTRING :
  150. begin
  151. i:=0;
  152. while (i<length(pattern)) do
  153. begin
  154. inc(i);
  155. if pattern[i]='''' then
  156. begin
  157. insert('''',pattern,i);
  158. inc(i);
  159. end;
  160. end;
  161. preprocfile^.Add(''''+pattern+'''');
  162. end;
  163. _CCHAR :
  164. begin
  165. case pattern[1] of
  166. #39 :
  167. pattern:='''''''';
  168. #0..#31,
  169. #128..#255 :
  170. begin
  171. str(ord(pattern[1]),pattern);
  172. pattern:='#'+pattern;
  173. end;
  174. else
  175. pattern:=''''+pattern[1]+'''';
  176. end;
  177. preprocfile^.Add(pattern);
  178. end;
  179. _EOF :
  180. break;
  181. else
  182. preprocfile^.Add(tokeninfo^[token].str)
  183. end;
  184. until false;
  185. { free scanner }
  186. dispose(current_scanner,done);
  187. { close }
  188. dispose(preprocfile,done);
  189. end;
  190. procedure compile(const filename:string;compile_system:boolean);
  191. var
  192. { scanner }
  193. oldidtoken,
  194. oldtoken : ttoken;
  195. oldtokenpos : tfileposinfo;
  196. oldc : char;
  197. oldpattern,
  198. oldorgpattern : string;
  199. old_block_type : tblock_type;
  200. oldcurrent_scanner,prev_scanner,
  201. scanner : pscannerfile;
  202. { symtable }
  203. oldmacros,
  204. oldrefsymtable,
  205. olddefaultsymtablestack,
  206. oldsymtablestack : psymtable;
  207. oldprocprefix : string;
  208. oldaktprocsym : pprocsym;
  209. { cg }
  210. oldnextlabelnr : longint;
  211. oldparse_only : boolean;
  212. { asmlists }
  213. oldimports,
  214. oldexports,
  215. oldresource,
  216. oldrttilist,
  217. oldresourcestringlist,
  218. oldbsssegment,
  219. olddatasegment,
  220. oldcodesegment,
  221. oldexprasmlist,
  222. olddebuglist,
  223. oldconsts : paasmoutput;
  224. oldasmsymbollist : pasmsymbollist;
  225. { akt.. things }
  226. oldaktlocalswitches : tlocalswitches;
  227. oldaktmoduleswitches : tmoduleswitches;
  228. oldaktfilepos : tfileposinfo;
  229. oldaktpackrecords : tpackrecords;
  230. oldaktoutputformat : tasm;
  231. oldaktoptprocessor : tprocessors;
  232. oldaktasmmode : tasmmode;
  233. oldaktmodeswitches : tmodeswitches;
  234. old_compiled_module : pmodule;
  235. prev_name : pstring;
  236. {$ifdef USEEXCEPT}
  237. recoverpos : jmp_buf;
  238. oldrecoverpos : pjmp_buf;
  239. {$endif useexcept}
  240. {$ifdef newcg}
  241. oldcg : pcg;
  242. {$endif newcg}
  243. {$ifdef GDB}
  244. store_dbx : plongint;
  245. {$endif GDB}
  246. begin
  247. inc(compile_level);
  248. prev_name:=stringdup(parser_current_file);
  249. parser_current_file:=filename;
  250. old_compiled_module:=compiled_module;
  251. { save symtable state }
  252. oldsymtablestack:=symtablestack;
  253. olddefaultsymtablestack:=defaultsymtablestack;
  254. oldrefsymtable:=refsymtable;
  255. oldmacros:=macros;
  256. oldprocprefix:=procprefix;
  257. oldaktprocsym:=aktprocsym;
  258. { save scanner state }
  259. oldc:=c;
  260. oldpattern:=pattern;
  261. oldorgpattern:=orgpattern;
  262. oldtoken:=token;
  263. oldidtoken:=idtoken;
  264. old_block_type:=block_type;
  265. oldtokenpos:=tokenpos;
  266. oldcurrent_scanner:=current_scanner;
  267. { save cg }
  268. oldnextlabelnr:=nextlabelnr;
  269. oldparse_only:=parse_only;
  270. { save assembler lists }
  271. olddatasegment:=datasegment;
  272. oldbsssegment:=bsssegment;
  273. oldcodesegment:=codesegment;
  274. olddebuglist:=debuglist;
  275. oldconsts:=consts;
  276. oldrttilist:=rttilist;
  277. oldexprasmlist:=exprasmlist;
  278. oldimports:=importssection;
  279. oldexports:=exportssection;
  280. oldresource:=resourcesection;
  281. oldresourcestringlist:=resourcestringlist;
  282. oldasmsymbollist:=asmsymbollist;
  283. { save akt... state }
  284. oldaktlocalswitches:=aktlocalswitches;
  285. oldaktmoduleswitches:=aktmoduleswitches;
  286. oldaktpackrecords:=aktpackrecords;
  287. oldaktoutputformat:=aktoutputformat;
  288. oldaktoptprocessor:=aktoptprocessor;
  289. oldaktasmmode:=aktasmmode;
  290. oldaktfilepos:=aktfilepos;
  291. oldaktmodeswitches:=aktmodeswitches;
  292. {$ifdef newcg}
  293. oldcg:=cg;
  294. {$endif newcg}
  295. {$ifdef GDB}
  296. store_dbx:=dbx_counter;
  297. dbx_counter:=nil;
  298. {$endif GDB}
  299. { show info }
  300. Message1(parser_i_compiling,filename);
  301. { reset symtable }
  302. symtablestack:=nil;
  303. defaultsymtablestack:=nil;
  304. systemunit:=nil;
  305. refsymtable:=nil;
  306. aktprocsym:=nil;
  307. procprefix:='';
  308. registerdef:=true;
  309. { macros }
  310. macros:=new(psymtable,init(macrosymtable));
  311. macros^.name:=stringdup('Conditionals for '+filename);
  312. default_macros;
  313. { reset the unit or create a new program }
  314. if assigned(current_module) then
  315. begin
  316. {current_module^.reset this is wrong !! }
  317. scanner:=current_module^.scanner;
  318. current_module^.reset;
  319. current_module^.scanner:=scanner;
  320. end
  321. else
  322. begin
  323. current_module:=new(pmodule,init(filename,false));
  324. main_module:=current_module;
  325. end;
  326. compiled_module:=current_module;
  327. current_module^.in_compile:=true;
  328. { Load current state from the init values }
  329. aktlocalswitches:=initlocalswitches;
  330. aktmoduleswitches:=initmoduleswitches;
  331. aktmodeswitches:=initmodeswitches;
  332. aktpackrecords:=initpackrecords;
  333. aktpackenum:=initpackenum;
  334. aktoutputformat:=initoutputformat;
  335. aktoptprocessor:=initoptprocessor;
  336. aktasmmode:=initasmmode;
  337. { we need this to make the system unit }
  338. if compile_system then
  339. aktmoduleswitches:=aktmoduleswitches+[cs_compilesystem];
  340. { startup scanner, and save in current_module }
  341. current_scanner:=new(pscannerfile,Init(filename));
  342. current_scanner^.readtoken;
  343. prev_scanner:=current_module^.scanner;
  344. current_module^.scanner:=current_scanner;
  345. { init code generator for a new module }
  346. codegen_newmodule;
  347. {$ifdef newcg}
  348. {$ifdef i386}
  349. cg:=new(pcg386,init);
  350. {$endif i386}
  351. {$ifdef alpha}
  352. cg:=new(pcgalpha,init);
  353. {$endif alpha}
  354. {$ifdef powerpc}
  355. cg:=new(pcgppc,init);
  356. {$endif powerpc}
  357. {$endif newcg}
  358. { If the compile level > 1 we get a nice "unit expected" error
  359. message if we are trying to use a program as unit.}
  360. {$ifdef USEEXCEPT}
  361. if setjmp(recoverpos)=0 then
  362. begin
  363. oldrecoverpos:=recoverpospointer;
  364. recoverpospointer:=@recoverpos;
  365. {$endif USEEXCEPT}
  366. if (token=_UNIT) or (compile_level>1) then
  367. begin
  368. current_module^.is_unit:=true;
  369. proc_unit;
  370. end
  371. else
  372. proc_program(token=_LIBRARY);
  373. {$ifdef USEEXCEPT}
  374. recoverpospointer:=oldrecoverpos;
  375. end
  376. else
  377. begin
  378. recoverpospointer:=oldrecoverpos;
  379. longjump_used:=true;
  380. end;
  381. {$endif USEEXCEPT}
  382. { clear memory }
  383. {$ifdef Splitheap}
  384. if testsplit then
  385. begin
  386. { temp heap should be empty after that !!!}
  387. codegen_donemodule;
  388. Releasetempheap;
  389. end;
  390. {$endif Splitheap}
  391. { restore old state, close trees, > 0.99.5 has heapblocks, so
  392. it's the default to release the trees }
  393. codegen_donemodule;
  394. {$ifdef newcg}
  395. dispose(cg,done);
  396. {$endif newcg}
  397. { free ppu }
  398. if assigned(current_module^.ppufile) then
  399. begin
  400. dispose(current_module^.ppufile,done);
  401. current_module^.ppufile:=nil;
  402. end;
  403. { free scanner }
  404. dispose(current_scanner,done);
  405. { restore previous scanner !! }
  406. current_module^.scanner:=prev_scanner;
  407. if assigned(prev_scanner) then
  408. prev_scanner^.invalid:=true;
  409. { free macros }
  410. {!!! No check for unused macros yet !!! }
  411. dispose(macros,done);
  412. if (compile_level>1) then
  413. begin
  414. {$ifdef newcg}
  415. cg:=oldcg;
  416. {$endif newcg}
  417. {$ifdef GDB}
  418. dbx_counter:=store_dbx;
  419. {$endif GDB}
  420. { restore scanner }
  421. c:=oldc;
  422. pattern:=oldpattern;
  423. orgpattern:=oldorgpattern;
  424. token:=oldtoken;
  425. idtoken:=oldidtoken;
  426. tokenpos:=oldtokenpos;
  427. block_type:=old_block_type;
  428. current_scanner:=oldcurrent_scanner;
  429. { restore cg }
  430. nextlabelnr:=oldnextlabelnr;
  431. parse_only:=oldparse_only;
  432. { restore asmlists }
  433. exprasmlist:=oldexprasmlist;
  434. datasegment:=olddatasegment;
  435. bsssegment:=oldbsssegment;
  436. codesegment:=oldcodesegment;
  437. consts:=oldconsts;
  438. debuglist:=olddebuglist;
  439. importssection:=oldimports;
  440. exportssection:=oldexports;
  441. resourcesection:=oldresource;
  442. rttilist:=oldrttilist;
  443. resourcestringlist:=oldresourcestringlist;
  444. asmsymbollist:=oldasmsymbollist;
  445. { restore symtable state }
  446. refsymtable:=oldrefsymtable;
  447. symtablestack:=oldsymtablestack;
  448. defaultsymtablestack:=olddefaultsymtablestack;
  449. macros:=oldmacros;
  450. aktprocsym:=oldaktprocsym;
  451. procprefix:=oldprocprefix;
  452. aktlocalswitches:=oldaktlocalswitches;
  453. aktmoduleswitches:=oldaktmoduleswitches;
  454. aktpackrecords:=oldaktpackrecords;
  455. aktoutputformat:=oldaktoutputformat;
  456. aktoptprocessor:=oldaktoptprocessor;
  457. aktasmmode:=oldaktasmmode;
  458. aktfilepos:=oldaktfilepos;
  459. aktmodeswitches:=oldaktmodeswitches;
  460. end;
  461. { Shut down things when the last file is compiled }
  462. if (compile_level=1) then
  463. begin
  464. { Close script }
  465. if (not AsmRes.Empty) then
  466. begin
  467. Message1(exec_i_closing_script,AsmRes.Fn);
  468. AsmRes.WriteToDisk;
  469. end;
  470. {$ifdef USEEXCEPT}
  471. if not longjump_used then
  472. {$endif USEEXCEPT}
  473. { do not create browsers on errors !! }
  474. if status.errorcount=0 then
  475. begin
  476. {$ifdef BrowserLog}
  477. { Write Browser Log }
  478. if (cs_browser_log in aktglobalswitches) and
  479. (cs_browser in aktmoduleswitches) then
  480. begin
  481. if browserlog.elements_to_list^.empty then
  482. begin
  483. Message1(parser_i_writing_browser_log,browserlog.Fname);
  484. WriteBrowserLog;
  485. end
  486. else
  487. browserlog.list_elements;
  488. end;
  489. {$endif BrowserLog}
  490. {$ifdef BrowserCol}
  491. { Write Browser Collections }
  492. CreateBrowserCol;
  493. {$endif}
  494. end;
  495. if current_module^.in_second_compile then
  496. begin
  497. current_module^.in_second_compile:=false;
  498. current_module^.in_compile:=true;
  499. end
  500. else
  501. current_module^.in_compile:=false;
  502. (* Obsolete code aktprocsym
  503. is disposed by the localsymtable disposal (PM)
  504. { Free last aktprocsym }
  505. if assigned(aktprocsym) and (aktprocsym^.owner=nil) then
  506. begin
  507. { init parts are not needed in units !! }
  508. if current_module^.is_unit then
  509. aktprocsym^.definition^.forwarddef:=false;
  510. dispose(aktprocsym,done);
  511. end; *)
  512. end;
  513. dec(compile_level);
  514. parser_current_file:=prev_name^;
  515. stringdispose(prev_name);
  516. compiled_module:=old_compiled_module;
  517. {$ifdef USEEXCEPT}
  518. if longjump_used then
  519. longjmp(recoverpospointer^,1);
  520. {$endif USEEXCEPT}
  521. end;
  522. end.
  523. {
  524. $Log$
  525. Revision 1.94 1999-12-02 17:34:34 peter
  526. * preprocessor support. But it fails on the caret in type blocks
  527. Revision 1.93 1999/11/24 11:41:03 pierre
  528. * defaultsymtablestack is now restored after parser.compile
  529. Revision 1.92 1999/11/18 15:34:46 pierre
  530. * Notes/Hints for local syms changed to
  531. Set_varstate function
  532. Revision 1.91 1999/11/09 23:48:47 pierre
  533. * some DBX work, still does not work
  534. Revision 1.90 1999/11/06 14:34:21 peter
  535. * truncated log to 20 revs
  536. Revision 1.89 1999/10/22 10:39:34 peter
  537. * split type reading from pdecl to ptype unit
  538. * parameter_dec routine is now used for procedure and procvars
  539. Revision 1.88 1999/10/12 21:20:45 florian
  540. * new codegenerator compiles again
  541. Revision 1.87 1999/10/03 19:44:41 peter
  542. * removed objpasunit reference, tvarrec is now searched in systemunit
  543. where it already was located
  544. Revision 1.86 1999/10/01 08:02:45 peter
  545. * forward type declaration rewritten
  546. Revision 1.85 1999/09/16 08:02:39 pierre
  547. + old_compiled_module to avoid wrong file info when load PPU files
  548. Revision 1.84 1999/09/15 22:09:23 florian
  549. + rtti is now automatically generated for published classes, i.e.
  550. they are handled like an implicit property
  551. Revision 1.83 1999/08/31 15:51:11 pierre
  552. * in_second_compile cleaned up, in_compile and in_second_load added
  553. Revision 1.82 1999/08/26 20:24:41 michael
  554. + Hopefuly last fixes for resourcestrings
  555. Revision 1.81 1999/08/04 13:02:48 jonas
  556. * all tokens now start with an underscore
  557. * PowerPC compiles!!
  558. Revision 1.80 1999/08/03 17:09:37 florian
  559. * the alpha compiler can be compiled now
  560. Revision 1.79 1999/08/01 23:36:40 florian
  561. * some changes to compile the new code generator
  562. Revision 1.78 1999/07/24 16:22:18 michael
  563. + Improved resourcestring handling
  564. Revision 1.77 1999/07/23 16:05:22 peter
  565. * alignment is now saved in the symtable
  566. * C alignment added for records
  567. * PPU version increased to solve .12 <-> .13 probs
  568. Revision 1.76 1999/07/22 09:37:49 florian
  569. + resourcestring implemented
  570. + start of longstring support
  571. Revision 1.75 1999/06/15 13:23:48 pierre
  572. * don't generate browser if errors during compilation
  573. Revision 1.74 1999/05/27 19:44:41 peter
  574. * removed oldasm
  575. * plabel -> pasmlabel
  576. * -a switches to source writing automaticly
  577. * assembler readers OOPed
  578. * asmsymbol automaticly external
  579. * jumptables and other label fixes for asm readers
  580. Revision 1.73 1999/05/18 22:35:52 pierre
  581. * double dispose of aktprocsym removed
  582. Revision 1.72 1999/04/26 13:31:36 peter
  583. * release storenumber,double_checksum
  584. Revision 1.71 1999/03/26 00:05:33 peter
  585. * released valintern
  586. + deffile is now removed when compiling is finished
  587. * ^( compiles now correct
  588. + static directive
  589. * shrd fixed
  590. Revision 1.70 1999/03/24 23:17:10 peter
  591. * fixed bugs 212,222,225,227,229,231,233
  592. }